[SOLVED] Display window like a drawer or a sheet

Mac OSX specific forum
ergrull0
User
User
Posts: 23
Joined: Sat Mar 08, 2014 4:45 pm

[SOLVED] Display window like a drawer or a sheet

Post 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!
Last edited by ergrull0 on Sat Mar 22, 2014 1:50 pm, edited 1 time in total.
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Display window like a drawer or a sheet

Post 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
:?:
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
ergrull0
User
User
Posts: 23
Joined: Sat Mar 08, 2014 4:45 pm

Re: Display window like a drawer or a sheet

Post 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
User avatar
Shardik
Addict
Addict
Posts: 1991
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Display window like a drawer or a sheet

Post 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
ergrull0
User
User
Posts: 23
Joined: Sat Mar 08, 2014 4:45 pm

Re: Display window like a drawer or a sheet

Post 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!!!
User avatar
Shardik
Addict
Addict
Posts: 1991
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Display window like a drawer or a sheet

Post 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
ergrull0
User
User
Posts: 23
Joined: Sat Mar 08, 2014 4:45 pm

Post by ergrull0 »

double post sorry
Last edited by ergrull0 on Sun Mar 09, 2014 4:29 pm, edited 1 time in total.
ergrull0
User
User
Posts: 23
Joined: Sat Mar 08, 2014 4:45 pm

Re: Display window like a drawer or a sheet

Post 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!
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Display window like a drawer or a sheet

Post 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.
Windows (x64)
Raspberry Pi OS (Arm64)
ergrull0
User
User
Posts: 23
Joined: Sat Mar 08, 2014 4:45 pm

Re: Display window like a drawer or a sheet

Post 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. :)
User avatar
Shardik
Addict
Addict
Posts: 1991
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Display window like a drawer or a sheet

Post 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
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Display window like a drawer or a sheet

Post 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
Last edited by wilbert on Sun Sep 13, 2015 7:20 pm, edited 1 time in total.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
Shardik
Addict
Addict
Posts: 1991
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Display window like a drawer or a sheet

Post 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:
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Display window like a drawer or a sheet

Post 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. :?
Windows (x64)
Raspberry Pi OS (Arm64)
ergrull0
User
User
Posts: 23
Joined: Sat Mar 08, 2014 4:45 pm

Re: Display window like a drawer or a sheet

Post 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!
Post Reply