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 extern wxHashTable
*wxWidgetHashTable
;
65 #define wxUSE_INVISIBLE_RESIZE 1
67 #if !USE_SHARED_LIBRARY
68 IMPLEMENT_DYNAMIC_CLASS(wxDialog
, wxPanel
)
70 BEGIN_EVENT_TABLE(wxDialog
, wxPanel
)
71 EVT_BUTTON(wxID_OK
, wxDialog::OnOK
)
72 EVT_BUTTON(wxID_APPLY
, wxDialog::OnApply
)
73 EVT_BUTTON(wxID_CANCEL
, wxDialog::OnCancel
)
74 EVT_CHAR_HOOK(wxDialog::OnCharHook
)
75 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged
)
76 EVT_CLOSE(wxDialog::OnCloseWindow
)
83 m_modalShowing
= FALSE
;
84 m_backgroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
87 bool wxDialog::Create(wxWindow
*parent
, wxWindowID id
,
88 const wxString
& title
,
94 m_windowStyle
= style
;
95 m_modalShowing
= FALSE
;
96 m_dialogTitle
= title
;
98 m_backgroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
99 m_foregroundColour
= *wxBLACK
;
104 wxTopLevelWindows
.Append(this);
106 if (parent
) parent
->AddChild(this);
109 m_windowId
= (int)NewControlId();
113 Widget parentWidget
= (Widget
) 0;
115 parentWidget
= (Widget
) parent
->GetTopWidget();
117 parentWidget
= (Widget
) wxTheApp
->GetTopLevelWidget();
119 wxASSERT_MSG( (parentWidget
!= (Widget
) 0), "Could not find a suitable parent shell for dialog." );
122 XtSetArg (args
[0], XmNdefaultPosition
, False
);
123 XtSetArg (args
[1], XmNautoUnmanage
, False
);
124 Widget dialogShell
= XmCreateBulletinBoardDialog(parentWidget
, (char*) (const char*) name
, args
, 2);
125 m_mainWidget
= (WXWidget
) dialogShell
;
127 // We don't want margins, since there is enough elsewhere.
128 XtVaSetValues(dialogShell
,
131 XmNresizePolicy
, XmRESIZE_NONE
,
134 Widget shell
= XtParent(dialogShell
) ;
137 XmString str
= XmStringCreateSimple((char*) (const char*)title
);
138 XtVaSetValues(dialogShell
,
144 m_font
= wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
);
147 wxAddWindowToTable(dialogShell
, this);
149 // Intercept CLOSE messages from the window manager
150 Atom WM_DELETE_WINDOW
= XmInternAtom(XtDisplay(shell
), "WM_DELETE_WINDOW", False
);
152 /* Remove and add WM_DELETE_WINDOW so ours is only handler */
153 /* Why do we have to do this for wxDialog, but not wxFrame? */
154 XmRemoveWMProtocols(shell
, &WM_DELETE_WINDOW
, 1);
155 XmAddWMProtocols(shell
, &WM_DELETE_WINDOW
, 1);
156 XmActivateWMProtocol(shell
, WM_DELETE_WINDOW
);
158 // Modified Steve Hammes for Motif 2.0
159 #if (XmREVISION > 1 || XmVERSION > 1)
160 XmAddWMProtocolCallback(shell
, WM_DELETE_WINDOW
, (XtCallbackProc
) wxCloseDialogCallback
, (XtPointer
)this);
161 #elif XmREVISION == 1
162 XmAddWMProtocolCallback(shell
, WM_DELETE_WINDOW
, (XtCallbackProc
) wxCloseDialogCallback
, (caddr_t
)this);
164 XmAddWMProtocolCallback(shell
, WM_DELETE_WINDOW
, (void (*)())wxCloseDialogCallback
, (caddr_t
)this);
168 XtOverrideTranslations(dialogShell
,
169 ptr
= XtParseTranslationTable("<Configure>: resize()"));
172 // Can't remember what this was about... but I think it's necessary.
174 if (wxUSE_INVISIBLE_RESIZE
)
177 XtVaSetValues(dialogShell
, XmNx
, pos
.x
,
180 XtVaSetValues(dialogShell
, XmNy
, pos
.y
,
184 XtVaSetValues(dialogShell
, XmNwidth
, size
.x
, NULL
);
186 XtVaSetValues(dialogShell
, XmNheight
, size
.y
, NULL
);
189 // This patch come from Torsten Liermann lier@lier1.muc.de
190 if (XmIsMotifWMRunning(shell
))
193 if (m_windowStyle
& wxRESIZE_BORDER
)
194 decor
|= MWM_DECOR_RESIZEH
;
195 if (m_windowStyle
& wxSYSTEM_MENU
)
196 decor
|= MWM_DECOR_MENU
;
197 if ((m_windowStyle
& wxCAPTION
) ||
198 (m_windowStyle
& wxTINY_CAPTION_HORIZ
) ||
199 (m_windowStyle
& wxTINY_CAPTION_VERT
))
200 decor
|= MWM_DECOR_TITLE
;
201 if (m_windowStyle
& wxTHICK_FRAME
)
202 decor
|= MWM_DECOR_BORDER
;
203 if (m_windowStyle
& wxMINIMIZE_BOX
)
204 decor
|= MWM_DECOR_MINIMIZE
;
205 if (m_windowStyle
& wxMAXIMIZE_BOX
)
206 decor
|= MWM_DECOR_MAXIMIZE
;
208 XtVaSetValues(shell
,XmNmwmDecorations
,decor
,NULL
) ;
210 // This allows non-Motif window managers to support at least the
211 // no-decorations case.
214 if ((m_windowStyle
& wxCAPTION
) != wxCAPTION
)
215 XtVaSetValues((Widget
) shell
,XmNoverrideRedirect
,TRUE
,NULL
);
218 XtRealizeWidget(dialogShell
);
220 XtAddCallback(dialogShell
,XmNunmapCallback
,
221 (XtCallbackProc
)wxUnmapBulletinBoard
,this) ;
223 // Positioning of the dialog doesn't work properly unless the dialog
224 // is managed, so we manage without mapping to the screen.
225 // To show, we map the shell (actually it's parent).
226 if (!wxUSE_INVISIBLE_RESIZE
)
227 XtVaSetValues(shell
, XmNmappedWhenManaged
, FALSE
, NULL
);
229 if (!wxUSE_INVISIBLE_RESIZE
)
231 XtManageChild(dialogShell
);
232 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
234 XtAddEventHandler(dialogShell
,ExposureMask
,FALSE
,
235 wxUniversalRepaintProc
, (XtPointer
) this);
237 XtAddEventHandler(dialogShell
,
238 ButtonPressMask
| ButtonReleaseMask
| PointerMotionMask
| KeyPressMask
,
240 wxDialogBoxEventHandler
,
243 ChangeBackgroundColour();
248 void wxDialog::SetModal(bool flag
)
251 m_windowStyle
|= wxDIALOG_MODAL
;
253 if ( m_windowStyle
& wxDIALOG_MODAL
)
254 m_windowStyle
-= wxDIALOG_MODAL
;
256 wxModelessWindows
.DeleteObject(this);
258 wxModelessWindows
.Append(this);
261 wxDialog::~wxDialog()
264 XtRemoveEventHandler((Widget
) m_mainWidget
, ExposureMask
, FALSE
,
265 wxUniversalRepaintProc
, (XtPointer
) this);
267 m_modalShowing
= FALSE
;
268 if (!wxUSE_INVISIBLE_RESIZE
&& m_mainWidget
)
270 XtUnmapWidget((Widget
) m_mainWidget
);
273 wxTopLevelWindows
.DeleteObject(this);
275 if ( (GetWindowStyleFlag() & wxDIALOG_MODAL
) != wxDIALOG_MODAL
)
276 wxModelessWindows
.DeleteObject(this);
278 // If this is the last top-level window, exit.
279 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
281 wxTheApp
->SetTopWindow(NULL
);
283 if (wxTheApp
->GetExitOnFrameDelete())
285 wxTheApp
->ExitMainLoop();
289 // This event-flushing code used to be in wxWindow::PostDestroyChildren (wx_dialog.cpp)
290 // but I think this should work, if we destroy the children first.
291 // Note that this might need to be done for wxFrame also.
294 // The idea about doing it here is that if you have to remove the
295 // XtDestroyWidget from ~wxWindow, at least top-level windows
296 // will still be deleted (and destroy children implicitly).
299 DetachWidget(GetMainWidget()); // Removes event handlers
300 XtDestroyWidget((Widget
) GetMainWidget());
301 SetMainWidget((WXWidget
) NULL
);
305 // By default, pressing escape cancels the dialog
306 void wxDialog::OnCharHook(wxKeyEvent
& event
)
308 if (event
.m_keyCode
== WXK_ESCAPE
)
310 // Behaviour changed in 2.0: we'll send a Cancel message
311 // to the dialog instead of Close.
312 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
313 cancelEvent
.SetEventObject( this );
314 GetEventHandler()->ProcessEvent(cancelEvent
);
318 // We didn't process this event.
322 void wxDialog::Iconize(bool WXUNUSED(iconize
))
324 // Can't iconize a dialog in Motif, apparently
325 // TODO: try using the parent of m_mainShell.
326 // XtVaSetValues((Widget) m_mainWidget, XmNiconic, iconize, NULL);
329 bool wxDialog::IsIconized() const
333 XtVaGetValues((Widget) m_mainWidget, XmNiconic, &iconic, NULL);
340 void wxDialog::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
342 XtVaSetValues((Widget
) m_mainWidget
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
343 wxWindow::DoSetSize(x
, y
, width
, height
, sizeFlags
);
344 XtVaSetValues((Widget
) m_mainWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
347 void wxDialog::DoSetClientSize(int width
, int height
)
349 wxWindow::SetSize(-1, -1, width
, height
);
352 void wxDialog::SetTitle(const wxString
& title
)
354 m_dialogTitle
= title
;
357 XmString str
= XmStringCreateSimple((char*) (const char*) title
);
358 XtVaSetValues((Widget
) m_mainWidget
,
359 XmNtitle
, (char*) (const char*) title
,
360 XmNdialogTitle
, str
, // Roberto Cocchi
361 XmNiconName
, (char*) (const char*) title
,
367 wxString
wxDialog::GetTitle() const
369 return m_dialogTitle
;
372 void wxDialog::Centre(int direction
)
374 int x_offset
,y_offset
;
375 int display_width
, display_height
;
376 int width
, height
, x
, y
;
377 wxWindow
*parent
= GetParent();
378 if ((direction
& wxCENTER_FRAME
) && parent
)
380 parent
->GetPosition(&x_offset
,&y_offset
) ;
381 parent
->GetSize(&display_width
,&display_height
) ;
385 wxDisplaySize(&display_width
, &display_height
);
390 GetSize(&width
, &height
);
393 if (direction
& wxHORIZONTAL
)
394 x
= (int)((display_width
- width
)/2);
395 if (direction
& wxVERTICAL
)
396 y
= (int)((display_height
- height
)/2);
398 SetSize(x
+x_offset
, y
+y_offset
, width
, height
);
401 void wxDialog::Raise()
403 Window parent_window
= XtWindow((Widget
) m_mainWidget
),
404 next_parent
= XtWindow((Widget
) m_mainWidget
),
405 root
= RootWindowOfScreen(XtScreen((Widget
) m_mainWidget
));
406 // search for the parent that is child of ROOT, because the WM may
407 // reparent twice and notify only the next parent (like FVWM)
408 while (next_parent
!= root
) {
409 Window
*theChildren
; unsigned int n
;
410 parent_window
= next_parent
;
411 XQueryTree(XtDisplay((Widget
) m_mainWidget
), parent_window
, &root
,
412 &next_parent
, &theChildren
, &n
);
413 XFree(theChildren
); // not needed
415 XRaiseWindow(XtDisplay((Widget
) m_mainWidget
), parent_window
);
418 void wxDialog::Lower()
420 Window parent_window
= XtWindow((Widget
) m_mainWidget
),
421 next_parent
= XtWindow((Widget
) m_mainWidget
),
422 root
= RootWindowOfScreen(XtScreen((Widget
) m_mainWidget
));
423 // search for the parent that is child of ROOT, because the WM may
424 // reparent twice and notify only the next parent (like FVWM)
425 while (next_parent
!= root
) {
426 Window
*theChildren
; unsigned int n
;
427 parent_window
= next_parent
;
428 XQueryTree(XtDisplay((Widget
) m_mainWidget
), parent_window
, &root
,
429 &next_parent
, &theChildren
, &n
);
430 XFree(theChildren
); // not needed
432 XLowerWindow(XtDisplay((Widget
) m_mainWidget
), parent_window
);
435 bool wxDialog::Show(bool show
)
441 if (!wxUSE_INVISIBLE_RESIZE
)
442 XtMapWidget(XtParent((Widget
) m_mainWidget
));
444 XtManageChild((Widget
) m_mainWidget
) ;
446 XRaiseWindow(XtDisplay((Widget
) m_mainWidget
), XtWindow((Widget
) m_mainWidget
));
451 if (!wxUSE_INVISIBLE_RESIZE
)
452 XtUnmapWidget(XtParent((Widget
) m_mainWidget
));
454 XtUnmanageChild((Widget
) m_mainWidget
) ;
456 XFlush(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()));
457 XSync(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()), FALSE
);
463 // Shows a dialog modally, returning a return code
464 int wxDialog::ShowModal()
466 m_windowStyle
|= wxDIALOG_MODAL
;
473 wxModalShowingStack
.Insert((wxObject
*)TRUE
);
475 m_modalShowing
= TRUE
;
476 XtAddGrab((Widget
) m_mainWidget
, TRUE
, FALSE
);
480 // Loop until we signal that the dialog should be closed
481 while ((wxModalShowingStack
.Number() > 0) && ((int)(wxModalShowingStack
.First()->Data()) != 0))
483 // XtAppProcessEvent((XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
485 XtAppNextEvent((XtAppContext
) wxTheApp
->GetAppContext(), &event
);
486 wxTheApp
->ProcessXEvent((WXEvent
*) &event
);
489 // Remove modal dialog flag from stack
490 wxNode
*node
= wxModalShowingStack
.First();
494 // Now process all events in case they get sent to a destroyed dialog
495 XSync(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()), FALSE
);
496 while (XtAppPending((XtAppContext
) wxTheApp
->GetAppContext()))
498 XFlush(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()));
499 XtAppNextEvent((XtAppContext
) wxTheApp
->GetAppContext(), &event
);
501 wxTheApp
->ProcessXEvent((WXEvent
*) &event
);
504 // TODO: is it safe to call this, if the dialog may have been deleted
505 // by now? Probably only if we're using delayed deletion of dialogs.
506 return GetReturnCode();
509 void wxDialog::EndModal(int retCode
)
514 SetReturnCode(retCode
);
516 // Strangely, we don't seem to need this now.
517 // XtRemoveGrab((Widget) m_mainWidget);
521 m_modalShowing
= FALSE
;
523 wxNode
*node
= wxModalShowingStack
.First();
525 node
->SetData((wxObject
*)FALSE
);
529 void wxDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
531 if ( Validate() && TransferDataFromWindow() )
537 SetReturnCode(wxID_OK
);
543 void wxDialog::OnApply(wxCommandEvent
& WXUNUSED(event
))
546 TransferDataFromWindow();
547 // TODO probably need to disable the Apply button until things change again
550 void wxDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
553 EndModal(wxID_CANCEL
);
556 SetReturnCode(wxID_CANCEL
);
561 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
563 // We'll send a Cancel message by default,
564 // which may close the dialog.
565 // Check for looping if the Cancel event handler calls Close().
567 // Note that if a cancel button and handler aren't present in the dialog,
568 // nothing will happen when you close the dialog via the window manager, or
570 // We wouldn't want to destroy the dialog by default, since the dialog may have been
571 // created on the stack.
572 // However, this does mean that calling dialog->Close() won't delete the dialog
573 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
574 // sure to destroy the dialog.
575 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
577 static wxList closing
;
579 if ( closing
.Member(this) )
582 closing
.Append(this);
584 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
585 cancelEvent
.SetEventObject( this );
586 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
588 closing
.DeleteObject(this);
591 void wxDialog::OnPaint(wxPaintEvent
&WXUNUSED(event
))
593 // added for compatiblity only
596 // Destroy the window (delayed, if a managed window)
597 bool wxDialog::Destroy()
599 if (!wxPendingDelete
.Member(this))
600 wxPendingDelete
.Append(this);
604 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
606 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
615 // Handle a close event from the window manager
616 static void wxCloseDialogCallback( Widget
WXUNUSED(widget
), XtPointer client_data
,
617 XmAnyCallbackStruct
*WXUNUSED(cbs
))
619 wxDialog
*dialog
= (wxDialog
*)client_data
;
620 wxCloseEvent
closeEvent(wxEVT_CLOSE_WINDOW
, dialog
->GetId());
621 closeEvent
.SetEventObject(dialog
);
623 // May delete the dialog (with delayed deletion)
624 dialog
->GetEventHandler()->ProcessEvent(closeEvent
);
627 void wxDialogBoxEventHandler (Widget wid
,
628 XtPointer
WXUNUSED(client_data
),
630 Boolean
*continueToDispatch
)
632 wxDialog
*dialog
= (wxDialog
*)wxWidgetHashTable
->Get((long)wid
);
635 wxMouseEvent
wxevent(wxEVT_NULL
);
636 if (wxTranslateMouseEvent(wxevent
, dialog
, wid
, event
))
638 wxevent
.SetEventObject(dialog
);
639 wxevent
.SetId(dialog
->GetId());
640 dialog
->GetEventHandler()->ProcessEvent(wxevent
);
644 // An attempt to implement OnCharHook by calling OnCharHook first;
645 // if this returns TRUE, set continueToDispatch to False
646 // (don't continue processing).
647 // Otherwise set it to True and call OnChar.
648 wxKeyEvent
keyEvent(wxEVT_CHAR
);
649 if (wxTranslateKeyEvent(keyEvent
, dialog
, wid
, event
))
651 keyEvent
.SetEventObject(dialog
);
652 keyEvent
.SetId(dialog
->GetId());
653 keyEvent
.SetEventType(wxEVT_CHAR_HOOK
);
654 if (dialog
->GetEventHandler()->ProcessEvent(keyEvent
))
656 *continueToDispatch
= False
;
661 // For simplicity, OnKeyDown is the same as OnChar
662 // TODO: filter modifier key presses from OnChar
663 keyEvent
.SetEventType(wxEVT_KEY_DOWN
);
665 // Only process OnChar if OnKeyDown didn't swallow it
666 if (!dialog
->GetEventHandler()->ProcessEvent (keyEvent
))
668 keyEvent
.SetEventType(wxEVT_CHAR
);
669 dialog
->GetEventHandler()->ProcessEvent(keyEvent
);
675 *continueToDispatch
= True
;
678 static void wxUnmapBulletinBoard(Widget
WXUNUSED(dialog
), wxDialog
*WXUNUSED(client
), XtPointer
WXUNUSED(call
) )
680 /* This gets called when the dialog is being shown, which
681 * defeats modal showing.
682 client->m_modalShowing = FALSE ;
683 client->m_isShown = FALSE;
687 void wxDialog::ChangeFont(bool keepOriginalSize
)
689 wxWindow::ChangeFont(keepOriginalSize
);
692 void wxDialog::ChangeBackgroundColour()
695 DoChangeBackgroundColour(GetMainWidget(), m_backgroundColour
);
698 void wxDialog::ChangeForegroundColour()
701 DoChangeForegroundColour(GetMainWidget(), m_foregroundColour
);