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"
21 #include "wx/cursor.h"
24 #include "gdk/gdkprivate.h"
28 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
32 extern void wxapp_install_idle_handler();
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
39 extern wxList wxPendingDelete
;
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 bool gtk_dialog_delete_callback( GtkWidget
*WXUNUSED(widget
), GdkEvent
*WXUNUSED(event
), wxDialog
*win
)
48 wxapp_install_idle_handler();
55 //-----------------------------------------------------------------------------
57 //-----------------------------------------------------------------------------
59 static void gtk_dialog_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxDialog
*win
)
62 wxapp_install_idle_handler();
64 if (!win
->m_hasVMT
) return;
66 if ((win
->m_width
!= alloc
->width
) || (win
->m_height
!= alloc
->height
))
68 win
->m_width
= alloc
->width
;
69 win
->m_height
= alloc
->height
;
74 //-----------------------------------------------------------------------------
76 //-----------------------------------------------------------------------------
79 #if (GTK_MINOR_VERSON > 0)
80 gtk_dialog_configure_callback( GtkWidget
*WXUNUSED(widget
), GdkEventConfigure
*WXUNUSED(event
), wxDialog
*win
)
82 gtk_dialog_configure_callback( GtkWidget
*WXUNUSED(widget
), GdkEventConfigure
*event
, wxDialog
*win
)
86 wxapp_install_idle_handler();
88 if (!win
->m_hasVMT
) return FALSE
;
90 #if (GTK_MINOR_VERSON > 0)
93 gdk_window_get_root_origin( win
->m_widget
->window
, &x
, &y
);
101 wxMoveEvent
mevent( wxPoint(win
->m_x
,win
->m_y
), win
->GetId() );
102 mevent
.SetEventObject( win
);
103 win
->GetEventHandler()->ProcessEvent( mevent
);
108 //-----------------------------------------------------------------------------
109 // "realize" from m_widget
110 //-----------------------------------------------------------------------------
112 /* we cannot MWM hints and icons before the widget has been realized,
113 so we do this directly after realization */
116 gtk_dialog_realized_callback( GtkWidget
*widget
, wxDialog
*win
)
119 wxapp_install_idle_handler();
121 /* I haven''t been able to set the position of
122 the dialog before it is shown, so I set the
123 position in "realize" and "map" */
124 gtk_widget_set_uposition( widget
, win
->m_x
, win
->m_y
);
127 gint flag
= GDK_HINT_POS
;
128 if ((win
->GetMinWidth() != -1) || (win
->GetMinHeight() != -1)) flag
|= GDK_HINT_MIN_SIZE
;
129 if ((win
->GetMaxWidth() != -1) || (win
->GetMaxHeight() != -1)) flag
|= GDK_HINT_MAX_SIZE
;
132 gdk_window_set_hints( win
->m_widget
->window
,
134 win
->GetMinWidth(), win
->GetMinHeight(),
135 win
->GetMaxWidth(), win
->GetMaxHeight(),
140 if (win
->m_icon
!= wxNullIcon
)
142 wxIcon
icon( win
->m_icon
);
143 win
->m_icon
= wxNullIcon
;
144 win
->SetIcon( icon
);
150 //-----------------------------------------------------------------------------
151 // "map" from m_widget
152 //-----------------------------------------------------------------------------
155 gtk_dialog_map_callback( GtkWidget
*widget
, wxDialog
*win
)
158 wxapp_install_idle_handler();
160 /* I haven''t been able to set the position of
161 the dialog before it is shown, so I set the
162 position in "realize" and "map" */
163 gtk_widget_set_uposition( widget
, win
->m_x
, win
->m_y
);
165 /* all this is for Motif Window Manager "hints" and is supposed to be
166 recognized by other WM as well. not tested. */
167 long decor
= (long) GDK_DECOR_BORDER
;
168 long func
= (long) GDK_FUNC_MOVE
;
170 if ((win
->GetWindowStyle() & wxCAPTION
) != 0)
171 decor
|= GDK_DECOR_TITLE
;
172 if ((win
->GetWindowStyle() & wxSYSTEM_MENU
) != 0)
174 decor
|= GDK_DECOR_MENU
;
175 func
|= GDK_FUNC_CLOSE
;
177 if ((win
->GetWindowStyle() & wxMINIMIZE_BOX
) != 0)
179 func
|= GDK_FUNC_MINIMIZE
;
180 decor
|= GDK_DECOR_MINIMIZE
;
182 if ((win
->GetWindowStyle() & wxMAXIMIZE_BOX
) != 0)
184 decor
|= GDK_DECOR_MAXIMIZE
;
185 func
|= GDK_FUNC_MAXIMIZE
;
187 if ((win
->GetWindowStyle() & wxRESIZE_BORDER
) != 0)
189 func
|= GDK_FUNC_RESIZE
;
190 decor
|= GDK_DECOR_RESIZEH
;
192 gdk_window_set_decorations( win
->m_widget
->window
, (GdkWMDecoration
)decor
);
193 gdk_window_set_functions( win
->m_widget
->window
, (GdkWMFunction
)func
);
195 /* GTK's shrinking/growing policy */
196 if ((win
->GetWindowStyle() & wxRESIZE_BORDER
) == 0)
197 gtk_window_set_policy(GTK_WINDOW(win
->m_widget
), 0, 0, 1);
199 gtk_window_set_policy(GTK_WINDOW(win
->m_widget
), 1, 1, 1);
204 //-----------------------------------------------------------------------------
205 // InsertChild for wxDialog
206 //-----------------------------------------------------------------------------
208 /* Callback for wxFrame. This very strange beast has to be used because
209 * C++ has no virtual methods in a constructor. We have to emulate a
210 * virtual function here as wxWindows requires different ways to insert
211 * a child in container classes. */
213 static void wxInsertChildInDialog( wxDialog
* parent
, wxWindow
* child
)
215 gtk_myfixed_put( GTK_MYFIXED(parent
->m_wxwindow
),
216 GTK_WIDGET(child
->m_widget
),
222 if (parent
->HasFlag(wxTAB_TRAVERSAL
))
224 /* we now allow a window to get the focus as long as it
225 doesn't have any children. */
226 GTK_WIDGET_UNSET_FLAGS( parent
->m_wxwindow
, GTK_CAN_FOCUS
);
230 //-----------------------------------------------------------------------------
232 //-----------------------------------------------------------------------------
234 BEGIN_EVENT_TABLE(wxDialog
,wxPanel
)
235 EVT_BUTTON (wxID_OK
, wxDialog::OnOK
)
236 EVT_BUTTON (wxID_CANCEL
, wxDialog::OnCancel
)
237 EVT_BUTTON (wxID_APPLY
, wxDialog::OnApply
)
238 EVT_SIZE (wxDialog::OnSize
)
239 EVT_CLOSE (wxDialog::OnCloseWindow
)
242 IMPLEMENT_DYNAMIC_CLASS(wxDialog
,wxPanel
)
244 void wxDialog::Init()
248 m_modalShowing
= FALSE
;
251 wxDialog::wxDialog( wxWindow
*parent
,
252 wxWindowID id
, const wxString
&title
,
253 const wxPoint
&pos
, const wxSize
&size
,
254 long style
, const wxString
&name
)
258 Create( parent
, id
, title
, pos
, size
, style
, name
);
261 bool wxDialog::Create( wxWindow
*parent
,
262 wxWindowID id
, const wxString
&title
,
263 const wxPoint
&pos
, const wxSize
&size
,
264 long style
, const wxString
&name
)
266 wxTopLevelWindows
.Append( this );
268 m_needParent
= FALSE
;
270 if (!PreCreation( parent
, pos
, size
) ||
271 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
273 wxFAIL_MSG( wxT("wxDialog creation failed") );
277 m_insertCallback
= (wxInsertChildFunction
) wxInsertChildInDialog
;
279 m_widget
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
282 gtk_window_set_wmclass( GTK_WINDOW(m_widget
), name
.mb_str(), name
.mb_str() );
284 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
286 gtk_signal_connect( GTK_OBJECT(m_widget
), "delete_event",
287 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback
), (gpointer
)this );
289 m_wxwindow
= gtk_myfixed_new();
290 gtk_widget_show( m_wxwindow
);
291 GTK_WIDGET_UNSET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
293 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
297 if (m_parent
) m_parent
->AddChild( this );
301 /* we cannot set MWM hints before the widget has
302 been realized, so we do this directly after realization */
303 gtk_signal_connect( GTK_OBJECT(m_widget
), "realize",
304 GTK_SIGNAL_FUNC(gtk_dialog_realized_callback
), (gpointer
) this );
306 /* we set the position of the window after the map event. setting it
307 before has no effect (with KWM) */
308 gtk_signal_connect( GTK_OBJECT(m_widget
), "map",
309 GTK_SIGNAL_FUNC(gtk_dialog_map_callback
), (gpointer
) this );
311 /* the user resized the frame by dragging etc. */
312 gtk_signal_connect( GTK_OBJECT(m_widget
), "size_allocate",
313 GTK_SIGNAL_FUNC(gtk_dialog_size_callback
), (gpointer
)this );
315 gtk_signal_connect( GTK_OBJECT(m_widget
), "configure_event",
316 GTK_SIGNAL_FUNC(gtk_dialog_configure_callback
), (gpointer
)this );
321 wxDialog::~wxDialog()
323 m_isBeingDeleted
= TRUE
;
325 wxTopLevelWindows
.DeleteObject( this );
327 if (wxTheApp
->GetTopWindow() == this)
329 wxTheApp
->SetTopWindow( (wxWindow
*) NULL
);
332 if (wxTopLevelWindows
.Number() == 0)
334 wxTheApp
->ExitMainLoop();
338 void wxDialog::SetTitle( const wxString
& title
)
341 if (m_title
.IsNull()) m_title
= wxT("");
342 gtk_window_set_title( GTK_WINDOW(m_widget
), m_title
.mbc_str() );
345 wxString
wxDialog::GetTitle() const
347 return (wxString
&)m_title
;
350 void wxDialog::OnApply( wxCommandEvent
&WXUNUSED(event
) )
352 if (Validate()) TransferDataFromWindow();
355 void wxDialog::OnCancel( wxCommandEvent
&WXUNUSED(event
) )
359 EndModal(wxID_CANCEL
);
363 SetReturnCode(wxID_CANCEL
);
368 void wxDialog::OnOK( wxCommandEvent
&WXUNUSED(event
) )
370 if (Validate() && TransferDataFromWindow())
378 SetReturnCode(wxID_OK
);
384 void wxDialog::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
389 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
391 // We'll send a Cancel message by default,
392 // which may close the dialog.
393 // Check for looping if the Cancel event handler calls Close().
395 // Note that if a cancel button and handler aren't present in the dialog,
396 // nothing will happen when you close the dialog via the window manager, or
398 // We wouldn't want to destroy the dialog by default, since the dialog may have been
399 // created on the stack.
400 // However, this does mean that calling dialog->Close() won't delete the dialog
401 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
402 // sure to destroy the dialog.
403 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
405 static wxList s_closing
;
407 if (s_closing
.Member(this))
410 s_closing
.Append(this);
412 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
413 cancelEvent
.SetEventObject( this );
414 GetEventHandler()->ProcessEvent(cancelEvent
);
415 s_closing
.DeleteObject(this);
418 bool wxDialog::Destroy()
420 if (!wxPendingDelete
.Member(this)) wxPendingDelete
.Append(this);
425 void wxDialog::OnSize( wxSizeEvent
&WXUNUSED(event
) )
427 wxASSERT_MSG( (m_widget
!= NULL
), wxT("invalid dialog") );
429 #if wxUSE_CONSTRAINTS
435 #endif // wxUSE_CONSTRAINTS
437 /* no child: go out ! */
438 if (!GetChildren().First()) return;
440 /* do we have exactly one child? */
441 wxWindow
*child
= (wxWindow
*) NULL
;
442 for(wxNode
*node
= GetChildren().First(); node
; node
= node
->Next())
444 wxWindow
*win
= (wxWindow
*)node
->Data();
445 if (!wxIS_KIND_OF(win
,wxFrame
) && !wxIS_KIND_OF(win
,wxDialog
))
447 /* it's the second one: do nothing */
453 /* yes: set it's size to fill all the frame */
454 int client_x
, client_y
;
455 GetClientSize( &client_x
, &client_y
);
456 child
->SetSize( 1, 1, client_x
-2, client_y
);
460 void wxDialog::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
462 wxASSERT_MSG( (m_widget
!= NULL
), wxT("invalid dialog") );
463 wxASSERT_MSG( (m_wxwindow
!= NULL
), wxT("invalid dialog") );
465 if (m_resizing
) return; /* I don't like recursions */
470 int old_width
= m_width
;
471 int old_height
= m_height
;
473 if ((sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
) == 0)
475 if (x
!= -1) m_x
= x
;
476 if (y
!= -1) m_y
= y
;
477 if (width
!= -1) m_width
= width
;
478 if (height
!= -1) m_height
= height
;
488 if ((sizeFlags
& wxSIZE_AUTO_WIDTH
) == wxSIZE_AUTO_WIDTH
)
490 if (width
== -1) m_width
= 80;
493 if ((sizeFlags
& wxSIZE_AUTO_HEIGHT
) == wxSIZE_AUTO_HEIGHT
)
495 if (height
== -1) m_height
= 26;
498 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
499 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
500 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
501 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
503 if ((m_x
!= -1) || (m_y
!= -1))
505 if ((m_x
!= old_x
) || (m_y
!= old_y
))
507 /* we set the position here and when showing the dialog
508 for the first time in idle time */
509 gtk_widget_set_uposition( m_widget
, m_x
, m_y
);
513 if ((m_width
!= old_width
) || (m_height
!= old_height
))
515 /* actual resizing is deferred to GtkOnSize in idle time and
516 when showing the dialog */
523 void wxDialog::GtkOnSize( int WXUNUSED(x
), int WXUNUSED(y
), int width
, int height
)
525 // due to a bug in gtk, x,y are always 0
529 if ((m_height
== height
) && (m_width
== width
) && (m_sizeSet
)) return;
530 if (!m_wxwindow
) return;
535 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
536 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
537 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
538 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
540 /* we actually set the size of a frame here and no-where else */
541 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
545 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
546 event
.SetEventObject( this );
547 GetEventHandler()->ProcessEvent( event
);
550 void wxDialog::Centre( int direction
)
552 wxASSERT_MSG( (m_widget
!= NULL
), wxT("invalid dialog") );
557 if ((direction
& wxHORIZONTAL
) == wxHORIZONTAL
) x
= (gdk_screen_width () - m_width
) / 2;
558 if ((direction
& wxVERTICAL
) == wxVERTICAL
) y
= (gdk_screen_height () - m_height
) / 2;
563 void wxDialog::OnInternalIdle()
565 if (!m_sizeSet
&& GTK_WIDGET_REALIZED(m_wxwindow
))
566 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
568 wxWindow::OnInternalIdle();
571 bool wxDialog::Show( bool show
)
573 if (!show
&& IsModal())
575 EndModal( wxID_CANCEL
);
578 if (show
&& !m_sizeSet
)
580 /* by calling GtkOnSize here, we don't have to call
581 either after showing the frame, which would entail
582 much ugly flicker nor from within the size_allocate
583 handler, because GTK 1.1.X forbids that. */
585 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
588 bool ret
= wxWindow::Show( show
);
590 if (show
) InitDialog();
595 bool wxDialog::IsModal() const
597 return m_modalShowing
;
600 void wxDialog::SetModal( bool WXUNUSED(flag
) )
604 m_windowStyle |= wxDIALOG_MODAL;
606 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
608 wxFAIL_MSG( wxT("wxDialog:SetModal obsolete now") );
611 int wxDialog::ShowModal()
615 wxFAIL_MSG( wxT("wxDialog:ShowModal called twice") );
616 return GetReturnCode();
619 wxBusyCursorSuspender cs
; // temporarily suppress the busy cursor
623 m_modalShowing
= TRUE
;
625 gtk_grab_add( m_widget
);
627 gtk_grab_remove( m_widget
);
629 return GetReturnCode();
632 void wxDialog::EndModal( int retCode
)
634 SetReturnCode( retCode
);
638 wxFAIL_MSG( wxT("wxDialog:EndModal called twice") );
642 m_modalShowing
= FALSE
;
649 void wxDialog::InitDialog()
651 wxWindow::InitDialog();
654 void wxDialog::SetIcon( const wxIcon
&icon
)
657 if (!icon
.Ok()) return;
659 if (!m_widget
->window
) return;
661 wxMask
*mask
= icon
.GetMask();
662 GdkBitmap
*bm
= (GdkBitmap
*) NULL
;
663 if (mask
) bm
= mask
->GetBitmap();
665 gdk_window_set_icon( m_widget
->window
, (GdkWindow
*) NULL
, icon
.GetPixmap(), bm
);