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.
285 display
= XtDisplay((Widget
) m_mainWidget
);
287 display
= (Display
*) wxGetDisplay();
289 XSync(display
, FALSE
);
291 while (XtAppPending((XtAppContext
) wxTheApp
->GetAppContext())) {
293 XtAppNextEvent((XtAppContext
) wxTheApp
->GetAppContext(), &event
);
294 XtDispatchEvent(&event
);
298 // By default, pressing escape cancels the dialog
299 void wxDialog::OnCharHook(wxKeyEvent
& event
)
301 if (event
.m_keyCode
== WXK_ESCAPE
)
303 // Behaviour changed in 2.0: we'll send a Cancel message
304 // to the dialog instead of Close.
305 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
306 cancelEvent
.SetEventObject( this );
307 GetEventHandler()->ProcessEvent(cancelEvent
);
311 // We didn't process this event.
315 void wxDialog::Iconize(bool WXUNUSED(iconize
))
317 // Can't iconize a dialog in Motif, apparently
318 // TODO: try using the parent of m_mainShell.
319 // XtVaSetValues((Widget) m_mainWidget, XmNiconic, iconize, NULL);
322 bool wxDialog::IsIconized() const
326 XtVaGetValues((Widget) m_mainWidget, XmNiconic, &iconic, NULL);
333 void wxDialog::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
335 XtVaSetValues((Widget
) m_mainWidget
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
336 wxWindow::SetSize(x
, y
, width
, height
, sizeFlags
);
337 XtVaSetValues((Widget
) m_mainWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
340 void wxDialog::SetClientSize(int width
, int height
)
342 SetSize(-1, -1, width
, height
);
346 void wxDialog::SetTitle(const wxString
& title
)
348 m_dialogTitle
= title
;
351 XmString str
= XmStringCreateSimple((char*) (const char*) title
);
352 XtVaSetValues((Widget
) m_mainWidget
,
353 XmNtitle
, (char*) (const char*) title
,
354 XmNdialogTitle
, str
, // Roberto Cocchi
355 XmNiconName
, (char*) (const char*) title
,
361 wxString
wxDialog::GetTitle() const
363 return m_dialogTitle
;
366 void wxDialog::Centre(int direction
)
368 int x_offset
,y_offset
;
369 int display_width
, display_height
;
370 int width
, height
, x
, y
;
371 wxWindow
*parent
= GetParent();
372 if ((direction
& wxCENTER_FRAME
) && parent
)
374 parent
->GetPosition(&x_offset
,&y_offset
) ;
375 parent
->GetSize(&display_width
,&display_height
) ;
379 wxDisplaySize(&display_width
, &display_height
);
384 GetSize(&width
, &height
);
387 if (direction
& wxHORIZONTAL
)
388 x
= (int)((display_width
- width
)/2);
389 if (direction
& wxVERTICAL
)
390 y
= (int)((display_height
- height
)/2);
392 SetSize(x
+x_offset
, y
+y_offset
, width
, height
);
395 void wxDialog::Raise()
397 Window parent_window
= XtWindow((Widget
) m_mainWidget
),
398 next_parent
= XtWindow((Widget
) m_mainWidget
),
399 root
= RootWindowOfScreen(XtScreen((Widget
) m_mainWidget
));
400 // search for the parent that is child of ROOT, because the WM may
401 // reparent twice and notify only the next parent (like FVWM)
402 while (next_parent
!= root
) {
403 Window
*theChildren
; unsigned int n
;
404 parent_window
= next_parent
;
405 XQueryTree(XtDisplay((Widget
) m_mainWidget
), parent_window
, &root
,
406 &next_parent
, &theChildren
, &n
);
407 XFree(theChildren
); // not needed
409 XRaiseWindow(XtDisplay((Widget
) m_mainWidget
), parent_window
);
412 void wxDialog::Lower()
414 Window parent_window
= XtWindow((Widget
) m_mainWidget
),
415 next_parent
= XtWindow((Widget
) m_mainWidget
),
416 root
= RootWindowOfScreen(XtScreen((Widget
) m_mainWidget
));
417 // search for the parent that is child of ROOT, because the WM may
418 // reparent twice and notify only the next parent (like FVWM)
419 while (next_parent
!= root
) {
420 Window
*theChildren
; unsigned int n
;
421 parent_window
= next_parent
;
422 XQueryTree(XtDisplay((Widget
) m_mainWidget
), parent_window
, &root
,
423 &next_parent
, &theChildren
, &n
);
424 XFree(theChildren
); // not needed
426 XLowerWindow(XtDisplay((Widget
) m_mainWidget
), parent_window
);
429 bool wxDialog::Show(bool show
)
435 if (!wxUSE_INVISIBLE_RESIZE
)
436 XtMapWidget(XtParent((Widget
) m_mainWidget
));
438 XtManageChild((Widget
) m_mainWidget
) ;
440 XRaiseWindow(XtDisplay((Widget
) m_mainWidget
), XtWindow((Widget
) m_mainWidget
));
445 if (!wxUSE_INVISIBLE_RESIZE
)
446 XtUnmapWidget(XtParent((Widget
) m_mainWidget
));
448 XtUnmanageChild((Widget
) m_mainWidget
) ;
450 XFlush(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()));
451 XSync(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()), FALSE
);
457 // Shows a dialog modally, returning a return code
458 int wxDialog::ShowModal()
460 m_windowStyle
|= wxDIALOG_MODAL
;
467 wxModalShowingStack
.Insert((wxObject
*)TRUE
);
469 m_modalShowing
= TRUE
;
470 XtAddGrab((Widget
) m_mainWidget
, TRUE
, FALSE
);
473 // Loop until we signal that the dialog should be closed
474 while ((wxModalShowingStack
.Number() > 0) && (bool)wxModalShowingStack
.First()->Data())
476 XtAppProcessEvent((XtAppContext
) wxTheApp
->GetAppContext(), XtIMAll
);
479 // Remove modal dialog flag from stack
480 wxNode
*node
= wxModalShowingStack
.First();
484 // Now process all events in case they get sent to a destroyed dialog
485 XSync(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()), FALSE
);
486 while (XtAppPending((XtAppContext
) wxTheApp
->GetAppContext()))
488 XFlush(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()));
489 XtAppNextEvent((XtAppContext
) wxTheApp
->GetAppContext(), &event
);
490 XtDispatchEvent(&event
);
493 // TODO: is it safe to call this, if the dialog may have been deleted
494 // by now? Probably only if we're using delayed deletion of dialogs.
495 return GetReturnCode();
498 void wxDialog::EndModal(int retCode
)
503 SetReturnCode(retCode
);
505 XtRemoveGrab((Widget
) m_mainWidget
);
509 m_modalShowing
= FALSE
;
511 wxNode
*node
= wxModalShowingStack
.First();
513 node
->SetData((wxObject
*)FALSE
);
517 void wxDialog::OnOK(wxCommandEvent
& event
)
519 if ( Validate() && TransferDataFromWindow() )
525 SetReturnCode(wxID_OK
);
531 void wxDialog::OnApply(wxCommandEvent
& event
)
534 TransferDataFromWindow();
535 // TODO probably need to disable the Apply button until things change again
538 void wxDialog::OnCancel(wxCommandEvent
& event
)
541 EndModal(wxID_CANCEL
);
544 SetReturnCode(wxID_CANCEL
);
549 bool wxDialog::OnClose()
551 // Behaviour changed in 2.0: we'll send a Cancel message by default,
552 // which may close the dialog.
553 // Check for looping if the Cancel event handler calls Close()
555 static wxList closing
;
557 if ( closing
.Member(this) )
560 closing
.Append(this);
562 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
563 cancelEvent
.SetEventObject( this );
564 GetEventHandler()->ProcessEvent(cancelEvent
);
566 closing
.DeleteObject(this);
571 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
574 if ( GetEventHandler()->OnClose() || event
.GetForce())
580 // Destroy the window (delayed, if a managed window)
581 bool wxDialog::Destroy()
583 if (!wxPendingDelete
.Member(this))
584 wxPendingDelete
.Append(this);
588 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& event
)
590 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
598 // Handle a close event from the window manager
599 static void wxCloseDialogCallback(Widget widget
, XtPointer client_data
, XmAnyCallbackStruct
*cbs
)
601 wxDialog
*dialog
= (wxDialog
*)client_data
;
602 wxCloseEvent
closeEvent(wxEVT_CLOSE_WINDOW
, dialog
->GetId());
603 closeEvent
.SetEventObject(dialog
);
605 // May delete the dialog (with delayed deletion)
606 dialog
->GetEventHandler()->ProcessEvent(closeEvent
);
609 // TODO: Preferably, we should have a universal repaint proc.
610 // Meanwhile, use a special one for dialogs.
611 static void wxDialogBoxRepaintProc(Widget w
, XtPointer c_data
, XEvent
*event
, char *)
614 static XRectangle
*xrect
;
618 static int last_count
= 0;
619 static int draw_count
= 0;
621 wxWindow
* win
= (wxWindow
*)wxWidgetHashTable
->Get((long)w
);
625 switch(event
-> type
)
628 window
= (Window
) win
-> GetXWindow();
629 display
= (Display
*) win
-> GetXDisplay();
631 gc = (GC) panel -> GetDC() -> gc;
633 llp = event -> xexpose.count;
635 if ((last_count == 0) && (llp == 0))
637 xrect = new XRectangle[1];
638 xrect[0].x = event -> xexpose.x;
639 xrect[0].y = event -> xexpose.y;
640 xrect[0].width = event -> xexpose.width;
641 xrect[0].height = event -> xexpose.height;
643 XSetClipRectangles(display,gc,0,0,xrect,1,Unsorted);
644 // panel->DoPaint(xrect, 1);
645 panel->GetEventHandler()->OnPaint();
650 if ((last_count == 0) && (llp != 0))
652 xrect = new XRectangle[llp + 1];
653 draw_count = llp + 1;
655 xrect[draw_count - llp - 1].x = event -> xexpose.x;
656 xrect[draw_count - llp - 1].y = event -> xexpose.y;
657 xrect[draw_count - llp - 1].width = event -> xexpose.width;
658 xrect[draw_count - llp - 1].height = event -> xexpose.height;
661 if ((last_count != 0) && (llp != 0))
663 xrect[draw_count - llp - 1].x = event -> xexpose.x;
664 xrect[draw_count - llp - 1].y = event -> xexpose.y;
665 xrect[draw_count - llp - 1].width = event -> xexpose.width;
666 xrect[draw_count - llp - 1].height = event -> xexpose.height;
669 if ((last_count != 0) && (llp == 0))
671 xrect[draw_count - llp - 1].x = event -> xexpose.x;
672 xrect[draw_count - llp - 1].y = event -> xexpose.y;
673 xrect[draw_count - llp - 1].width = event -> xexpose.width;
674 xrect[draw_count - llp - 1].height = event -> xexpose.height;
676 XSetClipRectangles(display,gc,0,0,xrect,draw_count,Unsorted);
677 // panel->DoPaint(xrect,draw_count);
678 panel->GetEventHandler()->OnPaint();
682 last_count = event -> xexpose.count;
686 cout
<< "\n\nNew Event ! is = " << event
-> type
<< "\n";
691 static void wxDialogBoxEventHandler (Widget wid
,
692 XtPointer client_data
,
694 Boolean
*continueToDispatch
)
696 wxDialog
*dialog
= (wxDialog
*)wxWidgetHashTable
->Get((long)wid
);
699 wxMouseEvent
wxevent(wxEVT_NULL
);
700 if (wxTranslateMouseEvent(wxevent
, dialog
, wid
, event
))
702 wxevent
.SetEventObject(dialog
);
703 wxevent
.SetId(dialog
->GetId());
704 dialog
->GetEventHandler()->ProcessEvent(wxevent
);
708 // An attempt to implement OnCharHook by calling OnCharHook first;
709 // if this returns TRUE, set continueToDispatch to False
710 // (don't continue processing).
711 // Otherwise set it to True and call OnChar.
712 wxKeyEvent
keyEvent(wxEVENT_TYPE_CHAR
);
713 if (wxTranslateKeyEvent(keyEvent
, dialog
, wid
, event
))
715 keyEvent
.SetEventObject(dialog
);
716 keyEvent
.SetId(dialog
->GetId());
717 keyEvent
.SetEventType(wxEVT_CHAR_HOOK
);
718 if (dialog
->GetEventHandler()->ProcessEvent(keyEvent
))
720 *continueToDispatch
= False
;
725 keyEvent
.SetEventType(wxEVT_CHAR
);
726 dialog
->GetEventHandler()->ProcessEvent(keyEvent
);
731 *continueToDispatch
= True
;
734 static void wxUnmapBulletinBoard(Widget dialog
, wxDialog
*client
,XtPointer call
)
736 /* This gets called when the dialog is being shown, which
737 * defeats modal showing.
738 client->m_modalShowing = FALSE ;
739 client->m_isShown = FALSE;