Fixed bug in wxListCtrl
[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 EVT_ACTIVATE(wxMDIChildFrame::OnActivate)
180 END_EVENT_TABLE()
181
182 wxMDIChildFrame::wxMDIChildFrame(void)
183 {
184 m_menuBar = NULL;
185 m_page = NULL;
186 };
187
188 wxMDIChildFrame::wxMDIChildFrame( wxMDIParentFrame *parent,
189 wxWindowID id, const wxString& title,
190 const wxPoint& WXUNUSED(pos), const wxSize& size,
191 long style, const wxString& name )
192 {
193 m_menuBar = NULL;
194 m_page = NULL;
195 Create( parent, id, title, wxDefaultPosition, size, style, name );
196 };
197
198 wxMDIChildFrame::~wxMDIChildFrame(void)
199 {
200 if (m_menuBar)
201 {
202 wxMDIParentFrame *mdi_frame = (wxMDIParentFrame*)m_parent->m_parent;
203 if (mdi_frame->m_currentChild == this)
204 {
205 mdi_frame->SetMDIMenuBar( NULL );
206 mdi_frame->m_currentChild = NULL;
207 };
208 delete m_menuBar;
209 }
210 };
211
212 bool wxMDIChildFrame::Create( wxMDIParentFrame *parent,
213 wxWindowID id, const wxString& title,
214 const wxPoint& WXUNUSED(pos), const wxSize& size,
215 long style, const wxString& name )
216 {
217 m_title = title;
218 return wxPanel::Create( parent->GetClientWindow(), id, wxDefaultPosition, size, style, name );
219 };
220
221 void wxMDIChildFrame::OnCloseWindow( wxCloseEvent &event )
222 {
223 if ( GetEventHandler()->OnClose() || event.GetForce())
224 {
225 this->Destroy();
226 }
227 };
228
229 void wxMDIChildFrame::OnSize( wxSizeEvent &WXUNUSED(event) )
230 {
231 if ( GetAutoLayout() )
232 Layout();
233 else {
234 // no child: go out !
235 if (!GetChildren()->First())
236 return;
237
238 // do we have exactly one child?
239 wxWindow *child = NULL;
240 for(wxNode *node = GetChildren()->First(); node; node = node->Next())
241 {
242 wxWindow *win = (wxWindow *)node->Data();
243 if (!IS_KIND_OF(win,wxFrame) && !IS_KIND_OF(win,wxDialog))
244 {
245 if ( child ) // it's the second one: do nothing
246 return;
247
248 child = win;
249 };
250 };
251
252 // yes: set it's size to fill all the frame
253 int client_x, client_y;
254 GetClientSize(&client_x, &client_y);
255 child->SetSize( 1, 1, client_x-2, client_y);
256 }
257 };
258
259 bool wxMDIChildFrame::Destroy(void)
260 {
261 if (!wxPendingDelete.Member(this))
262 wxPendingDelete.Append(this);
263
264 return TRUE;
265 }
266
267 static void SetInvokingWindow( wxMenu *menu, wxWindow *win )
268 {
269 menu->SetInvokingWindow( win );
270 wxNode *node = menu->m_items.First();
271 while (node)
272 {
273 wxMenuItem *menuitem = (wxMenuItem*)node->Data();
274 if (menuitem->IsSubMenu())
275 SetInvokingWindow( menuitem->GetSubMenu(), win );
276 node = node->Next();
277 };
278 };
279
280 void wxMDIChildFrame::SetMenuBar( wxMenuBar *menu_bar )
281 {
282 m_menuBar = menu_bar;
283
284 if (m_menuBar)
285 {
286 wxMDIParentFrame *mdi_frame = (wxMDIParentFrame*)m_parent->m_parent;
287
288 if (m_menuBar->m_parent != this)
289 {
290 wxNode *node = m_menuBar->m_menus.First();
291 while (node)
292 {
293 wxMenu *menu = (wxMenu*)node->Data();
294 SetInvokingWindow( menu, this );
295 node = node->Next();
296 };
297
298 m_menuBar->m_parent = mdi_frame;
299 }
300 mdi_frame->SetMDIMenuBar( m_menuBar );
301
302 gtk_myfixed_put( GTK_MYFIXED(mdi_frame->m_mainWindow),
303 m_menuBar->m_widget, m_menuBar->m_x, m_menuBar->m_y );
304 }
305 };
306
307 void wxMDIChildFrame::Activate(void)
308 {
309 };
310
311 void wxMDIChildFrame::OnActivate( wxActivateEvent &WXUNUSED(event) )
312 {
313 };
314
315 //-----------------------------------------------------------------------------
316 // wxMDIClientWindow
317 //-----------------------------------------------------------------------------
318
319 IMPLEMENT_DYNAMIC_CLASS(wxMDIClientWindow,wxWindow)
320
321 wxMDIClientWindow::wxMDIClientWindow(void)
322 {
323 };
324
325 wxMDIClientWindow::wxMDIClientWindow( wxMDIParentFrame *parent, long style )
326 {
327 CreateClient( parent, style );
328 };
329
330 wxMDIClientWindow::~wxMDIClientWindow(void)
331 {
332 };
333
334 bool wxMDIClientWindow::CreateClient( wxMDIParentFrame *parent, long style )
335 {
336 m_needParent = TRUE;
337
338 PreCreation( parent, -1, wxPoint(10,10), wxSize(100,100), style, "wxMDIClientWindow" );
339
340 m_widget = gtk_notebook_new();
341
342 gtk_signal_connect( GTK_OBJECT(m_widget), "switch_page",
343 GTK_SIGNAL_FUNC(gtk_page_change_callback), (gpointer)this );
344
345 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget), 1 );
346
347 PostCreation();
348
349 Show( TRUE );
350
351 return TRUE;
352 };
353
354 void wxMDIClientWindow::AddChild( wxWindow *child )
355 {
356 if (!child->IsKindOf(CLASSINFO(wxMDIChildFrame)))
357 {
358 wxFAIL_MSG("wxNotebook::AddChild: Child has to be wxMDIChildFrame");
359 return;
360 };
361
362 m_children.Append( child );
363
364 wxString s;
365 wxMDIChildFrame* mdi_child = (wxMDIChildFrame*) child;
366 s = mdi_child->m_title;
367 if (s.IsNull()) s = "MDI child";
368
369 GtkWidget *label_widget;
370 label_widget = gtk_label_new( s );
371 gtk_misc_set_alignment( GTK_MISC(label_widget), 0.0, 0.5 );
372
373 gtk_signal_connect( GTK_OBJECT(child->m_widget), "size_allocate",
374 GTK_SIGNAL_FUNC(gtk_page_size_callback), (gpointer)child );
375
376 gtk_notebook_append_page( GTK_NOTEBOOK(m_widget), child->m_widget, label_widget );
377
378 mdi_child->m_page = (GtkNotebookPage*) (g_list_last(GTK_NOTEBOOK(m_widget)->children)->data);
379
380 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget), m_children.Number()-1 );
381
382 gtk_page_change_callback( NULL, mdi_child->m_page, 0, this );
383 };
384
385