]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/mdi.cpp
Disable wxUSE_ENH_METAFILE for wxGTK builds.
[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 (!m_useCachedClientSize && 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_get_preferred_height(menubar->m_widget, NULL, &req.height);
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::GTKHandleRealized()
298 {
299 // since m_widget is not a GtkWindow, must bypass wxTopLevelWindowGTK
300 wxTopLevelWindowBase::GTKHandleRealized();
301 }
302
303 void wxMDIChildFrame::SetMenuBar( wxMenuBar *menu_bar )
304 {
305 wxASSERT_MSG( m_menuBar == NULL, "Only one menubar allowed" );
306
307 m_menuBar = menu_bar;
308
309 if (m_menuBar)
310 {
311 wxMDIParentFrame *mdi_frame = (wxMDIParentFrame*)m_parent->GetParent();
312
313 m_menuBar->SetParent( mdi_frame );
314
315 /* insert the invisible menu bar into the _parent_ mdi frame */
316 m_menuBar->Show(false);
317 gtk_box_pack_start(GTK_BOX(mdi_frame->m_mainWidget), m_menuBar->m_widget, false, false, 0);
318 gtk_box_reorder_child(GTK_BOX(mdi_frame->m_mainWidget), m_menuBar->m_widget, 0);
319 gtk_widget_set_size_request(m_menuBar->m_widget, -1, -1);
320 }
321 }
322
323 wxMenuBar *wxMDIChildFrame::GetMenuBar() const
324 {
325 return m_menuBar;
326 }
327
328 GtkNotebook *wxMDIChildFrame::GTKGetNotebook() const
329 {
330 wxMDIClientWindow * const
331 client = wxStaticCast(GetParent(), wxMDIClientWindow);
332 wxCHECK( client, NULL );
333
334 return GTK_NOTEBOOK(client->m_widget);
335 }
336
337 void wxMDIChildFrame::Activate()
338 {
339 GtkNotebook * const notebook = GTKGetNotebook();
340 wxCHECK_RET( notebook, "no parent notebook?" );
341
342 gint pageno = gtk_notebook_page_num( notebook, m_widget );
343 gtk_notebook_set_current_page( notebook, pageno );
344 }
345
346 void wxMDIChildFrame::OnActivate( wxActivateEvent& WXUNUSED(event) )
347 {
348 }
349
350 void wxMDIChildFrame::OnMenuHighlight( wxMenuEvent& event )
351 {
352 #if wxUSE_STATUSBAR
353 wxMDIParentFrame *mdi_frame = (wxMDIParentFrame*)m_parent->GetParent();
354 if ( !ShowMenuHelp(event.GetMenuId()) )
355 {
356 // we don't have any help text for this item, but may be the MDI frame
357 // does?
358 mdi_frame->OnMenuHighlight(event);
359 }
360 #endif // wxUSE_STATUSBAR
361 }
362
363 void wxMDIChildFrame::SetTitle( const wxString &title )
364 {
365 if ( title == m_title )
366 return;
367
368 m_title = title;
369
370 GtkNotebook * const notebook = GTKGetNotebook();
371 wxCHECK_RET( notebook, "no parent notebook?" );
372 gtk_notebook_set_tab_label_text(notebook, m_widget, wxGTK_CONV( title ) );
373 }
374
375 //-----------------------------------------------------------------------------
376 // wxMDIClientWindow
377 //-----------------------------------------------------------------------------
378
379 IMPLEMENT_DYNAMIC_CLASS(wxMDIClientWindow, wxWindow)
380
381 wxMDIClientWindow::~wxMDIClientWindow()
382 {
383 // disconnect our handler because our ~wxWindow (which is going to be called
384 // after this dtor) will call DestroyChildren(); in turns our children
385 // ~wxWindow dtors will call wxWindow::Show(false) and this will generate
386 // a call to gtk_mdi_page_change_callback with an invalid parent
387 // (because gtk_mdi_page_change_callback expects a wxMDIClientWindow but
388 // at that point of the dtor chain we are a simple wxWindow!)
389 g_signal_handlers_disconnect_by_func(m_widget, (void*)switch_page, GetParent());
390 }
391
392 bool wxMDIClientWindow::CreateClient(wxMDIParentFrame *parent, long style)
393 {
394 if ( !PreCreation( parent, wxDefaultPosition, wxDefaultSize ) ||
395 !CreateBase( parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
396 style, wxDefaultValidator, "wxMDIClientWindow" ))
397 {
398 wxFAIL_MSG( "wxMDIClientWindow creation failed" );
399 return false;
400 }
401
402 m_widget = gtk_notebook_new();
403 g_object_ref(m_widget);
404
405 g_signal_connect(m_widget, "switch_page", G_CALLBACK(switch_page), parent);
406
407 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget), 1 );
408
409 m_parent->DoAddChild( this );
410
411 PostCreation();
412
413 Show( true );
414
415 return true;
416 }
417
418 void wxMDIClientWindow::AddChildGTK(wxWindowGTK* child)
419 {
420 wxMDIChildFrame* child_frame = static_cast<wxMDIChildFrame*>(child);
421 wxString s = child_frame->GetTitle();
422 if ( s.empty() )
423 s = _("MDI child");
424
425 GtkWidget *label_widget = gtk_label_new( s.mbc_str() );
426 gtk_misc_set_alignment( GTK_MISC(label_widget), 0.0, 0.5 );
427
428 GtkNotebook* notebook = GTK_NOTEBOOK(m_widget);
429
430 gtk_notebook_append_page( notebook, child->m_widget, label_widget );
431
432 wxMDIParentFrame* parent_frame = static_cast<wxMDIParentFrame*>(GetParent());
433 parent_frame->m_justInserted = true;
434 }
435
436 #endif // wxUSE_MDI