Page 1 of 1

[SOLVED] Display window like a drawer or a sheet

Posted: Sun Mar 09, 2014 9:00 am
by ergrull0
Hi all! I've just started to learn PureBasic on OS X and my question is: Is there a way to set some kind of window property that makes the window show like a sheet window or a drawer one using CocoaMessage?

Thanks!

Re: Display window like a drawer or a sheet

Posted: Sun Mar 09, 2014 9:27 am
by TI-994A
ergrull0 wrote:...window property that makes the window show like a sheet window or a drawer one using CocoaMessage?...
Hi ergrull0, and welcome to the wonderful world of PureBasic!

Could this be what you're looking for:

Code: Select all

OpenWindow (0, 200, 200, 300, 300, "", #PB_Window_BorderLess)
While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend
:?:

Re: Display window like a drawer or a sheet

Posted: Sun Mar 09, 2014 10:25 am
by ergrull0
TI-994A wrote:
ergrull0 wrote:...window property that makes the window show like a sheet window or a drawer one using CocoaMessage?...
Hi ergrull0, and welcome to the wonderful world of PureBasic!

Could this be what you're looking for:

Code: Select all

OpenWindow (0, 200, 200, 300, 300, "", #PB_Window_BorderLess)
While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend
:?:
Thanks for your reply! But nope. I mean something like this:

Image

or this:

Image

Re: Display window like a drawer or a sheet

Posted: Sun Mar 09, 2014 11:26 am
by Shardik
This is a very basic example for opening a drawer attached to a window:

Code: Select all

Enumeration
  #NSMinXEdge
  #NSMinYEdge
  #NSMaxXEdge
  #NSMaxYEdge
EndEnumeration

Define DrawerSize.NSSize

OpenWindow (0, 270, 100, 300, 300, "")

DrawerSize\width = 150
DrawerSize\height = 200

Drawer = CocoaMessage(0, CocoaMessage(0, 0, "NSDrawer new"),
  "initWithContentSize:@", @DrawerSize,
  "preferredEdge:", #NSMaxXEdge)

CocoaMessage(0, Drawer, "setParentWindow:", WindowID(0))
CocoaMessage(0, Drawer, "open")

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow

Re: Display window like a drawer or a sheet

Posted: Sun Mar 09, 2014 11:30 am
by ergrull0
Shardik wrote:This is a very basic example for opening a drawer attached to a window:

Code: Select all

Enumeration
  #NSMinXEdge
  #NSMinYEdge
  #NSMaxXEdge
  #NSMaxYEdge
EndEnumeration

Define DrawerSize.NSSize

OpenWindow (0, 270, 100, 300, 300, "")

DrawerSize\width = 150
DrawerSize\height = 200

Drawer = CocoaMessage(0, CocoaMessage(0, 0, "NSDrawer new"),
  "initWithContentSize:@", @DrawerSize,
  "preferredEdge:", #NSMaxXEdge)

CocoaMessage(0, Drawer, "setParentWindow:", WindowID(0))
CocoaMessage(0, Drawer, "open")

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
Nice example to work on! What about a Sheet window? Thanks!!!

Re: Display window like a drawer or a sheet

Posted: Sun Mar 09, 2014 11:42 am
by Shardik
I have modified my above drawer example to demonstrate how to place a button onto the drawer and detect a button click:

Code: Select all

Enumeration
  #NSMinXEdge
  #NSMinYEdge
  #NSMaxXEdge
  #NSMaxYEdge
EndEnumeration

Define DrawerSize.NSSize

OpenWindow (0, 270, 100, 300, 300, "")

DrawerSize\width = 150
DrawerSize\height = 200

Drawer = CocoaMessage(0, CocoaMessage(0, 0, "NSDrawer new"),
  "initWithContentSize:@", @DrawerSize,
  "preferredEdge:", #NSMaxXEdge)

CocoaMessage(0, Drawer, "setParentWindow:", WindowID(0))
CocoaMessage(0, Drawer, "open")

UseGadgetList(Drawer)
ButtonGadget(0, 20, 20, 100, 25, "Click me!")

Repeat
  Select WaitWindowEvent()
     Case #PB_Event_CloseWindow
       Break
     Case #PB_Event_Gadget
       If EventGadget() = 0
         Debug "Button in drawer was clicked!"
       EndIf
   EndSelect
ForEver

Posted: Sun Mar 09, 2014 4:28 pm
by ergrull0
double post sorry

Re: Display window like a drawer or a sheet

Posted: Sun Mar 09, 2014 4:29 pm
by ergrull0
Shardik wrote:I have modified my above drawer example to demonstrate how to place a button onto the drawer and detect a button click
Thanks for your effort! Can you please show me a similar example using a Sheet window instead of a Drawer?

Thanks again!

Re: Display window like a drawer or a sheet

Posted: Mon Mar 10, 2014 8:36 am
by wilbert
Nice example Shardik ! :)

@ergrull0,
Prior to OSX 10.9 a sheet should be started with beginSheet:modalForWindow:modalDelegate:didEndSelector:contextInfo: (NSApplication class).
For OSX 10.9+ beginSheet:completionHandler: (NSWindow class).
So the code would depend on what OSX version you want to target.

Re: Display window like a drawer or a sheet

Posted: Mon Mar 10, 2014 1:40 pm
by ergrull0
wilbert wrote:Nice example Shardik ! :)

@ergrull0,
Prior to OSX 10.9 a sheet should be started with beginSheet:modalForWindow:modalDelegate:didEndSelector:contextInfo: (NSApplication class).
For OSX 10.9+ beginSheet:completionHandler: (NSWindow class).
So the code would depend on what OSX version you want to target.
I'm still on snow leopard so an example that could run on my os version could be enough so far. :)

Re: Display window like a drawer or a sheet

Posted: Mon Mar 10, 2014 3:06 pm
by Shardik
ergrull0,

do you need a true sheet window (which will display your above posted error message when trying to close it) or do you simply need a window attached to a main window? Unfortunately I am still a Mac noob and therefore don't know the subtleties of the Mac user interface... :wink:

Here is a simple demonstration on how to attach a second window to a first one:

Code: Select all

EnableExplicit

#NSWindowAbove =  1
#NSWindowBelow = -1

OpenWindow(0, 270, 100, 200, 170, "Main window")
OpenWindow(1, 400, 130, 350, 120, "Attached window")
EditorGadget(0, 10, 10, WindowWidth(1) - 20, WindowHeight(1) - 20,
  #PB_Editor_WordWrap)
SetGadgetText(0, "This window is attached to a main window." + #CR$ + #CR$ +
  "When moving the main window, the attached window will be moved together " + 
  "with the main window. " + #CR$ +
  "But it's possible to move this attached window without moving the main " + 
  "window!")
CocoaMessage(0, WindowID(0),
  "addChildWindow:", WindowID(1),
  "ordered:", #NSWindowAbove)
SetActiveWindow(1)

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
To program a true sheet window in PureBasic is much more complex. This is a starting point which I already tried yesterday but isn't complete and therefore I didn't post it yet. I also normally still work on Snow Leopard (although having Lion, Mountain Lion and Mavericks on an external harddisk ready to boot for testing purposes). I utilized beginSheet:modalForWindow:modalDelegate:didEndSelector:contextInfo: which is replaced on Mavericks by beginSheet:completionHandler: (as already mentioned by Wilbert). You may also attach the sheet window as demonstrated by the code example above. To display a warning message as in your above screenshot, the example has to be extended by at least one callback which is called on didEndSheet:

Code: Select all

EnableExplicit

ImportC ""
  sel_registerName(MethodName.S)
EndImport

Define Selector.I = sel_registerName("didEndSheet:returnCode:contextInfo:")

OpenWindow(0, 270, 100, 200, 170, "Standard window")
OpenWindow(1, 480, 100, 200, 170, "Sheet window")
EditorGadget(0, 10, 10, WindowWidth(0) - 20, WindowHeight(0) - 20)
SetGadgetText(0, "This is a sheet window...")
SetActiveWindow(1)

; ----- Start document modal session
CocoaMessage(0, CocoaMessage(0, 0, "NSApplication sharedApplication"),
  "beginSheet:", WindowID(1),
  "modalForWindow:", WindowID(0),
  "modalDelegate:", 0,
  "didEndSelector:", Selector,
  "contextInfo:", 0)

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow

Re: Display window like a drawer or a sheet

Posted: Mon Mar 10, 2014 3:21 pm
by wilbert
Does this work on Snow Leopard ?

Code: Select all

Global NSApp = CocoaMessage(0, 0, "NSApplication sharedApplication")

Procedure BeginSheet(Sheet, ParentWindowID)
  CocoaMessage(0, NSApp, "beginSheet:", Sheet, "modalForWindow:", ParentWindowID, "modalDelegate:", #nil, "didEndSelector:", 0, "contextInfo:", 0); <= OSX 10.8
  ;CocoaMessage(0, ParentWindowID, "beginSheet:", Sheet, "completionHandler:", #nil); >= OSX 10.9
EndProcedure

Procedure EndSheet(Sheet, ParentWindowID)
  CocoaMessage(0, NSApp, "endSheet:", Sheet); <= OSX 10.8
  ;CocoaMessage(0, ParentWindowID, "endSheet:", Sheet); >= OSX 10.9
  CocoaMessage(0, Sheet, "orderOut:", #nil)
EndProcedure


OpenWindow(1, 0, 0, 140, 95, "", #PB_Window_SystemMenu | #PB_Window_Invisible); Sheet window has to be invisible
ComboBoxGadget(1, 20, 20, 100, 25)
For a = 1 To 5
  AddGadgetItem(1, -1,"ComboBox item " + Str(a))
Next
ButtonGadget(2, 20, 50, 100, 25, "Close")
Sheet = WindowID(1)

OpenWindow(0, 270, 100, 300, 300, "")
ButtonGadget(0, 20, 20, 100, 25, "Click me!")
WindowID = WindowID(0)

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 0; "Click me!" button on window clicked
          BeginSheet(Sheet, WindowID)
        Case 1
          Debug "ComboBox event"
        Case 2; "Close" button on sheet clicked
          EndSheet(Sheet, WindowID)
          Debug "ComboBox state : " + Str(GetGadgetState(1))
      EndSelect
  EndSelect
ForEver

Re: Display window like a drawer or a sheet

Posted: Mon Mar 10, 2014 6:14 pm
by Shardik
wilbert wrote:Does this work on Snow Leopard ?
Yes, your nice example works without problems on Snow Leopard and Mavericks.
On Mavericks the old beginSheet:modalForWindow:modalDelegate:didEndSelector:contextInfo: is still working although it's marked as deprecated and shouldn't be used in new code:
https://developer.apple.com/library/mac ... plication/
Apple's NSApplication Class Reference wrote:Deprecated in OS X v10.9

beginSheet:modalForWindow:modalDelegate:didEndSelector:contextInfo:

Re: Display window like a drawer or a sheet

Posted: Mon Mar 10, 2014 7:58 pm
by wilbert
Shardik wrote:On Mavericks the old beginSheet:modalForWindow:modalDelegate:didEndSelector:contextInfo: is still working although it's marked as deprecated and shouldn't be used in new code
It would be possible to use OSVersion() or respondsToSelector: but I didn't know which one would be best. :?

Re: Display window like a drawer or a sheet

Posted: Mon Mar 10, 2014 9:50 pm
by ergrull0
wilbert wrote:Does this work on Snow Leopard ?
Thanks wilbert! Just what I was looking for! I think it deserves to be added to the Cocoa methods, tips and tricks post together with the Drawer code by Shardik!