Code::Blocks  SVN r11506
editpathdlg.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: 11365 $
6  * $Id: editpathdlg.cpp 11365 2018-04-12 07:02:34Z fuscated $
7  * $HeadURL: https://svn.code.sf.net/p/codeblocks/code/trunk/src/sdk/editpathdlg.cpp $
8  */
9 
10 #include "sdk_precomp.h"
11 
12 #ifndef CB_PRECOMP
13  #include <wx/intl.h>
14  #include <wx/xrc/xmlres.h>
15  #include <wx/textctrl.h>
16  #include <wx/button.h>
17  #include <wx/filename.h>
18  #include <wx/stattext.h>
19  #include "globals.h"
20  #include "manager.h"
21  #include "macrosmanager.h"
22  #include "uservarmanager.h"
23 #endif
24 
25 #include "editpathdlg.h"
26 #include <wx/filedlg.h>
27 #include <wx/msgdlg.h>
28 
29 // remember last path, when supplied path is empty
31 
32 BEGIN_EVENT_TABLE(EditPathDlg, wxScrollingDialog)
33  EVT_UPDATE_UI(-1, EditPathDlg::OnUpdateUI)
34  EVT_BUTTON(XRCID("btnBrowse"), EditPathDlg::OnBrowse)
35  EVT_BUTTON(XRCID("btnOther"), EditPathDlg::OnOther)
36 END_EVENT_TABLE()
37 
39  const wxString& path,
40  const wxString& basepath,
41  const wxString& title,
42  const wxString& message,
43  const bool wantDir,
44  const bool allowMultiSel,
45  const wxString& filter)
46 {
47  //ctor
48  wxXmlResource::Get()->LoadObject(this, parent, _T("dlgEditPath"),_T("wxScrollingDialog"));
49  XRCCTRL(*this, "wxID_OK", wxButton)->SetDefault();
50 
51  XRCCTRL(*this, "txtPath", wxTextCtrl)->SetValue(path);
52  XRCCTRL(*this, "dlgEditPath", wxScrollingDialog)->SetTitle(title);
53 
54  if (!wantDir) {
55  XRCCTRL(*this, "lblText", wxStaticText)->SetLabel(_("File:"));
56  }
57 
58  m_Path = path;
59  m_WantDir = wantDir;
60  m_AllowMultiSel = allowMultiSel;
61  m_Message = message;
62  m_Basepath = basepath;
63  m_Filter = filter;
64  m_AskMakeRelative = true;
65  m_ShowCreateDirButton = false;
66  XRCCTRL(*this, "txtPath", wxTextCtrl)->SetFocus();
67 
68  // Limit vertical resizing.
69  SetMinSize(wxSize(400, GetMinHeight()));
70  SetMaxSize(wxSize(-1, GetMinHeight()));
71 }
72 
74 {
75  //dtor
76 }
77 
79 {
80  wxButton* btn = (wxButton*)FindWindow(wxID_OK);
81  if (btn)
82  btn->Enable(!XRCCTRL(*this, "txtPath", wxTextCtrl)->GetValue().IsEmpty());
83 }
84 
85 void EditPathDlg::OnBrowse(cb_unused wxCommandEvent& event)
86 {
87  wxFileName path;
88  wxArrayString multi;
89 
90  wxString val = XRCCTRL(*this, "txtPath", wxTextCtrl)->GetValue();
91  int idx = val.Find(DEFAULT_ARRAY_SEP);
92  if (idx != -1)
93  val.Remove(idx);
94  wxFileName fname(val);
95 
96  if (m_WantDir)
97  {
98  // try to "decode" custom var
99  wxString initial_val = val;
101  fname = val;
102  fname.MakeAbsolute(m_Basepath);
103  m_Path = fname.GetFullPath();
104 
107 
108  if (path.GetFullPath().IsEmpty())
109  return;
110 
111  // if it was a custom var, see if we can re-insert it
112  if (initial_val != val)
113  {
114  wxString tmp = path.GetFullPath();
115  if (tmp.Replace(val, initial_val) != 0)
116  {
117  // done here
118  XRCCTRL(*this, "txtPath", wxTextCtrl)->SetValue(tmp);
119  return;
120  }
121  }
122  }
123  else
124  {
125  wxFileDialog dlg(this, m_Message, (fname.GetPath().IsEmpty() ? s_LastPath : fname.GetPath()),
127 
128  PlaceWindow(&dlg);
129  if (dlg.ShowModal() == wxID_OK)
130  {
131  if (m_AllowMultiSel)
132  dlg.GetPaths(multi);
133  else
134  path = dlg.GetPath();
135  }
136  else
137  return;
138  }
139 
140  if (m_AllowMultiSel && multi.GetCount() != 0 && !multi[0].IsEmpty())
141  s_LastPath = multi[0];
142  else if (!path.GetFullPath().IsEmpty())
143  s_LastPath = path.GetFullPath();
144 
145  wxString result;
147  {
148  // ask the user if he wants it to be kept as relative
149  if (cbMessageBox(_("Keep this as a relative path?"),
150  _("Question"),
151  wxICON_QUESTION | wxYES_NO, this) == wxID_YES)
152  {
153  if (m_AllowMultiSel)
154  {
155  for (unsigned int i = 0; i < multi.GetCount(); ++i)
156  {
157  path = multi[i];
159  multi[i] = path.GetFullPath();
160  }
161  result = GetStringFromArray(multi);
162  }
163  else
164  {
166  result = path.GetFullPath();
167  }
168  }
169  else
170  {
171  if (m_AllowMultiSel)
172  result = GetStringFromArray(multi);
173  else
174  result = path.GetFullPath();
175  }
176  }
177  else // always absolute path
178  {
179  if (m_AllowMultiSel)
180  result = GetStringFromArray(multi);
181  else
182  result = path.GetFullPath();
183  }
184 
185  // finally set the path
186  XRCCTRL(*this, "txtPath", wxTextCtrl)->SetValue(result);
187 }
188 
189 void EditPathDlg::OnOther(cb_unused wxCommandEvent& event)
190 {
192  wxTextCtrl *txtPath = XRCCTRL(*this, "txtPath", wxTextCtrl);
193 
194  const wxString &user_var = userMgr->GetVariable(this, txtPath->GetValue());
195  if ( !user_var.IsEmpty() )
196  {
197  txtPath->SetValue(user_var);
198  m_WantDir = true;
199  }
200 }
201 
202 void EditPathDlg::EndModal(int retCode)
203 {
204  // update m_Path
205  m_Path = XRCCTRL(*this, "txtPath", wxTextCtrl)->GetValue();
207 }
void EndModal(int retCode)
Definition: sc_dialog.cpp:112
virtual int ShowModal()
#define wxICON_QUESTION
UserVariableManager * GetUserVariableManager() const
Definition: manager.cpp:464
static Manager * Get()
Use Manager::Get() to get a pointer to its instance Manager::Get() is guaranteed to never return an i...
Definition: manager.cpp:182
DLLIMPORT wxString ChooseDirectory(wxWindow *parent, const wxString &message=_("Select directory"), const wxString &initialPath=_T(""), const wxString &basePath=_T(""), bool askToMakeRelative=false, bool showCreateDirButton=false)
Definition: globals.cpp:639
void OnBrowse(wxCommandEvent &event)
Definition: editpathdlg.cpp:85
#define _T(string)
#define wxYES_NO
wxString m_Basepath
Definition: editpathdlg.h:37
void OnUpdateUI(wxUpdateUIEvent &event)
Definition: editpathdlg.cpp:78
DLLIMPORT wxString GetStringFromArray(const wxArrayString &array, const wxString &separator=DEFAULT_ARRAY_SEP, bool SeparatorAtEnd=true)
Definition: globals.cpp:122
wxString & Remove(size_t pos)
wxString m_Message
Definition: editpathdlg.h:36
bool m_WantDir
Definition: editpathdlg.h:39
bool MakeRelativeTo(const wxString &pathBase=wxEmptyString, wxPathFormat format=wxPATH_NATIVE)
bool m_AllowMultiSel
Definition: editpathdlg.h:40
static wxString s_LastPath
Definition: editpathdlg.cpp:30
wxString GetVariable(wxWindow *parent, const wxString &old)
size_t Replace(const wxString &strOld, const wxString &strNew, bool replaceAll=true)
bool IsEmpty() const
virtual wxString GetPath() const
MacrosManager * GetMacrosManager() const
Definition: manager.cpp:454
const wxString & _(const wxString &string)
void SetMinSize(wxPropertyGrid *grid)
DLLIMPORT const wxString DEFAULT_ARRAY_SEP
Definition: globals.cpp:56
bool IsEmpty() const
DLLIMPORT void PlaceWindow(wxTopLevelWindow *w, cbPlaceDialogMode mode=pdlBest, bool enforce=false)
Definition: globals.cpp:1177
wxString GetPath(int flags=wxPATH_GET_VOLUME, wxPathFormat format=wxPATH_NATIVE) const
void ReplaceEnvVars(wxString &buffer)
Definition: macrosmanager.h:32
wxString m_Path
Definition: editpathdlg.h:35
wxString GetFullName() const
virtual void GetPaths(wxArrayString &paths) const
bool m_AskMakeRelative
Definition: editpathdlg.h:41
void OnOther(wxCommandEvent &event)
void EndModal(int retCode) override
size_t GetCount() const
int Find(wxUniChar ch, bool fromEnd=false) const
static wxXmlResource * Get()
wxString m_Filter
Definition: editpathdlg.h:38
bool m_ShowCreateDirButton
Definition: editpathdlg.h:42
~EditPathDlg() override
Definition: editpathdlg.cpp:73
bool MakeAbsolute(const wxString &cwd=wxEmptyString, wxPathFormat format=wxPATH_NATIVE)
wxString GetFullPath(wxPathFormat format=wxPATH_NATIVE) const
wxObject * LoadObject(wxWindow *parent, const wxString &name, const wxString &classname)
DLLIMPORT int cbMessageBox(const wxString &message, const wxString &caption=wxEmptyString, int style=wxOK, wxWindow *parent=NULL, int x=-1, int y=-1)
wxMessageBox wrapper.
Definition: globals.cpp:1395