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