]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/dialog.cpp
call Show(false) from ~wxDialog to call EndModal if the dialog is still modal consist...
[wxWidgets.git] / src / gtk / dialog.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/dialog.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #include "wx/dialog.h"
14
15 #ifndef WX_PRECOMP
16 #include "wx/cursor.h"
17 #endif // WX_PRECOMP
18
19 #include "wx/evtloop.h"
20
21 #include <gtk/gtk.h>
22
23 // this is defined in src/gtk/toplevel.cpp
24 extern int wxOpenModalDialogsCount;
25
26 //-----------------------------------------------------------------------------
27 // wxDialog
28 //-----------------------------------------------------------------------------
29
30 IMPLEMENT_DYNAMIC_CLASS(wxDialog,wxTopLevelWindow)
31
32 void wxDialog::Init()
33 {
34 m_returnCode = 0;
35 m_modalShowing = false;
36 m_themeEnabled = true;
37 }
38
39 wxDialog::wxDialog( wxWindow *parent,
40 wxWindowID id, const wxString &title,
41 const wxPoint &pos, const wxSize &size,
42 long style, const wxString &name )
43 {
44 Init();
45
46 (void)Create( parent, id, title, pos, size, style, name );
47 }
48
49 bool wxDialog::Create( wxWindow *parent,
50 wxWindowID id, const wxString &title,
51 const wxPoint &pos, const wxSize &size,
52 long style, const wxString &name )
53 {
54 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
55
56 // all dialogs should have tab traversal enabled
57 style |= wxTAB_TRAVERSAL;
58
59 return wxTopLevelWindow::Create(parent, id, title, pos, size, style, name);
60 }
61
62 bool wxDialog::Show( bool show )
63 {
64 if (!show && IsModal())
65 {
66 EndModal( wxID_CANCEL );
67 }
68
69 if (show && CanDoLayoutAdaptation())
70 DoLayoutAdaptation();
71
72 bool ret = wxWindow::Show( show );
73
74 if (show)
75 InitDialog();
76
77 return ret;
78 }
79
80 wxDialog::~wxDialog()
81 {
82 m_isBeingDeleted = true;
83
84 // if the dialog is modal, this will end its event loop
85 Show(false);
86 }
87
88 bool wxDialog::IsModal() const
89 {
90 return m_modalShowing;
91 }
92
93 void wxDialog::SetModal( bool WXUNUSED(flag) )
94 {
95 wxFAIL_MSG( wxT("wxDialog:SetModal obsolete now") );
96 }
97
98 int wxDialog::ShowModal()
99 {
100 if (IsModal())
101 {
102 wxFAIL_MSG( wxT("wxDialog:ShowModal called twice") );
103 return GetReturnCode();
104 }
105
106 // release the mouse if it's currently captured as the window having it
107 // will be disabled when this dialog is shown -- but will still keep the
108 // capture making it impossible to do anything in the modal dialog itself
109 wxWindow * const win = wxWindow::GetCapture();
110 if ( win )
111 win->GTKReleaseMouseAndNotify();
112
113 // use the apps top level window as parent if none given unless explicitly
114 // forbidden
115 if ( !GetParent() && !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT) )
116 {
117 wxWindow * const parent = GetParentForModalDialog();
118 if ( parent && parent != this )
119 {
120 gtk_window_set_transient_for( GTK_WINDOW(m_widget),
121 GTK_WINDOW(parent->m_widget) );
122 }
123 }
124
125 wxBusyCursorSuspender cs; // temporarily suppress the busy cursor
126
127 Show( true );
128
129 m_modalShowing = true;
130
131 wxOpenModalDialogsCount++;
132
133 // NOTE: gtk_window_set_modal internally calls gtk_grab_add() !
134 gtk_window_set_modal(GTK_WINDOW(m_widget), TRUE);
135
136 wxGUIEventLoop().Run();
137
138 gtk_window_set_modal(GTK_WINDOW(m_widget), FALSE);
139
140 wxOpenModalDialogsCount--;
141
142 return GetReturnCode();
143 }
144
145 void wxDialog::EndModal( int retCode )
146 {
147 SetReturnCode( retCode );
148
149 if (!IsModal())
150 {
151 wxFAIL_MSG( "either wxDialog:EndModal called twice or ShowModal wasn't called" );
152 return;
153 }
154
155 m_modalShowing = false;
156
157 gtk_main_quit();
158
159 Show( false );
160 }