]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/dialog.cpp
Added test for long long type. (Really trivial, see setup.h: SIZEOF_xxx)
[wxWidgets.git] / src / gtk / 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
2b854a32 7// Licence: wxWindows licence
c801d85f
KB
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 )
2b854a32 31{
c801d85f 32/*
fb1585ae
RR
33 printf( "OnDelete from " );
34 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
35 printf( win->GetClassInfo()->GetClassName() );
36 printf( ".\n" );
c801d85f 37*/
2b854a32 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
36b3b54a
RR
62//-----------------------------------------------------------------------------
63// "configure_event"
64//-----------------------------------------------------------------------------
65
66static gint gtk_dialog_configure_callback( GtkWidget *WXUNUSED(widget), GdkEventConfigure *event, wxDialog *win )
67{
68 if (!win->HasVMT()) return FALSE;
69
70 win->m_x = event->x;
71 win->m_y = event->y;
72
73 wxMoveEvent mevent( wxPoint(win->m_x,win->m_y), win->GetId() );
74 mevent.SetEventObject( win );
75 win->GetEventHandler()->ProcessEvent( mevent );
76
77 return FALSE;
78}
79
c801d85f
KB
80//-----------------------------------------------------------------------------
81// wxDialog
82//-----------------------------------------------------------------------------
83
a60c99e6 84BEGIN_EVENT_TABLE(wxDialog,wxPanel)
fb1585ae
RR
85 EVT_BUTTON (wxID_OK, wxDialog::OnOK)
86 EVT_BUTTON (wxID_CANCEL, wxDialog::OnCancel)
87 EVT_BUTTON (wxID_APPLY, wxDialog::OnApply)
e52f60e6 88 EVT_SIZE (wxDialog::OnSize)
fb1585ae 89 EVT_CLOSE (wxDialog::OnCloseWindow)
c801d85f
KB
90END_EVENT_TABLE()
91
a60c99e6 92IMPLEMENT_DYNAMIC_CLASS(wxDialog,wxPanel)
c801d85f 93
43a18898 94wxDialog::wxDialog()
c801d85f 95{
fb1585ae
RR
96 m_title = "";
97 m_modalShowing = FALSE;
c33c4050 98}
c801d85f 99
2b854a32 100wxDialog::wxDialog( wxWindow *parent,
fb1585ae 101 wxWindowID id, const wxString &title,
2b854a32 102 const wxPoint &pos, const wxSize &size,
fb1585ae 103 long style, const wxString &name )
c801d85f 104{
fb1585ae 105 m_modalShowing = FALSE;
fb1585ae 106 Create( parent, id, title, pos, size, style, name );
c33c4050 107}
c801d85f
KB
108
109bool wxDialog::Create( wxWindow *parent,
fb1585ae 110 wxWindowID id, const wxString &title,
2b854a32 111 const wxPoint &pos, const wxSize &size,
fb1585ae 112 long style, const wxString &name )
c801d85f 113{
a802c3a1 114 wxTopLevelWindows.Append( this );
2b854a32 115
fb1585ae 116 m_needParent = FALSE;
2b854a32 117
fb1585ae 118 PreCreation( parent, id, pos, size, style, name );
2b854a32 119
fb1585ae
RR
120 m_widget = gtk_window_new( GTK_WINDOW_TOPLEVEL );
121 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
2b854a32 122
fb1585ae 123 gtk_widget_set( m_widget, "GtkWindow::allow_shrink", TRUE, NULL);
2b854a32
VZ
124
125 gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event",
fb1585ae 126 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback), (gpointer)this );
2b854a32 127
fb1585ae
RR
128 m_wxwindow = gtk_myfixed_new();
129 gtk_widget_show( m_wxwindow );
130 GTK_WIDGET_UNSET_FLAGS( m_wxwindow, GTK_CAN_FOCUS );
2b854a32 131
fb1585ae 132 gtk_container_add( GTK_CONTAINER(m_widget), m_wxwindow );
2b854a32 133
fb1585ae 134 SetTitle( title );
2b854a32 135
fb1585ae
RR
136 if ((m_x != -1) || (m_y != -1))
137 gtk_widget_set_uposition( m_widget, m_x, m_y );
2b854a32 138
fb1585ae 139 gtk_widget_set_usize( m_widget, m_width, m_height );
2b854a32
VZ
140
141 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
36b3b54a 142 GTK_SIGNAL_FUNC(gtk_dialog_size_callback), (gpointer)this );
2b854a32 143
36b3b54a
RR
144 gtk_signal_connect( GTK_OBJECT(m_widget), "configure_event",
145 GTK_SIGNAL_FUNC(gtk_dialog_configure_callback), (gpointer)this );
146
fb1585ae 147 if (m_parent) m_parent->AddChild( this );
2b854a32 148
fb1585ae 149 PostCreation();
2b854a32 150
fb1585ae 151 return TRUE;
c33c4050 152}
c801d85f 153
43a18898 154wxDialog::~wxDialog()
c801d85f 155{
fb1585ae 156 wxTopLevelWindows.DeleteObject( this );
2b854a32 157
0d2a2b60
RR
158 if (wxTheApp->GetTopWindow() == this)
159 {
160 wxTheApp->SetTopWindow( (wxWindow*) NULL );
161 }
2b854a32 162
0d2a2b60 163 if (wxTopLevelWindows.Number() == 0)
2b854a32 164 {
0d2a2b60
RR
165 wxTheApp->ExitMainLoop();
166 }
c33c4050 167}
c801d85f 168
43a18898 169void wxDialog::SetTitle( const wxString& title )
c801d85f 170{
fb1585ae
RR
171 m_title = title;
172 if (m_title.IsNull()) m_title = "";
173 gtk_window_set_title( GTK_WINDOW(m_widget), m_title );
c33c4050 174}
c801d85f 175
43a18898 176wxString wxDialog::GetTitle() const
c801d85f 177{
fb1585ae 178 return (wxString&)m_title;
c33c4050 179}
c801d85f
KB
180
181void wxDialog::OnApply( wxCommandEvent &WXUNUSED(event) )
182{
fb1585ae 183 if (Validate()) TransferDataFromWindow();
c33c4050 184}
c801d85f
KB
185
186void wxDialog::OnCancel( wxCommandEvent &WXUNUSED(event) )
187{
fb1585ae
RR
188 if (IsModal())
189 {
190 EndModal(wxID_CANCEL);
191 }
192 else
193 {
194 SetReturnCode(wxID_CANCEL);
195 this->Show(FALSE);
196 }
c33c4050 197}
c801d85f 198
903f689b 199void wxDialog::OnOK( wxCommandEvent &WXUNUSED(event) )
c801d85f 200{
fb1585ae 201 if ( Validate() && TransferDataFromWindow())
1a6944fd 202 {
2b854a32 203 if (IsModal())
fb1585ae
RR
204 {
205 EndModal(wxID_OK);
206 }
207 else
208 {
209 SetReturnCode(wxID_OK);
210 this->Show(FALSE);
211 }
1a6944fd 212 }
c33c4050 213}
c801d85f
KB
214
215void wxDialog::OnPaint( wxPaintEvent& WXUNUSED(event) )
216{
2b854a32 217 // yes
c33c4050 218}
c801d85f 219
2b854a32 220void wxDialog::OnCloseWindow(wxCloseEvent& event)
c801d85f 221{
e3065973
JS
222 // We'll send a Cancel message by default,
223 // which may close the dialog.
224 // Check for looping if the Cancel event handler calls Close().
225
226 // Note that if a cancel button and handler aren't present in the dialog,
227 // nothing will happen when you close the dialog via the window manager, or
228 // via Close().
229 // We wouldn't want to destroy the dialog by default, since the dialog may have been
230 // created on the stack.
231 // However, this does mean that calling dialog->Close() won't delete the dialog
232 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
233 // sure to destroy the dialog.
234 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
235
fb1585ae 236 static wxList closing;
c801d85f 237
2b854a32
VZ
238 if (closing.Member(this))
239 return; // no loops
240
fb1585ae 241 closing.Append(this);
c801d85f 242
fb1585ae
RR
243 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
244 cancelEvent.SetEventObject( this );
245 GetEventHandler()->ProcessEvent(cancelEvent);
246 closing.DeleteObject(this);
c801d85f
KB
247}
248
43a18898 249bool wxDialog::Destroy()
e2414cbe 250{
fb1585ae 251 if (!wxPendingDelete.Member(this)) wxPendingDelete.Append(this);
e2414cbe 252
fb1585ae 253 return TRUE;
e2414cbe
RR
254}
255
e52f60e6
RR
256void wxDialog::GtkOnSize( int WXUNUSED(x), int WXUNUSED(y), int width, int height )
257{
258 // due to a bug in gtk, x,y are always 0
259 // m_x = x;
260 // m_y = y;
261
262 if ((m_height == height) && (m_width == width) &&
263 (m_sizeSet)) return;
264 if (!m_wxwindow) return;
2b854a32 265
e52f60e6
RR
266 m_width = width;
267 m_height = height;
2b854a32 268
e52f60e6
RR
269 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
270 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
0c77152e
RR
271 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_maxWidth;
272 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_maxHeight;
e52f60e6
RR
273
274 gtk_widget_set_usize( m_widget, m_width, m_height );
275
276 m_sizeSet = TRUE;
2b854a32 277
e52f60e6
RR
278 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
279 event.SetEventObject( this );
280 GetEventHandler()->ProcessEvent( event );
281}
282
283void wxDialog::OnSize( wxSizeEvent &WXUNUSED(event) )
284{
2830bf19 285 wxASSERT_MSG( (m_widget != NULL), "invalid dialog" );
2b854a32 286
e52f60e6
RR
287 if (GetAutoLayout())
288 {
289 Layout();
290 }
2b854a32 291 else
e52f60e6
RR
292 {
293 // no child: go out !
db1b4961 294 if (!GetChildren().First()) return;
2b854a32 295
e52f60e6
RR
296 // do we have exactly one child?
297 wxWindow *child = (wxWindow *) NULL;
db1b4961 298 for(wxNode *node = GetChildren().First(); node; node = node->Next())
e52f60e6
RR
299 {
300 wxWindow *win = (wxWindow *)node->Data();
de135918 301 if (!wxIS_KIND_OF(win,wxFrame) && !wxIS_KIND_OF(win,wxDialog))
e52f60e6 302 {
2b854a32 303 // it's the second one: do nothing
e52f60e6
RR
304 if (child) return;
305 child = win;
306 }
307 }
308
309 // yes: set it's size to fill all the frame
310 int client_x, client_y;
311 GetClientSize( &client_x, &client_y );
312 child->SetSize( 1, 1, client_x-2, client_y);
313 }
314}
315
fb1585ae 316void wxDialog::SetSize( int x, int y, int width, int height, int sizeFlags )
903f689b 317{
fb1585ae 318 wxASSERT_MSG( (m_widget != NULL), "invalid window" );
2b854a32 319
fb1585ae
RR
320 // Don't do anything for children of wxMDIChildFrame
321 if (!m_wxwindow) return;
322
323 if (m_resizing) return; // I don't like recursions
324 m_resizing = TRUE;
325
326 int old_x = m_x;
327 int old_y = m_y;
328 int old_width = m_width;
329 int old_height = m_height;
2b854a32 330
fb1585ae
RR
331 if ((sizeFlags & wxSIZE_USE_EXISTING) == wxSIZE_USE_EXISTING)
332 {
333 if (x != -1) m_x = x;
334 if (y != -1) m_y = y;
335 if (width != -1) m_width = width;
336 if (height != -1) m_height = height;
337 }
338 else
339 {
340 m_x = x;
341 m_y = y;
342 m_width = width;
343 m_height = height;
344 }
345
346 if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH)
347 {
348 if (width == -1) m_width = 80;
349 }
350
351 if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT)
352 {
353 if (height == -1) m_height = 26;
354 }
2b854a32 355
fb1585ae
RR
356 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
357 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
0c77152e
RR
358 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_maxWidth;
359 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_maxHeight;
fb1585ae
RR
360
361 if ((m_x != -1) || (m_y != -1))
362 {
2b854a32 363 if ((m_x != old_x) || (m_y != old_y))
fb1585ae
RR
364 gtk_widget_set_uposition( m_widget, m_x, m_y );
365 }
2b854a32 366
fb1585ae
RR
367 if ((m_width != old_width) || (m_height != old_height))
368 {
369 gtk_widget_set_usize( m_widget, m_width, m_height );
370 }
2b854a32 371
fb1585ae
RR
372 m_sizeSet = TRUE;
373
374 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
375 event.SetEventObject( this );
e52f60e6 376 GetEventHandler()->ProcessEvent( event );
fb1585ae
RR
377
378 m_resizing = FALSE;
903f689b
RR
379}
380
43a18898
RR
381void wxDialog::SetSize( int width, int height )
382{
383 SetSize( -1, -1, width, height, wxSIZE_USE_EXISTING );
384}
385
903f689b
RR
386void wxDialog::Centre( int direction )
387{
fb1585ae 388 wxASSERT_MSG( (m_widget != NULL), "invalid frame" );
2b854a32 389
43a18898
RR
390 int x = 0;
391 int y = 0;
2b854a32 392
fb1585ae
RR
393 if (direction & wxHORIZONTAL == wxHORIZONTAL) x = (gdk_screen_width () - m_width) / 2;
394 if (direction & wxVERTICAL == wxVERTICAL) y = (gdk_screen_height () - m_height) / 2;
2b854a32 395
fb1585ae 396 Move( x, y );
903f689b
RR
397}
398
debe6624 399bool wxDialog::Show( bool show )
c801d85f 400{
fb1585ae
RR
401 if (!show && IsModal())
402 {
403 EndModal( wxID_CANCEL );
404 }
c801d85f 405
fb1585ae 406 wxWindow::Show( show );
2b854a32 407
fb1585ae 408 if (show) InitDialog();
2b854a32 409
fb1585ae 410 return TRUE;
c33c4050
RR
411}
412
43a18898 413bool wxDialog::IsModal() const
e1e955e1 414{
fb1585ae 415 return m_modalShowing;
e1e955e1
RR
416}
417
418void wxDialog::SetModal( bool WXUNUSED(flag) )
c33c4050 419{
e1e955e1 420/*
c33c4050
RR
421 if (flag)
422 m_windowStyle |= wxDIALOG_MODAL;
423 else
424 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
e1e955e1 425*/
fb1585ae 426 wxFAIL_MSG( "wxDialog:SetModal obsolete now" );
c33c4050 427}
c801d85f 428
43a18898 429int wxDialog::ShowModal()
c801d85f 430{
fb1585ae
RR
431 if (IsModal())
432 {
433 wxFAIL_MSG( "wxDialog:ShowModal called twice" );
434 return GetReturnCode();
435 }
f234c60c 436
fb1585ae 437 Show( TRUE );
2b854a32 438
fb1585ae 439 m_modalShowing = TRUE;
2b854a32 440
fb1585ae
RR
441 gtk_grab_add( m_widget );
442 gtk_main();
443 gtk_grab_remove( m_widget );
2b854a32 444
fb1585ae 445 return GetReturnCode();
c33c4050 446}
c801d85f
KB
447
448void wxDialog::EndModal( int retCode )
449{
fb1585ae 450 SetReturnCode( retCode );
2b854a32 451
fb1585ae
RR
452 if (!IsModal())
453 {
454 wxFAIL_MSG( "wxDialog:EndModal called twice" );
455 return;
456 }
2b854a32 457
fb1585ae 458 m_modalShowing = FALSE;
2b854a32 459
fb1585ae 460 gtk_main_quit();
2b854a32 461
fb1585ae 462 Show( FALSE );
c33c4050 463}
c801d85f 464
43a18898 465void wxDialog::InitDialog()
c801d85f 466{
fb1585ae 467 wxWindow::InitDialog();
c33c4050
RR
468}
469
c33c4050
RR
470void wxDialog::SetIcon( const wxIcon &icon )
471{
fb1585ae
RR
472 m_icon = icon;
473 if (!icon.Ok()) return;
2b854a32 474
fb1585ae
RR
475 wxMask *mask = icon.GetMask();
476 GdkBitmap *bm = (GdkBitmap *) NULL;
477 if (mask) bm = mask->GetBitmap();
2b854a32 478
fb1585ae 479 gdk_window_set_icon( m_widget->window, (GdkWindow *) NULL, icon.GetPixmap(), bm );
c33c4050 480}