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