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 // Now process all events, because otherwise
290 // this might remain on the screen.
293 display
= XtDisplay((Widget
) m_mainWidget
);
295 display
= (Display
*) wxGetDisplay();
297 XSync(display
, FALSE
);
299 while (XtAppPending((XtAppContext
) wxTheApp
->GetAppContext())) {
301 XtAppNextEvent((XtAppContext
) wxTheApp
->GetAppContext(), &event
);
302 XtDispatchEvent(&event
);
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 bool wxDialog::IsIconized() const
334 XtVaGetValues((Widget) m_mainWidget, XmNiconic, &iconic, NULL);
341 void wxDialog::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
343 XtVaSetValues((Widget
) m_mainWidget
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
344 wxWindow::SetSize(x
, y
, width
, height
, sizeFlags
);
345 XtVaSetValues((Widget
) m_mainWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
348 void wxDialog::SetClientSize(int width
, int height
)
350 SetSize(-1, -1, width
, height
);
354 void wxDialog::SetTitle(const wxString
& title
)
356 m_dialogTitle
= title
;
359 XmString str
= XmStringCreateSimple((char*) (const char*) title
);
360 XtVaSetValues((Widget
) m_mainWidget
,
361 XmNtitle
, (char*) (const char*) title
,
362 XmNdialogTitle
, str
, // Roberto Cocchi
363 XmNiconName
, (char*) (const char*) title
,
369 wxString
wxDialog::GetTitle() const
371 return m_dialogTitle
;
374 void wxDialog::Centre(int direction
)
376 int x_offset
,y_offset
;
377 int display_width
, display_height
;
378 int width
, height
, x
, y
;
379 wxWindow
*parent
= GetParent();
380 if ((direction
& wxCENTER_FRAME
) && parent
)
382 parent
->GetPosition(&x_offset
,&y_offset
) ;
383 parent
->GetSize(&display_width
,&display_height
) ;
387 wxDisplaySize(&display_width
, &display_height
);
392 GetSize(&width
, &height
);
395 if (direction
& wxHORIZONTAL
)
396 x
= (int)((display_width
- width
)/2);
397 if (direction
& wxVERTICAL
)
398 y
= (int)((display_height
- height
)/2);
400 SetSize(x
+x_offset
, y
+y_offset
, width
, height
);
403 void wxDialog::Raise()
405 Window parent_window
= XtWindow((Widget
) m_mainWidget
),
406 next_parent
= XtWindow((Widget
) m_mainWidget
),
407 root
= RootWindowOfScreen(XtScreen((Widget
) m_mainWidget
));
408 // search for the parent that is child of ROOT, because the WM may
409 // reparent twice and notify only the next parent (like FVWM)
410 while (next_parent
!= root
) {
411 Window
*theChildren
; unsigned int n
;
412 parent_window
= next_parent
;
413 XQueryTree(XtDisplay((Widget
) m_mainWidget
), parent_window
, &root
,
414 &next_parent
, &theChildren
, &n
);
415 XFree(theChildren
); // not needed
417 XRaiseWindow(XtDisplay((Widget
) m_mainWidget
), parent_window
);
420 void wxDialog::Lower()
422 Window parent_window
= XtWindow((Widget
) m_mainWidget
),
423 next_parent
= XtWindow((Widget
) m_mainWidget
),
424 root
= RootWindowOfScreen(XtScreen((Widget
) m_mainWidget
));
425 // search for the parent that is child of ROOT, because the WM may
426 // reparent twice and notify only the next parent (like FVWM)
427 while (next_parent
!= root
) {
428 Window
*theChildren
; unsigned int n
;
429 parent_window
= next_parent
;
430 XQueryTree(XtDisplay((Widget
) m_mainWidget
), parent_window
, &root
,
431 &next_parent
, &theChildren
, &n
);
432 XFree(theChildren
); // not needed
434 XLowerWindow(XtDisplay((Widget
) m_mainWidget
), parent_window
);
437 bool wxDialog::Show(bool show
)
443 if (!wxUSE_INVISIBLE_RESIZE
)
444 XtMapWidget(XtParent((Widget
) m_mainWidget
));
446 XtManageChild((Widget
) m_mainWidget
) ;
448 XRaiseWindow(XtDisplay((Widget
) m_mainWidget
), XtWindow((Widget
) m_mainWidget
));
453 if (!wxUSE_INVISIBLE_RESIZE
)
454 XtUnmapWidget(XtParent((Widget
) m_mainWidget
));
456 XtUnmanageChild((Widget
) m_mainWidget
) ;
458 XFlush(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()));
459 XSync(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()), FALSE
);
465 // Shows a dialog modally, returning a return code
466 int wxDialog::ShowModal()
468 m_windowStyle
|= wxDIALOG_MODAL
;
475 wxModalShowingStack
.Insert((wxObject
*)TRUE
);
477 m_modalShowing
= TRUE
;
478 XtAddGrab((Widget
) m_mainWidget
, TRUE
, FALSE
);
482 // Loop until we signal that the dialog should be closed
483 while ((wxModalShowingStack
.Number() > 0) && ((int)(wxModalShowingStack
.First()->Data()) != 0))
485 // XtAppProcessEvent((XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
487 XtAppNextEvent((XtAppContext
) wxTheApp
->GetAppContext(), &event
);
488 wxTheApp
->ProcessXEvent((WXEvent
*) &event
);
491 // Remove modal dialog flag from stack
492 wxNode
*node
= wxModalShowingStack
.First();
496 // Now process all events in case they get sent to a destroyed dialog
497 XSync(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()), FALSE
);
498 while (XtAppPending((XtAppContext
) wxTheApp
->GetAppContext()))
500 XFlush(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()));
501 XtAppNextEvent((XtAppContext
) wxTheApp
->GetAppContext(), &event
);
503 wxTheApp
->ProcessXEvent((WXEvent
*) &event
);
506 // TODO: is it safe to call this, if the dialog may have been deleted
507 // by now? Probably only if we're using delayed deletion of dialogs.
508 return GetReturnCode();
511 void wxDialog::EndModal(int retCode
)
516 SetReturnCode(retCode
);
518 XtRemoveGrab((Widget
) m_mainWidget
);
522 m_modalShowing
= FALSE
;
524 wxNode
*node
= wxModalShowingStack
.First();
526 node
->SetData((wxObject
*)FALSE
);
530 void wxDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
532 if ( Validate() && TransferDataFromWindow() )
538 SetReturnCode(wxID_OK
);
544 void wxDialog::OnApply(wxCommandEvent
& WXUNUSED(event
))
547 TransferDataFromWindow();
548 // TODO probably need to disable the Apply button until things change again
551 void wxDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
554 EndModal(wxID_CANCEL
);
557 SetReturnCode(wxID_CANCEL
);
562 bool wxDialog::OnClose()
564 // Behaviour changed in 2.0: we'll send a Cancel message by default,
565 // which may close the dialog.
566 // Check for looping if the Cancel event handler calls Close()
568 static wxList closing
;
570 if ( closing
.Member(this) )
573 closing
.Append(this);
575 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
576 cancelEvent
.SetEventObject( this );
577 GetEventHandler()->ProcessEvent(cancelEvent
);
579 closing
.DeleteObject(this);
584 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
587 if ( GetEventHandler()->OnClose() || event
.GetForce())
593 // Destroy the window (delayed, if a managed window)
594 bool wxDialog::Destroy()
596 if (!wxPendingDelete
.Member(this))
597 wxPendingDelete
.Append(this);
601 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
603 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
611 // Handle a close event from the window manager
612 static void wxCloseDialogCallback( Widget
WXUNUSED(widget
), XtPointer client_data
,
613 XmAnyCallbackStruct
*WXUNUSED(cbs
))
615 wxDialog
*dialog
= (wxDialog
*)client_data
;
616 wxCloseEvent
closeEvent(wxEVT_CLOSE_WINDOW
, dialog
->GetId());
617 closeEvent
.SetEventObject(dialog
);
619 // May delete the dialog (with delayed deletion)
620 dialog
->GetEventHandler()->ProcessEvent(closeEvent
);
623 // TODO: Preferably, we should have a universal repaint proc.
624 // Meanwhile, use a special one for dialogs.
625 static void wxDialogBoxRepaintProc(Widget w
, XtPointer
WXUNUSED(c_data
), XEvent
*event
, char *)
630 wxWindow
* win
= (wxWindow
*)wxWidgetHashTable
->Get((long)w
);
634 switch(event
-> type
)
638 window
= (Window
) win
-> GetXWindow();
639 display
= (Display
*) win
-> GetXDisplay();
641 wxRect
* rect
= new wxRect(event
->xexpose
.x
, event
->xexpose
.y
,
642 event
->xexpose
.width
, event
->xexpose
.height
);
643 win
->m_updateRects
.Append((wxObject
*) rect
);
645 if (event
-> xexpose
.count
== 0)
647 wxPaintEvent
event(win
->GetId());
648 event
.SetEventObject(win
);
649 win
->GetEventHandler()->ProcessEvent(event
);
651 win
->ClearUpdateRects();
657 cout
<< "\n\nNew Event ! is = " << event
-> type
<< "\n";
663 static void wxDialogBoxEventHandler (Widget wid
,
664 XtPointer
WXUNUSED(client_data
),
666 Boolean
*continueToDispatch
)
668 wxDialog
*dialog
= (wxDialog
*)wxWidgetHashTable
->Get((long)wid
);
671 wxMouseEvent
wxevent(wxEVT_NULL
);
672 if (wxTranslateMouseEvent(wxevent
, dialog
, wid
, event
))
674 wxevent
.SetEventObject(dialog
);
675 wxevent
.SetId(dialog
->GetId());
676 dialog
->GetEventHandler()->ProcessEvent(wxevent
);
680 // An attempt to implement OnCharHook by calling OnCharHook first;
681 // if this returns TRUE, set continueToDispatch to False
682 // (don't continue processing).
683 // Otherwise set it to True and call OnChar.
684 wxKeyEvent
keyEvent(wxEVENT_TYPE_CHAR
);
685 if (wxTranslateKeyEvent(keyEvent
, dialog
, wid
, event
))
687 keyEvent
.SetEventObject(dialog
);
688 keyEvent
.SetId(dialog
->GetId());
689 keyEvent
.SetEventType(wxEVT_CHAR_HOOK
);
690 if (dialog
->GetEventHandler()->ProcessEvent(keyEvent
))
692 *continueToDispatch
= False
;
697 keyEvent
.SetEventType(wxEVT_CHAR
);
698 dialog
->GetEventHandler()->ProcessEvent(keyEvent
);
703 *continueToDispatch
= True
;
706 static void wxUnmapBulletinBoard(Widget
WXUNUSED(dialog
), wxDialog
*WXUNUSED(client
), XtPointer
WXUNUSED(call
) )
708 /* This gets called when the dialog is being shown, which
709 * defeats modal showing.
710 client->m_modalShowing = FALSE ;
711 client->m_isShown = FALSE;
715 void wxDialog::ChangeFont(bool keepOriginalSize
)
717 wxWindow::ChangeFont(keepOriginalSize
);
720 void wxDialog::ChangeBackgroundColour()
723 DoChangeBackgroundColour(GetMainWidget(), m_backgroundColour
);
726 void wxDialog::ChangeForegroundColour()
729 DoChangeForegroundColour(GetMainWidget(), m_foregroundColour
);