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 SetBackgroundColour(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 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
101 wxTopLevelWindows
.Append(this);
103 if (parent
) parent
->AddChild(this);
106 m_windowId
= (int)NewControlId();
110 Widget parentWidget
= (Widget
) 0;
112 parentWidget
= (Widget
) parent
->GetTopWidget();
114 parentWidget
= (Widget
) wxTheApp
->GetTopLevelWidget();
116 wxASSERT_MSG( (parentWidget
!= (Widget
) 0), "Could not find a suitable parent shell for dialog." );
119 XtSetArg (args
[0], XmNdefaultPosition
, False
);
120 Widget dialogShell
= XmCreateBulletinBoardDialog(parentWidget
, (char*) (const char*) name
, args
, 1);
121 m_mainWidget
= (WXWidget
) dialogShell
;
123 // We don't want margins, since there is enough elsewhere.
124 XtVaSetValues(dialogShell
,
127 XmNresizePolicy
, XmRESIZE_NONE
,
130 Widget shell
= XtParent(dialogShell
) ;
133 XmString str
= XmStringCreateSimple((char*) (const char*)title
);
134 XtVaSetValues(dialogShell
,
140 wxAddWindowToTable(dialogShell
, this);
142 // Intercept CLOSE messages from the window manager
143 Atom WM_DELETE_WINDOW
= XmInternAtom(XtDisplay(shell
), "WM_DELETE_WINDOW", False
);
145 /* MATTHEW: [8] Remove and add WM_DELETE_WINDOW so ours is only handler */
146 /* Why do we have to do this for wxDialog, but not wxFrame? */
147 XmRemoveWMProtocols(shell
, &WM_DELETE_WINDOW
, 1);
148 XmAddWMProtocols(shell
, &WM_DELETE_WINDOW
, 1);
149 XmActivateWMProtocol(shell
, WM_DELETE_WINDOW
);
151 // Modified Steve Hammes for Motif 2.0
152 #if (XmREVISION > 1 || XmVERSION > 1)
153 XmAddWMProtocolCallback(shell
, WM_DELETE_WINDOW
, (XtCallbackProc
) wxCloseDialogCallback
, (XtPointer
)this);
154 #elif XmREVISION == 1
155 XmAddWMProtocolCallback(shell
, WM_DELETE_WINDOW
, (XtCallbackProc
) wxCloseDialogCallback
, (caddr_t
)this);
157 XmAddWMProtocolCallback(shell
, WM_DELETE_WINDOW
, (void (*)())wxCloseDialogCallback
, (caddr_t
)this);
161 XtOverrideTranslations(dialogShell
,
162 ptr
= XtParseTranslationTable("<Configure>: resize()"));
165 // Can't remember what this was about... but I think it's necessary.
167 if (wxUSE_INVISIBLE_RESIZE
)
170 XtVaSetValues(dialogShell
, XmNx
, pos
.x
,
173 XtVaSetValues(dialogShell
, XmNy
, pos
.y
,
177 XtVaSetValues(dialogShell
, XmNwidth
, size
.x
, NULL
);
179 XtVaSetValues(dialogShell
, XmNheight
, size
.y
, NULL
);
182 // This patch come from Torsten Liermann lier@lier1.muc.de
183 if (XmIsMotifWMRunning(shell
))
186 if (m_windowStyle
& wxRESIZE_BORDER
)
187 decor
|= MWM_DECOR_RESIZEH
;
188 if (m_windowStyle
& wxSYSTEM_MENU
)
189 decor
|= MWM_DECOR_MENU
;
190 if ((m_windowStyle
& wxCAPTION
) ||
191 (m_windowStyle
& wxTINY_CAPTION_HORIZ
) ||
192 (m_windowStyle
& wxTINY_CAPTION_VERT
))
193 decor
|= MWM_DECOR_TITLE
;
194 if (m_windowStyle
& wxTHICK_FRAME
)
195 decor
|= MWM_DECOR_BORDER
;
196 if (m_windowStyle
& wxMINIMIZE_BOX
)
197 decor
|= MWM_DECOR_MINIMIZE
;
198 if (m_windowStyle
& wxMAXIMIZE_BOX
)
199 decor
|= MWM_DECOR_MAXIMIZE
;
201 XtVaSetValues(shell
,XmNmwmDecorations
,decor
,NULL
) ;
203 // This allows non-Motif window managers to support at least the
204 // no-decorations case.
207 if ((m_windowStyle
& wxCAPTION
) != wxCAPTION
)
208 XtVaSetValues((Widget
) shell
,XmNoverrideRedirect
,TRUE
,NULL
);
211 XtRealizeWidget(dialogShell
);
213 XtAddCallback(dialogShell
,XmNunmapCallback
,
214 (XtCallbackProc
)wxUnmapBulletinBoard
,this) ;
216 // Positioning of the dialog doesn't work properly unless the dialog
217 // is managed, so we manage without mapping to the screen.
218 // To show, we map the shell (actually it's parent).
219 if (!wxUSE_INVISIBLE_RESIZE
)
220 XtVaSetValues(shell
, XmNmappedWhenManaged
, FALSE
, NULL
);
222 if (!wxUSE_INVISIBLE_RESIZE
)
224 XtManageChild(dialogShell
);
225 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
227 XtAddEventHandler(dialogShell
,ExposureMask
,FALSE
,
228 wxDialogBoxRepaintProc
, (XtPointer
) this);
230 XtAddEventHandler(dialogShell
,
231 ButtonPressMask
| ButtonReleaseMask
| PointerMotionMask
| KeyPressMask
,
233 wxDialogBoxEventHandler
,
239 void wxDialog::SetModal(bool flag
)
242 m_windowStyle
|= wxDIALOG_MODAL
;
244 if ( m_windowStyle
& wxDIALOG_MODAL
)
245 m_windowStyle
-= wxDIALOG_MODAL
;
247 wxModelessWindows
.DeleteObject(this);
249 wxModelessWindows
.Append(this);
252 wxDialog::~wxDialog()
254 m_modalShowing
= FALSE
;
255 if (!wxUSE_INVISIBLE_RESIZE
&& m_mainWidget
)
257 XtUnmapWidget((Widget
) m_mainWidget
);
260 wxTopLevelWindows
.DeleteObject(this);
262 if ( (GetWindowStyleFlag() & wxDIALOG_MODAL
) != wxDIALOG_MODAL
)
263 wxModelessWindows
.DeleteObject(this);
265 // If this is the last top-level window, exit.
266 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
268 wxTheApp
->SetTopWindow(NULL
);
270 if (wxTheApp
->GetExitOnFrameDelete())
272 wxTheApp
->ExitMainLoop();
276 // This event-flushing code used to be in wxWindow::PostDestroyChildren (wx_dialog.cpp)
277 // but I think this should work, if we destroy the children first.
278 // Note that this might need to be done for wxFrame also.
281 // Now process all events, because otherwise
282 // this might remain on the screen.
283 XSync(XtDisplay(XtParent((Widget
) m_mainWidget
)), FALSE
);
285 while (XtAppPending((XtAppContext
) wxTheApp
->GetAppContext())) {
286 XFlush(XtDisplay((Widget
) XtParent((Widget
) m_mainWidget
)));
287 XtAppNextEvent((XtAppContext
) wxTheApp
->GetAppContext(), &event
);
288 XtDispatchEvent(&event
);
292 // By default, pressing escape cancels the dialog
293 void wxDialog::OnCharHook(wxKeyEvent
& event
)
295 if (event
.m_keyCode
== WXK_ESCAPE
)
297 // Behaviour changed in 2.0: we'll send a Cancel message
298 // to the dialog instead of Close.
299 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
300 cancelEvent
.SetEventObject( this );
301 GetEventHandler()->ProcessEvent(cancelEvent
);
305 // We didn't process this event.
309 void wxDialog::Iconize(bool WXUNUSED(iconize
))
311 // Can't iconize a dialog in Motif, apparently
312 // TODO: try using the parent of m_mainShell.
313 // XtVaSetValues((Widget) m_mainWidget, XmNiconic, iconize, NULL);
316 bool wxDialog::IsIconized() const
320 XtVaGetValues((Widget) m_mainWidget, XmNiconic, &iconic, NULL);
327 void wxDialog::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
329 XtVaSetValues((Widget
) m_mainWidget
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
330 wxWindow::SetSize(x
, y
, width
, height
, sizeFlags
);
331 XtVaSetValues((Widget
) m_mainWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
334 void wxDialog::SetClientSize(int width
, int height
)
336 SetSize(-1, -1, width
, height
);
340 void wxDialog::SetTitle(const wxString
& title
)
342 m_dialogTitle
= title
;
345 XmString str
= XmStringCreateSimple((char*) (const char*) title
);
346 XtVaSetValues((Widget
) m_mainWidget
,
347 XmNtitle
, (char*) (const char*) title
,
348 XmNdialogTitle
, str
, // Roberto Cocchi
349 XmNiconName
, (char*) (const char*) title
,
355 wxString
wxDialog::GetTitle() const
357 return m_dialogTitle
;
360 void wxDialog::Centre(int direction
)
362 int x_offset
,y_offset
;
363 int display_width
, display_height
;
364 int width
, height
, x
, y
;
365 wxWindow
*parent
= GetParent();
366 if ((direction
& wxCENTER_FRAME
) && parent
)
368 parent
->GetPosition(&x_offset
,&y_offset
) ;
369 parent
->GetSize(&display_width
,&display_height
) ;
373 wxDisplaySize(&display_width
, &display_height
);
378 GetSize(&width
, &height
);
381 if (direction
& wxHORIZONTAL
)
382 x
= (int)((display_width
- width
)/2);
383 if (direction
& wxVERTICAL
)
384 y
= (int)((display_height
- height
)/2);
386 SetSize(x
+x_offset
, y
+y_offset
, width
, height
);
389 void wxDialog::Raise()
391 Window parent_window
= XtWindow((Widget
) m_mainWidget
),
392 next_parent
= XtWindow((Widget
) m_mainWidget
),
393 root
= RootWindowOfScreen(XtScreen((Widget
) m_mainWidget
));
394 // search for the parent that is child of ROOT, because the WM may
395 // reparent twice and notify only the next parent (like FVWM)
396 while (next_parent
!= root
) {
397 Window
*theChildren
; unsigned int n
;
398 parent_window
= next_parent
;
399 XQueryTree(XtDisplay((Widget
) m_mainWidget
), parent_window
, &root
,
400 &next_parent
, &theChildren
, &n
);
401 XFree(theChildren
); // not needed
403 XRaiseWindow(XtDisplay((Widget
) m_mainWidget
), parent_window
);
406 void wxDialog::Lower()
408 Window parent_window
= XtWindow((Widget
) m_mainWidget
),
409 next_parent
= XtWindow((Widget
) m_mainWidget
),
410 root
= RootWindowOfScreen(XtScreen((Widget
) m_mainWidget
));
411 // search for the parent that is child of ROOT, because the WM may
412 // reparent twice and notify only the next parent (like FVWM)
413 while (next_parent
!= root
) {
414 Window
*theChildren
; unsigned int n
;
415 parent_window
= next_parent
;
416 XQueryTree(XtDisplay((Widget
) m_mainWidget
), parent_window
, &root
,
417 &next_parent
, &theChildren
, &n
);
418 XFree(theChildren
); // not needed
420 XLowerWindow(XtDisplay((Widget
) m_mainWidget
), parent_window
);
423 bool wxDialog::Show(bool show
)
429 if (!wxUSE_INVISIBLE_RESIZE
)
430 XtMapWidget(XtParent((Widget
) m_mainWidget
));
432 XtManageChild((Widget
) m_mainWidget
) ;
434 XRaiseWindow(XtDisplay((Widget
) m_mainWidget
), XtWindow((Widget
) m_mainWidget
));
439 if (!wxUSE_INVISIBLE_RESIZE
)
440 XtUnmapWidget(XtParent((Widget
) m_mainWidget
));
442 XtUnmanageChild((Widget
) m_mainWidget
) ;
444 XFlush(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()));
445 XSync(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()), FALSE
);
451 // Shows a dialog modally, returning a return code
452 int wxDialog::ShowModal()
454 m_windowStyle
|= wxDIALOG_MODAL
;
461 wxModalShowingStack
.Insert((wxObject
*)TRUE
);
463 m_modalShowing
= TRUE
;
464 XtAddGrab((Widget
) m_mainWidget
, TRUE
, FALSE
);
467 // Loop until we signal that the dialog should be closed
468 while ((wxModalShowingStack
.Number() > 0) && (bool)wxModalShowingStack
.First()->Data())
470 XtAppProcessEvent((XtAppContext
) wxTheApp
->GetAppContext(), XtIMAll
);
473 // Remove modal dialog flag from stack
474 wxNode
*node
= wxModalShowingStack
.First();
478 // Now process all events in case they get sent to a destroyed dialog
479 XSync(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()), FALSE
);
480 while (XtAppPending((XtAppContext
) wxTheApp
->GetAppContext()))
482 XFlush(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()));
483 XtAppNextEvent((XtAppContext
) wxTheApp
->GetAppContext(), &event
);
484 XtDispatchEvent(&event
);
487 // TODO: is it safe to call this, if the dialog may have been deleted
488 // by now? Probably only if we're using delayed deletion of dialogs.
489 return GetReturnCode();
492 void wxDialog::EndModal(int retCode
)
497 SetReturnCode(retCode
);
499 XtRemoveGrab((Widget
) m_mainWidget
);
503 m_modalShowing
= FALSE
;
505 wxNode
*node
= wxModalShowingStack
.First();
507 node
->SetData((wxObject
*)FALSE
);
511 void wxDialog::OnOK(wxCommandEvent
& event
)
513 if ( Validate() && TransferDataFromWindow() )
519 SetReturnCode(wxID_OK
);
525 void wxDialog::OnApply(wxCommandEvent
& event
)
528 TransferDataFromWindow();
529 // TODO probably need to disable the Apply button until things change again
532 void wxDialog::OnCancel(wxCommandEvent
& event
)
535 EndModal(wxID_CANCEL
);
538 SetReturnCode(wxID_CANCEL
);
543 bool wxDialog::OnClose()
545 // Behaviour changed in 2.0: we'll send a Cancel message by default,
546 // which may close the dialog.
547 // Check for looping if the Cancel event handler calls Close()
549 static wxList closing
;
551 if ( closing
.Member(this) )
554 closing
.Append(this);
556 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
557 cancelEvent
.SetEventObject( this );
558 GetEventHandler()->ProcessEvent(cancelEvent
);
560 closing
.DeleteObject(this);
565 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
568 if ( GetEventHandler()->OnClose() || event
.GetForce())
574 // Destroy the window (delayed, if a managed window)
575 bool wxDialog::Destroy()
577 if (!wxPendingDelete
.Member(this))
578 wxPendingDelete
.Append(this);
582 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& event
)
584 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
592 // Handle a close event from the window manager
593 static void wxCloseDialogCallback(Widget widget
, XtPointer client_data
, XmAnyCallbackStruct
*cbs
)
595 wxDialog
*dialog
= (wxDialog
*)client_data
;
596 wxCloseEvent
closeEvent(wxEVT_CLOSE_WINDOW
, dialog
->GetId());
597 closeEvent
.SetEventObject(dialog
);
599 // May delete the dialog (with delayed deletion)
600 dialog
->GetEventHandler()->ProcessEvent(closeEvent
);
603 // TODO: Preferably, we should have a universal repaint proc.
604 // Meanwhile, use a special one for dialogs.
605 static void wxDialogBoxRepaintProc(Widget w
, XtPointer c_data
, XEvent
*event
, char *)
608 static XRectangle
*xrect
;
612 static int last_count
= 0;
613 static int draw_count
= 0;
615 wxWindow
* win
= (wxWindow
*)wxWidgetHashTable
->Get((long)w
);
619 switch(event
-> type
)
622 window
= (Window
) win
-> GetXWindow();
623 display
= (Display
*) win
-> GetXDisplay();
625 gc = (GC) panel -> GetDC() -> gc;
627 llp = event -> xexpose.count;
629 if ((last_count == 0) && (llp == 0))
631 xrect = new XRectangle[1];
632 xrect[0].x = event -> xexpose.x;
633 xrect[0].y = event -> xexpose.y;
634 xrect[0].width = event -> xexpose.width;
635 xrect[0].height = event -> xexpose.height;
637 XSetClipRectangles(display,gc,0,0,xrect,1,Unsorted);
638 // panel->DoPaint(xrect, 1);
639 panel->GetEventHandler()->OnPaint();
644 if ((last_count == 0) && (llp != 0))
646 xrect = new XRectangle[llp + 1];
647 draw_count = llp + 1;
649 xrect[draw_count - llp - 1].x = event -> xexpose.x;
650 xrect[draw_count - llp - 1].y = event -> xexpose.y;
651 xrect[draw_count - llp - 1].width = event -> xexpose.width;
652 xrect[draw_count - llp - 1].height = event -> xexpose.height;
655 if ((last_count != 0) && (llp != 0))
657 xrect[draw_count - llp - 1].x = event -> xexpose.x;
658 xrect[draw_count - llp - 1].y = event -> xexpose.y;
659 xrect[draw_count - llp - 1].width = event -> xexpose.width;
660 xrect[draw_count - llp - 1].height = event -> xexpose.height;
663 if ((last_count != 0) && (llp == 0))
665 xrect[draw_count - llp - 1].x = event -> xexpose.x;
666 xrect[draw_count - llp - 1].y = event -> xexpose.y;
667 xrect[draw_count - llp - 1].width = event -> xexpose.width;
668 xrect[draw_count - llp - 1].height = event -> xexpose.height;
670 XSetClipRectangles(display,gc,0,0,xrect,draw_count,Unsorted);
671 // panel->DoPaint(xrect,draw_count);
672 panel->GetEventHandler()->OnPaint();
676 last_count = event -> xexpose.count;
680 cout
<< "\n\nNew Event ! is = " << event
-> type
<< "\n";
685 static void wxDialogBoxEventHandler (Widget wid
,
686 XtPointer client_data
,
688 Boolean
*continueToDispatch
)
690 wxDialog
*dialog
= (wxDialog
*)wxWidgetHashTable
->Get((long)wid
);
693 wxMouseEvent
wxevent(wxEVT_NULL
);
694 if (wxTranslateMouseEvent(wxevent
, dialog
, wid
, event
))
696 wxevent
.SetEventObject(dialog
);
697 wxevent
.SetId(dialog
->GetId());
698 dialog
->GetEventHandler()->ProcessEvent(wxevent
);
702 // An attempt to implement OnCharHook by calling OnCharHook first;
703 // if this returns TRUE, set continueToDispatch to False
704 // (don't continue processing).
705 // Otherwise set it to True and call OnChar.
706 wxKeyEvent
keyEvent(wxEVENT_TYPE_CHAR
);
707 if (wxTranslateKeyEvent(keyEvent
, dialog
, wid
, event
))
709 keyEvent
.SetEventObject(dialog
);
710 keyEvent
.SetId(dialog
->GetId());
711 keyEvent
.SetEventType(wxEVT_CHAR_HOOK
);
712 if (dialog
->GetEventHandler()->ProcessEvent(keyEvent
))
714 *continueToDispatch
= False
;
719 keyEvent
.SetEventType(wxEVT_CHAR
);
720 dialog
->GetEventHandler()->ProcessEvent(keyEvent
);
725 *continueToDispatch
= True
;
728 static void wxUnmapBulletinBoard(Widget dialog
, wxDialog
*client
,XtPointer call
)
730 /* This gets called when the dialog is being shown, which
731 * defeats modal showing.
732 client->m_modalShowing = FALSE ;
733 client->m_isShown = FALSE;