]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/dialog.cpp
Changed setup.h so that global memory operators are switched off for GnuWin32;
[wxWidgets.git] / src / gtk1 / dialog.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dialog.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "dialog.h"
12 #endif
13
14 #include "wx/dialog.h"
15 #include "wx/frame.h"
16 #include "wx/app.h"
17 #include "wx/gtk/win_gtk.h"
18
19 //-----------------------------------------------------------------------------
20
21 extern wxList wxPendingDelete;
22
23 //-----------------------------------------------------------------------------
24 // "delete_event"
25 //-----------------------------------------------------------------------------
26
27 bool gtk_dialog_delete_callback( GtkWidget *WXUNUSED(widget), GdkEvent *WXUNUSED(event), wxDialog *win )
28 {
29 /*
30 printf( "OnDelete from " );
31 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
32 printf( win->GetClassInfo()->GetClassName() );
33 printf( ".\n" );
34 */
35
36 win->Close();
37
38 return TRUE;
39 }
40
41 //-----------------------------------------------------------------------------
42 // wxDialog
43 //-----------------------------------------------------------------------------
44
45 BEGIN_EVENT_TABLE(wxDialog,wxPanel)
46 EVT_BUTTON (wxID_OK, wxDialog::OnOK)
47 EVT_BUTTON (wxID_CANCEL, wxDialog::OnCancel)
48 EVT_BUTTON (wxID_APPLY, wxDialog::OnApply)
49 EVT_CLOSE (wxDialog::OnCloseWindow)
50 END_EVENT_TABLE()
51
52 IMPLEMENT_DYNAMIC_CLASS(wxDialog,wxPanel)
53
54 wxDialog::wxDialog(void)
55 {
56 m_title = "";
57 m_modalShowing = FALSE;
58 wxTopLevelWindows.Insert( this );
59 }
60
61 wxDialog::wxDialog( wxWindow *parent,
62 wxWindowID id, const wxString &title,
63 const wxPoint &pos, const wxSize &size,
64 long style, const wxString &name )
65 {
66 m_modalShowing = FALSE;
67 wxTopLevelWindows.Insert( this );
68 Create( parent, id, title, pos, size, style, name );
69 }
70
71 bool wxDialog::Create( wxWindow *parent,
72 wxWindowID id, const wxString &title,
73 const wxPoint &pos, const wxSize &size,
74 long style, const wxString &name )
75 {
76 m_needParent = FALSE;
77
78 PreCreation( parent, id, pos, size, style, name );
79
80 m_widget = gtk_window_new( GTK_WINDOW_TOPLEVEL );
81 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
82
83 gtk_widget_set( m_widget, "GtkWindow::allow_shrink", TRUE, NULL);
84
85 gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event",
86 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback), (gpointer)this );
87
88 m_wxwindow = gtk_myfixed_new();
89 gtk_widget_show( m_wxwindow );
90 GTK_WIDGET_UNSET_FLAGS( m_wxwindow, GTK_CAN_FOCUS );
91
92 gtk_container_add( GTK_CONTAINER(m_widget), m_wxwindow );
93
94 SetTitle( title );
95
96 if ((m_x != -1) || (m_y != -1))
97 gtk_widget_set_uposition( m_widget, m_x, m_y );
98
99 gtk_widget_set_usize( m_widget, m_width, m_height );
100
101 if (m_parent) m_parent->AddChild( this );
102
103
104 PostCreation();
105
106 return TRUE;
107 }
108
109 wxDialog::~wxDialog(void)
110 {
111 wxTopLevelWindows.DeleteObject( this );
112 if (wxTopLevelWindows.Number() == 0) wxTheApp->ExitMainLoop();
113 }
114
115 void wxDialog::SetTitle(const wxString& title )
116 {
117 m_title = title;
118 if (m_title.IsNull()) m_title = "";
119 gtk_window_set_title( GTK_WINDOW(m_widget), m_title );
120 }
121
122 wxString wxDialog::GetTitle(void) const
123 {
124 return (wxString&)m_title;
125 }
126
127 void wxDialog::OnApply( wxCommandEvent &WXUNUSED(event) )
128 {
129 if (Validate()) TransferDataFromWindow();
130 }
131
132 void wxDialog::OnCancel( wxCommandEvent &WXUNUSED(event) )
133 {
134 if (IsModal())
135 {
136 EndModal(wxID_CANCEL);
137 }
138 else
139 {
140 SetReturnCode(wxID_CANCEL);
141 this->Show(FALSE);
142 }
143 }
144
145 void wxDialog::OnOK( wxCommandEvent &WXUNUSED(event) )
146 {
147 if ( Validate() && TransferDataFromWindow())
148 {
149 if (IsModal())
150 {
151 EndModal(wxID_OK);
152 }
153 else
154 {
155 SetReturnCode(wxID_OK);
156 this->Show(FALSE);
157 }
158 }
159 }
160
161 void wxDialog::OnPaint( wxPaintEvent& WXUNUSED(event) )
162 {
163 // yes
164 }
165
166 bool wxDialog::OnClose(void)
167 {
168 static wxList closing;
169
170 if (closing.Member(this)) return FALSE; // no loops
171
172 closing.Append(this);
173
174 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
175 cancelEvent.SetEventObject( this );
176 GetEventHandler()->ProcessEvent(cancelEvent);
177 closing.DeleteObject(this);
178
179 return FALSE;
180 }
181
182 bool wxDialog::Destroy(void)
183 {
184 if (!wxPendingDelete.Member(this))
185 wxPendingDelete.Append(this);
186
187 return TRUE;
188 }
189
190 void wxDialog::OnCloseWindow(wxCloseEvent& event)
191 {
192 if (GetEventHandler()->OnClose() || event.GetForce())
193 {
194 this->Destroy();
195 }
196 }
197
198 void wxDialog::ImplementSetPosition(void)
199 {
200 if ((m_x != -1) || (m_y != -1))
201 gtk_widget_set_uposition( m_widget, m_x, m_y );
202 }
203
204 void wxDialog::Centre( int direction )
205 {
206 if (direction & wxHORIZONTAL == wxHORIZONTAL) m_x = (gdk_screen_width () - m_width) / 2;
207 if (direction & wxVERTICAL == wxVERTICAL) m_y = (gdk_screen_height () - m_height) / 2;
208 ImplementSetPosition();
209 }
210
211 bool wxDialog::Show( bool show )
212 {
213 if (!show && IsModal())
214 {
215 EndModal( wxID_CANCEL );
216 }
217
218 wxWindow::Show( show );
219
220 if (show) InitDialog();
221
222 return TRUE;
223 }
224
225 bool wxDialog::IsModal(void) const
226 {
227 return m_modalShowing;
228 }
229
230 void wxDialog::SetModal( bool WXUNUSED(flag) )
231 {
232 /*
233 if (flag)
234 m_windowStyle |= wxDIALOG_MODAL;
235 else
236 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
237 */
238 wxFAIL_MSG( "wxDialog:SetModal obsolete now" );
239 }
240
241 int wxDialog::ShowModal(void)
242 {
243 if (IsModal())
244 {
245 wxFAIL_MSG( "wxDialog:ShowModal called twice" );
246 return GetReturnCode();
247 }
248
249 Show( TRUE );
250
251 m_modalShowing = TRUE;
252
253 gtk_grab_add( m_widget );
254 gtk_main();
255 gtk_grab_remove( m_widget );
256
257 return GetReturnCode();
258 }
259
260 void wxDialog::EndModal( int retCode )
261 {
262 SetReturnCode( retCode );
263
264 if (!IsModal())
265 {
266 wxFAIL_MSG( "wxDialog:EndModal called twice" );
267 return;
268 }
269
270 m_modalShowing = FALSE;
271
272 gtk_main_quit();
273
274 Show( FALSE );
275 }
276
277 void wxDialog::InitDialog(void)
278 {
279 wxWindow::InitDialog();
280 }
281
282 void wxDialog::SetIcon( const wxIcon &icon )
283 {
284 m_icon = icon;
285 if (!icon.Ok()) return;
286
287 wxMask *mask = icon.GetMask();
288 GdkBitmap *bm = (GdkBitmap *) NULL;
289 if (mask) bm = mask->GetBitmap();
290
291 gdk_window_set_icon( m_widget->window, (GdkWindow *) NULL, icon.GetPixmap(), bm );
292 }