]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/dialog.cpp
Still bit fiddling in wxImage.
[wxWidgets.git] / src / gtk / 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 #include "wx/gtk/win_gtk.h"
18
19 //-----------------------------------------------------------------------------
20
21 extern wxList wxPendingDelete;
22
23 //-----------------------------------------------------------------------------
24 // "delete_event"
25 //-----------------------------------------------------------------------------
26
27 bool gtk_dialog_delete_callback( GtkWidget *WXUNUSED(widget), GdkEvent *WXUNUSED(event), wxDialog *win )
28 {
29 /*
30 printf( "OnDelete from " );
31 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
32 printf( win->GetClassInfo()->GetClassName() );
33 printf( ".\n" );
34 */
35
36 win->Close();
37
38 return TRUE;
39 }
40
41 //-----------------------------------------------------------------------------
42 // wxDialog
43 //-----------------------------------------------------------------------------
44
45 BEGIN_EVENT_TABLE(wxDialog,wxPanel)
46 EVT_BUTTON (wxID_OK, wxDialog::OnOK)
47 EVT_BUTTON (wxID_CANCEL, wxDialog::OnCancel)
48 EVT_BUTTON (wxID_APPLY, wxDialog::OnApply)
49 EVT_CLOSE (wxDialog::OnCloseWindow)
50 END_EVENT_TABLE()
51
52 IMPLEMENT_DYNAMIC_CLASS(wxDialog,wxPanel)
53
54 wxDialog::wxDialog(void)
55 {
56 m_title = "";
57 m_modalShowing = FALSE;
58 wxTopLevelWindows.Insert( this );
59 }
60
61 wxDialog::wxDialog( wxWindow *parent,
62 wxWindowID id, const wxString &title,
63 const wxPoint &pos, const wxSize &size,
64 long style, const wxString &name )
65 {
66 m_modalShowing = FALSE;
67 wxTopLevelWindows.Insert( this );
68 Create( parent, id, title, pos, size, style, name );
69 }
70
71 bool wxDialog::Create( wxWindow *parent,
72 wxWindowID id, const wxString &title,
73 const wxPoint &pos, const wxSize &size,
74 long style, const wxString &name )
75 {
76 m_needParent = FALSE;
77
78 PreCreation( parent, id, pos, size, style, name );
79
80 m_widget = gtk_window_new( GTK_WINDOW_TOPLEVEL );
81 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
82
83 gtk_widget_set( m_widget, "GtkWindow::allow_shrink", TRUE, NULL);
84
85 gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event",
86 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback), (gpointer)this );
87
88 m_wxwindow = gtk_myfixed_new();
89 gtk_widget_show( m_wxwindow );
90 GTK_WIDGET_UNSET_FLAGS( m_wxwindow, GTK_CAN_FOCUS );
91
92 gtk_container_add( GTK_CONTAINER(m_widget), m_wxwindow );
93
94 SetTitle( title );
95
96 if ((m_x != -1) || (m_y != -1))
97 gtk_widget_set_uposition( m_widget, m_x, m_y );
98
99 gtk_widget_set_usize( m_widget, m_width, m_height );
100
101 if (m_parent) m_parent->AddChild( this );
102
103
104 PostCreation();
105
106 return TRUE;
107 }
108
109 wxDialog::~wxDialog(void)
110 {
111 wxTopLevelWindows.DeleteObject( this );
112 if (wxTopLevelWindows.Number() == 0) wxTheApp->ExitMainLoop();
113 }
114
115 void wxDialog::SetTitle(const wxString& title )
116 {
117 m_title = title;
118 if (m_title.IsNull()) m_title = "";
119 gtk_window_set_title( GTK_WINDOW(m_widget), m_title );
120 }
121
122 wxString wxDialog::GetTitle(void) const
123 {
124 return (wxString&)m_title;
125 }
126
127 void wxDialog::OnApply( wxCommandEvent &WXUNUSED(event) )
128 {
129 if (Validate()) TransferDataFromWindow();
130 }
131
132 void wxDialog::OnCancel( wxCommandEvent &WXUNUSED(event) )
133 {
134 if (IsModal())
135 {
136 EndModal(wxID_CANCEL);
137 }
138 else
139 {
140 SetReturnCode(wxID_CANCEL);
141 this->Show(FALSE);
142 }
143 }
144
145 void wxDialog::OnOK( wxCommandEvent &WXUNUSED(event) )
146 {
147 if ( Validate() && TransferDataFromWindow())
148 {
149 if (IsModal())
150 {
151 EndModal(wxID_OK);
152 }
153 else
154 {
155 SetReturnCode(wxID_OK);
156 this->Show(FALSE);
157 }
158 }
159 }
160
161 void wxDialog::OnPaint( wxPaintEvent& WXUNUSED(event) )
162 {
163 // yes
164 }
165
166 bool wxDialog::OnClose(void)
167 {
168 static wxList closing;
169
170 if (closing.Member(this)) return FALSE; // no loops
171
172 closing.Append(this);
173
174 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
175 cancelEvent.SetEventObject( this );
176 GetEventHandler()->ProcessEvent(cancelEvent);
177 closing.DeleteObject(this);
178
179 return FALSE;
180 }
181
182 bool wxDialog::Destroy(void)
183 {
184 if (!wxPendingDelete.Member(this)) wxPendingDelete.Append(this);
185
186 return TRUE;
187 }
188
189 void wxDialog::OnCloseWindow(wxCloseEvent& event)
190 {
191 if (GetEventHandler()->OnClose() || event.GetForce())
192 {
193 this->Destroy();
194 }
195 }
196
197 void wxDialog::SetSize( int x, int y, int width, int height, int sizeFlags )
198 {
199 wxASSERT_MSG( (m_widget != NULL), "invalid window" );
200
201 // Don't do anything for children of wxMDIChildFrame
202 if (!m_wxwindow) return;
203
204 if (m_resizing) return; // I don't like recursions
205 m_resizing = TRUE;
206
207 int old_x = m_x;
208 int old_y = m_y;
209 int old_width = m_width;
210 int old_height = m_height;
211
212 if ((sizeFlags & wxSIZE_USE_EXISTING) == wxSIZE_USE_EXISTING)
213 {
214 if (x != -1) m_x = x;
215 if (y != -1) m_y = y;
216 if (width != -1) m_width = width;
217 if (height != -1) m_height = height;
218 }
219 else
220 {
221 m_x = x;
222 m_y = y;
223 m_width = width;
224 m_height = height;
225 }
226
227 if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH)
228 {
229 if (width == -1) m_width = 80;
230 }
231
232 if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT)
233 {
234 if (height == -1) m_height = 26;
235 }
236
237 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
238 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
239 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_minWidth;
240 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_minHeight;
241
242 if ((m_x != -1) || (m_y != -1))
243 {
244 if ((m_x != old_x) || (m_y != old_y))
245 gtk_widget_set_uposition( m_widget, m_x, m_y );
246 }
247
248 if ((m_width != old_width) || (m_height != old_height))
249 {
250 gtk_widget_set_usize( m_widget, m_width, m_height );
251 }
252
253 m_sizeSet = TRUE;
254
255 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
256 event.SetEventObject( this );
257 ProcessEvent( event );
258
259 m_resizing = FALSE;
260 }
261
262 void wxDialog::Centre( int direction )
263 {
264 wxASSERT_MSG( (m_widget != NULL), "invalid frame" );
265
266 int x,y;
267
268 if (direction & wxHORIZONTAL == wxHORIZONTAL) x = (gdk_screen_width () - m_width) / 2;
269 if (direction & wxVERTICAL == wxVERTICAL) y = (gdk_screen_height () - m_height) / 2;
270
271 Move( x, y );
272 }
273
274 bool wxDialog::Show( bool show )
275 {
276 if (!show && IsModal())
277 {
278 EndModal( wxID_CANCEL );
279 }
280
281 wxWindow::Show( show );
282
283 if (show) InitDialog();
284
285 return TRUE;
286 }
287
288 bool wxDialog::IsModal(void) const
289 {
290 return m_modalShowing;
291 }
292
293 void wxDialog::SetModal( bool WXUNUSED(flag) )
294 {
295 /*
296 if (flag)
297 m_windowStyle |= wxDIALOG_MODAL;
298 else
299 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
300 */
301 wxFAIL_MSG( "wxDialog:SetModal obsolete now" );
302 }
303
304 int wxDialog::ShowModal(void)
305 {
306 if (IsModal())
307 {
308 wxFAIL_MSG( "wxDialog:ShowModal called twice" );
309 return GetReturnCode();
310 }
311
312 Show( TRUE );
313
314 m_modalShowing = TRUE;
315
316 gtk_grab_add( m_widget );
317 gtk_main();
318 gtk_grab_remove( m_widget );
319
320 return GetReturnCode();
321 }
322
323 void wxDialog::EndModal( int retCode )
324 {
325 SetReturnCode( retCode );
326
327 if (!IsModal())
328 {
329 wxFAIL_MSG( "wxDialog:EndModal called twice" );
330 return;
331 }
332
333 m_modalShowing = FALSE;
334
335 gtk_main_quit();
336
337 Show( FALSE );
338 }
339
340 void wxDialog::InitDialog(void)
341 {
342 wxWindow::InitDialog();
343 }
344
345 void wxDialog::SetIcon( const wxIcon &icon )
346 {
347 m_icon = icon;
348 if (!icon.Ok()) return;
349
350 wxMask *mask = icon.GetMask();
351 GdkBitmap *bm = (GdkBitmap *) NULL;
352 if (mask) bm = mask->GetBitmap();
353
354 gdk_window_set_icon( m_widget->window, (GdkWindow *) NULL, icon.GetPixmap(), bm );
355 }