]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/frame.cpp
Correct the wxButton::Setlabel nonsense
[wxWidgets.git] / src / gtk1 / frame.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: frame.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Created: 01/02/97
6 // Id:
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #ifdef __GNUG__
12 #pragma implementation "frame.h"
13 #endif
14
15 #include "wx/frame.h"
16 #include "wx/dialog.h"
17 #include "wx/control.h"
18 #include "wx/app.h"
19 #include "wx/menu.h"
20 #include "wx/toolbar.h"
21 #include "wx/statusbr.h"
22 #include "wx/mdi.h"
23 #include "wx/gtk/win_gtk.h"
24
25 const wxMENU_HEIGHT = 28;
26 const wxSTATUS_HEIGHT = 25;
27
28 extern wxList wxTopLevelWindows;
29 extern wxList wxPendingDelete;
30
31 //-----------------------------------------------------------------------------
32 // wxFrame
33 //-----------------------------------------------------------------------------
34
35 //-----------------------------------------------------------------------------
36 // set size
37
38 void gtk_frame_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxFrame *win )
39 {
40 if (!win->HasVMT()) return;
41
42 /*
43 printf( "OnFrameResize from " );
44 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
45 printf( win->GetClassInfo()->GetClassName() );
46 printf( ".\n" );
47 */
48
49 win->GtkOnSize( alloc->x, alloc->y, alloc->width, alloc->height );
50 };
51
52 //-----------------------------------------------------------------------------
53 // delete
54
55 bool gtk_frame_delete_callback( GtkWidget *WXUNUSED(widget), GdkEvent *WXUNUSED(event), wxFrame *win )
56 {
57 /*
58 printf( "OnDelete from " );
59 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
60 printf( win->GetClassInfo()->GetClassName() );
61 printf( ".\n" );
62 */
63
64 win->Close();
65
66 return TRUE;
67 };
68
69 //-----------------------------------------------------------------------------
70 // configure
71
72 gint gtk_frame_configure_callback( GtkWidget *WXUNUSED(widget), GdkEventConfigure *event, wxFrame *win )
73 {
74 if (!win->HasVMT()) return FALSE;
75
76 win->m_x = event->x;
77 win->m_y = event->y;
78
79 return FALSE;
80 };
81
82 //-----------------------------------------------------------------------------
83
84 BEGIN_EVENT_TABLE(wxFrame, wxWindow)
85 EVT_SIZE(wxFrame::OnSize)
86 EVT_CLOSE(wxFrame::OnCloseWindow)
87 EVT_IDLE(wxFrame::OnIdle)
88 END_EVENT_TABLE()
89
90 IMPLEMENT_DYNAMIC_CLASS(wxFrame,wxWindow)
91
92 wxFrame::wxFrame()
93 {
94 m_frameMenuBar = NULL;
95 m_frameStatusBar = NULL;
96 m_frameToolBar = NULL;
97 m_sizeSet = FALSE;
98 m_addPrivateChild = FALSE;
99 m_wxwindow = NULL;
100 m_mainWindow = NULL;
101 wxTopLevelWindows.Insert( this );
102 };
103
104 wxFrame::wxFrame( wxWindow *parent, wxWindowID id, const wxString &title,
105 const wxPoint &pos, const wxSize &size,
106 long style, const wxString &name )
107 {
108 m_frameMenuBar = NULL;
109 m_frameStatusBar = NULL;
110 m_frameToolBar = NULL;
111 m_sizeSet = FALSE;
112 m_addPrivateChild = FALSE;
113 m_wxwindow = NULL;
114 m_mainWindow = NULL;
115 Create( parent, id, title, pos, size, style, name );
116 wxTopLevelWindows.Insert( this );
117 };
118
119 bool wxFrame::Create( wxWindow *parent, wxWindowID id, const wxString &title,
120 const wxPoint &pos, const wxSize &size,
121 long style, const wxString &name )
122 {
123 m_needParent = FALSE;
124
125 PreCreation( parent, id, pos, size, style, name );
126
127 m_title = title;
128
129 m_widget = gtk_window_new( GTK_WINDOW_TOPLEVEL );
130 if ((size.x != -1) && (size.y != -1))
131 gtk_widget_set_usize( m_widget, m_width, m_height );
132 if ((pos.x != -1) && (pos.y != -1))
133 gtk_widget_set_uposition( m_widget, m_x, m_y );
134
135 gtk_window_set_title( GTK_WINDOW(m_widget), title );
136 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
137
138 gtk_widget_set( m_widget, "GtkWindow::allow_shrink", TRUE, NULL);
139
140 gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event",
141 GTK_SIGNAL_FUNC(gtk_frame_delete_callback), (gpointer)this );
142
143 m_mainWindow = gtk_myfixed_new();
144 gtk_widget_show( m_mainWindow );
145 GTK_WIDGET_UNSET_FLAGS( m_mainWindow, GTK_CAN_FOCUS );
146
147 gtk_container_add( GTK_CONTAINER(m_widget), m_mainWindow );
148 gtk_widget_set_uposition( m_mainWindow, 0, 0 );
149
150 m_wxwindow = gtk_myfixed_new();
151 gtk_widget_show( m_wxwindow );
152 GTK_WIDGET_UNSET_FLAGS( m_wxwindow, GTK_CAN_FOCUS );
153
154 gtk_container_add( GTK_CONTAINER(m_mainWindow), m_wxwindow );
155
156 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
157 GTK_SIGNAL_FUNC(gtk_frame_size_callback), (gpointer)this );
158
159 gtk_signal_connect( GTK_OBJECT(m_widget), "configure_event",
160 GTK_SIGNAL_FUNC(gtk_frame_configure_callback), (gpointer)this );
161
162 PostCreation();
163
164 gtk_widget_realize( m_mainWindow );
165
166 return TRUE;
167 };
168
169 wxFrame::~wxFrame()
170 {
171 if (m_frameMenuBar) delete m_frameMenuBar;
172 if (m_frameStatusBar) delete m_frameStatusBar;
173
174 // if (m_mainWindow) gtk_widget_destroy( m_mainWindow );
175
176 wxTopLevelWindows.DeleteObject( this );
177 if (wxTopLevelWindows.Number() == 0) wxTheApp->ExitMainLoop();
178 };
179
180 bool wxFrame::Show( bool show )
181 {
182 if (show)
183 {
184 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
185 m_sizeSet = FALSE;
186 ProcessEvent( event );
187 };
188 return wxWindow::Show( show );
189 };
190
191 void wxFrame::Enable( bool enable )
192 {
193 wxWindow::Enable( enable );
194 gtk_widget_set_sensitive( m_mainWindow, enable );
195 };
196
197 void wxFrame::OnCloseWindow( wxCloseEvent &event )
198 {
199 if ( GetEventHandler()->OnClose() || event.GetForce())
200 {
201 this->Destroy();
202 }
203 };
204
205 bool wxFrame::Destroy()
206 {
207 if (!wxPendingDelete.Member(this))
208 wxPendingDelete.Append(this);
209
210 return TRUE;
211 }
212
213 void wxFrame::GetClientSize( int *width, int *height ) const
214 {
215 wxWindow::GetClientSize( width, height );
216 if (height)
217 {
218 if (m_frameMenuBar) (*height) -= wxMENU_HEIGHT;
219 if (m_frameStatusBar) (*height) -= wxSTATUS_HEIGHT;
220 if (m_frameToolBar)
221 {
222 int y = 0;
223 m_frameToolBar->GetSize( NULL, &y );
224 (*height) -= y;
225 }
226 };
227 };
228
229 void wxFrame::SetClientSize( int const width, int const height )
230 {
231 int h = height;
232 if (m_frameMenuBar) h += wxMENU_HEIGHT;
233 if (m_frameStatusBar) h += wxSTATUS_HEIGHT;
234 if (m_frameToolBar)
235 {
236 int y = 0;
237 m_frameToolBar->GetSize( NULL, &y );
238 h += y;
239 }
240 wxWindow::SetClientSize( width, h );
241 };
242
243 void wxFrame::GtkOnSize( int WXUNUSED(x), int WXUNUSED(y), int width, int height )
244 {
245 // due to a bug in gtk, x,y are always 0
246 // m_x = x;
247 // m_y = y;
248
249 if ((m_height == height) && (m_width == width) &&
250 (m_sizeSet)) return;
251 if (!m_mainWindow) return;
252 if (!m_wxwindow) return;
253
254 m_width = width;
255 m_height = height;
256
257 gtk_widget_set_usize( m_widget, width, height );
258
259 int main_x = 0;
260 int main_y = 0;
261 int main_height = height;
262 int main_width = width;
263
264 // This emulates Windows behaviour:
265 // The menu bar is part of the main window, but the status bar
266 // is on the implementation side in the client area. The
267 // function GetClientSize returns the size of the client area
268 // minus the status bar height. Under wxGTK, the main window
269 // is represented by m_mainWindow. The menubar is inserted
270 // into m_mainWindow whereas the statusbar is insertes into
271 // m_wxwindow just like any other window.
272
273 // not really needed
274 // gtk_widget_set_usize( m_mainWindow, width, height );
275
276 if (m_frameMenuBar)
277 {
278 main_y = wxMENU_HEIGHT;
279 main_height -= wxMENU_HEIGHT;
280 };
281
282 int toolbar_height = 0;
283 if (m_frameToolBar) m_frameToolBar->GetSize( NULL, &toolbar_height );
284
285 main_y += toolbar_height;
286 main_height -= toolbar_height;
287
288 gtk_myfixed_move( GTK_MYFIXED(m_mainWindow), m_wxwindow, main_x, main_y );
289 gtk_widget_set_usize( m_wxwindow, main_width, main_height );
290
291 if (m_frameMenuBar)
292 {
293 gtk_myfixed_move( GTK_MYFIXED(m_mainWindow), m_frameMenuBar->m_widget, 1, 1 );
294 gtk_widget_set_usize( m_frameMenuBar->m_widget, width-2, wxMENU_HEIGHT-2 );
295 };
296
297 if (m_frameToolBar)
298 {
299 gtk_myfixed_move( GTK_MYFIXED(m_mainWindow), m_frameToolBar->m_widget, 1, wxMENU_HEIGHT );
300 gtk_widget_set_usize( m_frameToolBar->m_widget, width-2, toolbar_height );
301 };
302
303 if (m_frameStatusBar)
304 {
305 m_frameStatusBar->SetSize( 0, main_height-wxSTATUS_HEIGHT, width, wxSTATUS_HEIGHT );
306 };
307
308 m_sizeSet = TRUE;
309
310 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
311 event.SetEventObject( this );
312 ProcessEvent( event );
313 };
314
315 void wxFrame::OnSize( wxSizeEvent &WXUNUSED(event) )
316 {
317 if ( GetAutoLayout() )
318 Layout();
319 else {
320 // no child: go out !
321 if (!GetChildren()->First())
322 return;
323
324 // do we have exactly one child?
325 wxWindow *child = NULL;
326 for(wxNode *node = GetChildren()->First(); node; node = node->Next())
327 {
328 wxWindow *win = (wxWindow *)node->Data();
329 if (!IS_KIND_OF(win,wxFrame) && !IS_KIND_OF(win,wxDialog)
330 #if 0 // not in m_children anyway
331 && (win != m_frameMenuBar) &&
332 (win != m_frameToolBar) &&
333 (win != m_frameStatusBar)
334 #endif
335 )
336 {
337 if ( child ) // it's the second one: do nothing
338 return;
339
340 child = win;
341 };
342 };
343
344 // yes: set it's size to fill all the frame
345 int client_x, client_y;
346 GetClientSize(&client_x, &client_y);
347 child->SetSize( 1, 1, client_x-2, client_y);
348 }
349 };
350
351 void wxFrame::AddChild( wxWindow *child )
352 {
353 // wxFrame and wxDialog as children aren't placed into the parents
354
355 if (IS_KIND_OF(child,wxMDIChildFrame)) printf( "wxFrame::AddChild error.\n" );
356
357 if ( IS_KIND_OF(child,wxFrame) || IS_KIND_OF(child,wxDialog))
358 {
359 m_children.Append( child );
360
361 if ((child->m_x != -1) && (child->m_y != -1))
362 gtk_widget_set_uposition( child->m_widget, child->m_x, child->m_y );
363
364 return;
365 }
366
367 if (m_addPrivateChild)
368 {
369 gtk_myfixed_put( GTK_MYFIXED(m_mainWindow), child->m_widget, child->m_x, child->m_y );
370
371 gtk_widget_set_usize( child->m_widget, child->m_width, child->m_height );
372 }
373 else
374 {
375 m_children.Append( child );
376
377 if (m_wxwindow)
378 gtk_myfixed_put( GTK_MYFIXED(m_wxwindow), child->m_widget, child->m_x, child->m_y );
379
380 gtk_widget_set_usize( child->m_widget, child->m_width, child->m_height );
381 }
382 };
383
384 static void SetInvokingWindow( wxMenu *menu, wxWindow *win )
385 {
386 menu->SetInvokingWindow( win );
387 wxNode *node = menu->m_items.First();
388 while (node)
389 {
390 wxMenuItem *menuitem = (wxMenuItem*)node->Data();
391 if (menuitem->IsSubMenu())
392 SetInvokingWindow( menuitem->GetSubMenu(), win );
393 node = node->Next();
394 };
395 };
396
397 void wxFrame::SetMenuBar( wxMenuBar *menuBar )
398 {
399 m_frameMenuBar = menuBar;
400
401 if (m_frameMenuBar)
402 {
403 if (m_frameMenuBar->m_parent != this)
404 {
405 wxNode *node = m_frameMenuBar->m_menus.First();
406 while (node)
407 {
408 wxMenu *menu = (wxMenu*)node->Data();
409 SetInvokingWindow( menu, this );
410 node = node->Next();
411 };
412
413 m_frameMenuBar->m_parent = this;
414 gtk_myfixed_put( GTK_MYFIXED(m_mainWindow),
415 m_frameMenuBar->m_widget, m_frameMenuBar->m_x, m_frameMenuBar->m_y );
416 }
417 }
418 };
419
420 wxMenuBar *wxFrame::GetMenuBar(void)
421 {
422 return m_frameMenuBar;
423 };
424
425 wxToolBar *wxFrame::CreateToolBar( long style , wxWindowID id, const wxString& name )
426 {
427 m_addPrivateChild = TRUE;
428
429 m_frameToolBar = new wxToolBar( this, id, wxDefaultPosition, wxDefaultSize, style, name );
430
431 m_addPrivateChild = FALSE;
432
433 return m_frameToolBar;
434 };
435
436 wxToolBar *wxFrame::GetToolBar(void)
437 {
438 return m_frameToolBar;
439 };
440
441 wxStatusBar* wxFrame::CreateStatusBar( int number, long style, wxWindowID id, const wxString& name )
442 {
443 if (m_frameStatusBar)
444 delete m_frameStatusBar;
445
446 m_frameStatusBar = new wxStatusBar( this, id, wxPoint(0,0), wxSize(100,20), style, name );
447
448 m_frameStatusBar->SetFieldsCount( number );
449
450 return m_frameStatusBar;
451 };
452
453 void wxFrame::SetStatusText( const wxString &text, int number )
454 {
455 if (m_frameStatusBar) m_frameStatusBar->SetStatusText( text, number );
456 };
457
458 void wxFrame::SetStatusWidths( int n, int *width )
459 {
460 if (m_frameStatusBar) m_frameStatusBar->SetStatusWidths( n, width );
461 };
462
463 wxStatusBar *wxFrame::GetStatusBar(void)
464 {
465 return m_frameStatusBar;
466 };
467
468 void wxFrame::SetTitle( const wxString &title )
469 {
470 m_title = title;
471 gtk_window_set_title( GTK_WINDOW(m_widget), title );
472 };
473
474 void wxFrame::SetSizeHints(int minW, int minH, int maxW, int maxH, int WXUNUSED(incW) )
475 {
476 if (m_wxwindow)
477 gdk_window_set_hints( m_wxwindow->window, -1, -1,
478 minW, minH, maxW, maxH, GDK_HINT_MIN_SIZE | GDK_HINT_MIN_SIZE );
479 }
480
481 void wxFrame::SetIcon( const wxIcon &icon )
482 {
483 m_icon = icon;
484 if (!icon.Ok()) return;
485
486 wxMask *mask = icon.GetMask();
487 GdkBitmap *bm = NULL;
488 if (mask) bm = mask->GetBitmap();
489
490 gdk_window_set_icon( m_widget->window, NULL, icon.GetPixmap(), bm );
491 }
492