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