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();
239 SetFont(* parent
->GetFont());
244 void wxDialog::SetModal(bool flag
)
247 m_windowStyle
|= wxDIALOG_MODAL
;
249 if ( m_windowStyle
& wxDIALOG_MODAL
)
250 m_windowStyle
-= wxDIALOG_MODAL
;
252 wxModelessWindows
.DeleteObject(this);
254 wxModelessWindows
.Append(this);
257 wxDialog::~wxDialog()
259 m_modalShowing
= FALSE
;
260 if (!wxUSE_INVISIBLE_RESIZE
&& m_mainWidget
)
262 XtUnmapWidget((Widget
) m_mainWidget
);
265 wxTopLevelWindows
.DeleteObject(this);
267 if ( (GetWindowStyleFlag() & wxDIALOG_MODAL
) != wxDIALOG_MODAL
)
268 wxModelessWindows
.DeleteObject(this);
270 // If this is the last top-level window, exit.
271 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
273 wxTheApp
->SetTopWindow(NULL
);
275 if (wxTheApp
->GetExitOnFrameDelete())
277 wxTheApp
->ExitMainLoop();
281 // This event-flushing code used to be in wxWindow::PostDestroyChildren (wx_dialog.cpp)
282 // but I think this should work, if we destroy the children first.
283 // Note that this might need to be done for wxFrame also.
286 // Now process all events, because otherwise
287 // this might remain on the screen.
290 display
= XtDisplay((Widget
) m_mainWidget
);
292 display
= (Display
*) wxGetDisplay();
294 XSync(display
, FALSE
);
296 while (XtAppPending((XtAppContext
) wxTheApp
->GetAppContext())) {
298 XtAppNextEvent((XtAppContext
) wxTheApp
->GetAppContext(), &event
);
299 XtDispatchEvent(&event
);
303 // By default, pressing escape cancels the dialog
304 void wxDialog::OnCharHook(wxKeyEvent
& event
)
306 if (event
.m_keyCode
== WXK_ESCAPE
)
308 // Behaviour changed in 2.0: we'll send a Cancel message
309 // to the dialog instead of Close.
310 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
311 cancelEvent
.SetEventObject( this );
312 GetEventHandler()->ProcessEvent(cancelEvent
);
316 // We didn't process this event.
320 void wxDialog::Iconize(bool WXUNUSED(iconize
))
322 // Can't iconize a dialog in Motif, apparently
323 // TODO: try using the parent of m_mainShell.
324 // XtVaSetValues((Widget) m_mainWidget, XmNiconic, iconize, NULL);
327 bool wxDialog::IsIconized() const
331 XtVaGetValues((Widget) m_mainWidget, XmNiconic, &iconic, NULL);
338 void wxDialog::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
340 XtVaSetValues((Widget
) m_mainWidget
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
341 wxWindow::SetSize(x
, y
, width
, height
, sizeFlags
);
342 XtVaSetValues((Widget
) m_mainWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
345 void wxDialog::SetClientSize(int width
, int height
)
347 SetSize(-1, -1, width
, height
);
351 void wxDialog::SetTitle(const wxString
& title
)
353 m_dialogTitle
= title
;
356 XmString str
= XmStringCreateSimple((char*) (const char*) title
);
357 XtVaSetValues((Widget
) m_mainWidget
,
358 XmNtitle
, (char*) (const char*) title
,
359 XmNdialogTitle
, str
, // Roberto Cocchi
360 XmNiconName
, (char*) (const char*) title
,
366 wxString
wxDialog::GetTitle() const
368 return m_dialogTitle
;
371 void wxDialog::Centre(int direction
)
373 int x_offset
,y_offset
;
374 int display_width
, display_height
;
375 int width
, height
, x
, y
;
376 wxWindow
*parent
= GetParent();
377 if ((direction
& wxCENTER_FRAME
) && parent
)
379 parent
->GetPosition(&x_offset
,&y_offset
) ;
380 parent
->GetSize(&display_width
,&display_height
) ;
384 wxDisplaySize(&display_width
, &display_height
);
389 GetSize(&width
, &height
);
392 if (direction
& wxHORIZONTAL
)
393 x
= (int)((display_width
- width
)/2);
394 if (direction
& wxVERTICAL
)
395 y
= (int)((display_height
- height
)/2);
397 SetSize(x
+x_offset
, y
+y_offset
, width
, height
);
400 void wxDialog::Raise()
402 Window parent_window
= XtWindow((Widget
) m_mainWidget
),
403 next_parent
= XtWindow((Widget
) m_mainWidget
),
404 root
= RootWindowOfScreen(XtScreen((Widget
) m_mainWidget
));
405 // search for the parent that is child of ROOT, because the WM may
406 // reparent twice and notify only the next parent (like FVWM)
407 while (next_parent
!= root
) {
408 Window
*theChildren
; unsigned int n
;
409 parent_window
= next_parent
;
410 XQueryTree(XtDisplay((Widget
) m_mainWidget
), parent_window
, &root
,
411 &next_parent
, &theChildren
, &n
);
412 XFree(theChildren
); // not needed
414 XRaiseWindow(XtDisplay((Widget
) m_mainWidget
), parent_window
);
417 void wxDialog::Lower()
419 Window parent_window
= XtWindow((Widget
) m_mainWidget
),
420 next_parent
= XtWindow((Widget
) m_mainWidget
),
421 root
= RootWindowOfScreen(XtScreen((Widget
) m_mainWidget
));
422 // search for the parent that is child of ROOT, because the WM may
423 // reparent twice and notify only the next parent (like FVWM)
424 while (next_parent
!= root
) {
425 Window
*theChildren
; unsigned int n
;
426 parent_window
= next_parent
;
427 XQueryTree(XtDisplay((Widget
) m_mainWidget
), parent_window
, &root
,
428 &next_parent
, &theChildren
, &n
);
429 XFree(theChildren
); // not needed
431 XLowerWindow(XtDisplay((Widget
) m_mainWidget
), parent_window
);
434 bool wxDialog::Show(bool show
)
440 if (!wxUSE_INVISIBLE_RESIZE
)
441 XtMapWidget(XtParent((Widget
) m_mainWidget
));
443 XtManageChild((Widget
) m_mainWidget
) ;
445 XRaiseWindow(XtDisplay((Widget
) m_mainWidget
), XtWindow((Widget
) m_mainWidget
));
450 if (!wxUSE_INVISIBLE_RESIZE
)
451 XtUnmapWidget(XtParent((Widget
) m_mainWidget
));
453 XtUnmanageChild((Widget
) m_mainWidget
) ;
455 XFlush(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()));
456 XSync(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()), FALSE
);
462 // Shows a dialog modally, returning a return code
463 int wxDialog::ShowModal()
465 m_windowStyle
|= wxDIALOG_MODAL
;
472 wxModalShowingStack
.Insert((wxObject
*)TRUE
);
474 m_modalShowing
= TRUE
;
475 XtAddGrab((Widget
) m_mainWidget
, TRUE
, FALSE
);
478 // Loop until we signal that the dialog should be closed
479 while ((wxModalShowingStack
.Number() > 0) && (bool)wxModalShowingStack
.First()->Data())
481 XtAppProcessEvent((XtAppContext
) wxTheApp
->GetAppContext(), XtIMAll
);
484 // Remove modal dialog flag from stack
485 wxNode
*node
= wxModalShowingStack
.First();
489 // Now process all events in case they get sent to a destroyed dialog
490 XSync(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()), FALSE
);
491 while (XtAppPending((XtAppContext
) wxTheApp
->GetAppContext()))
493 XFlush(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()));
494 XtAppNextEvent((XtAppContext
) wxTheApp
->GetAppContext(), &event
);
495 XtDispatchEvent(&event
);
498 // TODO: is it safe to call this, if the dialog may have been deleted
499 // by now? Probably only if we're using delayed deletion of dialogs.
500 return GetReturnCode();
503 void wxDialog::EndModal(int retCode
)
508 SetReturnCode(retCode
);
510 XtRemoveGrab((Widget
) m_mainWidget
);
514 m_modalShowing
= FALSE
;
516 wxNode
*node
= wxModalShowingStack
.First();
518 node
->SetData((wxObject
*)FALSE
);
522 void wxDialog::OnOK(wxCommandEvent
& event
)
524 if ( Validate() && TransferDataFromWindow() )
530 SetReturnCode(wxID_OK
);
536 void wxDialog::OnApply(wxCommandEvent
& event
)
539 TransferDataFromWindow();
540 // TODO probably need to disable the Apply button until things change again
543 void wxDialog::OnCancel(wxCommandEvent
& event
)
546 EndModal(wxID_CANCEL
);
549 SetReturnCode(wxID_CANCEL
);
554 bool wxDialog::OnClose()
556 // Behaviour changed in 2.0: we'll send a Cancel message by default,
557 // which may close the dialog.
558 // Check for looping if the Cancel event handler calls Close()
560 static wxList closing
;
562 if ( closing
.Member(this) )
565 closing
.Append(this);
567 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
568 cancelEvent
.SetEventObject( this );
569 GetEventHandler()->ProcessEvent(cancelEvent
);
571 closing
.DeleteObject(this);
576 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
579 if ( GetEventHandler()->OnClose() || event
.GetForce())
585 // Destroy the window (delayed, if a managed window)
586 bool wxDialog::Destroy()
588 if (!wxPendingDelete
.Member(this))
589 wxPendingDelete
.Append(this);
593 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& event
)
595 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
603 // Handle a close event from the window manager
604 static void wxCloseDialogCallback(Widget widget
, XtPointer client_data
, XmAnyCallbackStruct
*cbs
)
606 wxDialog
*dialog
= (wxDialog
*)client_data
;
607 wxCloseEvent
closeEvent(wxEVT_CLOSE_WINDOW
, dialog
->GetId());
608 closeEvent
.SetEventObject(dialog
);
610 // May delete the dialog (with delayed deletion)
611 dialog
->GetEventHandler()->ProcessEvent(closeEvent
);
614 // TODO: Preferably, we should have a universal repaint proc.
615 // Meanwhile, use a special one for dialogs.
616 static void wxDialogBoxRepaintProc(Widget w
, XtPointer c_data
, XEvent
*event
, char *)
619 static XRectangle
*xrect
;
623 static int last_count
= 0;
624 static int draw_count
= 0;
626 wxWindow
* win
= (wxWindow
*)wxWidgetHashTable
->Get((long)w
);
630 switch(event
-> type
)
633 window
= (Window
) win
-> GetXWindow();
634 display
= (Display
*) win
-> GetXDisplay();
636 gc = (GC) panel -> GetDC() -> gc;
638 llp = event -> xexpose.count;
640 if ((last_count == 0) && (llp == 0))
642 xrect = new XRectangle[1];
643 xrect[0].x = event -> xexpose.x;
644 xrect[0].y = event -> xexpose.y;
645 xrect[0].width = event -> xexpose.width;
646 xrect[0].height = event -> xexpose.height;
648 XSetClipRectangles(display,gc,0,0,xrect,1,Unsorted);
649 // panel->DoPaint(xrect, 1);
650 panel->GetEventHandler()->OnPaint();
655 if ((last_count == 0) && (llp != 0))
657 xrect = new XRectangle[llp + 1];
658 draw_count = llp + 1;
660 xrect[draw_count - llp - 1].x = event -> xexpose.x;
661 xrect[draw_count - llp - 1].y = event -> xexpose.y;
662 xrect[draw_count - llp - 1].width = event -> xexpose.width;
663 xrect[draw_count - llp - 1].height = event -> xexpose.height;
666 if ((last_count != 0) && (llp != 0))
668 xrect[draw_count - llp - 1].x = event -> xexpose.x;
669 xrect[draw_count - llp - 1].y = event -> xexpose.y;
670 xrect[draw_count - llp - 1].width = event -> xexpose.width;
671 xrect[draw_count - llp - 1].height = event -> xexpose.height;
674 if ((last_count != 0) && (llp == 0))
676 xrect[draw_count - llp - 1].x = event -> xexpose.x;
677 xrect[draw_count - llp - 1].y = event -> xexpose.y;
678 xrect[draw_count - llp - 1].width = event -> xexpose.width;
679 xrect[draw_count - llp - 1].height = event -> xexpose.height;
681 XSetClipRectangles(display,gc,0,0,xrect,draw_count,Unsorted);
682 // panel->DoPaint(xrect,draw_count);
683 panel->GetEventHandler()->OnPaint();
687 last_count = event -> xexpose.count;
691 cout
<< "\n\nNew Event ! is = " << event
-> type
<< "\n";
696 static void wxDialogBoxEventHandler (Widget wid
,
697 XtPointer client_data
,
699 Boolean
*continueToDispatch
)
701 wxDialog
*dialog
= (wxDialog
*)wxWidgetHashTable
->Get((long)wid
);
704 wxMouseEvent
wxevent(wxEVT_NULL
);
705 if (wxTranslateMouseEvent(wxevent
, dialog
, wid
, event
))
707 wxevent
.SetEventObject(dialog
);
708 wxevent
.SetId(dialog
->GetId());
709 dialog
->GetEventHandler()->ProcessEvent(wxevent
);
713 // An attempt to implement OnCharHook by calling OnCharHook first;
714 // if this returns TRUE, set continueToDispatch to False
715 // (don't continue processing).
716 // Otherwise set it to True and call OnChar.
717 wxKeyEvent
keyEvent(wxEVENT_TYPE_CHAR
);
718 if (wxTranslateKeyEvent(keyEvent
, dialog
, wid
, event
))
720 keyEvent
.SetEventObject(dialog
);
721 keyEvent
.SetId(dialog
->GetId());
722 keyEvent
.SetEventType(wxEVT_CHAR_HOOK
);
723 if (dialog
->GetEventHandler()->ProcessEvent(keyEvent
))
725 *continueToDispatch
= False
;
730 keyEvent
.SetEventType(wxEVT_CHAR
);
731 dialog
->GetEventHandler()->ProcessEvent(keyEvent
);
736 *continueToDispatch
= True
;
739 static void wxUnmapBulletinBoard(Widget dialog
, wxDialog
*client
,XtPointer call
)
741 /* This gets called when the dialog is being shown, which
742 * defeats modal showing.
743 client->m_modalShowing = FALSE ;
744 client->m_isShown = FALSE;
748 void wxDialog::ChangeFont()
753 void wxDialog::ChangeBackgroundColour()
758 void wxDialog::ChangeForegroundColour()