]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: dialog.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling | |
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" | |
17 | ||
18 | #include "gdk/gdk.h" | |
19 | #include "gtk/gtk.h" | |
20 | #include "wx/gtk/win_gtk.h" | |
21 | ||
22 | //----------------------------------------------------------------------------- | |
23 | ||
24 | extern wxList wxPendingDelete; | |
25 | ||
26 | //----------------------------------------------------------------------------- | |
27 | // "delete_event" | |
28 | //----------------------------------------------------------------------------- | |
29 | ||
30 | bool gtk_dialog_delete_callback( GtkWidget *WXUNUSED(widget), GdkEvent *WXUNUSED(event), wxDialog *win ) | |
31 | { | |
32 | /* | |
33 | printf( "OnDelete from " ); | |
34 | if (win->GetClassInfo() && win->GetClassInfo()->GetClassName()) | |
35 | printf( win->GetClassInfo()->GetClassName() ); | |
36 | printf( ".\n" ); | |
37 | */ | |
38 | ||
39 | win->Close(); | |
40 | ||
41 | return TRUE; | |
42 | } | |
43 | ||
44 | //----------------------------------------------------------------------------- | |
45 | // "size_allocate" | |
46 | //----------------------------------------------------------------------------- | |
47 | ||
48 | static 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 | 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 | } | |
65 | } | |
66 | ||
67 | //----------------------------------------------------------------------------- | |
68 | // "configure_event" | |
69 | //----------------------------------------------------------------------------- | |
70 | ||
71 | static 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 | ||
85 | //----------------------------------------------------------------------------- | |
86 | // "realize" from m_widget | |
87 | //----------------------------------------------------------------------------- | |
88 | ||
89 | /* we cannot MWM hints and icons before the widget has been realized, | |
90 | so we do this directly after realization */ | |
91 | ||
92 | static gint | |
93 | gtk_dialog_realized_callback( GtkWidget *widget, wxDialog *win ) | |
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 | ||
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 | ||
132 | return FALSE; | |
133 | } | |
134 | ||
135 | //----------------------------------------------------------------------------- | |
136 | // "map" from m_widget | |
137 | //----------------------------------------------------------------------------- | |
138 | ||
139 | static gint | |
140 | gtk_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 | ||
147 | //----------------------------------------------------------------------------- | |
148 | // wxDialog | |
149 | //----------------------------------------------------------------------------- | |
150 | ||
151 | BEGIN_EVENT_TABLE(wxDialog,wxPanel) | |
152 | EVT_BUTTON (wxID_OK, wxDialog::OnOK) | |
153 | EVT_BUTTON (wxID_CANCEL, wxDialog::OnCancel) | |
154 | EVT_BUTTON (wxID_APPLY, wxDialog::OnApply) | |
155 | EVT_SIZE (wxDialog::OnSize) | |
156 | EVT_CLOSE (wxDialog::OnCloseWindow) | |
157 | END_EVENT_TABLE() | |
158 | ||
159 | IMPLEMENT_DYNAMIC_CLASS(wxDialog,wxPanel) | |
160 | ||
161 | void wxDialog::Init() | |
162 | { | |
163 | m_sizeSet = FALSE; | |
164 | m_modalShowing = FALSE; | |
165 | } | |
166 | ||
167 | wxDialog::wxDialog( wxWindow *parent, | |
168 | wxWindowID id, const wxString &title, | |
169 | const wxPoint &pos, const wxSize &size, | |
170 | long style, const wxString &name ) | |
171 | { | |
172 | Init(); | |
173 | ||
174 | Create( parent, id, title, pos, size, style, name ); | |
175 | } | |
176 | ||
177 | bool wxDialog::Create( wxWindow *parent, | |
178 | wxWindowID id, const wxString &title, | |
179 | const wxPoint &pos, const wxSize &size, | |
180 | long style, const wxString &name ) | |
181 | { | |
182 | wxTopLevelWindows.Append( this ); | |
183 | ||
184 | m_needParent = FALSE; | |
185 | ||
186 | PreCreation( parent, id, pos, size, style, name ); | |
187 | ||
188 | m_widget = gtk_window_new( GTK_WINDOW_TOPLEVEL ); | |
189 | GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS ); | |
190 | ||
191 | gtk_widget_set( m_widget, "GtkWindow::allow_shrink", TRUE, NULL); | |
192 | ||
193 | gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event", | |
194 | GTK_SIGNAL_FUNC(gtk_dialog_delete_callback), (gpointer)this ); | |
195 | ||
196 | m_wxwindow = gtk_myfixed_new(); | |
197 | gtk_widget_show( m_wxwindow ); | |
198 | GTK_WIDGET_UNSET_FLAGS( m_wxwindow, GTK_CAN_FOCUS ); | |
199 | ||
200 | gtk_container_add( GTK_CONTAINER(m_widget), m_wxwindow ); | |
201 | ||
202 | SetTitle( title ); | |
203 | ||
204 | if (m_parent) m_parent->AddChild( this ); | |
205 | ||
206 | PostCreation(); | |
207 | ||
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 ); | |
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 | ||
218 | /* the user resized the frame by dragging etc. */ | |
219 | gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate", | |
220 | GTK_SIGNAL_FUNC(gtk_dialog_size_callback), (gpointer)this ); | |
221 | ||
222 | gtk_signal_connect( GTK_OBJECT(m_widget), "configure_event", | |
223 | GTK_SIGNAL_FUNC(gtk_dialog_configure_callback), (gpointer)this ); | |
224 | ||
225 | return TRUE; | |
226 | } | |
227 | ||
228 | wxDialog::~wxDialog() | |
229 | { | |
230 | wxTopLevelWindows.DeleteObject( this ); | |
231 | ||
232 | if (wxTheApp->GetTopWindow() == this) | |
233 | { | |
234 | wxTheApp->SetTopWindow( (wxWindow*) NULL ); | |
235 | } | |
236 | ||
237 | if (wxTopLevelWindows.Number() == 0) | |
238 | { | |
239 | wxTheApp->ExitMainLoop(); | |
240 | } | |
241 | } | |
242 | ||
243 | void wxDialog::SetTitle( const wxString& title ) | |
244 | { | |
245 | m_title = title; | |
246 | if (m_title.IsNull()) m_title = _T(""); | |
247 | gtk_window_set_title( GTK_WINDOW(m_widget), m_title.mbc_str() ); | |
248 | } | |
249 | ||
250 | wxString wxDialog::GetTitle() const | |
251 | { | |
252 | return (wxString&)m_title; | |
253 | } | |
254 | ||
255 | void wxDialog::OnApply( wxCommandEvent &WXUNUSED(event) ) | |
256 | { | |
257 | if (Validate()) TransferDataFromWindow(); | |
258 | } | |
259 | ||
260 | void wxDialog::OnCancel( wxCommandEvent &WXUNUSED(event) ) | |
261 | { | |
262 | if (IsModal()) | |
263 | { | |
264 | EndModal(wxID_CANCEL); | |
265 | } | |
266 | else | |
267 | { | |
268 | SetReturnCode(wxID_CANCEL); | |
269 | this->Show(FALSE); | |
270 | } | |
271 | } | |
272 | ||
273 | void wxDialog::OnOK( wxCommandEvent &WXUNUSED(event) ) | |
274 | { | |
275 | if ( Validate() && TransferDataFromWindow()) | |
276 | { | |
277 | if (IsModal()) | |
278 | { | |
279 | EndModal(wxID_OK); | |
280 | } | |
281 | else | |
282 | { | |
283 | SetReturnCode(wxID_OK); | |
284 | this->Show(FALSE); | |
285 | } | |
286 | } | |
287 | } | |
288 | ||
289 | void wxDialog::OnPaint( wxPaintEvent& WXUNUSED(event) ) | |
290 | { | |
291 | // yes | |
292 | } | |
293 | ||
294 | void wxDialog::OnCloseWindow(wxCloseEvent& event) | |
295 | { | |
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 | ||
310 | static wxList s_closing; | |
311 | ||
312 | if (s_closing.Member(this)) | |
313 | return; // no loops | |
314 | ||
315 | s_closing.Append(this); | |
316 | ||
317 | wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL); | |
318 | cancelEvent.SetEventObject( this ); | |
319 | GetEventHandler()->ProcessEvent(cancelEvent); | |
320 | s_closing.DeleteObject(this); | |
321 | } | |
322 | ||
323 | bool wxDialog::Destroy() | |
324 | { | |
325 | if (!wxPendingDelete.Member(this)) wxPendingDelete.Append(this); | |
326 | ||
327 | return TRUE; | |
328 | } | |
329 | ||
330 | void wxDialog::OnSize( wxSizeEvent &WXUNUSED(event) ) | |
331 | { | |
332 | wxASSERT_MSG( (m_widget != NULL), _T("invalid dialog") ); | |
333 | ||
334 | if (GetAutoLayout()) | |
335 | { | |
336 | Layout(); | |
337 | } | |
338 | else | |
339 | { | |
340 | /* no child: go out ! */ | |
341 | if (!GetChildren().First()) return; | |
342 | ||
343 | /* do we have exactly one child? */ | |
344 | wxWindow *child = (wxWindow *) NULL; | |
345 | for(wxNode *node = GetChildren().First(); node; node = node->Next()) | |
346 | { | |
347 | wxWindow *win = (wxWindow *)node->Data(); | |
348 | if (!wxIS_KIND_OF(win,wxFrame) && !wxIS_KIND_OF(win,wxDialog)) | |
349 | { | |
350 | /* it's the second one: do nothing */ | |
351 | if (child) return; | |
352 | child = win; | |
353 | } | |
354 | } | |
355 | ||
356 | /* yes: set it's size to fill all the frame */ | |
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 | ||
363 | void wxDialog::DoSetSize( int x, int y, int width, int height, int sizeFlags ) | |
364 | { | |
365 | wxASSERT_MSG( (m_widget != NULL), _T("invalid dialog") ); | |
366 | wxASSERT_MSG( (m_wxwindow != NULL), _T("invalid dialog") ); | |
367 | ||
368 | if (m_resizing) return; /* I don't like recursions */ | |
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; | |
375 | ||
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 | } | |
400 | ||
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; | |
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; | |
405 | ||
406 | if ((m_x != -1) || (m_y != -1)) | |
407 | { | |
408 | if ((m_x != old_x) || (m_y != old_y)) | |
409 | { | |
410 | /* we set the position here and when showing the dialog | |
411 | for the first time in idle time */ | |
412 | gtk_widget_set_uposition( m_widget, m_x, m_y ); | |
413 | } | |
414 | } | |
415 | ||
416 | if ((m_width != old_width) || (m_height != old_height)) | |
417 | { | |
418 | /* actual resizing is deferred to GtkOnSize in idle time and | |
419 | when showing the dialog */ | |
420 | m_sizeSet = FALSE; | |
421 | } | |
422 | ||
423 | m_resizing = FALSE; | |
424 | } | |
425 | ||
426 | void 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 | ||
453 | void wxDialog::Centre( int direction ) | |
454 | { | |
455 | wxASSERT_MSG( (m_widget != NULL), _T("invalid dialog") ); | |
456 | ||
457 | int x = 0; | |
458 | int y = 0; | |
459 | ||
460 | if ((direction & wxHORIZONTAL) == wxHORIZONTAL) x = (gdk_screen_width () - m_width) / 2; | |
461 | if ((direction & wxVERTICAL) == wxVERTICAL) y = (gdk_screen_height () - m_height) / 2; | |
462 | ||
463 | Move( x, y ); | |
464 | } | |
465 | ||
466 | void wxDialog::OnInternalIdle() | |
467 | { | |
468 | if (!m_sizeSet && GTK_WIDGET_REALIZED(m_wxwindow)) | |
469 | GtkOnSize( m_x, m_y, m_width, m_height ); | |
470 | } | |
471 | ||
472 | bool wxDialog::Show( bool show ) | |
473 | { | |
474 | if (!show && IsModal()) | |
475 | { | |
476 | EndModal( wxID_CANCEL ); | |
477 | } | |
478 | ||
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 | } | |
488 | ||
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 | } | |
500 | ||
501 | if (show) InitDialog(); | |
502 | ||
503 | return TRUE; | |
504 | } | |
505 | ||
506 | bool wxDialog::IsModal() const | |
507 | { | |
508 | return m_modalShowing; | |
509 | } | |
510 | ||
511 | void wxDialog::SetModal( bool WXUNUSED(flag) ) | |
512 | { | |
513 | /* | |
514 | if (flag) | |
515 | m_windowStyle |= wxDIALOG_MODAL; | |
516 | else | |
517 | if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL; | |
518 | */ | |
519 | wxFAIL_MSG( _T("wxDialog:SetModal obsolete now") ); | |
520 | } | |
521 | ||
522 | int wxDialog::ShowModal() | |
523 | { | |
524 | if (IsModal()) | |
525 | { | |
526 | wxFAIL_MSG( _T("wxDialog:ShowModal called twice") ); | |
527 | return GetReturnCode(); | |
528 | } | |
529 | ||
530 | Show( TRUE ); | |
531 | ||
532 | m_modalShowing = TRUE; | |
533 | ||
534 | gtk_grab_add( m_widget ); | |
535 | gtk_main(); | |
536 | gtk_grab_remove( m_widget ); | |
537 | ||
538 | return GetReturnCode(); | |
539 | } | |
540 | ||
541 | void wxDialog::EndModal( int retCode ) | |
542 | { | |
543 | SetReturnCode( retCode ); | |
544 | ||
545 | if (!IsModal()) | |
546 | { | |
547 | wxFAIL_MSG( _T("wxDialog:EndModal called twice") ); | |
548 | return; | |
549 | } | |
550 | ||
551 | m_modalShowing = FALSE; | |
552 | ||
553 | gtk_main_quit(); | |
554 | ||
555 | Show( FALSE ); | |
556 | } | |
557 | ||
558 | void wxDialog::InitDialog() | |
559 | { | |
560 | wxWindow::InitDialog(); | |
561 | } | |
562 | ||
563 | void wxDialog::SetIcon( const wxIcon &icon ) | |
564 | { | |
565 | m_icon = icon; | |
566 | if (!icon.Ok()) return; | |
567 | ||
568 | wxMask *mask = icon.GetMask(); | |
569 | GdkBitmap *bm = (GdkBitmap *) NULL; | |
570 | if (mask) bm = mask->GetBitmap(); | |
571 | ||
572 | gdk_window_set_icon( m_widget->window, (GdkWindow *) NULL, icon.GetPixmap(), bm ); | |
573 | } |