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/Missing.h"
33 // #include "../MolLib/Ruby_bind/Molby_extern.h"
36 myID_addFileButton = 500,
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)
48 #pragma mark ====== Static utility functions ======
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)
55 GlobalParameterFilesFrame::~GlobalParameterFilesFrame()
57 wxGetApp().DocManager()->FileHistoryRemoveMenu(file_history_menu);
61 GlobalParameterFilesFrame::OnCreate()
64 // vertical{ listview, horizontal{ plus_button, minus_button } }
65 wxSize size(340, 160);
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);
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);
82 this->SetSizer(sizer0);
83 listctrl->SetDataSource(this);
84 remove_button->Enable(false);
87 wxMenuBar *menu_bar = wxGetApp().CreateMenuBar(2, &file_history_menu, &edit_menu);
91 GlobalParameterFilesFrame *
92 GlobalParameterFilesFrame::CreateGlobalParameterFilesFrame(wxMDIParentFrame *parent)
94 GlobalParameterFilesFrame *frame = new GlobalParameterFilesFrame(parent, _T("Load/Unload Global Parameters"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE);
102 GlobalParameterFilesFrame::OnCloseWindow(wxCloseEvent &event)
104 // Do not delete this window; it may be reopened later
109 GlobalParameterFilesFrame::OnClose(wxCommandEvent &event)
115 GlobalParameterFilesFrame::OnUpdateUI(wxUpdateUIEvent& event)
117 int uid = event.GetId();
118 if (uid == wxID_CLOSE)
123 GlobalParameterFilesFrame::OnAddGlobalParameterFile(wxCommandEvent& event)
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)));
130 int src_idx = ParameterCommentIndexForGlobalFileName(p);
131 for (i = gGlobalParInfo.count - 1; i >= 0; i--) {
132 if (gGlobalParInfo.files[i].src == src_idx)
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)
142 ParameterReadFromFile(NULL, p, &wbuf, NULL);
144 wxGetApp().SetConsoleColor(1);
145 wxGetApp().AppendConsoleMessage(wbuf);
146 wxGetApp().SetConsoleColor(0);
150 /* Need to update MD parameters for all molecules */
153 for (i = 0; (mol = MoleculeCallback_moleculeAtIndex(i)) != NULL; i++) {
154 mol->needsMDRebuild = 1;
159 listctrl->RefreshTable();
162 MyListCtrl *parameterListCtrl = wxGetApp().GetGlobalParameterListCtrl();
163 if (parameterListCtrl != NULL)
164 parameterListCtrl->RefreshTable();
169 GlobalParameterFilesFrame::OnRemoveGlobalParameterFile(wxCommandEvent& event)
177 n = listctrl->GetItemCount();
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);
185 files << wxString(cp, WX_DEFAULT_CONV);
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)
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);
203 listctrl->RefreshTable();
205 /* Request to update MD parameters for all molecules */
208 for (i = 0; (mol = MoleculeCallback_moleculeAtIndex(i)) != NULL; i++) {
209 mol->needsMDRebuild = 1;
214 MyListCtrl *parameterListCtrl = wxGetApp().GetGlobalParameterListCtrl();
215 if (parameterListCtrl != NULL)
216 parameterListCtrl->RefreshTable();
219 remove_button->Enable(false);
222 #pragma mark ====== MyListCtrl data source ======
224 /* Get information from gGlobalParInfo (defined in Parameter.c) */
226 GlobalParameterFilesFrame::GetItemCount(MyListCtrl *ctrl)
228 return gGlobalParInfo.count;
232 GlobalParameterFilesFrame::GetItemText(MyListCtrl *ctrl, long row, long column) const
234 ParFileInfo *ip = &gGlobalParInfo.files[row];
235 const char *p = NULL;
237 p = ParameterGetComment(ip->src);
238 else if (column == 1) {
239 if (row < gGlobalParInfo.builtinCount)
244 wxString str((p ? p : ""), WX_DEFAULT_CONV);
249 GlobalParameterFilesFrame::SetItemText(MyListCtrl *ctrl, long row, long column, const wxString &value)
255 GlobalParameterFilesFrame::DragSelectionToRow(MyListCtrl *ctrl, long row)
260 GlobalParameterFilesFrame::IsItemEditable(MyListCtrl *ctrl, long row, long column)
266 GlobalParameterFilesFrame::IsDragAndDropEnabled(MyListCtrl *ctrl)
272 GlobalParameterFilesFrame::OnSelectionChanged(MyListCtrl *ctrl)
275 ctrl->EnableSelectionChangeNotification(false);
276 n = ctrl->GetItemCount();
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);
285 ctrl->EnableSelectionChangeNotification(true);
287 remove_button->Enable(true);
288 else remove_button->Enable(false);
292 GlobalParameterFilesFrame::SetItemColor(MyListCtrl *ctrl, long row, long col, float *fg, float *bg)
294 if (row >= 0 && row < gGlobalParInfo.builtinCount && col == -1) {
295 fg[0] = fg[1] = fg[2] = 0.5;