Code::Blocks  SVN r11506
autodetectcompilers.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: 10912 $
6  * $Id: autodetectcompilers.cpp 10912 2016-09-25 16:10:13Z fuscated $
7  * $HeadURL: https://svn.code.sf.net/p/codeblocks/code/trunk/src/sdk/autodetectcompilers.cpp $
8  */
9 
10 #include "sdk_precomp.h"
11 #include "autodetectcompilers.h"
12 
13 #ifndef CB_PRECOMP
14  #include <wx/button.h>
15  #include <wx/filename.h>
16  #include <wx/intl.h>
17  #include <wx/listctrl.h>
18  #include <wx/stattext.h>
19  #include <wx/string.h>
20  #include <wx/xrc/xmlres.h>
21 
22  #include "compiler.h"
23  #include "compilerfactory.h"
24  #include "configmanager.h"
25  #include "manager.h"
26  #include "macrosmanager.h"
27 #endif
28 #include <wx/tooltip.h>
29 
30 #include "infowindow.h"
31 
32 BEGIN_EVENT_TABLE(AutoDetectCompilers, wxScrollingDialog)
33  EVT_UPDATE_UI(-1, AutoDetectCompilers::OnUpdateUI)
34  EVT_BUTTON(XRCID("btnDefault"), AutoDetectCompilers::OnDefaultClick)
35 END_EVENT_TABLE()
36 
38 {
39  //ctor
40  wxXmlResource::Get()->LoadObject(this, parent, _T("dlgAutoDetectCompilers"),_T("wxScrollingDialog"));
41  XRCCTRL(*this, "wxID_OK", wxButton)->SetDefault();
42 
43  wxListCtrl* list = XRCCTRL(*this, "lcCompilers", wxListCtrl);
44  if (list)
45  {
46  list->Connect(wxEVT_MOTION, wxMouseEventHandler(AutoDetectCompilers::OnMouseMotion));
47  list->ClearAll();
48  list->InsertColumn(0, _("Compiler"), wxLIST_FORMAT_LEFT, 380);
49  list->InsertColumn(1, _("Status"), wxLIST_FORMAT_LEFT, 100);
50 
51  for (size_t i = 0; i < CompilerFactory::GetCompilersCount(); ++i)
52  {
54  if (!compiler)
55  continue;
56 
57  list->InsertItem(list->GetItemCount(), compiler->GetName());
58 
59  wxString path = compiler->GetMasterPath();
60  wxString path_no_macros = compiler->GetMasterPath();
61  Manager::Get()->GetMacrosManager()->ReplaceMacros(path_no_macros);
62 
63  int idx = list->GetItemCount() - 1;
64  int highlight = 0;
65  if (path.IsEmpty() && Manager::Get()->GetConfigManager(wxT("compiler"))->Exists(wxT("/sets/") + compiler->GetID() + wxT("/name")))
66  {
67  // Here, some user-interaction is required not to show this
68  // dialog again on each new start-up of C::B.
69  list->SetItem(idx, 1, _("Invalid"));
70  // So we better clearly HIGHLIGHT this entry:
71  highlight = 1;
72  }
73  else // The compiler is *probably* invalid, but at least a master-path is set
74  {
75  list->SetItem(idx, 1, _("Not found"));
76  highlight = -1;
77  }
78 
79  // Inspect deeper and probably try to auto-detect invalid compilers:
80  if (compiler->GetParentID().IsEmpty()) // built-in compiler
81  {
82  // Try auto-detection (which is for built-in compilers only)
83  bool detected = compiler->AutoDetectInstallationDir() == adrDetected;
84  wxString pathDetected( compiler->GetMasterPath() );
85 
86  // In case auto-detection was successful:
87  if (detected)
88  {
89  // No path setup before OR path detected as it was setup before
90  if (path.IsEmpty() || path == pathDetected || path_no_macros == pathDetected)
91  list->SetItem(idx, 1, _("Detected")); // OK
92  else
93  list->SetItem(idx, 1, _("User-defined")); // OK
94  highlight = 0;
95  }
96  // In case auto-detection failed but a path was setup before:
97  else if ( !path.IsEmpty() )
98  {
99  // Check, if the master path is valid:
100  if ( wxFileName::DirExists(path_no_macros) && !(path == pathDetected || path_no_macros == pathDetected) )
101  {
102  list->SetItem(idx, 1, _("User-defined")); // OK
103  highlight = 0;
104  }
105 
106  // Assume the user did the setup on purpose, so reset the old settings anyways:
107  compiler->SetMasterPath(path);
108  }
109  }
110  else // no built-in, but user-defined (i.e. copied) compiler
111  {
112  // Check, if the master path is valid:
113  if ( !path.IsEmpty() && wxFileName::DirExists(path_no_macros) )
114  {
115  list->SetItem(idx, 1, _("User-defined")); // OK
116  highlight = 0;
117  }
118  }
119 
120  if (highlight == 1)
121  list->SetItemBackgroundColour(idx, *wxRED);
122  else if (highlight == -1)
123  list->SetItemTextColour(idx, *wxLIGHT_GREY);
124  }
125  // Resize columns so one can read the whole stuff:
128  }
129 
130  XRCCTRL(*this, "lblDefCompiler", wxStaticText)->SetLabel(CompilerFactory::GetDefaultCompiler()->GetName());
131 }
132 
134 {
135  //dtor
136 }
137 
139 {
140  wxListCtrl* list = XRCCTRL(*this, "lcCompilers", wxListCtrl);
141  int idx = list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
142  if (idx != -1)
143  {
145  XRCCTRL(*this, "lblDefCompiler", wxStaticText)->SetLabel(CompilerFactory::GetDefaultCompiler()->GetName());
146  }
147 }
148 
150 {
151  wxListCtrl* list = XRCCTRL(*this, "lcCompilers", wxListCtrl);
152  int flags = 0;
153  int idx = list->HitTest(event.GetPosition(), flags);
154  wxString txt = wxEmptyString;
155  if (idx != wxNOT_FOUND)
156  {
157  wxListItem itm;
158  itm.m_itemId = idx;
159  itm.m_col = 1;
160  itm.m_mask = wxLIST_MASK_TEXT;
161  if (list->GetItem(itm))
162  txt = itm.m_text;
163  }
164  if (txt == wxT("Detected") || txt == wxT("User-defined"))
166  else
167  txt = wxEmptyString;
168  if (list->GetToolTip())
169  {
170  if (txt.IsEmpty())
171  list->UnsetToolTip();
172  else if (txt != list->GetToolTip()->GetTip())
173  list->SetToolTip(txt);
174  }
175  else if (!txt.IsEmpty())
176  list->SetToolTip(txt);
177 }
178 
180 {
181  wxListCtrl* list = XRCCTRL(*this, "lcCompilers", wxListCtrl);
182  bool en = list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED) != -1;
183  XRCCTRL(*this, "btnDefault", wxButton)->Enable(en);
184 
185  event.Skip();
186 }
virtual const wxString & GetMasterPath() const
Get the compiler&#39;s master path (must contain "bin", "include" and "lib")
Definition: compiler.h:295
ConfigManager * GetConfigManager(const wxString &name_space) const
Definition: manager.cpp:474
void SetItemBackgroundColour(long item, const wxColour &col)
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
long GetNextItem(long item, int geometry=wxLIST_NEXT_ALL, int state=wxLIST_STATE_DONTCARE) const
static Compiler * GetDefaultCompiler()
static Compiler * GetCompiler(size_t index)
#define wxLIST_STATE_SELECTED
#define _T(string)
#define wxT(string)
void OnDefaultClick(wxCommandEvent &event)
#define wxNOT_FOUND
wxPoint GetPosition() const
#define wxLIST_MASK_TEXT
const wxString & GetID() const
Get this compiler&#39;s unique ID.
Definition: compiler.h:351
long InsertItem(wxListItem &info)
wxColour * wxLIGHT_GREY
bool Exists(const wxString &name)
wxColour * wxRED
void SetItemTextColour(long item, const wxColour &col)
static void SetDefaultCompiler(size_t index)
int GetItemCount() const
wxString wxEmptyString
void OnUpdateUI(wxUpdateUIEvent &event)
MacrosManager * GetMacrosManager() const
Definition: manager.cpp:454
const wxString & _(const wxString &string)
void ReplaceMacros(wxString &buffer, ProjectBuildTarget *target=nullptr, bool subrequest=false)
Abstract base class for compilers.
Definition: compiler.h:274
bool IsEmpty() const
wxEventType wxEVT_MOTION
virtual const wxString & GetName() const
Get the compiler&#39;s name.
Definition: compiler.h:293
const wxString & GetParentID() const
Get this compiler&#39;s parent&#39;s unique ID.
Definition: compiler.h:353
long InsertColumn(long col, const wxListItem &info)
bool SetItem(wxListItem &info)
bool SetColumnWidth(int col, int width)
static size_t GetCompilersCount()
long HitTest(const wxPoint &point, int &flags, long *ptrSubItem=NULL) const
static wxXmlResource * Get()
void ClearAll()
bool DirExists() const
virtual AutoDetectResult AutoDetectInstallationDir()=0
Try to auto-detect the compiler&#39;s installation directory.
void OnMouseMotion(wxMouseEvent &event)
wxObject * LoadObject(wxWindow *parent, const wxString &name, const wxString &classname)
virtual void SetMasterPath(const wxString &path)
Set the compiler&#39;s master path (must contain "bin", "include" and "lib")
Definition: compiler.h:321
bool GetItem(wxListItem &info) const