Code::Blocks  SVN r11506
globals.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: 11467 $
6  * $Id: globals.cpp 11467 2018-09-16 13:07:44Z fuscated $
7  * $HeadURL: https://svn.code.sf.net/p/codeblocks/code/trunk/src/sdk/globals.cpp $
8  */
9 
10 #include "sdk_precomp.h"
11 
12 #ifndef CB_PRECOMP
13  #include <wx/choicdlg.h>
14  #include <wx/file.h>
15  #include <wx/filename.h>
16  #include <wx/filesys.h>
17  #include <wx/image.h>
18  #include <wx/imaglist.h>
19  #include <wx/listctrl.h>
20  #include <wx/menu.h>
21  #include <wx/textdlg.h>
22 
23  #include "wx/wxscintilla.h"
24 
25  #include "cbexception.h"
26  #include "configmanager.h" // ReadBool
27  #include "filemanager.h"
28  #include "globals.h"
29  #include "logmanager.h"
30  #include "manager.h"
31  #include "projectmanager.h"
32 #endif
33 
34 #include <tinyxml.h>
35 
36 #include <wx/dirdlg.h>
37 #include <wx/display.h>
38 #include <wx/filefn.h>
39 #include <wx/fontmap.h>
40 #include <wx/msgdlg.h>
41 #include <wx/tokenzr.h>
42 
43 #include <algorithm>
44 #include <string>
45 
46 #include "filefilters.h"
47 #include "tinywxuni.h"
48 #include "filegroupsandmasks.h"
49 
50 #ifndef __WXMSW__
51  #include <unistd.h> // readlink
52  #include <sys/stat.h> // lstat
53 #endif
54 
55 const wxString DEFAULT_WORKSPACE = _T("default.workspace");
56 const wxString DEFAULT_ARRAY_SEP = _T(";");
57 
58 #ifndef __WXMAC__
59 const wxString DEFAULT_CONSOLE_TERM = _T("xterm -T $TITLE -e");
60 #else
61 const wxString DEFAULT_CONSOLE_TERM = _T("osascript -e 'tell app \"Terminal\"' -e 'activate' -e 'do script quoted form of \"$SCRIPT\"' -e 'end tell'");
62 #endif
63 const wxString DEFAULT_CONSOLE_SHELL = _T("/bin/sh -c");
64 
65 #if defined __WXMSW__
66 const wxString cbDEFAULT_OPEN_FOLDER_CMD = _T("explorer.exe /select,");
67 #elif defined __WXMAC__
68 const wxString cbDEFAULT_OPEN_FOLDER_CMD = _T("open -R");
69 #else
70 const wxString cbDEFAULT_OPEN_FOLDER_CMD = _T("xdg-open");
71 #endif
72 
73 int GetPlatformsFromString(const wxString& platforms)
74 {
75  bool pW = platforms.Contains(_("Windows"));
76  bool pU = platforms.Contains(_("Unix"));
77  bool pM = platforms.Contains(_("Mac"));
78  bool pA = platforms.Contains(_("All"));
79 
80  if (pA || (pW && pU && pM))
81  return spAll;
82 
83  int p = 0;
84  if (pW) p |= spWindows;
85  if (pU) p |= spUnix;
86  if (pM) p |= spMac;
87  return p;
88 }
89 
90 wxString GetStringFromPlatforms(int platforms, bool forceSeparate)
91 {
92  wxString ret;
93 
94  if (!forceSeparate)
95  {
96  int tmpAll = spWindows | spUnix | spMac;
97  if (((platforms & tmpAll) == tmpAll) || ((platforms & spAll) == spAll))
98  return _("All");
99  }
100 
101  if (platforms & spWindows)
102  ret << _("Windows;");
103  if (platforms & spUnix)
104  ret << _("Unix;");
105  if (platforms & spMac)
106  ret << _("Mac;");
107  return ret;
108 }
109 
110 /*
111  Killerbot : 6 Oktober 2007
112  The method has been extended with a bool to specify if the seperator should be appended at the end.
113  Ultimate goal : every client gives as a value : false.
114  Why : well a seperator should separate, there's nothing to separate at the end (like nothing to separate
115  at the beginning, we don't put one there ...), but some client code is having problems when the separator is
116  not present at the end. So for compatibility issues we have as default value for the new argument true, so the
117  old code has the same behaviour as before [TODO: make those clients no longer dependent on this stupid final separator behaviour]
118  New code will specify false as the bool value, so hoping rather soon a search on this method will have all hits showing
119  false as the last argument ....
120 */
121 
122 wxString GetStringFromArray(const wxArrayString& array, const wxString& separator, bool SeparatorAtEnd)
123 {
124  wxString out;
125  for (unsigned int i = 0; i < array.GetCount(); ++i)
126  {
127  out << array[i];
128  if (i < array.GetCount() - 1 || SeparatorAtEnd)
129  out << separator;
130  }
131  return out;
132 }
133 
134 wxArrayString GetArrayFromString(const wxString& text, const wxString& separator, bool trimSpaces)
135 {
136  wxArrayString out;
137  wxString search = text;
138  int seplen = separator.Length();
139  while (true)
140  {
141  int idx = search.Find(separator);
142  if (idx == -1)
143  {
144  if (trimSpaces)
145  {
146  search.Trim(false);
147  search.Trim(true);
148  }
149  if (!search.IsEmpty())
150  out.Add(search);
151  break;
152  }
153  wxString part = search.Left(idx);
154  search.Remove(0, idx + seplen);
155  if (trimSpaces)
156  {
157  part.Trim(false);
158  part.Trim(true);
159  }
160  if (!part.IsEmpty())
161  out.Add(part);
162  }
163  return out;
164 }
165 
166 wxStringVec GetVectorFromString(const wxString& text, const wxString& separator, bool trimSpaces)
167 {
168  wxStringVec out;
169  wxString search = text;
170  int seplen = separator.Length();
171  while (true)
172  {
173  int idx = search.Find(separator);
174  if (idx == -1)
175  {
176  if (trimSpaces)
177  {
178  search.Trim(false);
179  search.Trim(true);
180  }
181  if (!search.IsEmpty())
182  out.push_back(search);
183  break;
184  }
185  wxString part = search.Left(idx);
186  search.Remove(0, idx + seplen);
187  if (trimSpaces)
188  {
189  part.Trim(false);
190  part.Trim(true);
191  }
192  if (!part.IsEmpty())
193  out.push_back(part);
194  }
195  return out;
196 }
197 
198 wxArrayString MakeUniqueArray(const wxArrayString& array, bool caseSens)
199 {
200  wxArrayString out;
201  for (size_t i = 0; i < array.GetCount(); ++i)
202  {
203  if (caseSens)
204  {
205  if (out.Index(array[i]) == wxNOT_FOUND)
206  out.Add(array[i]);
207  }
208  else
209  {
210  if (out.Index(array[i].Lower()) == wxNOT_FOUND)
211  out.Add(array[i].Lower()); // append only lower-case for the moment
212  }
213  }
214  return out;
215 }
216 
217 wxString MakeUniqueString(const wxString& text, const wxString& separator, bool caseSens)
218 {
219  return GetStringFromArray( MakeUniqueArray( GetArrayFromString(text, separator), caseSens ), separator, false );
220 }
221 
223 {
224  for (size_t i = 0; i < from.GetCount(); ++i)
225  to.Add(from[i]);
226 }
227 
228 wxString UnixFilename(const wxString& filename, wxPathFormat format)
229 {
230  wxString result = filename;
231 
232  if (format == wxPATH_NATIVE)
233  {
234  if (platform::windows)
235  format = wxPATH_WIN;
236  else
237  format = wxPATH_UNIX;
238  }
239 
240  // Unc-names always override platform specific settings otherwise they become corrupted
241  bool unc_name = result.StartsWith(_T("\\\\"));
242  if (format == wxPATH_WIN || unc_name) // wxPATH_WIN == wxPATH_DOS == wxPATH_OS2
243  {
244  result.Replace(wxT("/"), wxT("\\"));
245  while (result.Replace(wxT("\\\\"), wxT("\\")))
246  ; // loop for recursive removal of duplicate slashes
247  if (unc_name)
248  result.Prepend(wxT("\\"));
249  }
250  else
251  {
252  result.Replace(wxT("\\"), wxT("/"));
253  while (result.Replace(wxT("//"), wxT("/")))
254  ; // loop for recursive removal of duplicate slashes
255  }
256 
257  return result;
258 }
259 
261 {
262  if ( NeedQuotes(str) )
263  str = wxString(_T("\"")) + str + _T("\"");
264 }
265 
266 bool NeedQuotes(const wxString &str)
267 {
268  bool hasSpace = str.Find(_T(' ')) != -1;
269  bool hasParen = !platform::windows && (str.Find(_T('(')) != -1 || str.Find(_T(')')) != -1);
270  return !str.IsEmpty() && str.GetChar(0) != _T('"') && (hasSpace || hasParen);
271 }
272 
274 {
275  wxString ret = str;
276  if (!ret.IsEmpty() && ret[0] != _T('"') && ret[0] != _T('\''))
277  {
278  // TODO: make it faster
279  ret.Replace(_T(" "), _T("\\ "));
280  ret.Replace(_T("\t"), _T("\\\t"));
281  }
282  return ret;
283 }
284 
285 FileType FileTypeOf(const wxString& filename)
286 {
287  wxString ext = filename.AfterLast(_T('.')).Lower();
288 
289  if (ext.IsSameAs(FileFilters::ASM_EXT) ||
308  )
309  return ftSource;
310 
311  else if (ext.IsSameAs(FileFilters::TPP_EXT) ||
313  )
314  return ftTemplateSource;
315 
316  else if (ext.IsSameAs(FileFilters::H_EXT) ||
322  )
323  return ftHeader;
324 
326  return ftCodeBlocksProject;
327 
328  else if (ext.IsSameAs(FileFilters::WORKSPACE_EXT))
329  return ftCodeBlocksWorkspace;
330 
331  else if (ext.IsSameAs(FileFilters::DEVCPP_EXT))
332  return ftDevCppProject;
333 
334  else if (ext.IsSameAs(FileFilters::MSVC6_EXT))
335  return ftMSVC6Project;
336 
337  else if (ext.IsSameAs(FileFilters::MSVC7_EXT))
338  return ftMSVC7Project;
339 
340  else if (ext.IsSameAs(FileFilters::MSVC10_EXT))
341  return ftMSVC10Project;
342 
344  return ftMSVC6Workspace;
345 
347  return ftMSVC7Workspace;
348 
349  else if (ext.IsSameAs(FileFilters::XCODE1_EXT))
350  return ftXcode1Project; // Xcode 1.0+ (Mac OS X 10.3)
351 
352  else if (ext.IsSameAs(FileFilters::XCODE2_EXT))
353  return ftXcode2Project; // Xcode 2.1+ (Mac OS X 10.4)
354 
355  else if (ext.IsSameAs(FileFilters::OBJECT_EXT))
356  return ftObject;
357 
359  return ftXRCResource;
360 
361  else if (ext.IsSameAs(FileFilters::RESOURCE_EXT))
362  return ftResource;
363 
365  return ftResourceBin;
366 
367  else if (ext.IsSameAs(FileFilters::STATICLIB_EXT))
368  return ftStaticLib;
369 
371  return ftDynamicLib;
372 
373  else if (ext.IsSameAs(FileFilters::NATIVE_EXT))
374  return ftNative;
375 
377  return ftExecutable;
378 
379  else if (ext.IsSameAs(FileFilters::XML_EXT))
380  return ftXMLDocument;
381 
382  else if (ext.IsSameAs(FileFilters::SCRIPT_EXT))
383  return ftScript;
384 
385  // DrewBoo: Before giving up, see if the ProjectManager
386  // considers this extension a source or header
387  // TODO (Morten#5#): Do what DrewBoo said: Try removing the above code
388  // TODO (Morten#3#): This code should actually be a method of filegroups and masks or alike. So we collect all extension specific things in one place. As of now this would break ABI compatibilty with 08.02 so this should happen later.
389  else
390  {
391  // This code breaks ABI compatibility as noted by (Morten#3#) above.
392  // Code commented out by (pecan 2018/04/15). See http://forums.codeblocks.org/index.php/topic,22576.0.html
393  // The user can perform an equivalent objective by:
394  // 1) Fetching FilesGroupsAndMasks and adding the file extention(s) to file masks in the appropriate group.
395  // 2) Using the cbEVT_FILE_ADDED event to set the added file(s) properties (eg., compile and link).
396 
397  //ProjectManager *prjMgr = Manager::Get()->GetProjectManager();
398  //if ( prjMgr )
399  //{
400  // const FilesGroupsAndMasks* fgm = prjMgr->GetFilesGroupsAndMasks();
401  // // Since "ext" var has no "." prefixed, but FilesGropupsAndMasks uses
402  // // dot notation(".ext"), prefix a '.' here.
403  // wxString dotExt = _T(".") + ext;
404  // if (fgm)
405  // {
406  // for (unsigned int i = 0; i != fgm->GetGroupsCount(); ++i)
407  // {
408  // if (fgm->GetGroupName(i) == _T("Sources") && fgm->MatchesMask(dotExt, i))
409  // return ftSource;
410  // if (fgm->GetGroupName(i) == _T("Headers") && fgm->MatchesMask(dotExt, i))
411  // return ftHeader;
412  // }
413  // }
414  //}
415  }
416 
417  return ftOther;
418 }
419 
421 {
422  wxString pathValues;
423  wxGetEnv(_T("PATH"), &pathValues);
424  if (pathValues.empty())
425  return wxEmptyString;
426 
427  const wxString &sep = platform::windows ? _T(";") : _T(":");
429  const wxArrayString &pathArray = GetArrayFromString(pathValues, sep);
430  for (size_t i = 0; i < pathArray.GetCount(); ++i)
431  {
432  if (wxFileExists(pathArray[i] + pathSep + filename))
433  {
434  if (pathArray[i].AfterLast(pathSep).IsSameAs(_T("bin")))
435  return pathArray[i];
436  }
437  }
438  return wxEmptyString;
439 }
440 
441 void DoRememberSelectedNodes(wxTreeCtrl* tree, wxArrayString& selectedItemPaths)
442 {
443  wxArrayTreeItemIds items;
444 
445  if (tree->GetSelections(items) < 1 )
446  return;
447 
448  for (size_t i=0; i < items.GetCount(); ++i)
449  {
450  wxString path = wxEmptyString;
451  wxTreeItemId item = items[i];
452  while(item.IsOk())
453  {
454  path = _T("/") + tree->GetItemText(item) + path;
455  item = tree->GetItemParent(item);
456  }
457  if (path != wxEmptyString)
458  selectedItemPaths.Add(path);
459  }
460 }
461 
462 void DoSelectRememberedNode(wxTreeCtrl* tree, const wxTreeItemId& parent, wxString& selectedItemPath)
463 {
464  if (tree && !selectedItemPath.IsEmpty())
465  {
466  wxString tmpPath;
467  wxString folder;
468  tmpPath = selectedItemPath;
469  int pos = tmpPath.Find(_T('/'));
470  while (pos == 0)
471  {
472  tmpPath = tmpPath.Right(tmpPath.Length() - pos - 1);
473  pos = tmpPath.Find(_T('/'));
474  }
475 
476  folder = tmpPath.Left(pos);
477  tmpPath = tmpPath.Right(tmpPath.Length() - pos - 1);
478  wxTreeItemId item = parent;
479  wxTreeItemIdValue cookie = nullptr;
480 
481  while (item.IsOk())
482  {
483  if (tree->GetItemText(item) != folder)
484  item = tree->GetNextSibling(item);
485  else
486  {
487  if (pos < 0)
488  {
489  tree->SelectItem(item);
490  break;
491  }
492  else
493  {
494  item = tree->GetNextChild(item, cookie);
495  DoSelectRememberedNode(tree, item, tmpPath);
496  }
497  }
498  }
499 
500  }
501 }
502 
503 bool DoRememberExpandedNodes(wxTreeCtrl* tree, const wxTreeItemId& parent, wxArrayString& nodePaths, wxString& path)
504 {
505  // remember expanded tree nodes of this tree
506  if (!tree || !parent.IsOk())
507  return false;
508 
509  wxString originalPath = path;
510  bool found = false;
511 
512  wxTreeItemIdValue cookie = nullptr;
513 
514  wxTreeItemId child = tree->GetFirstChild(parent, cookie);
515  while (child.IsOk())
516  {
517  if (tree->ItemHasChildren(child) && tree->IsExpanded(child))
518  {
519  found = true;
520  path << _T("/") << tree->GetItemText(child);
521  DoRememberExpandedNodes(tree, child, nodePaths, path);
522  nodePaths.Add(path);
523  path = originalPath;
524  }
525  child = tree->GetNextChild(parent, cookie);
526  }
527  return found;
528 }
529 
530 void DoExpandRememberedNode(wxTreeCtrl* tree, const wxTreeItemId& parent, const wxString& path)
531 {
532  if (!path.IsEmpty())
533  {
534  //Manager::Get()->GetLogManager()->Log(mltDevDebug, path);
535  wxString tmpPath;
536  tmpPath = path;
537  wxString folder;
538  int pos = tmpPath.Find(_T('/'));
539  while (pos == 0)
540  {
541  tmpPath = tmpPath.Right(tmpPath.Length() - pos - 1);
542  pos = tmpPath.Find(_T('/'));
543  }
544 
545  if (pos < 0) // no '/'
546  {
547  folder = tmpPath;
548  tmpPath.Clear();
549  }
550  else
551  {
552  folder = tmpPath.Left(pos);
553  tmpPath = tmpPath.Right(tmpPath.Length() - pos - 1);
554  }
555 
556  //Manager::Get()->GetLogManager()->Log(mltDevDebug, "%s, %s", folder.c_str(), tmpPath.c_str());
557 
558  wxTreeItemIdValue cookie = nullptr;
559 
560  wxTreeItemId child = tree->GetFirstChild(parent, cookie);
561  while (child.IsOk())
562  {
563  wxString itemText = tree->GetItemText(child);
564  if (itemText.Matches(folder))
565  {
566  tree->Expand(child);
567  DoExpandRememberedNode(tree, child, tmpPath);
568  break;
569  }
570  child = tree->GetNextChild(parent, cookie);
571  }
572  }
573 }
574 
575 void SaveTreeState(wxTreeCtrl* tree, const wxTreeItemId& parent, wxArrayString& nodePaths, wxArrayString& selectedItemPaths)
576 {
577  nodePaths.Clear();
578  if (!parent.IsOk() || !tree || !tree->ItemHasChildren(parent) || !tree->IsExpanded(parent))
579  return;
580 
581  wxString tmp;
582  if (!DoRememberExpandedNodes(tree, parent, nodePaths, tmp))
583  nodePaths.Add(tmp); // just the tree root
584 
585  selectedItemPaths.Clear();
586  DoRememberSelectedNodes(tree, selectedItemPaths);
587 }
588 
589 void RestoreTreeState(wxTreeCtrl* tree, const wxTreeItemId& parent, wxArrayString& nodePaths, wxArrayString& selectedItemPaths)
590 {
591  if (!parent.IsOk() || !tree)
592  return;
593 
594  if (nodePaths.GetCount() == 0)
595  {
596  tree->Collapse(parent);
597  return;
598  }
599 
600  for (unsigned int i = 0; i < nodePaths.GetCount(); ++i)
601  DoExpandRememberedNode(tree, parent, nodePaths[i]);
602 
603  nodePaths.Clear();
604  for (unsigned int i = 0; i < selectedItemPaths.GetCount(); ++i)
605  DoSelectRememberedNode(tree, tree->GetRootItem(), selectedItemPaths[i]);
606 
607  selectedItemPaths.Clear();
608 }
609 
610 bool CreateDirRecursively(const wxString& full_path, int perms)
611 {
612  wxFileName tmp(full_path);
613  if (wxDirExists(tmp.GetPath())) // early out, even if full_path is a filename, but the path already exists
614  return true;
615 
616  wxArrayString dirs;
617  wxString currdir;
618  currdir = tmp.GetVolume() + tmp.GetVolumeSeparator() + wxFILE_SEP_PATH;
619  dirs = tmp.GetDirs();
620 
621  for (size_t i = 0; i < dirs.GetCount(); ++i)
622  {
623  currdir << dirs[i];
624  if (!wxDirExists(currdir) && !wxMkdir(currdir, perms))
625  return false;
626  currdir << wxFILE_SEP_PATH;
627  }
628  return true;
629 }
630 
631 bool CreateDir(const wxString& full_path, int perms)
632 {
633  if (!wxDirExists(full_path) && !wxMkdir(full_path, perms))
634  return false;
635 
636  return true;
637 }
638 
640  const wxString& message,
641  const wxString& initialPath,
642  const wxString& basePath,
643  bool askToMakeRelative, // relative to initialPath
644  bool showCreateDirButton) // where supported
645 {
646  wxDirDialog dlg(parent, message, _T(""),
647  (showCreateDirButton ? wxDD_NEW_DIR_BUTTON : 0) | wxRESIZE_BORDER);
648  dlg.SetPath(initialPath);
649  PlaceWindow(&dlg);
650  if (dlg.ShowModal() != wxID_OK)
651  return wxEmptyString;
652 
653  wxFileName path(dlg.GetPath());
654  if (askToMakeRelative && !basePath.IsEmpty())
655  {
656  // ask the user if he wants it to be kept as relative
657  if (cbMessageBox(_("Keep this as a relative path?"),
658  _("Question"),
660  {
661  path.MakeRelativeTo(basePath);
662  }
663  }
664  return path.GetFullPath();
665 }
666 
667 // Reads a wxString from a file. File must be open. File is closed automatically.
668 bool cbRead(wxFile& file, wxString& st, wxFontEncoding encoding)
669 {
670  st.Empty();
671  if (!file.IsOpened())
672  return false;
673 
674  int len = file.Length();
675  if (!len)
676  {
677  file.Close();
678  return true;
679  }
680 
681  char* buff = new char[len+1];
682  if (!buff) // remark by killerbot : this is useless, since when out of mem --> exception (this is not malloc you know)
683  {
684  file.Close();
685  return false;
686  }
687  file.Read((void*)buff, len);
688  file.Close();
689  buff[len]='\0';
690 
691  DetectEncodingAndConvert(buff, st, encoding);
692  delete [] buff;
693 
694  return true;
695 }
696 
698 {
699  wxString st;
700  cbRead(file, st, encoding);
701  return st;
702 }
703 
704 // Writes a wxString to a file. File must be open. File is closed automatically.
705 bool cbWrite(wxFile& file, const wxString& buff, wxFontEncoding encoding)
706 {
707  bool result = false;
708  if (file.IsOpened())
709  {
710  wxCSConv conv(encoding);
711  result = file.Write(buff,conv);
712  if (result)
713  file.Flush();
714  file.Close();
715  }
716  return result;
717 }
718 
719 // Writes a wxString to a file. Takes care of unicode and uses a temporary file
720 // to save first and then it copies it over the original.
721 bool cbSaveToFile(const wxString& filename, const wxString& contents, wxFontEncoding encoding, bool bom)
722 {
723  return Manager::Get()->GetFileManager()->Save(filename, contents, encoding, bom);
724 }
725 
726 // Save a TinyXML document correctly, even if the path contains unicode characters.
727 bool cbSaveTinyXMLDocument(TiXmlDocument* doc, const wxString& filename)
728 {
729  return TinyXML::SaveDocument(filename, doc);
730 }
731 
732 // Return @c str as a proper unicode-compatible string
733 wxString cbC2U(const char* str)
734 {
735 #if wxUSE_UNICODE
736  return wxString(str, wxConvUTF8);
737 #else
738  return wxString(str);
739 #endif
740 }
741 
742 // Return multibyte (C string) representation of the string
743 const wxWX2MBbuf cbU2C(const wxString& str)
744 {
745  if (platform::unicode)
746  return str.mb_str(wxConvUTF8);
747  else
748  return str.mb_str();
749 }
750 
751 // Try converting a C-string from different encodings until a possible match is found.
752 // This tries the following encoding converters (in the same order):
753 // utf8, system, default and iso8859-1 to iso8859-15
754 wxFontEncoding DetectEncodingAndConvert(const char* strIn, wxString& strOut, wxFontEncoding possibleEncoding)
755 {
756  wxFontEncoding encoding = possibleEncoding;
757  strOut.Clear();
758 
759  if (platform::unicode)
760  {
761  if (possibleEncoding != wxFONTENCODING_UTF16 &&
762  possibleEncoding != wxFONTENCODING_UTF16LE &&
763  possibleEncoding != wxFONTENCODING_UTF16BE &&
764  possibleEncoding != wxFONTENCODING_UTF32 &&
765  possibleEncoding != wxFONTENCODING_UTF32LE &&
766  possibleEncoding != wxFONTENCODING_UTF32BE)
767  {
768  // crashes deep in the runtime (windows, at least)
769  // if one of the above encodings, hence the guard
770  wxCSConv conv(possibleEncoding);
771  strOut = wxString(strIn, conv);
772 
773  if (strOut.Length() == 0)
774  {
775  // oops! wrong encoding...
776 
777  // try utf8 first, if that was not what was asked for
778  if (possibleEncoding != wxFONTENCODING_UTF8)
779  {
780  encoding = wxFONTENCODING_UTF8;
781  strOut = wxString(strIn, wxConvUTF8);
782  }
783 
784  // check again: if still not right, try system encoding, default encoding and then iso8859-1 to iso8859-15
785  if (strOut.Length() == 0)
786  {
787  for (int i = wxFONTENCODING_SYSTEM; i < wxFONTENCODING_ISO8859_MAX; ++i)
788  {
789  encoding = (wxFontEncoding)i;
790  if (encoding == possibleEncoding)
791  continue; // skip if same as what was asked
792  wxCSConv csconv(encoding);
793  strOut = wxString(strIn, csconv);
794  if (strOut.Length() != 0)
795  break; // got it!
796  }
797  }
798  }
799  }
800  else
801  {
802  strOut = (const wxChar*) strIn;
803  }
804  }
805  else
806  {
807  strOut = (const wxChar*) strIn;
808  }
809  return encoding;
810 }
811 
812 wxString GetEOLStr(int eolMode)
813 {
814  if (eolMode == -1)
815  {
816  static const int defEOL = platform::windows ? wxSCI_EOL_CRLF : wxSCI_EOL_LF;
817  eolMode = Manager::Get()->GetConfigManager(wxT("editor"))->ReadInt(wxT("/eol/eolmode"), defEOL);
818  if (eolMode == 3) // auto-detect EOL
819  eolMode = defEOL;
820  }
821  switch (eolMode)
822  {
823  case wxSCI_EOL_CR:
824  return wxT("\r");
825  case wxSCI_EOL_LF:
826  return wxT("\n");
827  default: // wxSCI_EOL_CRLF
828  return wxT("\r\n");
829  }
830 }
831 
832 wxString URLEncode(const wxString &str) // not sure this is 100% standards compliant, but I hope so
833 {
834  wxString ret;
835  wxString t;
836  for (unsigned int i = 0; i < str.length(); ++i)
837  {
838  wxChar c = str[i];
839  if ( (c >= _T('A') && c <= _T('Z'))
840  || (c >= _T('a') && c <= _T('z'))
841  || (c >= _T('0') && c <= _T('9'))
842  || (c == _T('.'))
843  || (c == _T('-'))
844  || (c == _T('_')) )
845 
846  ret.Append(c);
847  else if (c == _T(' '))
848  ret.Append(_T('+'));
849  else
850  {
851  t.sprintf(_T("%%%02X"), (unsigned int) c);
852  ret.Append(t);
853  }
854  }
855  return ret;
856 }
857 
859 typedef std::map<wxString, wxString> BackticksMap;
860 BackticksMap m_Backticks; // all calls share the same cache
861 wxString ExpandBackticks(wxString& str) // backticks are written in-place to str
862 {
863  wxString ret;
864 
865  // this function is not windows-only anymore because we parse the backticked command's output
866  // for compiler/linker search dirs
867 
868  size_t start = str.find(_T('`'));
869  if (start == wxString::npos)
870  return ret; // no backticks here
871  size_t end = str.find(_T('`'), start + 1);
872  if (end == wxString::npos)
873  return ret; // no ending backtick; error?
874 
875  while (start != wxString::npos && end != wxString::npos)
876  {
877  wxString cmd = str.substr(start + 1, end - start - 1);
878  cmd.Trim(true);
879  cmd.Trim(false);
880  if (cmd.IsEmpty())
881  break;
882 
883  wxString bt;
884  BackticksMap::iterator it = m_Backticks.find(cmd);
885  if (it != m_Backticks.end()) // in the cache :)
886  bt = it->second;
887  else
888  {
889  Manager::Get()->GetLogManager()->DebugLog(F(_T("Caching result of `%s`"), cmd.wx_str()));
890  wxArrayString output;
892  wxExecute(_T("cmd /c ") + cmd, output, wxEXEC_NODISABLE);
893  else
894  wxExecute(cmd, output, wxEXEC_NODISABLE);
895  bt = GetStringFromArray(output, _T(" "), false);
896  // add it in the cache
897  m_Backticks[cmd] = bt;
898  Manager::Get()->GetLogManager()->DebugLog(_T("Cached"));
899  }
900  ret << bt << _T(' ');
901  str = str.substr(0, start) + bt + str.substr(end + 1, wxString::npos);
902 
903  // find next occurrence
904  start = str.find(_T('`'));
905  end = str.find(_T('`'), start + 1);
906  }
907 
908  return ret; // return a list of the replaced expressions
909 }
910 
911 wxMenu* CopyMenu(wxMenu* mnu, bool with_accelerators)
912 {
913  if (!mnu || mnu->GetMenuItemCount() < 1)
914  return nullptr;
915  wxMenu* theMenu = new wxMenu();
916 
917  for (size_t i = 0; i < mnu->GetMenuItemCount();++i)
918  {
919  wxMenuItem* tmpItem = mnu->FindItemByPosition(i);
920  wxMenuItem* theItem = new wxMenuItem(nullptr,
921  tmpItem->GetId(),
922  with_accelerators?tmpItem->GetItemLabel():tmpItem->GetItemLabelText(),
923  tmpItem->GetHelp(),
924  tmpItem->GetKind(),
925  CopyMenu(tmpItem->GetSubMenu()));
926  theMenu->Append(theItem);
927  }
928  return theMenu;
929 }
930 
932 {
933  while (win && win->IsShown())
934  {
935  win = win->GetParent();
936  if (!win)
937  return true;
938  }
939  return false;
940 }
941 
942 bool NormalizePath(wxFileName& f,const wxString& base)
943 {
944  bool result = true;
945 // if (!f.IsAbsolute())
946  {
948  result = f.IsOk();
949  }
950  return result;
951 }
952 
953 // Checks whether 'suffix' could be a suffix of 'path' and therefore represents
954 // the same path. This is used to check whether a relative path could represent
955 // the same path as absolute path. For instance, for
956 // suffix = sdk/globals.cpp
957 // path = /home/user/codeblocks/trunk/src/sdk/globals.cpp
958 // it returns true. The function expects that 'path' is normalized and compares
959 // 'path' with 'suffix' starting from the end of the path. When it reaches .. in
960 // 'suffix' it gives up (there is no way to check relative filename names
961 // exactly) and if the path compared so far is identical, it returns true
962 bool IsSuffixOfPath(wxFileName const & suffix, wxFileName const & path)
963 {
964  if (path.GetFullName() != suffix.GetFullName())
965  {
966  return false;
967  }
968 
969  wxArrayString suffixDirArray = suffix.GetDirs();
970  wxArrayString pathDirArray = path.GetDirs();
971 
972  int j = pathDirArray.GetCount() - 1;
973  for (int i = suffixDirArray.GetCount() - 1; i >= 0; i--)
974  {
975  // skip paths like /./././ and ////
976  if (suffixDirArray[i] == _T(".") || suffixDirArray[i] == _T(""))
977  continue;
978 
979  // suffix has more directories than path - cannot represent the same path
980  if (j < 0)
981  return false;
982 
983  // suffix contains ".." - from now on we cannot precisely determine
984  // whether suffix and path match - we assume that they do
985  if (suffixDirArray[i] == _T(".."))
986  return true;
987  // the corresponding directories of the two paths differ
988  else if (suffixDirArray[i] != pathDirArray[j])
989  return false;
990 
991  j--;
992  }
993 
994  if (suffix.IsAbsolute() && (j >= 0 || suffix.GetVolume() != path.GetVolume()))
995  return false;
996 
997  // 'suffix' is a suffix of 'path'
998  return true;
999 }
1000 
1002 {
1003 #ifdef _WIN32
1004  return false;
1005 #else
1006  if (dirpath.empty())
1007  return false;
1008  if (dirpath.Last() == wxFILE_SEP_PATH)
1009  dirpath.RemoveLast();
1010 
1011  struct stat fileStats;
1012  if (lstat(dirpath.mb_str(wxConvUTF8), &fileStats) != 0)
1013  return false;
1014 
1015  // If the path is a symbolic link, then try to resolve it.
1016  // This is needed to prevent infinite loops, when a folder is pointing to itself or its parent folder.
1017  if (S_ISLNK(fileStats.st_mode))
1018  {
1019  char buffer[4096];
1020  int result = readlink(dirpath.mb_str(wxConvUTF8), buffer, WXSIZEOF(buffer) - 1);
1021  if (result != -1)
1022  {
1023  buffer[result] = '\0'; // readlink() doesn't NUL-terminate the buffer
1024  wxString pathStr(buffer, wxConvUTF8);
1025  wxFileName fileName = wxFileName::DirName(pathStr);
1026 
1027  // If this is a relative symbolic link, we need to make it absolute.
1028  if (!fileName.IsAbsolute())
1029  {
1030  wxFileName dirNamePath;
1031  if (dirpath.Last() == wxFILE_SEP_PATH)
1032  dirNamePath = wxFileName::DirName(dirpath);
1033  else
1034  dirNamePath = wxFileName::DirName(dirpath + wxFILE_SEP_PATH);
1035  dirNamePath.RemoveLastDir();
1036  // Make the new filename absolute relative to the parent folder.
1037  fileName.MakeAbsolute(dirNamePath.GetFullPath());
1038  }
1039 
1040  wxString fullPath = fileName.GetFullPath();
1041  if (!fullPath.empty() && fullPath.Last() == wxT('.')) // this case should be handled because of a bug in wxWidgets
1042  fullPath.RemoveLast();
1043  if (fullPath.length() > 1 && fullPath.Last() == wxFILE_SEP_PATH)
1044  fullPath.RemoveLast();
1045  dirpath = fullPath;
1046  return true;
1047  }
1048  }
1049 
1050  return false;
1051 #endif // _WIN32
1052 }
1053 
1055 {
1056  while (cbResolveSymLinkedDirPath(dirpath))
1057  ;
1058  return dirpath;
1059 }
1060 
1061 // function to check the common controls version
1062 #ifdef __WXMSW__
1063 #include <windows.h>
1064 #include <shlwapi.h>
1066 {
1067  bool result = false;
1068  HINSTANCE hinstDll;
1069  hinstDll = LoadLibrary(_T("comctl32.dll"));
1070  if (hinstDll)
1071  {
1072  DLLGETVERSIONPROC pDllGetVersion;
1073  pDllGetVersion = (DLLGETVERSIONPROC)GetProcAddress(hinstDll, "DllGetVersion");
1074 
1075  if (pDllGetVersion)
1076  {
1077  DLLVERSIONINFO dvi;
1078  HRESULT hr;
1079 
1080  ZeroMemory(&dvi, sizeof(dvi));
1081  dvi.cbSize = sizeof(dvi);
1082 
1083  hr = (*pDllGetVersion)(&dvi);
1084 
1085  if (SUCCEEDED(hr))
1086  result = dvi.dwMajorVersion == 6;
1087  }
1088 
1089  FreeLibrary(hinstDll);
1090  }
1091  return result;
1092 }
1093 #else
1094 bool UsesCommonControls6()
1095 {
1096  // for non-windows platforms, return true
1097  // as this is only used for knowing if bitmaps support transparency or not
1098  return true;
1099 }
1100 #endif
1101 
1102 wxBitmap cbLoadBitmap(const wxString& filename, wxBitmapType bitmapType)
1103 {
1104  // cache this, can't change while we 're running :)
1105  static bool oldCommonControls = !UsesCommonControls6();
1106 
1107  wxImage im;
1108  wxFileSystem* fs = new wxFileSystem;
1109  wxFSFile* f = fs->OpenFile(filename);
1110  if (f)
1111  {
1112  wxInputStream* is = f->GetStream();
1113  im.LoadFile(*is, bitmapType);
1114  delete f;
1115  }
1116  delete fs;
1117  if (oldCommonControls && im.HasAlpha())
1118  im.ConvertAlphaToMask();
1119 
1120  return wxBitmap(im);
1121 }
1122 
1123 // this doesn't work under wxGTK, and is only needed on wxMSW, we work around it on wxGTK
1124 #ifdef __WXMSW__
1126 {
1127  long flags = lc->GetWindowStyleFlag();
1128  switch (style)
1129  {
1130 #if wxCHECK_VERSION(3, 0, 0)
1131  case sisNoIcons: flags = (flags & ~wxLC_MASK_TYPE) | wxLC_LIST; break;
1132 #else
1133  case sisNoIcons: flags = (flags & ~wxLC_MASK_TYPE) | wxLC_SMALL_ICON; break;
1134 #endif
1135  default: flags = (flags & ~wxLC_MASK_TYPE) | wxLC_ICON; break;
1136  }
1137  lc->SetWindowStyleFlag(flags);
1138 }
1139 #else
1140 void SetSettingsIconsStyle(cb_unused wxListCtrl* lc, cb_unused SettingsIconsStyle style) {}
1141 #endif
1142 
1144 {
1145  return GetSettingsIconsStyle();
1146 }
1147 
1149 {
1150  return SettingsIconsStyle(Manager::Get()->GetConfigManager(_T("app"))->ReadInt(_T("/environment/settings_size"), 0));
1151 }
1152 
1154 {
1155  wxRect monitorRect;
1156  if (wxDisplay::GetCount() > 0)
1157  {
1158  int displayIdx = wxDisplay::GetFromWindow(window);
1159  if (displayIdx == wxNOT_FOUND)
1160  displayIdx = 0;
1161  wxDisplay display(displayIdx);
1162  monitorRect = display.GetClientArea();
1163  // This is needed because on Linux the client area returned for the first monitor in a twin
1164  // monitor setup with nVidia card is spanning the two monitors.
1165  // The intersection function will return just the client for the specified monitor.
1166  monitorRect = display.GetGeometry().Intersect(monitorRect);
1167  }
1168  else
1169  {
1170  int width, height;
1171  wxDisplaySize(&width, &height);
1172  monitorRect = wxRect(0, 0, width, height);
1173  }
1174  return monitorRect;
1175 }
1176 
1177 void PlaceWindow(wxTopLevelWindow *w, cbPlaceDialogMode mode, bool enforce)
1178 {
1179  if (!w)
1180  cbThrow(_T("Passed NULL pointer to PlaceWindow."));
1181 
1182  ConfigManager *cfg = Manager::Get()->GetConfigManager(_T("app"));
1183  if (!enforce && cfg->ReadBool(_T("/dialog_placement/do_place")) == false)
1184  return;
1185 
1186  wxWindow* referenceWindow = Manager::Get()->GetAppWindow();
1187  if (!referenceWindow) // no application window available, so this is as good as we can get
1188  referenceWindow = w;
1189 
1190  int the_mode;
1191  if (mode == pdlBest)
1192  the_mode = cfg->ReadInt(_T("/dialog_placement/dialog_position"), (int) pdlCentre);
1193  else
1194  the_mode = (int) mode;
1195 
1196  const wxRect monitorRect = cbGetMonitorRectForWindow(referenceWindow);
1197  wxRect windowRect = w->GetRect();
1198 
1199  switch(the_mode)
1200  {
1201  case pdlCentre:
1202  {
1203  windowRect.x = monitorRect.x + (monitorRect.width - windowRect.width)/2;
1204  windowRect.y = monitorRect.y + (monitorRect.height - windowRect.height)/2;
1205  }
1206  break;
1207 
1208 
1209  case pdlHead:
1210  {
1211  windowRect.x = monitorRect.x + (monitorRect.width - windowRect.width)/2;
1212  windowRect.y = monitorRect.y + (monitorRect.height - windowRect.height)/3;
1213  }
1214  break;
1215 
1216 
1217  case pdlConstrain:
1218  {
1219  int x1 = windowRect.x;
1220  int x2 = windowRect.x + windowRect.width;
1221  int y1 = windowRect.y;
1222  int y2 = windowRect.y + windowRect.height;
1223 
1224  if (windowRect.width > monitorRect.width) // cannot place without clipping, so centre it
1225  {
1226  x1 = monitorRect.x + (monitorRect.width - windowRect.width)/2;
1227  x2 = x1 + windowRect.width;
1228  }
1229  else
1230  {
1231  x2 = std::min(monitorRect.GetRight(), windowRect.GetRight());
1232  x1 = std::max(x2 - windowRect.width, monitorRect.x);
1233  x2 = x1 + windowRect.width;
1234  }
1235  if (windowRect.height > monitorRect.height) // cannot place without clipping, so centre it
1236  {
1237  y1 = monitorRect.y + (monitorRect.height - windowRect.height)/2;
1238  y2 = y1 + windowRect.height;
1239  }
1240  else
1241  {
1242  y2 = std::min(monitorRect.GetBottom(), windowRect.GetBottom());
1243  y1 = std::max(y2 - windowRect.height, monitorRect.y);
1244  y2 = y1 + windowRect.height;
1245  }
1246  windowRect = wxRect(x1, y1, x2-x1, y2-y1);
1247  }
1248  break;
1249 
1250 
1251  case pdlClip:
1252  {
1253  int x1 = windowRect.x;
1254  int x2 = windowRect.x + windowRect.width;
1255  int y1 = windowRect.y;
1256  int y2 = windowRect.y + windowRect.height;
1257 
1258  x1 = std::max(x1, monitorRect.x);
1259  x2 = std::min(x2, monitorRect.GetRight());
1260  y1 = std::max(y1, monitorRect.y);
1261  y2 = std::min(y2, monitorRect.GetBottom());
1262 
1263  windowRect = wxRect(x1, y1, x2-x1, y2-y1);
1264  }
1265  break;
1266  }
1267 
1268  w->SetSize(windowRect.x, windowRect.y, windowRect.width, windowRect.height, wxSIZE_ALLOW_MINUS_ONE);
1269 }
1270 
1272 {
1273  wxString actualDir = dir;
1274  // append ending path separator if needed
1275  if (actualDir.Last() != _T('/') && actualDir.Last() != _T('\\'))
1276  actualDir << wxFILE_SEP_PATH;
1277 
1278  if (!wxDirExists(actualDir))
1279  return dacInvalidDir;
1280 
1281  wxString testFile = wxFileName::CreateTempFileName(actualDir);
1282  if (!testFile.IsEmpty())
1283  {
1284  // ok, write-access confirmed
1285  // now remove the temporary file and return success
1286  wxRemoveFile(testFile);
1287  return dacReadWrite;
1288  }
1289 
1290  // if we reached here, the directory is not writable
1291  return dacReadOnly;
1292 }
1293 
1294 
1295 namespace platform
1296 {
1298  {
1299  if (!platform::windows)
1300  {
1301  return winver_NotWindows;
1302  }
1303  else
1304  {
1305 
1306  int famWin95 = wxOS_WINDOWS_9X;
1307  int famWinNT = wxOS_WINDOWS_NT;
1308 
1309  int Major = 0;
1310  int Minor = 0;
1311  int family = wxGetOsVersion(&Major, &Minor);
1312 
1313  if (family == famWin95)
1314  return winver_Windows9598ME;
1315 
1316  if (family == famWinNT)
1317  {
1318  if (Major == 5 && Minor == 0)
1319  return winver_WindowsNT2000;
1320 
1321  if (Major == 5 && Minor == 1)
1322  return winver_WindowsXP;
1323 
1324  if (Major == 5 && Minor == 2)
1325  return winver_WindowsServer2003;
1326 
1327  if (Major == 6 && Minor == 0)
1328  return winver_WindowsVista;
1329 
1330  if (Major == 6 && Minor == 1)
1331  return winver_Windows7;
1332  }
1333 
1334  return winver_UnknownWindows;
1335  }
1336  }
1337 
1339  {
1340  static const windows_version_t theOS = cb_get_os();
1341  return theOS;
1342  }
1343 }
1344 
1345 // returns the real path of a file by resolving symlinks
1346 // not yet optimal but should do for now
1347 // one thing that's not checked yet are circular symlinks - watch out!
1349 {
1350 #ifdef __WXMSW__
1351  // no symlinks support on windows
1352  return path;
1353 #else
1354  char buf[2048] = {};
1355  struct stat buffer;
1356  std::string ret = (const char*)cbU2C(path);
1357  size_t lastPos = 0;
1358  size_t slashPos = ret.find('/', lastPos);
1359  while (slashPos != std::string::npos)
1360  {
1361  if (lstat(ret.substr(0, slashPos).c_str(), &buffer) == 0)
1362  {
1363  if (S_ISLNK(buffer.st_mode))
1364  {
1365  int s = readlink(ret.substr(0, slashPos).c_str(), buf, sizeof(buf));
1366  buf[s] = 0;
1367  if (s > 0 && buf[0] != '/' && buf[0] != '~')
1368  {
1369  // relative
1370  ret = ret.substr(0, lastPos) + buf + ret.substr(slashPos, ret.size() - slashPos);
1371  }
1372  else
1373  {
1374  // absolute
1375  ret = buf + ret.substr(slashPos, ret.size() - slashPos);
1376 
1377  // start again at the beginning in case the path returned also
1378  // has symlinks. For example if using /etc/alternatives this will
1379  // be the case
1380  s = 0;
1381  }
1382  slashPos = s;
1383  }
1384  }
1385 
1386  while (ret[++slashPos] == '/')
1387  ;
1388  lastPos = slashPos;
1389  slashPos = ret.find('/', slashPos);
1390  }
1391  return cbC2U(ret.c_str());
1392 #endif
1393 }
1394 
1395 int cbMessageBox(const wxString& message, const wxString& caption, int style, wxWindow *parent, int x, int y)
1396 {
1397  if (!parent)
1398  parent = Manager::Get()->GetAppWindow();
1399 
1400  // Cannot create a wxMessageDialog with a NULL as parent
1401  if (!parent)
1402  {
1403  // wxMessage*Box* returns any of: wxYES, wxNO, wxCANCEL, wxOK.
1404  int answer = wxMessageBox(message, caption, style, parent, x, y);
1405  switch (answer)
1406  {
1407  // map answer to the one of wxMessage*Dialog* to ensure compatibility
1408  case (wxOK):
1409  return wxID_OK;
1410  case (wxCANCEL):
1411  return wxID_CANCEL;
1412  case (wxYES):
1413  return wxID_YES;
1414  case (wxNO):
1415  return wxID_NO;
1416  default:
1417  return -1; // NOTE: Cannot happen unless wxWidgets API changes
1418  }
1419  }
1420 
1421  wxMessageDialog dlg(parent, message, caption, style, wxPoint(x,y));
1422  PlaceWindow(&dlg);
1423  // wxMessage*Dialog* returns any of wxID_OK, wxID_CANCEL, wxID_YES, wxID_NO
1424  return dlg.ShowModal();
1425 }
1426 
1427 DLLIMPORT int cbGetSingleChoiceIndex(const wxString& message, const wxString& caption,
1428  const wxArrayString& choices, wxWindow *parent,
1429  const wxSize &size, int initialSelection)
1430 {
1431  if (!parent)
1432  parent = Manager::Get()->GetAppWindow();
1433 
1434  wxSingleChoiceDialog dialog(parent, message, caption, choices);
1435  dialog.SetSelection(initialSelection);
1436  dialog.SetSize(size);
1437  PlaceWindow(&dialog);
1438  return (dialog.ShowModal() == wxID_OK ? dialog.GetSelection() : -1);
1439 }
1440 
1442  const wxArrayString& choices, wxWindow *parent,
1443  const wxSize& size, const wxArrayInt& initialSelection)
1444 {
1445  if (!parent)
1446  parent = Manager::Get()->GetAppWindow();
1447 
1448  wxMultiChoiceDialog dialog(parent, message, caption, choices);
1449  dialog.SetSelections(initialSelection);
1450  dialog.SetSize(size);
1451  PlaceWindow(&dialog);
1452 
1453  if (dialog.ShowModal() == wxID_OK)
1454  return dialog.GetSelections();
1455  else
1456  return wxArrayInt();
1457 }
1458 
1459 #if wxCHECK_VERSION(3, 0, 0)
1461 #else
1463 #endif // wxCHECK_VERSION
1464 
1465 wxString cbGetTextFromUser(const wxString& message, const wxString& caption, const wxString& defaultValue,
1466  wxWindow *parent, wxCoord x, wxCoord y, bool centre)
1467 {
1468  if (!parent)
1469  parent = Manager::Get()->GetAppWindow();
1470 
1471  long style = wxTextEntryDialogStyle;
1472  if (centre)
1473  style |= wxCENTRE;
1474  else
1475  style &= ~wxCENTRE;
1476 
1477  wxTextEntryDialog dialog(parent, message, caption, defaultValue, style, wxPoint(x, y));
1478  PlaceWindow(&dialog);
1479  wxString str;
1480  if (dialog.ShowModal() == wxID_OK)
1481  str = dialog.GetValue();
1482  return str;
1483 }
1484 
1485 
1487 {
1488  static const wxString imgs[] =
1489  {
1490  // NOTE: Keep in sync with FileVisualState in globals.h!
1491 
1492  // The following are related to (editable, source-) file states
1493  _T("file.png"), // fvsNormal
1494  _T("file-missing.png"), // fvsMissing,
1495  _T("file-modified.png"), // fvsModified,
1496  _T("file-readonly.png"), // fvsReadOnly,
1497 
1498  // The following are related to version control systems (vc)
1499  _T("rc-file-added.png"), // fvsVcAdded,
1500  _T("rc-file-conflict.png"), // fvsVcConflict,
1501  _T("rc-file-missing.png"), // fvsVcMissing,
1502  _T("rc-file-modified.png"), // fvsVcModified,
1503  _T("rc-file-outofdate.png"), // fvsVcOutOfDate,
1504  _T("rc-file-uptodate.png"), // fvsVcUpToDate,
1505  _T("rc-file-requireslock.png"), // fvsVcRequiresLock,
1506  _T("rc-file-external.png"), // fvsVcExternal,
1507  _T("rc-file-gotlock.png"), // fvsVcGotLock,
1508  _T("rc-file-lockstolen.png"), // fvsVcLockStolen,
1509  _T("rc-file-mismatch.png"), // fvsVcMismatch,
1510  _T("rc-file-noncontrolled.png"), // fvsVcNonControlled,
1511 
1512  // The following are related to C::B workspace/project/folder/virtual
1513  _T("workspace.png"), // fvsWorkspace, WorkspaceIconIndex()
1514  _T("workspace-readonly.png"), // fvsWorkspaceReadOnly, WorkspaceIconIndex(true)
1515  _T("project.png"), // fvsProject, ProjectIconIndex()
1516  _T("project-readonly.png"), // fvsProjectReadOnly, ProjectIconIndex(true)
1517  _T("folder_open.png"), // fvsFolder, FolderIconIndex()
1518  _T("vfolder_open.png"), // fvsVirtualFolder, VirtualFolderIconIndex()
1519 
1521  };
1522  wxBitmap bmp;
1523  wxImageList *images = new wxImageList(16, 16);
1524  wxString prefix = ConfigManager::ReadDataPath() + _T("/images/");
1525 
1526  for (int i = 0; !imgs[i].IsEmpty(); ++i)
1527  {
1528  bmp = cbLoadBitmap(prefix + imgs[i], wxBITMAP_TYPE_PNG); // workspace
1529  images->Add(bmp);
1530  }
1531  return images;
1532 }
1533 
1535 {
1536  if (read_only)
1537  return (int)fvsWorkspaceReadOnly;
1538 
1539  return (int)fvsWorkspace;
1540 }
1541 
1543 {
1544  if (read_only)
1545  return (int)fvsProjectReadOnly;
1546 
1547  return (int)fvsProject;
1548 }
1549 
1551 {
1552  return (int)fvsFolder;
1553 }
1554 
1556 {
1557  return (int)fvsVirtualFolder;
1558 }
virtual wxTreeItemId GetNextChild(const wxTreeItemId &item, wxTreeItemIdValue &cookie) const
wxString AfterLast(wxUniChar ch) const
DLLIMPORT wxArrayInt cbGetMultiChoiceDialog(const wxString &message, const wxString &caption, const wxArrayString &choices, wxWindow *parent, const wxSize &size, const wxArrayInt &initialSelection)
wxMultiChoiceDialog wrapper.
Definition: globals.cpp:1441
wxString F(const wxChar *msg,...)
sprintf-like function
Definition: logmanager.h:20
wxFontEncoding DetectEncodingAndConvert(const char *strIn, wxString &strOut, wxFontEncoding possibleEncoding)
Try converting a C-string from different encodings until a possible match is found.
Definition: globals.cpp:754
static int WorkspaceIconIndex(bool read_only=false)
Definition: globals.cpp:1534
#define wxTextEntryDialogStyle
bool wxGetEnv(const wxString &var, wxString *value)
wxString realpath(const wxString &path)
Definition: globals.cpp:1348
bool Matches(const wxString &mask) const
#define wxLC_MASK_TYPE
bool wxRemoveFile(const wxString &file)
#define wxICON_QUESTION
const DLLIMPORT wxString TCC_EXT
static int ProjectIconIndex(bool read_only=false)
Definition: globals.cpp:1542
static unsigned int GetCount()
const DLLIMPORT wxString FPP_EXT
const DLLIMPORT wxString CODEBLOCKS_EXT
virtual wxTreeItemId GetItemParent(const wxTreeItemId &item) const
const DLLIMPORT wxString F95_EXT
const DLLIMPORT wxString SCRIPT_EXT
bool cbResolveSymLinkedDirPath(wxString &dirpath)
If path is pointing to a symlink then the function will set dirpath parameter to the path the symlink...
Definition: globals.cpp:1001
ConfigManager * GetConfigManager(const wxString &name_space) const
Definition: manager.cpp:474
Current user has read-write access to the directory.
Definition: globals.h:381
int ReadInt(const wxString &name, int defaultVal=0)
bool IsOk() const
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
wxMenuItem * FindItemByPosition(size_t position) const
#define wxSCI_EOL_CR
Definition: wxscintilla.h:84
#define wxNO
DLLIMPORT windows_version_t WindowsVersion()
Definition: globals.cpp:1338
SettingsIconsStyle
Icons styles for settings dialogs.
Definition: globals.h:297
wxString substr(size_t nStart=0, size_t nLen=npos) const
void QuoteStringIfNeeded(wxString &str)
Definition: globals.cpp:260
void DoRememberSelectedNodes(wxTreeCtrl *tree, wxArrayString &selectedItemPaths)
Definition: globals.cpp:441
wxArrayString MakeUniqueArray(const wxArrayString &array, bool caseSens)
Definition: globals.cpp:198
const DLLIMPORT wxString MSVC10_EXT
wxString Lower() const
wxFileOffset Length() const
wxArrayInt GetSelections() const
std::map< wxString, wxString > BackticksMap
Adds support for backtick&#39;d expressions under Windows.
Definition: globals.cpp:859
virtual int ShowModal()
const DLLIMPORT wxString H_EXT
virtual wxString GetItemLabel() const
size_t length() const
Invalid directory (does not exist).
Definition: globals.h:380
bool IsOk() const
int GetSelection() const
const DLLIMPORT wxString INL_EXT
static wxString GetVolumeSeparator(wxPathFormat format=wxPATH_NATIVE)
bool CreateDir(const wxString &full_path, int perms)
Definition: globals.cpp:631
bool ConvertAlphaToMask(unsigned char threshold=wxIMAGE_ALPHA_THRESHOLD)
const DLLIMPORT wxString XCODE1_EXT
bool wxFileExists(const wxString &filename)
static int VirtualFolderIconIndex()
Definition: globals.cpp:1555
const DLLIMPORT wxString DEVCPP_EXT
#define wxSCI_EOL_LF
Definition: wxscintilla.h:85
bool NormalizePath(wxFileName &f, const wxString &base)
Definition: globals.cpp:942
void AppendArray(const wxArrayString &from, wxArrayString &to)
Definition: globals.cpp:222
bool ReadBool(const wxString &name, bool defaultVal=false)
int Index(const wxString &sz, bool bCase=true, bool bFromEnd=false) const
Definition: globals.h:157
static wxImageList * MakeImageList()
Definition: globals.cpp:1486
SettingsIconsStyle GetSettingsIconsStyle(cb_unused wxListCtrl *lc)
Definition: globals.cpp:1143
virtual wxString GetPath() const
windows_version_t
Definition: globals.h:395
wxBitmapType
#define wxCANCEL
FileManager * GetFileManager() const
Definition: manager.cpp:479
const DLLIMPORT wxString HPP_EXT
const DLLIMPORT wxString JAVA_EXT
size_t Length() const
virtual void SetPath(const wxString &path)
bool wxDirExists(const wxString &dirname)
wxMenuItem * Append(int id, const wxString &item=wxEmptyString, const wxString &helpString=wxEmptyString, wxItemKind kind=wxITEM_NORMAL)
wxString ExpandBackticks(wxString &str)
Definition: globals.cpp:861
wxRect GetClientArea() const
#define _T(string)
FileType
Known file types.
Definition: globals.h:49
const wxString & GetHelp() const
virtual size_t GetSelections(wxArrayTreeItemIds &selection) const
#define wxYES_NO
const DLLIMPORT wxString S62_EXT
const wxWX2MBbuf cbU2C(const wxString &str)
Return multibyte (C string) representation of the string.
Definition: globals.cpp:743
const DLLIMPORT wxString RESOURCEBIN_EXT
const DLLIMPORT wxString HXX_EXT
const DLLIMPORT wxString C_EXT
const DLLIMPORT wxString MSVC6_EXT
const wxCharBuffer mb_str(const wxMBConv &conv=wxConvLibc) const
void PlaceWindow(wxTopLevelWindow *w, cbPlaceDialogMode mode, bool enforce)
Definition: globals.cpp:1177
bool IsAbsolute(wxPathFormat format=wxPATH_NATIVE) const
#define wxLC_SMALL_ICON
const DLLIMPORT wxString F77_EXT
const DLLIMPORT wxString TPP_EXT
std::vector< wxString > wxStringVec
Definition: globals.h:205
virtual wxString GetItemText(const wxTreeItemId &item) const
wxString & Remove(size_t pos)
wxFontEncoding
bool IsSuffixOfPath(wxFileName const &suffix, wxFileName const &path)
Definition: globals.cpp:962
virtual bool IsExpanded(const wxTreeItemId &item) const
bool Save(const wxString &file, const wxString &data, wxFontEncoding encoding, bool bom)
#define wxLC_LIST
#define wxT(string)
DLLIMPORT int cbGetSingleChoiceIndex(const wxString &message, const wxString &caption, const wxArrayString &choices, wxWindow *parent, const wxSize &size, int initialSelection)
Definition: globals.cpp:1427
wxDynamicLibrary * LoadLibrary(const wxString &filename)
bool wxMkdir(const wxString &dir, int perm=wxS_DIR_DEFAULT)
#define wxNOT_FOUND
wxString cbResolveSymLinkedDirPathRecursive(wxString dirpath)
Call cbResolveSymLinkedPath until the path is not a symlink.
Definition: globals.cpp:1054
void DoSelectRememberedNode(wxTreeCtrl *tree, const wxTreeItemId &parent, wxString &selectedItemPath)
Definition: globals.cpp:462
size_t GetMenuItemCount() const
bool empty() const
size_t find(const wxString &str, size_t nStart=0) const
bool HasAlpha() const
wxOperatingSystemId wxGetOsVersion(int *major=NULL, int *minor=NULL)
const DLLIMPORT wxString CPLPL_EXT
wxWindow * GetAppWindow() const
Definition: manager.cpp:424
const DLLIMPORT wxString F_EXT
virtual bool LoadFile(wxInputStream &stream, wxBitmapType type=wxBITMAP_TYPE_ANY, int index=-1)
const DLLIMPORT wxString DYNAMICLIB_EXT
#define DLLIMPORT
Definition: settings.h:16
wxFSFile * OpenFile(const wxString &location, int flags=wxFS_READ)
wxRect cbGetMonitorRectForWindow(wxWindow *window)
Definition: globals.cpp:1153
No icons, just text.
Definition: globals.h:300
wxStringVec GetVectorFromString(const wxString &text, const wxString &separator, bool trimSpaces)
Definition: globals.cpp:166
#define wxSIZE_ALLOW_MINUS_ONE
wxItemKind GetKind() const
bool IsWindowReallyShown(wxWindow *win)
Finds out if a window is really shown.
Definition: globals.cpp:931
wxUSE_UNICODE_dependent wxChar
const wxArrayString & GetDirs() const
void SetSelection(int selection)
const DLLIMPORT wxString D_EXT
const char wxGetTextFromUserPromptStr[]
virtual wxString GetItemLabelText() const
bool MakeRelativeTo(const wxString &pathBase=wxEmptyString, wxPathFormat format=wxPATH_NATIVE)
virtual wxTreeItemId GetFirstChild(const wxTreeItemId &item, wxTreeItemIdValue &cookie) const
bool Contains(const wxString &str) const
const DLLIMPORT wxString MSVC6_WORKSPACE_EXT
void RemoveLastDir()
const DLLIMPORT wxString SS_EXT
const DLLIMPORT wxString XML_EXT
wxString & RemoveLast(size_t n=1)
const DLLIMPORT wxString F90_EXT
wxRect GetGeometry() const
wxMenu * CopyMenu(wxMenu *mnu, bool with_accelerators)
This function creates a new wxMenu object on the heap and recursively copies a given menu into it...
Definition: globals.cpp:911
#define wxDD_NEW_DIR_BUTTON
wxString cbGetTextFromUser(const wxString &message, const wxString &caption, const wxString &defaultValue, wxWindow *parent, wxCoord x, wxCoord y, bool centre)
Definition: globals.cpp:1465
windows_version_t cb_get_os()
Definition: globals.cpp:1297
void Empty()
const DLLIMPORT wxString WORKSPACE_EXT
wxString Left(size_t count) const
int GetBottom() const
bool Close()
wxString cbReadFileContents(wxFile &file, wxFontEncoding encoding)
Reads a wxString from a non-unicode file. File must be open. File is closed automatically.
Definition: globals.cpp:697
size_t Replace(const wxString &strOld, const wxString &strNew, bool replaceAll=true)
wxPathFormat
virtual void SelectItem(const wxTreeItemId &item, bool select=true)
const DLLIMPORT wxString STATICLIB_EXT
int GetId() const
wxString GetVolume() const
const DLLIMPORT wxString MSVC7_WORKSPACE_EXT
DirAccessCheck
Result values of cbDirAccessCheck()
Definition: globals.h:378
DirAccessCheck cbDirAccessCheck(const wxString &dir)
Check what access the current user has in a directory.
Definition: globals.cpp:1271
bool IsSameAs(const wxString &s, bool caseSensitive=true) const
#define wxLC_ICON
static const wxString sep
const DLLIMPORT wxString CC_EXT
int wxMessageBox(const wxString &message, const wxString &caption=wxMessageBoxCaptionStr, int style=wxOK|wxCENTRE, wxWindow *parent=NULL, int x=wxDefaultCoord, int y=wxDefaultCoord)
LogManager * GetLogManager() const
Definition: manager.cpp:439
static wxFileName DirName(const wxString &dir, wxPathFormat format=wxPATH_NATIVE)
const DLLIMPORT wxString S_EXT
wxBitmap cbLoadBitmap(const wxString &filename, wxBitmapType bitmapType)
This function loads a bitmap from disk.
Definition: globals.cpp:1102
BackticksMap m_Backticks
Definition: globals.cpp:860
bool IsOpened() const
virtual void Collapse(const wxTreeItemId &item)
static wxUniChar GetPathSeparator(wxPathFormat format=wxPATH_NATIVE)
const wxStringCharType * wx_str() const
virtual int ShowModal()
wxString URLEncode(const wxString &str)
Definition: globals.cpp:832
bool cbSaveTinyXMLDocument(TiXmlDocument *doc, const wxString &filename)
Saves a TinyXML document correctly, even if the path contains unicode characters. ...
Definition: globals.cpp:727
const DLLIMPORT wxString XCODE2_EXT
wxString wxEmptyString
wxString Right(size_t count) const
#define wxOK
wxString EscapeSpaces(const wxString &str)
Escapes spaces and tabs (NOT quoting the string)
Definition: globals.cpp:273
int GetRight() const
bool cbRead(wxFile &file, wxString &st, wxFontEncoding encoding)
Reads a wxString from a non-unicode file. File must be open. File is closed automatically.
Definition: globals.cpp:668
const DLLIMPORT wxString NATIVE_EXT
int width
const wxString & _(const wxString &string)
wxString & Trim(bool fromRight=true)
const DLLIMPORT wxString F08_EXT
const DLLIMPORT wxString CXX_EXT
FileType FileTypeOf(const wxString &filename)
Definition: globals.cpp:285
#define cbThrow(message)
Definition: cbexception.h:42
wxString MakeUniqueString(const wxString &text, const wxString &separator, bool caseSens)
Definition: globals.cpp:217
wxArray< int > wxArrayInt
ssize_t Read(void *buffer, size_t count)
wxString UnixFilename(const wxString &filename, wxPathFormat format)
Definition: globals.cpp:228
virtual void Expand(const wxTreeItemId &item)
static int GetFromWindow(const wxWindow *win)
const DLLIMPORT wxString EXECUTABLE_EXT
wxString & Append(const char *psz)
bool IsEmpty() const
void Clear()
wxString GetPath(int flags=wxPATH_GET_VOLUME, wxPathFormat format=wxPATH_NATIVE) const
wxString ChooseDirectory(wxWindow *parent, const wxString &message, const wxString &initialPath, const wxString &basePath, bool askToMakeRelative, bool showCreateDirButton)
Definition: globals.cpp:639
int cbMessageBox(const wxString &message, const wxString &caption, int style, wxWindow *parent, int x, int y)
wxMessageBox wrapper.
Definition: globals.cpp:1395
int GetPlatformsFromString(const wxString &platforms)
Return an integer representation of a platform string.
Definition: globals.cpp:73
virtual wxTreeItemId GetNextSibling(const wxTreeItemId &item) const
static const size_t npos
cbPlaceDialogMode
Definition: globals.h:322
wxString cbC2U(const char *str)
Return str as a proper unicode-compatible string.
Definition: globals.cpp:733
bool DoRememberExpandedNodes(wxTreeCtrl *tree, const wxTreeItemId &parent, wxArrayString &nodePaths, wxString &path)
Definition: globals.cpp:503
wxString GetFullName() const
int ShowModal()
const DLLIMPORT wxString XRCRESOURCE_EXT
const DLLIMPORT wxString HPLPL_EXT
wxString cbFindFileInPATH(const wxString &filename)
Definition: globals.cpp:420
void DebugLog(const wxString &msg, Logger::level lv=Logger::info)
Definition: logmanager.h:146
void SetWindowStyleFlag(long style)
wxString & Prepend(const wxString &str)
wxString GetStringFromPlatforms(int platforms, bool forceSeparate)
Return a string representation of a platform / multiple platforms.
Definition: globals.cpp:90
const DLLIMPORT wxString HH_EXT
#define wxYES
wxString GetStringFromArray(const wxArrayString &array, const wxString &separator, bool SeparatorAtEnd)
Definition: globals.cpp:122
bool cbWrite(wxFile &file, const wxString &buff, wxFontEncoding encoding)
Writes a wxString to a non-unicode file. File must be open. File is closed automatically.
Definition: globals.cpp:705
int height
size_t Add(const wxString &str, size_t copies=1)
const DLLIMPORT wxString F03_EXT
static wxString ReadDataPath()
bool StartsWith(const wxString &prefix, wxString *rest=NULL) const
void SaveTreeState(wxTreeCtrl *tree, const wxTreeItemId &parent, wxArrayString &nodePaths, wxArrayString &selectedItemPaths)
Definition: globals.cpp:575
#define wxSCI_EOL_CRLF
Definition: wxscintilla.h:83
wxInputStream * GetStream() const
bool Normalize(int flags=wxPATH_NORM_ALL, const wxString &cwd=wxEmptyString, wxPathFormat format=wxPATH_NATIVE)
const DLLIMPORT wxString CPP_EXT
bool SaveDocument(const wxString &, TiXmlDocument *)
Definition: tinywxuni.cpp:45
int Add(const wxBitmap &bitmap, const wxBitmap &mask=wxNullBitmap)
virtual wxTreeItemId GetRootItem() const
wxRect & Intersect(const wxRect &rect)
size_t GetCount() const
bool cbSaveToFile(const wxString &filename, const wxString &contents, wxFontEncoding encoding, bool bom)
Writes a wxString to a file.
Definition: globals.cpp:721
int Find(wxUniChar ch, bool fromEnd=false) const
wxUniChar GetChar(size_t n) const
wxUniChar Last() const
int wxCoord
bool NeedQuotes(const wxString &str)
Definition: globals.cpp:266
wxMenu * GetSubMenu() const
bool UsesCommonControls6()
Check if CommonControls version is at least 6 (XP and up)
Definition: globals.cpp:1065
const DLLIMPORT wxString OBJECT_EXT
void SetSettingsIconsStyle(wxListCtrl *lc, SettingsIconsStyle style)
Set the icons style for the supplied list control.
Definition: globals.cpp:1125
bool MakeAbsolute(const wxString &cwd=wxEmptyString, wxPathFormat format=wxPATH_NATIVE)
static wxString CreateTempFileName(const wxString &prefix, wxFile *fileTemp=NULL)
#define wxRESIZE_BORDER
wxString GetEOLStr(int eolMode)
Reads settings if eolMode is -1 Expected input (defined in sdk/wxscintilla/include/wx/wxscintilla.h) is: wxSCI_EOL_CRLF=0, wxSCI_EOL_CR=1, or wxSCI_EOL_LF=2.
Definition: globals.cpp:812
bool Flush()
const DLLIMPORT wxString ASM_EXT
void DoExpandRememberedNode(wxTreeCtrl *tree, const wxTreeItemId &parent, const wxString &path)
Definition: globals.cpp:530
wxString GetFullPath(wxPathFormat format=wxPATH_NATIVE) const
void wxDisplaySize(int *width, int *height)
bool CreateDirRecursively(const wxString &full_path, int perms)
Definition: globals.cpp:610
Current user has read-only access to the directory.
Definition: globals.h:382
const char * cbGetTextFromUserPromptStr
Definition: globals.cpp:1460
const DLLIMPORT wxString RESOURCE_EXT
static int FolderIconIndex()
Definition: globals.cpp:1550
size_t Write(const void *buffer, size_t count)
Definition: globals.h:161
void RestoreTreeState(wxTreeCtrl *tree, const wxTreeItemId &parent, wxArrayString &nodePaths, wxArrayString &selectedItemPaths)
Definition: globals.cpp:589
virtual bool ItemHasChildren(const wxTreeItemId &item) const
wxArrayString GetArrayFromString(const wxString &text, const wxString &separator, bool trimSpaces)
Definition: globals.cpp:134
const DLLIMPORT wxString FOR_EXT
long wxExecute(const wxString &command, int flags=wxEXEC_ASYNC, wxProcess *callback=NULL, const wxExecuteEnv *env=NULL)
const DLLIMPORT wxString MSVC7_EXT
void SetSelections(const wxArrayInt &selections)