Menu

[r1967]: / trunk / src / sdk / globals.cpp  Maximize  Restore  History

Download this file

774 lines (657 with data), 20.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
/*
* This file is part of Code::Blocks Studio, an open-source cross-platform IDE
* Copyright (C) 2003 Yiannis An. Mandravellos
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Contact e-mail: Yiannis An. Mandravellos <mandrav@codeblocks.org>
* Program URL : http://www.codeblocks.org
*
* $Revision$
* $Id$
* $HeadURL$
*/
#include "sdk_precomp.h"
#ifdef TIXML_USE_STL
#include <string>
#endif
#ifndef CB_PRECOMP
#include "globals.h"
#include <wx/filename.h>
#include <wx/file.h>
#include "cbexception.h"
#endif
#include "tinyxml/tinyxml.h"
#include <wx/tokenzr.h>
#include <wx/dirdlg.h>
#include <wx/msgdlg.h>
#include <wx/fontmap.h>
#include <algorithm>
wxString GetStringFromArray(const wxArrayString& array, const wxString& separator)
{
wxString out;
for (unsigned int i = 0; i < array.GetCount(); ++i)
out << array[i] << separator;
return out;
}
wxArrayString GetArrayFromString(const wxString& text, const wxString& separator, bool trimSpaces)
{
wxArrayString out;
wxString search = text;
int seplen = separator.Length();
while (true)
{
int idx = search.Find(separator);
if (idx == -1)
{
if (trimSpaces)
{
search.Trim(false);
search.Trim(true);
}
if (!search.IsEmpty())
out.Add(search);
break;
}
wxString part = search.Left(idx);
search.Remove(0, idx + seplen);
if (trimSpaces)
{
part.Trim(false);
part.Trim(true);
}
if (!part.IsEmpty())
out.Add(part);
}
return out;
}
void AppendArray(const wxArrayString& from, wxArrayString& to)
{
for (unsigned int i = 0; i < from.GetCount(); ++i)
{
to.Add(from[i]);
}
}
wxString UnixFilename(const wxString& filename)
{
wxString result = filename;
#ifdef __WXMSW__
bool unc_name = result.StartsWith(_T("\\\\"));
while (result.Replace(_T("/"), _T("\\")))
;
while (result.Replace(_T("\\\\"), _T("\\")))
;
if (unc_name)
result = _T("\\") + result;
#else
while (result.Replace(_T("\\"), _T("/")))
;
while (result.Replace(_T("//"), _T("/")))
;
#endif
return result;
}
void QuoteStringIfNeeded(wxString& str)
{
if (!str.IsEmpty() && str.GetChar(0) != _T('"') && str.Find(_T(' ')) != -1)
str = wxString(_T("\"")) + str + _T("\"");
}
FileType FileTypeOf(const wxString& filename)
{
wxString ext = filename.AfterLast(_T('.')).Lower();
if (ext.Matches(CPP_EXT) ||
ext.Matches(C_EXT) ||
ext.Matches(CC_EXT) ||
ext.Matches(CXX_EXT) ||
ext.Matches(D_EXT)
)
return ftSource;
else if (ext.Matches(HPP_EXT) ||
ext.Matches(H_EXT) ||
ext.Matches(HH_EXT) ||
ext.Matches(HXX_EXT)
)
return ftHeader;
else if (ext.Matches(CODEBLOCKS_EXT))
return ftCodeBlocksProject;
else if (ext.Matches(WORKSPACE_EXT))
return ftCodeBlocksWorkspace;
else if (ext.Matches(DEVCPP_EXT))
return ftDevCppProject;
else if (ext.Matches(MSVC6_EXT))
return ftMSVC6Project;
else if (ext.Matches(MSVC7_EXT))
return ftMSVC7Project;
else if (ext.Matches(MSVC6_WORKSPACE_EXT))
return ftMSVC6Workspace;
else if (ext.Matches(MSVC7_WORKSPACE_EXT))
return ftMSVC7Workspace;
else if (ext.Matches(OBJECT_EXT))
return ftObject;
else if (ext.Matches(XRCRESOURCE_EXT))
return ftXRCResource;
else if (ext.Matches(RESOURCE_EXT))
return ftResource;
else if (ext.Matches(RESOURCEBIN_EXT))
return ftResourceBin;
else if (ext.Matches(STATICLIB_EXT))
return ftStaticLib;
else if (ext.Matches(DYNAMICLIB_EXT))
return ftDynamicLib;
else if (ext.Matches(EXECUTABLE_EXT))
return ftExecutable;
return ftOther;
}
bool DoRememberExpandedNodes(wxTreeCtrl* tree, const wxTreeItemId& parent, wxArrayString& nodePaths, wxString& path)
{
// remember expanded tree nodes of this tree
if (!tree || !parent.IsOk())
return false;
wxString originalPath = path;
bool found = false;
#if (wxMAJOR_VERSION == 2) && (wxMINOR_VERSION < 5)
long int cookie = 0;
#else
wxTreeItemIdValue cookie; //2.6.0
#endif
wxTreeItemId child = tree->GetFirstChild(parent, cookie);
while (child.IsOk())
{
if (tree->ItemHasChildren(child) && tree->IsExpanded(child))
{
found = true;
path << _T("/") << tree->GetItemText(child);
DoRememberExpandedNodes(tree, child, nodePaths, path);
nodePaths.Add(path);
path = originalPath;
}
child = tree->GetNextChild(parent, cookie);
}
return found;
}
void DoExpandRememberedNode(wxTreeCtrl* tree, const wxTreeItemId& parent, const wxString& path)
{
if (!path.IsEmpty())
{
//Manager::Get()->GetMessageManager()->Log(mltDevDebug, path);
wxString tmpPath;
tmpPath = path;
wxString folder;
int pos = tmpPath.Find(_T('/'));
while (pos == 0)
{
tmpPath = tmpPath.Right(tmpPath.Length() - pos - 1);
pos = tmpPath.Find(_T('/'));
}
if (pos < 0) // no '/'
{
folder = tmpPath;
tmpPath.Clear();
}
else
{
folder = tmpPath.Left(pos);
tmpPath = tmpPath.Right(tmpPath.Length() - pos - 1);
}
//Manager::Get()->GetMessageManager()->Log(mltDevDebug, "%s, %s", folder.c_str(), tmpPath.c_str());
#if (wxMAJOR_VERSION == 2) && (wxMINOR_VERSION < 5)
long int cookie = 0;
#else
wxTreeItemIdValue cookie; //2.6.0
#endif
wxTreeItemId child = tree->GetFirstChild(parent, cookie);
while (child.IsOk())
{
wxString itemText = tree->GetItemText(child);
if (itemText.Matches(folder))
{
tree->Expand(child);
DoExpandRememberedNode(tree, child, tmpPath);
break;
}
child = tree->GetNextChild(parent, cookie);
}
}
}
void SaveTreeState(wxTreeCtrl* tree, const wxTreeItemId& parent, wxArrayString& nodePaths)
{
nodePaths.Clear();
if (!parent.IsOk() || !tree || !tree->ItemHasChildren(parent) || !tree->IsExpanded(parent))
return;
wxString tmp;
if (!DoRememberExpandedNodes(tree, parent, nodePaths, tmp))
nodePaths.Add(tmp); // just the tree root
}
void RestoreTreeState(wxTreeCtrl* tree, const wxTreeItemId& parent, wxArrayString& nodePaths)
{
if (!parent.IsOk() || !tree)
return;
if (nodePaths.GetCount() == 0)
{
tree->Collapse(parent);
return;
}
for (unsigned int i = 0; i < nodePaths.GetCount(); ++i)
DoExpandRememberedNode(tree, parent, nodePaths[i]);
nodePaths.Clear();
}
bool CreateDirRecursively(const wxString& full_path, int perms)
{
wxArrayString dirs;
wxString currdir;
#ifdef __WXMSW__
// hack to support for UNC filenames
if (full_path.StartsWith(_T("\\\\")))
{
wxFileName tmp(_T("C:") + full_path.SubString(1, full_path.Length()));
dirs = tmp.GetDirs();
currdir = _T("\\\\") + dirs[0] + wxFILE_SEP_PATH;
wxMessageBox(currdir);
dirs.RemoveAt(0);
}
else
#endif
{
wxFileName tmp(full_path);
currdir = tmp.GetVolume() + tmp.GetVolumeSeparator() + wxFILE_SEP_PATH;
dirs = tmp.GetDirs();
}
for (size_t i = 0; i < dirs.GetCount(); ++i)
{
currdir << dirs[i];
if (!wxDirExists(currdir) && !wxMkdir(currdir, perms))
return false;
currdir << wxFILE_SEP_PATH;
}
return true;
}
wxString ChooseDirectory(wxWindow* parent,
const wxString& message,
const wxString& initialPath,
const wxString& basePath,
bool askToMakeRelative, // relative to initialPath
bool showCreateDirButton) // where supported
{
wxDirDialog dlg(parent,
message,
_T(""),
(showCreateDirButton ? wxDD_NEW_DIR_BUTTON : 0) | wxRESIZE_BORDER);
dlg.SetPath(initialPath);
PlaceWindow(&dlg);
if (dlg.ShowModal() != wxID_OK)
return wxEmptyString;
wxFileName path(dlg.GetPath());
if (askToMakeRelative && !basePath.IsEmpty())
{
// ask the user if he wants it to be kept as relative
if (wxMessageBox(_("Keep this as a relative path?"),
_("Question"),
wxICON_QUESTION | wxYES_NO) == wxYES)
{
path.MakeRelativeTo(basePath);
}
}
return path.GetFullPath();
}
// Reads a wxString from a non-unicode file. File must be open. File is closed automatically.
bool cbRead(wxFile& file, wxString& st, wxFontEncoding encoding)
{
st.Empty();
if (!file.IsOpened())
return false;
int len = file.Length();
if(!len)
{
file.Close();
return true;
}
#if wxUSE_UNICODE
char* buff = new char[len+1];
if (!buff)
{
file.Close();
return false;
}
file.Read((void*)buff, len);
file.Close();
buff[len]='\0';
wxCSConv conv(encoding);
st = wxString((const char *)buff, conv);
delete[] buff;
#else
char* buff = st.GetWriteBuf(len); // GetWriteBuf already handles the extra '\0'.
file.Read((void*)buff, len);
file.Close();
st.UngetWriteBuf();
#endif
return true;
}
wxString cbReadFileContents(wxFile& file, wxFontEncoding encoding)
{
wxString st;
cbRead(file, st, encoding);
return st;
}
// Writes a wxString to a non-unicode file. File must be open. File is closed automatically.
bool cbWrite(wxFile& file, const wxString& buff, wxFontEncoding encoding)
{
bool result = false;
if (file.IsOpened())
{
wxCSConv conv(encoding);
result = file.Write(buff,conv);
if(result)
file.Flush();
file.Close();
}
return result;
}
// Writes a wxString to a file. Takes care of unicode and uses a temporary file
// to save first and then it copies it over the original.
bool cbSaveToFile(const wxString& filename, const wxString& contents, wxFontEncoding encoding, bool bom)
{
wxCSConv conv(encoding);
wxTempFile file(filename);
if (file.IsOpened())
{
if (bom)
{
char* mark = NULL;
int mark_length = 0;
/* TODO: write byte order mark */
switch (encoding)
{
case wxFONTENCODING_UTF7:
/* TODO: utf-7 bom is weird */
break;
case wxFONTENCODING_UTF8:
mark = "\xEF\xBB\xBF";
mark_length = 3;
break;
case wxFONTENCODING_UTF16BE:
mark = "\xFE\xFF";
mark_length = 2;
break;
case wxFONTENCODING_UTF16LE:
mark = "\xFF\xFE";
mark_length = 2;
break;
case wxFONTENCODING_UTF32BE:
mark = "\x00\x00\xFE\xFF";
mark_length = 4;
break;
case wxFONTENCODING_UTF32LE:
mark = "\xFF\xFE\x00\x00";
mark_length = 4;
break;
case wxFONTENCODING_SYSTEM:
default:
/* can't do anything here */
break;
}
if (mark_length>0 && mark)
{
if (!file.Write((void*)mark, mark_length))
return false;
}
}
if (!file.Write(contents, conv))
return false;
if (!file.Commit())
return false;
}
else
return false;
return true;
}
// Saves a TinyXML document correctly, even if the path contains unicode characters.
bool cbSaveTinyXMLDocument(TiXmlDocument* doc, const wxString& filename)
{
if (!doc)
return false;
const char *buffer; // UTF-8 encoded data
size_t len;
#ifdef TIXML_USE_STL
std::string outSt;
outSt << *doc;
buffer = outSt.c_str();
len = outSt.length();
#else
TiXmlOutStream outSt;
outSt << *doc;
buffer = outSt.c_str();
len = outSt.length();
#endif
wxTempFile file(filename);
if (file.IsOpened())
{
if (!file.Write(buffer, strlen(buffer)))
return false;
if (!file.Commit())
return false;
}
else
return false;
return true;
}
// Return @c str as a proper unicode-compatible string
wxString cbC2U(const char* str)
{
#if wxUSE_UNICODE
return wxString(str, wxConvLocal);
#else
return wxString(str);
#endif
}
// Return multibyte (C string) representation of the string
wxWX2MBbuf cbU2C(const wxString& str)
{
#if wxUSE_UNICODE
return str.mb_str(wxConvLocal);
#else
return (wxChar*)str.mb_str();
#endif
}
wxString URLEncode(const wxString &str) // not sure this is 100% standards compliant, but I hope so
{
wxString ret;
wxString t;
for(unsigned int i = 0; i < str.length(); ++i)
{
wxChar c = str[i];
if( (c >= _T('A') && c <= _T('Z'))
|| (c >= _T('a') && c <= _T('z'))
|| (c >= _T('0') && c <= _T('9'))
|| c == _T('.') || c == _T('-')|| c == _T('_') )
ret.Append(c);
else if(c == _T(' '))
ret.Append(_T('+'));
else
{
t.sprintf(_T("%%%02X"), (unsigned int) c);
ret.Append(t);
}
}
return ret;
}
bool IsWindowReallyShown(wxWindow* win)
{
while (win && win->IsShown())
{
win = win->GetParent();
if (!win)
return true;
}
return false;
}
bool NormalizePath(wxFileName& f,const wxString& base)
{
bool result = true;
// if(!f.IsAbsolute())
{
wxLogNull null_logger; // we do all file checks ourselves
f.Normalize(wxPATH_NORM_ALL & ~wxPATH_NORM_CASE, base);
result = f.IsOk();
}
return result;
}
inline bool IsRunningWindows2000()
{
int major = 0;
int minor = 0;
if(wxGetOsVersion(&major, &minor) == wxWINDOWS_NT && major < 5)
return true;
return false;
};
wxBitmap LoadPNGWindows2000Hack(const wxString& filename)
{
static bool isWindows2000 = IsRunningWindows2000();
wxImage im;
im.LoadFile(filename, wxBITMAP_TYPE_PNG);
if(isWindows2000)
im.ConvertAlphaToMask();
wxBitmap bmp(im);
return bmp;
}
#ifdef __WXMSW__
void PlaceWindow(wxTopLevelWindow *w, cbPlaceDialogMode mode, bool enforce)
{
HMONITOR hMonitor;
MONITORINFO mi;
RECT r;
int the_mode;
if(!w)
cbThrow(_T("Passed NULL pointer to PlaceWindow."));
wxTopLevelWindow *referenceWindow = Manager::Get()->GetAppWindow();
if(!referenceWindow) // no application window available, so this is as good as we can get
referenceWindow = w;
wxRect windowRect = w->GetRect();
ConfigManager *cfg = Manager::Get()->GetConfigManager(_T("app"));
if(!enforce && cfg->ReadBool(_T("/dialog_placement/do_place")) == false)
return;
if(mode == pdlBest)
the_mode = cfg->ReadInt(_T("/dialog_placement/dialog_position"), (int) pdlCentre);
else
the_mode = (int) mode;
hMonitor = MonitorFromWindow((HWND) referenceWindow->GetHandle(), MONITOR_DEFAULTTONEAREST);
mi.cbSize = sizeof(mi);
GetMonitorInfo(hMonitor, &mi);
r = mi.rcWork;
int monitorWidth = r.right - r.left;
int monitorHeight = r.bottom - r. top;
switch(the_mode)
{
case pdlCentre:
{
windowRect.x = r.left + (monitorWidth - windowRect.width)/2;
windowRect.y = r.top + (monitorHeight - windowRect.height)/2;
}
break;
case pdlHead:
{
windowRect.x = r.left + (monitorWidth - windowRect.width)/2;
windowRect.y = r.top + (monitorHeight - windowRect.height)/3;
}
break;
case pdlConstrain:
{
int x1 = windowRect.x;
int x2 = windowRect.x + windowRect.width;
int y1 = windowRect.y;
int y2 = windowRect.y + windowRect.height;
if(windowRect.width > monitorWidth) // cannot place without clipping, so center it
{
x1 = r.left + (monitorWidth - windowRect.width)/2;
x2 = x1 + windowRect.width;
}
else
{
x2 = std::min((int) r.right, windowRect.GetRight());
x1 = std::max(x2 - windowRect.width, (int) r.left);
x2 = x1 + windowRect.width;
}
if(windowRect.height > monitorHeight) // cannot place without clipping, so center it
{
y1 = r.top + (monitorHeight - windowRect.height)/2;
y2 = y1 + windowRect.height;
}
else
{
y2 = std::min((int) r.bottom, windowRect.GetBottom());
y1 = std::max(y2 - windowRect.height, (int) r.top);
y2 = y1 + windowRect.height;
}
windowRect = wxRect(x1, y1, x2-x1, y2-y1);
}
break;
case pdlClip:
{
int x1 = windowRect.x;
int x2 = windowRect.x + windowRect.width;
int y1 = windowRect.y;
int y2 = windowRect.y + windowRect.height;
x1 = std::max(x1, (int) r.left);
x2 = std::min(x2, (int) r.right);
y1 = std::max(y1, (int) r.top);
y2 = std::min(y2, (int) r.bottom);
windowRect = wxRect(x1, y1, x2-x1, y2-y1);
}
break;
}
w->SetSize(windowRect.x, windowRect.y, windowRect.width, windowRect.height, wxSIZE_ALLOW_MINUS_ONE);
}
#else // ----- non-Windows ----------------------------------------------
void PlaceWindow(wxTopLevelWindow *w, cbPlaceDialogMode mode, bool enforce)
// TODO (thomas#1#): The non-Windows implementation is *pathetic*.
// However, I don't know how to do it well under GTK / X / Xinerama / whatever.
// Anyone?
{
int the_mode;
wxFrame *referenceWindow = Manager::Get()->GetAppWindow();
if(!referenceWindow) // let's not crash on shutdown
return;
if(!w)
cbThrow(_T("Passed NULL pointer to PlaceWindow."));
ConfigManager *cfg = Manager::Get()->GetConfigManager(_T("app"));
if(!enforce && cfg->ReadBool(_T("/dialog_placement/do_place")) == false)
return;
if(mode == pdlBest)
the_mode = cfg->ReadInt(_T("/dialog_placement/dialog_position"), (int) pdlCentre);
else
the_mode = (int) mode;
if(the_mode == pdlCentre || the_mode == pdlHead)
{
if(w->GetParent())
w->CentreOnParent(); // poo!
else
w->CentreOnScreen();
return;
}
else
{
wxRect windowRect = w->GetRect();
wxRect parentRect = referenceWindow->GetRect(); // poo again!
int x1 = windowRect.x;
int x2 = windowRect.x + windowRect.width;
int y1 = windowRect.y;
int y2 = windowRect.y + windowRect.height;
x1 = std::max(x1, parentRect.x);
x2 = std::min(x2, parentRect.GetRight());
y1 = std::max(y1, parentRect.y);
y2 = std::min(y2, parentRect.GetBottom());
w->SetSize(x1, y1, x2-x1, y2-y1, wxSIZE_ALLOW_MINUS_ONE);
}
}
#endif //platform-specific placement code