]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: dialog.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
a81258be | 5 | // Id: $Id$ |
01111366 | 6 | // Copyright: (c) 1998 Robert Roebling |
c801d85f KB |
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 | #include "wx/gtk/win_gtk.h" | |
18 | ||
e2414cbe RR |
19 | //----------------------------------------------------------------------------- |
20 | ||
21 | extern wxList wxPendingDelete; | |
22 | ||
c801d85f | 23 | //----------------------------------------------------------------------------- |
e1e955e1 RR |
24 | // "delete_event" |
25 | //----------------------------------------------------------------------------- | |
c801d85f KB |
26 | |
27 | bool gtk_dialog_delete_callback( GtkWidget *WXUNUSED(widget), GdkEvent *WXUNUSED(event), wxDialog *win ) | |
28 | { | |
29 | /* | |
fb1585ae RR |
30 | printf( "OnDelete from " ); |
31 | if (win->GetClassInfo() && win->GetClassInfo()->GetClassName()) | |
32 | printf( win->GetClassInfo()->GetClassName() ); | |
33 | printf( ".\n" ); | |
c801d85f KB |
34 | */ |
35 | ||
fb1585ae | 36 | win->Close(); |
c801d85f | 37 | |
fb1585ae | 38 | return TRUE; |
c33c4050 | 39 | } |
c801d85f | 40 | |
e52f60e6 RR |
41 | //----------------------------------------------------------------------------- |
42 | // "size_allocate" | |
43 | //----------------------------------------------------------------------------- | |
44 | ||
45 | static void gtk_dialog_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxDialog *win ) | |
46 | { | |
47 | if (!win->HasVMT()) return; | |
48 | ||
49 | /* | |
50 | printf( "OnDialogResize from " ); | |
51 | if (win->GetClassInfo() && win->GetClassInfo()->GetClassName()) | |
52 | printf( win->GetClassInfo()->GetClassName() ); | |
53 | printf( ".\n" ); | |
54 | */ | |
55 | ||
56 | win->GtkOnSize( alloc->x, alloc->y, alloc->width, alloc->height ); | |
57 | } | |
58 | ||
c801d85f KB |
59 | //----------------------------------------------------------------------------- |
60 | // wxDialog | |
61 | //----------------------------------------------------------------------------- | |
62 | ||
a60c99e6 | 63 | BEGIN_EVENT_TABLE(wxDialog,wxPanel) |
fb1585ae RR |
64 | EVT_BUTTON (wxID_OK, wxDialog::OnOK) |
65 | EVT_BUTTON (wxID_CANCEL, wxDialog::OnCancel) | |
66 | EVT_BUTTON (wxID_APPLY, wxDialog::OnApply) | |
e52f60e6 | 67 | EVT_SIZE (wxDialog::OnSize) |
fb1585ae | 68 | EVT_CLOSE (wxDialog::OnCloseWindow) |
c801d85f KB |
69 | END_EVENT_TABLE() |
70 | ||
a60c99e6 | 71 | IMPLEMENT_DYNAMIC_CLASS(wxDialog,wxPanel) |
c801d85f | 72 | |
43a18898 | 73 | wxDialog::wxDialog() |
c801d85f | 74 | { |
fb1585ae RR |
75 | m_title = ""; |
76 | m_modalShowing = FALSE; | |
c33c4050 | 77 | } |
c801d85f KB |
78 | |
79 | wxDialog::wxDialog( wxWindow *parent, | |
fb1585ae RR |
80 | wxWindowID id, const wxString &title, |
81 | const wxPoint &pos, const wxSize &size, | |
82 | long style, const wxString &name ) | |
c801d85f | 83 | { |
fb1585ae | 84 | m_modalShowing = FALSE; |
fb1585ae | 85 | Create( parent, id, title, pos, size, style, name ); |
c33c4050 | 86 | } |
c801d85f KB |
87 | |
88 | bool wxDialog::Create( wxWindow *parent, | |
fb1585ae RR |
89 | wxWindowID id, const wxString &title, |
90 | const wxPoint &pos, const wxSize &size, | |
91 | long style, const wxString &name ) | |
c801d85f | 92 | { |
a802c3a1 RR |
93 | wxTopLevelWindows.Append( this ); |
94 | ||
fb1585ae | 95 | m_needParent = FALSE; |
c801d85f | 96 | |
fb1585ae | 97 | PreCreation( parent, id, pos, size, style, name ); |
c801d85f | 98 | |
fb1585ae RR |
99 | m_widget = gtk_window_new( GTK_WINDOW_TOPLEVEL ); |
100 | GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS ); | |
c801d85f | 101 | |
fb1585ae | 102 | gtk_widget_set( m_widget, "GtkWindow::allow_shrink", TRUE, NULL); |
c801d85f | 103 | |
fb1585ae RR |
104 | gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event", |
105 | GTK_SIGNAL_FUNC(gtk_dialog_delete_callback), (gpointer)this ); | |
c801d85f | 106 | |
e52f60e6 RR |
107 | gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate", |
108 | GTK_SIGNAL_FUNC(gtk_dialog_size_callback), (gpointer)this ); | |
109 | ||
fb1585ae RR |
110 | m_wxwindow = gtk_myfixed_new(); |
111 | gtk_widget_show( m_wxwindow ); | |
112 | GTK_WIDGET_UNSET_FLAGS( m_wxwindow, GTK_CAN_FOCUS ); | |
c801d85f | 113 | |
fb1585ae | 114 | gtk_container_add( GTK_CONTAINER(m_widget), m_wxwindow ); |
c801d85f | 115 | |
fb1585ae | 116 | SetTitle( title ); |
c801d85f | 117 | |
fb1585ae RR |
118 | if ((m_x != -1) || (m_y != -1)) |
119 | gtk_widget_set_uposition( m_widget, m_x, m_y ); | |
903f689b | 120 | |
fb1585ae | 121 | gtk_widget_set_usize( m_widget, m_width, m_height ); |
903f689b | 122 | |
fb1585ae | 123 | if (m_parent) m_parent->AddChild( this ); |
6ca41e57 RR |
124 | |
125 | ||
fb1585ae | 126 | PostCreation(); |
c801d85f | 127 | |
fb1585ae | 128 | return TRUE; |
c33c4050 | 129 | } |
c801d85f | 130 | |
43a18898 | 131 | wxDialog::~wxDialog() |
c801d85f | 132 | { |
fb1585ae RR |
133 | wxTopLevelWindows.DeleteObject( this ); |
134 | if (wxTopLevelWindows.Number() == 0) wxTheApp->ExitMainLoop(); | |
c33c4050 | 135 | } |
c801d85f | 136 | |
43a18898 | 137 | void wxDialog::SetTitle( const wxString& title ) |
c801d85f | 138 | { |
fb1585ae RR |
139 | m_title = title; |
140 | if (m_title.IsNull()) m_title = ""; | |
141 | gtk_window_set_title( GTK_WINDOW(m_widget), m_title ); | |
c33c4050 | 142 | } |
c801d85f | 143 | |
43a18898 | 144 | wxString wxDialog::GetTitle() const |
c801d85f | 145 | { |
fb1585ae | 146 | return (wxString&)m_title; |
c33c4050 | 147 | } |
c801d85f KB |
148 | |
149 | void wxDialog::OnApply( wxCommandEvent &WXUNUSED(event) ) | |
150 | { | |
fb1585ae | 151 | if (Validate()) TransferDataFromWindow(); |
c33c4050 | 152 | } |
c801d85f KB |
153 | |
154 | void wxDialog::OnCancel( wxCommandEvent &WXUNUSED(event) ) | |
155 | { | |
fb1585ae RR |
156 | if (IsModal()) |
157 | { | |
158 | EndModal(wxID_CANCEL); | |
159 | } | |
160 | else | |
161 | { | |
162 | SetReturnCode(wxID_CANCEL); | |
163 | this->Show(FALSE); | |
164 | } | |
c33c4050 | 165 | } |
c801d85f | 166 | |
903f689b | 167 | void wxDialog::OnOK( wxCommandEvent &WXUNUSED(event) ) |
c801d85f | 168 | { |
fb1585ae | 169 | if ( Validate() && TransferDataFromWindow()) |
1a6944fd | 170 | { |
fb1585ae RR |
171 | if (IsModal()) |
172 | { | |
173 | EndModal(wxID_OK); | |
174 | } | |
175 | else | |
176 | { | |
177 | SetReturnCode(wxID_OK); | |
178 | this->Show(FALSE); | |
179 | } | |
1a6944fd | 180 | } |
c33c4050 | 181 | } |
c801d85f KB |
182 | |
183 | void wxDialog::OnPaint( wxPaintEvent& WXUNUSED(event) ) | |
184 | { | |
185 | // yes | |
c33c4050 | 186 | } |
c801d85f | 187 | |
43a18898 | 188 | bool wxDialog::OnClose() |
c801d85f | 189 | { |
fb1585ae | 190 | static wxList closing; |
c801d85f | 191 | |
fb1585ae | 192 | if (closing.Member(this)) return FALSE; // no loops |
1a6944fd | 193 | |
fb1585ae | 194 | closing.Append(this); |
c801d85f | 195 | |
fb1585ae RR |
196 | wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL); |
197 | cancelEvent.SetEventObject( this ); | |
198 | GetEventHandler()->ProcessEvent(cancelEvent); | |
199 | closing.DeleteObject(this); | |
c801d85f | 200 | |
fb1585ae | 201 | return FALSE; |
c801d85f KB |
202 | } |
203 | ||
43a18898 | 204 | bool wxDialog::Destroy() |
e2414cbe | 205 | { |
fb1585ae | 206 | if (!wxPendingDelete.Member(this)) wxPendingDelete.Append(this); |
e2414cbe | 207 | |
fb1585ae | 208 | return TRUE; |
e2414cbe RR |
209 | } |
210 | ||
43a18898 | 211 | void wxDialog::OnCloseWindow( wxCloseEvent& event ) |
c801d85f | 212 | { |
fb1585ae RR |
213 | if (GetEventHandler()->OnClose() || event.GetForce()) |
214 | { | |
215 | this->Destroy(); | |
216 | } | |
c33c4050 | 217 | } |
c801d85f | 218 | |
e52f60e6 RR |
219 | void wxDialog::GtkOnSize( int WXUNUSED(x), int WXUNUSED(y), int width, int height ) |
220 | { | |
221 | // due to a bug in gtk, x,y are always 0 | |
222 | // m_x = x; | |
223 | // m_y = y; | |
224 | ||
225 | if ((m_height == height) && (m_width == width) && | |
226 | (m_sizeSet)) return; | |
227 | if (!m_wxwindow) return; | |
228 | ||
229 | m_width = width; | |
230 | m_height = height; | |
231 | ||
232 | if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth; | |
233 | if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight; | |
234 | if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_minWidth; | |
235 | if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_minHeight; | |
236 | ||
237 | gtk_widget_set_usize( m_widget, m_width, m_height ); | |
238 | ||
239 | m_sizeSet = TRUE; | |
240 | ||
241 | wxSizeEvent event( wxSize(m_width,m_height), GetId() ); | |
242 | event.SetEventObject( this ); | |
243 | GetEventHandler()->ProcessEvent( event ); | |
244 | } | |
245 | ||
246 | void wxDialog::OnSize( wxSizeEvent &WXUNUSED(event) ) | |
247 | { | |
248 | wxASSERT_MSG( (m_widget != NULL), "invalid frame" ); | |
249 | ||
250 | if (GetAutoLayout()) | |
251 | { | |
252 | Layout(); | |
253 | } | |
254 | else | |
255 | { | |
256 | // no child: go out ! | |
db1b4961 | 257 | if (!GetChildren().First()) return; |
e52f60e6 RR |
258 | |
259 | // do we have exactly one child? | |
260 | wxWindow *child = (wxWindow *) NULL; | |
db1b4961 | 261 | for(wxNode *node = GetChildren().First(); node; node = node->Next()) |
e52f60e6 RR |
262 | { |
263 | wxWindow *win = (wxWindow *)node->Data(); | |
264 | if (!IS_KIND_OF(win,wxFrame) && !IS_KIND_OF(win,wxDialog)) | |
265 | { | |
266 | // it's the second one: do nothing | |
267 | if (child) return; | |
268 | child = win; | |
269 | } | |
270 | } | |
271 | ||
272 | // yes: set it's size to fill all the frame | |
273 | int client_x, client_y; | |
274 | GetClientSize( &client_x, &client_y ); | |
275 | child->SetSize( 1, 1, client_x-2, client_y); | |
276 | } | |
277 | } | |
278 | ||
fb1585ae | 279 | void wxDialog::SetSize( int x, int y, int width, int height, int sizeFlags ) |
903f689b | 280 | { |
fb1585ae RR |
281 | wxASSERT_MSG( (m_widget != NULL), "invalid window" ); |
282 | ||
283 | // Don't do anything for children of wxMDIChildFrame | |
284 | if (!m_wxwindow) return; | |
285 | ||
286 | if (m_resizing) return; // I don't like recursions | |
287 | m_resizing = TRUE; | |
288 | ||
289 | int old_x = m_x; | |
290 | int old_y = m_y; | |
291 | int old_width = m_width; | |
292 | int old_height = m_height; | |
293 | ||
294 | if ((sizeFlags & wxSIZE_USE_EXISTING) == wxSIZE_USE_EXISTING) | |
295 | { | |
296 | if (x != -1) m_x = x; | |
297 | if (y != -1) m_y = y; | |
298 | if (width != -1) m_width = width; | |
299 | if (height != -1) m_height = height; | |
300 | } | |
301 | else | |
302 | { | |
303 | m_x = x; | |
304 | m_y = y; | |
305 | m_width = width; | |
306 | m_height = height; | |
307 | } | |
308 | ||
309 | if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH) | |
310 | { | |
311 | if (width == -1) m_width = 80; | |
312 | } | |
313 | ||
314 | if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT) | |
315 | { | |
316 | if (height == -1) m_height = 26; | |
317 | } | |
318 | ||
319 | if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth; | |
320 | if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight; | |
321 | if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_minWidth; | |
322 | if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_minHeight; | |
323 | ||
324 | if ((m_x != -1) || (m_y != -1)) | |
325 | { | |
326 | if ((m_x != old_x) || (m_y != old_y)) | |
327 | gtk_widget_set_uposition( m_widget, m_x, m_y ); | |
328 | } | |
329 | ||
330 | if ((m_width != old_width) || (m_height != old_height)) | |
331 | { | |
332 | gtk_widget_set_usize( m_widget, m_width, m_height ); | |
333 | } | |
334 | ||
335 | m_sizeSet = TRUE; | |
336 | ||
337 | wxSizeEvent event( wxSize(m_width,m_height), GetId() ); | |
338 | event.SetEventObject( this ); | |
e52f60e6 | 339 | GetEventHandler()->ProcessEvent( event ); |
fb1585ae RR |
340 | |
341 | m_resizing = FALSE; | |
903f689b RR |
342 | } |
343 | ||
43a18898 RR |
344 | void wxDialog::SetSize( int width, int height ) |
345 | { | |
346 | SetSize( -1, -1, width, height, wxSIZE_USE_EXISTING ); | |
347 | } | |
348 | ||
903f689b RR |
349 | void wxDialog::Centre( int direction ) |
350 | { | |
fb1585ae RR |
351 | wxASSERT_MSG( (m_widget != NULL), "invalid frame" ); |
352 | ||
43a18898 RR |
353 | int x = 0; |
354 | int y = 0; | |
fb1585ae RR |
355 | |
356 | if (direction & wxHORIZONTAL == wxHORIZONTAL) x = (gdk_screen_width () - m_width) / 2; | |
357 | if (direction & wxVERTICAL == wxVERTICAL) y = (gdk_screen_height () - m_height) / 2; | |
358 | ||
359 | Move( x, y ); | |
903f689b RR |
360 | } |
361 | ||
debe6624 | 362 | bool wxDialog::Show( bool show ) |
c801d85f | 363 | { |
fb1585ae RR |
364 | if (!show && IsModal()) |
365 | { | |
366 | EndModal( wxID_CANCEL ); | |
367 | } | |
c801d85f | 368 | |
fb1585ae | 369 | wxWindow::Show( show ); |
c801d85f | 370 | |
fb1585ae | 371 | if (show) InitDialog(); |
c801d85f | 372 | |
fb1585ae | 373 | return TRUE; |
c33c4050 RR |
374 | } |
375 | ||
43a18898 | 376 | bool wxDialog::IsModal() const |
e1e955e1 | 377 | { |
fb1585ae | 378 | return m_modalShowing; |
e1e955e1 RR |
379 | } |
380 | ||
381 | void wxDialog::SetModal( bool WXUNUSED(flag) ) | |
c33c4050 | 382 | { |
e1e955e1 | 383 | /* |
c33c4050 RR |
384 | if (flag) |
385 | m_windowStyle |= wxDIALOG_MODAL; | |
386 | else | |
387 | if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL; | |
e1e955e1 | 388 | */ |
fb1585ae | 389 | wxFAIL_MSG( "wxDialog:SetModal obsolete now" ); |
c33c4050 | 390 | } |
c801d85f | 391 | |
43a18898 | 392 | int wxDialog::ShowModal() |
c801d85f | 393 | { |
fb1585ae RR |
394 | if (IsModal()) |
395 | { | |
396 | wxFAIL_MSG( "wxDialog:ShowModal called twice" ); | |
397 | return GetReturnCode(); | |
398 | } | |
d355d3fe | 399 | |
fb1585ae | 400 | Show( TRUE ); |
d355d3fe | 401 | |
fb1585ae | 402 | m_modalShowing = TRUE; |
d355d3fe | 403 | |
fb1585ae RR |
404 | gtk_grab_add( m_widget ); |
405 | gtk_main(); | |
406 | gtk_grab_remove( m_widget ); | |
d355d3fe | 407 | |
fb1585ae | 408 | return GetReturnCode(); |
c33c4050 | 409 | } |
c801d85f KB |
410 | |
411 | void wxDialog::EndModal( int retCode ) | |
412 | { | |
fb1585ae | 413 | SetReturnCode( retCode ); |
d355d3fe | 414 | |
fb1585ae RR |
415 | if (!IsModal()) |
416 | { | |
417 | wxFAIL_MSG( "wxDialog:EndModal called twice" ); | |
418 | return; | |
419 | } | |
b6af8d80 | 420 | |
fb1585ae | 421 | m_modalShowing = FALSE; |
d355d3fe | 422 | |
fb1585ae | 423 | gtk_main_quit(); |
5b011451 | 424 | |
fb1585ae | 425 | Show( FALSE ); |
c33c4050 | 426 | } |
c801d85f | 427 | |
43a18898 | 428 | void wxDialog::InitDialog() |
c801d85f | 429 | { |
fb1585ae | 430 | wxWindow::InitDialog(); |
c33c4050 RR |
431 | } |
432 | ||
c33c4050 RR |
433 | void wxDialog::SetIcon( const wxIcon &icon ) |
434 | { | |
fb1585ae RR |
435 | m_icon = icon; |
436 | if (!icon.Ok()) return; | |
c33c4050 | 437 | |
fb1585ae RR |
438 | wxMask *mask = icon.GetMask(); |
439 | GdkBitmap *bm = (GdkBitmap *) NULL; | |
440 | if (mask) bm = mask->GetBitmap(); | |
c33c4050 | 441 | |
fb1585ae | 442 | gdk_window_set_icon( m_widget->window, (GdkWindow *) NULL, icon.GetPixmap(), bm ); |
c33c4050 | 443 | } |