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

Go to the SVN repository for this file.

1  /* $Id: wx_app.cpp 46061 2021-01-21 18:38:07Z grichenk $
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 
32 #include <ncbi_pch.hpp>
33 
34 #include <corelib/ncbi_system.hpp>
36 
37 #include <wx/image.h>
38 #include <wx/sysopt.h>
39 #include <wx/window.h>
40 
45 
48 
51 
52 #if defined(__WXMAC__) && !defined(__WXUNIVERSAL__)
53  #include <ApplicationServices/ApplicationServices.h>
54 #endif
55 
56 #include <wx/filename.h>
57 
58 
60 
61 
62 ///////////////////////////////////////////////////////////////////////////////
63 /// CNCBIwxApplication
64 
66 {
67  //NB: don't do this; it screws up application name settings in the logging
68  //framework
69  //SetProgramDisplayName(app_name);
70 
71 }
72 
73 
75 {
76 }
77 
78 
80 {
81 #ifdef _DEBUG
83 #else
85 #endif
87 
88  // now we are ready to setup wxApp object
90 }
91 
92 
93 // setup command line argument descriptions
95 {
96  // Create command-line argument descriptions class
97  unique_ptr<CArgDescriptions> arg_desc(new CArgDescriptions);
98 
99  // Specify USAGE context
100  arg_desc->SetUsageContext(GetArguments().GetProgramBasename(),
102 
103  // Setup arg.descriptions for this application
104  SetupArgDescriptions(arg_desc.release());
105 }
106 
107 
108 // create and setup an instance of wxApp
110 {
111  // initializer is provided by DECLARE_APP macro
112  wxAppInitializerFunction fnCreate = wxApp::GetInitializerFunction();
113  if (fnCreate) {
114  wxAppConsole* app = (*fnCreate)();
115  wxApp::SetInstance(app);
116 
117  // wxApp instance should inherit program name from CNCBIwxApplication
118  app->SetAppName(ToWxString(GetProgramDisplayName()));
119  } else {
120  _ASSERT(false);
121  }
122 }
123 
124 
126 {
127  int retcode = -1;
128 
130 
131  try {
132  retcode = x_Run();
133  } NCBI_CATCH("CNCBIwxApplication::Run()");
134 
135  if(retcode != 0) {
136  LOG_POST(Error << "Application terminated");
137  }
138  // at this point wxApp is already dead
139  return retcode;
140 }
141 
142 
144 {
145  // now execute wxApp instance
146  int i = 0;
147  int retcode = wxEntry(i, (char**)NULL); // no command line supplied
148  return retcode;
149 }
150 
151 
153 {
154  SetDiagStream(0);
155 }
156 
157 
158 ///////////////////////////////////////////////////////////////////////////////
159 /// CwxNCBIApp
160 
161 //IMPLEMENT_APP_NO_MAIN( ncbi::CwxNCBIApp )
162 ///IMPLEMENT_WX_THEME_SUPPORT
163 
164 // CwxNCBIApp type definition
166 
167 // CwxNCBIApp event table definition
168 BEGIN_EVENT_TABLE( CwxNCBIApp, wxApp )
169  EVT_IDLE( CwxNCBIApp::OnIdle )
171 
172 
173 CwxNCBIApp::CwxNCBIApp( bool use_job_disp )
174  : wxApp()
175  , m_UseJobDisp( use_job_disp )
176  , m_MaxThreads(4)
177  , m_MaxJobs(10000)
178  , m_RegistryLoaded(false)
179 {
180  SetVendorName(wxT("NCBI")); // TODO move into the base class
181 }
182 
183 
185 {
186 }
187 
189 {
190 #if defined(__WXMAC__) && !defined(__WXUNIVERSAL__)
191  wxSystemOptions::SetOption( wxT("mac.listctrl.always_use_generic"), true );
192 #endif
193 
195 
197 
198  if (!x_TestNcbiConnection())
199  return false;
200 
201  if(m_UseJobDisp) {
203  }
204 
205  bool ok = x_CreateGUI();
206  return ok;
207 }
208 
209 
211 {
212  x_DestroyGUI();
214 
215  // shut down App Job Dispatcher
216  if(m_UseJobDisp) {
218  disp.ShutDown();
220  }
221 
222  return wxApp::OnExit();
223 }
224 
225 
227 {
229 
230  // register the default Thread Pool engine for executing background UI tasks
232  disp.RegisterEngine("ThreadPool", *engine);
233 }
234 
235 
237 {
238  _ASSERT(false);
239  // override this function in derived classes, create a main frame and show it
240  return false;
241 }
242 
243 
245 {
246  // override this function in derived classes
247 }
248 
249 
251 {
252  if( m_RegistryLoaded ){
253  return;
254  }
255 
257 
258  wxString path = x_GetGuiRegistryPath();
259  string action = "Loading app gui registry - ";
260 
261  if( !path.empty() ){
262  if( wxFileName::FileExists( path ) ){
263  CNcbiIfstream istr( path.fn_str() );
264  if( istr ){
265  gui_reg.SetLocal( istr );
266  m_RegistryLoaded = true;
267  } else {
268  LOG_POST( Error << action << "registry file is invalid " << path );
269  }
270  } else {
271  LOG_POST( Info << action << "skipping, no file " << path );
272  }
273  } else {
274  LOG_POST( Info << action << "skipping, path is empty." );
275  }
276 }
277 
278 
280 {
281  wxString path = x_GetGuiRegistryPath();
282  if( !path.empty() ){
283  CNcbiOfstream ostr( path.fn_str() );
285  }
286  else {
287  LOG_POST( Info << "Saving app gui registry - skipping, path is empty" );
288  }
289 }
290 
292 {
293  return wxEmptyString; // no registry, override this in derived classes
294 }
295 
297 {
298 #if wxUSE_XPM
299  wxImage::AddHandler(new wxXPMHandler);
300 #endif
301 #if wxUSE_LIBPNG
302  wxImage::AddHandler(new wxPNGHandler);
303 #endif
304 #if wxUSE_LIBJPEG
305  wxImage::AddHandler(new wxJPEGHandler);
306 #endif
307 #if wxUSE_GIF
308  wxImage::AddHandler(new wxGIFHandler);
309 #endif
310 }
311 
312 void CwxNCBIApp::OnIdle( wxIdleEvent& event )
313 {
314  static bool fReentry = false;
315  if (fReentry)
316  return;
317  CBoolGuard _guard(fReentry);
318 
320  return;
321 
322  if (wxWindow::GetCapture())
323  return;
324 
326  return;
327 
328  try {
329  // if something has been done in this cycle - request more events
330  bool handled = x_OnIdle();
331  if (handled) {
332  event.RequestMore();
333  }
334  } NCBI_CATCH("CwxNCBIApp::OnIdle()");
335 
336  // let's base class to handle it as well
337  event.Skip();
338 }
339 
340 
342 {
343  // we poll various services to let them process their messages, to keep
344  // the application responsive we poll only one service at a time
345  bool handled = false;
346 
347  // CEventHandler has the highest priority - try it first
348  //
349  // if post event queue is long enough we force multiple events
350  // processing
351  const int kPostEventCount = 10;
352  for (int i = 0; i < kPostEventCount; ++i) {
354  handled |= h;
355  if (!h) break;
356  }
357 
358  if(m_UseJobDisp && !handled) {
360  handled |= disp.IdleCallback();
361  }
362  return handled;
363 }
364 
365 
CAppJobDispatcher.
CArgDescriptions –.
Definition: ncbiargs.hpp:541
static bool InsideAsyncCall()
Definition: async_call.cpp:143
void Write(CNcbiOstream &ostr, int priority=ePriority_Local) const
Write the local policy to a specified stream.
Definition: registry.cpp:124
static CGuiRegistry & GetInstance()
access the application-wide singleton
Definition: registry.cpp:400
void SetLocal(CNcbiIstream &istr)
establish our "local" repository.
Definition: registry.cpp:55
static CJobHandler & Instance()
Definition: job_future.cpp:198
virtual int Run(void)
Run the application.
Definition: wx_app.cpp:125
virtual int x_Run()
Definition: wx_app.cpp:143
virtual ~CNCBIwxApplication()
Definition: wx_app.cpp:74
CNCBIwxApplication(const string &app_name)
CNCBIwxApplication.
Definition: wx_app.cpp:65
virtual void Exit(void)
Cleanup on application exit.
Definition: wx_app.cpp:152
virtual void x_Init_wxApplication()
Definition: wx_app.cpp:109
virtual void x_SetupArgDescriptions()
Definition: wx_app.cpp:94
virtual void Init(void)
Initialize the application.
Definition: wx_app.cpp:79
CThreadPoolEngine.
CwxNCBIApp - this class should be used in NCBI applications instead of wxApp it provides polling supp...
Definition: wx_app.hpp:80
virtual ~CwxNCBIApp()
Definition: wx_app.cpp:184
virtual bool x_TestNcbiConnection()
override this to test/configure NCBI connection before access to NCBI network services
Definition: wx_app.hpp:101
virtual bool x_CreateGUI()
create main application window and initialize associated objects return true if successful
Definition: wx_app.cpp:236
void OnIdle(wxIdleEvent &event)
Definition: wx_app.cpp:312
bool m_RegistryLoaded
Definition: wx_app.hpp:128
virtual void x_DestroyGUI()
destroy application GUI objects
Definition: wx_app.cpp:244
virtual void x_SaveGuiRegistry()
Definition: wx_app.cpp:279
virtual void x_InitAppJobDispatcher()
override this function to configure / register engines
Definition: wx_app.cpp:226
virtual void x_LoadGuiRegistry()
Definition: wx_app.cpp:250
virtual bool OnInit()
Initialises the application.
Definition: wx_app.cpp:188
virtual bool x_OnIdle()
called from OnIdle() and can be overridden in derived classes returns true if something was done
Definition: wx_app.cpp:341
virtual int OnExit()
Called on exit.
Definition: wx_app.cpp:210
int m_MaxJobs
Definition: wx_app.hpp:126
virtual void x_RegisterImageHandlers()
register wxWidgets image handlers
Definition: wx_app.cpp:296
bool m_UseJobDisp
Definition: wx_app.hpp:123
int m_MaxThreads
Definition: wx_app.hpp:125
virtual wxString x_GetGuiRegistryPath()
Definition: wx_app.cpp:291
IMPLEMENT_CLASS(CFloatingFrame, CFloatingFrameBaseClass) const static long kFloatFrameStyle
CFloatingFrame.
#define false
Definition: bool.h:36
const CNcbiRegistry & GetConfig(void) const
Get the application's cached configuration parameters (read-only).
virtual void SetupArgDescriptions(CArgDescriptions *arg_desc)
Setup the command line argument descriptions.
Definition: ncbiapp.cpp:1195
const string & GetProgramDisplayName(void) const
Get the application's "display" name.
const CNcbiArguments & GetArguments(void) const
Get the application's cached unprocessed command-line arguments.
#define NULL
Definition: ncbistd.hpp:225
EDiagSev SetDiagPostLevel(EDiagSev post_sev=eDiag_Error)
Set the threshold severity for posting the messages.
Definition: ncbidiag.cpp:6129
#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 SetDiagStream(CNcbiOstream *os, bool quick_flush=true, FDiagCleanup cleanup=0, void *cleanup_data=0, const string &stream_name="")
Set diagnostic stream.
Definition: ncbidiag.cpp:8083
@ eDiag_Trace
Trace message.
Definition: ncbidiag.hpp:657
@ eDiag_Info
Informational message.
Definition: ncbidiag.hpp:651
void Error(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1197
#define NCBI_CATCH(message)
Catch CExceptions as well This macro is deprecated - use *_X or *_XX variant instead of it.
Definition: ncbiexpt.hpp:580
void Info(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1185
void ShutDown()
Terminates all jobs and releases Engines.
static CAppJobDispatcher & GetInstance()
static bool HandlePostRequest()
bool IdleCallback()
this function shall be called in the the application idle function.
static void ReleaseInstance()
get the Singleton Dispatcher
bool RegisterEngine(const string &name, IAppJobEngine &engine)
Registers a new Engine, returns true if successful.
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
IO_PREFIX::ofstream CNcbiOfstream
Portable alias for ofstream.
Definition: ncbistre.hpp:500
IO_PREFIX::ifstream CNcbiIfstream
Portable alias for ifstream.
Definition: ncbistre.hpp:439
virtual void Process(SOCK sock)=0
Runs asynchronously (from a separate thread) for each request.
unsigned int m_MaxThreads
Maximum simultaneous threads.
void CONNECT_Init(const IRWRegistry *reg=0, CRWLock *lock=0, TConnectInitFlags flag=eConnectInit_OwnNothing, FSSLSetup ssl=0)
Init [X]CONNECT library with the specified "reg" and "lock" (ownership for either or both can be deta...
END_EVENT_TABLE()
int i
#define wxT(x)
Definition: muParser.cpp:41
#define _ASSERT
wxString ToWxString(const string &s)
Definition: wx_utils.hpp:173
Modified on Wed Apr 17 13:09:06 2024 by modify_doxy.py rev. 669887