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