OSDN Git Service

Unit Cell dialog is being modified. Still not working very well.
[molby/Molby.git] / wxSources / GlobalParameterFilesFrame.cpp
1 /*
2  *  GlobalParameterFilesFrame.cpp
3  *  Molby
4  *
5  *  Created by Toshi Nagata on 09/11/14.
6  *  Copyright 2009 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 #include "GlobalParameterFilesFrame.h"
19
20 #include "wx/menu.h"
21 #include "wx/regex.h"
22 #include "wx/colour.h"
23 #include "wx/button.h"
24 #include "wx/filedlg.h"
25 #include "wx/msgdlg.h"
26
27 #include "MyApp.h"
28 #include "MyListCtrl.h"
29 #include "../MolLib/MolLib.h"
30 #include "../MolLib/Ruby_bind/Molby_extern.h"
31 #include "../MolLib/Missing.h"
32 #include "MyMBConv.h"
33
34 // #include "../MolLib/Ruby_bind/Molby_extern.h"
35
36 enum {
37         myID_addFileButton = 500,
38         myID_removeFileButton
39 };
40
41 BEGIN_EVENT_TABLE(GlobalParameterFilesFrame, wxFrame)
42 EVT_CLOSE(GlobalParameterFilesFrame::OnCloseWindow)
43 EVT_MENU(wxID_CLOSE, GlobalParameterFilesFrame::OnClose)
44 EVT_UPDATE_UI(wxID_CLOSE, GlobalParameterFilesFrame::OnUpdateUI)
45 EVT_BUTTON(myID_addFileButton, GlobalParameterFilesFrame::OnAddGlobalParameterFile)
46 EVT_BUTTON(myID_removeFileButton, GlobalParameterFilesFrame::OnRemoveGlobalParameterFile)
47 END_EVENT_TABLE()
48
49 #pragma mark ====== Static utility functions ======
50                                                                         
51 GlobalParameterFilesFrame::GlobalParameterFilesFrame(wxWindow *parent, const wxString& title, const wxPoint& pos, const wxSize& size, long type):
52 wxFrame(parent, wxID_ANY, title, pos, size, type)
53 {
54 }
55
56 GlobalParameterFilesFrame::~GlobalParameterFilesFrame()
57 {
58         wxGetApp().DocManager()->FileHistoryRemoveMenu(file_history_menu);
59 }
60
61 void
62 GlobalParameterFilesFrame::OnCreate()
63 {
64         //  Frame structure:
65         //  vertical{ listview, horizontal{ plus_button, minus_button } }
66         wxSize size(340, 160);
67         wxBoxSizer *sizer0;
68         sizer0 = new wxBoxSizer(wxVERTICAL);
69         listctrl = new MyListCtrl();
70         listctrl->Create(this, wxID_ANY, wxDefaultPosition, size);
71         listctrl->InsertColumn(0, _T("name"), wxLIST_FORMAT_LEFT, 80);
72         listctrl->InsertColumn(1, _T("directory"), wxLIST_FORMAT_LEFT, 240);
73         sizer0->Add(listctrl, 1, wxLEFT | wxRIGHT | wxEXPAND, 0);
74         {
75                 wxBoxSizer *sizer1;
76                 sizer1 = new wxBoxSizer(wxHORIZONTAL);
77                 add_button = new wxButton(this, myID_addFileButton, _T("Load..."), wxDefaultPosition, wxDefaultSize);
78                 remove_button = new wxButton(this, myID_removeFileButton, _T("Unload"), wxDefaultPosition, wxDefaultSize);
79                 sizer1->Add(add_button, 0, wxALL | wxEXPAND, 3);
80                 sizer1->Add(remove_button, 0, wxALL | wxEXPAND, 3);
81                 sizer0->Add(sizer1, 0, wxALL | wxEXPAND, 8);
82         }
83         this->SetSizer(sizer0);
84         listctrl->SetDataSource(this);
85         remove_button->Enable(false);
86
87         /*  Create menu bar  */
88         wxMenuBar *menu_bar = wxGetApp().CreateMenuBar(2, &file_history_menu, &edit_menu);
89         SetMenuBar(menu_bar);
90 }
91
92 GlobalParameterFilesFrame *
93 GlobalParameterFilesFrame::CreateGlobalParameterFilesFrame(wxWindow *parent)
94 {
95 #ifdef __WXMSW__
96         wxPoint origin(16, 16);
97         wxSize size(700, 240);
98 #else
99         wxPoint origin(26, 40);
100         wxSize size(700, 240);
101 #endif
102
103         GlobalParameterFilesFrame *frame = new GlobalParameterFilesFrame(parent, _T("Load/Unload Global Parameters"), origin, size, wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE);
104         
105         frame->OnCreate();
106         return frame;
107 }
108
109 void
110 GlobalParameterFilesFrame::OnCloseWindow(wxCloseEvent &event)
111 {
112         //  Do not delete this window; it may be reopened later
113         this->Hide();
114         //  Check if all windows are gone
115         wxGetApp().CheckIfAllWindowsAreGone(NULL);
116 }
117
118 void
119 GlobalParameterFilesFrame::OnClose(wxCommandEvent &event)
120 {
121         this->Close();
122 }
123
124 void
125 GlobalParameterFilesFrame::OnUpdateUI(wxUpdateUIEvent& event)
126 {
127         int uid = event.GetId();
128         if (uid == wxID_CLOSE)
129                 event.Enable(true);
130 }
131
132 void
133 GlobalParameterFilesFrame::OnAddGlobalParameterFile(wxCommandEvent& event)
134 {
135         wxFileDialog *dialog = new wxFileDialog(NULL, _T("Choose Parameter File"), _T(""), _T(""), _T("All files (*.*)|*.*"), wxFD_OPEN | wxFD_CHANGE_DIR | wxFD_FILE_MUST_EXIST);
136         if (dialog->ShowModal() == wxID_OK) {
137                 char *p = strdup((const char *)(dialog->GetPath().mb_str(wxConvFile)));
138                 char *wbuf;
139                 int i;
140                 int src_idx = ParameterCommentIndexForGlobalFileName(p);
141                 for (i = gGlobalParInfo.count - 1; i >= 0; i--) {
142                         if (gGlobalParInfo.files[i].src == src_idx)
143                                 break;
144                 }
145                 if (i >= 0) {
146                         char *s;
147                         asprintf(&s, "File '%s' has already been read. Do you want to replace the parameters?", ParameterGetComment(src_idx));
148                         wxString mes(s, WX_DEFAULT_CONV);
149                         if (::wxMessageBox(mes, _T("Reload global parameter file"), wxOK | wxCANCEL | wxICON_EXCLAMATION) != wxOK)
150                                 return;
151                 }
152                 ParameterReadFromFile(NULL, p, &wbuf, NULL);
153                 if (wbuf != NULL) {
154                         MyAppCallback_setConsoleColor(1);
155                         MyAppCallback_showScriptMessage("%s", wbuf);
156                         MyAppCallback_setConsoleColor(0);
157                         free(wbuf);
158                 }
159                 free(p);
160                 /*  Need to update MD parameters for all molecules  */
161                 {
162                         Molecule *mol;
163                         for (i = 0; (mol = MoleculeCallback_moleculeAtIndex(i)) != NULL; i++) {
164                                 mol->needsMDRebuild = 1;
165                         }
166                 }
167         }
168         dialog->Destroy();
169         listctrl->RefreshTable();
170
171         {
172                 MyListCtrl *parameterListCtrl = wxGetApp().GetGlobalParameterListCtrl();
173                 if (parameterListCtrl != NULL)
174                         parameterListCtrl->RefreshTable();
175         }
176 }
177
178 void
179 GlobalParameterFilesFrame::OnRemoveGlobalParameterFile(wxCommandEvent& event)
180 {
181         int i, n, cn;
182         wxString files;
183         ParFileInfo *ip;
184         const char *cp;
185         char *s;
186
187         n = listctrl->GetItemCount();
188         cn = 0;
189         for (i = 0; i < n; i++) {
190                 if (i >= gGlobalParInfo.builtinCount && listctrl->GetItemState(i, wxLIST_STATE_SELECTED)) {
191                         ip = &gGlobalParInfo.files[i];
192                         cp = ParameterGetComment(ip->src);
193                         if (cn > 0)
194                                 files << _T(", ");
195                         files << wxString(cp, WX_DEFAULT_CONV);
196                         cn++;
197                 }
198         }
199         if (cn == 0)
200                 return;
201         
202         asprintf(&s, "Do you really want to unload the parameter%s %s?", (cn > 1 ? "s" : ""), (const char *)files.mb_str(WX_DEFAULT_CONV));
203         wxString mes(s, WX_DEFAULT_CONV);
204         if (::wxMessageBox(mes, _T("Unload global parameter file"), wxOK | wxCANCEL | wxICON_EXCLAMATION) != wxOK)
205                 return;
206         for (i = gGlobalParInfo.count - 1; i >= gGlobalParInfo.builtinCount; i--) {
207                 if (listctrl->GetItemState(i, wxLIST_STATE_SELECTED)) {
208                         ip = &gGlobalParInfo.files[i];
209                         ParameterDeleteAllEntriesForSource(gBuiltinParameters, ip->src);
210                         listctrl->SetItemState(i, 0, wxLIST_STATE_SELECTED);
211                 }
212         }
213         listctrl->RefreshTable();
214
215         /*  Request to update MD parameters for all molecules  */
216         {
217                 Molecule *mol;
218                 for (i = 0; (mol = MoleculeCallback_moleculeAtIndex(i)) != NULL; i++) {
219                         mol->needsMDRebuild = 1;
220                 }
221         }
222         
223         {
224                 MyListCtrl *parameterListCtrl = wxGetApp().GetGlobalParameterListCtrl();
225                 if (parameterListCtrl != NULL)
226                         parameterListCtrl->RefreshTable();
227         }
228         
229         remove_button->Enable(false);
230 }
231
232 #pragma mark ====== MyListCtrl data source ======
233
234 /*  Get information from gGlobalParInfo (defined in Parameter.c)  */
235 int
236 GlobalParameterFilesFrame::GetItemCount(MyListCtrl *ctrl)
237 {
238         return gGlobalParInfo.count;
239 }
240
241 wxString
242 GlobalParameterFilesFrame::GetItemText(MyListCtrl *ctrl, long row, long column) const
243 {
244         ParFileInfo *ip = &gGlobalParInfo.files[row];
245         const char *p = NULL;
246         if (column == 0)
247                 p = ParameterGetComment(ip->src);
248         else if (column == 1) {
249                 if (row < gGlobalParInfo.builtinCount)
250                         p = "(built-in)";
251                 else
252                         p = ip->dir;
253         }
254         wxString str((p ? p : ""), WX_DEFAULT_CONV);
255         return str;
256 }
257
258 int
259 GlobalParameterFilesFrame::SetItemText(MyListCtrl *ctrl, long row, long column, const wxString &value)
260 {
261         return 0;
262 }
263
264 void
265 GlobalParameterFilesFrame::DragSelectionToRow(MyListCtrl *ctrl, long row)
266 {
267 }
268
269 bool
270 GlobalParameterFilesFrame::IsItemEditable(MyListCtrl *ctrl, long row, long column)
271 {
272         return false;
273 }
274
275 bool
276 GlobalParameterFilesFrame::IsDragAndDropEnabled(MyListCtrl *ctrl)
277 {
278         return 0;
279 }
280
281 void
282 GlobalParameterFilesFrame::OnSelectionChanged(MyListCtrl *ctrl)
283 {
284         int i, n, cn;
285         ctrl->EnableSelectionChangeNotification(false);
286         n = ctrl->GetItemCount();
287         cn = 0;
288         for (i = 0; i < n; i++) {
289                 if (ctrl->GetItemState(i, wxLIST_STATE_SELECTED)) {
290                         if (i < gGlobalParInfo.builtinCount)
291                                 ctrl->SetItemState(i, 0, wxLIST_STATE_SELECTED);
292                         else cn++;
293                 }
294         }
295         ctrl->EnableSelectionChangeNotification(true);
296         if (cn > 0)
297                 remove_button->Enable(true);
298         else remove_button->Enable(false);
299 }
300
301 int
302 GlobalParameterFilesFrame::SetItemColor(MyListCtrl *ctrl, long row, long col, float *fg, float *bg)
303 {
304         if (row >= 0 && row < gGlobalParInfo.builtinCount && col == -1) {
305                 fg[0] = fg[1] = fg[2] = 0.5;
306                 return 1;
307         } else return 0;
308 }