]>
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 |
2b854a32 | 7 | // Licence: wxWindows licence |
c801d85f KB |
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 ) | |
2b854a32 | 31 | { |
c801d85f | 32 | /* |
fb1585ae RR |
33 | printf( "OnDelete from " ); |
34 | if (win->GetClassInfo() && win->GetClassInfo()->GetClassName()) | |
35 | printf( win->GetClassInfo()->GetClassName() ); | |
36 | printf( ".\n" ); | |
c801d85f | 37 | */ |
2b854a32 | 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 | ||
de8113d9 RR |
59 | if ((win->m_width != alloc->width) || (win->m_height != alloc->height)) |
60 | { | |
61 | win->m_sizeSet = FALSE; | |
62 | win->m_width = alloc->width; | |
63 | win->m_height = alloc->height; | |
64 | } | |
e52f60e6 RR |
65 | } |
66 | ||
36b3b54a RR |
67 | //----------------------------------------------------------------------------- |
68 | // "configure_event" | |
69 | //----------------------------------------------------------------------------- | |
70 | ||
71 | static gint gtk_dialog_configure_callback( GtkWidget *WXUNUSED(widget), GdkEventConfigure *event, wxDialog *win ) | |
72 | { | |
73 | if (!win->HasVMT()) return FALSE; | |
74 | ||
75 | win->m_x = event->x; | |
76 | win->m_y = event->y; | |
77 | ||
78 | wxMoveEvent mevent( wxPoint(win->m_x,win->m_y), win->GetId() ); | |
79 | mevent.SetEventObject( win ); | |
80 | win->GetEventHandler()->ProcessEvent( mevent ); | |
81 | ||
82 | return FALSE; | |
83 | } | |
84 | ||
c801d85f KB |
85 | //----------------------------------------------------------------------------- |
86 | // wxDialog | |
87 | //----------------------------------------------------------------------------- | |
88 | ||
a60c99e6 | 89 | BEGIN_EVENT_TABLE(wxDialog,wxPanel) |
fb1585ae RR |
90 | EVT_BUTTON (wxID_OK, wxDialog::OnOK) |
91 | EVT_BUTTON (wxID_CANCEL, wxDialog::OnCancel) | |
92 | EVT_BUTTON (wxID_APPLY, wxDialog::OnApply) | |
e52f60e6 | 93 | EVT_SIZE (wxDialog::OnSize) |
fb1585ae | 94 | EVT_CLOSE (wxDialog::OnCloseWindow) |
c801d85f KB |
95 | END_EVENT_TABLE() |
96 | ||
a60c99e6 | 97 | IMPLEMENT_DYNAMIC_CLASS(wxDialog,wxPanel) |
c801d85f | 98 | |
43a18898 | 99 | wxDialog::wxDialog() |
c801d85f | 100 | { |
fb1585ae | 101 | m_title = ""; |
de8113d9 | 102 | m_sizeSet = FALSE; |
fb1585ae | 103 | m_modalShowing = FALSE; |
c33c4050 | 104 | } |
c801d85f | 105 | |
2b854a32 | 106 | wxDialog::wxDialog( wxWindow *parent, |
fb1585ae | 107 | wxWindowID id, const wxString &title, |
2b854a32 | 108 | const wxPoint &pos, const wxSize &size, |
fb1585ae | 109 | long style, const wxString &name ) |
c801d85f | 110 | { |
fb1585ae | 111 | m_modalShowing = FALSE; |
de8113d9 | 112 | m_sizeSet = FALSE; |
fb1585ae | 113 | Create( parent, id, title, pos, size, style, name ); |
c33c4050 | 114 | } |
c801d85f KB |
115 | |
116 | bool wxDialog::Create( wxWindow *parent, | |
fb1585ae | 117 | wxWindowID id, const wxString &title, |
2b854a32 | 118 | const wxPoint &pos, const wxSize &size, |
fb1585ae | 119 | long style, const wxString &name ) |
c801d85f | 120 | { |
a802c3a1 | 121 | wxTopLevelWindows.Append( this ); |
2b854a32 | 122 | |
fb1585ae | 123 | m_needParent = FALSE; |
2b854a32 | 124 | |
fb1585ae | 125 | PreCreation( parent, id, pos, size, style, name ); |
2b854a32 | 126 | |
fb1585ae RR |
127 | m_widget = gtk_window_new( GTK_WINDOW_TOPLEVEL ); |
128 | GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS ); | |
2b854a32 | 129 | |
fb1585ae | 130 | gtk_widget_set( m_widget, "GtkWindow::allow_shrink", TRUE, NULL); |
2b854a32 VZ |
131 | |
132 | gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event", | |
fb1585ae | 133 | GTK_SIGNAL_FUNC(gtk_dialog_delete_callback), (gpointer)this ); |
2b854a32 | 134 | |
fb1585ae RR |
135 | m_wxwindow = gtk_myfixed_new(); |
136 | gtk_widget_show( m_wxwindow ); | |
137 | GTK_WIDGET_UNSET_FLAGS( m_wxwindow, GTK_CAN_FOCUS ); | |
2b854a32 | 138 | |
fb1585ae | 139 | gtk_container_add( GTK_CONTAINER(m_widget), m_wxwindow ); |
2b854a32 | 140 | |
fb1585ae | 141 | SetTitle( title ); |
2b854a32 | 142 | |
de8113d9 | 143 | if (m_parent) m_parent->AddChild( this ); |
2b854a32 | 144 | |
de8113d9 RR |
145 | PostCreation(); |
146 | ||
147 | gtk_widget_realize( m_widget ); | |
2b854a32 VZ |
148 | |
149 | gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate", | |
36b3b54a | 150 | GTK_SIGNAL_FUNC(gtk_dialog_size_callback), (gpointer)this ); |
2b854a32 | 151 | |
36b3b54a RR |
152 | gtk_signal_connect( GTK_OBJECT(m_widget), "configure_event", |
153 | GTK_SIGNAL_FUNC(gtk_dialog_configure_callback), (gpointer)this ); | |
154 | ||
fb1585ae | 155 | return TRUE; |
c33c4050 | 156 | } |
c801d85f | 157 | |
43a18898 | 158 | wxDialog::~wxDialog() |
c801d85f | 159 | { |
fb1585ae | 160 | wxTopLevelWindows.DeleteObject( this ); |
2b854a32 | 161 | |
0d2a2b60 RR |
162 | if (wxTheApp->GetTopWindow() == this) |
163 | { | |
164 | wxTheApp->SetTopWindow( (wxWindow*) NULL ); | |
165 | } | |
2b854a32 | 166 | |
0d2a2b60 | 167 | if (wxTopLevelWindows.Number() == 0) |
2b854a32 | 168 | { |
0d2a2b60 RR |
169 | wxTheApp->ExitMainLoop(); |
170 | } | |
c33c4050 | 171 | } |
c801d85f | 172 | |
43a18898 | 173 | void wxDialog::SetTitle( const wxString& title ) |
c801d85f | 174 | { |
fb1585ae RR |
175 | m_title = title; |
176 | if (m_title.IsNull()) m_title = ""; | |
177 | gtk_window_set_title( GTK_WINDOW(m_widget), m_title ); | |
c33c4050 | 178 | } |
c801d85f | 179 | |
43a18898 | 180 | wxString wxDialog::GetTitle() const |
c801d85f | 181 | { |
fb1585ae | 182 | return (wxString&)m_title; |
c33c4050 | 183 | } |
c801d85f KB |
184 | |
185 | void wxDialog::OnApply( wxCommandEvent &WXUNUSED(event) ) | |
186 | { | |
fb1585ae | 187 | if (Validate()) TransferDataFromWindow(); |
c33c4050 | 188 | } |
c801d85f KB |
189 | |
190 | void wxDialog::OnCancel( wxCommandEvent &WXUNUSED(event) ) | |
191 | { | |
fb1585ae RR |
192 | if (IsModal()) |
193 | { | |
194 | EndModal(wxID_CANCEL); | |
195 | } | |
196 | else | |
197 | { | |
198 | SetReturnCode(wxID_CANCEL); | |
199 | this->Show(FALSE); | |
200 | } | |
c33c4050 | 201 | } |
c801d85f | 202 | |
903f689b | 203 | void wxDialog::OnOK( wxCommandEvent &WXUNUSED(event) ) |
c801d85f | 204 | { |
fb1585ae | 205 | if ( Validate() && TransferDataFromWindow()) |
1a6944fd | 206 | { |
2b854a32 | 207 | if (IsModal()) |
fb1585ae RR |
208 | { |
209 | EndModal(wxID_OK); | |
210 | } | |
211 | else | |
212 | { | |
213 | SetReturnCode(wxID_OK); | |
214 | this->Show(FALSE); | |
215 | } | |
1a6944fd | 216 | } |
c33c4050 | 217 | } |
c801d85f KB |
218 | |
219 | void wxDialog::OnPaint( wxPaintEvent& WXUNUSED(event) ) | |
220 | { | |
2b854a32 | 221 | // yes |
c33c4050 | 222 | } |
c801d85f | 223 | |
2b854a32 | 224 | void wxDialog::OnCloseWindow(wxCloseEvent& event) |
c801d85f | 225 | { |
e3065973 JS |
226 | // We'll send a Cancel message by default, |
227 | // which may close the dialog. | |
228 | // Check for looping if the Cancel event handler calls Close(). | |
229 | ||
230 | // Note that if a cancel button and handler aren't present in the dialog, | |
231 | // nothing will happen when you close the dialog via the window manager, or | |
232 | // via Close(). | |
233 | // We wouldn't want to destroy the dialog by default, since the dialog may have been | |
234 | // created on the stack. | |
235 | // However, this does mean that calling dialog->Close() won't delete the dialog | |
236 | // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be | |
237 | // sure to destroy the dialog. | |
238 | // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog. | |
239 | ||
fb1585ae | 240 | static wxList closing; |
c801d85f | 241 | |
2b854a32 VZ |
242 | if (closing.Member(this)) |
243 | return; // no loops | |
244 | ||
fb1585ae | 245 | closing.Append(this); |
c801d85f | 246 | |
fb1585ae RR |
247 | wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL); |
248 | cancelEvent.SetEventObject( this ); | |
249 | GetEventHandler()->ProcessEvent(cancelEvent); | |
250 | closing.DeleteObject(this); | |
c801d85f KB |
251 | } |
252 | ||
43a18898 | 253 | bool wxDialog::Destroy() |
e2414cbe | 254 | { |
fb1585ae | 255 | if (!wxPendingDelete.Member(this)) wxPendingDelete.Append(this); |
e2414cbe | 256 | |
fb1585ae | 257 | return TRUE; |
e2414cbe RR |
258 | } |
259 | ||
e52f60e6 RR |
260 | void wxDialog::OnSize( wxSizeEvent &WXUNUSED(event) ) |
261 | { | |
2830bf19 | 262 | wxASSERT_MSG( (m_widget != NULL), "invalid dialog" ); |
2b854a32 | 263 | |
e52f60e6 RR |
264 | if (GetAutoLayout()) |
265 | { | |
266 | Layout(); | |
267 | } | |
2b854a32 | 268 | else |
e52f60e6 | 269 | { |
de8113d9 | 270 | /* no child: go out ! */ |
db1b4961 | 271 | if (!GetChildren().First()) return; |
2b854a32 | 272 | |
de8113d9 | 273 | /* do we have exactly one child? */ |
e52f60e6 | 274 | wxWindow *child = (wxWindow *) NULL; |
db1b4961 | 275 | for(wxNode *node = GetChildren().First(); node; node = node->Next()) |
e52f60e6 RR |
276 | { |
277 | wxWindow *win = (wxWindow *)node->Data(); | |
de135918 | 278 | if (!wxIS_KIND_OF(win,wxFrame) && !wxIS_KIND_OF(win,wxDialog)) |
e52f60e6 | 279 | { |
de8113d9 | 280 | /* it's the second one: do nothing */ |
e52f60e6 RR |
281 | if (child) return; |
282 | child = win; | |
283 | } | |
284 | } | |
285 | ||
de8113d9 | 286 | /* yes: set it's size to fill all the frame */ |
e52f60e6 RR |
287 | int client_x, client_y; |
288 | GetClientSize( &client_x, &client_y ); | |
289 | child->SetSize( 1, 1, client_x-2, client_y); | |
290 | } | |
291 | } | |
292 | ||
bfc6fde4 | 293 | void wxDialog::DoSetSize( int x, int y, int width, int height, int sizeFlags ) |
903f689b | 294 | { |
de8113d9 RR |
295 | wxASSERT_MSG( (m_widget != NULL), "invalid dialog" ); |
296 | wxASSERT_MSG( (m_wxwindow != NULL), "invalid dialog" ); | |
fb1585ae | 297 | |
de8113d9 | 298 | if (m_resizing) return; /* I don't like recursions */ |
fb1585ae RR |
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; | |
2b854a32 | 305 | |
fb1585ae RR |
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 | } | |
2b854a32 | 330 | |
fb1585ae RR |
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; | |
0c77152e RR |
333 | if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_maxWidth; |
334 | if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_maxHeight; | |
fb1585ae RR |
335 | |
336 | if ((m_x != -1) || (m_y != -1)) | |
337 | { | |
2b854a32 | 338 | if ((m_x != old_x) || (m_y != old_y)) |
de8113d9 RR |
339 | { |
340 | /* m_sizeSet = FALSE; */ | |
341 | gtk_widget_set_uposition( m_widget, m_x, m_y ); | |
342 | } | |
fb1585ae | 343 | } |
2b854a32 | 344 | |
fb1585ae RR |
345 | if ((m_width != old_width) || (m_height != old_height)) |
346 | { | |
de8113d9 | 347 | m_sizeSet = FALSE; |
fb1585ae | 348 | } |
2b854a32 | 349 | |
fb1585ae | 350 | m_resizing = FALSE; |
903f689b RR |
351 | } |
352 | ||
de8113d9 RR |
353 | void wxDialog::GtkOnSize( int WXUNUSED(x), int WXUNUSED(y), int width, int height ) |
354 | { | |
355 | // due to a bug in gtk, x,y are always 0 | |
356 | // m_x = x; | |
357 | // m_y = y; | |
358 | ||
359 | if ((m_height == height) && (m_width == width) && (m_sizeSet)) return; | |
360 | if (!m_wxwindow) return; | |
361 | ||
362 | m_width = width; | |
363 | m_height = height; | |
364 | ||
365 | if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth; | |
366 | if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight; | |
367 | if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_maxWidth; | |
368 | if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_maxHeight; | |
369 | ||
370 | /* we actually set the size of a frame here and no-where else */ | |
371 | gtk_widget_set_usize( m_widget, m_width, m_height ); | |
372 | ||
373 | m_sizeSet = TRUE; | |
374 | ||
375 | wxSizeEvent event( wxSize(m_width,m_height), GetId() ); | |
376 | event.SetEventObject( this ); | |
377 | GetEventHandler()->ProcessEvent( event ); | |
378 | } | |
379 | ||
903f689b RR |
380 | void wxDialog::Centre( int direction ) |
381 | { | |
f5eafd0e | 382 | wxASSERT_MSG( (m_widget != NULL), "invalid dialog" ); |
2b854a32 | 383 | |
43a18898 RR |
384 | int x = 0; |
385 | int y = 0; | |
2b854a32 | 386 | |
f5eafd0e RR |
387 | if ((direction & wxHORIZONTAL) == wxHORIZONTAL) x = (gdk_screen_width () - m_width) / 2; |
388 | if ((direction & wxVERTICAL) == wxVERTICAL) y = (gdk_screen_height () - m_height) / 2; | |
2b854a32 | 389 | |
fb1585ae | 390 | Move( x, y ); |
903f689b RR |
391 | } |
392 | ||
de8113d9 RR |
393 | void wxDialog::OnInternalIdle() |
394 | { | |
395 | if (!m_sizeSet) | |
396 | GtkOnSize( m_x, m_y, m_width, m_height ); | |
397 | } | |
398 | ||
debe6624 | 399 | bool wxDialog::Show( bool show ) |
c801d85f | 400 | { |
fb1585ae RR |
401 | if (!show && IsModal()) |
402 | { | |
de8113d9 | 403 | EndModal( wxID_CANCEL ); |
fb1585ae | 404 | } |
c801d85f | 405 | |
de8113d9 RR |
406 | if (show && !m_sizeSet) |
407 | { | |
408 | /* by calling GtkOnSize here, we don't have to call | |
409 | either after showing the frame, which would entail | |
410 | much ugly flicker nor from within the size_allocate | |
411 | handler, because GTK 1.1.X forbids that. */ | |
412 | ||
413 | GtkOnSize( m_x, m_y, m_width, m_height ); | |
414 | } | |
2b854a32 | 415 | |
de8113d9 RR |
416 | wxWindow::Show( show ); |
417 | ||
fb1585ae | 418 | if (show) InitDialog(); |
2b854a32 | 419 | |
fb1585ae | 420 | return TRUE; |
c33c4050 RR |
421 | } |
422 | ||
43a18898 | 423 | bool wxDialog::IsModal() const |
e1e955e1 | 424 | { |
fb1585ae | 425 | return m_modalShowing; |
e1e955e1 RR |
426 | } |
427 | ||
428 | void wxDialog::SetModal( bool WXUNUSED(flag) ) | |
c33c4050 | 429 | { |
e1e955e1 | 430 | /* |
c33c4050 RR |
431 | if (flag) |
432 | m_windowStyle |= wxDIALOG_MODAL; | |
433 | else | |
434 | if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL; | |
e1e955e1 | 435 | */ |
fb1585ae | 436 | wxFAIL_MSG( "wxDialog:SetModal obsolete now" ); |
c33c4050 | 437 | } |
c801d85f | 438 | |
43a18898 | 439 | int wxDialog::ShowModal() |
c801d85f | 440 | { |
fb1585ae RR |
441 | if (IsModal()) |
442 | { | |
443 | wxFAIL_MSG( "wxDialog:ShowModal called twice" ); | |
444 | return GetReturnCode(); | |
445 | } | |
f234c60c | 446 | |
fb1585ae | 447 | Show( TRUE ); |
2b854a32 | 448 | |
fb1585ae | 449 | m_modalShowing = TRUE; |
2b854a32 | 450 | |
fb1585ae RR |
451 | gtk_grab_add( m_widget ); |
452 | gtk_main(); | |
453 | gtk_grab_remove( m_widget ); | |
2b854a32 | 454 | |
fb1585ae | 455 | return GetReturnCode(); |
c33c4050 | 456 | } |
c801d85f KB |
457 | |
458 | void wxDialog::EndModal( int retCode ) | |
459 | { | |
fb1585ae | 460 | SetReturnCode( retCode ); |
2b854a32 | 461 | |
fb1585ae RR |
462 | if (!IsModal()) |
463 | { | |
464 | wxFAIL_MSG( "wxDialog:EndModal called twice" ); | |
465 | return; | |
466 | } | |
2b854a32 | 467 | |
fb1585ae | 468 | m_modalShowing = FALSE; |
2b854a32 | 469 | |
fb1585ae | 470 | gtk_main_quit(); |
2b854a32 | 471 | |
fb1585ae | 472 | Show( FALSE ); |
c33c4050 | 473 | } |
c801d85f | 474 | |
43a18898 | 475 | void wxDialog::InitDialog() |
c801d85f | 476 | { |
fb1585ae | 477 | wxWindow::InitDialog(); |
c33c4050 RR |
478 | } |
479 | ||
c33c4050 RR |
480 | void wxDialog::SetIcon( const wxIcon &icon ) |
481 | { | |
fb1585ae RR |
482 | m_icon = icon; |
483 | if (!icon.Ok()) return; | |
2b854a32 | 484 | |
fb1585ae RR |
485 | wxMask *mask = icon.GetMask(); |
486 | GdkBitmap *bm = (GdkBitmap *) NULL; | |
487 | if (mask) bm = mask->GetBitmap(); | |
2b854a32 | 488 | |
fb1585ae | 489 | gdk_window_set_icon( m_widget->window, (GdkWindow *) NULL, icon.GetPixmap(), bm ); |
c33c4050 | 490 | } |