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"
20 #include "wx/gtk/win_gtk.h"
22 //-----------------------------------------------------------------------------
24 //-----------------------------------------------------------------------------
26 extern void wxapp_install_idle_handler();
29 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
33 extern wxList wxPendingDelete
;
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
39 bool gtk_dialog_delete_callback( GtkWidget
*WXUNUSED(widget
), GdkEvent
*WXUNUSED(event
), wxDialog
*win
)
42 wxapp_install_idle_handler();
49 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
53 static void gtk_dialog_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxDialog
*win
)
56 wxapp_install_idle_handler();
58 if (!win
->m_hasVMT
) return;
60 if ((win
->m_width
!= alloc
->width
) || (win
->m_height
!= alloc
->height
))
62 win
->m_width
= alloc
->width
;
63 win
->m_height
= alloc
->height
;
68 //-----------------------------------------------------------------------------
70 //-----------------------------------------------------------------------------
73 #if (GTK_MINOR_VERSON > 0)
74 gtk_dialog_configure_callback( GtkWidget
*WXUNUSED(widget
), GdkEventConfigure
*WXUNUSED(event
), wxDialog
*win
)
76 gtk_dialog_configure_callback( GtkWidget
*WXUNUSED(widget
), GdkEventConfigure
*event
, wxDialog
*win
)
80 wxapp_install_idle_handler();
82 if (!win
->m_hasVMT
) return FALSE
;
84 #if (GTK_MINOR_VERSON > 0)
87 gdk_window_get_root_origin( win
->m_widget
->window
, &x
, &y
);
95 wxMoveEvent
mevent( wxPoint(win
->m_x
,win
->m_y
), win
->GetId() );
96 mevent
.SetEventObject( win
);
97 win
->GetEventHandler()->ProcessEvent( mevent
);
102 //-----------------------------------------------------------------------------
103 // "realize" from m_widget
104 //-----------------------------------------------------------------------------
106 /* we cannot MWM hints and icons before the widget has been realized,
107 so we do this directly after realization */
110 gtk_dialog_realized_callback( GtkWidget
*widget
, wxDialog
*win
)
113 wxapp_install_idle_handler();
115 /* I haven''t been able to set the position of
116 the dialog before it is shown, so I set the
117 position in "realize" and "map" */
118 gtk_widget_set_uposition( widget
, win
->m_x
, win
->m_y
);
121 if (win
->m_icon
!= wxNullIcon
)
123 wxIcon
icon( win
->m_icon
);
124 win
->m_icon
= wxNullIcon
;
125 win
->SetIcon( icon
);
131 //-----------------------------------------------------------------------------
132 // "map" from m_widget
133 //-----------------------------------------------------------------------------
136 gtk_dialog_map_callback( GtkWidget
*widget
, wxDialog
*win
)
139 wxapp_install_idle_handler();
141 /* I haven''t been able to set the position of
142 the dialog before it is shown, so I set the
143 position in "realize" and "map" */
144 gtk_widget_set_uposition( widget
, win
->m_x
, win
->m_y
);
146 /* all this is for Motif Window Manager "hints" and is supposed to be
147 recognized by other WM as well. not tested. */
148 long decor
= (long) GDK_DECOR_BORDER
;
149 long func
= (long) GDK_FUNC_MOVE
;
151 if ((win
->GetWindowStyle() & wxCAPTION
) != 0)
152 decor
|= GDK_DECOR_TITLE
;
153 if ((win
->GetWindowStyle() & wxSYSTEM_MENU
) != 0)
155 decor
|= GDK_DECOR_MENU
;
156 func
|= GDK_FUNC_CLOSE
;
158 if ((win
->GetWindowStyle() & wxMINIMIZE_BOX
) != 0)
160 func
|= GDK_FUNC_MINIMIZE
;
161 decor
|= GDK_DECOR_MINIMIZE
;
163 if ((win
->GetWindowStyle() & wxMAXIMIZE_BOX
) != 0)
165 decor
|= GDK_DECOR_MAXIMIZE
;
166 func
|= GDK_FUNC_MAXIMIZE
;
168 if ((win
->GetWindowStyle() & wxRESIZE_BORDER
) != 0)
170 func
|= GDK_FUNC_RESIZE
;
171 decor
|= GDK_DECOR_RESIZEH
;
173 gdk_window_set_decorations( win
->m_widget
->window
, (GdkWMDecoration
)decor
);
174 gdk_window_set_functions( win
->m_widget
->window
, (GdkWMFunction
)func
);
176 /* GTK's shrinking/growing policy */
177 if ((win
->GetWindowStyle() & wxRESIZE_BORDER
) == 0)
178 gtk_window_set_policy(GTK_WINDOW(win
->m_widget
), 0, 0, 1);
180 gtk_window_set_policy(GTK_WINDOW(win
->m_widget
), 1, 1, 1);
185 //-----------------------------------------------------------------------------
186 // InsertChild for wxDialog
187 //-----------------------------------------------------------------------------
189 /* Callback for wxFrame. This very strange beast has to be used because
190 * C++ has no virtual methods in a constructor. We have to emulate a
191 * virtual function here as wxWindows requires different ways to insert
192 * a child in container classes. */
194 static void wxInsertChildInDialog( wxDialog
* parent
, wxWindow
* child
)
196 gtk_myfixed_put( GTK_MYFIXED(parent
->m_wxwindow
),
197 GTK_WIDGET(child
->m_widget
),
203 if (parent
->HasFlag(wxTAB_TRAVERSAL
))
205 /* we now allow a window to get the focus as long as it
206 doesn't have any children. */
207 GTK_WIDGET_UNSET_FLAGS( parent
->m_wxwindow
, GTK_CAN_FOCUS
);
211 //-----------------------------------------------------------------------------
213 //-----------------------------------------------------------------------------
215 BEGIN_EVENT_TABLE(wxDialog
,wxPanel
)
216 EVT_BUTTON (wxID_OK
, wxDialog::OnOK
)
217 EVT_BUTTON (wxID_CANCEL
, wxDialog::OnCancel
)
218 EVT_BUTTON (wxID_APPLY
, wxDialog::OnApply
)
219 EVT_SIZE (wxDialog::OnSize
)
220 EVT_CLOSE (wxDialog::OnCloseWindow
)
223 IMPLEMENT_DYNAMIC_CLASS(wxDialog
,wxPanel
)
225 void wxDialog::Init()
229 m_modalShowing
= FALSE
;
232 wxDialog::wxDialog( wxWindow
*parent
,
233 wxWindowID id
, const wxString
&title
,
234 const wxPoint
&pos
, const wxSize
&size
,
235 long style
, const wxString
&name
)
239 Create( parent
, id
, title
, pos
, size
, style
, name
);
242 bool wxDialog::Create( wxWindow
*parent
,
243 wxWindowID id
, const wxString
&title
,
244 const wxPoint
&pos
, const wxSize
&size
,
245 long style
, const wxString
&name
)
247 wxTopLevelWindows
.Append( this );
249 m_needParent
= FALSE
;
251 PreCreation( parent
, id
, pos
, size
, style
, name
);
253 m_insertCallback
= (wxInsertChildFunction
) wxInsertChildInDialog
;
255 m_widget
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
258 gtk_window_set_wmclass( GTK_WINDOW(m_widget
), name
.mb_str(), name
.mb_str() );
260 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
262 gtk_signal_connect( GTK_OBJECT(m_widget
), "delete_event",
263 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback
), (gpointer
)this );
265 m_wxwindow
= gtk_myfixed_new();
266 gtk_widget_show( m_wxwindow
);
267 GTK_WIDGET_UNSET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
269 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
273 if (m_parent
) m_parent
->AddChild( this );
277 /* we cannot set MWM hints before the widget has
278 been realized, so we do this directly after realization */
279 gtk_signal_connect( GTK_OBJECT(m_widget
), "realize",
280 GTK_SIGNAL_FUNC(gtk_dialog_realized_callback
), (gpointer
) this );
282 /* we set the position of the window after the map event. setting it
283 before has no effect (with KWM) */
284 gtk_signal_connect( GTK_OBJECT(m_widget
), "map",
285 GTK_SIGNAL_FUNC(gtk_dialog_map_callback
), (gpointer
) this );
287 /* the user resized the frame by dragging etc. */
288 gtk_signal_connect( GTK_OBJECT(m_widget
), "size_allocate",
289 GTK_SIGNAL_FUNC(gtk_dialog_size_callback
), (gpointer
)this );
291 gtk_signal_connect( GTK_OBJECT(m_widget
), "configure_event",
292 GTK_SIGNAL_FUNC(gtk_dialog_configure_callback
), (gpointer
)this );
297 wxDialog::~wxDialog()
299 m_isBeingDeleted
= TRUE
;
301 wxTopLevelWindows
.DeleteObject( this );
303 if (wxTheApp
->GetTopWindow() == this)
305 wxTheApp
->SetTopWindow( (wxWindow
*) NULL
);
308 if (wxTopLevelWindows
.Number() == 0)
310 wxTheApp
->ExitMainLoop();
314 void wxDialog::SetTitle( const wxString
& title
)
317 if (m_title
.IsNull()) m_title
= _T("");
318 gtk_window_set_title( GTK_WINDOW(m_widget
), m_title
.mbc_str() );
321 wxString
wxDialog::GetTitle() const
323 return (wxString
&)m_title
;
326 void wxDialog::OnApply( wxCommandEvent
&WXUNUSED(event
) )
328 if (Validate()) TransferDataFromWindow();
331 void wxDialog::OnCancel( wxCommandEvent
&WXUNUSED(event
) )
335 EndModal(wxID_CANCEL
);
339 SetReturnCode(wxID_CANCEL
);
344 void wxDialog::OnOK( wxCommandEvent
&WXUNUSED(event
) )
346 if (Validate() && TransferDataFromWindow())
354 SetReturnCode(wxID_OK
);
360 void wxDialog::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
365 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
367 // We'll send a Cancel message by default,
368 // which may close the dialog.
369 // Check for looping if the Cancel event handler calls Close().
371 // Note that if a cancel button and handler aren't present in the dialog,
372 // nothing will happen when you close the dialog via the window manager, or
374 // We wouldn't want to destroy the dialog by default, since the dialog may have been
375 // created on the stack.
376 // However, this does mean that calling dialog->Close() won't delete the dialog
377 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
378 // sure to destroy the dialog.
379 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
381 static wxList s_closing
;
383 if (s_closing
.Member(this))
386 s_closing
.Append(this);
388 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
389 cancelEvent
.SetEventObject( this );
390 GetEventHandler()->ProcessEvent(cancelEvent
);
391 s_closing
.DeleteObject(this);
394 bool wxDialog::Destroy()
396 if (!wxPendingDelete
.Member(this)) wxPendingDelete
.Append(this);
401 void wxDialog::OnSize( wxSizeEvent
&WXUNUSED(event
) )
403 wxASSERT_MSG( (m_widget
!= NULL
), _T("invalid dialog") );
405 #if wxUSE_CONSTRAINTS
411 #endif // wxUSE_CONSTRAINTS
413 /* no child: go out ! */
414 if (!GetChildren().First()) return;
416 /* do we have exactly one child? */
417 wxWindow
*child
= (wxWindow
*) NULL
;
418 for(wxNode
*node
= GetChildren().First(); node
; node
= node
->Next())
420 wxWindow
*win
= (wxWindow
*)node
->Data();
421 if (!wxIS_KIND_OF(win
,wxFrame
) && !wxIS_KIND_OF(win
,wxDialog
))
423 /* it's the second one: do nothing */
429 /* yes: set it's size to fill all the frame */
430 int client_x
, client_y
;
431 GetClientSize( &client_x
, &client_y
);
432 child
->SetSize( 1, 1, client_x
-2, client_y
);
436 void wxDialog::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
438 wxASSERT_MSG( (m_widget
!= NULL
), _T("invalid dialog") );
439 wxASSERT_MSG( (m_wxwindow
!= NULL
), _T("invalid dialog") );
441 if (m_resizing
) return; /* I don't like recursions */
446 int old_width
= m_width
;
447 int old_height
= m_height
;
449 if ((sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
) == 0)
451 if (x
!= -1) m_x
= x
;
452 if (y
!= -1) m_y
= y
;
453 if (width
!= -1) m_width
= width
;
454 if (height
!= -1) m_height
= height
;
464 if ((sizeFlags
& wxSIZE_AUTO_WIDTH
) == wxSIZE_AUTO_WIDTH
)
466 if (width
== -1) m_width
= 80;
469 if ((sizeFlags
& wxSIZE_AUTO_HEIGHT
) == wxSIZE_AUTO_HEIGHT
)
471 if (height
== -1) m_height
= 26;
474 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
475 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
476 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
477 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
479 if ((m_x
!= -1) || (m_y
!= -1))
481 if ((m_x
!= old_x
) || (m_y
!= old_y
))
483 /* we set the position here and when showing the dialog
484 for the first time in idle time */
485 gtk_widget_set_uposition( m_widget
, m_x
, m_y
);
489 if ((m_width
!= old_width
) || (m_height
!= old_height
))
491 /* actual resizing is deferred to GtkOnSize in idle time and
492 when showing the dialog */
499 void wxDialog::GtkOnSize( int WXUNUSED(x
), int WXUNUSED(y
), int width
, int height
)
501 // due to a bug in gtk, x,y are always 0
505 if ((m_height
== height
) && (m_width
== width
) && (m_sizeSet
)) return;
506 if (!m_wxwindow
) return;
511 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
512 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
513 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
514 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
516 /* we actually set the size of a frame here and no-where else */
517 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
521 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
522 event
.SetEventObject( this );
523 GetEventHandler()->ProcessEvent( event
);
526 void wxDialog::Centre( int direction
)
528 wxASSERT_MSG( (m_widget
!= NULL
), _T("invalid dialog") );
533 if ((direction
& wxHORIZONTAL
) == wxHORIZONTAL
) x
= (gdk_screen_width () - m_width
) / 2;
534 if ((direction
& wxVERTICAL
) == wxVERTICAL
) y
= (gdk_screen_height () - m_height
) / 2;
539 void wxDialog::OnInternalIdle()
541 if (!m_sizeSet
&& GTK_WIDGET_REALIZED(m_wxwindow
))
542 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
545 bool wxDialog::Show( bool show
)
547 if (!show
&& IsModal())
549 EndModal( wxID_CANCEL
);
552 if (show
&& !m_sizeSet
)
554 /* by calling GtkOnSize here, we don't have to call
555 either after showing the frame, which would entail
556 much ugly flicker nor from within the size_allocate
557 handler, because GTK 1.1.X forbids that. */
559 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
562 bool ret
= wxWindow::Show( show
);
564 if (show
) InitDialog();
569 bool wxDialog::IsModal() const
571 return m_modalShowing
;
574 void wxDialog::SetModal( bool WXUNUSED(flag
) )
578 m_windowStyle |= wxDIALOG_MODAL;
580 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
582 wxFAIL_MSG( _T("wxDialog:SetModal obsolete now") );
585 int wxDialog::ShowModal()
589 wxFAIL_MSG( _T("wxDialog:ShowModal called twice") );
590 return GetReturnCode();
595 m_modalShowing
= TRUE
;
597 gtk_grab_add( m_widget
);
599 gtk_grab_remove( m_widget
);
601 return GetReturnCode();
604 void wxDialog::EndModal( int retCode
)
606 SetReturnCode( retCode
);
610 wxFAIL_MSG( _T("wxDialog:EndModal called twice") );
614 m_modalShowing
= FALSE
;
621 void wxDialog::InitDialog()
623 wxWindow::InitDialog();
626 void wxDialog::SetIcon( const wxIcon
&icon
)
629 if (!icon
.Ok()) return;
631 if (!m_widget
->window
) return;
633 wxMask
*mask
= icon
.GetMask();
634 GdkBitmap
*bm
= (GdkBitmap
*) NULL
;
635 if (mask
) bm
= mask
->GetBitmap();
637 gdk_window_set_icon( m_widget
->window
, (GdkWindow
*) NULL
, icon
.GetPixmap(), bm
);