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 wxAddWindowToTable(dialogShell
, this);
144 // Intercept CLOSE messages from the window manager
145 Atom WM_DELETE_WINDOW
= XmInternAtom(XtDisplay(shell
), "WM_DELETE_WINDOW", False
);
147 /* MATTHEW: [8] Remove and add WM_DELETE_WINDOW so ours is only handler */
148 /* Why do we have to do this for wxDialog, but not wxFrame? */
149 XmRemoveWMProtocols(shell
, &WM_DELETE_WINDOW
, 1);
150 XmAddWMProtocols(shell
, &WM_DELETE_WINDOW
, 1);
151 XmActivateWMProtocol(shell
, WM_DELETE_WINDOW
);
153 // Modified Steve Hammes for Motif 2.0
154 #if (XmREVISION > 1 || XmVERSION > 1)
155 XmAddWMProtocolCallback(shell
, WM_DELETE_WINDOW
, (XtCallbackProc
) wxCloseDialogCallback
, (XtPointer
)this);
156 #elif XmREVISION == 1
157 XmAddWMProtocolCallback(shell
, WM_DELETE_WINDOW
, (XtCallbackProc
) wxCloseDialogCallback
, (caddr_t
)this);
159 XmAddWMProtocolCallback(shell
, WM_DELETE_WINDOW
, (void (*)())wxCloseDialogCallback
, (caddr_t
)this);
163 XtOverrideTranslations(dialogShell
,
164 ptr
= XtParseTranslationTable("<Configure>: resize()"));
167 // Can't remember what this was about... but I think it's necessary.
169 if (wxUSE_INVISIBLE_RESIZE
)
172 XtVaSetValues(dialogShell
, XmNx
, pos
.x
,
175 XtVaSetValues(dialogShell
, XmNy
, pos
.y
,
179 XtVaSetValues(dialogShell
, XmNwidth
, size
.x
, NULL
);
181 XtVaSetValues(dialogShell
, XmNheight
, size
.y
, NULL
);
184 // This patch come from Torsten Liermann lier@lier1.muc.de
185 if (XmIsMotifWMRunning(shell
))
188 if (m_windowStyle
& wxRESIZE_BORDER
)
189 decor
|= MWM_DECOR_RESIZEH
;
190 if (m_windowStyle
& wxSYSTEM_MENU
)
191 decor
|= MWM_DECOR_MENU
;
192 if ((m_windowStyle
& wxCAPTION
) ||
193 (m_windowStyle
& wxTINY_CAPTION_HORIZ
) ||
194 (m_windowStyle
& wxTINY_CAPTION_VERT
))
195 decor
|= MWM_DECOR_TITLE
;
196 if (m_windowStyle
& wxTHICK_FRAME
)
197 decor
|= MWM_DECOR_BORDER
;
198 if (m_windowStyle
& wxMINIMIZE_BOX
)
199 decor
|= MWM_DECOR_MINIMIZE
;
200 if (m_windowStyle
& wxMAXIMIZE_BOX
)
201 decor
|= MWM_DECOR_MAXIMIZE
;
203 XtVaSetValues(shell
,XmNmwmDecorations
,decor
,NULL
) ;
205 // This allows non-Motif window managers to support at least the
206 // no-decorations case.
209 if ((m_windowStyle
& wxCAPTION
) != wxCAPTION
)
210 XtVaSetValues((Widget
) shell
,XmNoverrideRedirect
,TRUE
,NULL
);
213 XtRealizeWidget(dialogShell
);
215 XtAddCallback(dialogShell
,XmNunmapCallback
,
216 (XtCallbackProc
)wxUnmapBulletinBoard
,this) ;
218 // Positioning of the dialog doesn't work properly unless the dialog
219 // is managed, so we manage without mapping to the screen.
220 // To show, we map the shell (actually it's parent).
221 if (!wxUSE_INVISIBLE_RESIZE
)
222 XtVaSetValues(shell
, XmNmappedWhenManaged
, FALSE
, NULL
);
224 if (!wxUSE_INVISIBLE_RESIZE
)
226 XtManageChild(dialogShell
);
227 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
229 XtAddEventHandler(dialogShell
,ExposureMask
,FALSE
,
230 wxDialogBoxRepaintProc
, (XtPointer
) this);
232 XtAddEventHandler(dialogShell
,
233 ButtonPressMask
| ButtonReleaseMask
| PointerMotionMask
| KeyPressMask
,
235 wxDialogBoxEventHandler
,
238 ChangeBackgroundColour();
243 void wxDialog
::SetModal(bool flag
)
246 m_windowStyle
|= wxDIALOG_MODAL
;
248 if ( m_windowStyle
& wxDIALOG_MODAL
)
249 m_windowStyle
-= wxDIALOG_MODAL
;
251 wxModelessWindows
.DeleteObject(this);
253 wxModelessWindows
.Append(this);
256 wxDialog
::~wxDialog()
258 m_modalShowing
= FALSE
;
259 if (!wxUSE_INVISIBLE_RESIZE
&& m_mainWidget
)
261 XtUnmapWidget((Widget
) m_mainWidget
);
264 wxTopLevelWindows
.DeleteObject(this);
266 if ( (GetWindowStyleFlag() & wxDIALOG_MODAL
) != wxDIALOG_MODAL
)
267 wxModelessWindows
.DeleteObject(this);
269 // If this is the last top-level window, exit.
270 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
272 wxTheApp
->SetTopWindow(NULL
);
274 if (wxTheApp
->GetExitOnFrameDelete())
276 wxTheApp
->ExitMainLoop();
280 // This event-flushing code used to be in wxWindow::PostDestroyChildren (wx_dialog.cpp)
281 // but I think this should work, if we destroy the children first.
282 // Note that this might need to be done for wxFrame also.
285 // Now process all events, because otherwise
286 // this might remain on the screen.
289 display
= XtDisplay((Widget
) m_mainWidget
);
291 display
= (Display
*) wxGetDisplay();
293 XSync(display
, FALSE
);
295 while (XtAppPending((XtAppContext
) wxTheApp
->GetAppContext())) {
297 XtAppNextEvent((XtAppContext
) wxTheApp
->GetAppContext(), &event
);
298 XtDispatchEvent(&event
);
302 // By default, pressing escape cancels the dialog
303 void wxDialog
::OnCharHook(wxKeyEvent
& event
)
305 if (event
.m_keyCode
== WXK_ESCAPE
)
307 // Behaviour changed in 2.0: we'll send a Cancel message
308 // to the dialog instead of Close.
309 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
310 cancelEvent
.SetEventObject( this );
311 GetEventHandler()->ProcessEvent(cancelEvent
);
315 // We didn't process this event.
319 void wxDialog
::Iconize(bool WXUNUSED(iconize
))
321 // Can't iconize a dialog in Motif, apparently
322 // TODO: try using the parent of m_mainShell.
323 // XtVaSetValues((Widget) m_mainWidget, XmNiconic, iconize, NULL);
326 bool wxDialog
::IsIconized() const
330 XtVaGetValues((Widget) m_mainWidget, XmNiconic, &iconic, NULL);
337 void wxDialog
::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
339 XtVaSetValues((Widget
) m_mainWidget
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
340 wxWindow
::SetSize(x
, y
, width
, height
, sizeFlags
);
341 XtVaSetValues((Widget
) m_mainWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
344 void wxDialog
::SetClientSize(int width
, int height
)
346 SetSize(-1, -1, width
, height
);
350 void wxDialog
::SetTitle(const wxString
& title
)
352 m_dialogTitle
= title
;
355 XmString str
= XmStringCreateSimple((char*) (const char*) title
);
356 XtVaSetValues((Widget
) m_mainWidget
,
357 XmNtitle
, (char*) (const char*) title
,
358 XmNdialogTitle
, str
, // Roberto Cocchi
359 XmNiconName
, (char*) (const char*) title
,
365 wxString wxDialog
::GetTitle() const
367 return m_dialogTitle
;
370 void wxDialog
::Centre(int direction
)
372 int x_offset
,y_offset
;
373 int display_width
, display_height
;
374 int width
, height
, x
, y
;
375 wxWindow
*parent
= GetParent();
376 if ((direction
& wxCENTER_FRAME
) && parent
)
378 parent
->GetPosition(&x_offset
,&y_offset
) ;
379 parent
->GetSize(&display_width
,&display_height
) ;
383 wxDisplaySize(&display_width
, &display_height
);
388 GetSize(&width
, &height
);
391 if (direction
& wxHORIZONTAL
)
392 x
= (int)((display_width
- width
)/2);
393 if (direction
& wxVERTICAL
)
394 y
= (int)((display_height
- height
)/2);
396 SetSize(x
+x_offset
, y
+y_offset
, width
, height
);
399 void wxDialog
::Raise()
401 Window parent_window
= XtWindow((Widget
) m_mainWidget
),
402 next_parent
= XtWindow((Widget
) m_mainWidget
),
403 root
= RootWindowOfScreen(XtScreen((Widget
) m_mainWidget
));
404 // search for the parent that is child of ROOT, because the WM may
405 // reparent twice and notify only the next parent (like FVWM)
406 while (next_parent
!= root
) {
407 Window
*theChildren
; unsigned int n
;
408 parent_window
= next_parent
;
409 XQueryTree(XtDisplay((Widget
) m_mainWidget
), parent_window
, &root
,
410 &next_parent
, &theChildren
, &n
);
411 XFree(theChildren
); // not needed
413 XRaiseWindow(XtDisplay((Widget
) m_mainWidget
), parent_window
);
416 void wxDialog
::Lower()
418 Window parent_window
= XtWindow((Widget
) m_mainWidget
),
419 next_parent
= XtWindow((Widget
) m_mainWidget
),
420 root
= RootWindowOfScreen(XtScreen((Widget
) m_mainWidget
));
421 // search for the parent that is child of ROOT, because the WM may
422 // reparent twice and notify only the next parent (like FVWM)
423 while (next_parent
!= root
) {
424 Window
*theChildren
; unsigned int n
;
425 parent_window
= next_parent
;
426 XQueryTree(XtDisplay((Widget
) m_mainWidget
), parent_window
, &root
,
427 &next_parent
, &theChildren
, &n
);
428 XFree(theChildren
); // not needed
430 XLowerWindow(XtDisplay((Widget
) m_mainWidget
), parent_window
);
433 bool wxDialog
::Show(bool show
)
439 if (!wxUSE_INVISIBLE_RESIZE
)
440 XtMapWidget(XtParent((Widget
) m_mainWidget
));
442 XtManageChild((Widget
) m_mainWidget
) ;
444 XRaiseWindow(XtDisplay((Widget
) m_mainWidget
), XtWindow((Widget
) m_mainWidget
));
449 if (!wxUSE_INVISIBLE_RESIZE
)
450 XtUnmapWidget(XtParent((Widget
) m_mainWidget
));
452 XtUnmanageChild((Widget
) m_mainWidget
) ;
454 XFlush(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()));
455 XSync(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()), FALSE
);
461 // Shows a dialog modally, returning a return code
462 int wxDialog
::ShowModal()
464 m_windowStyle
|= wxDIALOG_MODAL
;
471 wxModalShowingStack
.Insert((wxObject
*)TRUE
);
473 m_modalShowing
= TRUE
;
474 XtAddGrab((Widget
) m_mainWidget
, TRUE
, FALSE
);
477 // Loop until we signal that the dialog should be closed
478 while ((wxModalShowingStack
.Number() > 0) && (bool)wxModalShowingStack
.First()->Data())
480 XtAppProcessEvent((XtAppContext
) wxTheApp
->GetAppContext(), XtIMAll
);
483 // Remove modal dialog flag from stack
484 wxNode
*node
= wxModalShowingStack
.First();
488 // Now process all events in case they get sent to a destroyed dialog
489 XSync(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()), FALSE
);
490 while (XtAppPending((XtAppContext
) wxTheApp
->GetAppContext()))
492 XFlush(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()));
493 XtAppNextEvent((XtAppContext
) wxTheApp
->GetAppContext(), &event
);
494 XtDispatchEvent(&event
);
497 // TODO: is it safe to call this, if the dialog may have been deleted
498 // by now? Probably only if we're using delayed deletion of dialogs.
499 return GetReturnCode();
502 void wxDialog
::EndModal(int retCode
)
507 SetReturnCode(retCode
);
509 XtRemoveGrab((Widget
) m_mainWidget
);
513 m_modalShowing
= FALSE
;
515 wxNode
*node
= wxModalShowingStack
.First();
517 node
->SetData((wxObject
*)FALSE
);
521 void wxDialog
::OnOK(wxCommandEvent
& event
)
523 if ( Validate() && TransferDataFromWindow() )
529 SetReturnCode(wxID_OK
);
535 void wxDialog
::OnApply(wxCommandEvent
& event
)
538 TransferDataFromWindow();
539 // TODO probably need to disable the Apply button until things change again
542 void wxDialog
::OnCancel(wxCommandEvent
& event
)
545 EndModal(wxID_CANCEL
);
548 SetReturnCode(wxID_CANCEL
);
553 bool wxDialog
::OnClose()
555 // Behaviour changed in 2.0: we'll send a Cancel message by default,
556 // which may close the dialog.
557 // Check for looping if the Cancel event handler calls Close()
559 static wxList closing
;
561 if ( closing
.Member(this) )
564 closing
.Append(this);
566 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
567 cancelEvent
.SetEventObject( this );
568 GetEventHandler()->ProcessEvent(cancelEvent
);
570 closing
.DeleteObject(this);
575 void wxDialog
::OnCloseWindow(wxCloseEvent
& event
)
578 if ( GetEventHandler()->OnClose() || event
.GetForce())
584 // Destroy the window (delayed, if a managed window)
585 bool wxDialog
::Destroy()
587 if (!wxPendingDelete
.Member(this))
588 wxPendingDelete
.Append(this);
592 void wxDialog
::OnSysColourChanged(wxSysColourChangedEvent
& event
)
594 SetBackgroundColour(wxSystemSettings
::GetSystemColour(wxSYS_COLOUR_3DFACE
));
602 // Handle a close event from the window manager
603 static void wxCloseDialogCallback(Widget widget
, XtPointer client_data
, XmAnyCallbackStruct
*cbs
)
605 wxDialog
*dialog
= (wxDialog
*)client_data
;
606 wxCloseEvent
closeEvent(wxEVT_CLOSE_WINDOW
, dialog
->GetId());
607 closeEvent
.SetEventObject(dialog
);
609 // May delete the dialog (with delayed deletion)
610 dialog
->GetEventHandler()->ProcessEvent(closeEvent
);
613 // TODO: Preferably, we should have a universal repaint proc.
614 // Meanwhile, use a special one for dialogs.
615 static void wxDialogBoxRepaintProc(Widget w
, XtPointer c_data
, XEvent
*event
, char *)
618 static XRectangle
*xrect
;
622 static int last_count
= 0;
623 static int draw_count
= 0;
625 wxWindow
* win
= (wxWindow
*)wxWidgetHashTable
->Get((long)w
);
629 switch(event
-> type
)
632 window
= (Window
) win
-> GetXWindow();
633 display
= (Display
*) win
-> GetXDisplay();
635 gc = (GC) panel -> GetDC() -> gc;
637 llp = event -> xexpose.count;
639 if ((last_count == 0) && (llp == 0))
641 xrect = new XRectangle[1];
642 xrect[0].x = event -> xexpose.x;
643 xrect[0].y = event -> xexpose.y;
644 xrect[0].width = event -> xexpose.width;
645 xrect[0].height = event -> xexpose.height;
647 XSetClipRectangles(display,gc,0,0,xrect,1,Unsorted);
648 // panel->DoPaint(xrect, 1);
649 panel->GetEventHandler()->OnPaint();
654 if ((last_count == 0) && (llp != 0))
656 xrect = new XRectangle[llp + 1];
657 draw_count = llp + 1;
659 xrect[draw_count - llp - 1].x = event -> xexpose.x;
660 xrect[draw_count - llp - 1].y = event -> xexpose.y;
661 xrect[draw_count - llp - 1].width = event -> xexpose.width;
662 xrect[draw_count - llp - 1].height = event -> xexpose.height;
665 if ((last_count != 0) && (llp != 0))
667 xrect[draw_count - llp - 1].x = event -> xexpose.x;
668 xrect[draw_count - llp - 1].y = event -> xexpose.y;
669 xrect[draw_count - llp - 1].width = event -> xexpose.width;
670 xrect[draw_count - llp - 1].height = event -> xexpose.height;
673 if ((last_count != 0) && (llp == 0))
675 xrect[draw_count - llp - 1].x = event -> xexpose.x;
676 xrect[draw_count - llp - 1].y = event -> xexpose.y;
677 xrect[draw_count - llp - 1].width = event -> xexpose.width;
678 xrect[draw_count - llp - 1].height = event -> xexpose.height;
680 XSetClipRectangles(display,gc,0,0,xrect,draw_count,Unsorted);
681 // panel->DoPaint(xrect,draw_count);
682 panel->GetEventHandler()->OnPaint();
686 last_count = event -> xexpose.count;
690 cout
<< "\n\nNew Event ! is = " << event
-> type
<< "\n";
695 static void wxDialogBoxEventHandler (Widget wid
,
696 XtPointer client_data
,
698 Boolean
*continueToDispatch
)
700 wxDialog
*dialog
= (wxDialog
*)wxWidgetHashTable
->Get((long)wid
);
703 wxMouseEvent
wxevent(wxEVT_NULL
);
704 if (wxTranslateMouseEvent(wxevent
, dialog
, wid
, event
))
706 wxevent
.SetEventObject(dialog
);
707 wxevent
.SetId(dialog
->GetId());
708 dialog
->GetEventHandler()->ProcessEvent(wxevent
);
712 // An attempt to implement OnCharHook by calling OnCharHook first;
713 // if this returns TRUE, set continueToDispatch to False
714 // (don't continue processing).
715 // Otherwise set it to True and call OnChar.
716 wxKeyEvent
keyEvent(wxEVENT_TYPE_CHAR
);
717 if (wxTranslateKeyEvent(keyEvent
, dialog
, wid
, event
))
719 keyEvent
.SetEventObject(dialog
);
720 keyEvent
.SetId(dialog
->GetId());
721 keyEvent
.SetEventType(wxEVT_CHAR_HOOK
);
722 if (dialog
->GetEventHandler()->ProcessEvent(keyEvent
))
724 *continueToDispatch
= False
;
729 keyEvent
.SetEventType(wxEVT_CHAR
);
730 dialog
->GetEventHandler()->ProcessEvent(keyEvent
);
735 *continueToDispatch
= True
;
738 static void wxUnmapBulletinBoard(Widget dialog
, wxDialog
*client
,XtPointer call
)
740 /* This gets called when the dialog is being shown, which
741 * defeats modal showing.
742 client->m_modalShowing = FALSE ;
743 client->m_isShown = FALSE;
747 void wxDialog
::ChangeFont()
752 void wxDialog
::ChangeBackgroundColour()
757 void wxDialog
::ChangeForegroundColour()