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

Go to the SVN repository for this file.

1 /* $Id: workbench_impl.cpp 34721 2016-02-04 14:05:23Z 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  */
29 
30 #include <ncbi_pch.hpp>
31 
32 #include <wx/platform.h>
33 
35 
42 
47 
50 
51 #include <wx/menu.h>
52 
55 
56 /// TODO
57 /*
58 current implementation does not have robust protection against NULL pointers.
59 This is done intentionaly, as it is not clear what policy we should implement
60 throw exceptions, ignore incorrect inputs, return boolean values indicating success etc.
61 */
62 
63 ///////////////////////////////////////////////////////////////////////////////
64 /// CWorkbench
65 
66 
68 : m_Advisor(advisor),
69  m_FileArtProvider(NULL),
70  m_MainFrame(NULL),
71  m_EventLogService(NULL),
72  m_MenuService(NULL),
73  m_StatusBarService(NULL),
74  m_WindowManagerService(NULL),
75  m_AppTaskService(NULL),
76  m_ViewManagerService(NULL)
77 {
78  //LOG_POST(Info << "CWorkbench constructor");
79  _ASSERT(advisor);
80 }
81 
82 
84 {
85  //LOG_POST(Info << "CWorkbench destructor");
86 }
87 
88 
89 /// register extra i.e. application-defined service
90 /// the recommended way is to used interface class name (typeid(interface).name())
91 /// as the name of a service
92 void CWorkbench::RegisterService(const string& class_name, IService* service)
93 {
95 
96  if(it != m_NameToExtraService.end()) {
97  _ASSERT(false);
98  string msg = "Service with name \"" + class_name +
99  "\" is already registered in Workbench";
101  } else {
102  /// if this service implements IRegSettings - set its registry path
103  IRegSettings* rs = dynamic_cast<IRegSettings*>(service);
104  if(rs) {
105  string reg_path = GetRegistrySectionPath(eServices);
106  rs->SetRegistryPath(reg_path);
107  }
108 
109  // provide a reference to Service Locator if needed
110  Consumer_SetLocator(service, this);
111 
112  // initialize the service and save the reference
113  service->InitService();
114  m_NameToExtraService[class_name] = CIRef<IService>(service);
115  }
116 }
117 
118 
119 void CWorkbench::UnRegisterService(const string& class_name)
120 {
122 
123  if(it == m_NameToExtraService.end()) {
124  _ASSERT(false);
125  string msg = "Service with name \"" + class_name +
126  "\" is not registered in Workbench";
128  } else {
129  IService* service = it->second.GetPointer();
130  service->ShutDownService();
131 
132  // disconnect from the Service Locator
133  Consumer_SetLocator(service, NULL);
134 
135  // reset registry path to prevent service from using it
136  IRegSettings* rs = dynamic_cast<IRegSettings*>(service);
137  if(rs) {
138  rs->SetRegistryPath("");
139  }
140 
141  m_NameToExtraService.erase(it); // remove the entry from the map
142  }
143 }
144 
145 
147 {
148  LOG_POST(Info << "Initializing Workbench...");
149 
151 
153 
154  // Create Event Logging Service
157 
158  x_InitMainFrame();
159 
160  // Status Bar Service
165 
166  // Menu Service
167  m_MenuService = new CMenuService();
169  RegisterService(typeid(IMenuService).name(), m_MenuService);
170 
171  // Window Manager
175 
176  /// give the application a chance to initialize its part of GUI
178 
179  /// connect Window Manager Service and Menu Service
182 
183  m_MainFrame->Show();
184 
185  // View Manager Service
189 
190  // Application Task Service
193 
194  m_Advisor->PostInit();
195 
196  LOG_POST(Info << "Finished initializing Workbench");
197 }
198 
199 
201 {
202  {{
203  CIRef<IStatusBarService> sb_srv = GetServiceByType<IStatusBarService>();
204  sb_srv->SetStatusMessage("Shutdown initiated.");
205  }}
206 
207  LOG_POST(Info << "Shutting down Workbench...");
209 
211 
212  if(m_MainFrame) {
213  UnRegisterService(typeid(IViewManagerService).name());
214 
216 
219 
221  UnRegisterService(typeid(IMenuService).name());
222 
224 
225  m_FileArtProvider = NULL; // the instance will be destroyed automatically
226  }
227 
229 
234 
235  LOG_POST(Info << "Finished shutting down Workbench");
236 }
237 
238 
240 {
241  //TODO ask View Manager to check whether we can Shut Down Data Views
242  return m_Advisor->CanShutDown();
243 }
244 
246 {
247  return m_EventLogService;
248 }
249 
250 
252 {
253  return m_StatusBarService;
254 }
255 
256 
258 {
259  return m_WindowManagerService;
260 }
261 
262 
264 {
266 }
267 
268 
269 // Registry keys
270 static const char* kMainFrameSection = ".MainFrame";
271 
272 
274 {
275  switch(section) {
276  case eServices:
277  return m_RegPath + ".Services";
278  case eDialogs:
279  return m_RegPath + ".Dialogs";
280  case eTools:
281  return m_RegPath + ".Tools";
282  default:
283  _ASSERT(false);
284  return "";
285  }
286 }
287 
289 {
290  return m_MenuService;
291 }
292 
293 
295 {
296  return m_WindowManagerService;
297 }
298 
299 
301 {
302  return m_AppTaskService;
303 }
304 
305 
307 {
308  return m_ViewManagerService;
309 }
310 
311 
313 {
315  return *m_FileArtProvider;
316 }
317 
318 
320 {
321  return m_MainFrame;
322 }
323 
324 
326 {
327  return this;
328 }
329 
330 
331 bool CWorkbench::HasService(const string& name)
332 {
334  return it != m_NameToExtraService.end();
335 }
336 
337 
339 {
341  if(it != m_NameToExtraService.end()) {
342  return it->second;
343  } else {
344  return CIRef<IService>();
345  }
346 }
347 
348 
350 {
352 }
353 
354 
356 {
357  if (m_StatusBarService) {
359  }
360 }
361 
362 
363 void CWorkbench::SetRegistryPath(const string& path)
364 {
365  m_RegPath = path;
366 }
367 
368 
370 {
371 }
372 
373 
375 {
376 }
377 
378 
379 ///////////////////////////////////////////////////////////////////////////////
380 ///
382 {
383  wxArtProvider::Push( new CwxSplittingArtProvider() );
384 
385  wxString dir = CSysPath::ResolvePath( wxT("<res>") );
387 
389 }
390 
391 
392 /// create application Main Frame
394 {
395  string title = m_Advisor->GetAppTitle();
396 
397  /// Create main Frame window
400 
403 }
404 
405 
406 /// destroy application Main Frame
408 {
410 
411  m_MainFrame->Destroy();
412  m_MainFrame = NULL; // will be deleted by the system
413 }
414 
415 
417 {
418  wxPoint def_pos(0, 0);
419  wxSize def_size(600, 500);
421  def_pos, def_size);
422  m_MainFrame->SetWorkbench(this);
424 
426  provider.RegisterFileAlias(wxT("frame::icon"), wxT("ncbilogo.ico"));
427 
428  wxIcon icon = provider.GetIcon(wxT("frame::icon"));
429  if (icon.IsOk())
430  m_MainFrame->SetIcon(icon);
431 
432  return m_MainFrame;
433 }
434 
435 
436 /// free resources associated with Extra Service Locator
438 {
439  LOG_POST(Info << "Shutting down services...");
440 
442  IService& service = *it->second;
443  service.ShutDownService();
444 
445  /// disconnect the service from Service Locator
446  Consumer_SetLocator(&service, NULL);
447  }
449 
450  LOG_POST(Info << "Finished shutting down services");
451 }
452 
453 /// filter events; forward command events to Window Manager in order to
454 /// deliver commands to active windows
455 bool CWorkbench::ProcessEvent(wxEvent& event)
456 {
457  static wxEventType inEvent = wxEVT_NULL;
458  if (inEvent == event.GetEventType())
459  return false;
460  inEvent = event.GetEventType();
461 
462  bool res = false;
463 
464  if(m_WindowManagerService && event.IsCommandEvent()) {
465  wxEventType type = event.GetEventType();
466  if(type == wxEVT_UPDATE_UI || type == wxEVT_COMMAND_MENU_SELECTED) {
467  // let Window Manager to process or deliver this command
468  wxCommandEvent* cmd_evt = dynamic_cast<wxCommandEvent*>(&event);
469  res = m_WindowManagerService->OnCommandEvent(*cmd_evt);
470  }
471  }
472  if( ! res) {
473  res = wxEvtHandler::ProcessEvent(event);
474  }
475 
476  inEvent = wxEVT_NULL;
477  return res;
478 }
479 
480 
CAppTaskService - Application Task Service.
CEventLogService - the standard implementation of IEventLogService.
virtual void SetHintListener(IHintListener *listener)
Definition: main_frame.cpp:75
virtual void SetRegistryPath(const string &path)
Definition: main_frame.cpp:81
virtual void LoadSettings()
Definition: main_frame.cpp:90
virtual void SaveSettings() const
Definition: main_frame.cpp:110
CMenuService - standard implementation of IMenuService.
CStatusBarService - the standard implementation of IStatusBarService interface.
static wxString ResolvePath(const wxString &path, const wxString &rel_name)
Utility function to hide the platform specifics of locating our standard directories and files.
Definition: sys_path.cpp:106
CUICommandRegistry is a centralized registry where all application commands should be registered.
Definition: ui_command.hpp:146
static CUICommandRegistry & GetInstance()
the main instance associated with the application
Definition: ui_command.cpp:176
CViewManagerService - the standard implementation of IViewManagerService interface.
CWindowManagerService - standard implementation of IWindowManagerService interface built on CWindowMa...
CWorkbenchFrame Main Application Frame for Workbench-based applications.
CwxSplittingArtProvider - an adapter for old-style image aliases.
Definition: wx_utils.hpp:255
IEventLogService - records application events.
IMenuService - Menu Service.
IRegSettings An interface for objects that save / restore settings using CGuiRegistry.
virtual void SetRegistryPath(const string &path)=0
IServiceLocator - an abstract mechanism for locating services.
Definition: service.hpp:71
IService an abstraction that represents an application component providing specific functional capa...
Definition: service.hpp:56
IToolBarService.
IViewManagerService IViewManagerService manages views in Workbench.
IWindowManagerService Window Manager Service provides access to Window Manager functionality.
IWorkbenchAdvisor This is an interface that assists IWorkbench in carrying out its functions.
Definition: workbench.hpp:71
void erase(iterator pos)
Definition: map.hpp:167
const_iterator end() const
Definition: map.hpp:152
void clear()
Definition: map.hpp:169
const_iterator find(const key_type &key) const
Definition: map.hpp:153
virtual void RegisterFileAlias(const wxArtID &anId, const wxArtClient &aClient, const wxSize &aSize, const wxString &aName, long aType=wxBITMAP_TYPE_ANY, int anIndex=-1)
#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
#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
void Consumer_SetLocator(T *obj, IServiceLocator *locator)
Definition: service.hpp:111
virtual void SetEventDisplayTime(int sec)
virtual void Init()
virtual IToolBarService * GetToolBarService()
CViewManagerService * m_ViewManagerService
CEventLogService * m_EventLogService
virtual CAppTaskService * GetAppTaskService()
virtual void x_ShutDownServices()
free resources associated with Extra Service Locator
virtual IViewManagerService * GetViewManagerService()
wxFileArtProvider * m_FileArtProvider
virtual wxFileArtProvider & GetFileArtProvider()
TNameToService m_NameToExtraService
a Map of Additional Services : Name -> Service
bool ProcessEvent(wxEvent &event)
filter events; forward command events to Window Manager in order to deliver commands to active window...
virtual wxFrame * GetMainWindow()
returns a pointer to the main application frame window
virtual void ShowCommandHint(const string &text)
virtual void PreShutDown()=0
called by Workbench before starting ShutDown
virtual CUICommandRegistry & GetUICommandRegistry()
returns an instance of Command Registry associated with the Workbench
virtual void x_InitMainFrame()
create application Main Frame
virtual IWindowManagerService * GetWindowManagerService()
virtual void SetWorkbench(IWorkbench *workbench)
virtual void SetRegistryPath(const string &path)
CWorkbenchFrame * m_MainFrame
pointers to the Standard Services
virtual CWorkbenchFrame * x_CreateMainFrame(const string &title)
virtual bool CanShutDown()
IWorkbenchAdvisor * m_Advisor
path to th Workbench section in the Registry
virtual void x_CreateFileArtProvider()
virtual CIRef< IService > GetService(const string &name)
retrieves the service
virtual ~CWorkbench()
bool OnCommandEvent(wxCommandEvent &event)
virtual void PreDestroyWindow()=0
called by Workbench before destroying main app window
CMenuService * m_MenuService
virtual void HideCommandHint()
virtual bool HasService(const string &name)
returns true if the service exists within the scope of the locator
virtual void AddContributor(IMenuContributor *contributor)
the service does NOT assume ownership of the contributor
virtual void SetWorkbench(IWorkbench *workbench)
connect / disconnect to / from Workbench
virtual void x_DestroyMainFrame()
destroy application Main Frame
CWorkbench(IWorkbenchAdvisor *advisor)
TODO.
virtual void InitService()=0
virtual void SetMenuService(IMenuService *service)
virtual void PostInit()=0
called after Workbench initialization has been completed
virtual void PostCreateWindow()=0
called by Workbench after creating main app window
virtual void SetFrame(wxFrame *frame, CUICommandRegistry &cmd_reg)
virtual bool CanShutDown()=0
virtual string GetRegistrySectionPath(ERegistrySection section)
returns a path to CGuiRegistry section where resources of the specified type shall be located
virtual void RegisterService(const string &class_name, IService *service)
register extra i.e.
virtual void UnRegisterService(const string &class_name)
remove application-specific service
virtual void SaveSettings() const
virtual void SetFrame(wxFrame *frame)
virtual IMenuService * GetMenuService()
virtual void ShutDownService()=0
virtual void ShutDown()
virtual IServiceLocator * GetServiceLocator()
get a Service Locator associated with the Workspace
CAppTaskService * m_AppTaskService
ERegistrySection
enumerates major Registry sections
Definition: workbench.hpp:116
virtual void LoadSettings()
virtual IStatusBarService * GetStatusBarService()
CStatusBarService * m_StatusBarService
virtual void ShowHintMessage(const string &msg)
virtual IEventLogService * GetEventLogService()
virtual string GetAppTitle()=0
get application title
virtual wxStatusBar * CreateStatusBar(wxFrame *frame)
CWindowManagerService * m_WindowManagerService
#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 void text(MDB_val *v)
Definition: mdb_dump.c:62
#define wxT(x)
Definition: muParser.cpp:41
wxEVT_COMMAND_MENU_SELECTED
static static static wxID_ANY
Definition: type.c:6
#define _ASSERT
USING_SCOPE(objects)
static const char * kMainFrameSection
void InitDefaultFileArtProvider(const wxString &dir)
Definition: wx_utils.cpp:323
wxFileArtProvider * GetDefaultFileArtProvider()
Definition: wx_utils.cpp:334
wxString ToWxString(const string &s)
Definition: wx_utils.hpp:173
Modified on Wed Nov 29 02:18:51 2023 by modify_doxy.py rev. 669887