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