Looks like default behavior of NSAlert dialogs. If the alert (a modal dialog) is opened when another modal window is active, it is
positioned relative to the modal window (which makes sense somehow).
If you don't want that behavior, you can change it by centering and displaying the alert before running it in modal mode:
Code: Select all
Enumeration Commands 1000
#Command_Copy
#Command_Paste
EndEnumeration
Procedure RunModal(_window_)
#NSModalResponseContinue = -1002
; begin a modal session
Protected session = CocoaMessage(0,CocoaMessage(0,0,"NSApplication sharedApplication"),"beginModalSessionForWindow:",WindowID(_window_))
; loop while modal session is still running
While CocoaMessage(0,CocoaMessage(0,0,"NSApplication sharedApplication"),"runModalSession:",session) = #NSModalResponseContinue
WaitWindowEvent() ; PB's event processing
Wend
; end modal session
CocoaMessage(0,CocoaMessage(0,0,"NSApplication sharedApplication"),"endModalSession:",session)
EndProcedure
Macro AbortModal()
CocoaMessage(0,CocoaMessage(0,0,"NSApplication sharedApplication"),"abortModal")
EndMacro
Macro StopModal()
CocoaMessage(0,CocoaMessage(0,0,"NSApplication sharedApplication"),"stopModal")
EndMacro
Procedure OnCopy()
Debug "Copy!"
;MessageRequester("ERROR!", "I'm not centered")
alert = CocoaMessage(0,CocoaMessage(0,0,"NSAlert alloc"),"init")
CocoaMessage(0,alert,"setMessageText:$",@"Message text.")
CocoaMessage(0,alert,"setInformativeText:$",@"Informative text.")
CocoaMessage(0,alert,"addButtonWithTitle:$",@"OK")
;CocoaMessage(0,alert,"addButtonWithTitle:$",@"Cancel")
;CocoaMessage(0,alert,"addButtonWithTitle:$",@"Abort")
CocoaMessage(0,alert,"layout")
; START FIX
alertWindow = CocoaMessage(0,alert,"window")
If alertWindow
CocoaMessage(0,alertWindow,"center") ; center the window
CocoaMessage(0,alertWindow,"makeKeyAndOrderFront:",0) ; display the window (without this line, it is displayed relative to the modal window)
EndIf
; END FIX
CocoaMessage(0,alert,"runModal")
CocoaMessage(0,alert,"release")
EndProcedure
Procedure OnPaste()
Debug "Paste!"
EndProcedure
Procedure OnButton_0()
;HideWindow(1,#False) ; without this line, hidden windows are always displayed automatically, and always centered
RunModal( 1 )
EndProcedure
Procedure OnButton_1()
AbortModal() ; StopModal()
SetActiveWindow(0)
HideWindow(1,#True)
EndProcedure
If OpenWindow(0,0,0,800,600, "PureBasic Window", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ButtonGadget(0,10,10,150,25,"Open Modal Window")
BindGadgetEvent(0,@OnButton_0())
ListViewGadget(2,10,45,300,300)
For i = 1 To 200 : AddGadgetItem(2,-1,"Item "+i) : Next
AddKeyboardShortcut(0,#PB_Key_1, #Command_Copy)
AddKeyboardShortcut(0,#PB_Key_2, #Command_Paste)
BindEvent(#PB_Event_Menu, @OnCopy() , 0, #Command_Copy)
BindEvent(#PB_Event_Menu, @OnPaste(), 0, #Command_Paste)
OpenWindow(1,200,300,900,600,"Tool",#PB_Window_Tool|#PB_Window_Invisible,WindowID(0))
ButtonGadget(1,10,10,150,25,"Close Modal Window")
BindGadgetEvent(1,@OnButton_1())
AddKeyboardShortcut(1,#PB_Key_1, #Command_Copy)
AddKeyboardShortcut(1,#PB_Key_2, #Command_Paste)
BindEvent(#PB_Event_Menu, @OnCopy() , 1, #Command_Copy)
BindEvent(#PB_Event_Menu, @OnPaste(), 1, #Command_Paste)
Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow
EndIf