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 BEGIN_EVENT_TABLE(wxDialog
,wxPanel
)
67 EVT_BUTTON (wxID_OK
, wxDialog::OnOK
)
68 EVT_BUTTON (wxID_CANCEL
, wxDialog::OnCancel
)
69 EVT_BUTTON (wxID_APPLY
, wxDialog::OnApply
)
70 EVT_SIZE (wxDialog::OnSize
)
71 EVT_CLOSE (wxDialog::OnCloseWindow
)
74 IMPLEMENT_DYNAMIC_CLASS(wxDialog
,wxPanel
)
79 m_modalShowing
= FALSE
;
82 wxDialog::wxDialog( wxWindow
*parent
,
83 wxWindowID id
, const wxString
&title
,
84 const wxPoint
&pos
, const wxSize
&size
,
85 long style
, const wxString
&name
)
87 m_modalShowing
= FALSE
;
88 Create( parent
, id
, title
, pos
, size
, style
, name
);
91 bool wxDialog::Create( wxWindow
*parent
,
92 wxWindowID id
, const wxString
&title
,
93 const wxPoint
&pos
, const wxSize
&size
,
94 long style
, const wxString
&name
)
96 wxTopLevelWindows
.Append( this );
100 PreCreation( parent
, id
, pos
, size
, style
, name
);
102 m_widget
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
103 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
105 gtk_widget_set( m_widget
, "GtkWindow::allow_shrink", TRUE
, NULL
);
107 gtk_signal_connect( GTK_OBJECT(m_widget
), "delete_event",
108 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback
), (gpointer
)this );
110 gtk_signal_connect( GTK_OBJECT(m_widget
), "size_allocate",
111 GTK_SIGNAL_FUNC(gtk_dialog_size_callback
), (gpointer
)this );
113 m_wxwindow
= gtk_myfixed_new();
114 gtk_widget_show( m_wxwindow
);
115 GTK_WIDGET_UNSET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
117 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
121 if ((m_x
!= -1) || (m_y
!= -1))
122 gtk_widget_set_uposition( m_widget
, m_x
, m_y
);
124 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
126 if (m_parent
) m_parent
->AddChild( this );
134 wxDialog::~wxDialog()
136 wxTopLevelWindows
.DeleteObject( this );
138 if (wxTheApp
->GetTopWindow() == this)
140 wxTheApp
->SetTopWindow( (wxWindow
*) NULL
);
143 if (wxTopLevelWindows
.Number() == 0)
145 wxTheApp
->ExitMainLoop();
149 void wxDialog::SetTitle( const wxString
& title
)
152 if (m_title
.IsNull()) m_title
= "";
153 gtk_window_set_title( GTK_WINDOW(m_widget
), m_title
);
156 wxString
wxDialog::GetTitle() const
158 return (wxString
&)m_title
;
161 void wxDialog::OnApply( wxCommandEvent
&WXUNUSED(event
) )
163 if (Validate()) TransferDataFromWindow();
166 void wxDialog::OnCancel( wxCommandEvent
&WXUNUSED(event
) )
170 EndModal(wxID_CANCEL
);
174 SetReturnCode(wxID_CANCEL
);
179 void wxDialog::OnOK( wxCommandEvent
&WXUNUSED(event
) )
181 if ( Validate() && TransferDataFromWindow())
189 SetReturnCode(wxID_OK
);
195 void wxDialog::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
200 bool wxDialog::OnClose()
202 static wxList closing
;
204 if (closing
.Member(this)) return FALSE
; // no loops
206 closing
.Append(this);
208 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
209 cancelEvent
.SetEventObject( this );
210 GetEventHandler()->ProcessEvent(cancelEvent
);
211 closing
.DeleteObject(this);
216 bool wxDialog::Destroy()
218 if (!wxPendingDelete
.Member(this)) wxPendingDelete
.Append(this);
223 void wxDialog::OnCloseWindow( wxCloseEvent
& event
)
225 if (GetEventHandler()->OnClose() || event
.GetForce())
231 void wxDialog::GtkOnSize( int WXUNUSED(x
), int WXUNUSED(y
), int width
, int height
)
233 // due to a bug in gtk, x,y are always 0
237 if ((m_height
== height
) && (m_width
== width
) &&
239 if (!m_wxwindow
) return;
244 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
245 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
246 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_minWidth
;
247 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_minHeight
;
249 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
253 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
254 event
.SetEventObject( this );
255 GetEventHandler()->ProcessEvent( event
);
258 void wxDialog::OnSize( wxSizeEvent
&WXUNUSED(event
) )
260 wxASSERT_MSG( (m_widget
!= NULL
), "invalid dialog" );
268 // no child: go out !
269 if (!GetChildren().First()) return;
271 // do we have exactly one child?
272 wxWindow
*child
= (wxWindow
*) NULL
;
273 for(wxNode
*node
= GetChildren().First(); node
; node
= node
->Next())
275 wxWindow
*win
= (wxWindow
*)node
->Data();
276 if (!wxIS_KIND_OF(win
,wxFrame
) && !wxIS_KIND_OF(win
,wxDialog
))
278 // it's the second one: do nothing
284 // yes: set it's size to fill all the frame
285 int client_x
, client_y
;
286 GetClientSize( &client_x
, &client_y
);
287 child
->SetSize( 1, 1, client_x
-2, client_y
);
291 void wxDialog::SetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
293 wxASSERT_MSG( (m_widget
!= NULL
), "invalid window" );
295 // Don't do anything for children of wxMDIChildFrame
296 if (!m_wxwindow
) return;
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_minWidth
;
334 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_minHeight
;
336 if ((m_x
!= -1) || (m_y
!= -1))
338 if ((m_x
!= old_x
) || (m_y
!= old_y
))
339 gtk_widget_set_uposition( m_widget
, m_x
, m_y
);
342 if ((m_width
!= old_width
) || (m_height
!= old_height
))
344 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
349 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
350 event
.SetEventObject( this );
351 GetEventHandler()->ProcessEvent( event
);
356 void wxDialog::SetSize( int width
, int height
)
358 SetSize( -1, -1, width
, height
, wxSIZE_USE_EXISTING
);
361 void wxDialog::Centre( int direction
)
363 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
368 if (direction
& wxHORIZONTAL
== wxHORIZONTAL
) x
= (gdk_screen_width () - m_width
) / 2;
369 if (direction
& wxVERTICAL
== wxVERTICAL
) y
= (gdk_screen_height () - m_height
) / 2;
374 bool wxDialog::Show( bool show
)
376 if (!show
&& IsModal())
378 EndModal( wxID_CANCEL
);
381 wxWindow::Show( show
);
383 if (show
) InitDialog();
388 bool wxDialog::IsModal() const
390 return m_modalShowing
;
393 void wxDialog::SetModal( bool WXUNUSED(flag
) )
397 m_windowStyle |= wxDIALOG_MODAL;
399 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
401 wxFAIL_MSG( "wxDialog:SetModal obsolete now" );
404 int wxDialog::ShowModal()
408 wxFAIL_MSG( "wxDialog:ShowModal called twice" );
409 return GetReturnCode();
414 m_modalShowing
= TRUE
;
416 gtk_grab_add( m_widget
);
418 gtk_grab_remove( m_widget
);
420 return GetReturnCode();
423 void wxDialog::EndModal( int retCode
)
425 SetReturnCode( retCode
);
429 wxFAIL_MSG( "wxDialog:EndModal called twice" );
433 m_modalShowing
= FALSE
;
440 void wxDialog::InitDialog()
442 wxWindow::InitDialog();
445 void wxDialog::SetIcon( const wxIcon
&icon
)
448 if (!icon
.Ok()) return;
450 wxMask
*mask
= icon
.GetMask();
451 GdkBitmap
*bm
= (GdkBitmap
*) NULL
;
452 if (mask
) bm
= mask
->GetBitmap();
454 gdk_window_set_icon( m_widget
->window
, (GdkWindow
*) NULL
, icon
.GetPixmap(), bm
);