]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/frame.cpp
Move code that does not need realized GdkWinow out of realize handler
[wxWidgets.git] / src / gtk / frame.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/frame.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Copyright: (c) 1998 Robert Roebling
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
11
12 #include "wx/frame.h"
13
14 #ifndef WX_PRECOMP
15 #include "wx/menu.h"
16 #include "wx/toolbar.h"
17 #include "wx/statusbr.h"
18 #endif // WX_PRECOMP
19
20 #include <gtk/gtk.h>
21 #include "wx/gtk/private/gtk2-compat.h"
22
23 #if wxUSE_LIBHILDON
24 #include <hildon-widgets/hildon-window.h>
25 #endif // wxUSE_LIBHILDON
26
27 #if wxUSE_LIBHILDON2
28 #include <hildon/hildon.h>
29 #endif // wxUSE_LIBHILDON2
30
31 // ----------------------------------------------------------------------------
32 // event tables
33 // ----------------------------------------------------------------------------
34
35 // ============================================================================
36 // implementation
37 // ============================================================================
38
39 // ----------------------------------------------------------------------------
40 // wxFrame creation
41 // ----------------------------------------------------------------------------
42
43 void wxFrame::Init()
44 {
45 m_fsSaveFlag = 0;
46 }
47
48 bool wxFrame::Create( wxWindow *parent,
49 wxWindowID id,
50 const wxString& title,
51 const wxPoint& pos,
52 const wxSize& sizeOrig,
53 long style,
54 const wxString &name )
55 {
56 return wxFrameBase::Create(parent, id, title, pos, sizeOrig, style, name);
57 }
58
59 wxFrame::~wxFrame()
60 {
61 SendDestroyEvent();
62
63 DeleteAllBars();
64 }
65
66 // ----------------------------------------------------------------------------
67 // overridden wxWindow methods
68 // ----------------------------------------------------------------------------
69
70 void wxFrame::DoGetClientSize( int *width, int *height ) const
71 {
72 wxASSERT_MSG( (m_widget != NULL), wxT("invalid frame") );
73
74 wxFrameBase::DoGetClientSize(width, height);
75
76 if (m_useCachedClientSize)
77 return;
78
79 if (height)
80 {
81 #if wxUSE_MENUS_NATIVE
82 // menu bar
83 if (m_frameMenuBar && m_frameMenuBar->IsShown())
84 {
85 int h;
86 gtk_widget_get_preferred_height(m_frameMenuBar->m_widget, NULL, &h);
87 #if !wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
88 *height -= h;
89 #endif
90 }
91 #endif // wxUSE_MENUS_NATIVE
92
93 #if wxUSE_STATUSBAR
94 // status bar
95 if (m_frameStatusBar && m_frameStatusBar->IsShown())
96 *height -= m_frameStatusBar->m_height;
97 #endif // wxUSE_STATUSBAR
98 }
99
100 #if wxUSE_TOOLBAR
101 // tool bar
102 if (m_frameToolBar && m_frameToolBar->IsShown())
103 {
104 if (m_frameToolBar->IsVertical())
105 {
106 if (width)
107 {
108 int w;
109 gtk_widget_get_preferred_width(m_frameToolBar->m_widget, NULL, &w);
110 *width -= w;
111 }
112 }
113 else
114 {
115 if (height)
116 {
117 int h;
118 gtk_widget_get_preferred_height(m_frameToolBar->m_widget, NULL, &h);
119 *height -= h;
120 }
121 }
122 }
123 #endif // wxUSE_TOOLBAR
124
125 if (width != NULL && *width < 0)
126 *width = 0;
127 if (height != NULL && *height < 0)
128 *height = 0;
129 }
130
131 #if wxUSE_MENUS && wxUSE_ACCEL
132 // Helper for wxCreateAcceleratorTableForMenuBar
133 static void wxAddAccelerators(wxList& accelEntries, wxMenu* menu)
134 {
135 size_t i;
136 for (i = 0; i < menu->GetMenuItems().GetCount(); i++)
137 {
138 wxMenuItem* item = (wxMenuItem*) menu->GetMenuItems().Item(i)->GetData();
139 if (item->GetSubMenu())
140 {
141 wxAddAccelerators(accelEntries, item->GetSubMenu());
142 }
143 else if (!item->GetItemLabel().IsEmpty())
144 {
145 wxAcceleratorEntry* entry = wxAcceleratorEntry::Create(item->GetItemLabel());
146 if (entry)
147 {
148 entry->Set(entry->GetFlags(), entry->GetKeyCode(), item->GetId());
149 accelEntries.Append((wxObject*) entry);
150 }
151 }
152 }
153 }
154
155 // Create an accelerator table consisting of all the accelerators
156 // from the menubar in the given menus
157 static wxAcceleratorTable wxCreateAcceleratorTableForMenuBar(wxMenuBar* menuBar)
158 {
159 wxList accelEntries;
160
161 size_t i;
162 for (i = 0; i < menuBar->GetMenuCount(); i++)
163 {
164 wxAddAccelerators(accelEntries, menuBar->GetMenu(i));
165 }
166
167 size_t n = accelEntries.GetCount();
168
169 if (n == 0)
170 return wxAcceleratorTable();
171
172 wxAcceleratorEntry* entries = new wxAcceleratorEntry[n];
173
174 for (i = 0; i < accelEntries.GetCount(); i++)
175 {
176 wxAcceleratorEntry* entry = (wxAcceleratorEntry*) accelEntries.Item(i)->GetData();
177 entries[i] = (*entry);
178 delete entry;
179
180 }
181
182 wxAcceleratorTable table(n, entries);
183 delete[] entries;
184
185 return table;
186 }
187 #endif // wxUSE_MENUS && wxUSE_ACCEL
188
189 bool wxFrame::ShowFullScreen(bool show, long style)
190 {
191 if (!wxFrameBase::ShowFullScreen(show, style))
192 return false;
193
194 #if wxUSE_MENUS && wxUSE_ACCEL
195 if (show && GetMenuBar())
196 {
197 wxAcceleratorTable table(wxCreateAcceleratorTableForMenuBar(GetMenuBar()));
198 if (table.IsOk())
199 SetAcceleratorTable(table);
200 }
201 #endif // wxUSE_MENUS && wxUSE_ACCEL
202
203 wxWindow* const bar[] = {
204 #if wxUSE_MENUS
205 m_frameMenuBar,
206 #else
207 NULL,
208 #endif
209 #if wxUSE_TOOLBAR
210 m_frameToolBar,
211 #else
212 NULL,
213 #endif
214 #if wxUSE_STATUSBAR
215 m_frameStatusBar,
216 #else
217 NULL,
218 #endif
219 };
220 const long fsNoBar[] = {
221 wxFULLSCREEN_NOMENUBAR, wxFULLSCREEN_NOTOOLBAR, wxFULLSCREEN_NOSTATUSBAR
222 };
223 for (int i = 0; i < 3; i++)
224 {
225 if (show)
226 {
227 if (bar[i] && (style & fsNoBar[i]))
228 {
229 if (bar[i]->IsShown())
230 bar[i]->Show(false);
231 else
232 style &= ~fsNoBar[i];
233 }
234 }
235 else
236 {
237 if (bar[i] && (m_fsSaveFlag & fsNoBar[i]))
238 bar[i]->Show(true);
239 }
240 }
241 if (show)
242 m_fsSaveFlag = style;
243
244 return true;
245 }
246
247 bool wxFrame::SendIdleEvents(wxIdleEvent& event)
248 {
249 bool needMore = wxFrameBase::SendIdleEvents(event);
250
251 #if wxUSE_MENUS
252 if (m_frameMenuBar && m_frameMenuBar->SendIdleEvents(event))
253 needMore = true;
254 #endif
255 #if wxUSE_TOOLBAR
256 if (m_frameToolBar && m_frameToolBar->SendIdleEvents(event))
257 needMore = true;
258 #endif
259 #if wxUSE_STATUSBAR
260 if (m_frameStatusBar && m_frameStatusBar->SendIdleEvents(event))
261 needMore = true;
262 #endif
263
264 return needMore;
265 }
266
267 // ----------------------------------------------------------------------------
268 // menu/tool/status bar stuff
269 // ----------------------------------------------------------------------------
270
271 #if wxUSE_MENUS_NATIVE
272
273 void wxFrame::DetachMenuBar()
274 {
275 wxASSERT_MSG( (m_widget != NULL), wxT("invalid frame") );
276 wxASSERT_MSG( (m_wxwindow != NULL), wxT("invalid frame") );
277
278 if ( m_frameMenuBar )
279 {
280 #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
281 hildon_window_set_menu(HILDON_WINDOW(m_widget), NULL);
282 #else // !wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
283 g_object_ref( m_frameMenuBar->m_widget );
284
285 gtk_container_remove( GTK_CONTAINER(m_mainWidget), m_frameMenuBar->m_widget );
286 #endif // wxUSE_LIBHILDON || wxUSE_LIBHILDON2 /!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
287 }
288
289 wxFrameBase::DetachMenuBar();
290
291 // make sure next size_allocate causes a wxSizeEvent
292 m_useCachedClientSize = false;
293 m_clientWidth = 0;
294 }
295
296 void wxFrame::AttachMenuBar( wxMenuBar *menuBar )
297 {
298 wxFrameBase::AttachMenuBar(menuBar);
299
300 if (m_frameMenuBar)
301 {
302 #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
303 hildon_window_set_menu(HILDON_WINDOW(m_widget),
304 GTK_MENU(m_frameMenuBar->m_widget));
305 #else // !wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
306
307 // menubar goes into top of vbox (m_mainWidget)
308 gtk_box_pack_start(
309 GTK_BOX(m_mainWidget), menuBar->m_widget, false, false, 0);
310 gtk_box_reorder_child(GTK_BOX(m_mainWidget), menuBar->m_widget, 0);
311
312 // reset size request to allow native sizing to work
313 gtk_widget_set_size_request(menuBar->m_widget, -1, -1);
314
315 gtk_widget_show( m_frameMenuBar->m_widget );
316 #endif // wxUSE_LIBHILDON || wxUSE_LIBHILDON2/!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2
317 }
318 // make sure next size_allocate causes a wxSizeEvent
319 m_useCachedClientSize = false;
320 m_clientWidth = 0;
321 }
322 #endif // wxUSE_MENUS_NATIVE
323
324 #if wxUSE_TOOLBAR
325
326 void wxFrame::SetToolBar(wxToolBar *toolbar)
327 {
328 m_frameToolBar = toolbar;
329 if (toolbar)
330 {
331 if (toolbar->IsVertical())
332 {
333 // Vertical toolbar and m_wxwindow go into an hbox, inside the
334 // vbox (m_mainWidget). hbox is created on demand.
335 GtkWidget* hbox = gtk_widget_get_parent(m_wxwindow);
336 if (hbox == m_mainWidget)
337 {
338 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
339 gtk_widget_show(hbox);
340 gtk_box_pack_start(GTK_BOX(m_mainWidget), hbox, true, true, 0);
341 gtk_widget_reparent(m_wxwindow, hbox);
342 }
343 gtk_widget_reparent(toolbar->m_widget, hbox);
344 gtk_box_set_child_packing(GTK_BOX(hbox),
345 toolbar->m_widget, false, false, 0, GTK_PACK_START);
346
347 int pos = 0; // left
348 if (toolbar->HasFlag(wxTB_RIGHT))
349 pos = 1; // right
350 gtk_box_reorder_child(GTK_BOX(hbox), toolbar->m_widget, pos);
351 }
352 else
353 {
354 // Horizontal toolbar goes into vbox (m_mainWidget)
355 gtk_widget_reparent(toolbar->m_widget, m_mainWidget);
356 gtk_box_set_child_packing(GTK_BOX(m_mainWidget),
357 toolbar->m_widget, false, false, 0, GTK_PACK_START);
358
359 int pos = 0; // top
360 if (m_frameMenuBar)
361 pos = 1; // below menubar
362 if (toolbar->HasFlag(wxTB_BOTTOM))
363 pos += 2; // below client area (m_wxwindow)
364 gtk_box_reorder_child(
365 GTK_BOX(m_mainWidget), toolbar->m_widget, pos);
366 }
367 // reset size request to allow native sizing to work
368 gtk_widget_set_size_request(toolbar->m_widget, -1, -1);
369 }
370 // make sure next size_allocate causes a wxSizeEvent
371 m_useCachedClientSize = false;
372 m_clientWidth = 0;
373 }
374
375 #endif // wxUSE_TOOLBAR
376
377 #if wxUSE_STATUSBAR
378
379 void wxFrame::SetStatusBar(wxStatusBar *statbar)
380 {
381 m_frameStatusBar = statbar;
382 if (statbar)
383 {
384 // statusbar goes into bottom of vbox (m_mainWidget)
385 gtk_widget_reparent(statbar->m_widget, m_mainWidget);
386 gtk_box_set_child_packing(GTK_BOX(m_mainWidget),
387 statbar->m_widget, false, false, 0, GTK_PACK_END);
388 // make sure next size_allocate on statusbar causes a size event
389 statbar->m_useCachedClientSize = false;
390 statbar->m_clientWidth = 0;
391 int h = -1;
392 if (statbar->m_wxwindow)
393 {
394 // statusbar is not a native widget, need to set height request
395 h = statbar->m_height;
396 }
397 gtk_widget_set_size_request(statbar->m_widget, -1, h);
398 }
399 // make sure next size_allocate causes a wxSizeEvent
400 m_useCachedClientSize = false;
401 m_clientWidth = 0;
402 }
403 #endif // wxUSE_STATUSBAR