1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDialog class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "dialog.h"
16 #include "wx/dialog.h"
20 #include "wx/settings.h"
23 #pragma message disable nosimpint
27 #include <X11/Shell.h>
31 #include <Xm/MwmUtil.h>
33 #include <Xm/BulletinB.h>
36 #include <Xm/DialogS.h>
37 #include <Xm/FileSB.h>
38 #include <Xm/RowColumn.h>
39 #include <Xm/LabelG.h>
40 #include <Xm/AtomMgr.h>
42 #include <Xm/Protocols.h>
45 #pragma message enable nosimpint
48 #include "wx/motif/private.h"
50 static void wxCloseDialogCallback(Widget widget
, XtPointer client_data
, XmAnyCallbackStruct
*cbs
);
51 static void wxDialogBoxEventHandler (Widget wid
,
52 XtPointer client_data
,
54 Boolean
*continueToDispatch
);
56 static void wxUnmapBulletinBoard(Widget dialog
, wxDialog
*client
,XtPointer call
);
58 // A stack of modal_showing flags, since we can't rely
59 // on accessing wxDialog::m_modalShowing within
60 // wxDialog::Show in case a callback has deleted the wxDialog.
61 static wxList wxModalShowingStack
;
63 // Lists to keep track of windows, so we can disable/enable them
65 wxList wxModalDialogs
;
66 wxList wxModelessWindows
; // Frames and modeless dialogs
67 extern wxList wxPendingDelete
;
69 #define wxUSE_INVISIBLE_RESIZE 1
71 IMPLEMENT_DYNAMIC_CLASS(wxDialog
, wxPanel
)
73 BEGIN_EVENT_TABLE(wxDialog
, wxPanel
)
74 EVT_SIZE(wxDialog::OnSize
)
75 EVT_BUTTON(wxID_OK
, wxDialog::OnOK
)
76 EVT_BUTTON(wxID_APPLY
, wxDialog::OnApply
)
77 EVT_BUTTON(wxID_CANCEL
, wxDialog::OnCancel
)
78 EVT_CHAR_HOOK(wxDialog::OnCharHook
)
79 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged
)
80 EVT_CLOSE(wxDialog::OnCloseWindow
)
86 m_modalShowing
= FALSE
;
87 m_backgroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
90 bool wxDialog::Create(wxWindow
*parent
, wxWindowID id
,
91 const wxString
& title
,
97 m_windowStyle
= style
;
98 m_modalShowing
= FALSE
;
99 m_dialogTitle
= title
;
101 m_backgroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
102 m_foregroundColour
= *wxBLACK
;
107 wxTopLevelWindows
.Append(this);
109 if (parent
) parent
->AddChild(this);
112 m_windowId
= (int)NewControlId();
116 Widget parentWidget
= (Widget
) 0;
118 parentWidget
= (Widget
) parent
->GetTopWidget();
120 parentWidget
= (Widget
) wxTheApp
->GetTopLevelWidget();
122 wxASSERT_MSG( (parentWidget
!= (Widget
) 0), "Could not find a suitable parent shell for dialog." );
125 XtSetArg (args
[0], XmNdefaultPosition
, False
);
126 XtSetArg (args
[1], XmNautoUnmanage
, False
);
127 Widget dialogShell
= XmCreateBulletinBoardDialog(parentWidget
, (char*) (const char*) name
, args
, 2);
128 m_mainWidget
= (WXWidget
) dialogShell
;
130 // We don't want margins, since there is enough elsewhere.
131 XtVaSetValues(dialogShell
,
134 XmNresizePolicy
, XmRESIZE_NONE
,
137 Widget shell
= XtParent(dialogShell
) ;
140 XmString str
= XmStringCreateSimple((char*) (const char*)title
);
141 XtVaSetValues(dialogShell
,
147 m_font
= wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
);
150 wxAddWindowToTable(dialogShell
, this);
152 // Intercept CLOSE messages from the window manager
153 Atom WM_DELETE_WINDOW
= XmInternAtom(XtDisplay(shell
), "WM_DELETE_WINDOW", False
);
155 /* Remove and add WM_DELETE_WINDOW so ours is only handler */
156 /* Why do we have to do this for wxDialog, but not wxFrame? */
157 XmRemoveWMProtocols(shell
, &WM_DELETE_WINDOW
, 1);
158 XmAddWMProtocols(shell
, &WM_DELETE_WINDOW
, 1);
159 XmActivateWMProtocol(shell
, WM_DELETE_WINDOW
);
161 // Modified Steve Hammes for Motif 2.0
162 #if (XmREVISION > 1 || XmVERSION > 1)
163 XmAddWMProtocolCallback(shell
, WM_DELETE_WINDOW
, (XtCallbackProc
) wxCloseDialogCallback
, (XtPointer
)this);
164 #elif XmREVISION == 1
165 XmAddWMProtocolCallback(shell
, WM_DELETE_WINDOW
, (XtCallbackProc
) wxCloseDialogCallback
, (caddr_t
)this);
167 XmAddWMProtocolCallback(shell
, WM_DELETE_WINDOW
, (void (*)())wxCloseDialogCallback
, (caddr_t
)this);
171 XtOverrideTranslations(dialogShell
,
172 ptr
= XtParseTranslationTable("<Configure>: resize()"));
175 // Can't remember what this was about... but I think it's necessary.
177 if (wxUSE_INVISIBLE_RESIZE
)
180 XtVaSetValues(dialogShell
, XmNx
, pos
.x
,
183 XtVaSetValues(dialogShell
, XmNy
, pos
.y
,
187 XtVaSetValues(dialogShell
, XmNwidth
, size
.x
, NULL
);
189 XtVaSetValues(dialogShell
, XmNheight
, size
.y
, NULL
);
192 // This patch come from Torsten Liermann lier@lier1.muc.de
193 if (XmIsMotifWMRunning(shell
))
196 if (m_windowStyle
& wxRESIZE_BORDER
)
197 decor
|= MWM_DECOR_RESIZEH
;
198 if (m_windowStyle
& wxSYSTEM_MENU
)
199 decor
|= MWM_DECOR_MENU
;
200 if ((m_windowStyle
& wxCAPTION
) ||
201 (m_windowStyle
& wxTINY_CAPTION_HORIZ
) ||
202 (m_windowStyle
& wxTINY_CAPTION_VERT
))
203 decor
|= MWM_DECOR_TITLE
;
204 if (m_windowStyle
& wxTHICK_FRAME
)
205 decor
|= MWM_DECOR_BORDER
;
206 if (m_windowStyle
& wxMINIMIZE_BOX
)
207 decor
|= MWM_DECOR_MINIMIZE
;
208 if (m_windowStyle
& wxMAXIMIZE_BOX
)
209 decor
|= MWM_DECOR_MAXIMIZE
;
211 XtVaSetValues(shell
,XmNmwmDecorations
,decor
,NULL
) ;
213 // This allows non-Motif window managers to support at least the
214 // no-decorations case.
217 if ((m_windowStyle
& wxCAPTION
) != wxCAPTION
)
218 XtVaSetValues((Widget
) shell
,XmNoverrideRedirect
,TRUE
,NULL
);
221 XtRealizeWidget(dialogShell
);
223 XtAddCallback(dialogShell
,XmNunmapCallback
,
224 (XtCallbackProc
)wxUnmapBulletinBoard
,this) ;
226 // Positioning of the dialog doesn't work properly unless the dialog
227 // is managed, so we manage without mapping to the screen.
228 // To show, we map the shell (actually it's parent).
229 if (!wxUSE_INVISIBLE_RESIZE
)
230 XtVaSetValues(shell
, XmNmappedWhenManaged
, FALSE
, NULL
);
232 if (!wxUSE_INVISIBLE_RESIZE
)
234 XtManageChild(dialogShell
);
235 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
237 XtAddEventHandler(dialogShell
,ExposureMask
,FALSE
,
238 wxUniversalRepaintProc
, (XtPointer
) this);
240 XtAddEventHandler(dialogShell
,
241 ButtonPressMask
| ButtonReleaseMask
| PointerMotionMask
| KeyPressMask
,
243 wxDialogBoxEventHandler
,
246 ChangeBackgroundColour();
251 void wxDialog::SetModal(bool flag
)
254 m_windowStyle
|= wxDIALOG_MODAL
;
256 if ( m_windowStyle
& wxDIALOG_MODAL
)
257 m_windowStyle
-= wxDIALOG_MODAL
;
259 wxModelessWindows
.DeleteObject(this);
261 wxModelessWindows
.Append(this);
264 wxDialog::~wxDialog()
266 m_isBeingDeleted
= TRUE
;
269 XtRemoveEventHandler((Widget
) m_mainWidget
, ExposureMask
, FALSE
,
270 wxUniversalRepaintProc
, (XtPointer
) this);
272 m_modalShowing
= FALSE
;
273 if (!wxUSE_INVISIBLE_RESIZE
&& m_mainWidget
)
275 XtUnmapWidget((Widget
) m_mainWidget
);
278 wxTopLevelWindows
.DeleteObject(this);
280 if ( (GetWindowStyleFlag() & wxDIALOG_MODAL
) != wxDIALOG_MODAL
)
281 wxModelessWindows
.DeleteObject(this);
283 // If this is the last top-level window, exit.
284 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
286 wxTheApp
->SetTopWindow(NULL
);
288 if (wxTheApp
->GetExitOnFrameDelete())
290 wxTheApp
->ExitMainLoop();
294 // This event-flushing code used to be in wxWindow::PostDestroyChildren (wx_dialog.cpp)
295 // but I think this should work, if we destroy the children first.
296 // Note that this might need to be done for wxFrame also.
299 // The idea about doing it here is that if you have to remove the
300 // XtDestroyWidget from ~wxWindow, at least top-level windows
301 // will still be deleted (and destroy children implicitly).
304 DetachWidget(GetMainWidget()); // Removes event handlers
305 XtDestroyWidget((Widget
) GetMainWidget());
306 SetMainWidget((WXWidget
) NULL
);
310 // By default, pressing escape cancels the dialog
311 void wxDialog::OnCharHook(wxKeyEvent
& event
)
313 if (event
.m_keyCode
== WXK_ESCAPE
)
315 // Behaviour changed in 2.0: we'll send a Cancel message
316 // to the dialog instead of Close.
317 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
318 cancelEvent
.SetEventObject( this );
319 GetEventHandler()->ProcessEvent(cancelEvent
);
323 // We didn't process this event.
327 void wxDialog::Iconize(bool WXUNUSED(iconize
))
329 // Can't iconize a dialog in Motif, apparently
330 // TODO: try using the parent of m_mainShell.
331 // XtVaSetValues((Widget) m_mainWidget, XmNiconic, iconize, NULL);
334 // Default resizing behaviour - if only ONE subwindow,
335 // resize to client rectangle size
336 void wxDialog::OnSize(wxSizeEvent
& WXUNUSED(event
))
338 // if we're using constraints - do use them
339 #if wxUSE_CONSTRAINTS
340 if ( GetAutoLayout() ) {
346 // do we have _exactly_ one child?
347 wxWindow
*child
= NULL
;
348 for ( wxNode
*node
= GetChildren().First(); node
; node
= node
->Next() )
350 wxWindow
*win
= (wxWindow
*)node
->Data();
351 if ( !win
->IsKindOf(CLASSINFO(wxFrame
)) &&
352 !win
->IsKindOf(CLASSINFO(wxDialog
)) )
355 return; // it's our second subwindow - nothing to do
361 // we have exactly one child - set it's size to fill the whole frame
362 int clientW
, clientH
;
363 GetClientSize(&clientW
, &clientH
);
368 child
->SetSize(x
, y
, clientW
, clientH
);
373 bool wxDialog::IsIconized() const
377 XtVaGetValues((Widget) m_mainWidget, XmNiconic, &iconic, NULL);
384 void wxDialog::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
386 XtVaSetValues((Widget
) m_mainWidget
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
387 wxWindow::DoSetSize(x
, y
, width
, height
, sizeFlags
);
388 XtVaSetValues((Widget
) m_mainWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
391 void wxDialog::DoSetClientSize(int width
, int height
)
393 wxWindow::SetSize(-1, -1, width
, height
);
396 void wxDialog::SetTitle(const wxString
& title
)
398 m_dialogTitle
= title
;
401 XmString str
= XmStringCreateSimple((char*) (const char*) title
);
402 XtVaSetValues((Widget
) m_mainWidget
,
403 XmNtitle
, (char*) (const char*) title
,
404 XmNdialogTitle
, str
, // Roberto Cocchi
405 XmNiconName
, (char*) (const char*) title
,
411 wxString
wxDialog::GetTitle() const
413 return m_dialogTitle
;
416 void wxDialog::Raise()
418 Window parent_window
= XtWindow((Widget
) m_mainWidget
),
419 next_parent
= XtWindow((Widget
) m_mainWidget
),
420 root
= RootWindowOfScreen(XtScreen((Widget
) m_mainWidget
));
421 // search for the parent that is child of ROOT, because the WM may
422 // reparent twice and notify only the next parent (like FVWM)
423 while (next_parent
!= root
) {
424 Window
*theChildren
; unsigned int n
;
425 parent_window
= next_parent
;
426 XQueryTree(XtDisplay((Widget
) m_mainWidget
), parent_window
, &root
,
427 &next_parent
, &theChildren
, &n
);
428 XFree(theChildren
); // not needed
430 XRaiseWindow(XtDisplay((Widget
) m_mainWidget
), parent_window
);
433 void wxDialog::Lower()
435 Window parent_window
= XtWindow((Widget
) m_mainWidget
),
436 next_parent
= XtWindow((Widget
) m_mainWidget
),
437 root
= RootWindowOfScreen(XtScreen((Widget
) m_mainWidget
));
438 // search for the parent that is child of ROOT, because the WM may
439 // reparent twice and notify only the next parent (like FVWM)
440 while (next_parent
!= root
) {
441 Window
*theChildren
; unsigned int n
;
442 parent_window
= next_parent
;
443 XQueryTree(XtDisplay((Widget
) m_mainWidget
), parent_window
, &root
,
444 &next_parent
, &theChildren
, &n
);
445 XFree(theChildren
); // not needed
447 XLowerWindow(XtDisplay((Widget
) m_mainWidget
), parent_window
);
450 bool wxDialog::Show(bool show
)
456 if (!wxUSE_INVISIBLE_RESIZE
)
457 XtMapWidget(XtParent((Widget
) m_mainWidget
));
459 XtManageChild((Widget
) m_mainWidget
) ;
461 XRaiseWindow(XtDisplay((Widget
) m_mainWidget
), XtWindow((Widget
) m_mainWidget
));
466 if (!wxUSE_INVISIBLE_RESIZE
)
467 XtUnmapWidget(XtParent((Widget
) m_mainWidget
));
469 XtUnmanageChild((Widget
) m_mainWidget
) ;
471 XFlush(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()));
472 XSync(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()), FALSE
);
478 // Shows a dialog modally, returning a return code
479 int wxDialog::ShowModal()
481 m_windowStyle
|= wxDIALOG_MODAL
;
488 wxModalShowingStack
.Insert((wxObject
*)TRUE
);
490 m_modalShowing
= TRUE
;
491 XtAddGrab((Widget
) m_mainWidget
, TRUE
, FALSE
);
495 // Loop until we signal that the dialog should be closed
496 while ((wxModalShowingStack
.Number() > 0) && ((int)(wxModalShowingStack
.First()->Data()) != 0))
498 // XtAppProcessEvent((XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
500 XtAppNextEvent((XtAppContext
) wxTheApp
->GetAppContext(), &event
);
501 wxTheApp
->ProcessXEvent((WXEvent
*) &event
);
503 if (XtAppPending( (XtAppContext
) wxTheApp
->GetAppContext() ) == 0)
505 if (!wxTheApp
->ProcessIdle())
508 // leave the main loop to give other threads a chance to
509 // perform their GUI work
518 // Remove modal dialog flag from stack
519 wxNode
*node
= wxModalShowingStack
.First();
523 // Now process all events in case they get sent to a destroyed dialog
524 XSync(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()), FALSE
);
525 while (XtAppPending((XtAppContext
) wxTheApp
->GetAppContext()))
527 XFlush(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()));
528 XtAppNextEvent((XtAppContext
) wxTheApp
->GetAppContext(), &event
);
530 wxTheApp
->ProcessXEvent((WXEvent
*) &event
);
533 // TODO: is it safe to call this, if the dialog may have been deleted
534 // by now? Probably only if we're using delayed deletion of dialogs.
535 return GetReturnCode();
538 void wxDialog::EndModal(int retCode
)
543 SetReturnCode(retCode
);
545 // Strangely, we don't seem to need this now.
546 // XtRemoveGrab((Widget) m_mainWidget);
550 m_modalShowing
= FALSE
;
552 wxNode
*node
= wxModalShowingStack
.First();
554 node
->SetData((wxObject
*)FALSE
);
558 void wxDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
560 if ( Validate() && TransferDataFromWindow() )
566 SetReturnCode(wxID_OK
);
572 void wxDialog::OnApply(wxCommandEvent
& WXUNUSED(event
))
575 TransferDataFromWindow();
576 // TODO probably need to disable the Apply button until things change again
579 void wxDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
582 EndModal(wxID_CANCEL
);
585 SetReturnCode(wxID_CANCEL
);
590 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
592 // We'll send a Cancel message by default,
593 // which may close the dialog.
594 // Check for looping if the Cancel event handler calls Close().
596 // Note that if a cancel button and handler aren't present in the dialog,
597 // nothing will happen when you close the dialog via the window manager, or
599 // We wouldn't want to destroy the dialog by default, since the dialog may have been
600 // created on the stack.
601 // However, this does mean that calling dialog->Close() won't delete the dialog
602 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
603 // sure to destroy the dialog.
604 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
606 static wxList closing
;
608 if ( closing
.Member(this) )
611 closing
.Append(this);
613 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
614 cancelEvent
.SetEventObject( this );
615 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
617 closing
.DeleteObject(this);
620 void wxDialog::OnPaint(wxPaintEvent
&WXUNUSED(event
))
622 // added for compatiblity only
625 // Destroy the window (delayed, if a managed window)
626 bool wxDialog::Destroy()
628 if (!wxPendingDelete
.Member(this))
629 wxPendingDelete
.Append(this);
633 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
635 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
639 // Handle a close event from the window manager
640 static void wxCloseDialogCallback( Widget
WXUNUSED(widget
), XtPointer client_data
,
641 XmAnyCallbackStruct
*WXUNUSED(cbs
))
643 wxDialog
*dialog
= (wxDialog
*)client_data
;
644 wxCloseEvent
closeEvent(wxEVT_CLOSE_WINDOW
, dialog
->GetId());
645 closeEvent
.SetEventObject(dialog
);
647 // May delete the dialog (with delayed deletion)
648 dialog
->GetEventHandler()->ProcessEvent(closeEvent
);
651 void wxDialogBoxEventHandler(Widget wid
,
652 XtPointer
WXUNUSED(client_data
),
654 Boolean
* continueToDispatch
)
656 wxDialog
*dialog
= (wxDialog
*)wxGetWindowFromTable(wid
);
659 wxMouseEvent
wxevent(wxEVT_NULL
);
660 if (wxTranslateMouseEvent(wxevent
, dialog
, wid
, event
))
662 wxevent
.SetEventObject(dialog
);
663 wxevent
.SetId(dialog
->GetId());
664 dialog
->GetEventHandler()->ProcessEvent(wxevent
);
668 // An attempt to implement OnCharHook by calling OnCharHook first;
669 // if this returns TRUE, set continueToDispatch to False
670 // (don't continue processing).
671 // Otherwise set it to True and call OnChar.
672 wxKeyEvent
keyEvent(wxEVT_CHAR
);
673 if (wxTranslateKeyEvent(keyEvent
, dialog
, wid
, event
))
675 keyEvent
.SetEventObject(dialog
);
676 keyEvent
.SetId(dialog
->GetId());
677 keyEvent
.SetEventType(wxEVT_CHAR_HOOK
);
678 if (dialog
->GetEventHandler()->ProcessEvent(keyEvent
))
680 *continueToDispatch
= False
;
685 // For simplicity, OnKeyDown is the same as OnChar
686 // TODO: filter modifier key presses from OnChar
687 keyEvent
.SetEventType(wxEVT_KEY_DOWN
);
689 // Only process OnChar if OnKeyDown didn't swallow it
690 if (!dialog
->GetEventHandler()->ProcessEvent (keyEvent
))
692 keyEvent
.SetEventType(wxEVT_CHAR
);
693 dialog
->GetEventHandler()->ProcessEvent(keyEvent
);
699 *continueToDispatch
= True
;
702 static void wxUnmapBulletinBoard(Widget
WXUNUSED(dialog
), wxDialog
*WXUNUSED(client
), XtPointer
WXUNUSED(call
) )
704 /* This gets called when the dialog is being shown, which
705 * defeats modal showing.
706 client->m_modalShowing = FALSE ;
707 client->m_isShown = FALSE;
711 void wxDialog::ChangeFont(bool keepOriginalSize
)
713 wxWindow::ChangeFont(keepOriginalSize
);
716 void wxDialog::ChangeBackgroundColour()
719 DoChangeBackgroundColour(GetMainWidget(), m_backgroundColour
);
722 void wxDialog::ChangeForegroundColour()
725 DoChangeForegroundColour(GetMainWidget(), m_foregroundColour
);