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 XtSetArg (args
[1], XmNautoUnmanage
, False
);
123 Widget dialogShell
= XmCreateBulletinBoardDialog(parentWidget
, (char*) (const char*) name
, args
, 2);
124 m_mainWidget
= (WXWidget
) dialogShell
;
126 // We don't want margins, since there is enough elsewhere.
127 XtVaSetValues(dialogShell
,
130 XmNresizePolicy
, XmRESIZE_NONE
,
133 Widget shell
= XtParent(dialogShell
) ;
136 XmString str
= XmStringCreateSimple((char*) (const char*)title
);
137 XtVaSetValues(dialogShell
,
143 m_windowFont
= wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
);
146 wxAddWindowToTable(dialogShell
, this);
148 // Intercept CLOSE messages from the window manager
149 Atom WM_DELETE_WINDOW
= XmInternAtom(XtDisplay(shell
), "WM_DELETE_WINDOW", False
);
151 /* Remove and add WM_DELETE_WINDOW so ours is only handler */
152 /* Why do we have to do this for wxDialog, but not wxFrame? */
153 XmRemoveWMProtocols(shell
, &WM_DELETE_WINDOW
, 1);
154 XmAddWMProtocols(shell
, &WM_DELETE_WINDOW
, 1);
155 XmActivateWMProtocol(shell
, WM_DELETE_WINDOW
);
157 // Modified Steve Hammes for Motif 2.0
158 #if (XmREVISION > 1 || XmVERSION > 1)
159 XmAddWMProtocolCallback(shell
, WM_DELETE_WINDOW
, (XtCallbackProc
) wxCloseDialogCallback
, (XtPointer
)this);
160 #elif XmREVISION == 1
161 XmAddWMProtocolCallback(shell
, WM_DELETE_WINDOW
, (XtCallbackProc
) wxCloseDialogCallback
, (caddr_t
)this);
163 XmAddWMProtocolCallback(shell
, WM_DELETE_WINDOW
, (void (*)())wxCloseDialogCallback
, (caddr_t
)this);
167 XtOverrideTranslations(dialogShell
,
168 ptr
= XtParseTranslationTable("<Configure>: resize()"));
171 // Can't remember what this was about... but I think it's necessary.
173 if (wxUSE_INVISIBLE_RESIZE
)
176 XtVaSetValues(dialogShell
, XmNx
, pos
.x
,
179 XtVaSetValues(dialogShell
, XmNy
, pos
.y
,
183 XtVaSetValues(dialogShell
, XmNwidth
, size
.x
, NULL
);
185 XtVaSetValues(dialogShell
, XmNheight
, size
.y
, NULL
);
188 // This patch come from Torsten Liermann lier@lier1.muc.de
189 if (XmIsMotifWMRunning(shell
))
192 if (m_windowStyle
& wxRESIZE_BORDER
)
193 decor
|= MWM_DECOR_RESIZEH
;
194 if (m_windowStyle
& wxSYSTEM_MENU
)
195 decor
|= MWM_DECOR_MENU
;
196 if ((m_windowStyle
& wxCAPTION
) ||
197 (m_windowStyle
& wxTINY_CAPTION_HORIZ
) ||
198 (m_windowStyle
& wxTINY_CAPTION_VERT
))
199 decor
|= MWM_DECOR_TITLE
;
200 if (m_windowStyle
& wxTHICK_FRAME
)
201 decor
|= MWM_DECOR_BORDER
;
202 if (m_windowStyle
& wxMINIMIZE_BOX
)
203 decor
|= MWM_DECOR_MINIMIZE
;
204 if (m_windowStyle
& wxMAXIMIZE_BOX
)
205 decor
|= MWM_DECOR_MAXIMIZE
;
207 XtVaSetValues(shell
,XmNmwmDecorations
,decor
,NULL
) ;
209 // This allows non-Motif window managers to support at least the
210 // no-decorations case.
213 if ((m_windowStyle
& wxCAPTION
) != wxCAPTION
)
214 XtVaSetValues((Widget
) shell
,XmNoverrideRedirect
,TRUE
,NULL
);
217 XtRealizeWidget(dialogShell
);
219 XtAddCallback(dialogShell
,XmNunmapCallback
,
220 (XtCallbackProc
)wxUnmapBulletinBoard
,this) ;
222 // Positioning of the dialog doesn't work properly unless the dialog
223 // is managed, so we manage without mapping to the screen.
224 // To show, we map the shell (actually it's parent).
225 if (!wxUSE_INVISIBLE_RESIZE
)
226 XtVaSetValues(shell
, XmNmappedWhenManaged
, FALSE
, NULL
);
228 if (!wxUSE_INVISIBLE_RESIZE
)
230 XtManageChild(dialogShell
);
231 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
233 XtAddEventHandler(dialogShell
,ExposureMask
,FALSE
,
234 wxDialogBoxRepaintProc
, (XtPointer
) this);
236 XtAddEventHandler(dialogShell
,
237 ButtonPressMask
| ButtonReleaseMask
| PointerMotionMask
| KeyPressMask
,
239 wxDialogBoxEventHandler
,
242 ChangeBackgroundColour();
247 void wxDialog::SetModal(bool flag
)
250 m_windowStyle
|= wxDIALOG_MODAL
;
252 if ( m_windowStyle
& wxDIALOG_MODAL
)
253 m_windowStyle
-= wxDIALOG_MODAL
;
255 wxModelessWindows
.DeleteObject(this);
257 wxModelessWindows
.Append(this);
260 wxDialog::~wxDialog()
262 m_modalShowing
= FALSE
;
263 if (!wxUSE_INVISIBLE_RESIZE
&& m_mainWidget
)
265 XtUnmapWidget((Widget
) m_mainWidget
);
268 wxTopLevelWindows
.DeleteObject(this);
270 if ( (GetWindowStyleFlag() & wxDIALOG_MODAL
) != wxDIALOG_MODAL
)
271 wxModelessWindows
.DeleteObject(this);
273 // If this is the last top-level window, exit.
274 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
276 wxTheApp
->SetTopWindow(NULL
);
278 if (wxTheApp
->GetExitOnFrameDelete())
280 wxTheApp
->ExitMainLoop();
284 // This event-flushing code used to be in wxWindow::PostDestroyChildren (wx_dialog.cpp)
285 // but I think this should work, if we destroy the children first.
286 // Note that this might need to be done for wxFrame also.
289 // This causes a crash in e.g. the resource sample when closing
290 // the example dialog. TODO: Probably not necessary (?)
292 // Now process all events, because otherwise
293 // this might remain on the screen.
296 display
= XtDisplay((Widget
) m_mainWidget
);
298 display
= (Display
*) wxGetDisplay();
300 XSync(display
, FALSE
);
302 while (XtAppPending((XtAppContext
) wxTheApp
->GetAppContext())) {
304 XtAppNextEvent((XtAppContext
) wxTheApp
->GetAppContext(), &event
);
305 XtDispatchEvent(&event
);
310 // By default, pressing escape cancels the dialog
311 void wxDialog::OnCharHook(wxKeyEvent
& event
)
313 if (event
.m_keyCode
== WXK_ESCAPE
)
315 // Behaviour changed in 2.0: we'll send a Cancel message
316 // to the dialog instead of Close.
317 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
318 cancelEvent
.SetEventObject( this );
319 GetEventHandler()->ProcessEvent(cancelEvent
);
323 // We didn't process this event.
327 void wxDialog::Iconize(bool WXUNUSED(iconize
))
329 // Can't iconize a dialog in Motif, apparently
330 // TODO: try using the parent of m_mainShell.
331 // XtVaSetValues((Widget) m_mainWidget, XmNiconic, iconize, NULL);
334 bool wxDialog::IsIconized() const
338 XtVaGetValues((Widget) m_mainWidget, XmNiconic, &iconic, NULL);
345 void wxDialog::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
347 XtVaSetValues((Widget
) m_mainWidget
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
348 wxWindow::SetSize(x
, y
, width
, height
, sizeFlags
);
349 XtVaSetValues((Widget
) m_mainWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
352 void wxDialog::SetClientSize(int width
, int height
)
354 SetSize(-1, -1, width
, height
);
358 void wxDialog::SetTitle(const wxString
& title
)
360 m_dialogTitle
= title
;
363 XmString str
= XmStringCreateSimple((char*) (const char*) title
);
364 XtVaSetValues((Widget
) m_mainWidget
,
365 XmNtitle
, (char*) (const char*) title
,
366 XmNdialogTitle
, str
, // Roberto Cocchi
367 XmNiconName
, (char*) (const char*) title
,
373 wxString
wxDialog::GetTitle() const
375 return m_dialogTitle
;
378 void wxDialog::Centre(int direction
)
380 int x_offset
,y_offset
;
381 int display_width
, display_height
;
382 int width
, height
, x
, y
;
383 wxWindow
*parent
= GetParent();
384 if ((direction
& wxCENTER_FRAME
) && parent
)
386 parent
->GetPosition(&x_offset
,&y_offset
) ;
387 parent
->GetSize(&display_width
,&display_height
) ;
391 wxDisplaySize(&display_width
, &display_height
);
396 GetSize(&width
, &height
);
399 if (direction
& wxHORIZONTAL
)
400 x
= (int)((display_width
- width
)/2);
401 if (direction
& wxVERTICAL
)
402 y
= (int)((display_height
- height
)/2);
404 SetSize(x
+x_offset
, y
+y_offset
, width
, height
);
407 void wxDialog::Raise()
409 Window parent_window
= XtWindow((Widget
) m_mainWidget
),
410 next_parent
= XtWindow((Widget
) m_mainWidget
),
411 root
= RootWindowOfScreen(XtScreen((Widget
) m_mainWidget
));
412 // search for the parent that is child of ROOT, because the WM may
413 // reparent twice and notify only the next parent (like FVWM)
414 while (next_parent
!= root
) {
415 Window
*theChildren
; unsigned int n
;
416 parent_window
= next_parent
;
417 XQueryTree(XtDisplay((Widget
) m_mainWidget
), parent_window
, &root
,
418 &next_parent
, &theChildren
, &n
);
419 XFree(theChildren
); // not needed
421 XRaiseWindow(XtDisplay((Widget
) m_mainWidget
), parent_window
);
424 void wxDialog::Lower()
426 Window parent_window
= XtWindow((Widget
) m_mainWidget
),
427 next_parent
= XtWindow((Widget
) m_mainWidget
),
428 root
= RootWindowOfScreen(XtScreen((Widget
) m_mainWidget
));
429 // search for the parent that is child of ROOT, because the WM may
430 // reparent twice and notify only the next parent (like FVWM)
431 while (next_parent
!= root
) {
432 Window
*theChildren
; unsigned int n
;
433 parent_window
= next_parent
;
434 XQueryTree(XtDisplay((Widget
) m_mainWidget
), parent_window
, &root
,
435 &next_parent
, &theChildren
, &n
);
436 XFree(theChildren
); // not needed
438 XLowerWindow(XtDisplay((Widget
) m_mainWidget
), parent_window
);
441 bool wxDialog::Show(bool show
)
447 if (!wxUSE_INVISIBLE_RESIZE
)
448 XtMapWidget(XtParent((Widget
) m_mainWidget
));
450 XtManageChild((Widget
) m_mainWidget
) ;
452 XRaiseWindow(XtDisplay((Widget
) m_mainWidget
), XtWindow((Widget
) m_mainWidget
));
457 if (!wxUSE_INVISIBLE_RESIZE
)
458 XtUnmapWidget(XtParent((Widget
) m_mainWidget
));
460 XtUnmanageChild((Widget
) m_mainWidget
) ;
462 XFlush(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()));
463 XSync(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()), FALSE
);
469 // Shows a dialog modally, returning a return code
470 int wxDialog::ShowModal()
472 m_windowStyle
|= wxDIALOG_MODAL
;
479 wxModalShowingStack
.Insert((wxObject
*)TRUE
);
481 m_modalShowing
= TRUE
;
482 XtAddGrab((Widget
) m_mainWidget
, TRUE
, FALSE
);
486 // Loop until we signal that the dialog should be closed
487 while ((wxModalShowingStack
.Number() > 0) && ((int)(wxModalShowingStack
.First()->Data()) != 0))
489 // XtAppProcessEvent((XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
491 XtAppNextEvent((XtAppContext
) wxTheApp
->GetAppContext(), &event
);
492 wxTheApp
->ProcessXEvent((WXEvent
*) &event
);
495 // Remove modal dialog flag from stack
496 wxNode
*node
= wxModalShowingStack
.First();
500 // Now process all events in case they get sent to a destroyed dialog
501 XSync(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()), FALSE
);
502 while (XtAppPending((XtAppContext
) wxTheApp
->GetAppContext()))
504 XFlush(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()));
505 XtAppNextEvent((XtAppContext
) wxTheApp
->GetAppContext(), &event
);
507 wxTheApp
->ProcessXEvent((WXEvent
*) &event
);
510 // TODO: is it safe to call this, if the dialog may have been deleted
511 // by now? Probably only if we're using delayed deletion of dialogs.
512 return GetReturnCode();
515 void wxDialog::EndModal(int retCode
)
520 SetReturnCode(retCode
);
522 // Strangely, we don't seem to need this now.
523 // XtRemoveGrab((Widget) m_mainWidget);
527 m_modalShowing
= FALSE
;
529 wxNode
*node
= wxModalShowingStack
.First();
531 node
->SetData((wxObject
*)FALSE
);
535 void wxDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
537 if ( Validate() && TransferDataFromWindow() )
543 SetReturnCode(wxID_OK
);
549 void wxDialog::OnApply(wxCommandEvent
& WXUNUSED(event
))
552 TransferDataFromWindow();
553 // TODO probably need to disable the Apply button until things change again
556 void wxDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
559 EndModal(wxID_CANCEL
);
562 SetReturnCode(wxID_CANCEL
);
567 bool wxDialog::OnClose()
569 // Behaviour changed in 2.0: we'll send a Cancel message by default,
570 // which may close the dialog.
571 // Check for looping if the Cancel event handler calls Close()
573 static wxList closing
;
575 if ( closing
.Member(this) )
578 closing
.Append(this);
580 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
581 cancelEvent
.SetEventObject( this );
582 GetEventHandler()->ProcessEvent(cancelEvent
);
584 closing
.DeleteObject(this);
589 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
592 if ( GetEventHandler()->OnClose() || event
.GetForce())
598 // Destroy the window (delayed, if a managed window)
599 bool wxDialog::Destroy()
601 if (!wxPendingDelete
.Member(this))
602 wxPendingDelete
.Append(this);
606 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
608 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
616 // Handle a close event from the window manager
617 static void wxCloseDialogCallback( Widget
WXUNUSED(widget
), XtPointer client_data
,
618 XmAnyCallbackStruct
*WXUNUSED(cbs
))
620 wxDialog
*dialog
= (wxDialog
*)client_data
;
621 wxCloseEvent
closeEvent(wxEVT_CLOSE_WINDOW
, dialog
->GetId());
622 closeEvent
.SetEventObject(dialog
);
624 // May delete the dialog (with delayed deletion)
625 dialog
->GetEventHandler()->ProcessEvent(closeEvent
);
628 // TODO: Preferably, we should have a universal repaint proc.
629 // Meanwhile, use a special one for dialogs.
630 static void wxDialogBoxRepaintProc(Widget w
, XtPointer
WXUNUSED(c_data
), XEvent
*event
, char *)
635 wxWindow
* win
= (wxWindow
*)wxWidgetHashTable
->Get((long)w
);
639 switch(event
-> type
)
643 window
= (Window
) win
-> GetXWindow();
644 display
= (Display
*) win
-> GetXDisplay();
646 wxRect
* rect
= new wxRect(event
->xexpose
.x
, event
->xexpose
.y
,
647 event
->xexpose
.width
, event
->xexpose
.height
);
648 win
->m_updateRects
.Append((wxObject
*) rect
);
650 if (event
-> xexpose
.count
== 0)
652 wxPaintEvent
event(win
->GetId());
653 event
.SetEventObject(win
);
654 win
->GetEventHandler()->ProcessEvent(event
);
656 win
->ClearUpdateRects();
662 cout
<< "\n\nNew Event ! is = " << event
-> type
<< "\n";
668 static void wxDialogBoxEventHandler (Widget wid
,
669 XtPointer
WXUNUSED(client_data
),
671 Boolean
*continueToDispatch
)
673 wxDialog
*dialog
= (wxDialog
*)wxWidgetHashTable
->Get((long)wid
);
676 wxMouseEvent
wxevent(wxEVT_NULL
);
677 if (wxTranslateMouseEvent(wxevent
, dialog
, wid
, event
))
679 wxevent
.SetEventObject(dialog
);
680 wxevent
.SetId(dialog
->GetId());
681 dialog
->GetEventHandler()->ProcessEvent(wxevent
);
685 // An attempt to implement OnCharHook by calling OnCharHook first;
686 // if this returns TRUE, set continueToDispatch to False
687 // (don't continue processing).
688 // Otherwise set it to True and call OnChar.
689 wxKeyEvent
keyEvent(wxEVENT_TYPE_CHAR
);
690 if (wxTranslateKeyEvent(keyEvent
, dialog
, wid
, event
))
692 keyEvent
.SetEventObject(dialog
);
693 keyEvent
.SetId(dialog
->GetId());
694 keyEvent
.SetEventType(wxEVT_CHAR_HOOK
);
695 if (dialog
->GetEventHandler()->ProcessEvent(keyEvent
))
697 *continueToDispatch
= False
;
702 keyEvent
.SetEventType(wxEVT_CHAR
);
703 dialog
->GetEventHandler()->ProcessEvent(keyEvent
);
708 *continueToDispatch
= True
;
711 static void wxUnmapBulletinBoard(Widget
WXUNUSED(dialog
), wxDialog
*WXUNUSED(client
), XtPointer
WXUNUSED(call
) )
713 /* This gets called when the dialog is being shown, which
714 * defeats modal showing.
715 client->m_modalShowing = FALSE ;
716 client->m_isShown = FALSE;
720 void wxDialog::ChangeFont(bool keepOriginalSize
)
722 wxWindow::ChangeFont(keepOriginalSize
);
725 void wxDialog::ChangeBackgroundColour()
728 DoChangeBackgroundColour(GetMainWidget(), m_backgroundColour
);
731 void wxDialog::ChangeForegroundColour()
734 DoChangeForegroundColour(GetMainWidget(), m_foregroundColour
);