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 wxDialogBoxRepaintProc(Widget w
, XtPointer c_data
, XEvent
*event
, char *);
46 static void wxDialogBoxEventHandler (Widget wid
,
47 XtPointer client_data
,
49 Boolean
*continueToDispatch
);
51 static void wxUnmapBulletinBoard(Widget dialog
, wxDialog
*client
,XtPointer call
);
53 // A stack of modal_showing flags, since we can't rely
54 // on accessing wxDialog::m_modalShowing within
55 // wxDialog::Show in case a callback has deleted the wxDialog.
56 static wxList wxModalShowingStack
;
58 // Lists to keep track of windows, so we can disable/enable them
60 wxList wxModalDialogs
;
61 wxList wxModelessWindows
; // Frames and modeless dialogs
62 extern wxList wxPendingDelete
;
64 #define wxUSE_INVISIBLE_RESIZE 1
66 #if !USE_SHARED_LIBRARY
67 IMPLEMENT_DYNAMIC_CLASS(wxDialog
, wxPanel
)
69 BEGIN_EVENT_TABLE(wxDialog
, wxPanel
)
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_windowFont
= 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 wxDialogBoxRepaintProc
, (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_modalShowing
= FALSE
;
263 if (!wxUSE_INVISIBLE_RESIZE
&& m_mainWidget
)
265 XtUnmapWidget((Widget
) m_mainWidget
);
268 wxTopLevelWindows
.DeleteObject(this);
270 if ( (GetWindowStyleFlag() & wxDIALOG_MODAL
) != wxDIALOG_MODAL
)
271 wxModelessWindows
.DeleteObject(this);
273 // If this is the last top-level window, exit.
274 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
276 wxTheApp
->SetTopWindow(NULL
);
278 if (wxTheApp
->GetExitOnFrameDelete())
280 wxTheApp
->ExitMainLoop();
284 // This event-flushing code used to be in wxWindow::PostDestroyChildren (wx_dialog.cpp)
285 // but I think this should work, if we destroy the children first.
286 // Note that this might need to be done for wxFrame also.
289 // The idea about doing it here is that if you have to remove the
290 // XtDestroyWidget from ~wxWindow, at least top-level windows
291 // will still be deleted (and destroy children implicitly).
294 DetachWidget(GetMainWidget()); // Removes event handlers
295 XtDestroyWidget((Widget
) GetMainWidget());
296 SetMainWidget((WXWidget
) NULL
);
300 // By default, pressing escape cancels the dialog
301 void wxDialog::OnCharHook(wxKeyEvent
& event
)
303 if (event
.m_keyCode
== WXK_ESCAPE
)
305 // Behaviour changed in 2.0: we'll send a Cancel message
306 // to the dialog instead of Close.
307 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
308 cancelEvent
.SetEventObject( this );
309 GetEventHandler()->ProcessEvent(cancelEvent
);
313 // We didn't process this event.
317 void wxDialog::Iconize(bool WXUNUSED(iconize
))
319 // Can't iconize a dialog in Motif, apparently
320 // TODO: try using the parent of m_mainShell.
321 // XtVaSetValues((Widget) m_mainWidget, XmNiconic, iconize, NULL);
324 bool wxDialog::IsIconized() const
328 XtVaGetValues((Widget) m_mainWidget, XmNiconic, &iconic, NULL);
335 void wxDialog::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
337 XtVaSetValues((Widget
) m_mainWidget
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
338 wxWindow::SetSize(x
, y
, width
, height
, sizeFlags
);
339 XtVaSetValues((Widget
) m_mainWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
342 void wxDialog::SetClientSize(int width
, int height
)
344 SetSize(-1, -1, width
, height
);
348 void wxDialog::SetTitle(const wxString
& title
)
350 m_dialogTitle
= title
;
353 XmString str
= XmStringCreateSimple((char*) (const char*) title
);
354 XtVaSetValues((Widget
) m_mainWidget
,
355 XmNtitle
, (char*) (const char*) title
,
356 XmNdialogTitle
, str
, // Roberto Cocchi
357 XmNiconName
, (char*) (const char*) title
,
363 wxString
wxDialog::GetTitle() const
365 return m_dialogTitle
;
368 void wxDialog::Centre(int direction
)
370 int x_offset
,y_offset
;
371 int display_width
, display_height
;
372 int width
, height
, x
, y
;
373 wxWindow
*parent
= GetParent();
374 if ((direction
& wxCENTER_FRAME
) && parent
)
376 parent
->GetPosition(&x_offset
,&y_offset
) ;
377 parent
->GetSize(&display_width
,&display_height
) ;
381 wxDisplaySize(&display_width
, &display_height
);
386 GetSize(&width
, &height
);
389 if (direction
& wxHORIZONTAL
)
390 x
= (int)((display_width
- width
)/2);
391 if (direction
& wxVERTICAL
)
392 y
= (int)((display_height
- height
)/2);
394 SetSize(x
+x_offset
, y
+y_offset
, width
, height
);
397 void wxDialog::Raise()
399 Window parent_window
= XtWindow((Widget
) m_mainWidget
),
400 next_parent
= XtWindow((Widget
) m_mainWidget
),
401 root
= RootWindowOfScreen(XtScreen((Widget
) m_mainWidget
));
402 // search for the parent that is child of ROOT, because the WM may
403 // reparent twice and notify only the next parent (like FVWM)
404 while (next_parent
!= root
) {
405 Window
*theChildren
; unsigned int n
;
406 parent_window
= next_parent
;
407 XQueryTree(XtDisplay((Widget
) m_mainWidget
), parent_window
, &root
,
408 &next_parent
, &theChildren
, &n
);
409 XFree(theChildren
); // not needed
411 XRaiseWindow(XtDisplay((Widget
) m_mainWidget
), parent_window
);
414 void wxDialog::Lower()
416 Window parent_window
= XtWindow((Widget
) m_mainWidget
),
417 next_parent
= XtWindow((Widget
) m_mainWidget
),
418 root
= RootWindowOfScreen(XtScreen((Widget
) m_mainWidget
));
419 // search for the parent that is child of ROOT, because the WM may
420 // reparent twice and notify only the next parent (like FVWM)
421 while (next_parent
!= root
) {
422 Window
*theChildren
; unsigned int n
;
423 parent_window
= next_parent
;
424 XQueryTree(XtDisplay((Widget
) m_mainWidget
), parent_window
, &root
,
425 &next_parent
, &theChildren
, &n
);
426 XFree(theChildren
); // not needed
428 XLowerWindow(XtDisplay((Widget
) m_mainWidget
), parent_window
);
431 bool wxDialog::Show(bool show
)
437 if (!wxUSE_INVISIBLE_RESIZE
)
438 XtMapWidget(XtParent((Widget
) m_mainWidget
));
440 XtManageChild((Widget
) m_mainWidget
) ;
442 XRaiseWindow(XtDisplay((Widget
) m_mainWidget
), XtWindow((Widget
) m_mainWidget
));
447 if (!wxUSE_INVISIBLE_RESIZE
)
448 XtUnmapWidget(XtParent((Widget
) m_mainWidget
));
450 XtUnmanageChild((Widget
) m_mainWidget
) ;
452 XFlush(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()));
453 XSync(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()), FALSE
);
459 // Shows a dialog modally, returning a return code
460 int wxDialog::ShowModal()
462 m_windowStyle
|= wxDIALOG_MODAL
;
469 wxModalShowingStack
.Insert((wxObject
*)TRUE
);
471 m_modalShowing
= TRUE
;
472 XtAddGrab((Widget
) m_mainWidget
, TRUE
, FALSE
);
476 // Loop until we signal that the dialog should be closed
477 while ((wxModalShowingStack
.Number() > 0) && ((int)(wxModalShowingStack
.First()->Data()) != 0))
479 // XtAppProcessEvent((XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
481 XtAppNextEvent((XtAppContext
) wxTheApp
->GetAppContext(), &event
);
482 wxTheApp
->ProcessXEvent((WXEvent
*) &event
);
485 // Remove modal dialog flag from stack
486 wxNode
*node
= wxModalShowingStack
.First();
490 // Now process all events in case they get sent to a destroyed dialog
491 XSync(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()), FALSE
);
492 while (XtAppPending((XtAppContext
) wxTheApp
->GetAppContext()))
494 XFlush(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()));
495 XtAppNextEvent((XtAppContext
) wxTheApp
->GetAppContext(), &event
);
497 wxTheApp
->ProcessXEvent((WXEvent
*) &event
);
500 // TODO: is it safe to call this, if the dialog may have been deleted
501 // by now? Probably only if we're using delayed deletion of dialogs.
502 return GetReturnCode();
505 void wxDialog::EndModal(int retCode
)
510 SetReturnCode(retCode
);
512 // Strangely, we don't seem to need this now.
513 // XtRemoveGrab((Widget) m_mainWidget);
517 m_modalShowing
= FALSE
;
519 wxNode
*node
= wxModalShowingStack
.First();
521 node
->SetData((wxObject
*)FALSE
);
525 void wxDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
527 if ( Validate() && TransferDataFromWindow() )
533 SetReturnCode(wxID_OK
);
539 void wxDialog::OnApply(wxCommandEvent
& WXUNUSED(event
))
542 TransferDataFromWindow();
543 // TODO probably need to disable the Apply button until things change again
546 void wxDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
549 EndModal(wxID_CANCEL
);
552 SetReturnCode(wxID_CANCEL
);
557 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
559 // We'll send a Cancel message by default,
560 // which may close the dialog.
561 // Check for looping if the Cancel event handler calls Close().
563 // Note that if a cancel button and handler aren't present in the dialog,
564 // nothing will happen when you close the dialog via the window manager, or
566 // We wouldn't want to destroy the dialog by default, since the dialog may have been
567 // created on the stack.
568 // However, this does mean that calling dialog->Close() won't delete the dialog
569 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
570 // sure to destroy the dialog.
571 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
573 static wxList closing
;
575 if ( closing
.Member(this) )
578 closing
.Append(this);
580 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
581 cancelEvent
.SetEventObject( this );
582 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
584 closing
.DeleteObject(this);
587 // Destroy the window (delayed, if a managed window)
588 bool wxDialog::Destroy()
590 if (!wxPendingDelete
.Member(this))
591 wxPendingDelete
.Append(this);
595 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
597 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
606 // Handle a close event from the window manager
607 static void wxCloseDialogCallback( Widget
WXUNUSED(widget
), XtPointer client_data
,
608 XmAnyCallbackStruct
*WXUNUSED(cbs
))
610 wxDialog
*dialog
= (wxDialog
*)client_data
;
611 wxCloseEvent
closeEvent(wxEVT_CLOSE_WINDOW
, dialog
->GetId());
612 closeEvent
.SetEventObject(dialog
);
614 // May delete the dialog (with delayed deletion)
615 dialog
->GetEventHandler()->ProcessEvent(closeEvent
);
618 // TODO: Preferably, we should have a universal repaint proc.
619 // Meanwhile, use a special one for dialogs.
620 static void wxDialogBoxRepaintProc(Widget w
, XtPointer
WXUNUSED(c_data
), XEvent
*event
, char *)
625 wxWindow
* win
= (wxWindow
*)wxWidgetHashTable
->Get((long)w
);
629 switch(event
-> type
)
633 window
= (Window
) win
-> GetXWindow();
634 display
= (Display
*) win
-> GetXDisplay();
636 wxRect
* rect
= new wxRect(event
->xexpose
.x
, event
->xexpose
.y
,
637 event
->xexpose
.width
, event
->xexpose
.height
);
638 win
->m_updateRects
.Append((wxObject
*) rect
);
640 if (event
-> xexpose
.count
== 0)
644 win
->ClearUpdateRects();
650 cout
<< "\n\nNew Event ! is = " << event
-> type
<< "\n";
656 static void wxDialogBoxEventHandler (Widget wid
,
657 XtPointer
WXUNUSED(client_data
),
659 Boolean
*continueToDispatch
)
661 wxDialog
*dialog
= (wxDialog
*)wxWidgetHashTable
->Get((long)wid
);
664 wxMouseEvent
wxevent(wxEVT_NULL
);
665 if (wxTranslateMouseEvent(wxevent
, dialog
, wid
, event
))
667 wxevent
.SetEventObject(dialog
);
668 wxevent
.SetId(dialog
->GetId());
669 dialog
->GetEventHandler()->ProcessEvent(wxevent
);
673 // An attempt to implement OnCharHook by calling OnCharHook first;
674 // if this returns TRUE, set continueToDispatch to False
675 // (don't continue processing).
676 // Otherwise set it to True and call OnChar.
677 wxKeyEvent
keyEvent(wxEVT_CHAR
);
678 if (wxTranslateKeyEvent(keyEvent
, dialog
, wid
, event
))
680 keyEvent
.SetEventObject(dialog
);
681 keyEvent
.SetId(dialog
->GetId());
682 keyEvent
.SetEventType(wxEVT_CHAR_HOOK
);
683 if (dialog
->GetEventHandler()->ProcessEvent(keyEvent
))
685 *continueToDispatch
= False
;
690 // For simplicity, OnKeyDown is the same as OnChar
691 // TODO: filter modifier key presses from OnChar
692 keyEvent
.SetEventType(wxEVT_KEY_DOWN
);
694 // Only process OnChar if OnKeyDown didn't swallow it
695 if (!dialog
->GetEventHandler()->ProcessEvent (keyEvent
))
697 keyEvent
.SetEventType(wxEVT_CHAR
);
698 dialog
->GetEventHandler()->ProcessEvent(keyEvent
);
704 *continueToDispatch
= True
;
707 static void wxUnmapBulletinBoard(Widget
WXUNUSED(dialog
), wxDialog
*WXUNUSED(client
), XtPointer
WXUNUSED(call
) )
709 /* This gets called when the dialog is being shown, which
710 * defeats modal showing.
711 client->m_modalShowing = FALSE ;
712 client->m_isShown = FALSE;
716 void wxDialog::ChangeFont(bool keepOriginalSize
)
718 wxWindow::ChangeFont(keepOriginalSize
);
721 void wxDialog::ChangeBackgroundColour()
724 DoChangeBackgroundColour(GetMainWidget(), m_backgroundColour
);
727 void wxDialog::ChangeForegroundColour()
730 DoChangeForegroundColour(GetMainWidget(), m_foregroundColour
);