Fixed bug in MDI
[wxWidgets.git] / src / gtk / mdi.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: mdi.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 "mdi.h"
13 #endif
14
15 #include "wx/mdi.h"
16 #include "wx/dialog.h"
17 #include "wx/gtk/win_gtk.h"
18
19 //-----------------------------------------------------------------------------
20
21 extern wxList wxPendingDelete;
22
23 //-----------------------------------------------------------------------------
24 // wxMDIParentFrame
25 //-----------------------------------------------------------------------------
26
27 static void gtk_page_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxWindow *win )
28 {
29 if ((win->m_x == alloc->x) &&
30 (win->m_y == alloc->y) &&
31 (win->m_width == alloc->width) &&
32 (win->m_height == alloc->height))
33 {
34 return;
35 };
36
37 win->SetSize( alloc->x, alloc->y, alloc->width, alloc->height );
38 };
39
40 // page change callback
41 static void gtk_page_change_callback( GtkNotebook *WXUNUSED(widget),
42 GtkNotebookPage *page,
43 gint WXUNUSED(nPage),
44 wxMDIClientWindow *client_win )
45 {
46 wxNode *node = client_win->m_children.First();
47 while (node)
48 {
49 wxMDIChildFrame *child_frame = (wxMDIChildFrame *)node->Data();
50 if (child_frame->m_page == page)
51 {
52 wxMDIParentFrame *mdi_frame = (wxMDIParentFrame*)client_win->m_parent;
53 mdi_frame->m_currentChild = child_frame;
54 mdi_frame->SetMDIMenuBar( child_frame->m_menuBar );
55 return;
56 };
57 node = node->Next();
58 }
59 }
60
61 //-----------------------------------------------------------------------------
62
63 IMPLEMENT_DYNAMIC_CLASS(wxMDIParentFrame,wxFrame)
64
65 BEGIN_EVENT_TABLE(wxMDIParentFrame, wxFrame)
66 END_EVENT_TABLE()
67
68 wxMDIParentFrame::wxMDIParentFrame(void)
69 {
70 m_clientWindow = NULL;
71 m_currentChild = NULL;
72 m_parentFrameActive = TRUE;
73 };
74
75 wxMDIParentFrame::wxMDIParentFrame( wxWindow *parent,
76 wxWindowID id, const wxString& title,
77 const wxPoint& pos, const wxSize& size,
78 long style, const wxString& name )
79 {
80 m_clientWindow = NULL;
81 m_currentChild = NULL;
82 m_parentFrameActive = TRUE;
83 Create( parent, id, title, pos, size, style, name );
84 };
85
86 wxMDIParentFrame::~wxMDIParentFrame(void)
87 {
88 };
89
90 bool wxMDIParentFrame::Create( wxWindow *parent,
91 wxWindowID id, const wxString& title,
92 const wxPoint& pos, const wxSize& size,
93 long style, const wxString& name )
94 {
95 wxFrame::Create( parent, id, title, pos, size, style, name );
96
97 OnCreateClient();
98
99 return TRUE;
100 };
101
102 void wxMDIParentFrame::GtkOnSize( int x, int y, int width, int height )
103 {
104 wxFrame::GtkOnSize( x, y, width, height );
105
106 if (m_mdiMenuBar)
107 {
108 int x = 0;
109 int y = 0;
110 GetClientSize( &x, &y );
111 m_mdiMenuBar->SetSize( 1, 1, x-2, 26 );
112 }
113 };
114
115 void wxMDIParentFrame::SetMDIMenuBar( wxMenuBar *menu_bar )
116 {
117 if (m_mdiMenuBar) m_mdiMenuBar->Show( FALSE );
118 m_mdiMenuBar = menu_bar;
119 if (m_mdiMenuBar)
120 {
121 int x = 0;
122 int y = 0;
123 GetClientSize( &x, &y );
124 m_mdiMenuBar->SetSize( 1, 1, x-2, 26 );
125 m_mdiMenuBar->Show( TRUE );
126 }
127 };
128
129 void wxMDIParentFrame::GetClientSize(int *width, int *height ) const
130 {
131 wxFrame::GetClientSize( width, height );
132 };
133
134 wxMDIChildFrame *wxMDIParentFrame::GetActiveChild(void) const
135 {
136 return m_currentChild;
137 };
138
139 wxMDIClientWindow *wxMDIParentFrame::GetClientWindow(void) const
140 {
141 return m_clientWindow;
142 };
143
144 wxMDIClientWindow *wxMDIParentFrame::OnCreateClient(void)
145 {
146 m_clientWindow = new wxMDIClientWindow( this );
147 return m_clientWindow;
148 };
149
150 void wxMDIParentFrame::ActivateNext(void)
151 {
152 if (m_clientWindow)
153 gtk_notebook_next_page( GTK_NOTEBOOK(m_clientWindow->m_widget) );
154 };
155
156 void wxMDIParentFrame::ActivatePrevious(void)
157 {
158 if (m_clientWindow)
159 gtk_notebook_prev_page( GTK_NOTEBOOK(m_clientWindow->m_widget) );
160 };
161
162 void wxMDIParentFrame::OnActivate( wxActivateEvent& WXUNUSED(event) )
163 {
164 };
165
166 void wxMDIParentFrame::OnSysColourChanged( wxSysColourChangedEvent& WXUNUSED(event) )
167 {
168 };
169
170 //-----------------------------------------------------------------------------
171 // wxMDIChildFrame
172 //-----------------------------------------------------------------------------
173
174 IMPLEMENT_DYNAMIC_CLASS(wxMDIChildFrame,wxPanel)
175
176 BEGIN_EVENT_TABLE(wxMDIChildFrame, wxPanel)
177 EVT_CLOSE(wxMDIChildFrame::OnCloseWindow)
178 EVT_SIZE(wxMDIChildFrame::OnSize)
179 END_EVENT_TABLE()
180
181 wxMDIChildFrame::wxMDIChildFrame(void)
182 {
183 m_menuBar = NULL;
184 m_page = NULL;
185 };
186
187 wxMDIChildFrame::wxMDIChildFrame( wxMDIParentFrame *parent,
188 wxWindowID id, const wxString& title,
189 const wxPoint& WXUNUSED(pos), const wxSize& size,
190 long style, const wxString& name )
191 {
192 m_menuBar = NULL;
193 m_page = NULL;
194 Create( parent, id, title, wxDefaultPosition, size, style, name );
195 };
196
197 wxMDIChildFrame::~wxMDIChildFrame(void)
198 {
199 if (m_menuBar)
200 {
201 wxMDIParentFrame *mdi_frame = (wxMDIParentFrame*)m_parent->m_parent;
202 if (mdi_frame->m_currentChild == this)
203 {
204 mdi_frame->SetMDIMenuBar( NULL );
205 mdi_frame->m_currentChild = NULL;
206 };
207 delete m_menuBar;
208 }
209 };
210
211 bool wxMDIChildFrame::Create( wxMDIParentFrame *parent,
212 wxWindowID id, const wxString& title,
213 const wxPoint& WXUNUSED(pos), const wxSize& size,
214 long style, const wxString& name )
215 {
216 m_title = title;
217 return wxPanel::Create( parent->GetClientWindow(), id, wxDefaultPosition, size, style, name );
218 };
219
220 void wxMDIChildFrame::OnCloseWindow( wxCloseEvent &event )
221 {
222 if ( GetEventHandler()->OnClose() || event.GetForce())
223 {
224 this->Destroy();
225 }
226 };
227
228 void wxMDIChildFrame::OnSize( wxSizeEvent &WXUNUSED(event) )
229 {
230 if ( GetAutoLayout() )
231 Layout();
232 else {
233 // no child: go out !
234 if (!GetChildren()->First())
235 return;
236
237 // do we have exactly one child?
238 wxWindow *child = NULL;
239 for(wxNode *node = GetChildren()->First(); node; node = node->Next())
240 {
241 wxWindow *win = (wxWindow *)node->Data();
242 if (!IS_KIND_OF(win,wxFrame) && !IS_KIND_OF(win,wxDialog))
243 {
244 if ( child ) // it's the second one: do nothing
245 return;
246
247 child = win;
248 };
249 };
250
251 // yes: set it's size to fill all the frame
252 int client_x, client_y;
253 GetClientSize(&client_x, &client_y);
254 child->SetSize( 1, 1, client_x-2, client_y);
255 }
256 };
257 bool wxMDIChildFrame::Destroy(void)
258 {
259 if (!wxPendingDelete.Member(this))
260 wxPendingDelete.Append(this);
261
262 return TRUE;
263 }
264
265 static void SetInvokingWindow( wxMenu *menu, wxWindow *win )
266 {
267 menu->SetInvokingWindow( win );
268 wxNode *node = menu->m_items.First();
269 while (node)
270 {
271 wxMenuItem *menuitem = (wxMenuItem*)node->Data();
272 if (menuitem->IsSubMenu())
273 SetInvokingWindow( menuitem->GetSubMenu(), win );
274 node = node->Next();
275 };
276 };
277
278 void wxMDIChildFrame::SetMenuBar( wxMenuBar *menu_bar )
279 {
280 m_menuBar = menu_bar;
281
282 if (m_menuBar)
283 {
284 wxMDIParentFrame *mdi_frame = (wxMDIParentFrame*)m_parent->m_parent;
285
286 if (m_menuBar->m_parent != this)
287 {
288 wxNode *node = m_menuBar->m_menus.First();
289 while (node)
290 {
291 wxMenu *menu = (wxMenu*)node->Data();
292 SetInvokingWindow( menu, this );
293 node = node->Next();
294 };
295
296 m_menuBar->m_parent = mdi_frame;
297 }
298 mdi_frame->SetMDIMenuBar( m_menuBar );
299
300 gtk_myfixed_put( GTK_MYFIXED(mdi_frame->m_mainWindow),
301 m_menuBar->m_widget, m_menuBar->m_x, m_menuBar->m_y );
302 }
303 };
304
305 void wxMDIChildFrame::Activate(void)
306 {
307 };
308
309 //-----------------------------------------------------------------------------
310 // wxMDIClientWindow
311 //-----------------------------------------------------------------------------
312
313 IMPLEMENT_DYNAMIC_CLASS(wxMDIClientWindow,wxWindow)
314
315 wxMDIClientWindow::wxMDIClientWindow(void)
316 {
317 };
318
319 wxMDIClientWindow::wxMDIClientWindow( wxMDIParentFrame *parent, long style )
320 {
321 CreateClient( parent, style );
322 };
323
324 wxMDIClientWindow::~wxMDIClientWindow(void)
325 {
326 };
327
328 bool wxMDIClientWindow::CreateClient( wxMDIParentFrame *parent, long style )
329 {
330 m_needParent = TRUE;
331
332 PreCreation( parent, -1, wxPoint(10,10), wxSize(100,100), style, "wxMDIClientWindow" );
333
334 m_widget = gtk_notebook_new();
335
336 gtk_signal_connect( GTK_OBJECT(m_widget), "switch_page",
337 GTK_SIGNAL_FUNC(gtk_page_change_callback), (gpointer)this );
338
339 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget), 1 );
340
341 PostCreation();
342
343 Show( TRUE );
344
345 return TRUE;
346 };
347
348 void wxMDIClientWindow::AddChild( wxWindow *child )
349 {
350 if (!child->IsKindOf(CLASSINFO(wxMDIChildFrame)))
351 {
352 wxFAIL_MSG("wxNotebook::AddChild: Child has to be wxMDIChildFrame");
353 return;
354 };
355
356 m_children.Append( child );
357
358 wxString s;
359 wxMDIChildFrame* mdi_child = (wxMDIChildFrame*) child;
360 s = mdi_child->m_title;
361 if (s.IsNull()) s = "MDI child";
362
363 GtkWidget *label_widget;
364 label_widget = gtk_label_new( s );
365 gtk_misc_set_alignment( GTK_MISC(label_widget), 0.0, 0.5 );
366
367 gtk_signal_connect( GTK_OBJECT(child->m_widget), "size_allocate",
368 GTK_SIGNAL_FUNC(gtk_page_size_callback), (gpointer)child );
369
370 gtk_notebook_append_page( GTK_NOTEBOOK(m_widget), child->m_widget, label_widget );
371
372 mdi_child->m_page = (GtkNotebookPage*) (g_list_last(GTK_NOTEBOOK(m_widget)->children)->data);
373
374 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget), m_children.Number()-1 );
375
376 gtk_page_change_callback( NULL, mdi_child->m_page, 0, this );
377 };
378
379