[SOLVED] Display window like a drawer or a sheet
[SOLVED] Display window like a drawer or a sheet
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!
			
			
													Thanks!
					Last edited by ergrull0 on Sat Mar 22, 2014 1:50 pm, edited 1 time in total.
									
			
									
						Re: Display window like a drawer or a sheet
Hi ergrull0, and welcome to the wonderful world of PureBasic!ergrull0 wrote:...window property that makes the window show like a sheet window or a drawer one using CocoaMessage?...
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 : WendTexas 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 
						Re: Display window like a drawer or a sheet
Thanks for your reply! But nope. I mean something like this:TI-994A wrote:Hi ergrull0, and welcome to the wonderful world of PureBasic!ergrull0 wrote:...window property that makes the window show like a sheet window or a drawer one using CocoaMessage?...
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

or this:

Re: Display window like a drawer or a sheet
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_CloseWindowRe: Display window like a drawer or a sheet
Nice example to work on! What about a Sheet window? Thanks!!!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
Re: Display window like a drawer or a sheet
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
ForEverRe: Display window like a drawer or a sheet
Thanks for your effort! Can you please show me a similar example using a Sheet window instead of a Drawer?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 again!
Re: Display window like a drawer or a sheet
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.
			
			
									
									@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)
						Raspberry Pi OS (Arm64)
Re: Display window like a drawer or a sheet
I'm still on snow leopard so an example that could run on my os version could be enough so far.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.
Re: Display window like a drawer or a sheet
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...
Here is a simple demonstration on how to attach a second window to a first one:
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:
			
			
									
									
						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...
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_CloseWindowCode: 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_CloseWindowRe: Display window like a drawer or a sheet
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)
						Raspberry Pi OS (Arm64)
Re: Display window like a drawer or a sheet
Yes, your nice example works without problems on Snow Leopard and Mavericks.wilbert wrote:Does this work on Snow Leopard ?
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
It would be possible to use OSVersion() or respondsToSelector: but I didn't know which one would be best.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
Windows (x64)
Raspberry Pi OS (Arm64)
						Raspberry Pi OS (Arm64)
Re: Display window like a drawer or a sheet
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!wilbert wrote:Does this work on Snow Leopard ?


