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