]>
git.saurik.com Git - wxWidgets.git/blob - utils/framelayout/src/frmview.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Contrib. demo
4 // Author: Aleksandras Gluchovas
8 // Copyright: (c) Aleksandras Gluchovas
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "frmview.h"
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
31 /***** Implementation for class wxFrameView *****/
33 BEGIN_EVENT_TABLE( wxFrameView
, wxEvtHandler
)
35 EVT_IDLE( wxFrameView::OnIdle
)
39 void wxFrameView::OnIdle( wxIdleEvent
& event
)
52 /*** public methods ***/
54 wxFrameView::wxFrameView()
60 wxFrameView::~wxFrameView()
62 if ( mpLayout
) delete mpLayout
;
65 wxFrame
* wxFrameView::GetParentFrame()
67 return mpFrameMgr
->GetParentFrame();
70 wxWindow
* wxFrameView::GetClientWindow()
72 return mpFrameMgr
->GetClientWindow();
75 void wxFrameView::Activate()
77 mpFrameMgr
->ActivateView( this );
80 void wxFrameView::Deactivate()
82 mpFrameMgr
->DeactivateCurrentView();
85 void wxFrameView::CreateLayout()
87 mpLayout
= new wxFrameLayout( GetParentFrame(), mpFrameMgr
->GetClientWindow(), FALSE
);
90 wxFrameLayout
* wxFrameView::GetLayout()
95 void wxFrameView::SetToolUpdates( bool doToolUpdates
)
97 mDoToolUpdates
= doToolUpdates
;
100 void wxFrameView::SetLayout( wxFrameLayout
* pLayout
)
102 if ( mpLayout
) delete mpLayout
;
107 wxFrameManager
& wxFrameView::GetFrameManager()
112 void wxFrameView::RegisterMenu( const wxString
& topMenuName
)
114 mTopMenus
.Add( topMenuName
);
119 /***** Implementation for class wxFrameViewSerializer *****/
121 // NOTE:: currently "stipple" property of the brush is not serialized
123 class wxFrameViewSerializer
: public wxEvtHandlerSerializer
125 DECLARE_SERIALIZER_CLASS( wxFrameViewSerializer
);
127 static void Serialize( wxObject
* pObj
, wxObjectStorage
& store
);
130 IMPLEMENT_SERIALIZER_CLASS( wxFrameView
,
131 wxFrameViewSerializer
,
132 wxFrameViewSerializer::Serialize
,
135 void wxFrameViewSerializer::Serialize( wxObject
* pObj
, wxObjectStorage
& store
)
137 // wxFrameViewSerializer is a kind of wxEvtHandler - peform serialization of
138 // the base class first
140 info
.SerializeInherited( pObj
, store
);
142 wxFrameView
* pView
= (wxFrameView
*)pObj
;
144 store
.XchgObjPtr( (wxObject
**) &pView
->mpFrameMgr
);
145 store
.XchgObjPtr( (wxObject
**) &pView
->mpLayout
);
146 store
.XchgBool ( pView
->mDoToolUpdates
);
148 // serialize members in derived classes
150 pView
->OnSerialize( store
);
155 /***** Implementation for class wxFrameManager *****/
157 void wxFrameManager::DoSerialize( wxObjectStorage
& store
)
160 store
.AddInitialRef( mpFrameWnd
);
161 store
.AddInitialRef( this );
162 if ( mpClientWnd
) store
.AddInitialRef( mpClientWnd
);
164 store
.XchgObj( (wxObject
*) &mViews
);
165 store
.XchgInt( mActiveViewNo
);
167 store
.Finalize(); // finish serialization
171 void wxFrameManager::DestroyViews()
173 DeactivateCurrentView();
175 wxNode
* pNode
= mViews
.First();
179 delete (wxFrameView
*)pNode
->Data();
181 pNode
= pNode
->Next();
184 if ( mActiveViewNo
!= -1 && GetParentFrame() )
186 GetParentFrame()->SetNextHandler( NULL
);
189 int wxFrameManager::GetViewNo( wxFrameView
* pView
)
191 wxNode
* pNode
= mViews
.First();
196 if ( (wxFrameView
*)pNode
->Data() == pView
)
201 pNode
= pNode
->Next();
207 void wxFrameManager::EnableMenusForView( wxFrameView
* pView
, bool enable
)
209 wxMenuBar
* pMenuBar
= GetParentFrame()->GetMenuBar();
210 int count
= pMenuBar
->GetMenuCount();
212 if ( !pMenuBar
) return;
214 wxStringListNode
* pNode
= pView
->mTopMenus
.GetFirst();
218 for( int i
= 0; i
!= count
; ++i
)
220 if ( pMenuBar
->GetMenu(i
)->GetTitle() == pNode
->GetData() )
222 pMenuBar
->EnableTop( i
, enable
);
225 pNode
= pNode
->GetNext();
229 void wxFrameManager::SyncAllMenus()
231 wxNode
* pNode
= mViews
.First();
236 if ( i
!= mActiveViewNo
)
238 EnableMenusForView( (wxFrameView
*)pNode
->GetData(), FALSE
);
240 pNode
= pNode
->Next();
243 EnableMenusForView( GetView( mActiveViewNo
), TRUE
);
246 /*** public methods ***/
248 wxFrameManager::wxFrameManager()
250 : mpFrameWnd( NULL
),
256 wxFrameManager::~wxFrameManager()
262 void wxFrameManager::Init( wxWindow
* pMainFrame
, const wxString
& settingsFile
)
264 mSettingsFile
= settingsFile
;
265 mpFrameWnd
= pMainFrame
;
267 wxNode
* pNode
= mViews
.First();
271 wxFrameView
* pView
= (wxFrameView
*)pNode
->Data();
274 pView
->OnInitMenus();
276 pNode
= pNode
->Next();
279 if ( !ReloadViews() )
281 // if loading of settings file failed (e.g. was not found),
282 // do recreation of items in each view
284 pNode
= mViews
.First();
288 wxFrameView
* pView
= (wxFrameView
*)pNode
->Data();
292 pNode
= pNode
->Next();
296 if ( mActiveViewNo
>= mViews
.Number() )
300 ActivateView( GetView( ( mActiveViewNo
== -1 ) ? 0 : mActiveViewNo
) );
305 void wxFrameManager::AddView( wxFrameView
* pFrmView
)
307 mViews
.Append( pFrmView
);
309 pFrmView
->mpFrameMgr
= this; // back ref.
312 void wxFrameManager::RemoveView( wxFrameView
* pFrmView
)
318 int wxFrameManager::GetActiveViewNo()
320 return mActiveViewNo
;
323 wxFrameView
* wxFrameManager::GetActiveView()
325 wxNode
* pNode
= mViews
.Nth( mActiveViewNo
);
327 if ( pNode
) return (wxFrameView
*)pNode
->Data();
331 wxNode
* wxFrameManager::GetActiveViewNode()
333 return mViews
.Nth( mActiveViewNo
);
336 wxFrame
* wxFrameManager::GetParentFrame()
338 return ((wxFrame
*)mpFrameWnd
);
341 wxWindow
* wxFrameManager::GetParentWindow()
346 wxFrameView
* wxFrameManager::GetView( int viewNo
)
348 wxNode
* pNode
= mViews
.Nth( viewNo
);
350 if ( pNode
) return (wxFrameView
*)pNode
->Data();
354 void wxFrameManager::ActivateView( int viewNo
)
356 ActivateView( GetView( viewNo
) );
359 void wxFrameManager::ActivateView( wxFrameView
* pFrmView
)
361 DeactivateCurrentView();
363 mActiveViewNo
= GetViewNo( pFrmView
);
365 if ( pFrmView
->mpLayout
)
367 pFrmView
->mpLayout
->Activate();
369 // FIXME:: we would have used PushEventHandler(),
370 // but wxFrame bypasses attached handlers when
371 // handling wxCommand events!
373 GetParentFrame()->PushEventHandler( pFrmView
);
375 EnableMenusForView( pFrmView
, TRUE
);
378 void wxFrameManager::SetClinetWindow( wxWindow
* pFrameClient
)
380 if ( mpClientWnd
) mpClientWnd
->Destroy();
382 mpClientWnd
= pFrameClient
;
385 wxWindow
* wxFrameManager::GetClientWindow()
389 mpClientWnd
= new wxWindow( GetParentFrame(), -1 );
394 void wxFrameManager::DeactivateCurrentView()
396 if ( mActiveViewNo
== -1 ) return;
398 wxFrameView
* pView
= GetActiveView();
401 wxASSERT( GetParentFrame()->GetEventHandler() == pView
);
403 GetParentFrame()->PopEventHandler();
405 if ( pView
->mpLayout
)
407 pView
->mpLayout
->Deactivate();
409 EnableMenusForView( pView
, FALSE
);
412 void wxFrameManager::SaveViewsNow()
415 if ( mSettingsFile
== "" ) return;
417 wxIOStreamWrapper stm
;
418 stm
.CreateForOutput( mSettingsFile
);
420 mStore
.SetDataStream( stm
);
421 DoSerialize( mStore
);
425 bool wxFrameManager::ReloadViews()
430 if ( mSettingsFile
== "" || !wxFileExists( mSettingsFile
) )
436 wxIOStreamWrapper stm
;
437 stm
.CreateForInput( mSettingsFile
);
439 mStore
.SetDataStream( stm
);
440 DoSerialize( mStore
);
446 bool wxFrameManager::ViewsAreLoaded()
448 return ( mViews
.Number() != 0 );