1 ///////////////////////////////////////////////////////////////////////////////
2 // Name:        MadUtils.cpp
3 // Description: utility functions. collects from internet
4 // Author:      madedit@gmail.com
5 // Licence:     GPL
6 ///////////////////////////////////////////////////////////////////////////////
7 
8 #include <wx/wxprec.h>
9 #include <wx/app.h>
10 #include <wx/gdicmn.h>
11 #include <wx/colour.h>
12 #include <wx/string.h>
13 #include <wx/filefn.h>
14 #include <wx/filename.h>
15 #include <locale.h>
16 
17 #if defined(__WXMAC__)
18 #include <wx/mac/private.h>
19 #include <Processes.h>
20 #endif
21 
22 #include "MadUtils.h"
23 
24 HtmlColor HtmlColorTable[]=
25 {
26     { wxT("AliceBlue"),            240,248,255 },
27     { wxT("AntiqueWhite"),         250,235,215 },
28     { wxT("Aqua"),                 0,255,255 },
29     { wxT("Aquamarine"),           127,255,212 },
30     { wxT("Azure"),                240,255,255 },
31     { wxT("Beige"),                245,245,220 },
32     { wxT("Bisque"),               255,228,196 },
33     { wxT("Black"),                0,0,0 },
34     { wxT("BlanchedAlmond"),       255,235,205 },
35     { wxT("Blue"),                 0,0,255 },
36     { wxT("BlueViolet"),           138,43,226 },
37     { wxT("Brown"),                165,42,42 },
38     { wxT("BurlyWood"),            222,184,135 },
39     { wxT("CadetBlue"),            95,158,160 },
40     { wxT("Chartreuse"),           127,255,0 },
41     { wxT("Chocolate"),            210,105,30 },
42     { wxT("Coral"),                255,127,80 },
43     { wxT("CornflowerBlue"),       100,149,237 },
44     { wxT("Cornsilk"),             255,248,220 },
45     { wxT("Crimson"),              237,164,61 },
46     { wxT("Cyan"),                 0,255,255 },
47     { wxT("DarkBlue"),             0,0,139 },
48     { wxT("DarkCyan"),             0,139,139 },
49     { wxT("DarkGoldenRod"),        184,134,11 },
50     { wxT("DarkGray"),             167,167,167 },
51     { wxT("DarkGreen"),            0,100,0 },
52     { wxT("DarkKhaki"),            189,183,107 },
53     { wxT("DarkMagenta"),          139,0,139 },
54     { wxT("DarkOliveGreen"),       85,107,47 },
55     { wxT("DarkOrchid"),           153,50,204 },
56     { wxT("DarkRed"),              139,0,0 },
57     { wxT("DarkSalmon"),           233,150,122 },
58     { wxT("DarkSeaGreen"),         143,188,143 },
59     { wxT("DarkSlateBlue"),        72,61,139 },
60     { wxT("DarkSlateGray"),        47,79,79 },
61     { wxT("DarkTurquoise"),        0,206,209 },
62     { wxT("DarkViolet"),           148,0,211 },
63     { wxT("Darkorange"),           255,140,0 },
64     { wxT("DeepPink"),             255,20,147 },
65     { wxT("DeepSkyBlue"),          0,191,255 },
66     { wxT("DimGray"),              105,105,105 },
67     { wxT("DodgerBlue"),           30,144,255 },
68     { wxT("Feldspar"),             209,146,117 },
69     { wxT("FireBrick"),            178,34,34 },
70     { wxT("FloralWhite"),          255,250,240 },
71     { wxT("ForestGreen"),          34,139,34 },
72     { wxT("Fuchsia"),              255,0,255 },
73     { wxT("Gainsboro"),            220,220,220 },
74     { wxT("GhostWhite"),           248,248,255 },
75     { wxT("Gold"),                 255,215,0 },
76     { wxT("GoldenRod"),            218,165,32 },
77     { wxT("Gray"),                 190,190,190 },
78     { wxT("Green"),                0,128,0 },
79     { wxT("GreenYellow"),          173,255,47 },
80     { wxT("HoneyDew"),             240,255,240 },
81     { wxT("HotPink"),              255,105,180 },
82     { wxT("IndianRed"),            205,92,92 },
83     { wxT("Indigo"),               75,0,130 },
84     { wxT("Ivory"),                255,255,240 },
85     { wxT("Khaki"),                240,230,140 },
86     { wxT("Lavender"),             230,230,250 },
87     { wxT("LavenderBlush"),        255,240,245 },
88     { wxT("LawnGreen"),            124,252,0 },
89     { wxT("LemonChiffon"),         255,250,205 },
90     { wxT("LightBlue"),            173,216,230 },
91     { wxT("LightCoral"),           240,128,128 },
92     { wxT("LightCyan"),            224,255,255 },
93     { wxT("LightGoldenRodYellow"), 250,250,210 },
94     { wxT("LightGreen"),           144,238,144 },
95     { wxT("LightGrey"),            211,211,211 },
96     { wxT("LightPink"),            255,182,193 },
97     { wxT("LightSalmon"),          255,160,122 },
98     { wxT("LightSeaGreen"),        32,178,170 },
99     { wxT("LightSkyBlue"),         135,206,250 },
100     { wxT("LightSlateBlue"),       132,112,255 },
101     { wxT("LightSlateGray"),       119,136,153 },
102     { wxT("LightSteelBlue"),       176,196,222 },
103     { wxT("LightYellow"),          255,255,224 },
104     { wxT("Lime"),                 0,255,0 },
105     { wxT("LimeGreen"),            50,205,50 },
106     { wxT("Linen"),                250,240,230 },
107     { wxT("Magenta"),              255,0,255 },
108     { wxT("Maroon"),               128,0,0 },
109     { wxT("MediumAquaMarine"),     102,205,170 },
110     { wxT("MediumBlue"),           0,0,205 },
111     { wxT("MediumOrchid"),         186,85,211 },
112     { wxT("MediumPurple"),         147,112,219 },
113     { wxT("MediumSeaGreen"),       60,179,113 },
114     { wxT("MediumSlateBlue"),      123,104,238 },
115     { wxT("MediumSpringGreen"),    0,250,154 },
116     { wxT("MediumTurquoise"),      72,209,204 },
117     { wxT("MediumVioletRed"),      199,21,133 },
118     { wxT("MidnightBlue"),         25,25,112 },
119     { wxT("MintCream"),            245,255,250 },
120     { wxT("MistyRose"),            255,228,225 },
121     { wxT("Moccasin"),             255,228,181 },
122     { wxT("NavajoWhite"),          255,222,173 },
123     { wxT("Navy"),                 0,0,128 },
124     { wxT("OldLace"),              253,245,230 },
125     { wxT("Olive"),                128,128,0 },
126     { wxT("OliveDrab"),            107,142,35 },
127     { wxT("Orange"),               255,165,0 },
128     { wxT("OrangeRed"),            255,69,0 },
129     { wxT("Orchid"),               218,112,214 },
130     { wxT("PaleGoldenRod"),        238,232,170 },
131     { wxT("PaleGreen"),            152,251,152 },
132     { wxT("PaleTurquoise"),        175,238,238 },
133     { wxT("PaleVioletRed"),        219,112,147 },
134     { wxT("PapayaWhip"),           255,239,213 },
135     { wxT("PeachPuff"),            255,218,185 },
136     { wxT("Peru"),                 205,133,63 },
137     { wxT("Pink"),                 255,192,203 },
138     { wxT("Plum"),                 221,160,221 },
139     { wxT("PowderBlue"),           176,224,230 },
140     { wxT("Purple"),               128,0,128 },
141     { wxT("Red"),                  255,0,0 },
142     { wxT("RosyBrown"),            188,143,143 },
143     { wxT("RoyalBlue"),            65,105,225 },
144     { wxT("SaddleBrown"),          139,69,19 },
145     { wxT("Salmon"),               250,128,114 },
146     { wxT("SandyBrown"),           244,164,96 },
147     { wxT("SeaGreen"),             46,139,87 },
148     { wxT("SeaShell"),             255,245,238 },
149     { wxT("Sienna"),               160,82,45 },
150     { wxT("Silver"),               192,192,192 },
151     { wxT("SkyBlue"),              135,206,235 },
152     { wxT("SlateBlue"),            106,90,205 },
153     { wxT("SlateGray"),            112,128,144 },
154     { wxT("Snow"),                 255,250,250 },
155     { wxT("SpringGreen"),          0,255,127 },
156     { wxT("SteelBlue"),            70,130,180 },
157     { wxT("Tan"),                  210,180,140 },
158     { wxT("Teal"),                 0,128,128 },
159     { wxT("Thistle"),              216,191,216 },
160     { wxT("Tomato"),               255,99,71 },
161     { wxT("Turquoise"),            64,224,208 },
162     { wxT("Violet"),               238,130,238 },
163     { wxT("VioletRed"),            208,32,144 },
164     { wxT("Wheat"),                245,222,179 },
165     { wxT("White"),                255,255,255 },
166     { wxT("WhiteSmoke"),           245,245,245 },
167     { wxT("Yellow"),               255,255,0 },
168     { wxT("YellowGreen"),          154,205,50 }
169 };
170 const int HtmlColorTableCount= sizeof(HtmlColorTable) / sizeof(HtmlColor);
171 
SetHtmlColors()172 void SetHtmlColors()
173 {
174     wxColourDatabase *cdb=wxTheColourDatabase;
175     HtmlColor *hc=HtmlColorTable;
176     for(int i=0; i<HtmlColorTableCount; ++i, ++hc)
177     {
178         cdb->AddColour(hc->name, wxColor(hc->red, hc->green, hc->blue));
179     }
180 }
181 
GetExecutablePath()182 wxString GetExecutablePath()
183 {
184     static bool found = false;
185     static wxString path;
186 
187     if (!found) {
188 #ifdef __WXMSW__
189 
190         wxChar buf[512];
191         *buf = wxT('\0');
192         GetModuleFileName(NULL, buf, 511);
193         path = buf;
194 
195 #elif defined(__WXMAC__)
196 
197         ProcessInfoRec processinfo;
198         ProcessSerialNumber procno ;
199         FSSpec fsSpec;
200 
201         procno.highLongOfPSN = NULL ;
202         procno.lowLongOfPSN = kCurrentProcess ;
203         processinfo.processInfoLength = sizeof(ProcessInfoRec);
204         processinfo.processName = NULL;
205         processinfo.processAppSpec = &fsSpec;
206 
207         GetProcessInformation( &procno , &processinfo ) ;
208         path = wxMacFSSpec2MacFilename(&fsSpec);
209 #else
210         wxString argv0 = wxTheApp->argv[0];
211 
212         if (wxIsAbsolutePath(argv0)) {
213             path = argv0;
214         }
215         else {
216             wxPathList pathlist;
217             pathlist.AddEnvList(wxT("PATH"));
218             path = pathlist.FindAbsoluteValidPath(argv0);
219         }
220 
221         wxFileName filename(path);
222         filename.Normalize();
223         path = filename.GetFullPath();
224 #endif
225         found = true;
226     }
227 
228     return path;
229 }
230 
231