]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/fl/frmview.cpp
don't try to refresh the menu bar before showing it, fixes crash on startup with...
[wxWidgets.git] / contrib / src / fl / 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 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/wx.h"
25 #endif
26
27 #include "wx/fl/frmview.h"
28 #include "wx/utils.h"
29
30 /***** Implementation for class wxFrameView *****/
31
32 BEGIN_EVENT_TABLE( wxFrameView, wxEvtHandler )
33
34 EVT_IDLE( wxFrameView::OnIdle )
35
36 END_EVENT_TABLE()
37
38 void wxFrameView::OnIdle( wxIdleEvent& event)
39 {
40 event.Skip();
41
42 if ( mDoToolUpdates )
43 {
44 int o = 0; //glt
45 ++o;
46
47 // TBD::
48 }
49 }
50
51 /*** public methods ***/
52
53 wxFrameView::wxFrameView()
54
55 : mpLayout( NULL ),
56 mpFrameMgr( NULL )
57 {}
58
59 wxFrameView::~wxFrameView()
60 {
61 if ( mpLayout ) delete mpLayout;
62 }
63
64 wxFrame* wxFrameView::GetParentFrame()
65 {
66 return mpFrameMgr->GetParentFrame();
67 }
68
69 wxWindow* wxFrameView::GetClientWindow()
70 {
71 return mpFrameMgr->GetClientWindow();
72 }
73
74 void wxFrameView::Activate()
75 {
76 mpFrameMgr->ActivateView( this );
77 }
78
79 void wxFrameView::Deactivate()
80 {
81 mpFrameMgr->DeactivateCurrentView();
82 }
83
84 void wxFrameView::CreateLayout()
85 {
86 mpLayout = new wxFrameLayout( GetParentFrame(), mpFrameMgr->GetClientWindow(), FALSE );
87 }
88
89 wxFrameLayout* wxFrameView::GetLayout()
90 {
91 return mpLayout;
92 }
93
94 void wxFrameView::SetToolUpdates( bool doToolUpdates )
95 {
96 mDoToolUpdates = doToolUpdates;
97 }
98
99 void wxFrameView::SetLayout( wxFrameLayout* pLayout )
100 {
101 if ( mpLayout ) delete mpLayout;
102
103 mpLayout = pLayout;
104 }
105
106 wxFrameManager& wxFrameView::GetFrameManager()
107 {
108 return *mpFrameMgr;
109 }
110
111 void wxFrameView::RegisterMenu( const wxString& topMenuName )
112 {
113 mTopMenus.Add( topMenuName );
114 }
115
116 #if 0
117
118 /***** Implementation for class wxFrameViewSerializer *****/
119
120 // NOTE:: currently "stipple" property of the brush is not serialized
121
122 class wxFrameViewSerializer : public wxEvtHandlerSerializer
123 {
124 DECLARE_SERIALIZER_CLASS( wxFrameViewSerializer );
125
126 static void Serialize( wxObject* pObj, wxObjectStorage& store );
127 };
128
129 IMPLEMENT_SERIALIZER_CLASS( wxFrameView,
130 wxFrameViewSerializer,
131 wxFrameViewSerializer::Serialize,
132 NO_CLASS_INIT )
133
134 void wxFrameViewSerializer::Serialize( wxObject* pObj, wxObjectStorage& store )
135 {
136 // wxFrameViewSerializer is a kind of wxEvtHandler - peform serialization of
137 // the base class first
138
139 info.SerializeInherited( pObj, store );
140
141 wxFrameView* pView = (wxFrameView*)pObj;
142
143 store.XchgObjPtr( (wxObject**) &pView->mpFrameMgr );
144 store.XchgObjPtr( (wxObject**) &pView->mpLayout );
145 store.XchgBool ( pView->mDoToolUpdates );
146
147 // serialize members in derived classes
148
149 pView->OnSerialize( store );
150 }
151
152 #endif
153
154 /***** Implementation for class wxFrameManager *****/
155
156 void wxFrameManager::DoSerialize( wxObjectStorage& store )
157 {
158 #if 0
159 store.AddInitialRef( mpFrameWnd );
160 store.AddInitialRef( this );
161 if ( mpClientWnd ) store.AddInitialRef( mpClientWnd );
162
163 store.XchgObj( (wxObject*) &mViews );
164 store.XchgInt( mActiveViewNo );
165
166 store.Finalize(); // finish serialization
167 #endif
168 }
169
170 void wxFrameManager::DestroyViews()
171 {
172 DeactivateCurrentView();
173
174 wxNode* pNode = mViews.First();
175
176 while( pNode )
177 {
178 delete (wxFrameView*)pNode->Data();
179
180 pNode = pNode->Next();
181 }
182
183 if ( mActiveViewNo != -1 && GetParentFrame() )
184
185 GetParentFrame()->SetNextHandler( NULL );
186 }
187
188 int wxFrameManager::GetViewNo( wxFrameView* pView )
189 {
190 wxNode* pNode = mViews.First();
191 int n = 0;
192
193 while( pNode )
194 {
195 if ( (wxFrameView*)pNode->Data() == pView )
196
197 return n;
198
199 ++n;
200 pNode = pNode->Next();
201 }
202
203 return -1;
204 }
205
206 void wxFrameManager::EnableMenusForView( wxFrameView* pView, bool enable )
207 {
208 wxMenuBar* pMenuBar = GetParentFrame()->GetMenuBar();
209 int count = pMenuBar->GetMenuCount();
210
211 if ( !pMenuBar ) return;
212
213 wxStringListNode* pNode = pView->mTopMenus.GetFirst();
214
215 while( pNode )
216 {
217 for( int i = 0; i != count; ++i )
218 {
219 if ( pMenuBar->GetMenu(i)->GetTitle() == pNode->GetData() )
220
221 pMenuBar->EnableTop( i, enable );
222 }
223
224 pNode = pNode->GetNext();
225 }
226 }
227
228 void wxFrameManager::SyncAllMenus()
229 {
230 wxNode* pNode = mViews.First();
231 int i = 0;
232
233 while( pNode )
234 {
235 if ( i != mActiveViewNo )
236
237 EnableMenusForView( (wxFrameView*)pNode->GetData(), FALSE );
238
239 pNode = pNode->Next();
240 }
241
242 EnableMenusForView( GetView( mActiveViewNo ), TRUE );
243 }
244
245 /*** public methods ***/
246
247 wxFrameManager::wxFrameManager()
248
249 : mpFrameWnd( NULL ),
250 mActiveViewNo( -1 ),
251 mpClientWnd( NULL )
252 {
253 }
254
255 wxFrameManager::~wxFrameManager()
256 {
257 SaveViewsNow();
258 DestroyViews();
259 }
260
261 void wxFrameManager::Init( wxWindow* pMainFrame, const wxString& settingsFile )
262 {
263 mSettingsFile = settingsFile;
264 mpFrameWnd = pMainFrame;
265
266 wxNode* pNode = mViews.First();
267
268 while( pNode )
269 {
270 wxFrameView* pView = (wxFrameView*)pNode->Data();
271
272 pView->OnInit();
273 pView->OnInitMenus();
274
275 pNode = pNode->Next();
276 }
277
278 if ( !ReloadViews() )
279 {
280 // if loading of settings file failed (e.g. was not found),
281 // do recreation of items in each view
282
283 pNode = mViews.First();
284
285 while( pNode )
286 {
287 wxFrameView* pView = (wxFrameView*)pNode->Data();
288
289 pView->OnRecreate();
290
291 pNode = pNode->Next();
292 }
293 }
294
295 if ( mActiveViewNo >= mViews.Number() )
296
297 mActiveViewNo = -1;
298
299 ActivateView( GetView( ( mActiveViewNo == -1 ) ? 0 : mActiveViewNo ) );
300
301 SyncAllMenus();
302 }
303
304 void wxFrameManager::AddView( wxFrameView* pFrmView )
305 {
306 mViews.Append( pFrmView );
307
308 pFrmView->mpFrameMgr = this; // back ref.
309 }
310
311 void wxFrameManager::RemoveView( wxFrameView* pFrmView )
312 {
313 // TBD::
314 int avoidCompilerWarning = 0;
315 wxASSERT(avoidCompilerWarning);
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 // TBD: ????
430 #if 0
431 if ( mSettingsFile == "" || !wxFileExists( mSettingsFile ) )
432
433 return FALSE;
434
435 DestroyViews();
436
437 wxIOStreamWrapper stm;
438 stm.CreateForInput( mSettingsFile );
439
440 mStore.SetDataStream( stm );
441 DoSerialize( mStore );
442
443 return TRUE;
444 #endif
445 }
446
447 bool wxFrameManager::ViewsAreLoaded()
448 {
449 return ( mViews.Number() != 0 );
450 }
451