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