1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "dialog.h"
14 #include "wx/dialog.h"
17 #include "wx/cursor.h"
21 #include <gdk/gdkkeysyms.h>
23 #include "wx/gtk/win_gtk.h"
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
29 extern void wxapp_install_idle_handler();
31 extern int g_openDialogs
;
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 extern wxList wxPendingDelete
;
39 //-----------------------------------------------------------------------------
40 // "focus" from m_window
41 //-----------------------------------------------------------------------------
43 static gint
gtk_dialog_focus_callback( GtkWidget
*widget
, GtkDirectionType
WXUNUSED(d
), wxWindow
*WXUNUSED(win
) )
46 wxapp_install_idle_handler();
48 // This disables GTK's tab traversal
49 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "focus" );
53 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
57 bool gtk_dialog_delete_callback( GtkWidget
*WXUNUSED(widget
), GdkEvent
*WXUNUSED(event
), wxDialog
*win
)
60 wxapp_install_idle_handler();
68 //-----------------------------------------------------------------------------
70 //-----------------------------------------------------------------------------
72 static void gtk_dialog_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxDialog
*win
)
75 wxapp_install_idle_handler();
77 if (!win
->m_hasVMT
) return;
79 if ((win
->m_width
!= alloc
->width
) || (win
->m_height
!= alloc
->height
))
81 win
->m_width
= alloc
->width
;
82 win
->m_height
= alloc
->height
;
87 //-----------------------------------------------------------------------------
89 //-----------------------------------------------------------------------------
92 #if (GTK_MINOR_VERSION > 0)
93 gtk_dialog_configure_callback( GtkWidget
*WXUNUSED(widget
), GdkEventConfigure
*WXUNUSED(event
), wxDialog
*win
)
95 gtk_dialog_configure_callback( GtkWidget
*WXUNUSED(widget
), GdkEventConfigure
*event
, wxDialog
*win
)
99 wxapp_install_idle_handler();
101 if (!win
->m_hasVMT
) return FALSE
;
103 #if (GTK_MINOR_VERSION > 0)
106 gdk_window_get_root_origin( win
->m_widget
->window
, &x
, &y
);
114 wxMoveEvent
mevent( wxPoint(win
->m_x
,win
->m_y
), win
->GetId() );
115 mevent
.SetEventObject( win
);
116 win
->GetEventHandler()->ProcessEvent( mevent
);
121 //-----------------------------------------------------------------------------
122 // "realize" from m_widget
123 //-----------------------------------------------------------------------------
125 /* we cannot MWM hints and icons before the widget has been realized,
126 so we do this directly after realization */
129 gtk_dialog_realized_callback( GtkWidget
* WXUNUSED(widget
), wxDialog
*win
)
132 wxapp_install_idle_handler();
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
;
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.
143 This avoids the problem, but looks odd. What shall we do?
145 decor
|= GDK_DECOR_RESIZEH
;
147 if ((win
->GetWindowStyle() & wxCAPTION
) != 0)
148 decor
|= GDK_DECOR_TITLE
;
149 if ((win
->GetWindowStyle() & wxSYSTEM_MENU
) != 0)
151 decor
|= GDK_DECOR_MENU
;
152 func
|= GDK_FUNC_CLOSE
;
154 if ((win
->GetWindowStyle() & wxMINIMIZE_BOX
) != 0)
156 func
|= GDK_FUNC_MINIMIZE
;
157 decor
|= GDK_DECOR_MINIMIZE
;
159 if ((win
->GetWindowStyle() & wxMAXIMIZE_BOX
) != 0)
161 decor
|= GDK_DECOR_MAXIMIZE
;
162 func
|= GDK_FUNC_MAXIMIZE
;
164 if ((win
->GetWindowStyle() & wxRESIZE_BORDER
) != 0)
166 func
|= GDK_FUNC_RESIZE
;
167 decor
|= GDK_DECOR_RESIZEH
;
169 gdk_window_set_decorations( win
->m_widget
->window
, (GdkWMDecoration
)decor
);
170 gdk_window_set_functions( win
->m_widget
->window
, (GdkWMFunction
)func
);
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);
176 gtk_window_set_policy(GTK_WINDOW(win
->m_widget
), 1, 1, 1);
179 if (win
->m_icon
!= wxNullIcon
)
181 wxIcon
icon( win
->m_icon
);
182 win
->m_icon
= wxNullIcon
;
183 win
->SetIcon( icon
);
189 //-----------------------------------------------------------------------------
190 // InsertChild for wxDialog
191 //-----------------------------------------------------------------------------
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. */
198 static void wxInsertChildInDialog( wxDialog
* parent
, wxWindow
* child
)
200 gtk_pizza_put( GTK_PIZZA(parent
->m_wxwindow
),
201 GTK_WIDGET(child
->m_widget
),
207 if (parent
->HasFlag(wxTAB_TRAVERSAL
))
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
);
215 //-----------------------------------------------------------------------------
217 //-----------------------------------------------------------------------------
219 BEGIN_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
)
227 IMPLEMENT_DYNAMIC_CLASS(wxDialog
,wxPanel
)
229 void wxDialog::Init()
233 m_modalShowing
= FALSE
;
234 m_themeEnabled
= TRUE
;
237 wxDialog::wxDialog( wxWindow
*parent
,
238 wxWindowID id
, const wxString
&title
,
239 const wxPoint
&pos
, const wxSize
&size
,
240 long style
, const wxString
&name
)
244 Create( parent
, id
, title
, pos
, size
, style
, name
);
247 bool wxDialog::Create( wxWindow
*parent
,
248 wxWindowID id
, const wxString
&title
,
249 const wxPoint
&pos
, const wxSize
&size
,
250 long style
, const wxString
&name
)
252 wxTopLevelWindows
.Append( this );
254 m_needParent
= FALSE
;
256 if (!PreCreation( parent
, pos
, size
) ||
257 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
259 wxFAIL_MSG( wxT("wxDialog creation failed") );
263 // All dialogs should really have this style
264 m_windowStyle
|= wxTAB_TRAVERSAL
;
266 m_insertCallback
= (wxInsertChildFunction
) wxInsertChildInDialog
;
268 m_widget
= gtk_window_new( GTK_WINDOW_DIALOG
);
270 if ((m_parent
) && (GTK_IS_WINDOW(m_parent
->m_widget
)))
271 gtk_window_set_transient_for( GTK_WINDOW(m_widget
), GTK_WINDOW(m_parent
->m_widget
) );
274 gtk_window_set_wmclass( GTK_WINDOW(m_widget
), name
.mb_str(), name
.mb_str() );
276 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
278 gtk_signal_connect( GTK_OBJECT(m_widget
), "delete_event",
279 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback
), (gpointer
)this );
281 m_wxwindow
= gtk_pizza_new();
282 gtk_widget_show( m_wxwindow
);
283 GTK_WIDGET_UNSET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
285 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
289 if (m_parent
) m_parent
->AddChild( this );
293 if ((m_x
!= -1) || (m_y
!= -1))
294 gtk_widget_set_uposition( m_widget
, m_x
, m_y
);
295 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
297 /* we cannot set MWM hints before the widget has
298 been realized, so we do this directly after realization */
299 gtk_signal_connect( GTK_OBJECT(m_widget
), "realize",
300 GTK_SIGNAL_FUNC(gtk_dialog_realized_callback
), (gpointer
) this );
302 /* the user resized the frame by dragging etc. */
303 gtk_signal_connect( GTK_OBJECT(m_widget
), "size_allocate",
304 GTK_SIGNAL_FUNC(gtk_dialog_size_callback
), (gpointer
)this );
306 gtk_signal_connect( GTK_OBJECT(m_widget
), "configure_event",
307 GTK_SIGNAL_FUNC(gtk_dialog_configure_callback
), (gpointer
)this );
309 /* disable native tab traversal */
310 gtk_signal_connect( GTK_OBJECT(m_widget
), "focus",
311 GTK_SIGNAL_FUNC(gtk_dialog_focus_callback
), (gpointer
)this );
316 wxDialog::~wxDialog()
320 if ((wxTopLevelWindows
.Number() == 0) &&
321 (wxTheApp
->GetExitOnFrameDelete()))
323 wxTheApp
->ExitMainLoop();
327 void wxDialog::SetTitle( const wxString
& title
)
330 gtk_window_set_title( GTK_WINDOW(m_widget
), m_title
.mbc_str() );
333 wxString
wxDialog::GetTitle() const
338 void wxDialog::OnApply( wxCommandEvent
&WXUNUSED(event
) )
340 if (Validate()) TransferDataFromWindow();
343 void wxDialog::OnCancel( wxCommandEvent
&WXUNUSED(event
) )
347 EndModal(wxID_CANCEL
);
351 SetReturnCode(wxID_CANCEL
);
356 void wxDialog::OnOK( wxCommandEvent
&WXUNUSED(event
) )
358 if (Validate() && TransferDataFromWindow())
366 SetReturnCode(wxID_OK
);
372 void wxDialog::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
377 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
379 // We'll send a Cancel message by default,
380 // which may close the dialog.
381 // Check for looping if the Cancel event handler calls Close().
383 // Note that if a cancel button and handler aren't present in the dialog,
384 // nothing will happen when you close the dialog via the window manager, or
386 // We wouldn't want to destroy the dialog by default, since the dialog may have been
387 // created on the stack.
388 // However, this does mean that calling dialog->Close() won't delete the dialog
389 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
390 // sure to destroy the dialog.
391 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
393 static wxList s_closing
;
395 if (s_closing
.Member(this))
398 s_closing
.Append(this);
400 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
401 cancelEvent
.SetEventObject( this );
402 GetEventHandler()->ProcessEvent(cancelEvent
);
403 s_closing
.DeleteObject(this);
406 bool wxDialog::Destroy()
408 // schedule the dialog for the deletion
409 if ( !wxPendingDelete
.Member(this) )
411 wxPendingDelete
.Append(this);
414 // don't leave a dangling pointer as the app top window, we can be deleted
415 // any moment at all now!
421 void wxDialog::CleanUp()
423 m_isBeingDeleted
= TRUE
;
425 if ( wxTheApp
->GetTopWindow() == this )
427 wxTheApp
->SetTopWindow( (wxWindow
*) NULL
);
430 wxTopLevelWindows
.DeleteObject( this );
433 void wxDialog::OnSize( wxSizeEvent
&WXUNUSED(event
) )
435 wxASSERT_MSG( (m_widget
!= NULL
), wxT("invalid dialog") );
437 #if wxUSE_CONSTRAINTS
443 #endif // wxUSE_CONSTRAINTS
445 /* no child: go out ! */
446 if (!GetChildren().First()) return;
448 /* do we have exactly one child? */
449 wxWindow
*child
= (wxWindow
*) NULL
;
450 for(wxNode
*node
= GetChildren().First(); node
; node
= node
->Next())
452 wxWindow
*win
= (wxWindow
*)node
->Data();
453 if (!wxIS_KIND_OF(win
,wxFrame
) && !wxIS_KIND_OF(win
,wxDialog
))
455 /* it's the second one: do nothing */
461 /* yes: set it's size to fill all the frame */
462 int client_x
, client_y
;
463 GetClientSize( &client_x
, &client_y
);
464 child
->SetSize( 1, 1, client_x
-2, client_y
);
468 void wxDialog::DoMoveWindow(int WXUNUSED(x
), int WXUNUSED(y
), int WXUNUSED(width
), int WXUNUSED(height
) )
470 wxFAIL_MSG( wxT("DoMoveWindow called for wxDialog") );
473 void wxDialog::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
475 wxASSERT_MSG( (m_widget
!= NULL
), wxT("invalid dialog") );
476 wxASSERT_MSG( (m_wxwindow
!= NULL
), wxT("invalid dialog") );
478 if (m_resizing
) return; /* I don't like recursions */
484 int old_width
= m_width
;
485 int old_height
= m_height
;
487 if ((sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
) == 0)
489 if (x
!= -1) m_x
= x
;
490 if (y
!= -1) m_y
= y
;
491 if (width
!= -1) m_width
= width
;
492 if (height
!= -1) m_height
= height
;
503 if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH)
505 if (width == -1) m_width = 80;
508 if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT)
510 if (height == -1) m_height = 26;
514 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
515 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
516 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
517 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
519 if ((m_x
!= -1) || (m_y
!= -1))
521 if ((m_x
!= old_x
) || (m_y
!= old_y
))
523 /* we set the position here and when showing the dialog
524 for the first time in idle time */
525 gtk_widget_set_uposition( m_widget
, m_x
, m_y
);
529 if ((m_width
!= old_width
) || (m_height
!= old_height
))
531 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
533 /* actual resizing is deferred to GtkOnSize in idle time and
534 when showing the dialog */
541 void wxDialog::GtkOnSize( int WXUNUSED(x
), int WXUNUSED(y
), int width
, int height
)
543 // due to a bug in gtk, x,y are always 0
547 if ((m_height
== height
) && (m_width
== width
) && (m_sizeSet
)) return;
548 if (!m_wxwindow
) return;
553 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
554 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
555 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
556 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
559 gint flag
= 0; // GDK_HINT_POS;
560 if ((m_minWidth
!= -1) || (m_minHeight
!= -1)) flag
|= GDK_HINT_MIN_SIZE
;
561 if ((m_maxWidth
!= -1) || (m_maxHeight
!= -1)) flag
|= GDK_HINT_MAX_SIZE
;
563 geom
.min_width
= m_minWidth
;
564 geom
.min_height
= m_minHeight
;
565 geom
.max_width
= m_maxWidth
;
566 geom
.max_height
= m_maxHeight
;
567 gtk_window_set_geometry_hints( GTK_WINDOW(m_widget
),
570 (GdkWindowHints
) flag
);
574 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
575 event
.SetEventObject( this );
576 GetEventHandler()->ProcessEvent( event
);
579 void wxDialog::OnInternalIdle()
581 if (!m_sizeSet
&& GTK_WIDGET_REALIZED(m_wxwindow
))
582 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
584 wxWindow::OnInternalIdle();
587 bool wxDialog::Show( bool show
)
589 if (!show
&& IsModal())
591 EndModal( wxID_CANCEL
);
594 if (show
&& !m_sizeSet
)
596 /* by calling GtkOnSize here, we don't have to call
597 either after showing the frame, which would entail
598 much ugly flicker nor from within the size_allocate
599 handler, because GTK 1.1.X forbids that. */
601 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
604 bool ret
= wxWindow::Show( show
);
606 if (show
) InitDialog();
611 bool wxDialog::IsModal() const
613 return m_modalShowing
;
616 void wxDialog::SetModal( bool WXUNUSED(flag
) )
620 m_windowStyle |= wxDIALOG_MODAL;
622 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
624 wxFAIL_MSG( wxT("wxDialog:SetModal obsolete now") );
627 int wxDialog::ShowModal()
631 wxFAIL_MSG( wxT("wxDialog:ShowModal called twice") );
632 return GetReturnCode();
635 // use the apps top level window as parent if none given unless explicitly
637 if ( !GetParent() && !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT
) )
639 wxWindow
*parent
= wxTheApp
->GetTopWindow();
640 if ( parent
&& parent
!= this )
643 gtk_window_set_transient_for( GTK_WINDOW(m_widget
), GTK_WINDOW(parent
->m_widget
) );
647 wxBusyCursorSuspender cs
; // temporarily suppress the busy cursor
651 m_modalShowing
= TRUE
;
655 gtk_grab_add( m_widget
);
657 gtk_grab_remove( m_widget
);
661 return GetReturnCode();
664 void wxDialog::EndModal( int retCode
)
666 SetReturnCode( retCode
);
670 wxFAIL_MSG( wxT("wxDialog:EndModal called twice") );
674 m_modalShowing
= FALSE
;
681 void wxDialog::InitDialog()
683 wxWindow::InitDialog();
686 void wxDialog::SetIcon( const wxIcon
&icon
)
689 if (!icon
.Ok()) return;
691 if (!m_widget
->window
) return;
693 wxMask
*mask
= icon
.GetMask();
694 GdkBitmap
*bm
= (GdkBitmap
*) NULL
;
695 if (mask
) bm
= mask
->GetBitmap();
697 gdk_window_set_icon( m_widget
->window
, (GdkWindow
*) NULL
, icon
.GetPixmap(), bm
);