OSDN Git Service

Support load/save of NBO info
[molby/Molby.git] / wxSources / MyApp.h
1 /*
2  *  MyApp.h
3  *  Molby
4  *
5  *  Created by Toshi Nagata on 08/10/24.
6  *  Copyright 2008 Toshi Nagata. All rights reserved.
7  *
8  This program is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation version 2 of the License.
11  
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  GNU General Public License for more details.
16  */
17
18 #ifndef __MyApp_H__
19 #define __MyApp_H__
20
21 #include "wx/app.h"
22 #include "wx/mdi.h"
23 #include "wx/docview.h"
24 #include "wx/docmdi.h"
25 #include "wx/hashmap.h"
26 #include "wx/process.h"
27 #include "wx/timer.h"
28
29 #if defined(__WXMSW__)
30 #include "wx/snglinst.h"
31 #endif
32
33 #include "MyDocManager.h"
34
35 class MyDocManager;
36
37 #if defined(__WXMSW__)
38 class MyServer;
39 class MyClient;
40 #endif
41
42 class wxMenuBar;
43 class wxMenu;
44 class wxProgressDialog;
45
46 class ConsoleFrame;
47 class ProgressFrame;
48 class GlobalParameterFrame;
49 class GlobalParameterFilesFrame;
50 class MyListCtrl;
51 class MyDocument;
52
53 #if defined(__WXOSX_COCOA__)
54 #define wxTOGGLEBUTTON_STYLE wxBORDER_SIMPLE
55 #else
56 #define wxTOGGLEBUTTON_STYLE 0
57 #endif
58
59 #if wxCHECK_VERSION(3,1,0)
60 #define FromFrameDIP(frame, x) frame->FromDIP(x)
61 #define ToFrameDIP(frame, x) frame->ToDIP(x)
62 #else
63 #define FromFrameDIP(frame, x) (x)
64 #define ToFrameDIP(frame, x) (x)
65 #endif
66
67 #define FromDCDIP FromFrameDIP
68 #define ToDCDIP ToFrameDIP
69
70 enum {
71         myMenuID_MyFirstMenuItem = 12000,
72         myMenuID_Import = 12001,
73         myMenuID_Export = 12002,
74         myMenuID_ExportGraphic = 12003,
75         myMenuID_SelectFragment = 12004,
76         myMenuID_SelectReverse = 12005,
77
78         myMenuID_CreateNewAtom = 12010,
79         myMenuID_CreateNewParameter = 12011,
80         myMenuID_CreateNewVdwParameter = 12012,
81         myMenuID_CreateNewBondParameter = 12013,
82         myMenuID_CreateNewAngleParameter = 12014,
83         myMenuID_CreateNewDihedralParameter = 12015,
84         myMenuID_CreateNewImproperParameter = 12016,
85         myMenuID_CreateNewVdwPairParameter = 12017,
86         myMenuID_CreateNewVdwCutoffParameter = 12018,
87         myMenuID_CreatePiAnchor = 12019,
88
89         myMenuID_AddHydrogen = 12030,
90         myMenuID_AddHydrogenSp3 = 12031,
91         myMenuID_AddHydrogenSp2 = 12032,
92         myMenuID_AddHydrogenLinear = 12033,
93         myMenuID_AddHydrogenPyramidal = 12034,
94         myMenuID_AddHydrogenBent = 12035,
95
96         myMenuID_FitToScreen = 12050,
97         myMenuID_CenterSelection = 12051,
98
99         myMenuID_ShowAllAtoms = 12060,
100         myMenuID_HideSelected = 12061,
101         myMenuID_HideUnselected = 12062,
102         myMenuID_HideReverse = 12063,
103
104         myMenuID_MolecularDynamics = 12070,
105         myMenuID_Minimize = 12071,
106         myMenuID_StopMDRun = 12072,
107         
108         myMenuID_ViewGlobalParameters = 12080,
109         myMenuID_ViewParameterFilesList = 12081,
110         
111         myMenuID_BringAllWindowsToFront = 12090,
112         
113         myMenuID_ExecuteScript = 12100,
114         myMenuID_OpenConsoleWindow = 12101,
115         myMenuID_EmptyConsoleWindow = 12102,
116
117         myMenuID_PredefinedFragment = 12200,
118         myMenuID_MyLastFragment = 12999,
119         
120         //  The ID of script menu has "1000 * depth" offset
121         myMenuID_CustomScript = 13000,
122         
123         myMenuID_MyLastMenuItem = 29999,
124
125         myMenuID_Internal_CheckIfAllWindowsAreGone = 30000
126 };
127
128 enum {
129         myMenuIndex_File = 0,
130         myMenuIndex_Edit = 1,
131         myMenuIndex_Show = 2,
132         myMenuIndex_MMMD = 3,
133         myMenuIndex_Script = 4
134 };
135
136 //  Global Setting Keys
137 extern const char *gSettingQuitOnCloseLastWindow;
138
139 WX_DECLARE_STRING_HASH_MAP( wxString, MyStringHash );
140
141 //  A support class for wxProcess
142 //  When the process terminates, the exit status is kept inside the object
143 class wxBetterProcess : public wxProcess
144 {
145 public:
146     wxBetterProcess(wxEvtHandler *parent, int id) : wxProcess(parent, id)
147     {
148         m_status = 0;
149         m_terminated = false;
150         m_killSignal = wxSIGNONE;
151     }
152     wxBetterProcess(int flags) : wxProcess(flags)
153     {
154         m_status = 0;
155         m_terminated = false;
156         m_killSignal = wxSIGNONE;
157     }
158     virtual ~wxBetterProcess() {}
159     virtual void OnTerminate(int pid, int status);
160     wxKillError KillProcess(wxSignal sig = wxSIGTERM, int flags = wxKILL_NOCHILDREN);
161     int PutLine(wxString str);
162     int GetLine(wxString &outStr);
163     int GetLineSub(wxString &outStr, wxInputStream *stream, wxMemoryBuffer &mbuf);
164     int GetErrorLine(wxString &outStr);
165     void CloseOutput();
166     bool IsTerminated() { return m_terminated; }
167     int GetStatus() { return m_status; }
168     int GetKillSignal() { return m_killSignal; }
169 protected:
170     bool m_terminated;
171     int m_status;
172     int m_killSignal;
173     wxMemoryBuffer m_stdout;
174     wxMemoryBuffer m_stderr;
175     wxMemoryBuffer m_stdin;
176 };
177
178 #if wxCHECK_VERSION(3,1,0)
179 #define wxExecuteArgv(_argv, _flags, _process) wxExecute(_argv, _flags, _process)
180 #else
181 #define wxExecuteArgv(_argv, _flags, _process) wxExecute((char **)(_argv), _flags, _process)
182 #endif
183
184 // Define a new application
185 class MyApp: public wxApp
186 {
187   public:
188     MyApp(void);
189     virtual bool Initialize(int& argc, wxChar **argv);
190     bool OnInit(void);
191     int OnExit(void);
192
193         ConsoleFrame *GetConsoleFrame() { return consoleFrame; }
194
195         void ShowProgressPanel(const char *mes);
196         void HideProgressPanel();
197         void SetProgressValue(double dval);
198         void SetProgressMessage(const char *mes);
199         int IsInterrupted();
200         // ProgressFrame *GetProgressFrame() { return m_progressFrame; }
201     
202     MyDocManager *DocManager() { return m_docManager; }
203
204         wxMenuBar *CreateMenuBar(int kind, wxMenu **out_file_history_menu = NULL, wxMenu **out_edit_menu = NULL);
205
206     wxString InitResourcePath(int& argc, wxChar **argv);
207
208     static wxString FindResourcePath();
209         static wxString DefaultSettingsPath();
210
211         void LoadDefaultSettings();
212         void SaveDefaultSettings();
213         void SetDefaultSetting(const wxString& key, const wxString& value);
214         wxString& GetDefaultSetting(const wxString& key);
215
216         int LookupScriptMenu(const char *title);
217         int RegisterScriptMenu(const char *title);
218         void UpdateScriptMenu(wxMenuBar *mbar);
219         void OnScriptMenuModified(wxCommandEvent& event);
220         void OnScriptMenuSelected(wxCommandEvent& event);
221         void UpdatePredefinedFragmentMenu(wxMenuBar *mbar);
222         void OnFragmentMenuSelected(wxCommandEvent& event);
223         
224         void OnUpdateUI(wxUpdateUIEvent &event);
225         void OnExecuteScript(wxCommandEvent &event);
226         void OnOpenConsoleWindow(wxCommandEvent &event);
227         void OnEmptyConsoleWindow(wxCommandEvent &event);
228         void OnViewGlobalParameters(wxCommandEvent &event);
229         void OnViewParameterFilesList(wxCommandEvent &event);
230         void OnBringAllWindowsToFront(wxCommandEvent &event);
231         
232 //      void OnEndProcess(wxProcessEvent &event);
233         int CallSubProcess(const char **argv, const char *procname, int (*callback)(void *) = NULL, void *callback_data = NULL, FILE *fpout = NULL, FILE *fperr = NULL, int *exitstatus_p = NULL, int *pid_p = NULL);
234
235         void OnActivate(wxActivateEvent &event);
236
237         void RequestOpenFilesByEvent(wxString& files);
238         void OnOpenFilesByEvent(wxCommandEvent& event);
239         
240         bool OnOpenFiles(const wxString &files);
241
242         MyListCtrl *GetGlobalParameterListCtrl();
243         
244 #if defined(__WXMAC__)
245         virtual void MacNewFile();
246         virtual void MacOpenFile(const wxString &fileName);
247     virtual void MacOpenFiles(const wxArrayString &fileNames);
248 #endif
249         
250         void EnableTimerForDocument(MyDocument *doc);
251         void DisableTimerForDocument(MyDocument *doc);
252         void TimerInvoked(wxTimerEvent &event);
253
254         void CheckIfAllWindowsAreGoneHandler(wxCommandEvent &event);
255         void CheckIfAllWindowsAreGone(wxTopLevelWindow *frame);
256
257     void OnHelp(wxCommandEvent& event);
258
259     int FilterEvent(wxEvent &event);
260
261 protected:
262     MyDocManager* m_docManager;
263         wxProgressDialog *m_progressDialog;
264 //      bool m_progressCanceled;
265 //      int m_progressValue;
266         MyStringHash m_defaultSettings;
267
268         //  For CallSubProcess()
269         wxBetterProcess *m_process;
270 //      bool m_processTerminated;
271 //      int m_processExitCode;
272
273         ConsoleFrame *consoleFrame;
274         GlobalParameterFrame *parameterFrame;
275         GlobalParameterFilesFrame *parameterFilesFrame;
276         
277         int countNonCustomScriptMenu;
278         int countScriptMenu;
279         char **scriptMenuTitles;
280         int *scriptMenuPositions;
281         bool scriptMenuModifiedEventPosted;
282
283         int m_CountNamedFragments;
284         char **m_NamedFragments;
285
286         int m_CountTimerDocs;
287         MyDocument **m_TimerDocs;
288         wxTimer *m_Timer;
289
290         wxString *m_pendingFilesToOpen;  /*  Files to be processed by OnOpenFilesByEvent()  */
291
292         wxTopLevelWindow *m_frameToBeDestroyed;   /*  Used in CheckIfAllWindowsAreGone()  */
293         
294     wxString m_resourcePath;
295
296 #if defined(__WXMSW__)
297 public:
298         wxSingleInstanceChecker *m_checker;
299         wxString *m_ipcServiceName;
300         MyServer *m_server;
301         MyClient *m_client;
302 #endif
303         
304 private:
305         DECLARE_EVENT_TABLE()
306 };
307
308 DECLARE_APP(MyApp)
309
310 // About dialog
311 class AboutDialog: public wxDialog
312 {
313     DECLARE_CLASS(AboutDialog)
314 public:
315     AboutDialog();
316     void OnOKPressed(wxCommandEvent &event);
317 private:
318     DECLARE_EVENT_TABLE()
319 };
320
321 // Define a new frame
322 class MyFrame: public wxDocParentFrame
323 {
324         DECLARE_CLASS(MyFrame)
325 public:
326         wxMenu *editMenu;
327   
328         MyFrame(wxDocManager *manager, wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size, long type);
329
330         void OnAbout(wxCommandEvent& event);
331
332 private:
333         DECLARE_EVENT_TABLE()
334 };
335
336 extern MyFrame *GetMainFrame(void);
337 extern bool singleWindowMode;
338
339 #endif  /* __MyApp_H__ */