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