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 );
137 if (wxTopLevelWindows
.Number() == 0) wxTheApp
->ExitMainLoop();
140 void wxDialog::SetTitle( const wxString
& title
)
143 if (m_title
.IsNull()) m_title
= "";
144 gtk_window_set_title( GTK_WINDOW(m_widget
), m_title
);
147 wxString
wxDialog::GetTitle() const
149 return (wxString
&)m_title
;
152 void wxDialog::OnApply( wxCommandEvent
&WXUNUSED(event
) )
154 if (Validate()) TransferDataFromWindow();
157 void wxDialog::OnCancel( wxCommandEvent
&WXUNUSED(event
) )
161 EndModal(wxID_CANCEL
);
165 SetReturnCode(wxID_CANCEL
);
170 void wxDialog::OnOK( wxCommandEvent
&WXUNUSED(event
) )
172 if ( Validate() && TransferDataFromWindow())
180 SetReturnCode(wxID_OK
);
186 void wxDialog::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
191 bool wxDialog::OnClose()
193 static wxList closing
;
195 if (closing
.Member(this)) return FALSE
; // no loops
197 closing
.Append(this);
199 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
200 cancelEvent
.SetEventObject( this );
201 GetEventHandler()->ProcessEvent(cancelEvent
);
202 closing
.DeleteObject(this);
207 bool wxDialog::Destroy()
209 if (!wxPendingDelete
.Member(this)) wxPendingDelete
.Append(this);
214 void wxDialog::OnCloseWindow( wxCloseEvent
& event
)
216 if (GetEventHandler()->OnClose() || event
.GetForce())
222 void wxDialog::GtkOnSize( int WXUNUSED(x
), int WXUNUSED(y
), int width
, int height
)
224 // due to a bug in gtk, x,y are always 0
228 if ((m_height
== height
) && (m_width
== width
) &&
230 if (!m_wxwindow
) return;
235 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
236 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
237 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_minWidth
;
238 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_minHeight
;
240 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
244 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
245 event
.SetEventObject( this );
246 GetEventHandler()->ProcessEvent( event
);
249 void wxDialog::OnSize( wxSizeEvent
&WXUNUSED(event
) )
251 wxASSERT_MSG( (m_widget
!= NULL
), "invalid dialog" );
259 // no child: go out !
260 if (!GetChildren().First()) return;
262 // do we have exactly one child?
263 wxWindow
*child
= (wxWindow
*) NULL
;
264 for(wxNode
*node
= GetChildren().First(); node
; node
= node
->Next())
266 wxWindow
*win
= (wxWindow
*)node
->Data();
267 if (!wxIS_KIND_OF(win
,wxFrame
) && !wxIS_KIND_OF(win
,wxDialog
))
269 // it's the second one: do nothing
275 // yes: set it's size to fill all the frame
276 int client_x
, client_y
;
277 GetClientSize( &client_x
, &client_y
);
278 child
->SetSize( 1, 1, client_x
-2, client_y
);
282 void wxDialog::SetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
284 wxASSERT_MSG( (m_widget
!= NULL
), "invalid window" );
286 // Don't do anything for children of wxMDIChildFrame
287 if (!m_wxwindow
) return;
289 if (m_resizing
) return; // I don't like recursions
294 int old_width
= m_width
;
295 int old_height
= m_height
;
297 if ((sizeFlags
& wxSIZE_USE_EXISTING
) == wxSIZE_USE_EXISTING
)
299 if (x
!= -1) m_x
= x
;
300 if (y
!= -1) m_y
= y
;
301 if (width
!= -1) m_width
= width
;
302 if (height
!= -1) m_height
= height
;
312 if ((sizeFlags
& wxSIZE_AUTO_WIDTH
) == wxSIZE_AUTO_WIDTH
)
314 if (width
== -1) m_width
= 80;
317 if ((sizeFlags
& wxSIZE_AUTO_HEIGHT
) == wxSIZE_AUTO_HEIGHT
)
319 if (height
== -1) m_height
= 26;
322 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
323 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
324 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_minWidth
;
325 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_minHeight
;
327 if ((m_x
!= -1) || (m_y
!= -1))
329 if ((m_x
!= old_x
) || (m_y
!= old_y
))
330 gtk_widget_set_uposition( m_widget
, m_x
, m_y
);
333 if ((m_width
!= old_width
) || (m_height
!= old_height
))
335 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
340 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
341 event
.SetEventObject( this );
342 GetEventHandler()->ProcessEvent( event
);
347 void wxDialog::SetSize( int width
, int height
)
349 SetSize( -1, -1, width
, height
, wxSIZE_USE_EXISTING
);
352 void wxDialog::Centre( int direction
)
354 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
359 if (direction
& wxHORIZONTAL
== wxHORIZONTAL
) x
= (gdk_screen_width () - m_width
) / 2;
360 if (direction
& wxVERTICAL
== wxVERTICAL
) y
= (gdk_screen_height () - m_height
) / 2;
365 bool wxDialog::Show( bool show
)
367 if (!show
&& IsModal())
369 EndModal( wxID_CANCEL
);
372 wxWindow::Show( show
);
374 if (show
) InitDialog();
379 bool wxDialog::IsModal() const
381 return m_modalShowing
;
384 void wxDialog::SetModal( bool WXUNUSED(flag
) )
388 m_windowStyle |= wxDIALOG_MODAL;
390 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
392 wxFAIL_MSG( "wxDialog:SetModal obsolete now" );
395 int wxDialog::ShowModal()
399 wxFAIL_MSG( "wxDialog:ShowModal called twice" );
400 return GetReturnCode();
405 m_modalShowing
= TRUE
;
407 gtk_grab_add( m_widget
);
409 gtk_grab_remove( m_widget
);
411 return GetReturnCode();
414 void wxDialog::EndModal( int retCode
)
416 SetReturnCode( retCode
);
420 wxFAIL_MSG( "wxDialog:EndModal called twice" );
424 m_modalShowing
= FALSE
;
431 void wxDialog::InitDialog()
433 wxWindow::InitDialog();
436 void wxDialog::SetIcon( const wxIcon
&icon
)
439 if (!icon
.Ok()) return;
441 wxMask
*mask
= icon
.GetMask();
442 GdkBitmap
*bm
= (GdkBitmap
*) NULL
;
443 if (mask
) bm
= mask
->GetBitmap();
445 gdk_window_set_icon( m_widget
->window
, (GdkWindow
*) NULL
, icon
.GetPixmap(), bm
);