It is currently Fri Nov 17, 2017 11:42 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 199 posts ]  Go to page Previous  1 ... 10, 11, 12, 13, 14
Author Message
 Post subject: Re: [PB Cocoa] Methods, Tips & Tricks
PostPosted: Fri Sep 16, 2016 11:43 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Jan 12, 2011 3:48 pm
Posts: 182
Location: Serbia
More on that (do something when user changes interface mode):

Code:
Define app = CocoaMessage(0,0,"NSApplication sharedApplication")
Define appDelegate = CocoaMessage(0,app,"delegate")
Define delegateClass = object_getClass_(appDelegate)
Define selector = sel_registerName_("darkModeChanged:")
Define distributedNotificationCenter = CocoaMessage(0,0,"NSDistributedNotificationCenter defaultCenter")

Procedure darkModeChanged(notification)
  Debug "mode changed"
EndProcedure

class_addMethod_(delegateClass,selector,@darkModeChanged(),"v@:@")
CocoaMessage(0,distributedNotificationCenter,
             "addObserver:",appDelegate,
             "selector:",selector,
             "name:$",@"AppleInterfaceThemeChangedNotification",
             "object:",#nil)

OpenWindow(0,#PB_Ignore,#PB_Ignore,100,100,"",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)

Repeat : Until WaitWindowEvent(100) = #PB_Event_CloseWindow


Again thanks wilbert for showing me how to add observers :)

_________________
PB examples collection
pb-osx-globalhotkeys
pb-osx-notifications


Top
 Profile  
Reply with quote  
 Post subject: Re: [PB Cocoa] Methods, Tips & Tricks
PostPosted: Mon Dec 12, 2016 8:22 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3000
Location: Netherlands
Small code to show how to format text of an EditorGadget with html code.
Code:
If OpenWindow(0, 0, 0, 320, 150, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
 
  EditorGadget(0, 10, 10, 300, 130)
 
 
  HTMLCode.s = "<font face='Helvetica' size='14'><b>Test</b> <font color='red'>code</font></font>"

  AttributedString = CocoaMessage(0, CocoaMessage(0, 0, "NSAttributedString alloc"), "initWithHTML:",
                                  CocoaMessage(0, CocoaMessage(0, 0, "NSString stringWithString:$", @HTMLCode),
                                               "dataUsingEncoding:", 10), "documentAttributes:", #Null)
  If AttributedString
    TextStorage = CocoaMessage(0, GadgetID(0), "textStorage")
    CocoaMessage(0, TextStorage, "setAttributedString:", AttributedString)
    CocoaMessage(0, AttributedString, "release")
  EndIf
 
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
 
EndIf

_________________
MacOS 10.13.1 High Sierra, PB 5.60 x64


Top
 Profile  
Reply with quote  
 Post subject: Re: [PB Cocoa] Methods, Tips & Tricks
PostPosted: Thu May 04, 2017 11:23 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Jan 12, 2011 3:48 pm
Posts: 182
Location: Serbia
Restarting your app (code ported from this gist)

Code:
Procedure restartApp(delay.i)
  Protected task = CocoaMessage(0,CocoaMessage(0,CocoaMessage(0,0,"NSTask alloc"),"init"),"autorelease")
  Protected args = CocoaMessage(0,0,"NSMutableArray arrayWithCapacity:",0)
  Protected appPath.s = PeekS(CocoaMessage(0,CocoaMessage(0,CocoaMessage(0,0,"NSBundle mainBundle"),"bundlePath"),"UTF8String"),-1,#PB_UTF8)
  Protected command.s = "sleep " + Str(delay) + ~"; open -a \"" + appPath + ~"\""
  CocoaMessage(0,args,"addObject:$",@"-c")
  CocoaMessage(0,args,"addObject:$",@command)
  CocoaMessage(0,task,"setLaunchPath:$",@"/bin/sh")
  CocoaMessage(0,task,"setArguments:",args)
  CocoaMessage(0,task,"launch")
  End
EndProcedure

OpenWindow(0,#PB_Ignore,#PB_Ignore,400,300,"restart app test",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
ButtonGadget(0,150,135,100,30,"restart")

Repeat
  ev = WaitWindowEvent()
  If ev = #PB_Event_Gadget And EventGadget() = 0
    restartApp(1)
  EndIf
Until ev = #PB_Event_CloseWindow

_________________
PB examples collection
pb-osx-globalhotkeys
pb-osx-notifications


Top
 Profile  
Reply with quote  
 Post subject: Re: [PB Cocoa] Methods, Tips & Tricks
PostPosted: Fri Nov 03, 2017 4:39 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3000
Location: Netherlands
Create UUID.

Method 1
Code:
ImportC ""
  CFUUIDCreate(alloc = #Null)
  CFUUIDCreateString(alloc, uuid)
EndImport

Procedure.s CreateUUID()
  Protected.i uuidRef, uuidStringRef, UUID.s
  uuidRef = CFUUIDCreate()
  uuidStringRef = CFUUIDCreateString(#Null, uuidRef)
  CFRelease_(uuidRef)
  UUID = PeekS(CocoaMessage(0, uuidStringRef, "UTF8String"), -1, #PB_UTF8)
  CFRelease_(uuidStringRef)
  ProcedureReturn UUID 
EndProcedure

Debug CreateUUID()


Method 2 (MacOS 10.8+)
Code:
UUID.s = PeekS(CocoaMessage(0, CocoaMessage(0, CocoaMessage(0, 0, "NSUUID UUID"), "UUIDString"), "UTF8String"), -1, #PB_UTF8)
Debug UUID

_________________
MacOS 10.13.1 High Sierra, PB 5.60 x64


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 199 posts ]  Go to page Previous  1 ... 10, 11, 12, 13, 14

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye