]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: dialog.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
a81258be | 5 | // Id: $Id$ |
01111366 | 6 | // Copyright: (c) 1998 Robert Roebling |
c801d85f KB |
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" | |
83624f79 RR |
17 | |
18 | #include "gdk/gdk.h" | |
19 | #include "gtk/gtk.h" | |
c801d85f KB |
20 | #include "wx/gtk/win_gtk.h" |
21 | ||
e2414cbe RR |
22 | //----------------------------------------------------------------------------- |
23 | ||
24 | extern wxList wxPendingDelete; | |
25 | ||
c801d85f | 26 | //----------------------------------------------------------------------------- |
e1e955e1 RR |
27 | // "delete_event" |
28 | //----------------------------------------------------------------------------- | |
c801d85f KB |
29 | |
30 | bool gtk_dialog_delete_callback( GtkWidget *WXUNUSED(widget), GdkEvent *WXUNUSED(event), wxDialog *win ) | |
31 | { | |
32 | /* | |
fb1585ae RR |
33 | printf( "OnDelete from " ); |
34 | if (win->GetClassInfo() && win->GetClassInfo()->GetClassName()) | |
35 | printf( win->GetClassInfo()->GetClassName() ); | |
36 | printf( ".\n" ); | |
c801d85f KB |
37 | */ |
38 | ||
fb1585ae | 39 | win->Close(); |
c801d85f | 40 | |
fb1585ae | 41 | return TRUE; |
c33c4050 | 42 | } |
c801d85f | 43 | |
e52f60e6 RR |
44 | //----------------------------------------------------------------------------- |
45 | // "size_allocate" | |
46 | //----------------------------------------------------------------------------- | |
47 | ||
48 | static void gtk_dialog_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxDialog *win ) | |
49 | { | |
50 | if (!win->HasVMT()) return; | |
51 | ||
52 | /* | |
53 | printf( "OnDialogResize from " ); | |
54 | if (win->GetClassInfo() && win->GetClassInfo()->GetClassName()) | |
55 | printf( win->GetClassInfo()->GetClassName() ); | |
56 | printf( ".\n" ); | |
57 | */ | |
58 | ||
59 | win->GtkOnSize( alloc->x, alloc->y, alloc->width, alloc->height ); | |
60 | } | |
61 | ||
c801d85f KB |
62 | //----------------------------------------------------------------------------- |
63 | // wxDialog | |
64 | //----------------------------------------------------------------------------- | |
65 | ||
a60c99e6 | 66 | BEGIN_EVENT_TABLE(wxDialog,wxPanel) |
fb1585ae RR |
67 | EVT_BUTTON (wxID_OK, wxDialog::OnOK) |
68 | EVT_BUTTON (wxID_CANCEL, wxDialog::OnCancel) | |
69 | EVT_BUTTON (wxID_APPLY, wxDialog::OnApply) | |
e52f60e6 | 70 | EVT_SIZE (wxDialog::OnSize) |
fb1585ae | 71 | EVT_CLOSE (wxDialog::OnCloseWindow) |
c801d85f KB |
72 | END_EVENT_TABLE() |
73 | ||
a60c99e6 | 74 | IMPLEMENT_DYNAMIC_CLASS(wxDialog,wxPanel) |
c801d85f | 75 | |
43a18898 | 76 | wxDialog::wxDialog() |
c801d85f | 77 | { |
fb1585ae RR |
78 | m_title = ""; |
79 | m_modalShowing = FALSE; | |
c33c4050 | 80 | } |
c801d85f KB |
81 | |
82 | wxDialog::wxDialog( wxWindow *parent, | |
fb1585ae RR |
83 | wxWindowID id, const wxString &title, |
84 | const wxPoint &pos, const wxSize &size, | |
85 | long style, const wxString &name ) | |
c801d85f | 86 | { |
fb1585ae | 87 | m_modalShowing = FALSE; |
fb1585ae | 88 | Create( parent, id, title, pos, size, style, name ); |
c33c4050 | 89 | } |
c801d85f KB |
90 | |
91 | bool wxDialog::Create( wxWindow *parent, | |
fb1585ae RR |
92 | wxWindowID id, const wxString &title, |
93 | const wxPoint &pos, const wxSize &size, | |
94 | long style, const wxString &name ) | |
c801d85f | 95 | { |
a802c3a1 RR |
96 | wxTopLevelWindows.Append( this ); |
97 | ||
fb1585ae | 98 | m_needParent = FALSE; |
c801d85f | 99 | |
fb1585ae | 100 | PreCreation( parent, id, pos, size, style, name ); |
c801d85f | 101 | |
fb1585ae RR |
102 | m_widget = gtk_window_new( GTK_WINDOW_TOPLEVEL ); |
103 | GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS ); | |
c801d85f | 104 | |
fb1585ae | 105 | gtk_widget_set( m_widget, "GtkWindow::allow_shrink", TRUE, NULL); |
c801d85f | 106 | |
fb1585ae RR |
107 | gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event", |
108 | GTK_SIGNAL_FUNC(gtk_dialog_delete_callback), (gpointer)this ); | |
c801d85f | 109 | |
e52f60e6 RR |
110 | gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate", |
111 | GTK_SIGNAL_FUNC(gtk_dialog_size_callback), (gpointer)this ); | |
112 | ||
fb1585ae RR |
113 | m_wxwindow = gtk_myfixed_new(); |
114 | gtk_widget_show( m_wxwindow ); | |
115 | GTK_WIDGET_UNSET_FLAGS( m_wxwindow, GTK_CAN_FOCUS ); | |
c801d85f | 116 | |
fb1585ae | 117 | gtk_container_add( GTK_CONTAINER(m_widget), m_wxwindow ); |
c801d85f | 118 | |
fb1585ae | 119 | SetTitle( title ); |
c801d85f | 120 | |
fb1585ae RR |
121 | if ((m_x != -1) || (m_y != -1)) |
122 | gtk_widget_set_uposition( m_widget, m_x, m_y ); | |
903f689b | 123 | |
fb1585ae | 124 | gtk_widget_set_usize( m_widget, m_width, m_height ); |
903f689b | 125 | |
fb1585ae | 126 | if (m_parent) m_parent->AddChild( this ); |
6ca41e57 RR |
127 | |
128 | ||
fb1585ae | 129 | PostCreation(); |
c801d85f | 130 | |
fb1585ae | 131 | return TRUE; |
c33c4050 | 132 | } |
c801d85f | 133 | |
43a18898 | 134 | wxDialog::~wxDialog() |
c801d85f | 135 | { |
fb1585ae | 136 | wxTopLevelWindows.DeleteObject( this ); |
0d2a2b60 RR |
137 | |
138 | if (wxTheApp->GetTopWindow() == this) | |
139 | { | |
140 | wxTheApp->SetTopWindow( (wxWindow*) NULL ); | |
141 | } | |
142 | ||
143 | if (wxTopLevelWindows.Number() == 0) | |
144 | { | |
145 | wxTheApp->ExitMainLoop(); | |
146 | } | |
c33c4050 | 147 | } |
c801d85f | 148 | |
43a18898 | 149 | void wxDialog::SetTitle( const wxString& title ) |
c801d85f | 150 | { |
fb1585ae RR |
151 | m_title = title; |
152 | if (m_title.IsNull()) m_title = ""; | |
153 | gtk_window_set_title( GTK_WINDOW(m_widget), m_title ); | |
c33c4050 | 154 | } |
c801d85f | 155 | |
43a18898 | 156 | wxString wxDialog::GetTitle() const |
c801d85f | 157 | { |
fb1585ae | 158 | return (wxString&)m_title; |
c33c4050 | 159 | } |
c801d85f KB |
160 | |
161 | void wxDialog::OnApply( wxCommandEvent &WXUNUSED(event) ) | |
162 | { | |
fb1585ae | 163 | if (Validate()) TransferDataFromWindow(); |
c33c4050 | 164 | } |
c801d85f KB |
165 | |
166 | void wxDialog::OnCancel( wxCommandEvent &WXUNUSED(event) ) | |
167 | { | |
fb1585ae RR |
168 | if (IsModal()) |
169 | { | |
170 | EndModal(wxID_CANCEL); | |
171 | } | |
172 | else | |
173 | { | |
174 | SetReturnCode(wxID_CANCEL); | |
175 | this->Show(FALSE); | |
176 | } | |
c33c4050 | 177 | } |
c801d85f | 178 | |
903f689b | 179 | void wxDialog::OnOK( wxCommandEvent &WXUNUSED(event) ) |
c801d85f | 180 | { |
fb1585ae | 181 | if ( Validate() && TransferDataFromWindow()) |
1a6944fd | 182 | { |
fb1585ae RR |
183 | if (IsModal()) |
184 | { | |
185 | EndModal(wxID_OK); | |
186 | } | |
187 | else | |
188 | { | |
189 | SetReturnCode(wxID_OK); | |
190 | this->Show(FALSE); | |
191 | } | |
1a6944fd | 192 | } |
c33c4050 | 193 | } |
c801d85f KB |
194 | |
195 | void wxDialog::OnPaint( wxPaintEvent& WXUNUSED(event) ) | |
196 | { | |
197 | // yes | |
c33c4050 | 198 | } |
c801d85f | 199 | |
43a18898 | 200 | bool wxDialog::OnClose() |
c801d85f | 201 | { |
fb1585ae | 202 | static wxList closing; |
c801d85f | 203 | |
fb1585ae | 204 | if (closing.Member(this)) return FALSE; // no loops |
1a6944fd | 205 | |
fb1585ae | 206 | closing.Append(this); |
c801d85f | 207 | |
fb1585ae RR |
208 | wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL); |
209 | cancelEvent.SetEventObject( this ); | |
210 | GetEventHandler()->ProcessEvent(cancelEvent); | |
211 | closing.DeleteObject(this); | |
c801d85f | 212 | |
fb1585ae | 213 | return FALSE; |
c801d85f KB |
214 | } |
215 | ||
43a18898 | 216 | bool wxDialog::Destroy() |
e2414cbe | 217 | { |
fb1585ae | 218 | if (!wxPendingDelete.Member(this)) wxPendingDelete.Append(this); |
e2414cbe | 219 | |
fb1585ae | 220 | return TRUE; |
e2414cbe RR |
221 | } |
222 | ||
43a18898 | 223 | void wxDialog::OnCloseWindow( wxCloseEvent& event ) |
c801d85f | 224 | { |
fb1585ae RR |
225 | if (GetEventHandler()->OnClose() || event.GetForce()) |
226 | { | |
227 | this->Destroy(); | |
228 | } | |
c33c4050 | 229 | } |
c801d85f | 230 | |
e52f60e6 RR |
231 | void wxDialog::GtkOnSize( int WXUNUSED(x), int WXUNUSED(y), int width, int height ) |
232 | { | |
233 | // due to a bug in gtk, x,y are always 0 | |
234 | // m_x = x; | |
235 | // m_y = y; | |
236 | ||
237 | if ((m_height == height) && (m_width == width) && | |
238 | (m_sizeSet)) return; | |
239 | if (!m_wxwindow) return; | |
240 | ||
241 | m_width = width; | |
242 | m_height = height; | |
243 | ||
244 | if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth; | |
245 | if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight; | |
246 | if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_minWidth; | |
247 | if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_minHeight; | |
248 | ||
249 | gtk_widget_set_usize( m_widget, m_width, m_height ); | |
250 | ||
251 | m_sizeSet = TRUE; | |
252 | ||
253 | wxSizeEvent event( wxSize(m_width,m_height), GetId() ); | |
254 | event.SetEventObject( this ); | |
255 | GetEventHandler()->ProcessEvent( event ); | |
256 | } | |
257 | ||
258 | void wxDialog::OnSize( wxSizeEvent &WXUNUSED(event) ) | |
259 | { | |
2830bf19 | 260 | wxASSERT_MSG( (m_widget != NULL), "invalid dialog" ); |
e52f60e6 RR |
261 | |
262 | if (GetAutoLayout()) | |
263 | { | |
264 | Layout(); | |
265 | } | |
266 | else | |
267 | { | |
268 | // no child: go out ! | |
db1b4961 | 269 | if (!GetChildren().First()) return; |
e52f60e6 RR |
270 | |
271 | // do we have exactly one child? | |
272 | wxWindow *child = (wxWindow *) NULL; | |
db1b4961 | 273 | for(wxNode *node = GetChildren().First(); node; node = node->Next()) |
e52f60e6 RR |
274 | { |
275 | wxWindow *win = (wxWindow *)node->Data(); | |
de135918 | 276 | if (!wxIS_KIND_OF(win,wxFrame) && !wxIS_KIND_OF(win,wxDialog)) |
e52f60e6 RR |
277 | { |
278 | // it's the second one: do nothing | |
279 | if (child) return; | |
280 | child = win; | |
281 | } | |
282 | } | |
283 | ||
284 | // yes: set it's size to fill all the frame | |
285 | int client_x, client_y; | |
286 | GetClientSize( &client_x, &client_y ); | |
287 | child->SetSize( 1, 1, client_x-2, client_y); | |
288 | } | |
289 | } | |
290 | ||
fb1585ae | 291 | void wxDialog::SetSize( int x, int y, int width, int height, int sizeFlags ) |
903f689b | 292 | { |
fb1585ae RR |
293 | wxASSERT_MSG( (m_widget != NULL), "invalid window" ); |
294 | ||
295 | // Don't do anything for children of wxMDIChildFrame | |
296 | if (!m_wxwindow) return; | |
297 | ||
298 | if (m_resizing) return; // I don't like recursions | |
299 | m_resizing = TRUE; | |
300 | ||
301 | int old_x = m_x; | |
302 | int old_y = m_y; | |
303 | int old_width = m_width; | |
304 | int old_height = m_height; | |
305 | ||
306 | if ((sizeFlags & wxSIZE_USE_EXISTING) == wxSIZE_USE_EXISTING) | |
307 | { | |
308 | if (x != -1) m_x = x; | |
309 | if (y != -1) m_y = y; | |
310 | if (width != -1) m_width = width; | |
311 | if (height != -1) m_height = height; | |
312 | } | |
313 | else | |
314 | { | |
315 | m_x = x; | |
316 | m_y = y; | |
317 | m_width = width; | |
318 | m_height = height; | |
319 | } | |
320 | ||
321 | if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH) | |
322 | { | |
323 | if (width == -1) m_width = 80; | |
324 | } | |
325 | ||
326 | if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT) | |
327 | { | |
328 | if (height == -1) m_height = 26; | |
329 | } | |
330 | ||
331 | if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth; | |
332 | if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight; | |
333 | if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_minWidth; | |
334 | if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_minHeight; | |
335 | ||
336 | if ((m_x != -1) || (m_y != -1)) | |
337 | { | |
338 | if ((m_x != old_x) || (m_y != old_y)) | |
339 | gtk_widget_set_uposition( m_widget, m_x, m_y ); | |
340 | } | |
341 | ||
342 | if ((m_width != old_width) || (m_height != old_height)) | |
343 | { | |
344 | gtk_widget_set_usize( m_widget, m_width, m_height ); | |
345 | } | |
346 | ||
347 | m_sizeSet = TRUE; | |
348 | ||
349 | wxSizeEvent event( wxSize(m_width,m_height), GetId() ); | |
350 | event.SetEventObject( this ); | |
e52f60e6 | 351 | GetEventHandler()->ProcessEvent( event ); |
fb1585ae RR |
352 | |
353 | m_resizing = FALSE; | |
903f689b RR |
354 | } |
355 | ||
43a18898 RR |
356 | void wxDialog::SetSize( int width, int height ) |
357 | { | |
358 | SetSize( -1, -1, width, height, wxSIZE_USE_EXISTING ); | |
359 | } | |
360 | ||
903f689b RR |
361 | void wxDialog::Centre( int direction ) |
362 | { | |
fb1585ae RR |
363 | wxASSERT_MSG( (m_widget != NULL), "invalid frame" ); |
364 | ||
43a18898 RR |
365 | int x = 0; |
366 | int y = 0; | |
fb1585ae RR |
367 | |
368 | if (direction & wxHORIZONTAL == wxHORIZONTAL) x = (gdk_screen_width () - m_width) / 2; | |
369 | if (direction & wxVERTICAL == wxVERTICAL) y = (gdk_screen_height () - m_height) / 2; | |
370 | ||
371 | Move( x, y ); | |
903f689b RR |
372 | } |
373 | ||
debe6624 | 374 | bool wxDialog::Show( bool show ) |
c801d85f | 375 | { |
fb1585ae RR |
376 | if (!show && IsModal()) |
377 | { | |
378 | EndModal( wxID_CANCEL ); | |
379 | } | |
c801d85f | 380 | |
fb1585ae | 381 | wxWindow::Show( show ); |
c801d85f | 382 | |
fb1585ae | 383 | if (show) InitDialog(); |
c801d85f | 384 | |
fb1585ae | 385 | return TRUE; |
c33c4050 RR |
386 | } |
387 | ||
43a18898 | 388 | bool wxDialog::IsModal() const |
e1e955e1 | 389 | { |
fb1585ae | 390 | return m_modalShowing; |
e1e955e1 RR |
391 | } |
392 | ||
393 | void wxDialog::SetModal( bool WXUNUSED(flag) ) | |
c33c4050 | 394 | { |
e1e955e1 | 395 | /* |
c33c4050 RR |
396 | if (flag) |
397 | m_windowStyle |= wxDIALOG_MODAL; | |
398 | else | |
399 | if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL; | |
e1e955e1 | 400 | */ |
fb1585ae | 401 | wxFAIL_MSG( "wxDialog:SetModal obsolete now" ); |
c33c4050 | 402 | } |
c801d85f | 403 | |
43a18898 | 404 | int wxDialog::ShowModal() |
c801d85f | 405 | { |
fb1585ae RR |
406 | if (IsModal()) |
407 | { | |
408 | wxFAIL_MSG( "wxDialog:ShowModal called twice" ); | |
409 | return GetReturnCode(); | |
410 | } | |
d355d3fe | 411 | |
fb1585ae | 412 | Show( TRUE ); |
d355d3fe | 413 | |
fb1585ae | 414 | m_modalShowing = TRUE; |
d355d3fe | 415 | |
fb1585ae RR |
416 | gtk_grab_add( m_widget ); |
417 | gtk_main(); | |
418 | gtk_grab_remove( m_widget ); | |
d355d3fe | 419 | |
fb1585ae | 420 | return GetReturnCode(); |
c33c4050 | 421 | } |
c801d85f KB |
422 | |
423 | void wxDialog::EndModal( int retCode ) | |
424 | { | |
fb1585ae | 425 | SetReturnCode( retCode ); |
d355d3fe | 426 | |
fb1585ae RR |
427 | if (!IsModal()) |
428 | { | |
429 | wxFAIL_MSG( "wxDialog:EndModal called twice" ); | |
430 | return; | |
431 | } | |
b6af8d80 | 432 | |
fb1585ae | 433 | m_modalShowing = FALSE; |
d355d3fe | 434 | |
fb1585ae | 435 | gtk_main_quit(); |
5b011451 | 436 | |
fb1585ae | 437 | Show( FALSE ); |
c33c4050 | 438 | } |
c801d85f | 439 | |
43a18898 | 440 | void wxDialog::InitDialog() |
c801d85f | 441 | { |
fb1585ae | 442 | wxWindow::InitDialog(); |
c33c4050 RR |
443 | } |
444 | ||
c33c4050 RR |
445 | void wxDialog::SetIcon( const wxIcon &icon ) |
446 | { | |
fb1585ae RR |
447 | m_icon = icon; |
448 | if (!icon.Ok()) return; | |
c33c4050 | 449 | |
fb1585ae RR |
450 | wxMask *mask = icon.GetMask(); |
451 | GdkBitmap *bm = (GdkBitmap *) NULL; | |
452 | if (mask) bm = mask->GetBitmap(); | |
c33c4050 | 453 | |
fb1585ae | 454 | gdk_window_set_icon( m_widget->window, (GdkWindow *) NULL, icon.GetPixmap(), bm ); |
c33c4050 | 455 | } |