NCBI C++ ToolKit
view_manager_service_impl.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: view_manager_service_impl.cpp 32743 2015-04-20 16:11:13Z katargir $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Andrey Yazhuk
27  *
28  * File Description:
29  */
30 
31 #include <ncbi_pch.hpp>
32 
33 #include <wx/platform.h>
34 
36 
39 
41 
43 
44 #include <wx/frame.h>
45 
46 
48 
49 static CExtensionPointDeclaration decl("view_manager_service::view_factory",
50  "View Manager Service - view factories");
51 
52 
54 : m_Workbench(NULL)
55 {
56 }
57 
58 
60 {
61 }
62 
63 
65 {
66  m_Workbench = workbench;
67 }
68 
69 
71 {
72  LOG_POST(Info << "Initializing View Manager Service...");
73 
75 
76  LOG_POST(Info << "Finished initializing View Manager Service");
77 }
78 
79 
81 {
82  LOG_POST(Info << "Shutting down View Manager Service...");
83 
84  // close all remaining views
85  if( ! m_Views.empty()) {
86  LOG_POST(Error << "Some views are still open! Closing them forecefully");
87  _ASSERT(false);
89  }
90 
91  // first get a list of all IWMClients
92  vector<IWMClient*> clients;
94  wm_srv->GetAllClients(clients);
95 
96  /// remove IViews
97  for(size_t i = 0; i < clients.size(); i++) {
98  IView* view = dynamic_cast<IView*>(clients[i]);
99  if(view) {
100  RemoveFromWorkbench(*view);
101  }
102  }
103 
105 
107 
108  LOG_POST(Info << "Finished shutting down View Manager Service");
109 }
110 
111 
113 {
115 
116  const CViewTypeDescriptor& descr = factory.GetViewTypeDescriptor();
117  const string& ui_name = descr.GetLabel();
118 
120  if(it == m_ViewNameToFactory.end()) {
121  m_ViewNameToFactory[ui_name] = CIRef<IViewFactory>(&factory);
122 
124  factory.RegisterIconAliases(provider);
125 
127  factory.RegisterCommands(cmd_reg, provider);
128 
129  IUICommandContributor* cmd_ctrb = dynamic_cast<IUICommandContributor*>( &factory );
130  if( cmd_ctrb ){
131  vector<CUICommand*> cmds = cmd_ctrb->GetCommands();
132  NON_CONST_ITERATE( vector<CUICommand*>, pix, cmds ){
133  cmd_reg.RegisterCommand( *pix );
134  }
135 
136  //x_PushEvtHandler( cmd_ctrb->GetEventHandler() );
137  }
138 
139  LOG_POST(Info << "CViewManagerService - added a view factory for " << ui_name);
140  } else {
141  LOG_POST(Error << "CViewManagerService::RegisterFactory() - a factory for view type "
142  << ui_name << " is already registered.");
143  }
144 }
145 
146 
148 {
150  IViewFactory& factory = *it->second;
151  const CViewTypeDescriptor& descr = factory.GetViewTypeDescriptor();
152  descrs.push_back(&descr);
153  }
154 }
155 
156 
158 {
160  CIRef<IViewFactory> factory = it->second;
161  factories.push_back(factory);
162  }
163 }
164 
165 
167 {
168  CIRef<IView> view;
170 
171  if(it != m_ViewNameToFactory.end()) {
172  IViewFactory& factory = *it->second;
173  view.Reset(factory.CreateInstance());
174  } else {
175  LOG_POST(Error << "CViewManagerService::CreateViewInstance() - cannot create " <<
176  type_ui_name << ", factory is not registred for this type");
177  _ASSERT(false);
178  }
179  return view;
180 }
181 
182 bool CViewManagerService::CanCreateView(const string& type_ui_name)
183 {
185  return it != m_ViewNameToFactory.end();
186 }
187 
188 
191 {
193  IViewFactory& factory = *it->second;
194  IView* view = factory.CreateInstanceByFingerprint(fingerprint);
195  if(view) {
196  return CIRef<IView>(view);
197  }
198  }
199  //_ASSERT(false);
200  return CIRef<IView>();
201 }
202 
203 
204 //TODO clarify error handling policy
206 {
207  //LOG_POST( Info << "VMS: adding view to workbench..." );
208 
209  const CViewTypeDescriptor& descr = view.GetTypeDescriptor();
210  IWMClient* client = dynamic_cast<IWMClient*>(&view);
211 
212  if( ! client) {
213  LOG_POST(
214  Error << "CViewManagerService::AddToWorkbench() - view "
215  << descr.GetLabel() << " does not implement IWMClient"
216  );
217  _ASSERT(false);
218  } else {
220 
221  if(m_Workbench) {
222  TViews::iterator it = std::find(m_Views.begin(), m_Views.end(), &view);
223 
224  if(it != m_Views.end()) {
225  const char* msg = "View Manager Service - view already registered!";
226  LOG_POST( Error << msg );
227  _ASSERT(false);
228 
230  }
231 
232  if(descr.IsSingleton()) {
233  const string& ui_name = descr.GetLabel();
234  if(m_SingletonMap.find(ui_name) != m_SingletonMap.end()) {
235  LOG_POST(
236  Error << "CViewManagerService::AddToWorkbench() - singleton view"
237  << ui_name << " already exists."
238  );
239 
240  _ASSERT(false);
241  return;
242  }
243  }
244 
245  wxFrame* parent = m_Workbench->GetMainWindow();
246 
247  x_DoAddToWorkbench(view, parent);
248 
249  // add to Window Manager
251  wm_srv->AddClient(*client, bFloat);
252 
253  x_LoadViewSettings(view);
254 
255  // On some platforms, (I'm looking at you, Mac..) the initial window may
256  // not display until a sizing or docking event, so we send an extra size
257  // notification to force display.
258  //
259  // This code also causes delayed crash on wxwidgets 2.9.3 GTK
260  // when opening system (non project) views
261 #ifdef __WXMAC__
262  parent->SendSizeEvent();
263  parent->Update();
264 #endif
265  }
266  }
267 }
268 
269 
271 {
272  //LOG_POST("CViewManagerService::RemoveFromWorkbench()");
273 
274  TViews::iterator it = std::find(m_Views.begin(), m_Views.end(), &view);
275  if(it == m_Views.end()) {
276  return;
277 // In composite view the child views are not registered with the View manager
278 // _ASSERT(false);
279 // const char* s = "CViewManagerService - cannot remove unregistered view";
280 // NCBI_THROW(CException, eUnknown, s);
281  }
282 
284 
285  IWMClient* client = dynamic_cast<IWMClient*>(&view);
286 
287  _ASSERT(client); // must implement
288 
289  if(m_Workbench) {
290  // save settings first (while this view is still in window manager)
291  x_SaveViewSettings(view);
292 
293  // remove from Window Manager
295  wm_srv->CloseClient(*client);
296 
297  // remove from Workbench
299  }
300 
301  //LOG_POST("CViewManagerService::RemoveFromWorkbench() END");
302 }
303 
304 
306 {
307  // make a safe copy, because m_Views may be modified in the loop below
308  TViews views = m_Views;
309 
310  for( TViews::reverse_iterator it = views.rbegin(); it != views.rend(); it++) {
311  IView& view = **it;
312  RemoveFromWorkbench(view);
313  }
314 }
315 
316 
317 bool CViewManagerService::HasView(const IView& view) const
318 {
319  TViews::const_iterator it = std::find(m_Views.begin(), m_Views.end(), &view);
320  return it != m_Views.end();
321 }
322 
323 
325 {
326  views = m_Views;
327 }
328 
329 
330 void CViewManagerService::x_DoAddToWorkbench(IView& view, wxWindow* parent)
331 {
332  //LOG_POST( Info << "VMS: doing add view to workbench..." );
333 
334  // here we assume that all preconditions are validated
335 
336  // create wxWindow for this view
337  view.CreateViewWindow(parent);
338 
339  //LOG_POST( Info << "VMS::x_DATW: pushing back view..." );
340 
341  // add to the list of registered views
342  m_Views.push_back(CIRef<IView>(&view));
343 
344  //LOG_POST( Info << "VMS::x_DATW: register singletons..." );
345 
346  // register singletons
347  const CViewTypeDescriptor& descr = view.GetTypeDescriptor();
348  if(descr.IsSingleton()) {
349  const string& ui_name = descr.GetLabel();
350  m_SingletonMap[ui_name] = &view;
351  }
352 
353  //LOG_POST( Info << "VMS::x_DATW: finally setting workbench..." );
354 
355  // connect to Workbench and Services
357 }
358 
359 
361 {
362  //LOG_POST("CViewManagerService::x_DoRemoveViewFromWorkbench()");
363 
364  // here we assume that all preconditions are validated
365  // disconnect from Workbench and Services
366  view.SetWorkbench(NULL);
367 
368  // unregister singletons
369  const CViewTypeDescriptor& descr = view.GetTypeDescriptor();
370  if(descr.IsSingleton()) {
371  const string& ui_name = descr.GetLabel();
372  m_SingletonMap.erase(ui_name);
373  }
374 
375  view.DestroyViewWindow();
376 
377  // unregister view
378  TViews::iterator it = std::find(m_Views.begin(), m_Views.end(), &view);
379  m_Views.erase(it);
380 
381  //LOG_POST("CViewManagerService::x_DoRemoveViewFromWorkbench() END");
382 }
383 
384 
386 {
387  IRegSettings* reg_settings = dynamic_cast<IRegSettings*>(&view);
388  if(reg_settings) {
389  string reg_path = x_GetViewRegistryPath(view);
390  reg_settings->SetRegistryPath(reg_path);
391  reg_settings->LoadSettings();
392  }
393 }
394 
395 
397 {
398  IRegSettings* reg_settings = dynamic_cast<IRegSettings*>(&view);
399  if(reg_settings) {
400  reg_settings->SaveSettings();
401  }
402 }
403 
404 
405 CIRef<IView> CViewManagerService::GetSingletonView(const string& singleton_ui_name)
406 {
407  TViewNameToFactory::iterator it = m_ViewNameToFactory.find(singleton_ui_name);
408 
409  if(it == m_ViewNameToFactory.end()) {
410  LOG_POST(Error << "CViewManagerService::ExistsInWorkbench() view "
411  << singleton_ui_name << " is not registered");
412  } else {
413  IViewFactory& factory = *it->second;
414  const CViewTypeDescriptor& descr = factory.GetViewTypeDescriptor();
415 
416  _ASSERT(descr.IsSingleton());
417 
418  if(descr.IsSingleton()) {
419  TSingletonMap::iterator it = m_SingletonMap.find(singleton_ui_name);
420  if(it != m_SingletonMap.end()) {
421  return it->second;
422  }
423  } else {
424  LOG_POST(Error << "CViewManagerService::ExistsInWorkbench() view "
425  << singleton_ui_name << " is not a singleton.");
426  }
427  }
428  return CIRef<IView>();
429 }
430 
431 
432 CIRef<IView> CViewManagerService::ShowSingletonView(const string& singleton_ui_name)
433 {
434  CIRef<IView> view = GetSingletonView(singleton_ui_name);
435  if(view) {
436  // activate existing view
438  IWMClient* client = dynamic_cast<IWMClient*>(view.GetPointer());
439 
440  _ASSERT(client);
441 
442  srv->ActivateClient(*client);
443  } else {
444  // create a new view
445  view = CreateViewInstance(singleton_ui_name);
446  if(view) {
447  AddToWorkbench(*view, false);
448  }
449  }
450  return view;
451 }
452 
453 
455  wxWindow* parent)
456 {
457  /// create IView object
458  CIRef<IView> view = CreateViewInstanceByFingerprint(fingerprint);
459 
460  // initialize view and connect to Workbench
461  if(view) {
462  x_DoAddToWorkbench(*view, parent);
463 
464  x_LoadViewSettings(*view);
465  }
466 
467  IWMClient* client = dynamic_cast<IWMClient*>(view.GetPointer());
468  return client;
469 }
470 
471 
472 void CViewManagerService::SetRegistryPath(const string& path)
473 {
474  m_RegPath = path;
475 }
476 
477 
479 {
480 }
481 
482 
484 {
485 }
486 
487 
489 {
490  // get View Factoroies from our extension point and register them
491  vector< CIRef<IViewFactory> > factories;
492  GetExtensionAsInterface("view_manager_service::view_factory", factories);
493 
494  for( size_t i = 0; i < factories.size(); i++ ) {
495  IViewFactory& factory = *factories[i];
496  RegisterFactory(factory);
497  }
498 
500  wxFrame* frame = m_Workbench->GetMainWindow();
501  cmd_reg.ApplyAccelerators(frame);
502 }
503 
504 
506 {
507  const CViewTypeDescriptor& descr = view.GetTypeDescriptor();
508 
509  _ASSERT( ! m_RegPath.empty());
510  _ASSERT( ! descr.GetCategory().empty());
511  _ASSERT( ! descr.GetLabel().empty());
512 
513  string view_key = descr.GetCategory() + "." + descr.GetLabel();
514  string reg_path = m_RegPath + ".Views." + view_key;
515  return reg_path;
516 }
517 
518 
519 ///////////////////////////////////////////////////////////////////////////////
520 /// CViewTypeDescriptor
522  const string& label,
523  const string& icon_alias,
524  const string& hint,
525  const string& description,
526  const string& help_id,
527  const string& category,
528  bool singleton
529 ): CUIObject( label, icon_alias, hint, description, help_id )
530  , m_Category( category )
531  , m_Singleton( singleton )
532 {
533 }
534 
535 
537 {
538 }
539 
540 
542 {
543  return m_Category;
544 }
545 
547 {
548  return m_Singleton;
549 }
550 
551 
552 void CViewTypeDescriptor::SetCategory(const string& category)
553 {
554  m_Category = category;
555 }
556 
557 
559 {
560  m_Singleton = singleton;
561 }
562 
CExtensionPointDeclaration - static declaration helper.
CUICommandRegistry is a centralized registry where all application commands should be registered.
Definition: ui_command.hpp:146
int RegisterCommand(CUICommand *cmd)
assumes ownership of the given object returns a command id (useful when registry is used for auto id ...
Definition: ui_command.cpp:198
void ApplyAccelerators(wxWindow *frame)
apply accumulated accelerators to the specifed frame
Definition: ui_command.cpp:281
CUIObject - default mix-in implementation of IUIObject.
Definition: ui_object.hpp:81
CViewTypeDescriptor - holds description of a view type.
Definition: view.hpp:98
IRegSettings An interface for objects that save / restore settings using CGuiRegistry.
virtual void SaveSettings() const =0
virtual void LoadSettings()=0
virtual void SetRegistryPath(const string &path)=0
IUICommandContributor - contributes commands to the common command space.
Definition: ui_command.hpp:351
virtual vector< CUICommand * > GetCommands()=0
returns a vector of commands (takes ownership over CUICommand pointers)
IViewFactory - view factory for use with IViewManagerService.
Definition: view.hpp:140
IView - represents a standard visual part of Workbench UI.
Definition: view.hpp:73
CFingerprint identifies an instance of IWMClient and is used for labeling layout positions.
Definition: wm_client.hpp:58
IWClient - abstract Window Manager client.
Definition: wm_client.hpp:50
IWindowManagerService Window Manager Service provides access to Window Manager functionality.
IWorkbench is the central interface in the application framework.
Definition: workbench.hpp:113
void erase(iterator pos)
Definition: map.hpp:167
const_iterator end() const
Definition: map.hpp:152
bool empty() const
Definition: map.hpp:149
void clear()
Definition: map.hpp:169
const_iterator find(const key_type &key) const
Definition: map.hpp:153
#define NON_CONST_ITERATE(Type, Var, Cont)
Non constant version of ITERATE macro.
Definition: ncbimisc.hpp:822
#define NULL
Definition: ncbistd.hpp:225
#define LOG_POST(message)
This macro is deprecated and it's strongly recomended to move in all projects (except tests) to macro...
Definition: ncbidiag.hpp:226
void Error(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1197
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
Definition: ncbiexpt.hpp:704
void Info(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1185
virtual wxFrame * GetMainWindow()=0
returns a pointer to the main application frame window
virtual void GetFactories(TFactories &factories)
virtual void AddClient(IWMClient &client, bool bFloat)=0
places the given IWMClient in the Main Tabbed Pane in Window Manager
virtual CIRef< IView > ShowSingletonView(const string &singleton_ui_name)
activates the specified view, create a new view if it does not exist
void x_SaveViewSettings(IView &view)
virtual CUICommandRegistry & GetUICommandRegistry()=0
returns an instance of Command Registry associated with the Workbench
virtual IView * CreateInstanceByFingerprint(const TFingerprint &fingerprint) const =0
if fingerprint is recognized - creates and returns a new instance
virtual const CViewTypeDescriptor & GetViewTypeDescriptor() const =0
returns a Descriptor for the View Type supported by the Factory
virtual void AddToWorkbench(IView &view, bool bFloat)
adds view to Workbench and connects to the services the view must be already initialized
vector< CIRef< IView > > TViews
virtual bool IsSingleton() const
true if only one instance of this type can be created
virtual void SetRegistryPath(const string &path)
virtual wxFileArtProvider & GetFileArtProvider()=0
virtual void RemoveAllViewsFromWorkbench()
disconnects and removes all view from Workbench
virtual CIRef< IView > CreateViewInstanceByFingerprint(const TFingerprint &fingerprint)
create a view instance of the specified type
vector< const CViewTypeDescriptor * > TDescrVec
virtual void SetWorkbench(IWorkbench *workbench)=0
connect / disconnect this view with / from Workbench
void x_DoRemoveViewFromWorkbench(IView &view)
vector< CIRef< IViewFactory > > TFactories
string m_Category
Definition: view.hpp:129
CViewTypeDescriptor(const string &label, const string &icon_alias, const string &hint, const string &description, const string &help_id, const string &category, bool singleton)
CViewTypeDescriptor.
virtual void DestroyViewWindow()=0
destroy Window corresponding to the view
virtual string GetCategory() const
get view category (used for view grouping in UI)
void x_DoAddToWorkbench(IView &view, wxWindow *parent)
TViewNameToFactory m_ViewNameToFactory
virtual IWindowManagerService * GetWindowManagerService()=0
virtual void SetWorkbench(IWorkbench *workbench)
connect / disconnect to / from Workbench
virtual void RemoveFromWorkbench(IView &view)
disconnects view from services and removes from the Workbench
virtual void GetAllClients(TClients &clients)=0
returns a list of all registered IWMClients
virtual void CloseClient(IWMClient &client)=0
remove client(s) from Window Manager these functions do not disconnect clients from other services
virtual void RegisterFactory(IViewFactory &factory)
assumes ownership of the factory
void x_LoadViewSettings(IView &view)
virtual IView * CreateInstance() const =0
creates a view instance
virtual void CreateViewWindow(wxWindow *parent)=0
create Window corresponding to the view
virtual bool HasView(const IView &view) const
virtual CIRef< IView > CreateViewInstance(const string &type_ui_name)
create a view instance of the specified type
virtual void RegisterCommands(CUICommandRegistry &, wxFileArtProvider &)
called by the framework to give Factory a chance to register commands used by view
Definition: view.hpp:150
virtual void ActivateClient(IWMClient &client)=0
makes client visible and focused
virtual const CViewTypeDescriptor & GetTypeDescriptor() const =0
return an object providing meta information about thei view type
virtual void GetTypeDescriptors(TDescrVec &descrs)
virtual bool CanCreateView(const string &type_ui_name)
virtual IWMClient * CreateClient(const TFingerprint &fingerprint, wxWindow *parent)
creates a client by fingerprint returns NULL if fingerprint is not recognized.
virtual void RegisterIconAliases(wxFileArtProvider &provider)=0
called by the framework to give Factory a chance to register images used by view
virtual void GetViews(TViews &views)
get all registered views
virtual CIRef< IView > GetSingletonView(const string &singleton_ui_name)
returns a pointer singleton view if it already exists in workbench or NULL
virtual void SetSingleton(bool singleton)
virtual void SaveSettings() const
string x_GetViewRegistryPath(IView &view)
virtual void SetCategory(const string &category)
void GetExtensionAsInterface(const string &ext_point_id, vector< CIRef< I > > &interfaces)
GetExtensionAsInterface() is a helper function that extracts all extensions implementing the specifie...
virtual const string & GetLabel() const
Definition: ui_object.cpp:124
TObjectType * GetPointer(void) THROWS_NONE
Get pointer,.
Definition: ncbiobj.hpp:998
void Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:773
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
static const char label[]
int i
static SLJIT_INLINE sljit_ins msg(sljit_gpr r, sljit_s32 d, sljit_gpr x, sljit_gpr b)
static CNamedPipeClient * client
#define _ASSERT
static CExtensionPointDeclaration decl("view_manager_service::view_factory", "View Manager Service - view factories")
Modified on Fri Sep 20 14:57:39 2024 by modify_doxy.py rev. 669887