]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/dialog.cpp
some really minor changes (the most important one: small memory hole in
[wxWidgets.git] / src / gtk1 / dialog.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: dialog.cpp
3// Purpose:
4// Author: Robert Roebling
a81258be 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
c801d85f
KB
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10#ifdef __GNUG__
11#pragma implementation "dialog.h"
12#endif
13
14#include "wx/dialog.h"
15#include "wx/frame.h"
16#include "wx/app.h"
83624f79
RR
17
18#include "gdk/gdk.h"
19#include "gtk/gtk.h"
c801d85f
KB
20#include "wx/gtk/win_gtk.h"
21
e2414cbe
RR
22//-----------------------------------------------------------------------------
23
24extern wxList wxPendingDelete;
25
c801d85f 26//-----------------------------------------------------------------------------
e1e955e1
RR
27// "delete_event"
28//-----------------------------------------------------------------------------
c801d85f
KB
29
30bool gtk_dialog_delete_callback( GtkWidget *WXUNUSED(widget), GdkEvent *WXUNUSED(event), wxDialog *win )
31{
32/*
fb1585ae
RR
33 printf( "OnDelete from " );
34 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
35 printf( win->GetClassInfo()->GetClassName() );
36 printf( ".\n" );
c801d85f
KB
37*/
38
fb1585ae 39 win->Close();
c801d85f 40
fb1585ae 41 return TRUE;
c33c4050 42}
c801d85f 43
e52f60e6
RR
44//-----------------------------------------------------------------------------
45// "size_allocate"
46//-----------------------------------------------------------------------------
47
48static void gtk_dialog_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxDialog *win )
49{
50 if (!win->HasVMT()) return;
51
52/*
53 printf( "OnDialogResize from " );
54 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
55 printf( win->GetClassInfo()->GetClassName() );
56 printf( ".\n" );
57*/
58
59 win->GtkOnSize( alloc->x, alloc->y, alloc->width, alloc->height );
60}
61
c801d85f
KB
62//-----------------------------------------------------------------------------
63// wxDialog
64//-----------------------------------------------------------------------------
65
a60c99e6 66BEGIN_EVENT_TABLE(wxDialog,wxPanel)
fb1585ae
RR
67 EVT_BUTTON (wxID_OK, wxDialog::OnOK)
68 EVT_BUTTON (wxID_CANCEL, wxDialog::OnCancel)
69 EVT_BUTTON (wxID_APPLY, wxDialog::OnApply)
e52f60e6 70 EVT_SIZE (wxDialog::OnSize)
fb1585ae 71 EVT_CLOSE (wxDialog::OnCloseWindow)
c801d85f
KB
72END_EVENT_TABLE()
73
a60c99e6 74IMPLEMENT_DYNAMIC_CLASS(wxDialog,wxPanel)
c801d85f 75
43a18898 76wxDialog::wxDialog()
c801d85f 77{
fb1585ae
RR
78 m_title = "";
79 m_modalShowing = FALSE;
c33c4050 80}
c801d85f
KB
81
82wxDialog::wxDialog( wxWindow *parent,
fb1585ae
RR
83 wxWindowID id, const wxString &title,
84 const wxPoint &pos, const wxSize &size,
85 long style, const wxString &name )
c801d85f 86{
fb1585ae 87 m_modalShowing = FALSE;
fb1585ae 88 Create( parent, id, title, pos, size, style, name );
c33c4050 89}
c801d85f
KB
90
91bool wxDialog::Create( wxWindow *parent,
fb1585ae
RR
92 wxWindowID id, const wxString &title,
93 const wxPoint &pos, const wxSize &size,
94 long style, const wxString &name )
c801d85f 95{
a802c3a1
RR
96 wxTopLevelWindows.Append( this );
97
fb1585ae 98 m_needParent = FALSE;
c801d85f 99
fb1585ae 100 PreCreation( parent, id, pos, size, style, name );
c801d85f 101
fb1585ae
RR
102 m_widget = gtk_window_new( GTK_WINDOW_TOPLEVEL );
103 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
c801d85f 104
fb1585ae 105 gtk_widget_set( m_widget, "GtkWindow::allow_shrink", TRUE, NULL);
c801d85f 106
fb1585ae
RR
107 gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event",
108 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback), (gpointer)this );
c801d85f 109
e52f60e6
RR
110 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
111 GTK_SIGNAL_FUNC(gtk_dialog_size_callback), (gpointer)this );
112
fb1585ae
RR
113 m_wxwindow = gtk_myfixed_new();
114 gtk_widget_show( m_wxwindow );
115 GTK_WIDGET_UNSET_FLAGS( m_wxwindow, GTK_CAN_FOCUS );
c801d85f 116
fb1585ae 117 gtk_container_add( GTK_CONTAINER(m_widget), m_wxwindow );
c801d85f 118
fb1585ae 119 SetTitle( title );
c801d85f 120
fb1585ae
RR
121 if ((m_x != -1) || (m_y != -1))
122 gtk_widget_set_uposition( m_widget, m_x, m_y );
903f689b 123
fb1585ae 124 gtk_widget_set_usize( m_widget, m_width, m_height );
903f689b 125
fb1585ae 126 if (m_parent) m_parent->AddChild( this );
6ca41e57
RR
127
128
fb1585ae 129 PostCreation();
c801d85f 130
fb1585ae 131 return TRUE;
c33c4050 132}
c801d85f 133
43a18898 134wxDialog::~wxDialog()
c801d85f 135{
fb1585ae
RR
136 wxTopLevelWindows.DeleteObject( this );
137 if (wxTopLevelWindows.Number() == 0) wxTheApp->ExitMainLoop();
c33c4050 138}
c801d85f 139
43a18898 140void wxDialog::SetTitle( const wxString& title )
c801d85f 141{
fb1585ae
RR
142 m_title = title;
143 if (m_title.IsNull()) m_title = "";
144 gtk_window_set_title( GTK_WINDOW(m_widget), m_title );
c33c4050 145}
c801d85f 146
43a18898 147wxString wxDialog::GetTitle() const
c801d85f 148{
fb1585ae 149 return (wxString&)m_title;
c33c4050 150}
c801d85f
KB
151
152void wxDialog::OnApply( wxCommandEvent &WXUNUSED(event) )
153{
fb1585ae 154 if (Validate()) TransferDataFromWindow();
c33c4050 155}
c801d85f
KB
156
157void wxDialog::OnCancel( wxCommandEvent &WXUNUSED(event) )
158{
fb1585ae
RR
159 if (IsModal())
160 {
161 EndModal(wxID_CANCEL);
162 }
163 else
164 {
165 SetReturnCode(wxID_CANCEL);
166 this->Show(FALSE);
167 }
c33c4050 168}
c801d85f 169
903f689b 170void wxDialog::OnOK( wxCommandEvent &WXUNUSED(event) )
c801d85f 171{
fb1585ae 172 if ( Validate() && TransferDataFromWindow())
1a6944fd 173 {
fb1585ae
RR
174 if (IsModal())
175 {
176 EndModal(wxID_OK);
177 }
178 else
179 {
180 SetReturnCode(wxID_OK);
181 this->Show(FALSE);
182 }
1a6944fd 183 }
c33c4050 184}
c801d85f
KB
185
186void wxDialog::OnPaint( wxPaintEvent& WXUNUSED(event) )
187{
188 // yes
c33c4050 189}
c801d85f 190
43a18898 191bool wxDialog::OnClose()
c801d85f 192{
fb1585ae 193 static wxList closing;
c801d85f 194
fb1585ae 195 if (closing.Member(this)) return FALSE; // no loops
1a6944fd 196
fb1585ae 197 closing.Append(this);
c801d85f 198
fb1585ae
RR
199 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
200 cancelEvent.SetEventObject( this );
201 GetEventHandler()->ProcessEvent(cancelEvent);
202 closing.DeleteObject(this);
c801d85f 203
fb1585ae 204 return FALSE;
c801d85f
KB
205}
206
43a18898 207bool wxDialog::Destroy()
e2414cbe 208{
fb1585ae 209 if (!wxPendingDelete.Member(this)) wxPendingDelete.Append(this);
e2414cbe 210
fb1585ae 211 return TRUE;
e2414cbe
RR
212}
213
43a18898 214void wxDialog::OnCloseWindow( wxCloseEvent& event )
c801d85f 215{
fb1585ae
RR
216 if (GetEventHandler()->OnClose() || event.GetForce())
217 {
218 this->Destroy();
219 }
c33c4050 220}
c801d85f 221
e52f60e6
RR
222void wxDialog::GtkOnSize( int WXUNUSED(x), int WXUNUSED(y), int width, int height )
223{
224 // due to a bug in gtk, x,y are always 0
225 // m_x = x;
226 // m_y = y;
227
228 if ((m_height == height) && (m_width == width) &&
229 (m_sizeSet)) return;
230 if (!m_wxwindow) return;
231
232 m_width = width;
233 m_height = height;
234
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;
239
240 gtk_widget_set_usize( m_widget, m_width, m_height );
241
242 m_sizeSet = TRUE;
243
244 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
245 event.SetEventObject( this );
246 GetEventHandler()->ProcessEvent( event );
247}
248
249void wxDialog::OnSize( wxSizeEvent &WXUNUSED(event) )
250{
2830bf19 251 wxASSERT_MSG( (m_widget != NULL), "invalid dialog" );
e52f60e6
RR
252
253 if (GetAutoLayout())
254 {
255 Layout();
256 }
257 else
258 {
259 // no child: go out !
db1b4961 260 if (!GetChildren().First()) return;
e52f60e6
RR
261
262 // do we have exactly one child?
263 wxWindow *child = (wxWindow *) NULL;
db1b4961 264 for(wxNode *node = GetChildren().First(); node; node = node->Next())
e52f60e6
RR
265 {
266 wxWindow *win = (wxWindow *)node->Data();
de135918 267 if (!wxIS_KIND_OF(win,wxFrame) && !wxIS_KIND_OF(win,wxDialog))
e52f60e6
RR
268 {
269 // it's the second one: do nothing
270 if (child) return;
271 child = win;
272 }
273 }
274
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);
279 }
280}
281
fb1585ae 282void wxDialog::SetSize( int x, int y, int width, int height, int sizeFlags )
903f689b 283{
fb1585ae
RR
284 wxASSERT_MSG( (m_widget != NULL), "invalid window" );
285
286 // Don't do anything for children of wxMDIChildFrame
287 if (!m_wxwindow) return;
288
289 if (m_resizing) return; // I don't like recursions
290 m_resizing = TRUE;
291
292 int old_x = m_x;
293 int old_y = m_y;
294 int old_width = m_width;
295 int old_height = m_height;
296
297 if ((sizeFlags & wxSIZE_USE_EXISTING) == wxSIZE_USE_EXISTING)
298 {
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;
303 }
304 else
305 {
306 m_x = x;
307 m_y = y;
308 m_width = width;
309 m_height = height;
310 }
311
312 if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH)
313 {
314 if (width == -1) m_width = 80;
315 }
316
317 if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT)
318 {
319 if (height == -1) m_height = 26;
320 }
321
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;
326
327 if ((m_x != -1) || (m_y != -1))
328 {
329 if ((m_x != old_x) || (m_y != old_y))
330 gtk_widget_set_uposition( m_widget, m_x, m_y );
331 }
332
333 if ((m_width != old_width) || (m_height != old_height))
334 {
335 gtk_widget_set_usize( m_widget, m_width, m_height );
336 }
337
338 m_sizeSet = TRUE;
339
340 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
341 event.SetEventObject( this );
e52f60e6 342 GetEventHandler()->ProcessEvent( event );
fb1585ae
RR
343
344 m_resizing = FALSE;
903f689b
RR
345}
346
43a18898
RR
347void wxDialog::SetSize( int width, int height )
348{
349 SetSize( -1, -1, width, height, wxSIZE_USE_EXISTING );
350}
351
903f689b
RR
352void wxDialog::Centre( int direction )
353{
fb1585ae
RR
354 wxASSERT_MSG( (m_widget != NULL), "invalid frame" );
355
43a18898
RR
356 int x = 0;
357 int y = 0;
fb1585ae
RR
358
359 if (direction & wxHORIZONTAL == wxHORIZONTAL) x = (gdk_screen_width () - m_width) / 2;
360 if (direction & wxVERTICAL == wxVERTICAL) y = (gdk_screen_height () - m_height) / 2;
361
362 Move( x, y );
903f689b
RR
363}
364
debe6624 365bool wxDialog::Show( bool show )
c801d85f 366{
fb1585ae
RR
367 if (!show && IsModal())
368 {
369 EndModal( wxID_CANCEL );
370 }
c801d85f 371
fb1585ae 372 wxWindow::Show( show );
c801d85f 373
fb1585ae 374 if (show) InitDialog();
c801d85f 375
fb1585ae 376 return TRUE;
c33c4050
RR
377}
378
43a18898 379bool wxDialog::IsModal() const
e1e955e1 380{
fb1585ae 381 return m_modalShowing;
e1e955e1
RR
382}
383
384void wxDialog::SetModal( bool WXUNUSED(flag) )
c33c4050 385{
e1e955e1 386/*
c33c4050
RR
387 if (flag)
388 m_windowStyle |= wxDIALOG_MODAL;
389 else
390 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
e1e955e1 391*/
fb1585ae 392 wxFAIL_MSG( "wxDialog:SetModal obsolete now" );
c33c4050 393}
c801d85f 394
43a18898 395int wxDialog::ShowModal()
c801d85f 396{
fb1585ae
RR
397 if (IsModal())
398 {
399 wxFAIL_MSG( "wxDialog:ShowModal called twice" );
400 return GetReturnCode();
401 }
d355d3fe 402
fb1585ae 403 Show( TRUE );
d355d3fe 404
fb1585ae 405 m_modalShowing = TRUE;
d355d3fe 406
fb1585ae
RR
407 gtk_grab_add( m_widget );
408 gtk_main();
409 gtk_grab_remove( m_widget );
d355d3fe 410
fb1585ae 411 return GetReturnCode();
c33c4050 412}
c801d85f
KB
413
414void wxDialog::EndModal( int retCode )
415{
fb1585ae 416 SetReturnCode( retCode );
d355d3fe 417
fb1585ae
RR
418 if (!IsModal())
419 {
420 wxFAIL_MSG( "wxDialog:EndModal called twice" );
421 return;
422 }
b6af8d80 423
fb1585ae 424 m_modalShowing = FALSE;
d355d3fe 425
fb1585ae 426 gtk_main_quit();
5b011451 427
fb1585ae 428 Show( FALSE );
c33c4050 429}
c801d85f 430
43a18898 431void wxDialog::InitDialog()
c801d85f 432{
fb1585ae 433 wxWindow::InitDialog();
c33c4050
RR
434}
435
c33c4050
RR
436void wxDialog::SetIcon( const wxIcon &icon )
437{
fb1585ae
RR
438 m_icon = icon;
439 if (!icon.Ok()) return;
c33c4050 440
fb1585ae
RR
441 wxMask *mask = icon.GetMask();
442 GdkBitmap *bm = (GdkBitmap *) NULL;
443 if (mask) bm = mask->GetBitmap();
c33c4050 444
fb1585ae 445 gdk_window_set_icon( m_widget->window, (GdkWindow *) NULL, icon.GetPixmap(), bm );
c33c4050 446}