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
;
252 wxDialog::wxDialog( wxWindow
*parent
,
253 wxWindowID id
, const wxString
&title
,
254 const wxPoint
&pos
, const wxSize
&size
,
255 long style
, const wxString
&name
)
259 Create( parent
, id
, title
, pos
, size
, style
, name
);
262 bool wxDialog::Create( wxWindow
*parent
,
263 wxWindowID id
, const wxString
&title
,
264 const wxPoint
&pos
, const wxSize
&size
,
265 long style
, const wxString
&name
)
267 wxTopLevelWindows
.Append( this );
269 m_needParent
= FALSE
;
271 if (!PreCreation( parent
, pos
, size
) ||
272 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
274 wxFAIL_MSG( wxT("wxDialog creation failed") );
278 m_insertCallback
= (wxInsertChildFunction
) wxInsertChildInDialog
;
280 m_widget
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
283 gtk_window_set_wmclass( GTK_WINDOW(m_widget
), name
.mb_str(), name
.mb_str() );
285 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
287 gtk_signal_connect( GTK_OBJECT(m_widget
), "delete_event",
288 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback
), (gpointer
)this );
290 m_wxwindow
= gtk_myfixed_new();
291 gtk_widget_show( m_wxwindow
);
292 GTK_WIDGET_UNSET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
294 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
298 if (m_parent
) m_parent
->AddChild( this );
302 /* we cannot set MWM hints before the widget has
303 been realized, so we do this directly after realization */
304 gtk_signal_connect( GTK_OBJECT(m_widget
), "realize",
305 GTK_SIGNAL_FUNC(gtk_dialog_realized_callback
), (gpointer
) this );
307 /* we set the position of the window after the map event. setting it
308 before has no effect (with KWM) */
309 gtk_signal_connect( GTK_OBJECT(m_widget
), "map",
310 GTK_SIGNAL_FUNC(gtk_dialog_map_callback
), (gpointer
) this );
312 /* the user resized the frame by dragging etc. */
313 gtk_signal_connect( GTK_OBJECT(m_widget
), "size_allocate",
314 GTK_SIGNAL_FUNC(gtk_dialog_size_callback
), (gpointer
)this );
316 gtk_signal_connect( GTK_OBJECT(m_widget
), "configure_event",
317 GTK_SIGNAL_FUNC(gtk_dialog_configure_callback
), (gpointer
)this );
322 wxDialog::~wxDialog()
324 m_isBeingDeleted
= TRUE
;
326 wxTopLevelWindows
.DeleteObject( this );
328 if (wxTheApp
->GetTopWindow() == this)
330 wxTheApp
->SetTopWindow( (wxWindow
*) NULL
);
333 if (wxTopLevelWindows
.Number() == 0)
335 wxTheApp
->ExitMainLoop();
339 void wxDialog::SetTitle( const wxString
& title
)
342 if (m_title
.IsNull()) m_title
= wxT("");
343 gtk_window_set_title( GTK_WINDOW(m_widget
), m_title
.mbc_str() );
346 wxString
wxDialog::GetTitle() const
348 return (wxString
&)m_title
;
351 void wxDialog::OnApply( wxCommandEvent
&WXUNUSED(event
) )
353 if (Validate()) TransferDataFromWindow();
356 void wxDialog::OnCancel( wxCommandEvent
&WXUNUSED(event
) )
360 EndModal(wxID_CANCEL
);
364 SetReturnCode(wxID_CANCEL
);
369 void wxDialog::OnOK( wxCommandEvent
&WXUNUSED(event
) )
371 if (Validate() && TransferDataFromWindow())
379 SetReturnCode(wxID_OK
);
385 void wxDialog::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
390 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
392 // We'll send a Cancel message by default,
393 // which may close the dialog.
394 // Check for looping if the Cancel event handler calls Close().
396 // Note that if a cancel button and handler aren't present in the dialog,
397 // nothing will happen when you close the dialog via the window manager, or
399 // We wouldn't want to destroy the dialog by default, since the dialog may have been
400 // created on the stack.
401 // However, this does mean that calling dialog->Close() won't delete the dialog
402 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
403 // sure to destroy the dialog.
404 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
406 static wxList s_closing
;
408 if (s_closing
.Member(this))
411 s_closing
.Append(this);
413 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
414 cancelEvent
.SetEventObject( this );
415 GetEventHandler()->ProcessEvent(cancelEvent
);
416 s_closing
.DeleteObject(this);
419 bool wxDialog::Destroy()
421 if (!wxPendingDelete
.Member(this)) wxPendingDelete
.Append(this);
426 void wxDialog::OnSize( wxSizeEvent
&WXUNUSED(event
) )
428 wxASSERT_MSG( (m_widget
!= NULL
), wxT("invalid dialog") );
430 #if wxUSE_CONSTRAINTS
436 #endif // wxUSE_CONSTRAINTS
438 /* no child: go out ! */
439 if (!GetChildren().First()) return;
441 /* do we have exactly one child? */
442 wxWindow
*child
= (wxWindow
*) NULL
;
443 for(wxNode
*node
= GetChildren().First(); node
; node
= node
->Next())
445 wxWindow
*win
= (wxWindow
*)node
->Data();
446 if (!wxIS_KIND_OF(win
,wxFrame
) && !wxIS_KIND_OF(win
,wxDialog
))
448 /* it's the second one: do nothing */
454 /* yes: set it's size to fill all the frame */
455 int client_x
, client_y
;
456 GetClientSize( &client_x
, &client_y
);
457 child
->SetSize( 1, 1, client_x
-2, client_y
);
461 void wxDialog::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
463 wxASSERT_MSG( (m_widget
!= NULL
), wxT("invalid dialog") );
464 wxASSERT_MSG( (m_wxwindow
!= NULL
), wxT("invalid dialog") );
466 if (m_resizing
) return; /* I don't like recursions */
471 int old_width
= m_width
;
472 int old_height
= m_height
;
474 if ((sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
) == 0)
476 if (x
!= -1) m_x
= x
;
477 if (y
!= -1) m_y
= y
;
478 if (width
!= -1) m_width
= width
;
479 if (height
!= -1) m_height
= height
;
489 if ((sizeFlags
& wxSIZE_AUTO_WIDTH
) == wxSIZE_AUTO_WIDTH
)
491 if (width
== -1) m_width
= 80;
494 if ((sizeFlags
& wxSIZE_AUTO_HEIGHT
) == wxSIZE_AUTO_HEIGHT
)
496 if (height
== -1) m_height
= 26;
499 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
500 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
501 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
502 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
504 if ((m_x
!= -1) || (m_y
!= -1))
506 if ((m_x
!= old_x
) || (m_y
!= old_y
))
508 /* we set the position here and when showing the dialog
509 for the first time in idle time */
510 gtk_widget_set_uposition( m_widget
, m_x
, m_y
);
514 if ((m_width
!= old_width
) || (m_height
!= old_height
))
516 /* actual resizing is deferred to GtkOnSize in idle time and
517 when showing the dialog */
524 void wxDialog::GtkOnSize( int WXUNUSED(x
), int WXUNUSED(y
), int width
, int height
)
526 // due to a bug in gtk, x,y are always 0
530 if ((m_height
== height
) && (m_width
== width
) && (m_sizeSet
)) return;
531 if (!m_wxwindow
) return;
536 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
537 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
538 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
539 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
541 /* we actually set the size of a frame here and no-where else */
542 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
546 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
547 event
.SetEventObject( this );
548 GetEventHandler()->ProcessEvent( event
);
551 void wxDialog::OnInternalIdle()
553 if (!m_sizeSet
&& GTK_WIDGET_REALIZED(m_wxwindow
))
554 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
556 wxWindow::OnInternalIdle();
559 bool wxDialog::Show( bool show
)
561 if (!show
&& IsModal())
563 EndModal( wxID_CANCEL
);
566 if (show
&& !m_sizeSet
)
568 /* by calling GtkOnSize here, we don't have to call
569 either after showing the frame, which would entail
570 much ugly flicker nor from within the size_allocate
571 handler, because GTK 1.1.X forbids that. */
573 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
576 bool ret
= wxWindow::Show( show
);
578 if (show
) InitDialog();
583 bool wxDialog::IsModal() const
585 return m_modalShowing
;
588 void wxDialog::SetModal( bool WXUNUSED(flag
) )
592 m_windowStyle |= wxDIALOG_MODAL;
594 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
596 wxFAIL_MSG( wxT("wxDialog:SetModal obsolete now") );
599 int wxDialog::ShowModal()
603 wxFAIL_MSG( wxT("wxDialog:ShowModal called twice") );
604 return GetReturnCode();
607 wxBusyCursorSuspender cs
; // temporarily suppress the busy cursor
611 m_modalShowing
= TRUE
;
613 gtk_grab_add( m_widget
);
615 gtk_grab_remove( m_widget
);
617 return GetReturnCode();
620 void wxDialog::EndModal( int retCode
)
622 SetReturnCode( retCode
);
626 wxFAIL_MSG( wxT("wxDialog:EndModal called twice") );
630 m_modalShowing
= FALSE
;
637 void wxDialog::InitDialog()
639 wxWindow::InitDialog();
642 void wxDialog::SetIcon( const wxIcon
&icon
)
645 if (!icon
.Ok()) return;
647 if (!m_widget
->window
) return;
649 wxMask
*mask
= icon
.GetMask();
650 GdkBitmap
*bm
= (GdkBitmap
*) NULL
;
651 if (mask
) bm
= mask
->GetBitmap();
653 gdk_window_set_icon( m_widget
->window
, (GdkWindow
*) NULL
, icon
.GetPixmap(), bm
);