]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk/dialog.cpp
Added chapter on collection and container classes to contents
[wxWidgets.git] / src / gtk / dialog.cpp
... / ...
CommitLineData
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
29extern void wxapp_install_idle_handler();
30extern bool g_isIdle;
31extern int g_openDialogs;
32
33//-----------------------------------------------------------------------------
34// data
35//-----------------------------------------------------------------------------
36
37extern wxList wxPendingDelete;
38
39//-----------------------------------------------------------------------------
40// "focus" from m_window
41//-----------------------------------------------------------------------------
42
43static 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
57bool 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
72static 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
91static gint
92#if (GTK_MINOR_VERSION > 0)
93gtk_dialog_configure_callback( GtkWidget *WXUNUSED(widget), GdkEventConfigure *WXUNUSED(event), wxDialog *win )
94#else
95gtk_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
128static gint
129gtk_dialog_realized_callback( GtkWidget * WXUNUSED(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 /* reset the icon */
179 if (win->m_icon != wxNullIcon)
180 {
181 wxIcon icon( win->m_icon );
182 win->m_icon = wxNullIcon;
183 win->SetIcon( icon );
184 }
185
186 return FALSE;
187}
188
189//-----------------------------------------------------------------------------
190// InsertChild for wxDialog
191//-----------------------------------------------------------------------------
192
193/* Callback for wxFrame. This very strange beast has to be used because
194 * C++ has no virtual methods in a constructor. We have to emulate a
195 * virtual function here as wxWindows requires different ways to insert
196 * a child in container classes. */
197
198static void wxInsertChildInDialog( wxDialog* parent, wxWindow* child )
199{
200 gtk_pizza_put( GTK_PIZZA(parent->m_wxwindow),
201 GTK_WIDGET(child->m_widget),
202 child->m_x,
203 child->m_y,
204 child->m_width,
205 child->m_height );
206
207 if (parent->HasFlag(wxTAB_TRAVERSAL))
208 {
209 /* we now allow a window to get the focus as long as it
210 doesn't have any children. */
211 GTK_WIDGET_UNSET_FLAGS( parent->m_wxwindow, GTK_CAN_FOCUS );
212 }
213}
214
215//-----------------------------------------------------------------------------
216// wxDialog
217//-----------------------------------------------------------------------------
218
219BEGIN_EVENT_TABLE(wxDialog,wxPanel)
220 EVT_BUTTON (wxID_OK, wxDialog::OnOK)
221 EVT_BUTTON (wxID_CANCEL, wxDialog::OnCancel)
222 EVT_BUTTON (wxID_APPLY, wxDialog::OnApply)
223 EVT_SIZE (wxDialog::OnSize)
224 EVT_CLOSE (wxDialog::OnCloseWindow)
225END_EVENT_TABLE()
226
227IMPLEMENT_DYNAMIC_CLASS(wxDialog,wxPanel)
228
229void wxDialog::Init()
230{
231 m_returnCode = 0;
232 m_sizeSet = FALSE;
233 m_modalShowing = FALSE;
234}
235
236wxDialog::wxDialog( wxWindow *parent,
237 wxWindowID id, const wxString &title,
238 const wxPoint &pos, const wxSize &size,
239 long style, const wxString &name )
240{
241 Init();
242
243 Create( parent, id, title, pos, size, style, name );
244}
245
246bool wxDialog::Create( wxWindow *parent,
247 wxWindowID id, const wxString &title,
248 const wxPoint &pos, const wxSize &size,
249 long style, const wxString &name )
250{
251 wxTopLevelWindows.Append( this );
252
253 m_needParent = FALSE;
254
255 if (!PreCreation( parent, pos, size ) ||
256 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
257 {
258 wxFAIL_MSG( wxT("wxDialog creation failed") );
259 return FALSE;
260 }
261
262 // All dialogs should really have this style
263 m_windowStyle |= wxTAB_TRAVERSAL;
264
265 m_insertCallback = (wxInsertChildFunction) wxInsertChildInDialog;
266
267 m_widget = gtk_window_new( GTK_WINDOW_DIALOG );
268
269 if ((m_parent) && (GTK_IS_WINDOW(m_parent->m_widget)))
270 gtk_window_set_transient_for( GTK_WINDOW(m_widget), GTK_WINDOW(m_parent->m_widget) );
271
272 if (!name.IsEmpty())
273 gtk_window_set_wmclass( GTK_WINDOW(m_widget), name.mb_str(), name.mb_str() );
274
275 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
276
277 gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event",
278 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback), (gpointer)this );
279
280 m_wxwindow = gtk_pizza_new();
281 gtk_widget_show( m_wxwindow );
282 GTK_WIDGET_UNSET_FLAGS( m_wxwindow, GTK_CAN_FOCUS );
283
284 gtk_container_add( GTK_CONTAINER(m_widget), m_wxwindow );
285
286 SetTitle( title );
287
288 if (m_parent) m_parent->AddChild( this );
289
290 PostCreation();
291
292 if ((m_x != -1) || (m_y != -1))
293 gtk_widget_set_uposition( m_widget, m_x, m_y );
294 gtk_widget_set_usize( m_widget, m_width, m_height );
295
296 /* we cannot set MWM hints before the widget has
297 been realized, so we do this directly after realization */
298 gtk_signal_connect( GTK_OBJECT(m_widget), "realize",
299 GTK_SIGNAL_FUNC(gtk_dialog_realized_callback), (gpointer) this );
300
301 /* the user resized the frame by dragging etc. */
302 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
303 GTK_SIGNAL_FUNC(gtk_dialog_size_callback), (gpointer)this );
304
305 gtk_signal_connect( GTK_OBJECT(m_widget), "configure_event",
306 GTK_SIGNAL_FUNC(gtk_dialog_configure_callback), (gpointer)this );
307
308 /* disable native tab traversal */
309 gtk_signal_connect( GTK_OBJECT(m_widget), "focus",
310 GTK_SIGNAL_FUNC(gtk_dialog_focus_callback), (gpointer)this );
311
312 return TRUE;
313}
314
315wxDialog::~wxDialog()
316{
317 CleanUp();
318
319 if ((wxTopLevelWindows.Number() == 0) &&
320 (wxTheApp->GetExitOnFrameDelete()))
321 {
322 wxTheApp->ExitMainLoop();
323 }
324}
325
326void wxDialog::SetTitle( const wxString& title )
327{
328 m_title = title;
329 gtk_window_set_title( GTK_WINDOW(m_widget), m_title.mbc_str() );
330}
331
332wxString wxDialog::GetTitle() const
333{
334 return m_title;
335}
336
337void wxDialog::OnApply( wxCommandEvent &WXUNUSED(event) )
338{
339 if (Validate()) TransferDataFromWindow();
340}
341
342void wxDialog::OnCancel( wxCommandEvent &WXUNUSED(event) )
343{
344 if (IsModal())
345 {
346 EndModal(wxID_CANCEL);
347 }
348 else
349 {
350 SetReturnCode(wxID_CANCEL);
351 Show(FALSE);
352 }
353}
354
355void wxDialog::OnOK( wxCommandEvent &WXUNUSED(event) )
356{
357 if (Validate() && TransferDataFromWindow())
358 {
359 if (IsModal())
360 {
361 EndModal(wxID_OK);
362 }
363 else
364 {
365 SetReturnCode(wxID_OK);
366 Show(FALSE);
367 }
368 }
369}
370
371void wxDialog::OnPaint( wxPaintEvent& WXUNUSED(event) )
372{
373 // yes
374}
375
376void wxDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
377{
378 // We'll send a Cancel message by default,
379 // which may close the dialog.
380 // Check for looping if the Cancel event handler calls Close().
381
382 // Note that if a cancel button and handler aren't present in the dialog,
383 // nothing will happen when you close the dialog via the window manager, or
384 // via Close().
385 // We wouldn't want to destroy the dialog by default, since the dialog may have been
386 // created on the stack.
387 // However, this does mean that calling dialog->Close() won't delete the dialog
388 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
389 // sure to destroy the dialog.
390 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
391
392 static wxList s_closing;
393
394 if (s_closing.Member(this))
395 return; // no loops
396
397 s_closing.Append(this);
398
399 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
400 cancelEvent.SetEventObject( this );
401 GetEventHandler()->ProcessEvent(cancelEvent);
402 s_closing.DeleteObject(this);
403}
404
405bool wxDialog::Destroy()
406{
407 // schedule the dialog for the deletion
408 if ( !wxPendingDelete.Member(this) )
409 {
410 wxPendingDelete.Append(this);
411 }
412
413 // don't leave a dangling pointer as the app top window, we can be deleted
414 // any moment at all now!
415 CleanUp();
416
417 return TRUE;
418}
419
420void wxDialog::CleanUp()
421{
422 m_isBeingDeleted = TRUE;
423
424 if ( wxTheApp->GetTopWindow() == this )
425 {
426 wxTheApp->SetTopWindow( (wxWindow*) NULL );
427 }
428
429 wxTopLevelWindows.DeleteObject( this );
430}
431
432void wxDialog::OnSize( wxSizeEvent &WXUNUSED(event) )
433{
434 wxASSERT_MSG( (m_widget != NULL), wxT("invalid dialog") );
435
436#if wxUSE_CONSTRAINTS
437 if (GetAutoLayout())
438 {
439 Layout();
440 }
441 else
442#endif // wxUSE_CONSTRAINTS
443 {
444 /* no child: go out ! */
445 if (!GetChildren().First()) return;
446
447 /* do we have exactly one child? */
448 wxWindow *child = (wxWindow *) NULL;
449 for(wxNode *node = GetChildren().First(); node; node = node->Next())
450 {
451 wxWindow *win = (wxWindow *)node->Data();
452 if (!wxIS_KIND_OF(win,wxFrame) && !wxIS_KIND_OF(win,wxDialog))
453 {
454 /* it's the second one: do nothing */
455 if (child) return;
456 child = win;
457 }
458 }
459
460 /* yes: set it's size to fill all the frame */
461 int client_x, client_y;
462 GetClientSize( &client_x, &client_y );
463 child->SetSize( 1, 1, client_x-2, client_y);
464 }
465}
466
467void wxDialog::DoMoveWindow(int WXUNUSED(x), int WXUNUSED(y), int WXUNUSED(width), int WXUNUSED(height) )
468{
469 wxFAIL_MSG( wxT("DoMoveWindow called for wxDialog") );
470}
471
472void wxDialog::DoSetSize( int x, int y, int width, int height, int sizeFlags )
473{
474 wxASSERT_MSG( (m_widget != NULL), wxT("invalid dialog") );
475 wxASSERT_MSG( (m_wxwindow != NULL), wxT("invalid dialog") );
476
477 if (m_resizing) return; /* I don't like recursions */
478 m_resizing = TRUE;
479
480 int old_x = m_x;
481 int old_y = m_y;
482
483 int old_width = m_width;
484 int old_height = m_height;
485
486 if ((sizeFlags & wxSIZE_ALLOW_MINUS_ONE) == 0)
487 {
488 if (x != -1) m_x = x;
489 if (y != -1) m_y = y;
490 if (width != -1) m_width = width;
491 if (height != -1) m_height = height;
492 }
493 else
494 {
495 m_x = x;
496 m_y = y;
497 m_width = width;
498 m_height = height;
499 }
500
501/*
502 if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH)
503 {
504 if (width == -1) m_width = 80;
505 }
506
507 if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT)
508 {
509 if (height == -1) m_height = 26;
510 }
511*/
512
513 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
514 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
515 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_maxWidth;
516 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_maxHeight;
517
518 if ((m_x != -1) || (m_y != -1))
519 {
520 if ((m_x != old_x) || (m_y != old_y))
521 {
522 /* we set the position here and when showing the dialog
523 for the first time in idle time */
524 gtk_widget_set_uposition( m_widget, m_x, m_y );
525 }
526 }
527
528 if ((m_width != old_width) || (m_height != old_height))
529 {
530 gtk_widget_set_usize( m_widget, m_width, m_height );
531
532 /* actual resizing is deferred to GtkOnSize in idle time and
533 when showing the dialog */
534 m_sizeSet = FALSE;
535 }
536
537 m_resizing = FALSE;
538}
539
540void wxDialog::GtkOnSize( int WXUNUSED(x), int WXUNUSED(y), int width, int height )
541{
542 // due to a bug in gtk, x,y are always 0
543 // m_x = x;
544 // m_y = y;
545
546 if ((m_height == height) && (m_width == width) && (m_sizeSet)) return;
547 if (!m_wxwindow) return;
548
549 m_width = width;
550 m_height = height;
551
552 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
553 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
554 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_maxWidth;
555 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_maxHeight;
556
557 /* set size hints */
558 gint flag = 0; // GDK_HINT_POS;
559 if ((m_minWidth != -1) || (m_minHeight != -1)) flag |= GDK_HINT_MIN_SIZE;
560 if ((m_maxWidth != -1) || (m_maxHeight != -1)) flag |= GDK_HINT_MAX_SIZE;
561 GdkGeometry geom;
562 geom.min_width = m_minWidth;
563 geom.min_height = m_minHeight;
564 geom.max_width = m_maxWidth;
565 geom.max_height = m_maxHeight;
566 gtk_window_set_geometry_hints( GTK_WINDOW(m_widget),
567 (GtkWidget*) NULL,
568 &geom,
569 (GdkWindowHints) flag );
570
571 m_sizeSet = TRUE;
572
573 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
574 event.SetEventObject( this );
575 GetEventHandler()->ProcessEvent( event );
576}
577
578void wxDialog::OnInternalIdle()
579{
580 if (!m_sizeSet && GTK_WIDGET_REALIZED(m_wxwindow))
581 GtkOnSize( m_x, m_y, m_width, m_height );
582
583 wxWindow::OnInternalIdle();
584}
585
586bool wxDialog::Show( bool show )
587{
588 if (!show && IsModal())
589 {
590 EndModal( wxID_CANCEL );
591 }
592
593 if (show && !m_sizeSet)
594 {
595 /* by calling GtkOnSize here, we don't have to call
596 either after showing the frame, which would entail
597 much ugly flicker nor from within the size_allocate
598 handler, because GTK 1.1.X forbids that. */
599
600 GtkOnSize( m_x, m_y, m_width, m_height );
601 }
602
603 bool ret = wxWindow::Show( show );
604
605 if (show) InitDialog();
606
607 return ret;
608}
609
610bool wxDialog::IsModal() const
611{
612 return m_modalShowing;
613}
614
615void wxDialog::SetModal( bool WXUNUSED(flag) )
616{
617/*
618 if (flag)
619 m_windowStyle |= wxDIALOG_MODAL;
620 else
621 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
622*/
623 wxFAIL_MSG( wxT("wxDialog:SetModal obsolete now") );
624}
625
626int wxDialog::ShowModal()
627{
628 if (IsModal())
629 {
630 wxFAIL_MSG( wxT("wxDialog:ShowModal called twice") );
631 return GetReturnCode();
632 }
633
634 if ( !GetParent() )
635 {
636 wxWindow *parent = wxTheApp->GetTopWindow();
637 if ( parent && parent != this )
638 {
639 m_parent = parent;
640 gtk_window_set_transient_for( GTK_WINDOW(m_widget), GTK_WINDOW(parent->m_widget) );
641 }
642 }
643
644 wxBusyCursorSuspender cs; // temporarily suppress the busy cursor
645
646 Show( TRUE );
647
648 m_modalShowing = TRUE;
649
650 g_openDialogs++;
651
652 gtk_grab_add( m_widget );
653 gtk_main();
654 gtk_grab_remove( m_widget );
655
656 g_openDialogs--;
657
658 return GetReturnCode();
659}
660
661void wxDialog::EndModal( int retCode )
662{
663 SetReturnCode( retCode );
664
665 if (!IsModal())
666 {
667 wxFAIL_MSG( wxT("wxDialog:EndModal called twice") );
668 return;
669 }
670
671 m_modalShowing = FALSE;
672
673 gtk_main_quit();
674
675 Show( FALSE );
676}
677
678void wxDialog::InitDialog()
679{
680 wxWindow::InitDialog();
681}
682
683void wxDialog::SetIcon( const wxIcon &icon )
684{
685 m_icon = icon;
686 if (!icon.Ok()) return;
687
688 if (!m_widget->window) return;
689
690 wxMask *mask = icon.GetMask();
691 GdkBitmap *bm = (GdkBitmap *) NULL;
692 if (mask) bm = mask->GetBitmap();
693
694 gdk_window_set_icon( m_widget->window, (GdkWindow *) NULL, icon.GetPixmap(), bm );
695}