]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/mdi.cpp
* Added Peek() to wxMemoryInputStream.
[wxWidgets.git] / src / gtk1 / mdi.cpp
CommitLineData
c801d85f
KB
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"
e3e65dac 16#include "wx/dialog.h"
716b7364
RR
17#include "wx/gtk/win_gtk.h"
18
19//-----------------------------------------------------------------------------
20
21extern wxList wxPendingDelete;
c801d85f
KB
22
23//-----------------------------------------------------------------------------
24// wxMDIParentFrame
25//-----------------------------------------------------------------------------
26
33d0b396
RR
27static 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
716b7364
RR
40// page change callback
41static 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
33d0b396
RR
61//-----------------------------------------------------------------------------
62
c801d85f
KB
63IMPLEMENT_DYNAMIC_CLASS(wxMDIParentFrame,wxFrame)
64
716b7364
RR
65BEGIN_EVENT_TABLE(wxMDIParentFrame, wxFrame)
66END_EVENT_TABLE()
67
c801d85f
KB
68wxMDIParentFrame::wxMDIParentFrame(void)
69{
70 m_clientWindow = NULL;
71 m_currentChild = NULL;
72 m_parentFrameActive = TRUE;
73};
74
75wxMDIParentFrame::wxMDIParentFrame( wxWindow *parent,
debe6624 76 wxWindowID id, const wxString& title,
c801d85f 77 const wxPoint& pos, const wxSize& size,
debe6624 78 long style, const wxString& name )
c801d85f
KB
79{
80 m_clientWindow = NULL;
81 m_currentChild = NULL;
82 m_parentFrameActive = TRUE;
83 Create( parent, id, title, pos, size, style, name );
84};
85
86wxMDIParentFrame::~wxMDIParentFrame(void)
87{
88};
89
90bool wxMDIParentFrame::Create( wxWindow *parent,
debe6624 91 wxWindowID id, const wxString& title,
c801d85f 92 const wxPoint& pos, const wxSize& size,
debe6624 93 long style, const wxString& name )
c801d85f
KB
94{
95 wxFrame::Create( parent, id, title, pos, size, style, name );
96
97 OnCreateClient();
98
99 return TRUE;
100};
101
716b7364 102void wxMDIParentFrame::GtkOnSize( int x, int y, int width, int height )
c801d85f 103{
716b7364
RR
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 }
c801d85f
KB
113};
114
716b7364
RR
115void 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
129void wxMDIParentFrame::GetClientSize(int *width, int *height ) const
c801d85f 130{
716b7364 131 wxFrame::GetClientSize( width, height );
c801d85f
KB
132};
133
c801d85f
KB
134wxMDIChildFrame *wxMDIParentFrame::GetActiveChild(void) const
135{
136 return m_currentChild;
137};
138
139wxMDIClientWindow *wxMDIParentFrame::GetClientWindow(void) const
140{
141 return m_clientWindow;
142};
143
144wxMDIClientWindow *wxMDIParentFrame::OnCreateClient(void)
145{
146 m_clientWindow = new wxMDIClientWindow( this );
147 return m_clientWindow;
148};
149
150void wxMDIParentFrame::ActivateNext(void)
151{
716b7364
RR
152 if (m_clientWindow)
153 gtk_notebook_next_page( GTK_NOTEBOOK(m_clientWindow->m_widget) );
c801d85f
KB
154};
155
156void wxMDIParentFrame::ActivatePrevious(void)
716b7364
RR
157{
158 if (m_clientWindow)
159 gtk_notebook_prev_page( GTK_NOTEBOOK(m_clientWindow->m_widget) );
160};
161
162void wxMDIParentFrame::OnActivate( wxActivateEvent& WXUNUSED(event) )
c801d85f
KB
163{
164};
165
166void wxMDIParentFrame::OnSysColourChanged( wxSysColourChangedEvent& WXUNUSED(event) )
167{
168};
169
170//-----------------------------------------------------------------------------
171// wxMDIChildFrame
172//-----------------------------------------------------------------------------
173
174IMPLEMENT_DYNAMIC_CLASS(wxMDIChildFrame,wxPanel)
175
716b7364
RR
176BEGIN_EVENT_TABLE(wxMDIChildFrame, wxPanel)
177 EVT_CLOSE(wxMDIChildFrame::OnCloseWindow)
e3e65dac 178 EVT_SIZE(wxMDIChildFrame::OnSize)
716b7364
RR
179END_EVENT_TABLE()
180
c801d85f
KB
181wxMDIChildFrame::wxMDIChildFrame(void)
182{
716b7364
RR
183 m_menuBar = NULL;
184 m_page = NULL;
c801d85f
KB
185};
186
187wxMDIChildFrame::wxMDIChildFrame( wxMDIParentFrame *parent,
debe6624 188 wxWindowID id, const wxString& title,
33d0b396 189 const wxPoint& WXUNUSED(pos), const wxSize& size,
debe6624 190 long style, const wxString& name )
c801d85f 191{
716b7364
RR
192 m_menuBar = NULL;
193 m_page = NULL;
33d0b396 194 Create( parent, id, title, wxDefaultPosition, size, style, name );
c801d85f
KB
195};
196
197wxMDIChildFrame::~wxMDIChildFrame(void)
198{
716b7364
RR
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 }
c801d85f
KB
209};
210
211bool wxMDIChildFrame::Create( wxMDIParentFrame *parent,
debe6624 212 wxWindowID id, const wxString& title,
33d0b396 213 const wxPoint& WXUNUSED(pos), const wxSize& size,
debe6624 214 long style, const wxString& name )
c801d85f
KB
215{
216 m_title = title;
33d0b396 217 return wxPanel::Create( parent->GetClientWindow(), id, wxDefaultPosition, size, style, name );
c801d85f
KB
218};
219
716b7364 220void wxMDIChildFrame::OnCloseWindow( wxCloseEvent &event )
c801d85f 221{
716b7364
RR
222 if ( GetEventHandler()->OnClose() || event.GetForce())
223 {
224 this->Destroy();
225 }
226};
227
e3e65dac
RR
228void 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};
716b7364
RR
257bool wxMDIChildFrame::Destroy(void)
258{
259 if (!wxPendingDelete.Member(this))
260 wxPendingDelete.Append(this);
261
262 return TRUE;
263}
264
265static 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();
46dc76ba
RR
272 if (menuitem->IsSubMenu())
273 SetInvokingWindow( menuitem->GetSubMenu(), win );
716b7364
RR
274 node = node->Next();
275 };
276};
277
278void 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 }
c801d85f
KB
303};
304
305void wxMDIChildFrame::Activate(void)
306{
307};
308
309//-----------------------------------------------------------------------------
310// wxMDIClientWindow
311//-----------------------------------------------------------------------------
312
313IMPLEMENT_DYNAMIC_CLASS(wxMDIClientWindow,wxWindow)
314
315wxMDIClientWindow::wxMDIClientWindow(void)
316{
317};
318
debe6624 319wxMDIClientWindow::wxMDIClientWindow( wxMDIParentFrame *parent, long style )
c801d85f
KB
320{
321 CreateClient( parent, style );
322};
323
324wxMDIClientWindow::~wxMDIClientWindow(void)
325{
326};
327
debe6624 328bool wxMDIClientWindow::CreateClient( wxMDIParentFrame *parent, long style )
c801d85f
KB
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
716b7364
RR
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
c801d85f
KB
341 PostCreation();
342
343 Show( TRUE );
344
345 return TRUE;
346};
347
348void wxMDIClientWindow::AddChild( wxWindow *child )
349{
716b7364 350 if (!child->IsKindOf(CLASSINFO(wxMDIChildFrame)))
c801d85f 351 {
716b7364
RR
352 wxFAIL_MSG("wxNotebook::AddChild: Child has to be wxMDIChildFrame");
353 return;
c801d85f
KB
354 };
355
716b7364
RR
356 m_children.Append( child );
357
358 wxString s;
359 wxMDIChildFrame* mdi_child = (wxMDIChildFrame*) child;
360 s = mdi_child->m_title;
c801d85f
KB
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 );
33d0b396
RR
366
367 gtk_signal_connect( GTK_OBJECT(child->m_widget), "size_allocate",
368 GTK_SIGNAL_FUNC(gtk_page_size_callback), (gpointer)child );
46dc76ba 369
c801d85f 370 gtk_notebook_append_page( GTK_NOTEBOOK(m_widget), child->m_widget, label_widget );
716b7364
RR
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 );
7b5408ea
RR
375
376 gtk_page_change_callback( NULL, mdi_child->m_page, 0, this );
c801d85f
KB
377};
378
379