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 Widget dialogShell
= XmCreateBulletinBoardDialog(parentWidget
, (char*) (const char*) name
, args
, 1);
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 wxDialogBoxRepaintProc
, (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()
261 m_modalShowing
= FALSE
;
262 if (!wxUSE_INVISIBLE_RESIZE
&& m_mainWidget
)
264 XtUnmapWidget((Widget
) m_mainWidget
);
267 wxTopLevelWindows
.DeleteObject(this);
269 if ( (GetWindowStyleFlag() & wxDIALOG_MODAL
) != wxDIALOG_MODAL
)
270 wxModelessWindows
.DeleteObject(this);
272 // If this is the last top-level window, exit.
273 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
275 wxTheApp
->SetTopWindow(NULL
);
277 if (wxTheApp
->GetExitOnFrameDelete())
279 wxTheApp
->ExitMainLoop();
283 // This event-flushing code used to be in wxWindow::PostDestroyChildren (wx_dialog.cpp)
284 // but I think this should work, if we destroy the children first.
285 // Note that this might need to be done for wxFrame also.
288 // Now process all events, because otherwise
289 // this might remain on the screen.
292 display
= XtDisplay((Widget
) m_mainWidget
);
294 display
= (Display
*) wxGetDisplay();
296 XSync(display
, FALSE
);
298 while (XtAppPending((XtAppContext
) wxTheApp
->GetAppContext())) {
300 XtAppNextEvent((XtAppContext
) wxTheApp
->GetAppContext(), &event
);
301 XtDispatchEvent(&event
);
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::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
342 XtVaSetValues((Widget
) m_mainWidget
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
343 wxWindow::SetSize(x
, y
, width
, height
, sizeFlags
);
344 XtVaSetValues((Widget
) m_mainWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
347 void wxDialog::SetClientSize(int width
, int height
)
349 SetSize(-1, -1, width
, height
);
353 void wxDialog::SetTitle(const wxString
& title
)
355 m_dialogTitle
= title
;
358 XmString str
= XmStringCreateSimple((char*) (const char*) title
);
359 XtVaSetValues((Widget
) m_mainWidget
,
360 XmNtitle
, (char*) (const char*) title
,
361 XmNdialogTitle
, str
, // Roberto Cocchi
362 XmNiconName
, (char*) (const char*) title
,
368 wxString
wxDialog::GetTitle() const
370 return m_dialogTitle
;
373 void wxDialog::Centre(int direction
)
375 int x_offset
,y_offset
;
376 int display_width
, display_height
;
377 int width
, height
, x
, y
;
378 wxWindow
*parent
= GetParent();
379 if ((direction
& wxCENTER_FRAME
) && parent
)
381 parent
->GetPosition(&x_offset
,&y_offset
) ;
382 parent
->GetSize(&display_width
,&display_height
) ;
386 wxDisplaySize(&display_width
, &display_height
);
391 GetSize(&width
, &height
);
394 if (direction
& wxHORIZONTAL
)
395 x
= (int)((display_width
- width
)/2);
396 if (direction
& wxVERTICAL
)
397 y
= (int)((display_height
- height
)/2);
399 SetSize(x
+x_offset
, y
+y_offset
, width
, height
);
402 void wxDialog::Raise()
404 Window parent_window
= XtWindow((Widget
) m_mainWidget
),
405 next_parent
= XtWindow((Widget
) m_mainWidget
),
406 root
= RootWindowOfScreen(XtScreen((Widget
) m_mainWidget
));
407 // search for the parent that is child of ROOT, because the WM may
408 // reparent twice and notify only the next parent (like FVWM)
409 while (next_parent
!= root
) {
410 Window
*theChildren
; unsigned int n
;
411 parent_window
= next_parent
;
412 XQueryTree(XtDisplay((Widget
) m_mainWidget
), parent_window
, &root
,
413 &next_parent
, &theChildren
, &n
);
414 XFree(theChildren
); // not needed
416 XRaiseWindow(XtDisplay((Widget
) m_mainWidget
), parent_window
);
419 void wxDialog::Lower()
421 Window parent_window
= XtWindow((Widget
) m_mainWidget
),
422 next_parent
= XtWindow((Widget
) m_mainWidget
),
423 root
= RootWindowOfScreen(XtScreen((Widget
) m_mainWidget
));
424 // search for the parent that is child of ROOT, because the WM may
425 // reparent twice and notify only the next parent (like FVWM)
426 while (next_parent
!= root
) {
427 Window
*theChildren
; unsigned int n
;
428 parent_window
= next_parent
;
429 XQueryTree(XtDisplay((Widget
) m_mainWidget
), parent_window
, &root
,
430 &next_parent
, &theChildren
, &n
);
431 XFree(theChildren
); // not needed
433 XLowerWindow(XtDisplay((Widget
) m_mainWidget
), parent_window
);
436 bool wxDialog::Show(bool show
)
442 if (!wxUSE_INVISIBLE_RESIZE
)
443 XtMapWidget(XtParent((Widget
) m_mainWidget
));
445 XtManageChild((Widget
) m_mainWidget
) ;
447 XRaiseWindow(XtDisplay((Widget
) m_mainWidget
), XtWindow((Widget
) m_mainWidget
));
452 if (!wxUSE_INVISIBLE_RESIZE
)
453 XtUnmapWidget(XtParent((Widget
) m_mainWidget
));
455 XtUnmanageChild((Widget
) m_mainWidget
) ;
457 XFlush(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()));
458 XSync(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()), FALSE
);
464 // Shows a dialog modally, returning a return code
465 int wxDialog::ShowModal()
467 m_windowStyle
|= wxDIALOG_MODAL
;
474 wxModalShowingStack
.Insert((wxObject
*)TRUE
);
476 m_modalShowing
= TRUE
;
477 XtAddGrab((Widget
) m_mainWidget
, TRUE
, FALSE
);
480 // Loop until we signal that the dialog should be closed
481 while ((wxModalShowingStack
.Number() > 0) && (bool)wxModalShowingStack
.First()->Data())
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 XtRemoveGrab((Widget
) m_mainWidget
);
520 m_modalShowing
= FALSE
;
522 wxNode
*node
= wxModalShowingStack
.First();
524 node
->SetData((wxObject
*)FALSE
);
528 void wxDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
530 if ( Validate() && TransferDataFromWindow() )
536 SetReturnCode(wxID_OK
);
542 void wxDialog::OnApply(wxCommandEvent
& WXUNUSED(event
))
545 TransferDataFromWindow();
546 // TODO probably need to disable the Apply button until things change again
549 void wxDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
552 EndModal(wxID_CANCEL
);
555 SetReturnCode(wxID_CANCEL
);
560 bool wxDialog::OnClose()
562 // Behaviour changed in 2.0: we'll send a Cancel message by default,
563 // which may close the dialog.
564 // Check for looping if the Cancel event handler calls Close()
566 static wxList closing
;
568 if ( closing
.Member(this) )
571 closing
.Append(this);
573 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
574 cancelEvent
.SetEventObject( this );
575 GetEventHandler()->ProcessEvent(cancelEvent
);
577 closing
.DeleteObject(this);
582 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
585 if ( GetEventHandler()->OnClose() || event
.GetForce())
591 // Destroy the window (delayed, if a managed window)
592 bool wxDialog::Destroy()
594 if (!wxPendingDelete
.Member(this))
595 wxPendingDelete
.Append(this);
599 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
601 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
609 // Handle a close event from the window manager
610 static void wxCloseDialogCallback( Widget
WXUNUSED(widget
), XtPointer client_data
,
611 XmAnyCallbackStruct
*WXUNUSED(cbs
))
613 wxDialog
*dialog
= (wxDialog
*)client_data
;
614 wxCloseEvent
closeEvent(wxEVT_CLOSE_WINDOW
, dialog
->GetId());
615 closeEvent
.SetEventObject(dialog
);
617 // May delete the dialog (with delayed deletion)
618 dialog
->GetEventHandler()->ProcessEvent(closeEvent
);
621 // TODO: Preferably, we should have a universal repaint proc.
622 // Meanwhile, use a special one for dialogs.
623 static void wxDialogBoxRepaintProc(Widget w
, XtPointer
WXUNUSED(c_data
), XEvent
*event
, char *)
628 wxWindow
* win
= (wxWindow
*)wxWidgetHashTable
->Get((long)w
);
632 switch(event
-> type
)
636 window
= (Window
) win
-> GetXWindow();
637 display
= (Display
*) win
-> GetXDisplay();
639 wxRect
* rect
= new wxRect(event
->xexpose
.x
, event
->xexpose
.y
,
640 event
->xexpose
.width
, event
->xexpose
.height
);
641 win
->m_updateRects
.Append((wxObject
*) rect
);
643 if (event
-> xexpose
.count
== 0)
645 wxPaintEvent
event(win
->GetId());
646 event
.SetEventObject(win
);
647 win
->GetEventHandler()->ProcessEvent(event
);
649 win
->ClearUpdateRects();
655 cout
<< "\n\nNew Event ! is = " << event
-> type
<< "\n";
661 static void wxDialogBoxEventHandler (Widget wid
,
662 XtPointer
WXUNUSED(client_data
),
664 Boolean
*continueToDispatch
)
666 wxDialog
*dialog
= (wxDialog
*)wxWidgetHashTable
->Get((long)wid
);
669 wxMouseEvent
wxevent(wxEVT_NULL
);
670 if (wxTranslateMouseEvent(wxevent
, dialog
, wid
, event
))
672 wxevent
.SetEventObject(dialog
);
673 wxevent
.SetId(dialog
->GetId());
674 dialog
->GetEventHandler()->ProcessEvent(wxevent
);
678 // An attempt to implement OnCharHook by calling OnCharHook first;
679 // if this returns TRUE, set continueToDispatch to False
680 // (don't continue processing).
681 // Otherwise set it to True and call OnChar.
682 wxKeyEvent
keyEvent(wxEVENT_TYPE_CHAR
);
683 if (wxTranslateKeyEvent(keyEvent
, dialog
, wid
, event
))
685 keyEvent
.SetEventObject(dialog
);
686 keyEvent
.SetId(dialog
->GetId());
687 keyEvent
.SetEventType(wxEVT_CHAR_HOOK
);
688 if (dialog
->GetEventHandler()->ProcessEvent(keyEvent
))
690 *continueToDispatch
= False
;
695 keyEvent
.SetEventType(wxEVT_CHAR
);
696 dialog
->GetEventHandler()->ProcessEvent(keyEvent
);
701 *continueToDispatch
= True
;
704 static void wxUnmapBulletinBoard(Widget
WXUNUSED(dialog
), wxDialog
*WXUNUSED(client
), XtPointer
WXUNUSED(call
) )
706 /* This gets called when the dialog is being shown, which
707 * defeats modal showing.
708 client->m_modalShowing = FALSE ;
709 client->m_isShown = FALSE;
713 void wxDialog::ChangeFont(bool WXUNUSED(keepOriginalSize
))
718 void wxDialog::ChangeBackgroundColour()
723 void wxDialog::ChangeForegroundColour()