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
*WXUNUSED(event
), wxDialog
*win
)
75 wxapp_install_idle_handler();
77 if (!win
->m_hasVMT
) return FALSE
;
81 gdk_window_get_root_origin( win
->m_widget
->window
, &x
, &y
);
86 wxMoveEvent
mevent( wxPoint(win
->m_x
,win
->m_y
), win
->GetId() );
87 mevent
.SetEventObject( win
);
88 win
->GetEventHandler()->ProcessEvent( mevent
);
93 //-----------------------------------------------------------------------------
94 // "realize" from m_widget
95 //-----------------------------------------------------------------------------
97 /* we cannot MWM hints and icons before the widget has been realized,
98 so we do this directly after realization */
101 gtk_dialog_realized_callback( GtkWidget
*widget
, wxDialog
*win
)
104 wxapp_install_idle_handler();
106 /* I haven''t been able to set the position of
107 the dialog before it is shown, so I set the
108 position in "realize" and "map" */
109 gtk_widget_set_uposition( widget
, win
->m_x
, win
->m_y
);
112 if (win
->m_icon
!= wxNullIcon
)
114 wxIcon
icon( win
->m_icon
);
115 win
->m_icon
= wxNullIcon
;
116 win
->SetIcon( icon
);
122 //-----------------------------------------------------------------------------
123 // "map" from m_widget
124 //-----------------------------------------------------------------------------
127 gtk_dialog_map_callback( GtkWidget
*widget
, wxDialog
*win
)
130 wxapp_install_idle_handler();
132 /* I haven''t been able to set the position of
133 the dialog before it is shown, so I set the
134 position in "realize" and "map" */
135 gtk_widget_set_uposition( widget
, win
->m_x
, win
->m_y
);
137 /* all this is for Motif Window Manager "hints" and is supposed to be
138 recognized by other WM as well. not tested. */
139 long decor
= (long) GDK_DECOR_BORDER
;
140 long func
= (long) GDK_FUNC_MOVE
;
142 if ((win
->GetWindowStyle() & wxCAPTION
) != 0)
143 decor
|= GDK_DECOR_TITLE
;
144 if ((win
->GetWindowStyle() & wxSYSTEM_MENU
) != 0)
146 decor
|= GDK_DECOR_MENU
;
147 func
|= GDK_FUNC_CLOSE
;
149 if ((win
->GetWindowStyle() & wxMINIMIZE_BOX
) != 0)
151 func
|= GDK_FUNC_MINIMIZE
;
152 decor
|= GDK_DECOR_MINIMIZE
;
154 if ((win
->GetWindowStyle() & wxMAXIMIZE_BOX
) != 0)
156 decor
|= GDK_DECOR_MAXIMIZE
;
157 func
|= GDK_FUNC_MAXIMIZE
;
159 if ((win
->GetWindowStyle() & wxRESIZE_BORDER
) != 0)
161 func
|= GDK_FUNC_RESIZE
;
162 decor
|= GDK_DECOR_RESIZEH
;
164 gdk_window_set_decorations( win
->m_widget
->window
, (GdkWMDecoration
)decor
);
165 gdk_window_set_functions( win
->m_widget
->window
, (GdkWMFunction
)func
);
167 /* GTK's shrinking/growing policy */
168 if ((win
->GetWindowStyle() & wxRESIZE_BORDER
) == 0)
169 gtk_window_set_policy(GTK_WINDOW(win
->m_widget
), 0, 0, 1);
171 gtk_window_set_policy(GTK_WINDOW(win
->m_widget
), 1, 1, 1);
176 //-----------------------------------------------------------------------------
177 // InsertChild for wxDialog
178 //-----------------------------------------------------------------------------
180 /* Callback for wxFrame. This very strange beast has to be used because
181 * C++ has no virtual methods in a constructor. We have to emulate a
182 * virtual function here as wxWindows requires different ways to insert
183 * a child in container classes. */
185 static void wxInsertChildInDialog( wxDialog
* parent
, wxWindow
* child
)
187 gtk_myfixed_put( GTK_MYFIXED(parent
->m_wxwindow
),
188 GTK_WIDGET(child
->m_widget
),
194 if (parent
->HasFlag(wxTAB_TRAVERSAL
))
196 /* we now allow a window to get the focus as long as it
197 doesn't have any children. */
198 GTK_WIDGET_UNSET_FLAGS( parent
->m_wxwindow
, GTK_CAN_FOCUS
);
202 //-----------------------------------------------------------------------------
204 //-----------------------------------------------------------------------------
206 BEGIN_EVENT_TABLE(wxDialog
,wxPanel
)
207 EVT_BUTTON (wxID_OK
, wxDialog::OnOK
)
208 EVT_BUTTON (wxID_CANCEL
, wxDialog::OnCancel
)
209 EVT_BUTTON (wxID_APPLY
, wxDialog::OnApply
)
210 EVT_SIZE (wxDialog::OnSize
)
211 EVT_CLOSE (wxDialog::OnCloseWindow
)
214 IMPLEMENT_DYNAMIC_CLASS(wxDialog
,wxPanel
)
216 void wxDialog::Init()
220 m_modalShowing
= FALSE
;
223 wxDialog::wxDialog( wxWindow
*parent
,
224 wxWindowID id
, const wxString
&title
,
225 const wxPoint
&pos
, const wxSize
&size
,
226 long style
, const wxString
&name
)
230 Create( parent
, id
, title
, pos
, size
, style
, name
);
233 bool wxDialog::Create( wxWindow
*parent
,
234 wxWindowID id
, const wxString
&title
,
235 const wxPoint
&pos
, const wxSize
&size
,
236 long style
, const wxString
&name
)
238 wxTopLevelWindows
.Append( this );
240 m_needParent
= FALSE
;
242 PreCreation( parent
, id
, pos
, size
, style
, name
);
244 m_insertCallback
= (wxInsertChildFunction
) wxInsertChildInDialog
;
246 m_widget
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
249 gtk_window_set_wmclass( GTK_WINDOW(m_widget
), name
.mb_str(), name
.mb_str() );
251 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
253 gtk_signal_connect( GTK_OBJECT(m_widget
), "delete_event",
254 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback
), (gpointer
)this );
256 m_wxwindow
= gtk_myfixed_new();
257 gtk_widget_show( m_wxwindow
);
258 GTK_WIDGET_UNSET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
260 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
264 if (m_parent
) m_parent
->AddChild( this );
268 /* we cannot set MWM hints before the widget has
269 been realized, so we do this directly after realization */
270 gtk_signal_connect( GTK_OBJECT(m_widget
), "realize",
271 GTK_SIGNAL_FUNC(gtk_dialog_realized_callback
), (gpointer
) this );
273 /* we set the position of the window after the map event. setting it
274 before has no effect (with KWM) */
275 gtk_signal_connect( GTK_OBJECT(m_widget
), "map",
276 GTK_SIGNAL_FUNC(gtk_dialog_map_callback
), (gpointer
) this );
278 /* the user resized the frame by dragging etc. */
279 gtk_signal_connect( GTK_OBJECT(m_widget
), "size_allocate",
280 GTK_SIGNAL_FUNC(gtk_dialog_size_callback
), (gpointer
)this );
282 gtk_signal_connect( GTK_OBJECT(m_widget
), "configure_event",
283 GTK_SIGNAL_FUNC(gtk_dialog_configure_callback
), (gpointer
)this );
288 wxDialog::~wxDialog()
290 m_isBeingDeleted
= TRUE
;
292 wxTopLevelWindows
.DeleteObject( this );
294 if (wxTheApp
->GetTopWindow() == this)
296 wxTheApp
->SetTopWindow( (wxWindow
*) NULL
);
299 if (wxTopLevelWindows
.Number() == 0)
301 wxTheApp
->ExitMainLoop();
305 void wxDialog::SetTitle( const wxString
& title
)
308 if (m_title
.IsNull()) m_title
= _T("");
309 gtk_window_set_title( GTK_WINDOW(m_widget
), m_title
.mbc_str() );
312 wxString
wxDialog::GetTitle() const
314 return (wxString
&)m_title
;
317 void wxDialog::OnApply( wxCommandEvent
&WXUNUSED(event
) )
319 if (Validate()) TransferDataFromWindow();
322 void wxDialog::OnCancel( wxCommandEvent
&WXUNUSED(event
) )
326 EndModal(wxID_CANCEL
);
330 SetReturnCode(wxID_CANCEL
);
335 void wxDialog::OnOK( wxCommandEvent
&WXUNUSED(event
) )
337 if (Validate() && TransferDataFromWindow())
345 SetReturnCode(wxID_OK
);
351 void wxDialog::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
356 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
358 // We'll send a Cancel message by default,
359 // which may close the dialog.
360 // Check for looping if the Cancel event handler calls Close().
362 // Note that if a cancel button and handler aren't present in the dialog,
363 // nothing will happen when you close the dialog via the window manager, or
365 // We wouldn't want to destroy the dialog by default, since the dialog may have been
366 // created on the stack.
367 // However, this does mean that calling dialog->Close() won't delete the dialog
368 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
369 // sure to destroy the dialog.
370 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
372 static wxList s_closing
;
374 if (s_closing
.Member(this))
377 s_closing
.Append(this);
379 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
380 cancelEvent
.SetEventObject( this );
381 GetEventHandler()->ProcessEvent(cancelEvent
);
382 s_closing
.DeleteObject(this);
385 bool wxDialog::Destroy()
387 if (!wxPendingDelete
.Member(this)) wxPendingDelete
.Append(this);
392 void wxDialog::OnSize( wxSizeEvent
&WXUNUSED(event
) )
394 wxASSERT_MSG( (m_widget
!= NULL
), _T("invalid dialog") );
402 /* no child: go out ! */
403 if (!GetChildren().First()) return;
405 /* do we have exactly one child? */
406 wxWindow
*child
= (wxWindow
*) NULL
;
407 for(wxNode
*node
= GetChildren().First(); node
; node
= node
->Next())
409 wxWindow
*win
= (wxWindow
*)node
->Data();
410 if (!wxIS_KIND_OF(win
,wxFrame
) && !wxIS_KIND_OF(win
,wxDialog
))
412 /* it's the second one: do nothing */
418 /* yes: set it's size to fill all the frame */
419 int client_x
, client_y
;
420 GetClientSize( &client_x
, &client_y
);
421 child
->SetSize( 1, 1, client_x
-2, client_y
);
425 void wxDialog::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
427 wxASSERT_MSG( (m_widget
!= NULL
), _T("invalid dialog") );
428 wxASSERT_MSG( (m_wxwindow
!= NULL
), _T("invalid dialog") );
430 if (m_resizing
) return; /* I don't like recursions */
435 int old_width
= m_width
;
436 int old_height
= m_height
;
438 if ((sizeFlags
& wxSIZE_USE_EXISTING
) == wxSIZE_USE_EXISTING
)
440 if (x
!= -1) m_x
= x
;
441 if (y
!= -1) m_y
= y
;
442 if (width
!= -1) m_width
= width
;
443 if (height
!= -1) m_height
= height
;
453 if ((sizeFlags
& wxSIZE_AUTO_WIDTH
) == wxSIZE_AUTO_WIDTH
)
455 if (width
== -1) m_width
= 80;
458 if ((sizeFlags
& wxSIZE_AUTO_HEIGHT
) == wxSIZE_AUTO_HEIGHT
)
460 if (height
== -1) m_height
= 26;
463 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
464 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
465 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
466 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
468 if ((m_x
!= -1) || (m_y
!= -1))
470 if ((m_x
!= old_x
) || (m_y
!= old_y
))
472 /* we set the position here and when showing the dialog
473 for the first time in idle time */
474 gtk_widget_set_uposition( m_widget
, m_x
, m_y
);
478 if ((m_width
!= old_width
) || (m_height
!= old_height
))
480 /* actual resizing is deferred to GtkOnSize in idle time and
481 when showing the dialog */
488 void wxDialog::GtkOnSize( int WXUNUSED(x
), int WXUNUSED(y
), int width
, int height
)
490 // due to a bug in gtk, x,y are always 0
494 if ((m_height
== height
) && (m_width
== width
) && (m_sizeSet
)) return;
495 if (!m_wxwindow
) return;
500 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
501 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
502 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
503 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
505 /* we actually set the size of a frame here and no-where else */
506 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
510 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
511 event
.SetEventObject( this );
512 GetEventHandler()->ProcessEvent( event
);
515 void wxDialog::Centre( int direction
)
517 wxASSERT_MSG( (m_widget
!= NULL
), _T("invalid dialog") );
522 if ((direction
& wxHORIZONTAL
) == wxHORIZONTAL
) x
= (gdk_screen_width () - m_width
) / 2;
523 if ((direction
& wxVERTICAL
) == wxVERTICAL
) y
= (gdk_screen_height () - m_height
) / 2;
528 void wxDialog::OnInternalIdle()
530 if (!m_sizeSet
&& GTK_WIDGET_REALIZED(m_wxwindow
))
531 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
534 bool wxDialog::Show( bool show
)
536 if (!show
&& IsModal())
538 EndModal( wxID_CANCEL
);
541 if (show
&& !m_sizeSet
)
543 /* by calling GtkOnSize here, we don't have to call
544 either after showing the frame, which would entail
545 much ugly flicker nor from within the size_allocate
546 handler, because GTK 1.1.X forbids that. */
548 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
551 bool ret
= wxWindow::Show( show
);
553 if (show
) InitDialog();
558 bool wxDialog::IsModal() const
560 return m_modalShowing
;
563 void wxDialog::SetModal( bool WXUNUSED(flag
) )
567 m_windowStyle |= wxDIALOG_MODAL;
569 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
571 wxFAIL_MSG( _T("wxDialog:SetModal obsolete now") );
574 int wxDialog::ShowModal()
578 wxFAIL_MSG( _T("wxDialog:ShowModal called twice") );
579 return GetReturnCode();
584 m_modalShowing
= TRUE
;
586 gtk_grab_add( m_widget
);
588 gtk_grab_remove( m_widget
);
590 return GetReturnCode();
593 void wxDialog::EndModal( int retCode
)
595 SetReturnCode( retCode
);
599 wxFAIL_MSG( _T("wxDialog:EndModal called twice") );
603 m_modalShowing
= FALSE
;
610 void wxDialog::InitDialog()
612 wxWindow::InitDialog();
615 void wxDialog::SetIcon( const wxIcon
&icon
)
618 if (!icon
.Ok()) return;
620 if (!m_widget
->window
) return;
622 wxMask
*mask
= icon
.GetMask();
623 GdkBitmap
*bm
= (GdkBitmap
*) NULL
;
624 if (mask
) bm
= mask
->GetBitmap();
626 gdk_window_set_icon( m_widget
->window
, (GdkWindow
*) NULL
, icon
.GetPixmap(), bm
);