1 /*
2  * This file is part of the Code::Blocks IDE and licensed under the GNU General Public License, version 3
3  * http://www.gnu.org/licenses/gpl-3.0.html
4  *
5  */
6 
7 #include "fpoptionsprjdlg.h"
8 
9 #include <sdk.h>
10 #ifndef CB_PRECOMP
11     #include <wx/button.h>
12     #include <wx/intl.h>
13     #include <wx/listbox.h>
14     #include <wx/xrc/xmlres.h>
15 
16     #include <cbproject.h>
17     #include <cbstyledtextctrl.h>
18     #include <globals.h>
19     #include <logmanager.h>
20     #include <manager.h>
21 #endif
22 
23 #include <editpathdlg.h>
24 
BEGIN_EVENT_TABLE(FPOptionsProjectDlg,wxPanel)25 BEGIN_EVENT_TABLE(FPOptionsProjectDlg, wxPanel)
26     EVT_UPDATE_UI(-1, FPOptionsProjectDlg::OnUpdateUI)
27     EVT_BUTTON(XRCID("btnAdd"), FPOptionsProjectDlg::OnAdd)
28     EVT_BUTTON(XRCID("btnEdit"), FPOptionsProjectDlg::OnEdit)
29     EVT_BUTTON(XRCID("btnDelete"), FPOptionsProjectDlg::OnDelete)
30 END_EVENT_TABLE()
31 
32 FPOptionsProjectDlg::FPOptionsProjectDlg(wxWindow* parent, cbProject* project, NativeParserF* np) :
33     m_pProject(project),
34     m_pNativeParser(np)
35 {
36     wxXmlResource::Get()->LoadPanel(this, parent, _T("pnlProjectFPOptions"));
37     m_OldPaths = m_pNativeParser->GetProjectSearchDirs(m_pProject);
38 
39     wxListBox* control = XRCCTRL(*this, "lstPaths", wxListBox);
40     control->Clear();
41     for (size_t i = 0; i < m_OldPaths.GetCount(); ++i)
42         control->Append(m_OldPaths[i]);
43 }
44 
~FPOptionsProjectDlg()45 FPOptionsProjectDlg::~FPOptionsProjectDlg()
46 {
47 }
48 
OnAdd(cb_unused wxCommandEvent & event)49 void FPOptionsProjectDlg::OnAdd(cb_unused wxCommandEvent& event)
50 {
51     wxListBox* control = XRCCTRL(*this, "lstPaths", wxListBox);
52 
53     EditPathDlg dlg(this,
54                     m_pProject ? m_pProject->GetBasePath() : _T(""),
55                     m_pProject ? m_pProject->GetBasePath() : _T(""),
56                     _("Add directory"));
57 
58     PlaceWindow(&dlg);
59     if (dlg.ShowModal() == wxID_OK)
60     {
61         wxString path = dlg.GetPath();
62         control->Append(path);
63     }
64 }
65 
OnEdit(cb_unused wxCommandEvent & event)66 void FPOptionsProjectDlg::OnEdit(cb_unused wxCommandEvent& event)
67 {
68     wxListBox* control = XRCCTRL(*this, "lstPaths", wxListBox);
69     int sel = control->GetSelection();
70     if (sel < 0)
71         return;
72 
73     EditPathDlg dlg(this,
74                     control->GetString(sel),
75                     m_pProject ? m_pProject->GetBasePath() : _T(""),
76                     _("Edit directory"));
77 
78     PlaceWindow(&dlg);
79     if (dlg.ShowModal() == wxID_OK)
80     {
81         wxString path = dlg.GetPath();
82         control->SetString(sel, path);
83     }
84 }
85 
OnDelete(cb_unused wxCommandEvent & event)86 void FPOptionsProjectDlg::OnDelete(cb_unused wxCommandEvent& event)
87 {
88     wxListBox* control = XRCCTRL(*this, "lstPaths", wxListBox);
89     int sel = control->GetSelection();
90     if (sel < 0)
91         return;
92 
93     control->Delete(sel);
94 }
95 
OnUpdateUI(cb_unused wxUpdateUIEvent & event)96 void FPOptionsProjectDlg::OnUpdateUI(cb_unused wxUpdateUIEvent& event)
97 {
98     wxListBox* control = XRCCTRL(*this, "lstPaths", wxListBox);
99     bool en = control->GetSelection() >= 0;
100 
101     XRCCTRL(*this, "btnEdit", wxButton)->Enable(en);
102     XRCCTRL(*this, "btnDelete", wxButton)->Enable(en);
103 }
104 
OnApply()105 void FPOptionsProjectDlg::OnApply()
106 {
107     wxArrayString newpaths;
108     wxListBox* control = XRCCTRL(*this, "lstPaths", wxListBox);
109     for (int i = 0; i < (int)control->GetCount(); ++i)
110         newpaths.Add(control->GetString(i));
111 
112     if (!m_pNativeParser || !m_pProject)
113         return;
114 
115     if (m_OldPaths != newpaths)
116     {
117         m_pNativeParser->SetProjectSearchDirs(m_pProject, newpaths);
118         m_pNativeParser->ForceReparseProjectSearchDirs();
119     }
120 }
121