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 extern wxList wxPendingDelete
;
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 bool gtk_dialog_delete_callback( GtkWidget
*WXUNUSED(widget
), GdkEvent
*WXUNUSED(event
), wxDialog
*win
)
33 printf( "OnDelete from " );
34 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
35 printf( win->GetClassInfo()->GetClassName() );
44 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
48 static void gtk_dialog_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxDialog
*win
)
50 if (!win
->HasVMT()) return;
53 printf( "OnDialogResize from " );
54 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
55 printf( win->GetClassInfo()->GetClassName() );
59 if ((win
->m_width
!= alloc
->width
) || (win
->m_height
!= alloc
->height
))
61 win
->m_sizeSet
= FALSE
;
62 win
->m_width
= alloc
->width
;
63 win
->m_height
= alloc
->height
;
67 //-----------------------------------------------------------------------------
69 //-----------------------------------------------------------------------------
71 static gint
gtk_dialog_configure_callback( GtkWidget
*WXUNUSED(widget
), GdkEventConfigure
*event
, wxDialog
*win
)
73 if (!win
->HasVMT()) return FALSE
;
78 wxMoveEvent
mevent( wxPoint(win
->m_x
,win
->m_y
), win
->GetId() );
79 mevent
.SetEventObject( win
);
80 win
->GetEventHandler()->ProcessEvent( mevent
);
85 //-----------------------------------------------------------------------------
87 //-----------------------------------------------------------------------------
89 BEGIN_EVENT_TABLE(wxDialog
,wxPanel
)
90 EVT_BUTTON (wxID_OK
, wxDialog::OnOK
)
91 EVT_BUTTON (wxID_CANCEL
, wxDialog::OnCancel
)
92 EVT_BUTTON (wxID_APPLY
, wxDialog::OnApply
)
93 EVT_SIZE (wxDialog::OnSize
)
94 EVT_CLOSE (wxDialog::OnCloseWindow
)
97 IMPLEMENT_DYNAMIC_CLASS(wxDialog
,wxPanel
)
103 m_modalShowing
= FALSE
;
106 wxDialog::wxDialog( wxWindow
*parent
,
107 wxWindowID id
, const wxString
&title
,
108 const wxPoint
&pos
, const wxSize
&size
,
109 long style
, const wxString
&name
)
111 m_modalShowing
= FALSE
;
113 Create( parent
, id
, title
, pos
, size
, style
, name
);
116 bool wxDialog::Create( wxWindow
*parent
,
117 wxWindowID id
, const wxString
&title
,
118 const wxPoint
&pos
, const wxSize
&size
,
119 long style
, const wxString
&name
)
121 wxTopLevelWindows
.Append( this );
123 m_needParent
= FALSE
;
125 PreCreation( parent
, id
, pos
, size
, style
, name
);
127 m_widget
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
128 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
130 gtk_widget_set( m_widget
, "GtkWindow::allow_shrink", TRUE
, NULL
);
132 gtk_signal_connect( GTK_OBJECT(m_widget
), "delete_event",
133 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback
), (gpointer
)this );
135 m_wxwindow
= gtk_myfixed_new();
136 gtk_widget_show( m_wxwindow
);
137 GTK_WIDGET_UNSET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
139 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
143 if (m_parent
) m_parent
->AddChild( this );
147 gtk_widget_realize( m_widget
);
149 gtk_signal_connect( GTK_OBJECT(m_widget
), "size_allocate",
150 GTK_SIGNAL_FUNC(gtk_dialog_size_callback
), (gpointer
)this );
152 gtk_signal_connect( GTK_OBJECT(m_widget
), "configure_event",
153 GTK_SIGNAL_FUNC(gtk_dialog_configure_callback
), (gpointer
)this );
158 wxDialog::~wxDialog()
160 wxTopLevelWindows
.DeleteObject( this );
162 if (wxTheApp
->GetTopWindow() == this)
164 wxTheApp
->SetTopWindow( (wxWindow
*) NULL
);
167 if (wxTopLevelWindows
.Number() == 0)
169 wxTheApp
->ExitMainLoop();
173 void wxDialog::SetTitle( const wxString
& title
)
176 if (m_title
.IsNull()) m_title
= "";
177 gtk_window_set_title( GTK_WINDOW(m_widget
), m_title
);
180 wxString
wxDialog::GetTitle() const
182 return (wxString
&)m_title
;
185 void wxDialog::OnApply( wxCommandEvent
&WXUNUSED(event
) )
187 if (Validate()) TransferDataFromWindow();
190 void wxDialog::OnCancel( wxCommandEvent
&WXUNUSED(event
) )
194 EndModal(wxID_CANCEL
);
198 SetReturnCode(wxID_CANCEL
);
203 void wxDialog::OnOK( wxCommandEvent
&WXUNUSED(event
) )
205 if ( Validate() && TransferDataFromWindow())
213 SetReturnCode(wxID_OK
);
219 void wxDialog::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
224 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
226 // We'll send a Cancel message by default,
227 // which may close the dialog.
228 // Check for looping if the Cancel event handler calls Close().
230 // Note that if a cancel button and handler aren't present in the dialog,
231 // nothing will happen when you close the dialog via the window manager, or
233 // We wouldn't want to destroy the dialog by default, since the dialog may have been
234 // created on the stack.
235 // However, this does mean that calling dialog->Close() won't delete the dialog
236 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
237 // sure to destroy the dialog.
238 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
240 static wxList closing
;
242 if (closing
.Member(this))
245 closing
.Append(this);
247 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
248 cancelEvent
.SetEventObject( this );
249 GetEventHandler()->ProcessEvent(cancelEvent
);
250 closing
.DeleteObject(this);
253 bool wxDialog::Destroy()
255 if (!wxPendingDelete
.Member(this)) wxPendingDelete
.Append(this);
260 void wxDialog::OnSize( wxSizeEvent
&WXUNUSED(event
) )
262 wxASSERT_MSG( (m_widget
!= NULL
), "invalid dialog" );
270 /* no child: go out ! */
271 if (!GetChildren().First()) return;
273 /* do we have exactly one child? */
274 wxWindow
*child
= (wxWindow
*) NULL
;
275 for(wxNode
*node
= GetChildren().First(); node
; node
= node
->Next())
277 wxWindow
*win
= (wxWindow
*)node
->Data();
278 if (!wxIS_KIND_OF(win
,wxFrame
) && !wxIS_KIND_OF(win
,wxDialog
))
280 /* it's the second one: do nothing */
286 /* yes: set it's size to fill all the frame */
287 int client_x
, client_y
;
288 GetClientSize( &client_x
, &client_y
);
289 child
->SetSize( 1, 1, client_x
-2, client_y
);
293 void wxDialog::SetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
295 wxASSERT_MSG( (m_widget
!= NULL
), "invalid dialog" );
296 wxASSERT_MSG( (m_wxwindow
!= NULL
), "invalid dialog" );
298 if (m_resizing
) return; /* I don't like recursions */
303 int old_width
= m_width
;
304 int old_height
= m_height
;
306 if ((sizeFlags
& wxSIZE_USE_EXISTING
) == wxSIZE_USE_EXISTING
)
308 if (x
!= -1) m_x
= x
;
309 if (y
!= -1) m_y
= y
;
310 if (width
!= -1) m_width
= width
;
311 if (height
!= -1) m_height
= height
;
321 if ((sizeFlags
& wxSIZE_AUTO_WIDTH
) == wxSIZE_AUTO_WIDTH
)
323 if (width
== -1) m_width
= 80;
326 if ((sizeFlags
& wxSIZE_AUTO_HEIGHT
) == wxSIZE_AUTO_HEIGHT
)
328 if (height
== -1) m_height
= 26;
331 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
332 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
333 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
334 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
336 if ((m_x
!= -1) || (m_y
!= -1))
338 if ((m_x
!= old_x
) || (m_y
!= old_y
))
340 /* m_sizeSet = FALSE; */
341 gtk_widget_set_uposition( m_widget
, m_x
, m_y
);
345 if ((m_width
!= old_width
) || (m_height
!= old_height
))
353 void wxDialog::SetSize( int width
, int height
)
355 SetSize( -1, -1, width
, height
, wxSIZE_USE_EXISTING
);
358 void wxDialog::GtkOnSize( int WXUNUSED(x
), int WXUNUSED(y
), int width
, int height
)
360 // due to a bug in gtk, x,y are always 0
364 if ((m_height
== height
) && (m_width
== width
) && (m_sizeSet
)) return;
365 if (!m_wxwindow
) return;
370 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
371 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
372 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
373 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
375 /* we actually set the size of a frame here and no-where else */
376 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
380 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
381 event
.SetEventObject( this );
382 GetEventHandler()->ProcessEvent( event
);
385 void wxDialog::Centre( int direction
)
387 wxASSERT_MSG( (m_widget
!= NULL
), "invalid dialog" );
392 if ((direction
& wxHORIZONTAL
) == wxHORIZONTAL
) x
= (gdk_screen_width () - m_width
) / 2;
393 if ((direction
& wxVERTICAL
) == wxVERTICAL
) y
= (gdk_screen_height () - m_height
) / 2;
398 void wxDialog::OnInternalIdle()
401 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
404 bool wxDialog::Show( bool show
)
406 if (!show
&& IsModal())
408 EndModal( wxID_CANCEL
);
411 if (show
&& !m_sizeSet
)
413 /* by calling GtkOnSize here, we don't have to call
414 either after showing the frame, which would entail
415 much ugly flicker nor from within the size_allocate
416 handler, because GTK 1.1.X forbids that. */
418 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
421 wxWindow::Show( show
);
423 if (show
) InitDialog();
428 bool wxDialog::IsModal() const
430 return m_modalShowing
;
433 void wxDialog::SetModal( bool WXUNUSED(flag
) )
437 m_windowStyle |= wxDIALOG_MODAL;
439 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
441 wxFAIL_MSG( "wxDialog:SetModal obsolete now" );
444 int wxDialog::ShowModal()
448 wxFAIL_MSG( "wxDialog:ShowModal called twice" );
449 return GetReturnCode();
454 m_modalShowing
= TRUE
;
456 gtk_grab_add( m_widget
);
458 gtk_grab_remove( m_widget
);
460 return GetReturnCode();
463 void wxDialog::EndModal( int retCode
)
465 SetReturnCode( retCode
);
469 wxFAIL_MSG( "wxDialog:EndModal called twice" );
473 m_modalShowing
= FALSE
;
480 void wxDialog::InitDialog()
482 wxWindow::InitDialog();
485 void wxDialog::SetIcon( const wxIcon
&icon
)
488 if (!icon
.Ok()) return;
490 wxMask
*mask
= icon
.GetMask();
491 GdkBitmap
*bm
= (GdkBitmap
*) NULL
;
492 if (mask
) bm
= mask
->GetBitmap();
494 gdk_window_set_icon( m_widget
->window
, (GdkWindow
*) NULL
, icon
.GetPixmap(), bm
);