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