]> git.saurik.com Git - wxWidgets.git/blob - utils/framelayout/src/frmview.cpp
Added MSW toplevel.cpp and regenerated makefiles and such.
[wxWidgets.git] / utils / framelayout / src / frmview.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: No names yet.
3 // Purpose: Contrib. demo
4 // Author: Aleksandras Gluchovas
5 // Modified by:
6 // Created: 02/01/99
7 // RCS-ID: $Id$
8 // Copyright: (c) Aleksandras Gluchovas
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "frmview.h"
14 // #pragma interface
15 #endif
16
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #ifndef WX_PRECOMP
25 #include "wx/wx.h"
26 #endif
27
28 #include "frmview.h"
29 #include "wx/utils.h"
30
31 /***** Implementation for class wxFrameView *****/
32
33 BEGIN_EVENT_TABLE( wxFrameView, wxEvtHandler )
34
35 EVT_IDLE( wxFrameView::OnIdle )
36
37 END_EVENT_TABLE()
38
39 void wxFrameView::OnIdle( wxIdleEvent& event)
40 {
41 event.Skip();
42
43 if ( mDoToolUpdates )
44 {
45 int o;
46 ++o;
47
48 // TBD::
49 }
50 }
51
52 /*** public methods ***/
53
54 wxFrameView::wxFrameView()
55
56 : mpLayout( NULL ),
57 mpFrameMgr( NULL )
58 {}
59
60 wxFrameView::~wxFrameView()
61 {
62 if ( mpLayout ) delete mpLayout;
63 }
64
65 wxFrame* wxFrameView::GetParentFrame()
66 {
67 return mpFrameMgr->GetParentFrame();
68 }
69
70 wxWindow* wxFrameView::GetClientWindow()
71 {
72 return mpFrameMgr->GetClientWindow();
73 }
74
75 void wxFrameView::Activate()
76 {
77 mpFrameMgr->ActivateView( this );
78 }
79
80 void wxFrameView::Deactivate()
81 {
82 mpFrameMgr->DeactivateCurrentView();
83 }
84
85 void wxFrameView::CreateLayout()
86 {
87 mpLayout = new wxFrameLayout( GetParentFrame(), mpFrameMgr->GetClientWindow(), FALSE );
88 }
89
90 wxFrameLayout* wxFrameView::GetLayout()
91 {
92 return mpLayout;
93 }
94
95 void wxFrameView::SetToolUpdates( bool doToolUpdates )
96 {
97 mDoToolUpdates = doToolUpdates;
98 }
99
100 void wxFrameView::SetLayout( wxFrameLayout* pLayout )
101 {
102 if ( mpLayout ) delete mpLayout;
103
104 mpLayout = pLayout;
105 }
106
107 wxFrameManager& wxFrameView::GetFrameManager()
108 {
109 return *mpFrameMgr;
110 }
111
112 void wxFrameView::RegisterMenu( const wxString& topMenuName )
113 {
114 mTopMenus.Add( topMenuName );
115 }
116
117 #if 0
118
119 /***** Implementation for class wxFrameViewSerializer *****/
120
121 // NOTE:: currently "stipple" property of the brush is not serialized
122
123 class wxFrameViewSerializer : public wxEvtHandlerSerializer
124 {
125 DECLARE_SERIALIZER_CLASS( wxFrameViewSerializer );
126
127 static void Serialize( wxObject* pObj, wxObjectStorage& store );
128 };
129
130 IMPLEMENT_SERIALIZER_CLASS( wxFrameView,
131 wxFrameViewSerializer,
132 wxFrameViewSerializer::Serialize,
133 NO_CLASS_INIT )
134
135 void wxFrameViewSerializer::Serialize( wxObject* pObj, wxObjectStorage& store )
136 {
137 // wxFrameViewSerializer is a kind of wxEvtHandler - peform serialization of
138 // the base class first
139
140 info.SerializeInherited( pObj, store );
141
142 wxFrameView* pView = (wxFrameView*)pObj;
143
144 store.XchgObjPtr( (wxObject**) &pView->mpFrameMgr );
145 store.XchgObjPtr( (wxObject**) &pView->mpLayout );
146 store.XchgBool ( pView->mDoToolUpdates );
147
148 // serialize members in derived classes
149
150 pView->OnSerialize( store );
151 }
152
153 #endif
154
155 /***** Implementation for class wxFrameManager *****/
156
157 void wxFrameManager::DoSerialize( wxObjectStorage& store )
158 {
159 #if 0
160 store.AddInitialRef( mpFrameWnd );
161 store.AddInitialRef( this );
162 if ( mpClientWnd ) store.AddInitialRef( mpClientWnd );
163
164 store.XchgObj( (wxObject*) &mViews );
165 store.XchgInt( mActiveViewNo );
166
167 store.Finalize(); // finish serialization
168 #endif
169 }
170
171 void wxFrameManager::DestroyViews()
172 {
173 DeactivateCurrentView();
174
175 wxNode* pNode = mViews.First();
176
177 while( pNode )
178 {
179 delete (wxFrameView*)pNode->Data();
180
181 pNode = pNode->Next();
182 }
183
184 if ( mActiveViewNo != -1 && GetParentFrame() )
185
186 GetParentFrame()->SetNextHandler( NULL );
187 }
188
189 int wxFrameManager::GetViewNo( wxFrameView* pView )
190 {
191 wxNode* pNode = mViews.First();
192 int n = 0;
193
194 while( pNode )
195 {
196 if ( (wxFrameView*)pNode->Data() == pView )
197
198 return n;
199
200 ++n;
201 pNode = pNode->Next();
202 }
203
204 return -1;
205 }
206
207 void wxFrameManager::EnableMenusForView( wxFrameView* pView, bool enable )
208 {
209 wxMenuBar* pMenuBar = GetParentFrame()->GetMenuBar();
210 int count = pMenuBar->GetMenuCount();
211
212 if ( !pMenuBar ) return;
213
214 wxStringListNode* pNode = pView->mTopMenus.GetFirst();
215
216 while( pNode )
217 {
218 for( int i = 0; i != count; ++i )
219 {
220 if ( pMenuBar->GetMenu(i)->GetTitle() == pNode->GetData() )
221
222 pMenuBar->EnableTop( i, enable );
223 }
224
225 pNode = pNode->GetNext();
226 }
227 }
228
229 void wxFrameManager::SyncAllMenus()
230 {
231 wxNode* pNode = mViews.First();
232 int i = 0;
233
234 while( pNode )
235 {
236 if ( i != mActiveViewNo )
237
238 EnableMenusForView( (wxFrameView*)pNode->GetData(), FALSE );
239
240 pNode = pNode->Next();
241 }
242
243 EnableMenusForView( GetView( mActiveViewNo ), TRUE );
244 }
245
246 /*** public methods ***/
247
248 wxFrameManager::wxFrameManager()
249
250 : mpFrameWnd( NULL ),
251 mActiveViewNo( -1 ),
252 mpClientWnd( NULL )
253 {
254 }
255
256 wxFrameManager::~wxFrameManager()
257 {
258 SaveViewsNow();
259 DestroyViews();
260 }
261
262 void wxFrameManager::Init( wxWindow* pMainFrame, const wxString& settingsFile )
263 {
264 mSettingsFile = settingsFile;
265 mpFrameWnd = pMainFrame;
266
267 wxNode* pNode = mViews.First();
268
269 while( pNode )
270 {
271 wxFrameView* pView = (wxFrameView*)pNode->Data();
272
273 pView->OnInit();
274 pView->OnInitMenus();
275
276 pNode = pNode->Next();
277 }
278
279 if ( !ReloadViews() )
280 {
281 // if loading of settings file failed (e.g. was not found),
282 // do recreation of items in each view
283
284 pNode = mViews.First();
285
286 while( pNode )
287 {
288 wxFrameView* pView = (wxFrameView*)pNode->Data();
289
290 pView->OnRecreate();
291
292 pNode = pNode->Next();
293 }
294 }
295
296 if ( mActiveViewNo >= mViews.Number() )
297
298 mActiveViewNo = -1;
299
300 ActivateView( GetView( ( mActiveViewNo == -1 ) ? 0 : mActiveViewNo ) );
301
302 SyncAllMenus();
303 }
304
305 void wxFrameManager::AddView( wxFrameView* pFrmView )
306 {
307 mViews.Append( pFrmView );
308
309 pFrmView->mpFrameMgr = this; // back ref.
310 }
311
312 void wxFrameManager::RemoveView( wxFrameView* pFrmView )
313 {
314 // TBD::
315 wxASSERT(0);
316 }
317
318 int wxFrameManager::GetActiveViewNo()
319 {
320 return mActiveViewNo;
321 }
322
323 wxFrameView* wxFrameManager::GetActiveView()
324 {
325 wxNode* pNode = mViews.Nth( mActiveViewNo );
326
327 if ( pNode ) return (wxFrameView*)pNode->Data();
328 else return NULL;
329 }
330
331 wxNode* wxFrameManager::GetActiveViewNode()
332 {
333 return mViews.Nth( mActiveViewNo );
334 }
335
336 wxFrame* wxFrameManager::GetParentFrame()
337 {
338 return ((wxFrame*)mpFrameWnd);
339 }
340
341 wxWindow* wxFrameManager::GetParentWindow()
342 {
343 return mpFrameWnd;
344 }
345
346 wxFrameView* wxFrameManager::GetView( int viewNo )
347 {
348 wxNode* pNode = mViews.Nth( viewNo );
349
350 if ( pNode ) return (wxFrameView*)pNode->Data();
351 else return NULL;
352 }
353
354 void wxFrameManager::ActivateView( int viewNo )
355 {
356 ActivateView( GetView( viewNo ) );
357 }
358
359 void wxFrameManager::ActivateView( wxFrameView* pFrmView )
360 {
361 DeactivateCurrentView();
362
363 mActiveViewNo = GetViewNo( pFrmView );
364
365 if ( pFrmView->mpLayout )
366
367 pFrmView->mpLayout->Activate();
368
369 // FIXME:: we would have used PushEventHandler(),
370 // but wxFrame bypasses attached handlers when
371 // handling wxCommand events!
372
373 GetParentFrame()->PushEventHandler( pFrmView );
374
375 EnableMenusForView( pFrmView, TRUE );
376 }
377
378 void wxFrameManager::SetClinetWindow( wxWindow* pFrameClient )
379 {
380 if ( mpClientWnd ) mpClientWnd->Destroy();
381
382 mpClientWnd = pFrameClient;
383 }
384
385 wxWindow* wxFrameManager::GetClientWindow()
386 {
387 if ( !mpClientWnd )
388
389 mpClientWnd = new wxWindow( GetParentFrame(), -1 );
390
391 return mpClientWnd;
392 }
393
394 void wxFrameManager::DeactivateCurrentView()
395 {
396 if ( mActiveViewNo == -1 ) return;
397
398 wxFrameView* pView = GetActiveView();
399
400 // FOR NOW::
401 wxASSERT( GetParentFrame()->GetEventHandler() == pView );
402
403 GetParentFrame()->PopEventHandler();
404
405 if ( pView->mpLayout )
406
407 pView->mpLayout->Deactivate();
408
409 EnableMenusForView( pView, FALSE );
410 }
411
412 void wxFrameManager::SaveViewsNow()
413 {
414 #if 0
415 if ( mSettingsFile == "" ) return;
416
417 wxIOStreamWrapper stm;
418 stm.CreateForOutput( mSettingsFile );
419
420 mStore.SetDataStream( stm );
421 DoSerialize( mStore );
422 #endif
423 }
424
425 bool wxFrameManager::ReloadViews()
426 {
427 return FALSE;
428
429 #if 0
430 if ( mSettingsFile == "" || !wxFileExists( mSettingsFile ) )
431
432 return FALSE;
433
434 DestroyViews();
435
436 wxIOStreamWrapper stm;
437 stm.CreateForInput( mSettingsFile );
438
439 mStore.SetDataStream( stm );
440 DoSerialize( mStore );
441 #endif
442
443 return TRUE;
444 }
445
446 bool wxFrameManager::ViewsAreLoaded()
447 {
448 return ( mViews.Number() != 0 );
449 }