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"
24 #include <X11/Shell.h>
28 #include <Xm/MwmUtil.h>
30 #include <Xm/BulletinB.h>
33 #include <Xm/DialogS.h>
34 #include <Xm/FileSB.h>
35 #include <Xm/RowColumn.h>
36 #include <Xm/LabelG.h>
37 #include <Xm/AtomMgr.h>
39 #include <Xm/Protocols.h>
42 #include "wx/motif/private.h"
44 static void wxCloseDialogCallback(Widget widget
, XtPointer client_data
, XmAnyCallbackStruct
*cbs
);
45 static void wxDialogBoxEventHandler (Widget wid
,
46 XtPointer client_data
,
48 Boolean
*continueToDispatch
);
50 static void wxUnmapBulletinBoard(Widget dialog
, wxDialog
*client
,XtPointer call
);
52 // A stack of modal_showing flags, since we can't rely
53 // on accessing wxDialog::m_modalShowing within
54 // wxDialog::Show in case a callback has deleted the wxDialog.
55 static wxList wxModalShowingStack
;
57 // Lists to keep track of windows, so we can disable/enable them
59 wxList wxModalDialogs
;
60 wxList wxModelessWindows
; // Frames and modeless dialogs
61 extern wxList wxPendingDelete
;
63 #define wxUSE_INVISIBLE_RESIZE 1
65 #if !USE_SHARED_LIBRARY
66 IMPLEMENT_DYNAMIC_CLASS(wxDialog
, wxPanel
)
68 BEGIN_EVENT_TABLE(wxDialog
, wxPanel
)
69 EVT_BUTTON(wxID_OK
, wxDialog::OnOK
)
70 EVT_BUTTON(wxID_APPLY
, wxDialog::OnApply
)
71 EVT_BUTTON(wxID_CANCEL
, wxDialog::OnCancel
)
72 EVT_CHAR_HOOK(wxDialog::OnCharHook
)
73 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged
)
74 EVT_CLOSE(wxDialog::OnCloseWindow
)
81 m_modalShowing
= FALSE
;
82 m_backgroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
85 bool wxDialog::Create(wxWindow
*parent
, wxWindowID id
,
86 const wxString
& title
,
92 m_windowStyle
= style
;
93 m_modalShowing
= FALSE
;
94 m_dialogTitle
= title
;
96 m_backgroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
97 m_foregroundColour
= *wxBLACK
;
102 wxTopLevelWindows
.Append(this);
104 if (parent
) parent
->AddChild(this);
107 m_windowId
= (int)NewControlId();
111 Widget parentWidget
= (Widget
) 0;
113 parentWidget
= (Widget
) parent
->GetTopWidget();
115 parentWidget
= (Widget
) wxTheApp
->GetTopLevelWidget();
117 wxASSERT_MSG( (parentWidget
!= (Widget
) 0), "Could not find a suitable parent shell for dialog." );
120 XtSetArg (args
[0], XmNdefaultPosition
, False
);
121 XtSetArg (args
[1], XmNautoUnmanage
, False
);
122 Widget dialogShell
= XmCreateBulletinBoardDialog(parentWidget
, (char*) (const char*) name
, args
, 2);
123 m_mainWidget
= (WXWidget
) dialogShell
;
125 // We don't want margins, since there is enough elsewhere.
126 XtVaSetValues(dialogShell
,
129 XmNresizePolicy
, XmRESIZE_NONE
,
132 Widget shell
= XtParent(dialogShell
) ;
135 XmString str
= XmStringCreateSimple((char*) (const char*)title
);
136 XtVaSetValues(dialogShell
,
142 m_windowFont
= wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
);
145 wxAddWindowToTable(dialogShell
, this);
147 // Intercept CLOSE messages from the window manager
148 Atom WM_DELETE_WINDOW
= XmInternAtom(XtDisplay(shell
), "WM_DELETE_WINDOW", False
);
150 /* Remove and add WM_DELETE_WINDOW so ours is only handler */
151 /* Why do we have to do this for wxDialog, but not wxFrame? */
152 XmRemoveWMProtocols(shell
, &WM_DELETE_WINDOW
, 1);
153 XmAddWMProtocols(shell
, &WM_DELETE_WINDOW
, 1);
154 XmActivateWMProtocol(shell
, WM_DELETE_WINDOW
);
156 // Modified Steve Hammes for Motif 2.0
157 #if (XmREVISION > 1 || XmVERSION > 1)
158 XmAddWMProtocolCallback(shell
, WM_DELETE_WINDOW
, (XtCallbackProc
) wxCloseDialogCallback
, (XtPointer
)this);
159 #elif XmREVISION == 1
160 XmAddWMProtocolCallback(shell
, WM_DELETE_WINDOW
, (XtCallbackProc
) wxCloseDialogCallback
, (caddr_t
)this);
162 XmAddWMProtocolCallback(shell
, WM_DELETE_WINDOW
, (void (*)())wxCloseDialogCallback
, (caddr_t
)this);
166 XtOverrideTranslations(dialogShell
,
167 ptr
= XtParseTranslationTable("<Configure>: resize()"));
170 // Can't remember what this was about... but I think it's necessary.
172 if (wxUSE_INVISIBLE_RESIZE
)
175 XtVaSetValues(dialogShell
, XmNx
, pos
.x
,
178 XtVaSetValues(dialogShell
, XmNy
, pos
.y
,
182 XtVaSetValues(dialogShell
, XmNwidth
, size
.x
, NULL
);
184 XtVaSetValues(dialogShell
, XmNheight
, size
.y
, NULL
);
187 // This patch come from Torsten Liermann lier@lier1.muc.de
188 if (XmIsMotifWMRunning(shell
))
191 if (m_windowStyle
& wxRESIZE_BORDER
)
192 decor
|= MWM_DECOR_RESIZEH
;
193 if (m_windowStyle
& wxSYSTEM_MENU
)
194 decor
|= MWM_DECOR_MENU
;
195 if ((m_windowStyle
& wxCAPTION
) ||
196 (m_windowStyle
& wxTINY_CAPTION_HORIZ
) ||
197 (m_windowStyle
& wxTINY_CAPTION_VERT
))
198 decor
|= MWM_DECOR_TITLE
;
199 if (m_windowStyle
& wxTHICK_FRAME
)
200 decor
|= MWM_DECOR_BORDER
;
201 if (m_windowStyle
& wxMINIMIZE_BOX
)
202 decor
|= MWM_DECOR_MINIMIZE
;
203 if (m_windowStyle
& wxMAXIMIZE_BOX
)
204 decor
|= MWM_DECOR_MAXIMIZE
;
206 XtVaSetValues(shell
,XmNmwmDecorations
,decor
,NULL
) ;
208 // This allows non-Motif window managers to support at least the
209 // no-decorations case.
212 if ((m_windowStyle
& wxCAPTION
) != wxCAPTION
)
213 XtVaSetValues((Widget
) shell
,XmNoverrideRedirect
,TRUE
,NULL
);
216 XtRealizeWidget(dialogShell
);
218 XtAddCallback(dialogShell
,XmNunmapCallback
,
219 (XtCallbackProc
)wxUnmapBulletinBoard
,this) ;
221 // Positioning of the dialog doesn't work properly unless the dialog
222 // is managed, so we manage without mapping to the screen.
223 // To show, we map the shell (actually it's parent).
224 if (!wxUSE_INVISIBLE_RESIZE
)
225 XtVaSetValues(shell
, XmNmappedWhenManaged
, FALSE
, NULL
);
227 if (!wxUSE_INVISIBLE_RESIZE
)
229 XtManageChild(dialogShell
);
230 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
232 XtAddEventHandler(dialogShell
,ExposureMask
,FALSE
,
233 wxUniversalRepaintProc
, (XtPointer
) this);
235 XtAddEventHandler(dialogShell
,
236 ButtonPressMask
| ButtonReleaseMask
| PointerMotionMask
| KeyPressMask
,
238 wxDialogBoxEventHandler
,
241 ChangeBackgroundColour();
246 void wxDialog::SetModal(bool flag
)
249 m_windowStyle
|= wxDIALOG_MODAL
;
251 if ( m_windowStyle
& wxDIALOG_MODAL
)
252 m_windowStyle
-= wxDIALOG_MODAL
;
254 wxModelessWindows
.DeleteObject(this);
256 wxModelessWindows
.Append(this);
259 wxDialog::~wxDialog()
262 XtRemoveEventHandler((Widget
) m_mainWidget
, ExposureMask
, FALSE
,
263 wxUniversalRepaintProc
, (XtPointer
) this);
265 m_modalShowing
= FALSE
;
266 if (!wxUSE_INVISIBLE_RESIZE
&& m_mainWidget
)
268 XtUnmapWidget((Widget
) m_mainWidget
);
271 wxTopLevelWindows
.DeleteObject(this);
273 if ( (GetWindowStyleFlag() & wxDIALOG_MODAL
) != wxDIALOG_MODAL
)
274 wxModelessWindows
.DeleteObject(this);
276 // If this is the last top-level window, exit.
277 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
279 wxTheApp
->SetTopWindow(NULL
);
281 if (wxTheApp
->GetExitOnFrameDelete())
283 wxTheApp
->ExitMainLoop();
287 // This event-flushing code used to be in wxWindow::PostDestroyChildren (wx_dialog.cpp)
288 // but I think this should work, if we destroy the children first.
289 // Note that this might need to be done for wxFrame also.
292 // The idea about doing it here is that if you have to remove the
293 // XtDestroyWidget from ~wxWindow, at least top-level windows
294 // will still be deleted (and destroy children implicitly).
297 DetachWidget(GetMainWidget()); // Removes event handlers
298 XtDestroyWidget((Widget
) GetMainWidget());
299 SetMainWidget((WXWidget
) NULL
);
303 // By default, pressing escape cancels the dialog
304 void wxDialog::OnCharHook(wxKeyEvent
& event
)
306 if (event
.m_keyCode
== WXK_ESCAPE
)
308 // Behaviour changed in 2.0: we'll send a Cancel message
309 // to the dialog instead of Close.
310 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
311 cancelEvent
.SetEventObject( this );
312 GetEventHandler()->ProcessEvent(cancelEvent
);
316 // We didn't process this event.
320 void wxDialog::Iconize(bool WXUNUSED(iconize
))
322 // Can't iconize a dialog in Motif, apparently
323 // TODO: try using the parent of m_mainShell.
324 // XtVaSetValues((Widget) m_mainWidget, XmNiconic, iconize, NULL);
327 bool wxDialog::IsIconized() const
331 XtVaGetValues((Widget) m_mainWidget, XmNiconic, &iconic, NULL);
338 void wxDialog::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
340 XtVaSetValues((Widget
) m_mainWidget
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
341 wxWindow::DoSetSize(x
, y
, width
, height
, sizeFlags
);
342 XtVaSetValues((Widget
) m_mainWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
345 void wxDialog::DoSetClientSize(int width
, int height
)
347 wxWindow::SetSize(-1, -1, width
, height
);
350 void wxDialog::SetTitle(const wxString
& title
)
352 m_dialogTitle
= title
;
355 XmString str
= XmStringCreateSimple((char*) (const char*) title
);
356 XtVaSetValues((Widget
) m_mainWidget
,
357 XmNtitle
, (char*) (const char*) title
,
358 XmNdialogTitle
, str
, // Roberto Cocchi
359 XmNiconName
, (char*) (const char*) title
,
365 wxString
wxDialog::GetTitle() const
367 return m_dialogTitle
;
370 void wxDialog::Centre(int direction
)
372 int x_offset
,y_offset
;
373 int display_width
, display_height
;
374 int width
, height
, x
, y
;
375 wxWindow
*parent
= GetParent();
376 if ((direction
& wxCENTER_FRAME
) && parent
)
378 parent
->GetPosition(&x_offset
,&y_offset
) ;
379 parent
->GetSize(&display_width
,&display_height
) ;
383 wxDisplaySize(&display_width
, &display_height
);
388 GetSize(&width
, &height
);
391 if (direction
& wxHORIZONTAL
)
392 x
= (int)((display_width
- width
)/2);
393 if (direction
& wxVERTICAL
)
394 y
= (int)((display_height
- height
)/2);
396 SetSize(x
+x_offset
, y
+y_offset
, width
, height
);
399 void wxDialog::Raise()
401 Window parent_window
= XtWindow((Widget
) m_mainWidget
),
402 next_parent
= XtWindow((Widget
) m_mainWidget
),
403 root
= RootWindowOfScreen(XtScreen((Widget
) m_mainWidget
));
404 // search for the parent that is child of ROOT, because the WM may
405 // reparent twice and notify only the next parent (like FVWM)
406 while (next_parent
!= root
) {
407 Window
*theChildren
; unsigned int n
;
408 parent_window
= next_parent
;
409 XQueryTree(XtDisplay((Widget
) m_mainWidget
), parent_window
, &root
,
410 &next_parent
, &theChildren
, &n
);
411 XFree(theChildren
); // not needed
413 XRaiseWindow(XtDisplay((Widget
) m_mainWidget
), parent_window
);
416 void wxDialog::Lower()
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 XLowerWindow(XtDisplay((Widget
) m_mainWidget
), parent_window
);
433 bool wxDialog::Show(bool show
)
439 if (!wxUSE_INVISIBLE_RESIZE
)
440 XtMapWidget(XtParent((Widget
) m_mainWidget
));
442 XtManageChild((Widget
) m_mainWidget
) ;
444 XRaiseWindow(XtDisplay((Widget
) m_mainWidget
), XtWindow((Widget
) m_mainWidget
));
449 if (!wxUSE_INVISIBLE_RESIZE
)
450 XtUnmapWidget(XtParent((Widget
) m_mainWidget
));
452 XtUnmanageChild((Widget
) m_mainWidget
) ;
454 XFlush(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()));
455 XSync(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()), FALSE
);
461 // Shows a dialog modally, returning a return code
462 int wxDialog::ShowModal()
464 m_windowStyle
|= wxDIALOG_MODAL
;
471 wxModalShowingStack
.Insert((wxObject
*)TRUE
);
473 m_modalShowing
= TRUE
;
474 XtAddGrab((Widget
) m_mainWidget
, TRUE
, FALSE
);
478 // Loop until we signal that the dialog should be closed
479 while ((wxModalShowingStack
.Number() > 0) && ((int)(wxModalShowingStack
.First()->Data()) != 0))
481 // XtAppProcessEvent((XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
483 XtAppNextEvent((XtAppContext
) wxTheApp
->GetAppContext(), &event
);
484 wxTheApp
->ProcessXEvent((WXEvent
*) &event
);
487 // Remove modal dialog flag from stack
488 wxNode
*node
= wxModalShowingStack
.First();
492 // Now process all events in case they get sent to a destroyed dialog
493 XSync(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()), FALSE
);
494 while (XtAppPending((XtAppContext
) wxTheApp
->GetAppContext()))
496 XFlush(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()));
497 XtAppNextEvent((XtAppContext
) wxTheApp
->GetAppContext(), &event
);
499 wxTheApp
->ProcessXEvent((WXEvent
*) &event
);
502 // TODO: is it safe to call this, if the dialog may have been deleted
503 // by now? Probably only if we're using delayed deletion of dialogs.
504 return GetReturnCode();
507 void wxDialog::EndModal(int retCode
)
512 SetReturnCode(retCode
);
514 // Strangely, we don't seem to need this now.
515 // XtRemoveGrab((Widget) m_mainWidget);
519 m_modalShowing
= FALSE
;
521 wxNode
*node
= wxModalShowingStack
.First();
523 node
->SetData((wxObject
*)FALSE
);
527 void wxDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
529 if ( Validate() && TransferDataFromWindow() )
535 SetReturnCode(wxID_OK
);
541 void wxDialog::OnApply(wxCommandEvent
& WXUNUSED(event
))
544 TransferDataFromWindow();
545 // TODO probably need to disable the Apply button until things change again
548 void wxDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
551 EndModal(wxID_CANCEL
);
554 SetReturnCode(wxID_CANCEL
);
559 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
561 // We'll send a Cancel message by default,
562 // which may close the dialog.
563 // Check for looping if the Cancel event handler calls Close().
565 // Note that if a cancel button and handler aren't present in the dialog,
566 // nothing will happen when you close the dialog via the window manager, or
568 // We wouldn't want to destroy the dialog by default, since the dialog may have been
569 // created on the stack.
570 // However, this does mean that calling dialog->Close() won't delete the dialog
571 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
572 // sure to destroy the dialog.
573 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
575 static wxList closing
;
577 if ( closing
.Member(this) )
580 closing
.Append(this);
582 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
583 cancelEvent
.SetEventObject( this );
584 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
586 closing
.DeleteObject(this);
589 // Destroy the window (delayed, if a managed window)
590 bool wxDialog::Destroy()
592 if (!wxPendingDelete
.Member(this))
593 wxPendingDelete
.Append(this);
597 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
599 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
608 // Handle a close event from the window manager
609 static void wxCloseDialogCallback( Widget
WXUNUSED(widget
), XtPointer client_data
,
610 XmAnyCallbackStruct
*WXUNUSED(cbs
))
612 wxDialog
*dialog
= (wxDialog
*)client_data
;
613 wxCloseEvent
closeEvent(wxEVT_CLOSE_WINDOW
, dialog
->GetId());
614 closeEvent
.SetEventObject(dialog
);
616 // May delete the dialog (with delayed deletion)
617 dialog
->GetEventHandler()->ProcessEvent(closeEvent
);
620 void wxDialogBoxEventHandler (Widget wid
,
621 XtPointer
WXUNUSED(client_data
),
623 Boolean
*continueToDispatch
)
625 wxDialog
*dialog
= (wxDialog
*)wxWidgetHashTable
->Get((long)wid
);
628 wxMouseEvent
wxevent(wxEVT_NULL
);
629 if (wxTranslateMouseEvent(wxevent
, dialog
, wid
, event
))
631 wxevent
.SetEventObject(dialog
);
632 wxevent
.SetId(dialog
->GetId());
633 dialog
->GetEventHandler()->ProcessEvent(wxevent
);
637 // An attempt to implement OnCharHook by calling OnCharHook first;
638 // if this returns TRUE, set continueToDispatch to False
639 // (don't continue processing).
640 // Otherwise set it to True and call OnChar.
641 wxKeyEvent
keyEvent(wxEVT_CHAR
);
642 if (wxTranslateKeyEvent(keyEvent
, dialog
, wid
, event
))
644 keyEvent
.SetEventObject(dialog
);
645 keyEvent
.SetId(dialog
->GetId());
646 keyEvent
.SetEventType(wxEVT_CHAR_HOOK
);
647 if (dialog
->GetEventHandler()->ProcessEvent(keyEvent
))
649 *continueToDispatch
= False
;
654 // For simplicity, OnKeyDown is the same as OnChar
655 // TODO: filter modifier key presses from OnChar
656 keyEvent
.SetEventType(wxEVT_KEY_DOWN
);
658 // Only process OnChar if OnKeyDown didn't swallow it
659 if (!dialog
->GetEventHandler()->ProcessEvent (keyEvent
))
661 keyEvent
.SetEventType(wxEVT_CHAR
);
662 dialog
->GetEventHandler()->ProcessEvent(keyEvent
);
668 *continueToDispatch
= True
;
671 static void wxUnmapBulletinBoard(Widget
WXUNUSED(dialog
), wxDialog
*WXUNUSED(client
), XtPointer
WXUNUSED(call
) )
673 /* This gets called when the dialog is being shown, which
674 * defeats modal showing.
675 client->m_modalShowing = FALSE ;
676 client->m_isShown = FALSE;
680 void wxDialog::ChangeFont(bool keepOriginalSize
)
682 wxWindow::ChangeFont(keepOriginalSize
);
685 void wxDialog::ChangeBackgroundColour()
688 DoChangeBackgroundColour(GetMainWidget(), m_backgroundColour
);
691 void wxDialog::ChangeForegroundColour()
694 DoChangeForegroundColour(GetMainWidget(), m_foregroundColour
);