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 win
->GtkOnSize( alloc
->x
, alloc
->y
, alloc
->width
, alloc
->height
);
62 //-----------------------------------------------------------------------------
64 //-----------------------------------------------------------------------------
66 static gint
gtk_dialog_configure_callback( GtkWidget
*WXUNUSED(widget
), GdkEventConfigure
*event
, wxDialog
*win
)
68 if (!win
->HasVMT()) return FALSE
;
73 wxMoveEvent
mevent( wxPoint(win
->m_x
,win
->m_y
), win
->GetId() );
74 mevent
.SetEventObject( win
);
75 win
->GetEventHandler()->ProcessEvent( mevent
);
80 //-----------------------------------------------------------------------------
82 //-----------------------------------------------------------------------------
84 BEGIN_EVENT_TABLE(wxDialog
,wxPanel
)
85 EVT_BUTTON (wxID_OK
, wxDialog::OnOK
)
86 EVT_BUTTON (wxID_CANCEL
, wxDialog::OnCancel
)
87 EVT_BUTTON (wxID_APPLY
, wxDialog::OnApply
)
88 EVT_SIZE (wxDialog::OnSize
)
89 EVT_CLOSE (wxDialog::OnCloseWindow
)
92 IMPLEMENT_DYNAMIC_CLASS(wxDialog
,wxPanel
)
97 m_modalShowing
= FALSE
;
100 wxDialog::wxDialog( wxWindow
*parent
,
101 wxWindowID id
, const wxString
&title
,
102 const wxPoint
&pos
, const wxSize
&size
,
103 long style
, const wxString
&name
)
105 m_modalShowing
= FALSE
;
106 Create( parent
, id
, title
, pos
, size
, style
, name
);
109 bool wxDialog::Create( wxWindow
*parent
,
110 wxWindowID id
, const wxString
&title
,
111 const wxPoint
&pos
, const wxSize
&size
,
112 long style
, const wxString
&name
)
114 wxTopLevelWindows
.Append( this );
116 m_needParent
= FALSE
;
118 PreCreation( parent
, id
, pos
, size
, style
, name
);
120 m_widget
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
121 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
123 gtk_widget_set( m_widget
, "GtkWindow::allow_shrink", TRUE
, NULL
);
125 gtk_signal_connect( GTK_OBJECT(m_widget
), "delete_event",
126 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback
), (gpointer
)this );
128 m_wxwindow
= gtk_myfixed_new();
129 gtk_widget_show( m_wxwindow
);
130 GTK_WIDGET_UNSET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
132 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
136 if ((m_x
!= -1) || (m_y
!= -1))
137 gtk_widget_set_uposition( m_widget
, m_x
, m_y
);
139 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
141 gtk_signal_connect( GTK_OBJECT(m_widget
), "size_allocate",
142 GTK_SIGNAL_FUNC(gtk_dialog_size_callback
), (gpointer
)this );
144 gtk_signal_connect( GTK_OBJECT(m_widget
), "configure_event",
145 GTK_SIGNAL_FUNC(gtk_dialog_configure_callback
), (gpointer
)this );
147 if (m_parent
) m_parent
->AddChild( this );
154 wxDialog::~wxDialog()
156 wxTopLevelWindows
.DeleteObject( this );
158 if (wxTheApp
->GetTopWindow() == this)
160 wxTheApp
->SetTopWindow( (wxWindow
*) NULL
);
163 if (wxTopLevelWindows
.Number() == 0)
165 wxTheApp
->ExitMainLoop();
169 void wxDialog::SetTitle( const wxString
& title
)
172 if (m_title
.IsNull()) m_title
= "";
173 gtk_window_set_title( GTK_WINDOW(m_widget
), m_title
);
176 wxString
wxDialog::GetTitle() const
178 return (wxString
&)m_title
;
181 void wxDialog::OnApply( wxCommandEvent
&WXUNUSED(event
) )
183 if (Validate()) TransferDataFromWindow();
186 void wxDialog::OnCancel( wxCommandEvent
&WXUNUSED(event
) )
190 EndModal(wxID_CANCEL
);
194 SetReturnCode(wxID_CANCEL
);
199 void wxDialog::OnOK( wxCommandEvent
&WXUNUSED(event
) )
201 if ( Validate() && TransferDataFromWindow())
209 SetReturnCode(wxID_OK
);
215 void wxDialog::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
220 bool wxDialog::OnClose()
222 static wxList closing
;
224 if (closing
.Member(this)) return FALSE
; // no loops
226 closing
.Append(this);
228 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
229 cancelEvent
.SetEventObject( this );
230 GetEventHandler()->ProcessEvent(cancelEvent
);
231 closing
.DeleteObject(this);
236 bool wxDialog::Destroy()
238 if (!wxPendingDelete
.Member(this)) wxPendingDelete
.Append(this);
243 void wxDialog::OnCloseWindow( wxCloseEvent
& event
)
245 if (GetEventHandler()->OnClose() || event
.GetForce())
251 void wxDialog::GtkOnSize( int WXUNUSED(x
), int WXUNUSED(y
), int width
, int height
)
253 // due to a bug in gtk, x,y are always 0
257 if ((m_height
== height
) && (m_width
== width
) &&
259 if (!m_wxwindow
) return;
264 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
265 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
266 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
267 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
269 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
273 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
274 event
.SetEventObject( this );
275 GetEventHandler()->ProcessEvent( event
);
278 void wxDialog::OnSize( wxSizeEvent
&WXUNUSED(event
) )
280 wxASSERT_MSG( (m_widget
!= NULL
), "invalid dialog" );
288 // no child: go out !
289 if (!GetChildren().First()) return;
291 // do we have exactly one child?
292 wxWindow
*child
= (wxWindow
*) NULL
;
293 for(wxNode
*node
= GetChildren().First(); node
; node
= node
->Next())
295 wxWindow
*win
= (wxWindow
*)node
->Data();
296 if (!wxIS_KIND_OF(win
,wxFrame
) && !wxIS_KIND_OF(win
,wxDialog
))
298 // it's the second one: do nothing
304 // yes: set it's size to fill all the frame
305 int client_x
, client_y
;
306 GetClientSize( &client_x
, &client_y
);
307 child
->SetSize( 1, 1, client_x
-2, client_y
);
311 void wxDialog::SetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
313 wxASSERT_MSG( (m_widget
!= NULL
), "invalid window" );
315 // Don't do anything for children of wxMDIChildFrame
316 if (!m_wxwindow
) return;
318 if (m_resizing
) return; // I don't like recursions
323 int old_width
= m_width
;
324 int old_height
= m_height
;
326 if ((sizeFlags
& wxSIZE_USE_EXISTING
) == wxSIZE_USE_EXISTING
)
328 if (x
!= -1) m_x
= x
;
329 if (y
!= -1) m_y
= y
;
330 if (width
!= -1) m_width
= width
;
331 if (height
!= -1) m_height
= height
;
341 if ((sizeFlags
& wxSIZE_AUTO_WIDTH
) == wxSIZE_AUTO_WIDTH
)
343 if (width
== -1) m_width
= 80;
346 if ((sizeFlags
& wxSIZE_AUTO_HEIGHT
) == wxSIZE_AUTO_HEIGHT
)
348 if (height
== -1) m_height
= 26;
351 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
352 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
353 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
354 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
356 if ((m_x
!= -1) || (m_y
!= -1))
358 if ((m_x
!= old_x
) || (m_y
!= old_y
))
359 gtk_widget_set_uposition( m_widget
, m_x
, m_y
);
362 if ((m_width
!= old_width
) || (m_height
!= old_height
))
364 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
369 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
370 event
.SetEventObject( this );
371 GetEventHandler()->ProcessEvent( event
);
376 void wxDialog::SetSize( int width
, int height
)
378 SetSize( -1, -1, width
, height
, wxSIZE_USE_EXISTING
);
381 void wxDialog::Centre( int direction
)
383 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
388 if (direction
& wxHORIZONTAL
== wxHORIZONTAL
) x
= (gdk_screen_width () - m_width
) / 2;
389 if (direction
& wxVERTICAL
== wxVERTICAL
) y
= (gdk_screen_height () - m_height
) / 2;
394 bool wxDialog::Show( bool show
)
396 if (!show
&& IsModal())
398 EndModal( wxID_CANCEL
);
401 wxWindow::Show( show
);
403 if (show
) InitDialog();
408 bool wxDialog::IsModal() const
410 return m_modalShowing
;
413 void wxDialog::SetModal( bool WXUNUSED(flag
) )
417 m_windowStyle |= wxDIALOG_MODAL;
419 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
421 wxFAIL_MSG( "wxDialog:SetModal obsolete now" );
424 int wxDialog::ShowModal()
428 wxFAIL_MSG( "wxDialog:ShowModal called twice" );
429 return GetReturnCode();
434 m_modalShowing
= TRUE
;
436 gtk_grab_add( m_widget
);
438 gtk_grab_remove( m_widget
);
440 return GetReturnCode();
443 void wxDialog::EndModal( int retCode
)
445 SetReturnCode( retCode
);
449 wxFAIL_MSG( "wxDialog:EndModal called twice" );
453 m_modalShowing
= FALSE
;
460 void wxDialog::InitDialog()
462 wxWindow::InitDialog();
465 void wxDialog::SetIcon( const wxIcon
&icon
)
468 if (!icon
.Ok()) return;
470 wxMask
*mask
= icon
.GetMask();
471 GdkBitmap
*bm
= (GdkBitmap
*) NULL
;
472 if (mask
) bm
= mask
->GetBitmap();
474 gdk_window_set_icon( m_widget
->window
, (GdkWindow
*) NULL
, icon
.GetPixmap(), bm
);