Code::Blocks  SVN r11506
selecttargetdlg.cpp
Go to the documentation of this file.
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: 11113 $
6  * $Id: selecttargetdlg.cpp 11113 2017-07-16 08:29:13Z fuscated $
7  * $HeadURL: https://svn.code.sf.net/p/codeblocks/code/trunk/src/sdk/selecttargetdlg.cpp $
8  */
9 
10 #include "sdk_precomp.h"
11 #ifndef CB_PRECOMP
12  #include <wx/button.h>
13  #include <wx/checkbox.h>
14  #include <wx/filename.h>
15  #include <wx/intl.h>
16  #include <wx/listbox.h>
17  #include <wx/textctrl.h>
18  #include <wx/xrc/xmlres.h>
19 
20  #include "projectbuildtarget.h"
21  #include "cbproject.h"
22 #endif
23 #include <wx/filedlg.h>
24 #include "selecttargetdlg.h"
25 
30 {
31  public:
32  TargetListData(const wxString& ExecParameters, const wxString& HostApp, bool RunHostAppInTerminal,
33  bool DefaultExecTarget) :
34  m_HostApp(HostApp),
35  m_ExecParameters(ExecParameters),
36  m_RunHostAppInTerminal(RunHostAppInTerminal),
37  m_DefaultExecTarget(DefaultExecTarget)
38  {
39  }
40 
41  void SetExecParameters(const wxString& ExecParameters)
42  {
43  m_ExecParameters = ExecParameters;
44  // Replace all '\n' by ' ' in the program-parameters .
45  // This is necessary because a multiline text control may add
46  // '\n' to the text but these characters must not be part of the
47  // parameters when executing the program.
48  m_ExecParameters.Replace(_T("\n"), _T(" "));
49  }
50  void SetHostApp(const wxString &hostApp) { m_HostApp = hostApp; }
51  void SetRunHostAppInTerminal(bool runAppInTerminal) { m_RunHostAppInTerminal = runAppInTerminal; }
52  void SetDefaultExecTarget(bool defaultTarget) { m_DefaultExecTarget = defaultTarget; }
53 
54  const wxString& GetHostApp() const { return m_HostApp; }
55  const wxString& GetExecParameters() const { return m_ExecParameters; }
57  bool GetDefaultExecTarget() const { return m_DefaultExecTarget; }
58 
59  private:
64 };
65 
66 BEGIN_EVENT_TABLE(SelectTargetDlg, wxScrollingDialog)
67  EVT_LISTBOX(XRCID("lstItems"), SelectTargetDlg::OnListboxSelection)
68  EVT_LISTBOX_DCLICK(XRCID("lstItems"), SelectTargetDlg::OnListboxDClick)
69  EVT_BUTTON(XRCID("btnHostApplication"), SelectTargetDlg::OnHostApplicationButtonClick)
70 END_EVENT_TABLE()
71 
72 SelectTargetDlg::SelectTargetDlg(wxWindow* parent, cbProject* project, int selected) :
73  m_pProject(project),
74  m_Selected(selected),
75  m_LastSelected(wxNOT_FOUND)
76 {
77  //ctor
78  cbAssert(m_pProject);
79 
80  wxXmlResource::Get()->LoadObject(this, parent, _T("dlgSelectTarget"), _T("wxScrollingDialog"));
81 
82  m_List = XRCCTRL(*this, "lstItems", wxListBox);
83  m_List->Clear();
84  for (int i = 0; i < m_pProject->GetBuildTargetsCount(); ++i)
85  {
86  ProjectBuildTarget* target = m_pProject->GetBuildTarget(i);
87  auto targetData = new TargetListData(target->GetExecutionParameters(), target->GetHostApplication(),
89  (m_pProject->GetDefaultExecuteTarget() == target->GetTitle()));
90  m_List->Append(target->GetTitle(), targetData);
91  }
92  if (selected != -1)
93  m_List->SetSelection(selected);
94  else
95  {
96  int item = m_List->FindString(m_pProject->GetActiveBuildTarget());
97  if (item == wxNOT_FOUND)
98  item = m_List->FindString(m_pProject->GetDefaultExecuteTarget());
99  m_List->SetSelection(item);
100  }
101 
102  UpdateSelected();
103  wxButton *okButton = XRCCTRL(*this, "wxID_OK", wxButton);
104  okButton->MoveBeforeInTabOrder (XRCCTRL(*this, "lstItems", wxListBox));
105  okButton->SetDefault();
106 }
107 
109 {
110  //dtor
111 }
112 
114 {
115  wxCheckBox* chkSetAsDefaultExec = XRCCTRL(*this, "chkSetAsDefaultExec", wxCheckBox);
116  wxTextCtrl* txtParams = XRCCTRL(*this, "txtParams", wxTextCtrl);
117  wxTextCtrl* txtHostApp = XRCCTRL(*this, "txtHostApp", wxTextCtrl);
118  wxCheckBox* chkHostInTerminal = XRCCTRL(*this, "chkHostInTerminal", wxCheckBox);
119 
120  if (m_LastSelected != wxNOT_FOUND)
121  {
122  auto lastData = static_cast<TargetListData*>(m_List->GetClientObject(m_LastSelected));
123  lastData->SetExecParameters(txtParams->GetValue());
124  lastData->SetHostApp(txtHostApp->GetValue());
125  lastData->SetRunHostAppInTerminal(chkHostInTerminal->GetValue());
126 
127  if (chkSetAsDefaultExec->GetValue())
128  {
129  for (size_t i = 0; i < m_List->GetCount(); i++)
130  {
131  auto data = static_cast<TargetListData*>(m_List->GetClientObject(i));
132  data->SetDefaultExecTarget(false);
133  }
134  }
135  lastData->SetDefaultExecTarget(chkSetAsDefaultExec->GetValue());
136  }
137 
138  if (m_List->GetSelection() != wxNOT_FOUND)
139  {
140  auto selectedData = static_cast<TargetListData*>(m_List->GetClientObject(m_List->GetSelection()));
141  chkSetAsDefaultExec->SetValue(selectedData->GetDefaultExecTarget());
142  txtParams->SetValue(selectedData->GetExecParameters());
143  txtHostApp->SetValue(selectedData->GetHostApp());
144  chkHostInTerminal->SetValue(selectedData->GetRunHostAppInTerminal());
145  }
146  m_LastSelected = m_List->GetSelection();
147 } // end of UpdateSelected
148 
150 {
151  return m_pProject->GetBuildTarget(m_Selected);
152 }
153 
154 // events
155 
157 {
158  UpdateSelected();
159 } // end of OnListboxSelection
160 
162 {
163  EndModal(wxID_OK);
164 } // end of OnListboxDClick
165 
167 {
168  if(wxTextCtrl* obj = XRCCTRL(*this, "txtHostApp", wxTextCtrl))
169  {
170  wxFileDialog dlg(this,
171  _("Select host application"),
172  _T(""),
173  obj->GetValue(),
174 #ifdef __WXMSW__
175  _("Executable files (*.exe)|*.exe"),
176 #else
177  _("All files (*)|*"),
178 #endif
179  wxFD_OPEN | wxFD_FILE_MUST_EXIST | compatibility::wxHideReadonly);
180  dlg.SetFilterIndex(0);
181  PlaceWindow(&dlg);
182  if (dlg.ShowModal() != wxID_OK)
183  return;
184  wxFileName fname(dlg.GetPath());
185  if (fname.GetFullPath().StartsWith(m_pProject->GetCommonTopLevelPath()))
186  {
187  fname.MakeRelativeTo(m_pProject->GetCommonTopLevelPath());
188  obj->SetValue(fname.GetFullPath());
189  }
190  else
191  obj->SetValue(fname.GetFullPath());
192  }
193 } // end of OnHostApplicationButtonClick
194 
195 void SelectTargetDlg::EndModal(int retCode)
196 {
197  if (retCode == wxID_OK)
198  {
199  UpdateSelected();
200  m_Selected = m_List->GetSelection();
201  wxString defaultExecTarget;
202 
203  for (size_t i = 0; i < m_List->GetCount(); i++)
204  {
205  auto data = static_cast<TargetListData*>(m_List->GetClientObject(i));
206  ProjectBuildTarget *target = m_pProject->GetBuildTarget(i);
207 
208  target->SetExecutionParameters(data->GetExecParameters());
209  target->SetHostApplication(data->GetHostApp());
210  target->SetRunHostApplicationInTerminal(data->GetRunHostAppInTerminal());
211 
212  if (data->GetDefaultExecTarget())
213  defaultExecTarget = m_List->GetString(i);
214  }
215 
216  if (defaultExecTarget != m_pProject->GetDefaultExecuteTarget())
217  m_pProject->SetDefaultExecuteTarget(defaultExecTarget);
218  }
220 } // end of EndModal
void EndModal(int retCode)
Definition: sc_dialog.cpp:112
void EndModal(int retCode) override
virtual const wxString & GetExecutionParameters() const
Read the target&#39;s execution parameters.
void SetDefaultExecTarget(bool defaultTarget)
virtual const wxString & GetHostApplication() const
Read the target&#39;s host application.
virtual void SetFilterIndex(int filterIndex)
#define _T(string)
ProjectBuildTarget * GetSelectionTarget()
virtual void SetExecutionParameters(const wxString &params)
Set the target&#39;s execution parameters to params.
#define wxNOT_FOUND
virtual bool GetRunHostApplicationInTerminal() const
Get the flag if the host app should be run in terminal.
void SetRunHostAppInTerminal(bool runAppInTerminal)
bool MakeRelativeTo(const wxString &pathBase=wxEmptyString, wxPathFormat format=wxPATH_NATIVE)
virtual void SetHostApplication(const wxString &app)
Set the target&#39;s host application to app.
Represents a Code::Blocks project.
Definition: cbproject.h:96
void OnListboxDClick(wxCommandEvent &event)
virtual const wxString & GetTitle() const
Read the target&#39;s title.
wxString m_ExecParameters
size_t Replace(const wxString &strOld, const wxString &strNew, bool replaceAll=true)
void SetExecParameters(const wxString &ExecParameters)
const wxString & GetHostApp() const
TargetListData(const wxString &ExecParameters, const wxString &HostApp, bool RunHostAppInTerminal, bool DefaultExecTarget)
bool GetRunHostAppInTerminal() const
Store target UI data for every target in the list box control in the SelectTargetDlg.
void SetHostApp(const wxString &hostApp)
const wxString & _(const wxString &string)
#define cbAssert(expr)
Definition: cbexception.h:48
~SelectTargetDlg() override
void OnListboxSelection(wxCommandEvent &event)
DLLIMPORT void PlaceWindow(wxTopLevelWindow *w, cbPlaceDialogMode mode=pdlBest, bool enforce=false)
Definition: globals.cpp:1177
const wxString & GetExecParameters() const
Represents a Code::Blocks project build target.
static wxXmlResource * Get()
virtual bool GetValue() const
virtual void SetValue(bool state)
virtual void SetRunHostApplicationInTerminal(bool in_terminal)
bool GetDefaultExecTarget() const
wxObject * LoadObject(wxWindow *parent, const wxString &name, const wxString &classname)
void OnHostApplicationButtonClick(wxCommandEvent &event)