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_SIZE(wxDialog::OnSize
)
70 EVT_BUTTON(wxID_OK
, wxDialog::OnOK
)
71 EVT_BUTTON(wxID_APPLY
, wxDialog::OnApply
)
72 EVT_BUTTON(wxID_CANCEL
, wxDialog::OnCancel
)
73 EVT_CHAR_HOOK(wxDialog::OnCharHook
)
74 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged
)
75 EVT_CLOSE(wxDialog::OnCloseWindow
)
82 m_modalShowing
= FALSE
;
83 m_backgroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
86 bool wxDialog::Create(wxWindow
*parent
, wxWindowID id
,
87 const wxString
& title
,
93 m_windowStyle
= style
;
94 m_modalShowing
= FALSE
;
95 m_dialogTitle
= title
;
97 m_backgroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
98 m_foregroundColour
= *wxBLACK
;
103 wxTopLevelWindows
.Append(this);
105 if (parent
) parent
->AddChild(this);
108 m_windowId
= (int)NewControlId();
112 Widget parentWidget
= (Widget
) 0;
114 parentWidget
= (Widget
) parent
->GetTopWidget();
116 parentWidget
= (Widget
) wxTheApp
->GetTopLevelWidget();
118 wxASSERT_MSG( (parentWidget
!= (Widget
) 0), "Could not find a suitable parent shell for dialog." );
121 XtSetArg (args
[0], XmNdefaultPosition
, False
);
122 XtSetArg (args
[1], XmNautoUnmanage
, False
);
123 Widget dialogShell
= XmCreateBulletinBoardDialog(parentWidget
, (char*) (const char*) name
, args
, 2);
124 m_mainWidget
= (WXWidget
) dialogShell
;
126 // We don't want margins, since there is enough elsewhere.
127 XtVaSetValues(dialogShell
,
130 XmNresizePolicy
, XmRESIZE_NONE
,
133 Widget shell
= XtParent(dialogShell
) ;
136 XmString str
= XmStringCreateSimple((char*) (const char*)title
);
137 XtVaSetValues(dialogShell
,
143 m_font
= wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
);
146 wxAddWindowToTable(dialogShell
, this);
148 // Intercept CLOSE messages from the window manager
149 Atom WM_DELETE_WINDOW
= XmInternAtom(XtDisplay(shell
), "WM_DELETE_WINDOW", False
);
151 /* Remove and add WM_DELETE_WINDOW so ours is only handler */
152 /* Why do we have to do this for wxDialog, but not wxFrame? */
153 XmRemoveWMProtocols(shell
, &WM_DELETE_WINDOW
, 1);
154 XmAddWMProtocols(shell
, &WM_DELETE_WINDOW
, 1);
155 XmActivateWMProtocol(shell
, WM_DELETE_WINDOW
);
157 // Modified Steve Hammes for Motif 2.0
158 #if (XmREVISION > 1 || XmVERSION > 1)
159 XmAddWMProtocolCallback(shell
, WM_DELETE_WINDOW
, (XtCallbackProc
) wxCloseDialogCallback
, (XtPointer
)this);
160 #elif XmREVISION == 1
161 XmAddWMProtocolCallback(shell
, WM_DELETE_WINDOW
, (XtCallbackProc
) wxCloseDialogCallback
, (caddr_t
)this);
163 XmAddWMProtocolCallback(shell
, WM_DELETE_WINDOW
, (void (*)())wxCloseDialogCallback
, (caddr_t
)this);
167 XtOverrideTranslations(dialogShell
,
168 ptr
= XtParseTranslationTable("<Configure>: resize()"));
171 // Can't remember what this was about... but I think it's necessary.
173 if (wxUSE_INVISIBLE_RESIZE
)
176 XtVaSetValues(dialogShell
, XmNx
, pos
.x
,
179 XtVaSetValues(dialogShell
, XmNy
, pos
.y
,
183 XtVaSetValues(dialogShell
, XmNwidth
, size
.x
, NULL
);
185 XtVaSetValues(dialogShell
, XmNheight
, size
.y
, NULL
);
188 // This patch come from Torsten Liermann lier@lier1.muc.de
189 if (XmIsMotifWMRunning(shell
))
192 if (m_windowStyle
& wxRESIZE_BORDER
)
193 decor
|= MWM_DECOR_RESIZEH
;
194 if (m_windowStyle
& wxSYSTEM_MENU
)
195 decor
|= MWM_DECOR_MENU
;
196 if ((m_windowStyle
& wxCAPTION
) ||
197 (m_windowStyle
& wxTINY_CAPTION_HORIZ
) ||
198 (m_windowStyle
& wxTINY_CAPTION_VERT
))
199 decor
|= MWM_DECOR_TITLE
;
200 if (m_windowStyle
& wxTHICK_FRAME
)
201 decor
|= MWM_DECOR_BORDER
;
202 if (m_windowStyle
& wxMINIMIZE_BOX
)
203 decor
|= MWM_DECOR_MINIMIZE
;
204 if (m_windowStyle
& wxMAXIMIZE_BOX
)
205 decor
|= MWM_DECOR_MAXIMIZE
;
207 XtVaSetValues(shell
,XmNmwmDecorations
,decor
,NULL
) ;
209 // This allows non-Motif window managers to support at least the
210 // no-decorations case.
213 if ((m_windowStyle
& wxCAPTION
) != wxCAPTION
)
214 XtVaSetValues((Widget
) shell
,XmNoverrideRedirect
,TRUE
,NULL
);
217 XtRealizeWidget(dialogShell
);
219 XtAddCallback(dialogShell
,XmNunmapCallback
,
220 (XtCallbackProc
)wxUnmapBulletinBoard
,this) ;
222 // Positioning of the dialog doesn't work properly unless the dialog
223 // is managed, so we manage without mapping to the screen.
224 // To show, we map the shell (actually it's parent).
225 if (!wxUSE_INVISIBLE_RESIZE
)
226 XtVaSetValues(shell
, XmNmappedWhenManaged
, FALSE
, NULL
);
228 if (!wxUSE_INVISIBLE_RESIZE
)
230 XtManageChild(dialogShell
);
231 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
233 XtAddEventHandler(dialogShell
,ExposureMask
,FALSE
,
234 wxUniversalRepaintProc
, (XtPointer
) this);
236 XtAddEventHandler(dialogShell
,
237 ButtonPressMask
| ButtonReleaseMask
| PointerMotionMask
| KeyPressMask
,
239 wxDialogBoxEventHandler
,
242 ChangeBackgroundColour();
247 void wxDialog::SetModal(bool flag
)
250 m_windowStyle
|= wxDIALOG_MODAL
;
252 if ( m_windowStyle
& wxDIALOG_MODAL
)
253 m_windowStyle
-= wxDIALOG_MODAL
;
255 wxModelessWindows
.DeleteObject(this);
257 wxModelessWindows
.Append(this);
260 wxDialog::~wxDialog()
262 m_isBeingDeleted
= TRUE
;
265 XtRemoveEventHandler((Widget
) m_mainWidget
, ExposureMask
, FALSE
,
266 wxUniversalRepaintProc
, (XtPointer
) this);
268 m_modalShowing
= FALSE
;
269 if (!wxUSE_INVISIBLE_RESIZE
&& m_mainWidget
)
271 XtUnmapWidget((Widget
) m_mainWidget
);
274 wxTopLevelWindows
.DeleteObject(this);
276 if ( (GetWindowStyleFlag() & wxDIALOG_MODAL
) != wxDIALOG_MODAL
)
277 wxModelessWindows
.DeleteObject(this);
279 // If this is the last top-level window, exit.
280 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
282 wxTheApp
->SetTopWindow(NULL
);
284 if (wxTheApp
->GetExitOnFrameDelete())
286 wxTheApp
->ExitMainLoop();
290 // This event-flushing code used to be in wxWindow::PostDestroyChildren (wx_dialog.cpp)
291 // but I think this should work, if we destroy the children first.
292 // Note that this might need to be done for wxFrame also.
295 // The idea about doing it here is that if you have to remove the
296 // XtDestroyWidget from ~wxWindow, at least top-level windows
297 // will still be deleted (and destroy children implicitly).
300 DetachWidget(GetMainWidget()); // Removes event handlers
301 XtDestroyWidget((Widget
) GetMainWidget());
302 SetMainWidget((WXWidget
) NULL
);
306 // By default, pressing escape cancels the dialog
307 void wxDialog::OnCharHook(wxKeyEvent
& event
)
309 if (event
.m_keyCode
== WXK_ESCAPE
)
311 // Behaviour changed in 2.0: we'll send a Cancel message
312 // to the dialog instead of Close.
313 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
314 cancelEvent
.SetEventObject( this );
315 GetEventHandler()->ProcessEvent(cancelEvent
);
319 // We didn't process this event.
323 void wxDialog::Iconize(bool WXUNUSED(iconize
))
325 // Can't iconize a dialog in Motif, apparently
326 // TODO: try using the parent of m_mainShell.
327 // XtVaSetValues((Widget) m_mainWidget, XmNiconic, iconize, NULL);
330 // Default resizing behaviour - if only ONE subwindow,
331 // resize to client rectangle size
332 void wxDialog::OnSize(wxSizeEvent
& WXUNUSED(event
))
334 // if we're using constraints - do use them
335 #if wxUSE_CONSTRAINTS
336 if ( GetAutoLayout() ) {
342 // do we have _exactly_ one child?
343 wxWindow
*child
= NULL
;
344 for ( wxNode
*node
= GetChildren().First(); node
; node
= node
->Next() )
346 wxWindow
*win
= (wxWindow
*)node
->Data();
347 if ( !win
->IsKindOf(CLASSINFO(wxFrame
)) &&
348 !win
->IsKindOf(CLASSINFO(wxDialog
)) )
351 return; // it's our second subwindow - nothing to do
357 // we have exactly one child - set it's size to fill the whole frame
358 int clientW
, clientH
;
359 GetClientSize(&clientW
, &clientH
);
364 child
->SetSize(x
, y
, clientW
, clientH
);
369 bool wxDialog::IsIconized() const
373 XtVaGetValues((Widget) m_mainWidget, XmNiconic, &iconic, NULL);
380 void wxDialog::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
382 XtVaSetValues((Widget
) m_mainWidget
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
383 wxWindow::DoSetSize(x
, y
, width
, height
, sizeFlags
);
384 XtVaSetValues((Widget
) m_mainWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
387 void wxDialog::DoSetClientSize(int width
, int height
)
389 wxWindow::SetSize(-1, -1, width
, height
);
392 void wxDialog::SetTitle(const wxString
& title
)
394 m_dialogTitle
= title
;
397 XmString str
= XmStringCreateSimple((char*) (const char*) title
);
398 XtVaSetValues((Widget
) m_mainWidget
,
399 XmNtitle
, (char*) (const char*) title
,
400 XmNdialogTitle
, str
, // Roberto Cocchi
401 XmNiconName
, (char*) (const char*) title
,
407 wxString
wxDialog::GetTitle() const
409 return m_dialogTitle
;
412 void wxDialog::Raise()
414 Window parent_window
= XtWindow((Widget
) m_mainWidget
),
415 next_parent
= XtWindow((Widget
) m_mainWidget
),
416 root
= RootWindowOfScreen(XtScreen((Widget
) m_mainWidget
));
417 // search for the parent that is child of ROOT, because the WM may
418 // reparent twice and notify only the next parent (like FVWM)
419 while (next_parent
!= root
) {
420 Window
*theChildren
; unsigned int n
;
421 parent_window
= next_parent
;
422 XQueryTree(XtDisplay((Widget
) m_mainWidget
), parent_window
, &root
,
423 &next_parent
, &theChildren
, &n
);
424 XFree(theChildren
); // not needed
426 XRaiseWindow(XtDisplay((Widget
) m_mainWidget
), parent_window
);
429 void wxDialog::Lower()
431 Window parent_window
= XtWindow((Widget
) m_mainWidget
),
432 next_parent
= XtWindow((Widget
) m_mainWidget
),
433 root
= RootWindowOfScreen(XtScreen((Widget
) m_mainWidget
));
434 // search for the parent that is child of ROOT, because the WM may
435 // reparent twice and notify only the next parent (like FVWM)
436 while (next_parent
!= root
) {
437 Window
*theChildren
; unsigned int n
;
438 parent_window
= next_parent
;
439 XQueryTree(XtDisplay((Widget
) m_mainWidget
), parent_window
, &root
,
440 &next_parent
, &theChildren
, &n
);
441 XFree(theChildren
); // not needed
443 XLowerWindow(XtDisplay((Widget
) m_mainWidget
), parent_window
);
446 bool wxDialog::Show(bool show
)
452 if (!wxUSE_INVISIBLE_RESIZE
)
453 XtMapWidget(XtParent((Widget
) m_mainWidget
));
455 XtManageChild((Widget
) m_mainWidget
) ;
457 XRaiseWindow(XtDisplay((Widget
) m_mainWidget
), XtWindow((Widget
) m_mainWidget
));
462 if (!wxUSE_INVISIBLE_RESIZE
)
463 XtUnmapWidget(XtParent((Widget
) m_mainWidget
));
465 XtUnmanageChild((Widget
) m_mainWidget
) ;
467 XFlush(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()));
468 XSync(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()), FALSE
);
474 // Shows a dialog modally, returning a return code
475 int wxDialog::ShowModal()
477 m_windowStyle
|= wxDIALOG_MODAL
;
484 wxModalShowingStack
.Insert((wxObject
*)TRUE
);
486 m_modalShowing
= TRUE
;
487 XtAddGrab((Widget
) m_mainWidget
, TRUE
, FALSE
);
491 // Loop until we signal that the dialog should be closed
492 while ((wxModalShowingStack
.Number() > 0) && ((int)(wxModalShowingStack
.First()->Data()) != 0))
494 // XtAppProcessEvent((XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
496 XtAppNextEvent((XtAppContext
) wxTheApp
->GetAppContext(), &event
);
497 wxTheApp
->ProcessXEvent((WXEvent
*) &event
);
500 // Remove modal dialog flag from stack
501 wxNode
*node
= wxModalShowingStack
.First();
505 // Now process all events in case they get sent to a destroyed dialog
506 XSync(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()), FALSE
);
507 while (XtAppPending((XtAppContext
) wxTheApp
->GetAppContext()))
509 XFlush(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()));
510 XtAppNextEvent((XtAppContext
) wxTheApp
->GetAppContext(), &event
);
512 wxTheApp
->ProcessXEvent((WXEvent
*) &event
);
515 // TODO: is it safe to call this, if the dialog may have been deleted
516 // by now? Probably only if we're using delayed deletion of dialogs.
517 return GetReturnCode();
520 void wxDialog::EndModal(int retCode
)
525 SetReturnCode(retCode
);
527 // Strangely, we don't seem to need this now.
528 // XtRemoveGrab((Widget) m_mainWidget);
532 m_modalShowing
= FALSE
;
534 wxNode
*node
= wxModalShowingStack
.First();
536 node
->SetData((wxObject
*)FALSE
);
540 void wxDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
542 if ( Validate() && TransferDataFromWindow() )
548 SetReturnCode(wxID_OK
);
554 void wxDialog::OnApply(wxCommandEvent
& WXUNUSED(event
))
557 TransferDataFromWindow();
558 // TODO probably need to disable the Apply button until things change again
561 void wxDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
564 EndModal(wxID_CANCEL
);
567 SetReturnCode(wxID_CANCEL
);
572 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
574 // We'll send a Cancel message by default,
575 // which may close the dialog.
576 // Check for looping if the Cancel event handler calls Close().
578 // Note that if a cancel button and handler aren't present in the dialog,
579 // nothing will happen when you close the dialog via the window manager, or
581 // We wouldn't want to destroy the dialog by default, since the dialog may have been
582 // created on the stack.
583 // However, this does mean that calling dialog->Close() won't delete the dialog
584 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
585 // sure to destroy the dialog.
586 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
588 static wxList closing
;
590 if ( closing
.Member(this) )
593 closing
.Append(this);
595 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
596 cancelEvent
.SetEventObject( this );
597 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
599 closing
.DeleteObject(this);
602 void wxDialog::OnPaint(wxPaintEvent
&WXUNUSED(event
))
604 // added for compatiblity only
607 // Destroy the window (delayed, if a managed window)
608 bool wxDialog::Destroy()
610 if (!wxPendingDelete
.Member(this))
611 wxPendingDelete
.Append(this);
615 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
617 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
621 // Handle a close event from the window manager
622 static void wxCloseDialogCallback( Widget
WXUNUSED(widget
), XtPointer client_data
,
623 XmAnyCallbackStruct
*WXUNUSED(cbs
))
625 wxDialog
*dialog
= (wxDialog
*)client_data
;
626 wxCloseEvent
closeEvent(wxEVT_CLOSE_WINDOW
, dialog
->GetId());
627 closeEvent
.SetEventObject(dialog
);
629 // May delete the dialog (with delayed deletion)
630 dialog
->GetEventHandler()->ProcessEvent(closeEvent
);
633 void wxDialogBoxEventHandler(Widget wid
,
634 XtPointer
WXUNUSED(client_data
),
636 Boolean
* continueToDispatch
)
638 wxDialog
*dialog
= (wxDialog
*)wxGetWindowFromTable(wid
);
641 wxMouseEvent
wxevent(wxEVT_NULL
);
642 if (wxTranslateMouseEvent(wxevent
, dialog
, wid
, event
))
644 wxevent
.SetEventObject(dialog
);
645 wxevent
.SetId(dialog
->GetId());
646 dialog
->GetEventHandler()->ProcessEvent(wxevent
);
650 // An attempt to implement OnCharHook by calling OnCharHook first;
651 // if this returns TRUE, set continueToDispatch to False
652 // (don't continue processing).
653 // Otherwise set it to True and call OnChar.
654 wxKeyEvent
keyEvent(wxEVT_CHAR
);
655 if (wxTranslateKeyEvent(keyEvent
, dialog
, wid
, event
))
657 keyEvent
.SetEventObject(dialog
);
658 keyEvent
.SetId(dialog
->GetId());
659 keyEvent
.SetEventType(wxEVT_CHAR_HOOK
);
660 if (dialog
->GetEventHandler()->ProcessEvent(keyEvent
))
662 *continueToDispatch
= False
;
667 // For simplicity, OnKeyDown is the same as OnChar
668 // TODO: filter modifier key presses from OnChar
669 keyEvent
.SetEventType(wxEVT_KEY_DOWN
);
671 // Only process OnChar if OnKeyDown didn't swallow it
672 if (!dialog
->GetEventHandler()->ProcessEvent (keyEvent
))
674 keyEvent
.SetEventType(wxEVT_CHAR
);
675 dialog
->GetEventHandler()->ProcessEvent(keyEvent
);
681 *continueToDispatch
= True
;
684 static void wxUnmapBulletinBoard(Widget
WXUNUSED(dialog
), wxDialog
*WXUNUSED(client
), XtPointer
WXUNUSED(call
) )
686 /* This gets called when the dialog is being shown, which
687 * defeats modal showing.
688 client->m_modalShowing = FALSE ;
689 client->m_isShown = FALSE;
693 void wxDialog::ChangeFont(bool keepOriginalSize
)
695 wxWindow::ChangeFont(keepOriginalSize
);
698 void wxDialog::ChangeBackgroundColour()
701 DoChangeBackgroundColour(GetMainWidget(), m_backgroundColour
);
704 void wxDialog::ChangeForegroundColour()
707 DoChangeForegroundColour(GetMainWidget(), m_foregroundColour
);