1 /*
2  * This file is part of the Code::Blocks IDE and licensed under the GNU Lesser General Public License, version 3
3  * http://www.gnu.org/licenses/lgpl-3.0.html
4  *
5  * $Revision: 10912 $
6  * $Id: projectdepsdlg.cpp 10912 2016-09-25 16:10:13Z fuscated $
7  * $HeadURL: svn://svn.code.sf.net/p/codeblocks/code/branches/release-20.xx/src/src/projectdepsdlg.cpp $
8  */
9 
10 #include "sdk.h"
11 
12 #ifndef CB_PRECOMP
13     #include "cbproject.h"
14     #include "manager.h"
15     #include "projectmanager.h"
16     #include <wx/button.h>
17     #include <wx/intl.h>
18     #include <wx/xrc/xmlres.h>
19     #include <wx/choice.h>
20     #include <wx/msgdlg.h>
21     #include <wx/checklst.h>
22 #endif
23 
24 #include "projectdepsdlg.h"
25 
26 
BEGIN_EVENT_TABLE(ProjectDepsDlg,wxScrollingDialog)27 BEGIN_EVENT_TABLE(ProjectDepsDlg, wxScrollingDialog)
28     EVT_CHOICE(XRCID("cmbProject"), ProjectDepsDlg::OnProjectChange)
29 END_EVENT_TABLE()
30 
31 ProjectDepsDlg::ProjectDepsDlg(wxWindow* parent, cbProject* sel)
32     : m_LastSel(-1)
33 {
34     //ctor
35     wxXmlResource::Get()->LoadObject(this, parent, _T("dlgConfigureProjectDeps"),_T("wxScrollingDialog"));
36     XRCCTRL(*this, "wxID_CANCEL", wxButton)->SetDefault();
37 
38     wxChoice* cmb = XRCCTRL(*this, "cmbProject", wxChoice);
39 
40     int idx = 0;
41     ProjectsArray* mainarr = Manager::Get()->GetProjectManager()->GetProjects();
42     for (size_t i = 0; i < mainarr->GetCount(); ++i)
43     {
44         cbProject* prj = mainarr->Item(i);
45         cmb->Append(prj->GetTitle(), prj);
46         if (prj == sel)
47             idx = i;
48     }
49     cmb->SetSelection(idx);
50     m_LastSel = idx;
51     FillList();
52 
53     Fit();
54 }
55 
~ProjectDepsDlg()56 ProjectDepsDlg::~ProjectDepsDlg()
57 {
58     //dtor
59 }
60 
SaveList()61 bool ProjectDepsDlg::SaveList()
62 {
63     wxChoice* cmb = XRCCTRL(*this, "cmbProject", wxChoice);
64     wxCheckListBox* lst = XRCCTRL(*this, "lstDeps", wxCheckListBox);
65 
66     if (m_LastSel == -1)
67         return true;
68 
69     cbProject* thisprj = static_cast<cbProject*>(cmb->GetClientData(m_LastSel));
70     if (!thisprj)
71         return true;
72 
73     // first clear all deps for this project
74     Manager::Get()->GetProjectManager()->ClearProjectDependencies(thisprj);
75 
76     // now set the the new deps
77     for (size_t i = 0; i < lst->GetCount(); ++i)
78     {
79         if (!lst->IsChecked(i))
80             continue;
81 
82         cbProject* prj = nullptr;
83 
84         ProjectsArray* mainarr = Manager::Get()->GetProjectManager()->GetProjects();
85         for (size_t x = 0; x < mainarr->GetCount(); ++x)
86         {
87             if (mainarr->Item(x)->GetTitle() == lst->GetString(i))
88             {
89                 prj = mainarr->Item(x);
90                 break;
91             }
92         }
93         if (!prj)
94             continue;
95 
96         if (!Manager::Get()->GetProjectManager()->AddProjectDependency(thisprj, prj))
97         {
98             cbMessageBox(wxString::Format(_("Cannot add project '%s' as a dependency to '%s' because this "
99                                             "would cause a circular dependency error..."),
100                                             thisprj->GetTitle().c_str(), prj->GetTitle().c_str()),
101                         _("Error"), wxICON_ERROR, this);
102             return false;
103         }
104     }
105     return true;
106 }
107 
FillList()108 void ProjectDepsDlg::FillList()
109 {
110     wxChoice* cmb = XRCCTRL(*this, "cmbProject", wxChoice);
111     wxCheckListBox* lst = XRCCTRL(*this, "lstDeps", wxCheckListBox);
112 
113     int idx = cmb->GetSelection();
114     if (m_LastSel != idx && m_LastSel != -1)
115     {
116         // save old list
117         SaveList();
118     }
119     m_LastSel = idx;
120     if (idx == -1)
121         return;
122 
123     cbProject* thisprj = static_cast<cbProject*>(cmb->GetClientData(idx));
124     if (!thisprj)
125         return;
126     const ProjectsArray* arr = Manager::Get()->GetProjectManager()->GetDependenciesForProject(thisprj);
127 
128     lst->Clear();
129     ProjectsArray* mainarr = Manager::Get()->GetProjectManager()->GetProjects();
130     for (size_t i = 0; i < mainarr->GetCount(); ++i)
131     {
132         cbProject* prj = mainarr->Item(i);
133         if (prj == thisprj)
134             continue;
135         lst->Append(prj->GetTitle());
136 
137         // check dependency
138         lst->Check(lst->GetCount() - 1, arr && arr->Index(prj) != wxNOT_FOUND);
139     }
140 }
141 
OnProjectChange(cb_unused wxCommandEvent & event)142 void ProjectDepsDlg::OnProjectChange(cb_unused wxCommandEvent& event)
143 {
144     FillList();
145 }
146 
EndModal(int retCode)147 void ProjectDepsDlg::EndModal(int retCode)
148 {
149     if (SaveList())
150         return wxScrollingDialog::EndModal(retCode);
151 }
152