OSDN Git Service

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