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"
17 #include "wx/gtk/win_gtk.h"
19 //-----------------------------------------------------------------------------
21 extern wxList wxPendingDelete
;
23 //-----------------------------------------------------------------------------
25 //-----------------------------------------------------------------------------
27 bool gtk_dialog_delete_callback( GtkWidget
*WXUNUSED(widget
), GdkEvent
*WXUNUSED(event
), wxDialog
*win
)
30 printf( "OnDelete from " );
31 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
32 printf( win->GetClassInfo()->GetClassName() );
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 static void gtk_dialog_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxDialog
*win
)
47 if (!win
->HasVMT()) return;
50 printf( "OnDialogResize from " );
51 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
52 printf( win->GetClassInfo()->GetClassName() );
56 win
->GtkOnSize( alloc
->x
, alloc
->y
, alloc
->width
, alloc
->height
);
59 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
63 BEGIN_EVENT_TABLE(wxDialog
,wxPanel
)
64 EVT_BUTTON (wxID_OK
, wxDialog::OnOK
)
65 EVT_BUTTON (wxID_CANCEL
, wxDialog::OnCancel
)
66 EVT_BUTTON (wxID_APPLY
, wxDialog::OnApply
)
67 EVT_SIZE (wxDialog::OnSize
)
68 EVT_CLOSE (wxDialog::OnCloseWindow
)
71 IMPLEMENT_DYNAMIC_CLASS(wxDialog
,wxPanel
)
76 m_modalShowing
= FALSE
;
79 wxDialog::wxDialog( wxWindow
*parent
,
80 wxWindowID id
, const wxString
&title
,
81 const wxPoint
&pos
, const wxSize
&size
,
82 long style
, const wxString
&name
)
84 m_modalShowing
= FALSE
;
85 Create( parent
, id
, title
, pos
, size
, style
, name
);
88 bool wxDialog::Create( wxWindow
*parent
,
89 wxWindowID id
, const wxString
&title
,
90 const wxPoint
&pos
, const wxSize
&size
,
91 long style
, const wxString
&name
)
93 wxTopLevelWindows
.Append( this );
97 PreCreation( parent
, id
, pos
, size
, style
, name
);
99 m_widget
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
100 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
102 gtk_widget_set( m_widget
, "GtkWindow::allow_shrink", TRUE
, NULL
);
104 gtk_signal_connect( GTK_OBJECT(m_widget
), "delete_event",
105 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback
), (gpointer
)this );
107 gtk_signal_connect( GTK_OBJECT(m_widget
), "size_allocate",
108 GTK_SIGNAL_FUNC(gtk_dialog_size_callback
), (gpointer
)this );
110 m_wxwindow
= gtk_myfixed_new();
111 gtk_widget_show( m_wxwindow
);
112 GTK_WIDGET_UNSET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
114 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
118 if ((m_x
!= -1) || (m_y
!= -1))
119 gtk_widget_set_uposition( m_widget
, m_x
, m_y
);
121 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
123 if (m_parent
) m_parent
->AddChild( this );
131 wxDialog::~wxDialog()
133 wxTopLevelWindows
.DeleteObject( this );
134 if (wxTopLevelWindows
.Number() == 0) wxTheApp
->ExitMainLoop();
137 void wxDialog::SetTitle( const wxString
& title
)
140 if (m_title
.IsNull()) m_title
= "";
141 gtk_window_set_title( GTK_WINDOW(m_widget
), m_title
);
144 wxString
wxDialog::GetTitle() const
146 return (wxString
&)m_title
;
149 void wxDialog::OnApply( wxCommandEvent
&WXUNUSED(event
) )
151 if (Validate()) TransferDataFromWindow();
154 void wxDialog::OnCancel( wxCommandEvent
&WXUNUSED(event
) )
158 EndModal(wxID_CANCEL
);
162 SetReturnCode(wxID_CANCEL
);
167 void wxDialog::OnOK( wxCommandEvent
&WXUNUSED(event
) )
169 if ( Validate() && TransferDataFromWindow())
177 SetReturnCode(wxID_OK
);
183 void wxDialog::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
188 bool wxDialog::OnClose()
190 static wxList closing
;
192 if (closing
.Member(this)) return FALSE
; // no loops
194 closing
.Append(this);
196 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
197 cancelEvent
.SetEventObject( this );
198 GetEventHandler()->ProcessEvent(cancelEvent
);
199 closing
.DeleteObject(this);
204 bool wxDialog::Destroy()
206 if (!wxPendingDelete
.Member(this)) wxPendingDelete
.Append(this);
211 void wxDialog::OnCloseWindow( wxCloseEvent
& event
)
213 if (GetEventHandler()->OnClose() || event
.GetForce())
219 void wxDialog::GtkOnSize( int WXUNUSED(x
), int WXUNUSED(y
), int width
, int height
)
221 // due to a bug in gtk, x,y are always 0
225 if ((m_height
== height
) && (m_width
== width
) &&
227 if (!m_wxwindow
) return;
232 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
233 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
234 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_minWidth
;
235 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_minHeight
;
237 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
241 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
242 event
.SetEventObject( this );
243 GetEventHandler()->ProcessEvent( event
);
246 void wxDialog::OnSize( wxSizeEvent
&WXUNUSED(event
) )
248 wxASSERT_MSG( (m_widget
!= NULL
), "invalid dialog" );
256 // no child: go out !
257 if (!GetChildren().First()) return;
259 // do we have exactly one child?
260 wxWindow
*child
= (wxWindow
*) NULL
;
261 for(wxNode
*node
= GetChildren().First(); node
; node
= node
->Next())
263 wxWindow
*win
= (wxWindow
*)node
->Data();
264 if (!wxIS_KIND_OF(win
,wxFrame
) && !wxIS_KIND_OF(win
,wxDialog
))
266 // it's the second one: do nothing
272 // yes: set it's size to fill all the frame
273 int client_x
, client_y
;
274 GetClientSize( &client_x
, &client_y
);
275 child
->SetSize( 1, 1, client_x
-2, client_y
);
279 void wxDialog::SetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
281 wxASSERT_MSG( (m_widget
!= NULL
), "invalid window" );
283 // Don't do anything for children of wxMDIChildFrame
284 if (!m_wxwindow
) return;
286 if (m_resizing
) return; // I don't like recursions
291 int old_width
= m_width
;
292 int old_height
= m_height
;
294 if ((sizeFlags
& wxSIZE_USE_EXISTING
) == wxSIZE_USE_EXISTING
)
296 if (x
!= -1) m_x
= x
;
297 if (y
!= -1) m_y
= y
;
298 if (width
!= -1) m_width
= width
;
299 if (height
!= -1) m_height
= height
;
309 if ((sizeFlags
& wxSIZE_AUTO_WIDTH
) == wxSIZE_AUTO_WIDTH
)
311 if (width
== -1) m_width
= 80;
314 if ((sizeFlags
& wxSIZE_AUTO_HEIGHT
) == wxSIZE_AUTO_HEIGHT
)
316 if (height
== -1) m_height
= 26;
319 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
320 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
321 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_minWidth
;
322 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_minHeight
;
324 if ((m_x
!= -1) || (m_y
!= -1))
326 if ((m_x
!= old_x
) || (m_y
!= old_y
))
327 gtk_widget_set_uposition( m_widget
, m_x
, m_y
);
330 if ((m_width
!= old_width
) || (m_height
!= old_height
))
332 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
337 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
338 event
.SetEventObject( this );
339 GetEventHandler()->ProcessEvent( event
);
344 void wxDialog::SetSize( int width
, int height
)
346 SetSize( -1, -1, width
, height
, wxSIZE_USE_EXISTING
);
349 void wxDialog::Centre( int direction
)
351 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
356 if (direction
& wxHORIZONTAL
== wxHORIZONTAL
) x
= (gdk_screen_width () - m_width
) / 2;
357 if (direction
& wxVERTICAL
== wxVERTICAL
) y
= (gdk_screen_height () - m_height
) / 2;
362 bool wxDialog::Show( bool show
)
364 if (!show
&& IsModal())
366 EndModal( wxID_CANCEL
);
369 wxWindow::Show( show
);
371 if (show
) InitDialog();
376 bool wxDialog::IsModal() const
378 return m_modalShowing
;
381 void wxDialog::SetModal( bool WXUNUSED(flag
) )
385 m_windowStyle |= wxDIALOG_MODAL;
387 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
389 wxFAIL_MSG( "wxDialog:SetModal obsolete now" );
392 int wxDialog::ShowModal()
396 wxFAIL_MSG( "wxDialog:ShowModal called twice" );
397 return GetReturnCode();
402 m_modalShowing
= TRUE
;
404 gtk_grab_add( m_widget
);
406 gtk_grab_remove( m_widget
);
408 return GetReturnCode();
411 void wxDialog::EndModal( int retCode
)
413 SetReturnCode( retCode
);
417 wxFAIL_MSG( "wxDialog:EndModal called twice" );
421 m_modalShowing
= FALSE
;
428 void wxDialog::InitDialog()
430 wxWindow::InitDialog();
433 void wxDialog::SetIcon( const wxIcon
&icon
)
436 if (!icon
.Ok()) return;
438 wxMask
*mask
= icon
.GetMask();
439 GdkBitmap
*bm
= (GdkBitmap
*) NULL
;
440 if (mask
) bm
= mask
->GetBitmap();
442 gdk_window_set_icon( m_widget
->window
, (GdkWindow
*) NULL
, icon
.GetPixmap(), bm
);