]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/dialog.cpp
Chnaged text id for clipboard to "text/plain"
[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
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
VZ
147
148 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
36b3b54a 149 GTK_SIGNAL_FUNC(gtk_dialog_size_callback), (gpointer)this );
2b854a32 150
36b3b54a
RR
151 gtk_signal_connect( GTK_OBJECT(m_widget), "configure_event",
152 GTK_SIGNAL_FUNC(gtk_dialog_configure_callback), (gpointer)this );
153
fb1585ae 154 return TRUE;
c33c4050 155}
c801d85f 156
43a18898 157wxDialog::~wxDialog()
c801d85f 158{
fb1585ae 159 wxTopLevelWindows.DeleteObject( this );
2b854a32 160
0d2a2b60
RR
161 if (wxTheApp->GetTopWindow() == this)
162 {
163 wxTheApp->SetTopWindow( (wxWindow*) NULL );
164 }
2b854a32 165
0d2a2b60 166 if (wxTopLevelWindows.Number() == 0)
2b854a32 167 {
0d2a2b60
RR
168 wxTheApp->ExitMainLoop();
169 }
c33c4050 170}
c801d85f 171
43a18898 172void wxDialog::SetTitle( const wxString& title )
c801d85f 173{
fb1585ae
RR
174 m_title = title;
175 if (m_title.IsNull()) m_title = "";
176 gtk_window_set_title( GTK_WINDOW(m_widget), m_title );
c33c4050 177}
c801d85f 178
43a18898 179wxString wxDialog::GetTitle() const
c801d85f 180{
fb1585ae 181 return (wxString&)m_title;
c33c4050 182}
c801d85f
KB
183
184void wxDialog::OnApply( wxCommandEvent &WXUNUSED(event) )
185{
fb1585ae 186 if (Validate()) TransferDataFromWindow();
c33c4050 187}
c801d85f
KB
188
189void wxDialog::OnCancel( wxCommandEvent &WXUNUSED(event) )
190{
fb1585ae
RR
191 if (IsModal())
192 {
193 EndModal(wxID_CANCEL);
194 }
195 else
196 {
197 SetReturnCode(wxID_CANCEL);
198 this->Show(FALSE);
199 }
c33c4050 200}
c801d85f 201
903f689b 202void wxDialog::OnOK( wxCommandEvent &WXUNUSED(event) )
c801d85f 203{
fb1585ae 204 if ( Validate() && TransferDataFromWindow())
1a6944fd 205 {
2b854a32 206 if (IsModal())
fb1585ae
RR
207 {
208 EndModal(wxID_OK);
209 }
210 else
211 {
212 SetReturnCode(wxID_OK);
213 this->Show(FALSE);
214 }
1a6944fd 215 }
c33c4050 216}
c801d85f
KB
217
218void wxDialog::OnPaint( wxPaintEvent& WXUNUSED(event) )
219{
2b854a32 220 // yes
c33c4050 221}
c801d85f 222
2b854a32 223void wxDialog::OnCloseWindow(wxCloseEvent& event)
c801d85f 224{
e3065973
JS
225 // We'll send a Cancel message by default,
226 // which may close the dialog.
227 // Check for looping if the Cancel event handler calls Close().
228
229 // Note that if a cancel button and handler aren't present in the dialog,
230 // nothing will happen when you close the dialog via the window manager, or
231 // via Close().
232 // We wouldn't want to destroy the dialog by default, since the dialog may have been
233 // created on the stack.
234 // However, this does mean that calling dialog->Close() won't delete the dialog
235 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
236 // sure to destroy the dialog.
237 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
238
fb1585ae 239 static wxList closing;
c801d85f 240
2b854a32
VZ
241 if (closing.Member(this))
242 return; // no loops
243
fb1585ae 244 closing.Append(this);
c801d85f 245
fb1585ae
RR
246 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
247 cancelEvent.SetEventObject( this );
248 GetEventHandler()->ProcessEvent(cancelEvent);
249 closing.DeleteObject(this);
c801d85f
KB
250}
251
43a18898 252bool wxDialog::Destroy()
e2414cbe 253{
fb1585ae 254 if (!wxPendingDelete.Member(this)) wxPendingDelete.Append(this);
e2414cbe 255
fb1585ae 256 return TRUE;
e2414cbe
RR
257}
258
e52f60e6
RR
259void wxDialog::OnSize( wxSizeEvent &WXUNUSED(event) )
260{
2830bf19 261 wxASSERT_MSG( (m_widget != NULL), "invalid dialog" );
2b854a32 262
e52f60e6
RR
263 if (GetAutoLayout())
264 {
265 Layout();
266 }
2b854a32 267 else
e52f60e6 268 {
de8113d9 269 /* no child: go out ! */
db1b4961 270 if (!GetChildren().First()) return;
2b854a32 271
de8113d9 272 /* do we have exactly one child? */
e52f60e6 273 wxWindow *child = (wxWindow *) NULL;
db1b4961 274 for(wxNode *node = GetChildren().First(); node; node = node->Next())
e52f60e6
RR
275 {
276 wxWindow *win = (wxWindow *)node->Data();
de135918 277 if (!wxIS_KIND_OF(win,wxFrame) && !wxIS_KIND_OF(win,wxDialog))
e52f60e6 278 {
de8113d9 279 /* it's the second one: do nothing */
e52f60e6
RR
280 if (child) return;
281 child = win;
282 }
283 }
284
de8113d9 285 /* yes: set it's size to fill all the frame */
e52f60e6
RR
286 int client_x, client_y;
287 GetClientSize( &client_x, &client_y );
288 child->SetSize( 1, 1, client_x-2, client_y);
289 }
290}
291
bfc6fde4 292void wxDialog::DoSetSize( int x, int y, int width, int height, int sizeFlags )
903f689b 293{
de8113d9
RR
294 wxASSERT_MSG( (m_widget != NULL), "invalid dialog" );
295 wxASSERT_MSG( (m_wxwindow != NULL), "invalid dialog" );
fb1585ae 296
de8113d9 297 if (m_resizing) return; /* I don't like recursions */
fb1585ae
RR
298 m_resizing = TRUE;
299
300 int old_x = m_x;
301 int old_y = m_y;
302 int old_width = m_width;
303 int old_height = m_height;
2b854a32 304
fb1585ae
RR
305 if ((sizeFlags & wxSIZE_USE_EXISTING) == wxSIZE_USE_EXISTING)
306 {
307 if (x != -1) m_x = x;
308 if (y != -1) m_y = y;
309 if (width != -1) m_width = width;
310 if (height != -1) m_height = height;
311 }
312 else
313 {
314 m_x = x;
315 m_y = y;
316 m_width = width;
317 m_height = height;
318 }
319
320 if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH)
321 {
322 if (width == -1) m_width = 80;
323 }
324
325 if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT)
326 {
327 if (height == -1) m_height = 26;
328 }
2b854a32 329
fb1585ae
RR
330 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
331 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
0c77152e
RR
332 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_maxWidth;
333 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_maxHeight;
fb1585ae
RR
334
335 if ((m_x != -1) || (m_y != -1))
336 {
2b854a32 337 if ((m_x != old_x) || (m_y != old_y))
de8113d9
RR
338 {
339 /* m_sizeSet = FALSE; */
e146b8c8 340 gtk_widget_set_uposition( m_widget, m_x, m_y );
de8113d9 341 }
fb1585ae 342 }
2b854a32 343
fb1585ae
RR
344 if ((m_width != old_width) || (m_height != old_height))
345 {
de8113d9 346 m_sizeSet = FALSE;
fb1585ae 347 }
2b854a32 348
fb1585ae 349 m_resizing = FALSE;
903f689b
RR
350}
351
de8113d9
RR
352void wxDialog::GtkOnSize( int WXUNUSED(x), int WXUNUSED(y), int width, int height )
353{
354 // due to a bug in gtk, x,y are always 0
355 // m_x = x;
356 // m_y = y;
357
358 if ((m_height == height) && (m_width == width) && (m_sizeSet)) return;
359 if (!m_wxwindow) return;
360
361 m_width = width;
362 m_height = height;
363
364 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
365 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
366 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_maxWidth;
367 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_maxHeight;
368
369 /* we actually set the size of a frame here and no-where else */
370 gtk_widget_set_usize( m_widget, m_width, m_height );
371
372 m_sizeSet = TRUE;
373
374 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
375 event.SetEventObject( this );
376 GetEventHandler()->ProcessEvent( event );
377}
378
903f689b
RR
379void wxDialog::Centre( int direction )
380{
f5eafd0e 381 wxASSERT_MSG( (m_widget != NULL), "invalid dialog" );
2b854a32 382
43a18898
RR
383 int x = 0;
384 int y = 0;
2b854a32 385
f5eafd0e
RR
386 if ((direction & wxHORIZONTAL) == wxHORIZONTAL) x = (gdk_screen_width () - m_width) / 2;
387 if ((direction & wxVERTICAL) == wxVERTICAL) y = (gdk_screen_height () - m_height) / 2;
2b854a32 388
fb1585ae 389 Move( x, y );
903f689b
RR
390}
391
de8113d9
RR
392void wxDialog::OnInternalIdle()
393{
394 if (!m_sizeSet)
395 GtkOnSize( m_x, m_y, m_width, m_height );
396}
397
debe6624 398bool wxDialog::Show( bool show )
c801d85f 399{
fb1585ae
RR
400 if (!show && IsModal())
401 {
de8113d9 402 EndModal( wxID_CANCEL );
fb1585ae 403 }
c801d85f 404
de8113d9
RR
405 if (show && !m_sizeSet)
406 {
407 /* by calling GtkOnSize here, we don't have to call
408 either after showing the frame, which would entail
409 much ugly flicker nor from within the size_allocate
410 handler, because GTK 1.1.X forbids that. */
411
412 GtkOnSize( m_x, m_y, m_width, m_height );
413 }
2b854a32 414
de8113d9 415 wxWindow::Show( show );
e146b8c8 416
fb1585ae 417 if (show) InitDialog();
2b854a32 418
fb1585ae 419 return TRUE;
c33c4050
RR
420}
421
43a18898 422bool wxDialog::IsModal() const
e1e955e1 423{
fb1585ae 424 return m_modalShowing;
e1e955e1
RR
425}
426
427void wxDialog::SetModal( bool WXUNUSED(flag) )
c33c4050 428{
e1e955e1 429/*
c33c4050
RR
430 if (flag)
431 m_windowStyle |= wxDIALOG_MODAL;
432 else
433 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
e1e955e1 434*/
fb1585ae 435 wxFAIL_MSG( "wxDialog:SetModal obsolete now" );
c33c4050 436}
c801d85f 437
43a18898 438int wxDialog::ShowModal()
c801d85f 439{
fb1585ae
RR
440 if (IsModal())
441 {
442 wxFAIL_MSG( "wxDialog:ShowModal called twice" );
443 return GetReturnCode();
444 }
e146b8c8 445
fb1585ae 446 Show( TRUE );
2b854a32 447
fb1585ae 448 m_modalShowing = TRUE;
2b854a32 449
fb1585ae
RR
450 gtk_grab_add( m_widget );
451 gtk_main();
452 gtk_grab_remove( m_widget );
2b854a32 453
fb1585ae 454 return GetReturnCode();
c33c4050 455}
c801d85f
KB
456
457void wxDialog::EndModal( int retCode )
458{
fb1585ae 459 SetReturnCode( retCode );
2b854a32 460
fb1585ae
RR
461 if (!IsModal())
462 {
463 wxFAIL_MSG( "wxDialog:EndModal called twice" );
464 return;
465 }
2b854a32 466
fb1585ae 467 m_modalShowing = FALSE;
2b854a32 468
fb1585ae 469 gtk_main_quit();
2b854a32 470
fb1585ae 471 Show( FALSE );
c33c4050 472}
c801d85f 473
43a18898 474void wxDialog::InitDialog()
c801d85f 475{
fb1585ae 476 wxWindow::InitDialog();
c33c4050
RR
477}
478
c33c4050
RR
479void wxDialog::SetIcon( const wxIcon &icon )
480{
fb1585ae
RR
481 m_icon = icon;
482 if (!icon.Ok()) return;
2b854a32 483
fb1585ae
RR
484 wxMask *mask = icon.GetMask();
485 GdkBitmap *bm = (GdkBitmap *) NULL;
486 if (mask) bm = mask->GetBitmap();
2b854a32 487
fb1585ae 488 gdk_window_set_icon( m_widget->window, (GdkWindow *) NULL, icon.GetPixmap(), bm );
c33c4050 489}