Added IsTopLevel() as a virtual function.
[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
18 #include "gdk/gdk.h"
19 #include "gtk/gtk.h"
20 #include "wx/gtk/win_gtk.h"
21 #include "wx/cursor.h"
22
23 /*
24 #include "gdk/gdkprivate.h"
25 #include "gdk/gdkx.h"
26 */
27
28 //-----------------------------------------------------------------------------
29 // idle system
30 //-----------------------------------------------------------------------------
31
32 extern void wxapp_install_idle_handler();
33 extern bool g_isIdle;
34
35 //-----------------------------------------------------------------------------
36 // data
37 //-----------------------------------------------------------------------------
38
39 extern wxList wxPendingDelete;
40
41 //-----------------------------------------------------------------------------
42 // "delete_event"
43 //-----------------------------------------------------------------------------
44
45 bool gtk_dialog_delete_callback( GtkWidget *WXUNUSED(widget), GdkEvent *WXUNUSED(event), wxDialog *win )
46 {
47 if (g_isIdle)
48 wxapp_install_idle_handler();
49
50 win->Close();
51
52 return TRUE;
53 }
54
55 //-----------------------------------------------------------------------------
56 // "size_allocate"
57 //-----------------------------------------------------------------------------
58
59 static void gtk_dialog_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxDialog *win )
60 {
61 if (g_isIdle)
62 wxapp_install_idle_handler();
63
64 if (!win->m_hasVMT) return;
65
66 if ((win->m_width != alloc->width) || (win->m_height != alloc->height))
67 {
68 win->m_width = alloc->width;
69 win->m_height = alloc->height;
70 win->UpdateSize();
71 }
72 }
73
74 //-----------------------------------------------------------------------------
75 // "configure_event"
76 //-----------------------------------------------------------------------------
77
78 static gint
79 #if (GTK_MINOR_VERSON > 0)
80 gtk_dialog_configure_callback( GtkWidget *WXUNUSED(widget), GdkEventConfigure *WXUNUSED(event), wxDialog *win )
81 #else
82 gtk_dialog_configure_callback( GtkWidget *WXUNUSED(widget), GdkEventConfigure *event, wxDialog *win )
83 #endif
84 {
85 if (g_isIdle)
86 wxapp_install_idle_handler();
87
88 if (!win->m_hasVMT) return FALSE;
89
90 #if (GTK_MINOR_VERSON > 0)
91 int x = 0;
92 int y = 0;
93 gdk_window_get_root_origin( win->m_widget->window, &x, &y );
94 win->m_x = x;
95 win->m_y = y;
96 #else
97 win->m_x = event->x;
98 win->m_y = event->y;
99 #endif
100
101 wxMoveEvent mevent( wxPoint(win->m_x,win->m_y), win->GetId() );
102 mevent.SetEventObject( win );
103 win->GetEventHandler()->ProcessEvent( mevent );
104
105 return FALSE;
106 }
107
108 //-----------------------------------------------------------------------------
109 // "realize" from m_widget
110 //-----------------------------------------------------------------------------
111
112 /* we cannot MWM hints and icons before the widget has been realized,
113 so we do this directly after realization */
114
115 static gint
116 gtk_dialog_realized_callback( GtkWidget *widget, wxDialog *win )
117 {
118 if (g_isIdle)
119 wxapp_install_idle_handler();
120
121 /* I haven't been able to set the position of
122 the dialog before it is shown, so I set the
123 position in "realize" */
124 gtk_widget_set_uposition( widget, win->m_x, win->m_y );
125
126 /* all this is for Motif Window Manager "hints" and is supposed to be
127 recognized by other WM as well. not tested. */
128 long decor = (long) GDK_DECOR_BORDER;
129 long func = (long) GDK_FUNC_MOVE ;
130
131 /* Some WM don't display any border around the frame contents if
132 used with these hints, so we add a resize border around it,
133 without automatically allowinng it to be resized though.
134
135 This avoids the problem, but looks odd. What shall we do?
136 */
137 decor |= GDK_DECOR_RESIZEH;
138
139 if ((win->GetWindowStyle() & wxCAPTION) != 0)
140 decor |= GDK_DECOR_TITLE;
141 if ((win->GetWindowStyle() & wxSYSTEM_MENU) != 0)
142 {
143 decor |= GDK_DECOR_MENU;
144 func |= GDK_FUNC_CLOSE;
145 }
146 if ((win->GetWindowStyle() & wxMINIMIZE_BOX) != 0)
147 {
148 func |= GDK_FUNC_MINIMIZE;
149 decor |= GDK_DECOR_MINIMIZE;
150 }
151 if ((win->GetWindowStyle() & wxMAXIMIZE_BOX) != 0)
152 {
153 decor |= GDK_DECOR_MAXIMIZE;
154 func |= GDK_FUNC_MAXIMIZE;
155 }
156 if ((win->GetWindowStyle() & wxRESIZE_BORDER) != 0)
157 {
158 func |= GDK_FUNC_RESIZE;
159 decor |= GDK_DECOR_RESIZEH;
160 }
161 gdk_window_set_decorations( win->m_widget->window, (GdkWMDecoration)decor);
162 gdk_window_set_functions( win->m_widget->window, (GdkWMFunction)func);
163
164 /* GTK's shrinking/growing policy */
165 if ((win->GetWindowStyle() & wxRESIZE_BORDER) == 0)
166 gtk_window_set_policy(GTK_WINDOW(win->m_widget), 0, 0, 1);
167 else
168 gtk_window_set_policy(GTK_WINDOW(win->m_widget), 1, 1, 1);
169
170 /* set size hints */
171 gint flag = 0; // GDK_HINT_POS;
172 if ((win->GetMinWidth() != -1) || (win->GetMinHeight() != -1)) flag |= GDK_HINT_MIN_SIZE;
173 if ((win->GetMaxWidth() != -1) || (win->GetMaxHeight() != -1)) flag |= GDK_HINT_MAX_SIZE;
174 if (flag)
175 {
176 gdk_window_set_hints( win->m_widget->window,
177 win->m_x, win->m_y,
178 win->GetMinWidth(), win->GetMinHeight(),
179 win->GetMaxWidth(), win->GetMaxHeight(),
180 flag );
181 }
182
183 /* reset the icon */
184 if (win->m_icon != wxNullIcon)
185 {
186 wxIcon icon( win->m_icon );
187 win->m_icon = wxNullIcon;
188 win->SetIcon( icon );
189 }
190
191 return FALSE;
192 }
193
194 //-----------------------------------------------------------------------------
195 // InsertChild for wxDialog
196 //-----------------------------------------------------------------------------
197
198 /* Callback for wxFrame. This very strange beast has to be used because
199 * C++ has no virtual methods in a constructor. We have to emulate a
200 * virtual function here as wxWindows requires different ways to insert
201 * a child in container classes. */
202
203 static void wxInsertChildInDialog( wxDialog* parent, wxWindow* child )
204 {
205 gtk_pizza_put( GTK_PIZZA(parent->m_wxwindow),
206 GTK_WIDGET(child->m_widget),
207 child->m_x,
208 child->m_y,
209 child->m_width,
210 child->m_height );
211
212 if (parent->HasFlag(wxTAB_TRAVERSAL))
213 {
214 /* we now allow a window to get the focus as long as it
215 doesn't have any children. */
216 GTK_WIDGET_UNSET_FLAGS( parent->m_wxwindow, GTK_CAN_FOCUS );
217 }
218 }
219
220 //-----------------------------------------------------------------------------
221 // wxDialog
222 //-----------------------------------------------------------------------------
223
224 BEGIN_EVENT_TABLE(wxDialog,wxPanel)
225 EVT_BUTTON (wxID_OK, wxDialog::OnOK)
226 EVT_BUTTON (wxID_CANCEL, wxDialog::OnCancel)
227 EVT_BUTTON (wxID_APPLY, wxDialog::OnApply)
228 EVT_SIZE (wxDialog::OnSize)
229 EVT_CLOSE (wxDialog::OnCloseWindow)
230 END_EVENT_TABLE()
231
232 IMPLEMENT_DYNAMIC_CLASS(wxDialog,wxPanel)
233
234 void wxDialog::Init()
235 {
236 m_returnCode = 0;
237 m_sizeSet = FALSE;
238 m_modalShowing = FALSE;
239 m_isFrame = TRUE;
240 }
241
242 wxDialog::wxDialog( wxWindow *parent,
243 wxWindowID id, const wxString &title,
244 const wxPoint &pos, const wxSize &size,
245 long style, const wxString &name )
246 {
247 Init();
248
249 Create( parent, id, title, pos, size, style, name );
250 }
251
252 bool wxDialog::Create( wxWindow *parent,
253 wxWindowID id, const wxString &title,
254 const wxPoint &pos, const wxSize &size,
255 long style, const wxString &name )
256 {
257 wxTopLevelWindows.Append( this );
258
259 m_needParent = FALSE;
260
261 if (!PreCreation( parent, pos, size ) ||
262 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
263 {
264 wxFAIL_MSG( wxT("wxDialog creation failed") );
265 return FALSE;
266 }
267
268 m_insertCallback = (wxInsertChildFunction) wxInsertChildInDialog;
269
270 m_widget = gtk_window_new( GTK_WINDOW_DIALOG );
271
272 if ((m_parent) && (GTK_IS_WINDOW(m_parent->m_widget)))
273 gtk_window_set_transient_for( GTK_WINDOW(m_widget), GTK_WINDOW(m_parent->m_widget) );
274
275 if (!name.IsEmpty())
276 gtk_window_set_wmclass( GTK_WINDOW(m_widget), name.mb_str(), name.mb_str() );
277
278 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
279
280 gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event",
281 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback), (gpointer)this );
282
283 m_wxwindow = gtk_pizza_new();
284 gtk_widget_show( m_wxwindow );
285 GTK_WIDGET_UNSET_FLAGS( m_wxwindow, GTK_CAN_FOCUS );
286
287 gtk_container_add( GTK_CONTAINER(m_widget), m_wxwindow );
288
289 SetTitle( title );
290
291 if (m_parent) m_parent->AddChild( this );
292
293 PostCreation();
294
295 /* we cannot set MWM hints before the widget has
296 been realized, so we do this directly after realization */
297 gtk_signal_connect( GTK_OBJECT(m_widget), "realize",
298 GTK_SIGNAL_FUNC(gtk_dialog_realized_callback), (gpointer) this );
299
300 /* the user resized the frame by dragging etc. */
301 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
302 GTK_SIGNAL_FUNC(gtk_dialog_size_callback), (gpointer)this );
303
304 gtk_signal_connect( GTK_OBJECT(m_widget), "configure_event",
305 GTK_SIGNAL_FUNC(gtk_dialog_configure_callback), (gpointer)this );
306
307 return TRUE;
308 }
309
310 wxDialog::~wxDialog()
311 {
312 m_isBeingDeleted = TRUE;
313
314 wxTopLevelWindows.DeleteObject( this );
315
316 if (wxTheApp->GetTopWindow() == this)
317 {
318 wxTheApp->SetTopWindow( (wxWindow*) NULL );
319 }
320
321 if (wxTopLevelWindows.Number() == 0)
322 {
323 wxTheApp->ExitMainLoop();
324 }
325 }
326
327 void wxDialog::SetTitle( const wxString& title )
328 {
329 m_title = title;
330 if (m_title.IsNull()) m_title = wxT("");
331 gtk_window_set_title( GTK_WINDOW(m_widget), m_title.mbc_str() );
332 }
333
334 wxString wxDialog::GetTitle() const
335 {
336 return (wxString&)m_title;
337 }
338
339 void wxDialog::OnApply( wxCommandEvent &WXUNUSED(event) )
340 {
341 if (Validate()) TransferDataFromWindow();
342 }
343
344 void wxDialog::OnCancel( wxCommandEvent &WXUNUSED(event) )
345 {
346 if (IsModal())
347 {
348 EndModal(wxID_CANCEL);
349 }
350 else
351 {
352 SetReturnCode(wxID_CANCEL);
353 Show(FALSE);
354 }
355 }
356
357 void wxDialog::OnOK( wxCommandEvent &WXUNUSED(event) )
358 {
359 if (Validate() && TransferDataFromWindow())
360 {
361 if (IsModal())
362 {
363 EndModal(wxID_OK);
364 }
365 else
366 {
367 SetReturnCode(wxID_OK);
368 this->Show(FALSE);
369 }
370 }
371 }
372
373 void wxDialog::OnPaint( wxPaintEvent& WXUNUSED(event) )
374 {
375 // yes
376 }
377
378 void wxDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
379 {
380 // We'll send a Cancel message by default,
381 // which may close the dialog.
382 // Check for looping if the Cancel event handler calls Close().
383
384 // Note that if a cancel button and handler aren't present in the dialog,
385 // nothing will happen when you close the dialog via the window manager, or
386 // via Close().
387 // We wouldn't want to destroy the dialog by default, since the dialog may have been
388 // created on the stack.
389 // However, this does mean that calling dialog->Close() won't delete the dialog
390 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
391 // sure to destroy the dialog.
392 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
393
394 static wxList s_closing;
395
396 if (s_closing.Member(this))
397 return; // no loops
398
399 s_closing.Append(this);
400
401 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
402 cancelEvent.SetEventObject( this );
403 GetEventHandler()->ProcessEvent(cancelEvent);
404 s_closing.DeleteObject(this);
405 }
406
407 bool wxDialog::Destroy()
408 {
409 if (!wxPendingDelete.Member(this)) wxPendingDelete.Append(this);
410
411 return TRUE;
412 }
413
414 void wxDialog::OnSize( wxSizeEvent &WXUNUSED(event) )
415 {
416 wxASSERT_MSG( (m_widget != NULL), wxT("invalid dialog") );
417
418 #if wxUSE_CONSTRAINTS
419 if (GetAutoLayout())
420 {
421 Layout();
422 }
423 else
424 #endif // wxUSE_CONSTRAINTS
425 {
426 /* no child: go out ! */
427 if (!GetChildren().First()) return;
428
429 /* do we have exactly one child? */
430 wxWindow *child = (wxWindow *) NULL;
431 for(wxNode *node = GetChildren().First(); node; node = node->Next())
432 {
433 wxWindow *win = (wxWindow *)node->Data();
434 if (!wxIS_KIND_OF(win,wxFrame) && !wxIS_KIND_OF(win,wxDialog))
435 {
436 /* it's the second one: do nothing */
437 if (child) return;
438 child = win;
439 }
440 }
441
442 /* yes: set it's size to fill all the frame */
443 int client_x, client_y;
444 GetClientSize( &client_x, &client_y );
445 child->SetSize( 1, 1, client_x-2, client_y);
446 }
447 }
448
449 void wxDialog::DoSetSize( int x, int y, int width, int height, int sizeFlags )
450 {
451 wxASSERT_MSG( (m_widget != NULL), wxT("invalid dialog") );
452 wxASSERT_MSG( (m_wxwindow != NULL), wxT("invalid dialog") );
453
454 if (m_resizing) return; /* I don't like recursions */
455 m_resizing = TRUE;
456
457 int old_x = m_x;
458 int old_y = m_y;
459 int old_width = m_width;
460 int old_height = m_height;
461
462 if ((sizeFlags & wxSIZE_ALLOW_MINUS_ONE) == 0)
463 {
464 if (x != -1) m_x = x;
465 if (y != -1) m_y = y;
466 if (width != -1) m_width = width;
467 if (height != -1) m_height = height;
468 }
469 else
470 {
471 m_x = x;
472 m_y = y;
473 m_width = width;
474 m_height = height;
475 }
476
477 /*
478 if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH)
479 {
480 if (width == -1) m_width = 80;
481 }
482
483 if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT)
484 {
485 if (height == -1) m_height = 26;
486 }
487 */
488
489 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
490 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
491 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_maxWidth;
492 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_maxHeight;
493
494 if ((m_x != -1) || (m_y != -1))
495 {
496 if ((m_x != old_x) || (m_y != old_y))
497 {
498 /* we set the position here and when showing the dialog
499 for the first time in idle time */
500 gtk_widget_set_uposition( m_widget, m_x, m_y );
501 }
502 }
503
504 if ((m_width != old_width) || (m_height != old_height))
505 {
506 /* actual resizing is deferred to GtkOnSize in idle time and
507 when showing the dialog */
508 m_sizeSet = FALSE;
509 }
510
511 m_resizing = FALSE;
512 }
513
514 void wxDialog::GtkOnSize( int WXUNUSED(x), int WXUNUSED(y), int width, int height )
515 {
516 // due to a bug in gtk, x,y are always 0
517 // m_x = x;
518 // m_y = y;
519
520 if ((m_height == height) && (m_width == width) && (m_sizeSet)) return;
521 if (!m_wxwindow) return;
522
523 m_width = width;
524 m_height = height;
525
526 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
527 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
528 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_maxWidth;
529 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_maxHeight;
530
531 /* we actually set the size of a frame here and no-where else */
532 gtk_widget_set_usize( m_widget, m_width, m_height );
533
534 m_sizeSet = TRUE;
535
536 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
537 event.SetEventObject( this );
538 GetEventHandler()->ProcessEvent( event );
539 }
540
541 void wxDialog::OnInternalIdle()
542 {
543 if (!m_sizeSet && GTK_WIDGET_REALIZED(m_wxwindow))
544 GtkOnSize( m_x, m_y, m_width, m_height );
545
546 wxWindow::OnInternalIdle();
547 }
548
549 bool wxDialog::Show( bool show )
550 {
551 if (!show && IsModal())
552 {
553 EndModal( wxID_CANCEL );
554 }
555
556 if (show && !m_sizeSet)
557 {
558 /* by calling GtkOnSize here, we don't have to call
559 either after showing the frame, which would entail
560 much ugly flicker nor from within the size_allocate
561 handler, because GTK 1.1.X forbids that. */
562
563 GtkOnSize( m_x, m_y, m_width, m_height );
564 }
565
566 bool ret = wxWindow::Show( show );
567
568 if (show) InitDialog();
569
570 return ret;
571 }
572
573 bool wxDialog::IsModal() const
574 {
575 return m_modalShowing;
576 }
577
578 void wxDialog::SetModal( bool WXUNUSED(flag) )
579 {
580 /*
581 if (flag)
582 m_windowStyle |= wxDIALOG_MODAL;
583 else
584 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
585 */
586 wxFAIL_MSG( wxT("wxDialog:SetModal obsolete now") );
587 }
588
589 int wxDialog::ShowModal()
590 {
591 if (IsModal())
592 {
593 wxFAIL_MSG( wxT("wxDialog:ShowModal called twice") );
594 return GetReturnCode();
595 }
596
597 wxBusyCursorSuspender cs; // temporarily suppress the busy cursor
598
599 Show( TRUE );
600
601 m_modalShowing = TRUE;
602
603 gtk_grab_add( m_widget );
604 gtk_main();
605 gtk_grab_remove( m_widget );
606
607 return GetReturnCode();
608 }
609
610 void wxDialog::EndModal( int retCode )
611 {
612 SetReturnCode( retCode );
613
614 if (!IsModal())
615 {
616 wxFAIL_MSG( wxT("wxDialog:EndModal called twice") );
617 return;
618 }
619
620 m_modalShowing = FALSE;
621
622 gtk_main_quit();
623
624 Show( FALSE );
625 }
626
627 void wxDialog::InitDialog()
628 {
629 wxWindow::InitDialog();
630 }
631
632 void wxDialog::SetIcon( const wxIcon &icon )
633 {
634 m_icon = icon;
635 if (!icon.Ok()) return;
636
637 if (!m_widget->window) return;
638
639 wxMask *mask = icon.GetMask();
640 GdkBitmap *bm = (GdkBitmap *) NULL;
641 if (mask) bm = mask->GetBitmap();
642
643 gdk_window_set_icon( m_widget->window, (GdkWindow *) NULL, icon.GetPixmap(), bm );
644 }