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 //-----------------------------------------------------------------------------
72 static gint
gtk_dialog_configure_callback( GtkWidget
*WXUNUSED(widget
), GdkEventConfigure
*event
, wxDialog
*win
)
75 wxapp_install_idle_handler();
77 if (!win
->m_hasVMT
) return FALSE
;
82 wxMoveEvent
mevent( wxPoint(win
->m_x
,win
->m_y
), win
->GetId() );
83 mevent
.SetEventObject( win
);
84 win
->GetEventHandler()->ProcessEvent( mevent
);
89 //-----------------------------------------------------------------------------
90 // "realize" from m_widget
91 //-----------------------------------------------------------------------------
93 /* we cannot MWM hints and icons before the widget has been realized,
94 so we do this directly after realization */
97 gtk_dialog_realized_callback( GtkWidget
*widget
, wxDialog
*win
)
100 wxapp_install_idle_handler();
102 /* I haven''t been able to set the position of
103 the dialog before it is shown, so I set the
104 position in "realize" and "map" */
105 gtk_widget_set_uposition( widget
, win
->m_x
, win
->m_y
);
108 if (win
->m_icon
!= wxNullIcon
)
110 wxIcon
icon( win
->m_icon
);
111 win
->m_icon
= wxNullIcon
;
112 win
->SetIcon( icon
);
118 //-----------------------------------------------------------------------------
119 // "map" from m_widget
120 //-----------------------------------------------------------------------------
123 gtk_dialog_map_callback( GtkWidget
*widget
, wxDialog
*win
)
126 wxapp_install_idle_handler();
128 /* I haven''t been able to set the position of
129 the dialog before it is shown, so I set the
130 position in "realize" and "map" */
131 gtk_widget_set_uposition( widget
, win
->m_x
, win
->m_y
);
133 /* all this is for Motif Window Manager "hints" and is supposed to be
134 recognized by other WM as well. not tested. */
135 long decor
= (long) GDK_DECOR_BORDER
;
136 long func
= (long) GDK_FUNC_MOVE
;
138 if ((win
->GetWindowStyle() & wxCAPTION
) != 0)
139 decor
|= GDK_DECOR_TITLE
;
140 if ((win
->GetWindowStyle() & wxSYSTEM_MENU
) != 0)
142 decor
|= GDK_DECOR_MENU
;
143 func
|= GDK_FUNC_CLOSE
;
145 if ((win
->GetWindowStyle() & wxMINIMIZE_BOX
) != 0)
147 func
|= GDK_FUNC_MINIMIZE
;
148 decor
|= GDK_DECOR_MINIMIZE
;
150 if ((win
->GetWindowStyle() & wxMAXIMIZE_BOX
) != 0)
152 decor
|= GDK_DECOR_MAXIMIZE
;
153 func
|= GDK_FUNC_MAXIMIZE
;
155 if ((win
->GetWindowStyle() & wxRESIZE_BORDER
) != 0)
157 func
|= GDK_FUNC_RESIZE
;
158 decor
|= GDK_DECOR_RESIZEH
;
160 gdk_window_set_decorations( win
->m_widget
->window
, (GdkWMDecoration
)decor
);
161 gdk_window_set_functions( win
->m_widget
->window
, (GdkWMFunction
)func
);
163 /* GTK's shrinking/growing policy */
164 if ((win
->GetWindowStyle() & wxRESIZE_BORDER
) == 0)
165 gtk_window_set_policy(GTK_WINDOW(win
->m_widget
), 0, 0, 1);
167 gtk_window_set_policy(GTK_WINDOW(win
->m_widget
), 1, 1, 1);
172 //-----------------------------------------------------------------------------
173 // InsertChild for wxDialog
174 //-----------------------------------------------------------------------------
176 /* Callback for wxFrame. This very strange beast has to be used because
177 * C++ has no virtual methods in a constructor. We have to emulate a
178 * virtual function here as wxWindows requires different ways to insert
179 * a child in container classes. */
181 static void wxInsertChildInDialog( wxDialog
* parent
, wxWindow
* child
)
183 gtk_myfixed_put( GTK_MYFIXED(parent
->m_wxwindow
),
184 GTK_WIDGET(child
->m_widget
),
190 if (parent
->HasFlag(wxTAB_TRAVERSAL
))
192 /* we now allow a window to get the focus as long as it
193 doesn't have any children. */
194 GTK_WIDGET_UNSET_FLAGS( parent
->m_wxwindow
, GTK_CAN_FOCUS
);
198 //-----------------------------------------------------------------------------
200 //-----------------------------------------------------------------------------
202 BEGIN_EVENT_TABLE(wxDialog
,wxPanel
)
203 EVT_BUTTON (wxID_OK
, wxDialog::OnOK
)
204 EVT_BUTTON (wxID_CANCEL
, wxDialog::OnCancel
)
205 EVT_BUTTON (wxID_APPLY
, wxDialog::OnApply
)
206 EVT_SIZE (wxDialog::OnSize
)
207 EVT_CLOSE (wxDialog::OnCloseWindow
)
210 IMPLEMENT_DYNAMIC_CLASS(wxDialog
,wxPanel
)
212 void wxDialog::Init()
216 m_modalShowing
= FALSE
;
219 wxDialog::wxDialog( wxWindow
*parent
,
220 wxWindowID id
, const wxString
&title
,
221 const wxPoint
&pos
, const wxSize
&size
,
222 long style
, const wxString
&name
)
226 Create( parent
, id
, title
, pos
, size
, style
, name
);
229 bool wxDialog::Create( wxWindow
*parent
,
230 wxWindowID id
, const wxString
&title
,
231 const wxPoint
&pos
, const wxSize
&size
,
232 long style
, const wxString
&name
)
234 wxTopLevelWindows
.Append( this );
236 m_needParent
= FALSE
;
238 PreCreation( parent
, id
, pos
, size
, style
, name
);
240 m_insertCallback
= (wxInsertChildFunction
) wxInsertChildInDialog
;
242 m_widget
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
245 gtk_window_set_wmclass( GTK_WINDOW(m_widget
), name
.mb_str(), name
.mb_str() );
247 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
249 gtk_signal_connect( GTK_OBJECT(m_widget
), "delete_event",
250 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback
), (gpointer
)this );
252 m_wxwindow
= gtk_myfixed_new();
253 gtk_widget_show( m_wxwindow
);
254 GTK_WIDGET_UNSET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
256 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
260 if (m_parent
) m_parent
->AddChild( this );
264 /* we cannot set MWM hints before the widget has
265 been realized, so we do this directly after realization */
266 gtk_signal_connect( GTK_OBJECT(m_widget
), "realize",
267 GTK_SIGNAL_FUNC(gtk_dialog_realized_callback
), (gpointer
) this );
269 /* we set the position of the window after the map event. setting it
270 before has no effect (with KWM) */
271 gtk_signal_connect( GTK_OBJECT(m_widget
), "map",
272 GTK_SIGNAL_FUNC(gtk_dialog_map_callback
), (gpointer
) this );
274 /* the user resized the frame by dragging etc. */
275 gtk_signal_connect( GTK_OBJECT(m_widget
), "size_allocate",
276 GTK_SIGNAL_FUNC(gtk_dialog_size_callback
), (gpointer
)this );
278 gtk_signal_connect( GTK_OBJECT(m_widget
), "configure_event",
279 GTK_SIGNAL_FUNC(gtk_dialog_configure_callback
), (gpointer
)this );
284 wxDialog::~wxDialog()
286 m_isBeingDeleted
= TRUE
;
288 wxTopLevelWindows
.DeleteObject( this );
290 if (wxTheApp
->GetTopWindow() == this)
292 wxTheApp
->SetTopWindow( (wxWindow
*) NULL
);
295 if (wxTopLevelWindows
.Number() == 0)
297 wxTheApp
->ExitMainLoop();
301 void wxDialog::SetTitle( const wxString
& title
)
304 if (m_title
.IsNull()) m_title
= _T("");
305 gtk_window_set_title( GTK_WINDOW(m_widget
), m_title
.mbc_str() );
308 wxString
wxDialog::GetTitle() const
310 return (wxString
&)m_title
;
313 void wxDialog::OnApply( wxCommandEvent
&WXUNUSED(event
) )
315 if (Validate()) TransferDataFromWindow();
318 void wxDialog::OnCancel( wxCommandEvent
&WXUNUSED(event
) )
322 EndModal(wxID_CANCEL
);
326 SetReturnCode(wxID_CANCEL
);
331 void wxDialog::OnOK( wxCommandEvent
&WXUNUSED(event
) )
333 if (Validate() && TransferDataFromWindow())
341 SetReturnCode(wxID_OK
);
347 void wxDialog::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
352 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
354 // We'll send a Cancel message by default,
355 // which may close the dialog.
356 // Check for looping if the Cancel event handler calls Close().
358 // Note that if a cancel button and handler aren't present in the dialog,
359 // nothing will happen when you close the dialog via the window manager, or
361 // We wouldn't want to destroy the dialog by default, since the dialog may have been
362 // created on the stack.
363 // However, this does mean that calling dialog->Close() won't delete the dialog
364 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
365 // sure to destroy the dialog.
366 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
368 static wxList s_closing
;
370 if (s_closing
.Member(this))
373 s_closing
.Append(this);
375 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
376 cancelEvent
.SetEventObject( this );
377 GetEventHandler()->ProcessEvent(cancelEvent
);
378 s_closing
.DeleteObject(this);
381 bool wxDialog::Destroy()
383 if (!wxPendingDelete
.Member(this)) wxPendingDelete
.Append(this);
388 void wxDialog::OnSize( wxSizeEvent
&WXUNUSED(event
) )
390 wxASSERT_MSG( (m_widget
!= NULL
), _T("invalid dialog") );
398 /* no child: go out ! */
399 if (!GetChildren().First()) return;
401 /* do we have exactly one child? */
402 wxWindow
*child
= (wxWindow
*) NULL
;
403 for(wxNode
*node
= GetChildren().First(); node
; node
= node
->Next())
405 wxWindow
*win
= (wxWindow
*)node
->Data();
406 if (!wxIS_KIND_OF(win
,wxFrame
) && !wxIS_KIND_OF(win
,wxDialog
))
408 /* it's the second one: do nothing */
414 /* yes: set it's size to fill all the frame */
415 int client_x
, client_y
;
416 GetClientSize( &client_x
, &client_y
);
417 child
->SetSize( 1, 1, client_x
-2, client_y
);
421 void wxDialog::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
423 wxASSERT_MSG( (m_widget
!= NULL
), _T("invalid dialog") );
424 wxASSERT_MSG( (m_wxwindow
!= NULL
), _T("invalid dialog") );
426 if (m_resizing
) return; /* I don't like recursions */
431 int old_width
= m_width
;
432 int old_height
= m_height
;
434 if ((sizeFlags
& wxSIZE_USE_EXISTING
) == wxSIZE_USE_EXISTING
)
436 if (x
!= -1) m_x
= x
;
437 if (y
!= -1) m_y
= y
;
438 if (width
!= -1) m_width
= width
;
439 if (height
!= -1) m_height
= height
;
449 if ((sizeFlags
& wxSIZE_AUTO_WIDTH
) == wxSIZE_AUTO_WIDTH
)
451 if (width
== -1) m_width
= 80;
454 if ((sizeFlags
& wxSIZE_AUTO_HEIGHT
) == wxSIZE_AUTO_HEIGHT
)
456 if (height
== -1) m_height
= 26;
459 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
460 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
461 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
462 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
464 if ((m_x
!= -1) || (m_y
!= -1))
466 if ((m_x
!= old_x
) || (m_y
!= old_y
))
468 /* we set the position here and when showing the dialog
469 for the first time in idle time */
470 gtk_widget_set_uposition( m_widget
, m_x
, m_y
);
474 if ((m_width
!= old_width
) || (m_height
!= old_height
))
476 /* actual resizing is deferred to GtkOnSize in idle time and
477 when showing the dialog */
484 void wxDialog::GtkOnSize( int WXUNUSED(x
), int WXUNUSED(y
), int width
, int height
)
486 // due to a bug in gtk, x,y are always 0
490 if ((m_height
== height
) && (m_width
== width
) && (m_sizeSet
)) return;
491 if (!m_wxwindow
) return;
496 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
497 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
498 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
499 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
501 /* we actually set the size of a frame here and no-where else */
502 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
506 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
507 event
.SetEventObject( this );
508 GetEventHandler()->ProcessEvent( event
);
511 void wxDialog::Centre( int direction
)
513 wxASSERT_MSG( (m_widget
!= NULL
), _T("invalid dialog") );
518 if ((direction
& wxHORIZONTAL
) == wxHORIZONTAL
) x
= (gdk_screen_width () - m_width
) / 2;
519 if ((direction
& wxVERTICAL
) == wxVERTICAL
) y
= (gdk_screen_height () - m_height
) / 2;
524 void wxDialog::OnInternalIdle()
526 if (!m_sizeSet
&& GTK_WIDGET_REALIZED(m_wxwindow
))
527 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
530 bool wxDialog::Show( bool show
)
532 if (!show
&& IsModal())
534 EndModal( wxID_CANCEL
);
537 if (show
&& !m_sizeSet
)
539 /* by calling GtkOnSize here, we don't have to call
540 either after showing the frame, which would entail
541 much ugly flicker nor from within the size_allocate
542 handler, because GTK 1.1.X forbids that. */
544 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
547 bool ret
= wxWindow::Show( show
);
549 if (show
) InitDialog();
554 bool wxDialog::IsModal() const
556 return m_modalShowing
;
559 void wxDialog::SetModal( bool WXUNUSED(flag
) )
563 m_windowStyle |= wxDIALOG_MODAL;
565 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
567 wxFAIL_MSG( _T("wxDialog:SetModal obsolete now") );
570 int wxDialog::ShowModal()
574 wxFAIL_MSG( _T("wxDialog:ShowModal called twice") );
575 return GetReturnCode();
580 m_modalShowing
= TRUE
;
582 gtk_grab_add( m_widget
);
584 gtk_grab_remove( m_widget
);
586 return GetReturnCode();
589 void wxDialog::EndModal( int retCode
)
591 SetReturnCode( retCode
);
595 wxFAIL_MSG( _T("wxDialog:EndModal called twice") );
599 m_modalShowing
= FALSE
;
606 void wxDialog::InitDialog()
608 wxWindow::InitDialog();
611 void wxDialog::SetIcon( const wxIcon
&icon
)
614 if (!icon
.Ok()) return;
616 if (!m_widget
->window
) return;
618 wxMask
*mask
= icon
.GetMask();
619 GdkBitmap
*bm
= (GdkBitmap
*) NULL
;
620 if (mask
) bm
= mask
->GetBitmap();
622 gdk_window_set_icon( m_widget
->window
, (GdkWindow
*) NULL
, icon
.GetPixmap(), bm
);