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