1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDialog class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "dialog.h"
17 #define XtDisplay XTDISPLAY
18 #define XtWindow XTWINDOW
19 #define XtParent XTPARENT
20 #define XtScreen XTSCREEN
23 #include "wx/dialog.h"
27 #include "wx/settings.h"
30 #pragma message disable nosimpint
34 #include <X11/Shell.h>
38 #include <Xm/MwmUtil.h>
40 #include <Xm/BulletinB.h>
43 #include <Xm/DialogS.h>
44 #include <Xm/FileSB.h>
45 #include <Xm/RowColumn.h>
46 #include <Xm/LabelG.h>
47 #include <Xm/AtomMgr.h>
49 #include <Xm/Protocols.h>
52 #pragma message enable nosimpint
55 #include "wx/motif/private.h"
57 static void wxCloseDialogCallback(Widget widget
, XtPointer client_data
, XmAnyCallbackStruct
*cbs
);
58 static void wxDialogBoxEventHandler (Widget wid
,
59 XtPointer client_data
,
61 Boolean
*continueToDispatch
);
63 static void wxUnmapBulletinBoard(Widget dialog
, wxDialog
*client
,XtPointer call
);
65 // A stack of modal_showing flags, since we can't rely
66 // on accessing wxDialog::m_modalShowing within
67 // wxDialog::Show in case a callback has deleted the wxDialog.
68 static wxList wxModalShowingStack
;
70 // Lists to keep track of windows, so we can disable/enable them
72 wxList wxModalDialogs
;
73 wxList wxModelessWindows
; // Frames and modeless dialogs
74 extern wxList wxPendingDelete
;
76 #define wxUSE_INVISIBLE_RESIZE 1
78 IMPLEMENT_DYNAMIC_CLASS(wxDialog
, wxPanel
)
80 BEGIN_EVENT_TABLE(wxDialog
, wxPanel
)
81 EVT_SIZE(wxDialog::OnSize
)
82 EVT_BUTTON(wxID_OK
, wxDialog::OnOK
)
83 EVT_BUTTON(wxID_APPLY
, wxDialog::OnApply
)
84 EVT_BUTTON(wxID_CANCEL
, wxDialog::OnCancel
)
85 EVT_CHAR_HOOK(wxDialog::OnCharHook
)
86 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged
)
87 EVT_CLOSE(wxDialog::OnCloseWindow
)
93 m_modalShowing
= FALSE
;
94 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
97 bool wxDialog::Create(wxWindow
*parent
, wxWindowID id
,
98 const wxString
& title
,
102 const wxString
& name
)
104 m_windowStyle
= style
;
105 m_modalShowing
= FALSE
;
106 m_dialogTitle
= title
;
108 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
109 m_foregroundColour
= *wxBLACK
;
114 wxTopLevelWindows
.Append(this);
116 if (parent
) parent
->AddChild(this);
119 m_windowId
= (int)NewControlId();
123 Widget parentWidget
= (Widget
) 0;
125 parentWidget
= (Widget
) parent
->GetTopWidget();
127 parentWidget
= (Widget
) wxTheApp
->GetTopLevelWidget();
129 wxASSERT_MSG( (parentWidget
!= (Widget
) 0), "Could not find a suitable parent shell for dialog." );
132 XtSetArg (args
[0], XmNdefaultPosition
, False
);
133 XtSetArg (args
[1], XmNautoUnmanage
, False
);
134 Widget dialogShell
= XmCreateBulletinBoardDialog(parentWidget
, (char*) (const char*) name
, args
, 2);
135 m_mainWidget
= (WXWidget
) dialogShell
;
137 // We don't want margins, since there is enough elsewhere.
138 XtVaSetValues(dialogShell
,
141 XmNresizePolicy
, XmRESIZE_NONE
,
144 Widget shell
= XtParent(dialogShell
) ;
147 XmString str
= XmStringCreateSimple((char*) (const char*)title
);
148 XtVaSetValues(dialogShell
,
154 m_font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
157 wxAddWindowToTable(dialogShell
, this);
159 // Intercept CLOSE messages from the window manager
160 Atom WM_DELETE_WINDOW
= XmInternAtom(XtDisplay(shell
), "WM_DELETE_WINDOW", False
);
162 /* Remove and add WM_DELETE_WINDOW so ours is only handler */
163 /* Why do we have to do this for wxDialog, but not wxFrame? */
164 XmRemoveWMProtocols(shell
, &WM_DELETE_WINDOW
, 1);
165 XmAddWMProtocols(shell
, &WM_DELETE_WINDOW
, 1);
166 XmActivateWMProtocol(shell
, WM_DELETE_WINDOW
);
168 // Modified Steve Hammes for Motif 2.0
169 #if (XmREVISION > 1 || XmVERSION > 1)
170 XmAddWMProtocolCallback(shell
, WM_DELETE_WINDOW
, (XtCallbackProc
) wxCloseDialogCallback
, (XtPointer
)this);
171 #elif XmREVISION == 1
172 XmAddWMProtocolCallback(shell
, WM_DELETE_WINDOW
, (XtCallbackProc
) wxCloseDialogCallback
, (caddr_t
)this);
174 XmAddWMProtocolCallback(shell
, WM_DELETE_WINDOW
, (void (*)())wxCloseDialogCallback
, (caddr_t
)this);
178 XtOverrideTranslations(dialogShell
,
179 ptr
= XtParseTranslationTable("<Configure>: resize()"));
182 // Can't remember what this was about... but I think it's necessary.
184 if (wxUSE_INVISIBLE_RESIZE
)
187 XtVaSetValues(dialogShell
, XmNx
, pos
.x
,
190 XtVaSetValues(dialogShell
, XmNy
, pos
.y
,
194 XtVaSetValues(dialogShell
, XmNwidth
, size
.x
, NULL
);
196 XtVaSetValues(dialogShell
, XmNheight
, size
.y
, NULL
);
199 // This patch come from Torsten Liermann lier@lier1.muc.de
200 if (XmIsMotifWMRunning(shell
))
203 if (m_windowStyle
& wxRESIZE_BORDER
)
204 decor
|= MWM_DECOR_RESIZEH
;
205 if (m_windowStyle
& wxSYSTEM_MENU
)
206 decor
|= MWM_DECOR_MENU
;
207 if ((m_windowStyle
& wxCAPTION
) ||
208 (m_windowStyle
& wxTINY_CAPTION_HORIZ
) ||
209 (m_windowStyle
& wxTINY_CAPTION_VERT
))
210 decor
|= MWM_DECOR_TITLE
;
211 if (m_windowStyle
& wxTHICK_FRAME
)
212 decor
|= MWM_DECOR_BORDER
;
213 if (m_windowStyle
& wxMINIMIZE_BOX
)
214 decor
|= MWM_DECOR_MINIMIZE
;
215 if (m_windowStyle
& wxMAXIMIZE_BOX
)
216 decor
|= MWM_DECOR_MAXIMIZE
;
218 XtVaSetValues(shell
,XmNmwmDecorations
,decor
,NULL
) ;
220 // This allows non-Motif window managers to support at least the
221 // no-decorations case.
224 if ((m_windowStyle
& wxCAPTION
) != wxCAPTION
)
225 XtVaSetValues((Widget
) shell
,XmNoverrideRedirect
,TRUE
,NULL
);
228 XtRealizeWidget(dialogShell
);
230 XtAddCallback(dialogShell
,XmNunmapCallback
,
231 (XtCallbackProc
)wxUnmapBulletinBoard
,this) ;
233 // Positioning of the dialog doesn't work properly unless the dialog
234 // is managed, so we manage without mapping to the screen.
235 // To show, we map the shell (actually it's parent).
236 if (!wxUSE_INVISIBLE_RESIZE
)
237 XtVaSetValues(shell
, XmNmappedWhenManaged
, FALSE
, NULL
);
239 if (!wxUSE_INVISIBLE_RESIZE
)
241 XtManageChild(dialogShell
);
242 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
244 XtAddEventHandler(dialogShell
,ExposureMask
,FALSE
,
245 wxUniversalRepaintProc
, (XtPointer
) this);
247 XtAddEventHandler(dialogShell
,
248 ButtonPressMask
| ButtonReleaseMask
| PointerMotionMask
| KeyPressMask
,
250 wxDialogBoxEventHandler
,
253 ChangeBackgroundColour();
258 void wxDialog::SetModal(bool flag
)
261 m_windowStyle
|= wxDIALOG_MODAL
;
263 if ( m_windowStyle
& wxDIALOG_MODAL
)
264 m_windowStyle
-= wxDIALOG_MODAL
;
266 wxModelessWindows
.DeleteObject(this);
268 wxModelessWindows
.Append(this);
271 wxDialog::~wxDialog()
273 m_isBeingDeleted
= TRUE
;
276 XtRemoveEventHandler((Widget
) m_mainWidget
, ExposureMask
, FALSE
,
277 wxUniversalRepaintProc
, (XtPointer
) this);
279 m_modalShowing
= FALSE
;
280 if (!wxUSE_INVISIBLE_RESIZE
&& m_mainWidget
)
282 XtUnmapWidget((Widget
) m_mainWidget
);
285 wxTopLevelWindows
.DeleteObject(this);
287 if ( (GetWindowStyleFlag() & wxDIALOG_MODAL
) != wxDIALOG_MODAL
)
288 wxModelessWindows
.DeleteObject(this);
290 // If this is the last top-level window, exit.
291 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
293 wxTheApp
->SetTopWindow(NULL
);
295 if (wxTheApp
->GetExitOnFrameDelete())
297 wxTheApp
->ExitMainLoop();
301 // This event-flushing code used to be in wxWindow::PostDestroyChildren (wx_dialog.cpp)
302 // but I think this should work, if we destroy the children first.
303 // Note that this might need to be done for wxFrame also.
306 // The idea about doing it here is that if you have to remove the
307 // XtDestroyWidget from ~wxWindow, at least top-level windows
308 // will still be deleted (and destroy children implicitly).
311 DetachWidget(GetMainWidget()); // Removes event handlers
312 XtDestroyWidget((Widget
) GetMainWidget());
313 SetMainWidget((WXWidget
) NULL
);
317 // By default, pressing escape cancels the dialog
318 void wxDialog::OnCharHook(wxKeyEvent
& event
)
320 if (event
.m_keyCode
== WXK_ESCAPE
)
322 // Behaviour changed in 2.0: we'll send a Cancel message
323 // to the dialog instead of Close.
324 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
325 cancelEvent
.SetEventObject( this );
326 GetEventHandler()->ProcessEvent(cancelEvent
);
330 // We didn't process this event.
334 void wxDialog::Iconize(bool WXUNUSED(iconize
))
336 // Can't iconize a dialog in Motif, apparently
337 // TODO: try using the parent of m_mainShell.
338 // XtVaSetValues((Widget) m_mainWidget, XmNiconic, iconize, NULL);
341 // Default resizing behaviour - if only ONE subwindow,
342 // resize to client rectangle size
343 void wxDialog::OnSize(wxSizeEvent
& WXUNUSED(event
))
345 // if we're using constraints - do use them
346 #if wxUSE_CONSTRAINTS
347 if ( GetAutoLayout() ) {
353 // do we have _exactly_ one child?
354 wxWindow
*child
= NULL
;
355 for ( wxNode
*node
= GetChildren().First(); node
; node
= node
->Next() )
357 wxWindow
*win
= (wxWindow
*)node
->Data();
358 if ( !win
->IsKindOf(CLASSINFO(wxFrame
)) &&
359 !win
->IsKindOf(CLASSINFO(wxDialog
)) )
362 return; // it's our second subwindow - nothing to do
368 // we have exactly one child - set it's size to fill the whole frame
369 int clientW
, clientH
;
370 GetClientSize(&clientW
, &clientH
);
375 child
->SetSize(x
, y
, clientW
, clientH
);
380 bool wxDialog::IsIconized() const
384 XtVaGetValues((Widget) m_mainWidget, XmNiconic, &iconic, NULL);
391 void wxDialog::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
393 XtVaSetValues((Widget
) m_mainWidget
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
394 wxWindow::DoSetSize(x
, y
, width
, height
, sizeFlags
);
395 XtVaSetValues((Widget
) m_mainWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
398 void wxDialog::DoSetClientSize(int width
, int height
)
400 wxWindow::SetSize(-1, -1, width
, height
);
403 void wxDialog::SetTitle(const wxString
& title
)
405 m_dialogTitle
= title
;
408 XmString str
= XmStringCreateSimple((char*) (const char*) title
);
409 XtVaSetValues((Widget
) m_mainWidget
,
410 XmNtitle
, (char*) (const char*) title
,
411 XmNdialogTitle
, str
, // Roberto Cocchi
412 XmNiconName
, (char*) (const char*) title
,
418 wxString
wxDialog::GetTitle() const
420 return m_dialogTitle
;
423 void wxDialog::Raise()
425 Window parent_window
= XtWindow((Widget
) m_mainWidget
),
426 next_parent
= XtWindow((Widget
) m_mainWidget
),
427 root
= RootWindowOfScreen(XtScreen((Widget
) m_mainWidget
));
428 // search for the parent that is child of ROOT, because the WM may
429 // reparent twice and notify only the next parent (like FVWM)
430 while (next_parent
!= root
) {
431 Window
*theChildren
; unsigned int n
;
432 parent_window
= next_parent
;
433 XQueryTree(XtDisplay((Widget
) m_mainWidget
), parent_window
, &root
,
434 &next_parent
, &theChildren
, &n
);
435 XFree(theChildren
); // not needed
437 XRaiseWindow(XtDisplay((Widget
) m_mainWidget
), parent_window
);
440 void wxDialog::Lower()
442 Window parent_window
= XtWindow((Widget
) m_mainWidget
),
443 next_parent
= XtWindow((Widget
) m_mainWidget
),
444 root
= RootWindowOfScreen(XtScreen((Widget
) m_mainWidget
));
445 // search for the parent that is child of ROOT, because the WM may
446 // reparent twice and notify only the next parent (like FVWM)
447 while (next_parent
!= root
) {
448 Window
*theChildren
; unsigned int n
;
449 parent_window
= next_parent
;
450 XQueryTree(XtDisplay((Widget
) m_mainWidget
), parent_window
, &root
,
451 &next_parent
, &theChildren
, &n
);
452 XFree(theChildren
); // not needed
454 XLowerWindow(XtDisplay((Widget
) m_mainWidget
), parent_window
);
457 bool wxDialog::Show(bool show
)
463 if (!wxUSE_INVISIBLE_RESIZE
)
464 XtMapWidget(XtParent((Widget
) m_mainWidget
));
466 XtManageChild((Widget
) m_mainWidget
) ;
468 XRaiseWindow(XtDisplay((Widget
) m_mainWidget
), XtWindow((Widget
) m_mainWidget
));
473 if (!wxUSE_INVISIBLE_RESIZE
)
474 XtUnmapWidget(XtParent((Widget
) m_mainWidget
));
476 XtUnmanageChild((Widget
) m_mainWidget
) ;
478 XFlush(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()));
479 XSync(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()), FALSE
);
485 // Shows a dialog modally, returning a return code
486 int wxDialog::ShowModal()
488 m_windowStyle
|= wxDIALOG_MODAL
;
495 wxModalShowingStack
.Insert((wxObject
*)TRUE
);
497 m_modalShowing
= TRUE
;
498 XtAddGrab((Widget
) m_mainWidget
, TRUE
, FALSE
);
502 // Loop until we signal that the dialog should be closed
503 while ((wxModalShowingStack
.Number() > 0) && ((int)(wxModalShowingStack
.First()->Data()) != 0))
505 // XtAppProcessEvent((XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
507 XtAppNextEvent((XtAppContext
) wxTheApp
->GetAppContext(), &event
);
508 wxTheApp
->ProcessXEvent((WXEvent
*) &event
);
510 if (XtAppPending( (XtAppContext
) wxTheApp
->GetAppContext() ) == 0)
512 if (!wxTheApp
->ProcessIdle())
515 // leave the main loop to give other threads a chance to
516 // perform their GUI work
525 // Remove modal dialog flag from stack
526 wxNode
*node
= wxModalShowingStack
.First();
530 // Now process all events in case they get sent to a destroyed dialog
531 XSync(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()), FALSE
);
532 while (XtAppPending((XtAppContext
) wxTheApp
->GetAppContext()))
534 XFlush(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()));
535 XtAppNextEvent((XtAppContext
) wxTheApp
->GetAppContext(), &event
);
537 wxTheApp
->ProcessXEvent((WXEvent
*) &event
);
540 // TODO: is it safe to call this, if the dialog may have been deleted
541 // by now? Probably only if we're using delayed deletion of dialogs.
542 return GetReturnCode();
545 void wxDialog::EndModal(int retCode
)
550 SetReturnCode(retCode
);
552 // Strangely, we don't seem to need this now.
553 // XtRemoveGrab((Widget) m_mainWidget);
557 m_modalShowing
= FALSE
;
559 wxNode
*node
= wxModalShowingStack
.First();
561 node
->SetData((wxObject
*)FALSE
);
565 void wxDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
567 if ( Validate() && TransferDataFromWindow() )
573 SetReturnCode(wxID_OK
);
579 void wxDialog::OnApply(wxCommandEvent
& WXUNUSED(event
))
582 TransferDataFromWindow();
583 // TODO probably need to disable the Apply button until things change again
586 void wxDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
589 EndModal(wxID_CANCEL
);
592 SetReturnCode(wxID_CANCEL
);
597 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
599 // We'll send a Cancel message by default,
600 // which may close the dialog.
601 // Check for looping if the Cancel event handler calls Close().
603 // Note that if a cancel button and handler aren't present in the dialog,
604 // nothing will happen when you close the dialog via the window manager, or
606 // We wouldn't want to destroy the dialog by default, since the dialog may have been
607 // created on the stack.
608 // However, this does mean that calling dialog->Close() won't delete the dialog
609 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
610 // sure to destroy the dialog.
611 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
613 static wxList closing
;
615 if ( closing
.Member(this) )
618 closing
.Append(this);
620 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
621 cancelEvent
.SetEventObject( this );
622 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
624 closing
.DeleteObject(this);
627 void wxDialog::OnPaint(wxPaintEvent
&WXUNUSED(event
))
629 // added for compatiblity only
632 // Destroy the window (delayed, if a managed window)
633 bool wxDialog::Destroy()
635 if (!wxPendingDelete
.Member(this))
636 wxPendingDelete
.Append(this);
640 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
642 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
646 // Handle a close event from the window manager
647 static void wxCloseDialogCallback( Widget
WXUNUSED(widget
), XtPointer client_data
,
648 XmAnyCallbackStruct
*WXUNUSED(cbs
))
650 wxDialog
*dialog
= (wxDialog
*)client_data
;
651 wxCloseEvent
closeEvent(wxEVT_CLOSE_WINDOW
, dialog
->GetId());
652 closeEvent
.SetEventObject(dialog
);
654 // May delete the dialog (with delayed deletion)
655 dialog
->GetEventHandler()->ProcessEvent(closeEvent
);
658 void wxDialogBoxEventHandler(Widget wid
,
659 XtPointer
WXUNUSED(client_data
),
661 Boolean
* continueToDispatch
)
663 wxDialog
*dialog
= (wxDialog
*)wxGetWindowFromTable(wid
);
666 wxMouseEvent
wxevent(wxEVT_NULL
);
667 if (wxTranslateMouseEvent(wxevent
, dialog
, wid
, event
))
669 wxevent
.SetEventObject(dialog
);
670 wxevent
.SetId(dialog
->GetId());
671 dialog
->GetEventHandler()->ProcessEvent(wxevent
);
675 // An attempt to implement OnCharHook by calling OnCharHook first;
676 // if this returns TRUE, set continueToDispatch to False
677 // (don't continue processing).
678 // Otherwise set it to True and call OnChar.
679 wxKeyEvent
keyEvent(wxEVT_CHAR
);
680 if (wxTranslateKeyEvent(keyEvent
, dialog
, wid
, event
))
682 keyEvent
.SetEventObject(dialog
);
683 keyEvent
.SetId(dialog
->GetId());
684 keyEvent
.SetEventType(wxEVT_CHAR_HOOK
);
685 if (dialog
->GetEventHandler()->ProcessEvent(keyEvent
))
687 *continueToDispatch
= False
;
692 // For simplicity, OnKeyDown is the same as OnChar
693 // TODO: filter modifier key presses from OnChar
694 keyEvent
.SetEventType(wxEVT_KEY_DOWN
);
696 // Only process OnChar if OnKeyDown didn't swallow it
697 if (!dialog
->GetEventHandler()->ProcessEvent (keyEvent
))
699 keyEvent
.SetEventType(wxEVT_CHAR
);
700 dialog
->GetEventHandler()->ProcessEvent(keyEvent
);
706 *continueToDispatch
= True
;
709 static void wxUnmapBulletinBoard(Widget
WXUNUSED(dialog
), wxDialog
*WXUNUSED(client
), XtPointer
WXUNUSED(call
) )
711 /* This gets called when the dialog is being shown, which
712 * defeats modal showing.
713 client->m_modalShowing = FALSE ;
714 client->m_isShown = FALSE;
718 void wxDialog::ChangeFont(bool keepOriginalSize
)
720 wxWindow::ChangeFont(keepOriginalSize
);
723 void wxDialog::ChangeBackgroundColour()
726 DoChangeBackgroundColour(GetMainWidget(), m_backgroundColour
);
729 void wxDialog::ChangeForegroundColour()
732 DoChangeForegroundColour(GetMainWidget(), m_foregroundColour
);