]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/dialog.cpp
Added the missing check for libw needed for wcslen() (Just defines in headers
[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
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
de8113d9
RR
59 if ((win->m_width != alloc->width) || (win->m_height != alloc->height))
60 {
61 win->m_sizeSet = FALSE;
62 win->m_width = alloc->width;
63 win->m_height = alloc->height;
64 }
e52f60e6
RR
65}
66
36b3b54a
RR
67//-----------------------------------------------------------------------------
68// "configure_event"
69//-----------------------------------------------------------------------------
70
71static gint gtk_dialog_configure_callback( GtkWidget *WXUNUSED(widget), GdkEventConfigure *event, wxDialog *win )
72{
73 if (!win->HasVMT()) return FALSE;
74
75 win->m_x = event->x;
76 win->m_y = event->y;
77
78 wxMoveEvent mevent( wxPoint(win->m_x,win->m_y), win->GetId() );
79 mevent.SetEventObject( win );
80 win->GetEventHandler()->ProcessEvent( mevent );
81
82 return FALSE;
83}
84
2b07d713
RR
85//-----------------------------------------------------------------------------
86// "realize" from m_widget
87//-----------------------------------------------------------------------------
88
58dea4b0 89/* we cannot MWM hints and icons before the widget has been realized,
2b07d713
RR
90 so we do this directly after realization */
91
92static gint
58dea4b0 93gtk_dialog_realized_callback( GtkWidget *widget, wxDialog *win )
2b07d713
RR
94{
95 /* all this is for Motif Window Manager "hints" and is supposed to be
96 recognized by other WM as well. not tested. */
97 long decor = (long) GDK_DECOR_ALL;
98 long func = (long) GDK_FUNC_ALL;
99
100 if ((win->m_windowStyle & wxCAPTION) == 0)
101 decor |= GDK_DECOR_TITLE;
102/* if ((win->m_windowStyle & wxMINIMIZE) == 0)
103 func |= GDK_FUNC_MINIMIZE;
104 if ((win->m_windowStyle & wxMAXIMIZE) == 0)
105 func |= GDK_FUNC_MAXIMIZE; */
106 if ((win->m_windowStyle & wxSYSTEM_MENU) == 0)
107 decor |= GDK_DECOR_MENU;
108 if ((win->m_windowStyle & wxMINIMIZE_BOX) == 0)
109 decor |= GDK_DECOR_MINIMIZE;
110 if ((win->m_windowStyle & wxMAXIMIZE_BOX) == 0)
111 decor |= GDK_DECOR_MAXIMIZE;
112 if ((win->m_windowStyle & wxRESIZE_BORDER) == 0)
113 func |= GDK_FUNC_RESIZE;
114
115 gdk_window_set_decorations( win->m_widget->window, (GdkWMDecoration)decor);
116 gdk_window_set_functions( win->m_widget->window, (GdkWMFunction)func);
117
118 /* GTK's shrinking/growing policy */
119 if ((win->m_windowStyle & wxRESIZE_BORDER) == 0)
120 gtk_window_set_policy(GTK_WINDOW(win->m_widget), 0, 0, 1);
121 else
122 gtk_window_set_policy(GTK_WINDOW(win->m_widget), 1, 1, 1);
123
58dea4b0
RR
124 /* reset the icon */
125 if (win->m_icon != wxNullIcon)
126 {
127 wxIcon icon( win->m_icon );
128 win->m_icon = wxNullIcon;
129 win->SetIcon( icon );
130 }
131
2b07d713
RR
132 return FALSE;
133}
134
227e5e99
RR
135//-----------------------------------------------------------------------------
136// "map" from m_widget
137//-----------------------------------------------------------------------------
138
139static gint
140gtk_dialog_map_callback( GtkWidget *widget, wxDialog *win )
141{
142 gtk_widget_set_uposition( widget, win->m_x, win->m_y );
143
144 return FALSE;
145}
146
c801d85f
KB
147//-----------------------------------------------------------------------------
148// wxDialog
149//-----------------------------------------------------------------------------
150
a60c99e6 151BEGIN_EVENT_TABLE(wxDialog,wxPanel)
fb1585ae
RR
152 EVT_BUTTON (wxID_OK, wxDialog::OnOK)
153 EVT_BUTTON (wxID_CANCEL, wxDialog::OnCancel)
154 EVT_BUTTON (wxID_APPLY, wxDialog::OnApply)
e52f60e6 155 EVT_SIZE (wxDialog::OnSize)
fb1585ae 156 EVT_CLOSE (wxDialog::OnCloseWindow)
c801d85f
KB
157END_EVENT_TABLE()
158
a60c99e6 159IMPLEMENT_DYNAMIC_CLASS(wxDialog,wxPanel)
c801d85f 160
68995f26 161void wxDialog::Init()
c801d85f 162{
de8113d9 163 m_sizeSet = FALSE;
fb1585ae 164 m_modalShowing = FALSE;
c33c4050 165}
c801d85f 166
2b854a32 167wxDialog::wxDialog( wxWindow *parent,
fb1585ae 168 wxWindowID id, const wxString &title,
2b854a32 169 const wxPoint &pos, const wxSize &size,
fb1585ae 170 long style, const wxString &name )
c801d85f 171{
68995f26
VZ
172 Init();
173
fb1585ae 174 Create( parent, id, title, pos, size, style, name );
c33c4050 175}
c801d85f
KB
176
177bool wxDialog::Create( wxWindow *parent,
fb1585ae 178 wxWindowID id, const wxString &title,
2b854a32 179 const wxPoint &pos, const wxSize &size,
fb1585ae 180 long style, const wxString &name )
c801d85f 181{
a802c3a1 182 wxTopLevelWindows.Append( this );
2b854a32 183
fb1585ae 184 m_needParent = FALSE;
2b854a32 185
fb1585ae 186 PreCreation( parent, id, pos, size, style, name );
2b854a32 187
fb1585ae
RR
188 m_widget = gtk_window_new( GTK_WINDOW_TOPLEVEL );
189 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
2b854a32 190
fb1585ae 191 gtk_widget_set( m_widget, "GtkWindow::allow_shrink", TRUE, NULL);
2b854a32
VZ
192
193 gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event",
fb1585ae 194 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback), (gpointer)this );
2b854a32 195
fb1585ae
RR
196 m_wxwindow = gtk_myfixed_new();
197 gtk_widget_show( m_wxwindow );
198 GTK_WIDGET_UNSET_FLAGS( m_wxwindow, GTK_CAN_FOCUS );
2b854a32 199
fb1585ae 200 gtk_container_add( GTK_CONTAINER(m_widget), m_wxwindow );
2b854a32 201
fb1585ae 202 SetTitle( title );
2b854a32 203
de8113d9 204 if (m_parent) m_parent->AddChild( this );
2b854a32 205
de8113d9 206 PostCreation();
e146b8c8 207
2b07d713
RR
208 /* we cannot set MWM hints before the widget has
209 been realized, so we do this directly after realization */
210 gtk_signal_connect( GTK_OBJECT(m_widget), "realize",
211 GTK_SIGNAL_FUNC(gtk_dialog_realized_callback), (gpointer) this );
227e5e99
RR
212
213 /* we set the position of the window after the map event. setting it
214 before has no effect (with KWM) */
215 gtk_signal_connect( GTK_OBJECT(m_widget), "map",
216 GTK_SIGNAL_FUNC(gtk_dialog_map_callback), (gpointer) this );
217
2b07d713 218 /* the user resized the frame by dragging etc. */
2b854a32 219 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
36b3b54a 220 GTK_SIGNAL_FUNC(gtk_dialog_size_callback), (gpointer)this );
2b854a32 221
36b3b54a
RR
222 gtk_signal_connect( GTK_OBJECT(m_widget), "configure_event",
223 GTK_SIGNAL_FUNC(gtk_dialog_configure_callback), (gpointer)this );
224
fb1585ae 225 return TRUE;
c33c4050 226}
c801d85f 227
43a18898 228wxDialog::~wxDialog()
c801d85f 229{
fb1585ae 230 wxTopLevelWindows.DeleteObject( this );
2b854a32 231
0d2a2b60
RR
232 if (wxTheApp->GetTopWindow() == this)
233 {
234 wxTheApp->SetTopWindow( (wxWindow*) NULL );
235 }
2b854a32 236
0d2a2b60 237 if (wxTopLevelWindows.Number() == 0)
2b854a32 238 {
0d2a2b60
RR
239 wxTheApp->ExitMainLoop();
240 }
c33c4050 241}
c801d85f 242
43a18898 243void wxDialog::SetTitle( const wxString& title )
c801d85f 244{
fb1585ae 245 m_title = title;
ed9b9841
OK
246 if (m_title.IsNull()) m_title = _T("");
247 gtk_window_set_title( GTK_WINDOW(m_widget), m_title.mbc_str() );
c33c4050 248}
c801d85f 249
43a18898 250wxString wxDialog::GetTitle() const
c801d85f 251{
fb1585ae 252 return (wxString&)m_title;
c33c4050 253}
c801d85f
KB
254
255void wxDialog::OnApply( wxCommandEvent &WXUNUSED(event) )
256{
fb1585ae 257 if (Validate()) TransferDataFromWindow();
c33c4050 258}
c801d85f
KB
259
260void wxDialog::OnCancel( wxCommandEvent &WXUNUSED(event) )
261{
fb1585ae
RR
262 if (IsModal())
263 {
264 EndModal(wxID_CANCEL);
265 }
266 else
267 {
268 SetReturnCode(wxID_CANCEL);
269 this->Show(FALSE);
270 }
c33c4050 271}
c801d85f 272
903f689b 273void wxDialog::OnOK( wxCommandEvent &WXUNUSED(event) )
c801d85f 274{
fb1585ae 275 if ( Validate() && TransferDataFromWindow())
1a6944fd 276 {
2b854a32 277 if (IsModal())
fb1585ae
RR
278 {
279 EndModal(wxID_OK);
280 }
281 else
282 {
283 SetReturnCode(wxID_OK);
284 this->Show(FALSE);
285 }
1a6944fd 286 }
c33c4050 287}
c801d85f
KB
288
289void wxDialog::OnPaint( wxPaintEvent& WXUNUSED(event) )
290{
2b854a32 291 // yes
c33c4050 292}
c801d85f 293
2b854a32 294void wxDialog::OnCloseWindow(wxCloseEvent& event)
c801d85f 295{
e3065973
JS
296 // We'll send a Cancel message by default,
297 // which may close the dialog.
298 // Check for looping if the Cancel event handler calls Close().
299
300 // Note that if a cancel button and handler aren't present in the dialog,
301 // nothing will happen when you close the dialog via the window manager, or
302 // via Close().
303 // We wouldn't want to destroy the dialog by default, since the dialog may have been
304 // created on the stack.
305 // However, this does mean that calling dialog->Close() won't delete the dialog
306 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
307 // sure to destroy the dialog.
308 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
309
ab2b3dd4 310 static wxList s_closing;
c801d85f 311
ab2b3dd4 312 if (s_closing.Member(this))
2b854a32
VZ
313 return; // no loops
314
ab2b3dd4 315 s_closing.Append(this);
c801d85f 316
fb1585ae
RR
317 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
318 cancelEvent.SetEventObject( this );
319 GetEventHandler()->ProcessEvent(cancelEvent);
ab2b3dd4 320 s_closing.DeleteObject(this);
c801d85f
KB
321}
322
43a18898 323bool wxDialog::Destroy()
e2414cbe 324{
fb1585ae 325 if (!wxPendingDelete.Member(this)) wxPendingDelete.Append(this);
e2414cbe 326
fb1585ae 327 return TRUE;
e2414cbe
RR
328}
329
e52f60e6
RR
330void wxDialog::OnSize( wxSizeEvent &WXUNUSED(event) )
331{
ed9b9841 332 wxASSERT_MSG( (m_widget != NULL), _T("invalid dialog") );
2b854a32 333
e52f60e6
RR
334 if (GetAutoLayout())
335 {
336 Layout();
337 }
2b854a32 338 else
e52f60e6 339 {
de8113d9 340 /* no child: go out ! */
db1b4961 341 if (!GetChildren().First()) return;
2b854a32 342
de8113d9 343 /* do we have exactly one child? */
e52f60e6 344 wxWindow *child = (wxWindow *) NULL;
db1b4961 345 for(wxNode *node = GetChildren().First(); node; node = node->Next())
e52f60e6
RR
346 {
347 wxWindow *win = (wxWindow *)node->Data();
de135918 348 if (!wxIS_KIND_OF(win,wxFrame) && !wxIS_KIND_OF(win,wxDialog))
e52f60e6 349 {
de8113d9 350 /* it's the second one: do nothing */
e52f60e6
RR
351 if (child) return;
352 child = win;
353 }
354 }
355
de8113d9 356 /* yes: set it's size to fill all the frame */
e52f60e6
RR
357 int client_x, client_y;
358 GetClientSize( &client_x, &client_y );
359 child->SetSize( 1, 1, client_x-2, client_y);
360 }
361}
362
bfc6fde4 363void wxDialog::DoSetSize( int x, int y, int width, int height, int sizeFlags )
903f689b 364{
ed9b9841
OK
365 wxASSERT_MSG( (m_widget != NULL), _T("invalid dialog") );
366 wxASSERT_MSG( (m_wxwindow != NULL), _T("invalid dialog") );
fb1585ae 367
de8113d9 368 if (m_resizing) return; /* I don't like recursions */
fb1585ae
RR
369 m_resizing = TRUE;
370
371 int old_x = m_x;
372 int old_y = m_y;
373 int old_width = m_width;
374 int old_height = m_height;
2b854a32 375
fb1585ae
RR
376 if ((sizeFlags & wxSIZE_USE_EXISTING) == wxSIZE_USE_EXISTING)
377 {
378 if (x != -1) m_x = x;
379 if (y != -1) m_y = y;
380 if (width != -1) m_width = width;
381 if (height != -1) m_height = height;
382 }
383 else
384 {
385 m_x = x;
386 m_y = y;
387 m_width = width;
388 m_height = height;
389 }
390
391 if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH)
392 {
393 if (width == -1) m_width = 80;
394 }
395
396 if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT)
397 {
398 if (height == -1) m_height = 26;
399 }
2b854a32 400
fb1585ae
RR
401 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
402 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
0c77152e
RR
403 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_maxWidth;
404 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_maxHeight;
fb1585ae
RR
405
406 if ((m_x != -1) || (m_y != -1))
407 {
2b854a32 408 if ((m_x != old_x) || (m_y != old_y))
de8113d9 409 {
227e5e99
RR
410 /* we set the position here and when showing the dialog
411 for the first time in idle time */
e146b8c8 412 gtk_widget_set_uposition( m_widget, m_x, m_y );
de8113d9 413 }
fb1585ae 414 }
2b854a32 415
fb1585ae
RR
416 if ((m_width != old_width) || (m_height != old_height))
417 {
227e5e99
RR
418 /* actual resizing is deferred to GtkOnSize in idle time and
419 when showing the dialog */
de8113d9 420 m_sizeSet = FALSE;
fb1585ae 421 }
2b854a32 422
fb1585ae 423 m_resizing = FALSE;
903f689b
RR
424}
425
de8113d9
RR
426void wxDialog::GtkOnSize( int WXUNUSED(x), int WXUNUSED(y), int width, int height )
427{
428 // due to a bug in gtk, x,y are always 0
429 // m_x = x;
430 // m_y = y;
431
432 if ((m_height == height) && (m_width == width) && (m_sizeSet)) return;
433 if (!m_wxwindow) return;
434
435 m_width = width;
436 m_height = height;
437
438 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
439 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
440 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_maxWidth;
441 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_maxHeight;
442
443 /* we actually set the size of a frame here and no-where else */
444 gtk_widget_set_usize( m_widget, m_width, m_height );
445
446 m_sizeSet = TRUE;
447
448 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
449 event.SetEventObject( this );
450 GetEventHandler()->ProcessEvent( event );
451}
452
903f689b
RR
453void wxDialog::Centre( int direction )
454{
ed9b9841 455 wxASSERT_MSG( (m_widget != NULL), _T("invalid dialog") );
2b854a32 456
43a18898
RR
457 int x = 0;
458 int y = 0;
2b854a32 459
f5eafd0e
RR
460 if ((direction & wxHORIZONTAL) == wxHORIZONTAL) x = (gdk_screen_width () - m_width) / 2;
461 if ((direction & wxVERTICAL) == wxVERTICAL) y = (gdk_screen_height () - m_height) / 2;
2b854a32 462
fb1585ae 463 Move( x, y );
903f689b
RR
464}
465
de8113d9
RR
466void wxDialog::OnInternalIdle()
467{
1b3667ab 468 if (!m_sizeSet && GTK_WIDGET_REALIZED(m_wxwindow))
de8113d9
RR
469 GtkOnSize( m_x, m_y, m_width, m_height );
470}
471
debe6624 472bool wxDialog::Show( bool show )
c801d85f 473{
fb1585ae
RR
474 if (!show && IsModal())
475 {
de8113d9 476 EndModal( wxID_CANCEL );
fb1585ae 477 }
c801d85f 478
de8113d9
RR
479 if (show && !m_sizeSet)
480 {
481 /* by calling GtkOnSize here, we don't have to call
482 either after showing the frame, which would entail
483 much ugly flicker nor from within the size_allocate
484 handler, because GTK 1.1.X forbids that. */
485
486 GtkOnSize( m_x, m_y, m_width, m_height );
487 }
2b854a32 488
227e5e99
RR
489 if (show != m_isShown)
490 {
491 if (show)
492 {
493 gtk_widget_show( m_widget );
494 }
495 else
496 gtk_widget_hide( m_widget );
497
498 m_isShown = show;
499 }
e146b8c8 500
fb1585ae 501 if (show) InitDialog();
2b854a32 502
fb1585ae 503 return TRUE;
c33c4050
RR
504}
505
43a18898 506bool wxDialog::IsModal() const
e1e955e1 507{
fb1585ae 508 return m_modalShowing;
e1e955e1
RR
509}
510
511void wxDialog::SetModal( bool WXUNUSED(flag) )
c33c4050 512{
e1e955e1 513/*
c33c4050
RR
514 if (flag)
515 m_windowStyle |= wxDIALOG_MODAL;
516 else
517 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
e1e955e1 518*/
ed9b9841 519 wxFAIL_MSG( _T("wxDialog:SetModal obsolete now") );
c33c4050 520}
c801d85f 521
43a18898 522int wxDialog::ShowModal()
c801d85f 523{
fb1585ae
RR
524 if (IsModal())
525 {
ed9b9841 526 wxFAIL_MSG( _T("wxDialog:ShowModal called twice") );
fb1585ae
RR
527 return GetReturnCode();
528 }
e146b8c8 529
fb1585ae 530 Show( TRUE );
2b854a32 531
fb1585ae 532 m_modalShowing = TRUE;
2b854a32 533
fb1585ae
RR
534 gtk_grab_add( m_widget );
535 gtk_main();
536 gtk_grab_remove( m_widget );
2b854a32 537
fb1585ae 538 return GetReturnCode();
c33c4050 539}
c801d85f
KB
540
541void wxDialog::EndModal( int retCode )
542{
fb1585ae 543 SetReturnCode( retCode );
2b854a32 544
fb1585ae
RR
545 if (!IsModal())
546 {
ed9b9841 547 wxFAIL_MSG( _T("wxDialog:EndModal called twice") );
fb1585ae
RR
548 return;
549 }
2b854a32 550
fb1585ae 551 m_modalShowing = FALSE;
2b854a32 552
fb1585ae 553 gtk_main_quit();
2b854a32 554
fb1585ae 555 Show( FALSE );
c33c4050 556}
c801d85f 557
43a18898 558void wxDialog::InitDialog()
c801d85f 559{
fb1585ae 560 wxWindow::InitDialog();
c33c4050
RR
561}
562
c33c4050
RR
563void wxDialog::SetIcon( const wxIcon &icon )
564{
fb1585ae
RR
565 m_icon = icon;
566 if (!icon.Ok()) return;
2b854a32 567
fb1585ae
RR
568 wxMask *mask = icon.GetMask();
569 GdkBitmap *bm = (GdkBitmap *) NULL;
570 if (mask) bm = mask->GetBitmap();
2b854a32 571
fb1585ae 572 gdk_window_set_icon( m_widget->window, (GdkWindow *) NULL, icon.GetPixmap(), bm );
c33c4050 573}