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