]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/mdi.cpp
Add some version checks to help compiling on OSX.
[wxWidgets.git] / src / gtk / mdi.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/mdi.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 #if wxUSE_MDI
14
15 #include "wx/mdi.h"
16
17 #ifndef WX_PRECOMP
18 #include "wx/intl.h"
19 #include "wx/menu.h"
20 #endif
21
22 #include "wx/gtk/private.h"
23
24 //-----------------------------------------------------------------------------
25 // "switch_page"
26 //-----------------------------------------------------------------------------
27
28 extern "C" {
29 static void
30 switch_page(GtkNotebook* widget, GtkNotebookPage*, guint page_num, wxMDIParentFrame* parent)
31 {
32 // send deactivate event to old child
33
34 wxMDIChildFrame *child = parent->GetActiveChild();
35 if (child)
36 {
37 wxActivateEvent event1( wxEVT_ACTIVATE, false, child->GetId() );
38 event1.SetEventObject( child);
39 child->HandleWindowEvent( event1 );
40 }
41
42 // send activate event to new child
43
44 wxMDIClientWindowBase *client_window = parent->GetClientWindow();
45 if ( !client_window )
46 return;
47
48 child = NULL;
49 GtkWidget* page = gtk_notebook_get_nth_page(widget, page_num);
50
51 wxWindowList::compatibility_iterator node = client_window->GetChildren().GetFirst();
52 while ( node )
53 {
54 wxMDIChildFrame *child_frame = wxDynamicCast( node->GetData(), wxMDIChildFrame );
55
56 // child_frame can be NULL when this is called from dtor, probably
57 // because g_signal_connect (m_widget, "switch_page", (see below)
58 // isn't deleted early enough
59 if (child_frame && child_frame->m_widget == page)
60 {
61 child = child_frame;
62 break;
63 }
64 node = node->GetNext();
65 }
66
67 if (!child)
68 return;
69
70 wxActivateEvent event2( wxEVT_ACTIVATE, true, child->GetId() );
71 event2.SetEventObject( child);
72 child->HandleWindowEvent( event2 );
73 }
74 }
75
76 //-----------------------------------------------------------------------------
77 // wxMDIParentFrame
78 //-----------------------------------------------------------------------------
79
80 IMPLEMENT_DYNAMIC_CLASS(wxMDIParentFrame,wxFrame)
81
82 void wxMDIParentFrame::Init()
83 {
84 m_justInserted = false;
85 }
86
87 bool wxMDIParentFrame::Create(wxWindow *parent,
88 wxWindowID id,
89 const wxString& title,
90 const wxPoint& pos,
91 const wxSize& size,
92 long style,
93 const wxString& name )
94 {
95 if ( !wxFrame::Create( parent, id, title, pos, size, style, name ) )
96 return false;
97
98 m_clientWindow = OnCreateClient();
99 if ( !m_clientWindow->CreateClient(this, GetWindowStyleFlag()) )
100 return false;
101
102 return true;
103 }
104
105 void wxMDIParentFrame::OnInternalIdle()
106 {
107 /* if a MDI child window has just been inserted
108 it has to be brought to the top in idle time. we
109 simply set the last notebook page active as new
110 pages can only be appended at the end */
111
112 if (m_justInserted)
113 {
114 GtkNotebook *notebook = GTK_NOTEBOOK(m_clientWindow->m_widget);
115 gtk_notebook_set_current_page(notebook, -1);
116
117 /* need to set the menubar of the child */
118 wxMDIChildFrame *active_child_frame = GetActiveChild();
119 if (active_child_frame != NULL)
120 {
121 wxMenuBar *menu_bar = active_child_frame->m_menuBar;
122 if (menu_bar)
123 {
124 menu_bar->Attach(active_child_frame);
125 }
126 }
127 m_justInserted = false;
128 return;
129 }
130
131 wxFrame::OnInternalIdle();
132
133 wxMDIChildFrame *active_child_frame = GetActiveChild();
134 bool visible_child_menu = false;
135
136 wxWindowList::compatibility_iterator node = m_clientWindow->GetChildren().GetFirst();
137 while (node)
138 {
139 wxMDIChildFrame *child_frame = wxDynamicCast( node->GetData(), wxMDIChildFrame );
140
141 if ( child_frame )
142 {
143 wxMenuBar *menu_bar = child_frame->m_menuBar;
144 if ( menu_bar )
145 {
146 if (child_frame == active_child_frame)
147 {
148 if (menu_bar->Show(true))
149 {
150 // Attach() asserts if we call it for an already
151 // attached menu bar so don't do it if we're already
152 // associated with this frame (it would be nice to get
153 // rid of this check and ensure that this doesn't
154 // happen...)
155 if ( menu_bar->GetFrame() != child_frame )
156 menu_bar->Attach( child_frame );
157 }
158 visible_child_menu = true;
159 }
160 else
161 {
162 if (menu_bar->Show(false))
163 {
164 menu_bar->Detach();
165 }
166 }
167 }
168 }
169
170 node = node->GetNext();
171 }
172
173 /* show/hide parent menu bar as required */
174 if ((m_frameMenuBar) &&
175 (m_frameMenuBar->IsShown() == visible_child_menu))
176 {
177 if (visible_child_menu)
178 {
179 m_frameMenuBar->Show( false );
180 m_frameMenuBar->Detach();
181 }
182 else
183 {
184 m_frameMenuBar->Show( true );
185 m_frameMenuBar->Attach( this );
186 }
187 }
188 }
189
190 void wxMDIParentFrame::DoGetClientSize(int* width, int* height) const
191 {
192 wxFrame::DoGetClientSize(width, height);
193
194 if (height)
195 {
196 wxMDIChildFrame* active_child_frame = GetActiveChild();
197 if (active_child_frame)
198 {
199 wxMenuBar* menubar = active_child_frame->m_menuBar;
200 if (menubar && menubar->IsShown())
201 {
202 GtkRequisition req;
203 gtk_widget_size_request(menubar->m_widget, &req);
204 *height -= req.height;
205 if (*height < 0) *height = 0;
206 }
207 }
208 }
209 }
210
211 wxMDIChildFrame *wxMDIParentFrame::GetActiveChild() const
212 {
213 if (!m_clientWindow) return NULL;
214
215 GtkNotebook *notebook = GTK_NOTEBOOK(m_clientWindow->m_widget);
216 if (!notebook) return NULL;
217
218 gint i = gtk_notebook_get_current_page( notebook );
219 if (i < 0) return NULL;
220
221 GtkWidget* page = gtk_notebook_get_nth_page(notebook, i);
222 if (!page) return NULL;
223
224 wxWindowList::compatibility_iterator node = m_clientWindow->GetChildren().GetFirst();
225 while (node)
226 {
227 if ( wxPendingDelete.Member(node->GetData()) )
228 return NULL;
229
230 wxMDIChildFrame *child_frame = wxDynamicCast( node->GetData(), wxMDIChildFrame );
231
232 if (!child_frame)
233 return NULL;
234
235 if (child_frame->m_widget == page)
236 return child_frame;
237
238 node = node->GetNext();
239 }
240
241 return NULL;
242 }
243
244 void wxMDIParentFrame::ActivateNext()
245 {
246 if (m_clientWindow)
247 gtk_notebook_next_page( GTK_NOTEBOOK(m_clientWindow->m_widget) );
248 }
249
250 void wxMDIParentFrame::ActivatePrevious()
251 {
252 if (m_clientWindow)
253 gtk_notebook_prev_page( GTK_NOTEBOOK(m_clientWindow->m_widget) );
254 }
255
256 //-----------------------------------------------------------------------------
257 // wxMDIChildFrame
258 //-----------------------------------------------------------------------------
259
260 IMPLEMENT_DYNAMIC_CLASS(wxMDIChildFrame,wxFrame)
261
262 BEGIN_EVENT_TABLE(wxMDIChildFrame, wxFrame)
263 EVT_ACTIVATE(wxMDIChildFrame::OnActivate)
264 EVT_MENU_HIGHLIGHT_ALL(wxMDIChildFrame::OnMenuHighlight)
265 END_EVENT_TABLE()
266
267 void wxMDIChildFrame::Init()
268 {
269 m_menuBar = NULL;
270 }
271
272 bool wxMDIChildFrame::Create(wxMDIParentFrame *parent,
273 wxWindowID id,
274 const wxString& title,
275 const wxPoint& WXUNUSED(pos),
276 const wxSize& size,
277 long style,
278 const wxString& name)
279 {
280 m_mdiParent = parent;
281 m_title = title;
282
283 return wxWindow::Create(parent->GetClientWindow(), id,
284 wxDefaultPosition, size,
285 style, name);
286 }
287
288 wxMDIChildFrame::~wxMDIChildFrame()
289 {
290 delete m_menuBar;
291
292 // wxMDIClientWindow does not get redrawn properly after last child is removed
293 if (m_parent && m_parent->GetChildren().size() <= 1)
294 gtk_widget_queue_draw(m_parent->m_widget);
295 }
296
297 void wxMDIChildFrame::SetMenuBar( wxMenuBar *menu_bar )
298 {
299 wxASSERT_MSG( m_menuBar == NULL, "Only one menubar allowed" );
300
301 m_menuBar = menu_bar;
302
303 if (m_menuBar)
304 {
305 wxMDIParentFrame *mdi_frame = (wxMDIParentFrame*)m_parent->GetParent();
306
307 m_menuBar->SetParent( mdi_frame );
308
309 /* insert the invisible menu bar into the _parent_ mdi frame */
310 m_menuBar->Show(false);
311 gtk_box_pack_start(GTK_BOX(mdi_frame->m_mainWidget), m_menuBar->m_widget, false, false, 0);
312 gtk_box_reorder_child(GTK_BOX(mdi_frame->m_mainWidget), m_menuBar->m_widget, 0);
313
314 gulong handler_id = g_signal_handler_find(
315 m_menuBar->m_widget,
316 GSignalMatchType(G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DATA),
317 g_signal_lookup("size_request", GTK_TYPE_WIDGET),
318 0, NULL, NULL, m_menuBar);
319 if (handler_id != 0)
320 g_signal_handler_disconnect(m_menuBar->m_widget, handler_id);
321 gtk_widget_set_size_request(m_menuBar->m_widget, -1, -1);
322 }
323 }
324
325 wxMenuBar *wxMDIChildFrame::GetMenuBar() const
326 {
327 return m_menuBar;
328 }
329
330 GtkNotebook *wxMDIChildFrame::GTKGetNotebook() const
331 {
332 wxMDIClientWindow * const
333 client = wxStaticCast(GetParent(), wxMDIClientWindow);
334 wxCHECK( client, NULL );
335
336 return GTK_NOTEBOOK(client->m_widget);
337 }
338
339 void wxMDIChildFrame::Activate()
340 {
341 GtkNotebook * const notebook = GTKGetNotebook();
342 wxCHECK_RET( notebook, "no parent notebook?" );
343
344 gint pageno = gtk_notebook_page_num( notebook, m_widget );
345 gtk_notebook_set_current_page( notebook, pageno );
346 }
347
348 void wxMDIChildFrame::OnActivate( wxActivateEvent& WXUNUSED(event) )
349 {
350 }
351
352 void wxMDIChildFrame::OnMenuHighlight( wxMenuEvent& event )
353 {
354 #if wxUSE_STATUSBAR
355 wxMDIParentFrame *mdi_frame = (wxMDIParentFrame*)m_parent->GetParent();
356 if ( !ShowMenuHelp(event.GetMenuId()) )
357 {
358 // we don't have any help text for this item, but may be the MDI frame
359 // does?
360 mdi_frame->OnMenuHighlight(event);
361 }
362 #endif // wxUSE_STATUSBAR
363 }
364
365 void wxMDIChildFrame::SetTitle( const wxString &title )
366 {
367 if ( title == m_title )
368 return;
369
370 m_title = title;
371
372 GtkNotebook * const notebook = GTKGetNotebook();
373 wxCHECK_RET( notebook, "no parent notebook?" );
374 gtk_notebook_set_tab_label_text(notebook, m_widget, wxGTK_CONV( title ) );
375 }
376
377 //-----------------------------------------------------------------------------
378 // wxMDIClientWindow
379 //-----------------------------------------------------------------------------
380
381 IMPLEMENT_DYNAMIC_CLASS(wxMDIClientWindow, wxWindow)
382
383 wxMDIClientWindow::~wxMDIClientWindow()
384 {
385 // disconnect our handler because our ~wxWindow (which is going to be called
386 // after this dtor) will call DestroyChildren(); in turns our children
387 // ~wxWindow dtors will call wxWindow::Show(false) and this will generate
388 // a call to gtk_mdi_page_change_callback with an invalid parent
389 // (because gtk_mdi_page_change_callback expects a wxMDIClientWindow but
390 // at that point of the dtor chain we are a simple wxWindow!)
391 g_signal_handlers_disconnect_by_func(m_widget, (void*)switch_page, GetParent());
392 }
393
394 bool wxMDIClientWindow::CreateClient(wxMDIParentFrame *parent, long style)
395 {
396 if ( !PreCreation( parent, wxDefaultPosition, wxDefaultSize ) ||
397 !CreateBase( parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
398 style, wxDefaultValidator, "wxMDIClientWindow" ))
399 {
400 wxFAIL_MSG( "wxMDIClientWindow creation failed" );
401 return false;
402 }
403
404 m_widget = gtk_notebook_new();
405 g_object_ref(m_widget);
406
407 g_signal_connect(m_widget, "switch_page", G_CALLBACK(switch_page), parent);
408
409 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget), 1 );
410
411 m_parent->DoAddChild( this );
412
413 PostCreation();
414
415 Show( true );
416
417 return true;
418 }
419
420 void wxMDIClientWindow::AddChildGTK(wxWindowGTK* child)
421 {
422 wxMDIChildFrame* child_frame = static_cast<wxMDIChildFrame*>(child);
423 wxString s = child_frame->GetTitle();
424 if ( s.empty() )
425 s = _("MDI child");
426
427 GtkWidget *label_widget = gtk_label_new( s.mbc_str() );
428 gtk_misc_set_alignment( GTK_MISC(label_widget), 0.0, 0.5 );
429
430 GtkNotebook* notebook = GTK_NOTEBOOK(m_widget);
431
432 gtk_notebook_append_page( notebook, child->m_widget, label_widget );
433
434 wxMDIParentFrame* parent_frame = static_cast<wxMDIParentFrame*>(GetParent());
435 parent_frame->m_justInserted = true;
436 }
437
438 #endif // wxUSE_MDI