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"
23 //-----------------------------------------------------------------------------
25 //-----------------------------------------------------------------------------
27 extern void wxapp_install_idle_handler();
30 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
34 extern wxList wxPendingDelete
;
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
40 bool gtk_dialog_delete_callback( GtkWidget
*WXUNUSED(widget
), GdkEvent
*WXUNUSED(event
), wxDialog
*win
)
43 wxapp_install_idle_handler();
50 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
54 static void gtk_dialog_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxDialog
*win
)
57 wxapp_install_idle_handler();
59 if (!win
->m_hasVMT
) return;
61 if ((win
->m_width
!= alloc
->width
) || (win
->m_height
!= alloc
->height
))
63 win
->m_width
= alloc
->width
;
64 win
->m_height
= alloc
->height
;
69 //-----------------------------------------------------------------------------
71 //-----------------------------------------------------------------------------
74 #if (GTK_MINOR_VERSON > 0)
75 gtk_dialog_configure_callback( GtkWidget
*WXUNUSED(widget
), GdkEventConfigure
*WXUNUSED(event
), wxDialog
*win
)
77 gtk_dialog_configure_callback( GtkWidget
*WXUNUSED(widget
), GdkEventConfigure
*event
, wxDialog
*win
)
81 wxapp_install_idle_handler();
83 if (!win
->m_hasVMT
) return FALSE
;
85 #if (GTK_MINOR_VERSON > 0)
88 gdk_window_get_root_origin( win
->m_widget
->window
, &x
, &y
);
96 wxMoveEvent
mevent( wxPoint(win
->m_x
,win
->m_y
), win
->GetId() );
97 mevent
.SetEventObject( win
);
98 win
->GetEventHandler()->ProcessEvent( mevent
);
103 //-----------------------------------------------------------------------------
104 // "realize" from m_widget
105 //-----------------------------------------------------------------------------
107 /* we cannot MWM hints and icons before the widget has been realized,
108 so we do this directly after realization */
111 gtk_dialog_realized_callback( GtkWidget
*widget
, wxDialog
*win
)
114 wxapp_install_idle_handler();
116 /* I haven''t been able to set the position of
117 the dialog before it is shown, so I set the
118 position in "realize" and "map" */
119 gtk_widget_set_uposition( widget
, win
->m_x
, win
->m_y
);
122 if (win
->m_icon
!= wxNullIcon
)
124 wxIcon
icon( win
->m_icon
);
125 win
->m_icon
= wxNullIcon
;
126 win
->SetIcon( icon
);
132 //-----------------------------------------------------------------------------
133 // "map" from m_widget
134 //-----------------------------------------------------------------------------
137 gtk_dialog_map_callback( GtkWidget
*widget
, wxDialog
*win
)
140 wxapp_install_idle_handler();
142 /* I haven''t been able to set the position of
143 the dialog before it is shown, so I set the
144 position in "realize" and "map" */
145 gtk_widget_set_uposition( widget
, win
->m_x
, win
->m_y
);
147 /* all this is for Motif Window Manager "hints" and is supposed to be
148 recognized by other WM as well. not tested. */
149 long decor
= (long) GDK_DECOR_BORDER
;
150 long func
= (long) GDK_FUNC_MOVE
;
152 if ((win
->GetWindowStyle() & wxCAPTION
) != 0)
153 decor
|= GDK_DECOR_TITLE
;
154 if ((win
->GetWindowStyle() & wxSYSTEM_MENU
) != 0)
156 decor
|= GDK_DECOR_MENU
;
157 func
|= GDK_FUNC_CLOSE
;
159 if ((win
->GetWindowStyle() & wxMINIMIZE_BOX
) != 0)
161 func
|= GDK_FUNC_MINIMIZE
;
162 decor
|= GDK_DECOR_MINIMIZE
;
164 if ((win
->GetWindowStyle() & wxMAXIMIZE_BOX
) != 0)
166 decor
|= GDK_DECOR_MAXIMIZE
;
167 func
|= GDK_FUNC_MAXIMIZE
;
169 if ((win
->GetWindowStyle() & wxRESIZE_BORDER
) != 0)
171 func
|= GDK_FUNC_RESIZE
;
172 decor
|= GDK_DECOR_RESIZEH
;
174 gdk_window_set_decorations( win
->m_widget
->window
, (GdkWMDecoration
)decor
);
175 gdk_window_set_functions( win
->m_widget
->window
, (GdkWMFunction
)func
);
177 /* GTK's shrinking/growing policy */
178 if ((win
->GetWindowStyle() & wxRESIZE_BORDER
) == 0)
179 gtk_window_set_policy(GTK_WINDOW(win
->m_widget
), 0, 0, 1);
181 gtk_window_set_policy(GTK_WINDOW(win
->m_widget
), 1, 1, 1);
186 //-----------------------------------------------------------------------------
187 // InsertChild for wxDialog
188 //-----------------------------------------------------------------------------
190 /* Callback for wxFrame. This very strange beast has to be used because
191 * C++ has no virtual methods in a constructor. We have to emulate a
192 * virtual function here as wxWindows requires different ways to insert
193 * a child in container classes. */
195 static void wxInsertChildInDialog( wxDialog
* parent
, wxWindow
* child
)
197 gtk_myfixed_put( GTK_MYFIXED(parent
->m_wxwindow
),
198 GTK_WIDGET(child
->m_widget
),
204 if (parent
->HasFlag(wxTAB_TRAVERSAL
))
206 /* we now allow a window to get the focus as long as it
207 doesn't have any children. */
208 GTK_WIDGET_UNSET_FLAGS( parent
->m_wxwindow
, GTK_CAN_FOCUS
);
212 //-----------------------------------------------------------------------------
214 //-----------------------------------------------------------------------------
216 BEGIN_EVENT_TABLE(wxDialog
,wxPanel
)
217 EVT_BUTTON (wxID_OK
, wxDialog::OnOK
)
218 EVT_BUTTON (wxID_CANCEL
, wxDialog::OnCancel
)
219 EVT_BUTTON (wxID_APPLY
, wxDialog::OnApply
)
220 EVT_SIZE (wxDialog::OnSize
)
221 EVT_CLOSE (wxDialog::OnCloseWindow
)
224 IMPLEMENT_DYNAMIC_CLASS(wxDialog
,wxPanel
)
226 void wxDialog::Init()
230 m_modalShowing
= FALSE
;
233 wxDialog::wxDialog( wxWindow
*parent
,
234 wxWindowID id
, const wxString
&title
,
235 const wxPoint
&pos
, const wxSize
&size
,
236 long style
, const wxString
&name
)
240 Create( parent
, id
, title
, pos
, size
, style
, name
);
243 bool wxDialog::Create( wxWindow
*parent
,
244 wxWindowID id
, const wxString
&title
,
245 const wxPoint
&pos
, const wxSize
&size
,
246 long style
, const wxString
&name
)
248 wxTopLevelWindows
.Append( this );
250 m_needParent
= FALSE
;
252 if (!PreCreation( parent
, pos
, size
) ||
253 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
255 wxFAIL_MSG( _T("wxDialog creation failed") );
259 m_insertCallback
= (wxInsertChildFunction
) wxInsertChildInDialog
;
261 m_widget
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
264 gtk_window_set_wmclass( GTK_WINDOW(m_widget
), name
.mb_str(), name
.mb_str() );
266 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
268 gtk_signal_connect( GTK_OBJECT(m_widget
), "delete_event",
269 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback
), (gpointer
)this );
271 m_wxwindow
= gtk_myfixed_new();
272 gtk_widget_show( m_wxwindow
);
273 GTK_WIDGET_UNSET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
275 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
279 if (m_parent
) m_parent
->AddChild( this );
283 /* we cannot set MWM hints before the widget has
284 been realized, so we do this directly after realization */
285 gtk_signal_connect( GTK_OBJECT(m_widget
), "realize",
286 GTK_SIGNAL_FUNC(gtk_dialog_realized_callback
), (gpointer
) this );
288 /* we set the position of the window after the map event. setting it
289 before has no effect (with KWM) */
290 gtk_signal_connect( GTK_OBJECT(m_widget
), "map",
291 GTK_SIGNAL_FUNC(gtk_dialog_map_callback
), (gpointer
) this );
293 /* the user resized the frame by dragging etc. */
294 gtk_signal_connect( GTK_OBJECT(m_widget
), "size_allocate",
295 GTK_SIGNAL_FUNC(gtk_dialog_size_callback
), (gpointer
)this );
297 gtk_signal_connect( GTK_OBJECT(m_widget
), "configure_event",
298 GTK_SIGNAL_FUNC(gtk_dialog_configure_callback
), (gpointer
)this );
303 wxDialog::~wxDialog()
305 m_isBeingDeleted
= TRUE
;
307 wxTopLevelWindows
.DeleteObject( this );
309 if (wxTheApp
->GetTopWindow() == this)
311 wxTheApp
->SetTopWindow( (wxWindow
*) NULL
);
314 if (wxTopLevelWindows
.Number() == 0)
316 wxTheApp
->ExitMainLoop();
320 void wxDialog::SetTitle( const wxString
& title
)
323 if (m_title
.IsNull()) m_title
= _T("");
324 gtk_window_set_title( GTK_WINDOW(m_widget
), m_title
.mbc_str() );
327 wxString
wxDialog::GetTitle() const
329 return (wxString
&)m_title
;
332 void wxDialog::OnApply( wxCommandEvent
&WXUNUSED(event
) )
334 if (Validate()) TransferDataFromWindow();
337 void wxDialog::OnCancel( wxCommandEvent
&WXUNUSED(event
) )
341 EndModal(wxID_CANCEL
);
345 SetReturnCode(wxID_CANCEL
);
350 void wxDialog::OnOK( wxCommandEvent
&WXUNUSED(event
) )
352 if (Validate() && TransferDataFromWindow())
360 SetReturnCode(wxID_OK
);
366 void wxDialog::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
371 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
373 // We'll send a Cancel message by default,
374 // which may close the dialog.
375 // Check for looping if the Cancel event handler calls Close().
377 // Note that if a cancel button and handler aren't present in the dialog,
378 // nothing will happen when you close the dialog via the window manager, or
380 // We wouldn't want to destroy the dialog by default, since the dialog may have been
381 // created on the stack.
382 // However, this does mean that calling dialog->Close() won't delete the dialog
383 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
384 // sure to destroy the dialog.
385 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
387 static wxList s_closing
;
389 if (s_closing
.Member(this))
392 s_closing
.Append(this);
394 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
395 cancelEvent
.SetEventObject( this );
396 GetEventHandler()->ProcessEvent(cancelEvent
);
397 s_closing
.DeleteObject(this);
400 bool wxDialog::Destroy()
402 if (!wxPendingDelete
.Member(this)) wxPendingDelete
.Append(this);
407 void wxDialog::OnSize( wxSizeEvent
&WXUNUSED(event
) )
409 wxASSERT_MSG( (m_widget
!= NULL
), _T("invalid dialog") );
411 #if wxUSE_CONSTRAINTS
417 #endif // wxUSE_CONSTRAINTS
419 /* no child: go out ! */
420 if (!GetChildren().First()) return;
422 /* do we have exactly one child? */
423 wxWindow
*child
= (wxWindow
*) NULL
;
424 for(wxNode
*node
= GetChildren().First(); node
; node
= node
->Next())
426 wxWindow
*win
= (wxWindow
*)node
->Data();
427 if (!wxIS_KIND_OF(win
,wxFrame
) && !wxIS_KIND_OF(win
,wxDialog
))
429 /* it's the second one: do nothing */
435 /* yes: set it's size to fill all the frame */
436 int client_x
, client_y
;
437 GetClientSize( &client_x
, &client_y
);
438 child
->SetSize( 1, 1, client_x
-2, client_y
);
442 void wxDialog::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
444 wxASSERT_MSG( (m_widget
!= NULL
), _T("invalid dialog") );
445 wxASSERT_MSG( (m_wxwindow
!= NULL
), _T("invalid dialog") );
447 if (m_resizing
) return; /* I don't like recursions */
452 int old_width
= m_width
;
453 int old_height
= m_height
;
455 if ((sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
) == 0)
457 if (x
!= -1) m_x
= x
;
458 if (y
!= -1) m_y
= y
;
459 if (width
!= -1) m_width
= width
;
460 if (height
!= -1) m_height
= height
;
470 if ((sizeFlags
& wxSIZE_AUTO_WIDTH
) == wxSIZE_AUTO_WIDTH
)
472 if (width
== -1) m_width
= 80;
475 if ((sizeFlags
& wxSIZE_AUTO_HEIGHT
) == wxSIZE_AUTO_HEIGHT
)
477 if (height
== -1) m_height
= 26;
480 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
481 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
482 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
483 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
485 if ((m_x
!= -1) || (m_y
!= -1))
487 if ((m_x
!= old_x
) || (m_y
!= old_y
))
489 /* we set the position here and when showing the dialog
490 for the first time in idle time */
491 gtk_widget_set_uposition( m_widget
, m_x
, m_y
);
495 if ((m_width
!= old_width
) || (m_height
!= old_height
))
497 /* actual resizing is deferred to GtkOnSize in idle time and
498 when showing the dialog */
505 void wxDialog::GtkOnSize( int WXUNUSED(x
), int WXUNUSED(y
), int width
, int height
)
507 // due to a bug in gtk, x,y are always 0
511 if ((m_height
== height
) && (m_width
== width
) && (m_sizeSet
)) return;
512 if (!m_wxwindow
) return;
517 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
518 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
519 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
520 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
522 /* we actually set the size of a frame here and no-where else */
523 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
527 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
528 event
.SetEventObject( this );
529 GetEventHandler()->ProcessEvent( event
);
532 void wxDialog::Centre( int direction
)
534 wxASSERT_MSG( (m_widget
!= NULL
), _T("invalid dialog") );
539 if ((direction
& wxHORIZONTAL
) == wxHORIZONTAL
) x
= (gdk_screen_width () - m_width
) / 2;
540 if ((direction
& wxVERTICAL
) == wxVERTICAL
) y
= (gdk_screen_height () - m_height
) / 2;
545 void wxDialog::OnInternalIdle()
547 if (!m_sizeSet
&& GTK_WIDGET_REALIZED(m_wxwindow
))
548 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
551 bool wxDialog::Show( bool show
)
553 if (!show
&& IsModal())
555 EndModal( wxID_CANCEL
);
558 if (show
&& !m_sizeSet
)
560 /* by calling GtkOnSize here, we don't have to call
561 either after showing the frame, which would entail
562 much ugly flicker nor from within the size_allocate
563 handler, because GTK 1.1.X forbids that. */
565 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
568 bool ret
= wxWindow::Show( show
);
570 if (show
) InitDialog();
575 bool wxDialog::IsModal() const
577 return m_modalShowing
;
580 void wxDialog::SetModal( bool WXUNUSED(flag
) )
584 m_windowStyle |= wxDIALOG_MODAL;
586 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
588 wxFAIL_MSG( _T("wxDialog:SetModal obsolete now") );
591 int wxDialog::ShowModal()
595 wxFAIL_MSG( _T("wxDialog:ShowModal called twice") );
596 return GetReturnCode();
599 wxBusyCursorSuspender cs
; // temporarily suppress the busy cursor
603 m_modalShowing
= TRUE
;
605 gtk_grab_add( m_widget
);
607 gtk_grab_remove( m_widget
);
609 return GetReturnCode();
612 void wxDialog::EndModal( int retCode
)
614 SetReturnCode( retCode
);
618 wxFAIL_MSG( _T("wxDialog:EndModal called twice") );
622 m_modalShowing
= FALSE
;
629 void wxDialog::InitDialog()
631 wxWindow::InitDialog();
634 void wxDialog::SetIcon( const wxIcon
&icon
)
637 if (!icon
.Ok()) return;
639 if (!m_widget
->window
) return;
641 wxMask
*mask
= icon
.GetMask();
642 GdkBitmap
*bm
= (GdkBitmap
*) NULL
;
643 if (mask
) bm
= mask
->GetBitmap();
645 gdk_window_set_icon( m_widget
->window
, (GdkWindow
*) NULL
, icon
.GetPixmap(), bm
);