2 * GlobalParameterFilesFrame.cpp
5 * Created by Toshi Nagata on 09/11/14.
6 * Copyright 2009 Toshi Nagata. All rights reserved.
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.
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.
18 #include "GlobalParameterFilesFrame.h"
22 #include "wx/colour.h"
23 #include "wx/button.h"
24 #include "wx/filedlg.h"
25 #include "wx/msgdlg.h"
28 #include "MyListCtrl.h"
29 #include "../MolLib/MolLib.h"
30 #include "../MolLib/Ruby_bind/Molby_extern.h"
31 #include "../MolLib/Missing.h"
34 // #include "../MolLib/Ruby_bind/Molby_extern.h"
37 myID_addFileButton = 500,
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)
49 #pragma mark ====== Static utility functions ======
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)
56 GlobalParameterFilesFrame::~GlobalParameterFilesFrame()
58 wxGetApp().DocManager()->FileHistoryRemoveMenu(file_history_menu);
62 GlobalParameterFilesFrame::OnCreate()
65 // vertical{ listview, horizontal{ plus_button, minus_button } }
66 wxSize size(340, 160);
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);
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);
83 this->SetSizer(sizer0);
84 listctrl->SetDataSource(this);
85 remove_button->Enable(false);
88 wxMenuBar *menu_bar = wxGetApp().CreateMenuBar(2, &file_history_menu, &edit_menu);
92 GlobalParameterFilesFrame *
93 GlobalParameterFilesFrame::CreateGlobalParameterFilesFrame(wxWindow *parent)
96 wxPoint origin(16, 16);
97 wxSize size(700, 240);
99 wxPoint origin(26, 40);
100 wxSize size(700, 240);
103 GlobalParameterFilesFrame *frame = new GlobalParameterFilesFrame(parent, _T("Load/Unload Global Parameters"), origin, size, wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE);
110 GlobalParameterFilesFrame::OnCloseWindow(wxCloseEvent &event)
112 // Do not delete this window; it may be reopened later
114 // Check if all windows are gone
115 wxGetApp().CheckIfAllWindowsAreGone(NULL);
119 GlobalParameterFilesFrame::OnClose(wxCommandEvent &event)
125 GlobalParameterFilesFrame::OnUpdateUI(wxUpdateUIEvent& event)
127 int uid = event.GetId();
128 if (uid == wxID_CLOSE)
133 GlobalParameterFilesFrame::OnAddGlobalParameterFile(wxCommandEvent& event)
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)));
140 int src_idx = ParameterCommentIndexForGlobalFileName(p);
141 for (i = gGlobalParInfo.count - 1; i >= 0; i--) {
142 if (gGlobalParInfo.files[i].src == src_idx)
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)
152 ParameterReadFromFile(NULL, p, &wbuf, NULL);
154 MyAppCallback_setConsoleColor(1);
155 MyAppCallback_showScriptMessage("%s", wbuf);
156 MyAppCallback_setConsoleColor(0);
160 /* Need to update MD parameters for all molecules */
163 for (i = 0; (mol = MoleculeCallback_moleculeAtIndex(i)) != NULL; i++) {
164 mol->needsMDRebuild = 1;
169 listctrl->RefreshTable();
172 MyListCtrl *parameterListCtrl = wxGetApp().GetGlobalParameterListCtrl();
173 if (parameterListCtrl != NULL)
174 parameterListCtrl->RefreshTable();
179 GlobalParameterFilesFrame::OnRemoveGlobalParameterFile(wxCommandEvent& event)
187 n = listctrl->GetItemCount();
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);
195 files << wxString(cp, WX_DEFAULT_CONV);
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)
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);
213 listctrl->RefreshTable();
215 /* Request to update MD parameters for all molecules */
218 for (i = 0; (mol = MoleculeCallback_moleculeAtIndex(i)) != NULL; i++) {
219 mol->needsMDRebuild = 1;
224 MyListCtrl *parameterListCtrl = wxGetApp().GetGlobalParameterListCtrl();
225 if (parameterListCtrl != NULL)
226 parameterListCtrl->RefreshTable();
229 remove_button->Enable(false);
232 #pragma mark ====== MyListCtrl data source ======
234 /* Get information from gGlobalParInfo (defined in Parameter.c) */
236 GlobalParameterFilesFrame::GetItemCount(MyListCtrl *ctrl)
238 return gGlobalParInfo.count;
242 GlobalParameterFilesFrame::GetItemText(MyListCtrl *ctrl, long row, long column) const
244 ParFileInfo *ip = &gGlobalParInfo.files[row];
245 const char *p = NULL;
247 p = ParameterGetComment(ip->src);
248 else if (column == 1) {
249 if (row < gGlobalParInfo.builtinCount)
254 wxString str((p ? p : ""), WX_DEFAULT_CONV);
259 GlobalParameterFilesFrame::SetItemText(MyListCtrl *ctrl, long row, long column, const wxString &value)
265 GlobalParameterFilesFrame::DragSelectionToRow(MyListCtrl *ctrl, long row)
270 GlobalParameterFilesFrame::IsItemEditable(MyListCtrl *ctrl, long row, long column)
276 GlobalParameterFilesFrame::IsDragAndDropEnabled(MyListCtrl *ctrl)
282 GlobalParameterFilesFrame::OnSelectionChanged(MyListCtrl *ctrl)
285 ctrl->EnableSelectionChangeNotification(false);
286 n = ctrl->GetItemCount();
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);
295 ctrl->EnableSelectionChangeNotification(true);
297 remove_button->Enable(true);
298 else remove_button->Enable(false);
302 GlobalParameterFilesFrame::SetItemColor(MyListCtrl *ctrl, long row, long col, float *fg, float *bg)
304 if (row >= 0 && row < gGlobalParInfo.builtinCount && col == -1) {
305 fg[0] = fg[1] = fg[2] = 0.5;