]> git.saurik.com Git - wxWidgets.git/blame - src/common/gdicmn.cpp
Fixed some potential buffer overruns
[wxWidgets.git] / src / common / gdicmn.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: gdicmn.cpp
3// Purpose: Common GDI classes
4// Author: Julian Smart
5// Modified by:
6// Created: 01/02/97
7// RCS-ID: $Id$
55d99c7a 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
c801d85f
KB
13#pragma implementation "gdicmn.h"
14#endif
15
4dff3400
JJ
16#ifdef __VMS
17#define XtDisplay XTDISPLAY
18#endif
19
c801d85f
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24#pragma hdrstop
25#endif
26
2432b92d 27#include "wx/event.h"
c801d85f
KB
28#include "wx/gdicmn.h"
29#include "wx/brush.h"
30#include "wx/pen.h"
31#include "wx/bitmap.h"
32#include "wx/icon.h"
33#include "wx/cursor.h"
34#include "wx/font.h"
35#include "wx/palette.h"
46ccb510 36#include "wx/app.h"
b0e0d661 37#include "wx/dc.h"
b93d0c64 38#include "wx/utils.h"
5100cabf 39#include "wx/settings.h"
222ed1d6 40#include "wx/hashmap.h"
c801d85f 41
cb38104e 42#include "wx/log.h"
c801d85f
KB
43#include <string.h>
44
82ef81ed 45#if defined(__WXMSW__)
9ed0d735 46#include "wx/msw/wrapwin.h"
c801d85f
KB
47#endif
48
46ccb510 49#ifdef __WXMOTIF__
338dd992
JJ
50#ifdef __VMS__
51#pragma message disable nosimpint
52#endif
46ccb510 53#include <Xm/Xm.h>
338dd992
JJ
54#ifdef __VMS__
55#pragma message enable nosimpint
56#endif
46ccb510
JS
57#endif
58
7266b672
JS
59#ifdef __WXX11__
60#include "X11/Xlib.h"
61#endif
62
99fd0d71
SC
63#ifdef __WXMAC__
64#include "wx/mac/private.h"
e4a05ccd 65#include "wx/mac/uma.h"
99fd0d71 66#endif
a23fd0e1 67
cb73e600
SC
68#if wxUSE_EXTENDED_RTTI
69
70// wxPoint
71
72template<> void wxStringReadValue(const wxString &s , wxPoint &data )
73{
5d3e7b52 74 wxSscanf(s, wxT("%d,%d"), &data.x , &data.y ) ;
cb73e600
SC
75}
76
77template<> void wxStringWriteValue(wxString &s , const wxPoint &data )
78{
5d3e7b52 79 s = wxString::Format(wxT("%d,%d"), data.x , data.y ) ;
cb73e600
SC
80}
81
8805dbab 82wxCUSTOM_TYPE_INFO(wxPoint, wxToStringConverter<wxPoint> , wxFromStringConverter<wxPoint>)
cb73e600
SC
83
84template<> void wxStringReadValue(const wxString &s , wxSize &data )
85{
5d3e7b52 86 wxSscanf(s, wxT("%d,%d"), &data.x , &data.y ) ;
cb73e600
SC
87}
88
89template<> void wxStringWriteValue(wxString &s , const wxSize &data )
90{
5d3e7b52 91 s = wxString::Format(wxT("%d,%d"), data.x , data.y ) ;
cb73e600
SC
92}
93
8805dbab 94wxCUSTOM_TYPE_INFO(wxSize, wxToStringConverter<wxSize> , wxFromStringConverter<wxSize>)
cb73e600
SC
95
96#endif
97
45816ddd 98IMPLEMENT_ABSTRACT_CLASS(wxDCBase, wxObject)
c801d85f 99
d7a7546b 100wxRect::wxRect(const wxPoint& point1, const wxPoint& point2)
c801d85f 101{
5d3e7b52
WS
102 x = point1.x;
103 y = point1.y;
104 width = point2.x - point1.x;
105 height = point2.y - point1.y;
8bbe427f 106
5d3e7b52
WS
107 if (width < 0)
108 {
109 width = -width;
110 x = point2.x;
111 }
112 width++;
8bbe427f 113
5d3e7b52
WS
114 if (height < 0)
115 {
116 height = -height;
117 y = point2.y;
118 }
119 height++;
c801d85f
KB
120}
121
a23fd0e1 122bool wxRect::operator==(const wxRect& rect) const
c801d85f 123{
5d3e7b52
WS
124 return ((x == rect.x) &&
125 (y == rect.y) &&
126 (width == rect.width) &&
127 (height == rect.height));
c801d85f
KB
128}
129
df83b840 130wxRect wxRect::operator+(const wxRect& rect) const
619d0528 131{
45816ddd
VZ
132 int x1 = wxMin(this->x, rect.x);
133 int y1 = wxMin(this->y, rect.y);
134 int y2 = wxMax(y+height, rect.height+rect.y);
135 int x2 = wxMax(x+width, rect.width+rect.x);
136 return wxRect(x1, y1, x2-x1, y2-y1);
dcb44466
JS
137}
138
df83b840
VZ
139wxRect& wxRect::Union(const wxRect& rect)
140{
c71ce675
VZ
141 // ignore empty rectangles: union with an empty rectangle shouldn't extend
142 // this one to (0, 0)
143 if ( !width || !height )
144 {
145 *this = rect;
146 }
147 else if ( rect.width && rect.height )
df83b840
VZ
148 {
149 int x1 = wxMin(x, rect.x);
150 int y1 = wxMin(y, rect.y);
151 int y2 = wxMax(y + height, rect.height + rect.y);
152 int x2 = wxMax(x + width, rect.width + rect.x);
153
154 x = x1;
155 y = y1;
156 width = x2 - x1;
157 height = y2 - y1;
158 }
c71ce675 159 //else: we're not empty and rect is empty
df83b840
VZ
160
161 return *this;
162}
163
1e6feb95
VZ
164wxRect& wxRect::Inflate(wxCoord dx, wxCoord dy)
165{
166 x -= dx;
167 y -= dy;
168 width += 2*dx;
169 height += 2*dy;
170
171 // check that we didn't make the rectangle invalid by accident (you almost
172 // never want to have negative coords and never want negative size)
3379ed37
VZ
173 if ( x < 0 )
174 x = 0;
175 if ( y < 0 )
176 y = 0;
177
178 // what else can we do?
179 if ( width < 0 )
180 width = 0;
181 if ( height < 0 )
182 height = 0;
1e6feb95
VZ
183
184 return *this;
185}
186
dcb44466
JS
187bool wxRect::Inside(int cx, int cy) const
188{
45816ddd
VZ
189 return ( (cx >= x) && (cy >= y)
190 && ((cy - y) < height)
191 && ((cx - x) < width)
192 );
dcb44466
JS
193}
194
1e6feb95
VZ
195wxRect& wxRect::Intersect(const wxRect& rect)
196{
197 int x2 = GetRight(),
198 y2 = GetBottom();
199
200 if ( x < rect.x )
201 x = rect.x;
202 if ( y < rect.y )
203 y = rect.y;
204 if ( x2 > rect.GetRight() )
205 x2 = rect.GetRight();
206 if ( y2 > rect.GetBottom() )
207 y2 = rect.GetBottom();
208
209 width = x2 - x + 1;
210 height = y2 - y + 1;
211
212 if ( width <= 0 || height <= 0 )
213 {
214 width =
215 height = 0;
216 }
217
218 return *this;
219}
220
221bool wxRect::Intersects(const wxRect& rect) const
222{
223 wxRect r = Intersect(rect);
224
225 // if there is no intersection, both width and height are 0
226 return r.width != 0;
227}
228
c50f92d0
VZ
229// ============================================================================
230// wxColourDatabase
231// ============================================================================
232
c50f92d0
VZ
233// ----------------------------------------------------------------------------
234// wxColourDatabase ctor/dtor
235// ----------------------------------------------------------------------------
222ed1d6
MB
236
237wxColourDatabase::wxColourDatabase ()
c801d85f 238{
c50f92d0
VZ
239 // will be created on demand in Initialize()
240 m_map = NULL;
c801d85f
KB
241}
242
6a6c0a8b 243wxColourDatabase::~wxColourDatabase ()
c801d85f 244{
c50f92d0
VZ
245 if ( m_map )
246 {
247 WX_CLEAR_HASH_MAP(wxStringToColourHashMap, *m_map);
248
249 delete m_map;
250 }
222ed1d6 251
19bf0c69
DW
252#ifdef __WXPM__
253 delete [] m_palTable;
254#endif
c801d85f
KB
255}
256
257// Colour database stuff
c50f92d0 258void wxColourDatabase::Initialize()
c801d85f 259{
c50f92d0
VZ
260 if ( m_map )
261 {
262 // already initialized
263 return;
264 }
265
266 m_map = new wxStringToColourHashMap;
267
f6bcfd97
BP
268 static const struct wxColourDesc
269 {
270 const wxChar *name;
5c519b6c 271 unsigned char r,g,b;
f6bcfd97
BP
272 }
273 wxColourTable[] =
274 {
5e2ab1ea 275 {wxT("AQUAMARINE"),112, 219, 147},
f6bcfd97
BP
276 {wxT("BLACK"),0, 0, 0},
277 {wxT("BLUE"), 0, 0, 255},
5e2ab1ea 278 {wxT("BLUE VIOLET"), 159, 95, 159},
f6bcfd97 279 {wxT("BROWN"), 165, 42, 42},
5e2ab1ea
VZ
280 {wxT("CADET BLUE"), 95, 159, 159},
281 {wxT("CORAL"), 255, 127, 0},
282 {wxT("CORNFLOWER BLUE"), 66, 66, 111},
f6bcfd97 283 {wxT("CYAN"), 0, 255, 255},
5e2ab1ea
VZ
284 {wxT("DARK GREY"), 47, 47, 47}, // ?
285
286 {wxT("DARK GREEN"), 47, 79, 47},
287 {wxT("DARK OLIVE GREEN"), 79, 79, 47},
f6bcfd97 288 {wxT("DARK ORCHID"), 153, 50, 204},
5e2ab1ea 289 {wxT("DARK SLATE BLUE"), 107, 35, 142},
f6bcfd97 290 {wxT("DARK SLATE GREY"), 47, 79, 79},
5e2ab1ea
VZ
291 {wxT("DARK TURQUOISE"), 112, 147, 219},
292 {wxT("DIM GREY"), 84, 84, 84},
293 {wxT("FIREBRICK"), 142, 35, 35},
294 {wxT("FOREST GREEN"), 35, 142, 35},
295 {wxT("GOLD"), 204, 127, 50},
296 {wxT("GOLDENROD"), 219, 219, 112},
297 {wxT("GREY"), 128, 128, 128},
f6bcfd97 298 {wxT("GREEN"), 0, 255, 0},
5e2ab1ea
VZ
299 {wxT("GREEN YELLOW"), 147, 219, 112},
300 {wxT("INDIAN RED"), 79, 47, 47},
301 {wxT("KHAKI"), 159, 159, 95},
302 {wxT("LIGHT BLUE"), 191, 216, 216},
303 {wxT("LIGHT GREY"), 192, 192, 192},
304 {wxT("LIGHT STEEL BLUE"), 143, 143, 188},
305 {wxT("LIME GREEN"), 50, 204, 50},
306 {wxT("LIGHT MAGENTA"), 255, 0, 255},
f6bcfd97 307 {wxT("MAGENTA"), 255, 0, 255},
5e2ab1ea
VZ
308 {wxT("MAROON"), 142, 35, 107},
309 {wxT("MEDIUM AQUAMARINE"), 50, 204, 153},
310 {wxT("MEDIUM GREY"), 100, 100, 100},
311 {wxT("MEDIUM BLUE"), 50, 50, 204},
312 {wxT("MEDIUM FOREST GREEN"), 107, 142, 35},
313 {wxT("MEDIUM GOLDENROD"), 234, 234, 173},
314 {wxT("MEDIUM ORCHID"), 147, 112, 219},
315 {wxT("MEDIUM SEA GREEN"), 66, 111, 66},
316 {wxT("MEDIUM SLATE BLUE"), 127, 0, 255},
317 {wxT("MEDIUM SPRING GREEN"), 127, 255, 0},
318 {wxT("MEDIUM TURQUOISE"), 112, 219, 219},
319 {wxT("MEDIUM VIOLET RED"), 219, 112, 147},
320 {wxT("MIDNIGHT BLUE"), 47, 47, 79},
321 {wxT("NAVY"), 35, 35, 142},
322 {wxT("ORANGE"), 204, 50, 50},
323 {wxT("ORANGE RED"), 255, 0, 127},
324 {wxT("ORCHID"), 219, 112, 219},
325 {wxT("PALE GREEN"), 143, 188, 143},
326 {wxT("PINK"), 188, 143, 234},
327 {wxT("PLUM"), 234, 173, 234},
328 {wxT("PURPLE"), 176, 0, 255},
f6bcfd97 329 {wxT("RED"), 255, 0, 0},
5e2ab1ea
VZ
330 {wxT("SALMON"), 111, 66, 66},
331 {wxT("SEA GREEN"), 35, 142, 107},
332 {wxT("SIENNA"), 142, 107, 35},
333 {wxT("SKY BLUE"), 50, 153, 204},
334 {wxT("SLATE BLUE"), 0, 127, 255},
f6bcfd97 335 {wxT("SPRING GREEN"), 0, 255, 127},
5e2ab1ea
VZ
336 {wxT("STEEL BLUE"), 35, 107, 142},
337 {wxT("TAN"), 219, 147, 112},
f6bcfd97 338 {wxT("THISTLE"), 216, 191, 216},
5e2ab1ea
VZ
339 {wxT("TURQUOISE"), 173, 234, 234},
340 {wxT("VIOLET"), 79, 47, 79},
341 {wxT("VIOLET RED"), 204, 50, 153},
342 {wxT("WHEAT"), 216, 216, 191},
f6bcfd97
BP
343 {wxT("WHITE"), 255, 255, 255},
344 {wxT("YELLOW"), 255, 255, 0},
aad6765c 345 {wxT("YELLOW GREEN"), 153, 204, 50}
f6bcfd97
BP
346 };
347
c50f92d0 348 size_t n;
bf2c4b94
DW
349
350 for ( n = 0; n < WXSIZEOF(wxColourTable); n++ )
f6bcfd97
BP
351 {
352 const wxColourDesc& cc = wxColourTable[n];
c50f92d0 353 (*m_map)[cc.name] = new wxColour(cc.r, cc.g, cc.b);
f6bcfd97 354 }
c50f92d0 355
19bf0c69
DW
356#ifdef __WXPM__
357 m_palTable = new long[n];
358 for ( n = 0; n < WXSIZEOF(wxColourTable); n++ )
359 {
360 const wxColourDesc& cc = wxColourTable[n];
361 m_palTable[n] = OS2RGB(cc.r,cc.g,cc.b);
362 }
363 m_nSize = n;
364#endif
c801d85f
KB
365}
366
c50f92d0
VZ
367// ----------------------------------------------------------------------------
368// wxColourDatabase operations
369// ----------------------------------------------------------------------------
222ed1d6 370
c50f92d0 371void wxColourDatabase::AddColour(const wxString& name, const wxColour& colour)
222ed1d6 372{
c50f92d0 373 Initialize();
222ed1d6 374
c50f92d0
VZ
375 // canonicalize the colour names before using them as keys: they should be
376 // in upper case
8f520a56
MB
377 wxString colName = name;
378 colName.MakeUpper();
c50f92d0
VZ
379
380 // ... and we also allow both grey/gray
381 wxString colNameAlt = colName;
382 if ( !colNameAlt.Replace(_T("GRAY"), _T("GREY")) )
383 {
384 // but in this case it is not necessary so avoid extra search below
385 colNameAlt.clear();
386 }
8f520a56
MB
387
388 wxStringToColourHashMap::iterator it = m_map->find(colName);
c50f92d0
VZ
389 if ( it == m_map->end() && !colNameAlt.empty() )
390 it = m_map->find(colNameAlt);
8f520a56
MB
391 if ( it != m_map->end() )
392 {
c50f92d0
VZ
393 *(it->second) = colour;
394 }
395 else // new colour
396 {
5767e836 397 (*m_map)[colName] = new wxColour(colour);
8f520a56 398 }
8f520a56
MB
399}
400
c50f92d0 401wxColour wxColourDatabase::Find(const wxString& colour) const
c801d85f 402{
c50f92d0
VZ
403 wxColourDatabase * const self = wxConstCast(this, wxColourDatabase);
404 self->Initialize();
405
406 // first look among the existing colours
407
408 // make the comparaison case insensitive and also match both grey and gray
f6bcfd97
BP
409 wxString colName = colour;
410 colName.MakeUpper();
c50f92d0
VZ
411 wxString colNameAlt = colName;
412 if ( !colNameAlt.Replace(_T("GRAY"), _T("GREY")) )
413 colNameAlt.clear();
f6bcfd97 414
222ed1d6 415 wxStringToColourHashMap::iterator it = m_map->find(colName);
c50f92d0
VZ
416 if ( it == m_map->end() && !colNameAlt.empty() )
417 it = m_map->find(colNameAlt);
222ed1d6 418 if ( it != m_map->end() )
c50f92d0 419 return *(it->second);
34138703 420
4e6b8309
VZ
421 // if we didn't find it, query the system, maybe it knows about it
422#if defined(__WXGTK__) || defined(__X__)
423 wxColour col = wxColour::CreateByName(colour);
8bbe427f 424
c50f92d0
VZ
425 if ( col.Ok() )
426 {
427 // cache it
428 self->AddColour(colour, col);
429 }
c801d85f 430
c50f92d0
VZ
431 return col;
432#elif defined(__X__)
4e6b8309 433 // TODO: move this to wxColour::CreateByName()
c801d85f
KB
434 XColor xcolour;
435
2049ba38 436#ifdef __WXMOTIF__
46ccb510 437 Display *display = XtDisplay((Widget) wxTheApp->GetTopLevelWidget()) ;
c801d85f 438#endif
7266b672
JS
439#ifdef __WXX11__
440 Display* display = (Display*) wxGetDisplay();
1c4a764c 441#endif
c801d85f 442 /* MATTHEW: [4] Use wxGetMainColormap */
2b5f62a0 443 if (!XParseColor(display, (Colormap) wxTheApp->GetMainColormap((WXDisplay*) display), colour.ToAscii() ,&xcolour))
c50f92d0 444 return NULL;
c801d85f 445
0b5c0e1a
JS
446#if wxUSE_NANOX
447 unsigned char r = (unsigned char)(xcolour.red);
448 unsigned char g = (unsigned char)(xcolour.green);
449 unsigned char b = (unsigned char)(xcolour.blue);
450#else
c801d85f
KB
451 unsigned char r = (unsigned char)(xcolour.red >> 8);
452 unsigned char g = (unsigned char)(xcolour.green >> 8);
453 unsigned char b = (unsigned char)(xcolour.blue >> 8);
0b5c0e1a 454#endif
482ee397 455
c50f92d0 456 wxColour col(r, g, b);
8f520a56 457 AddColour(colour, col);
c801d85f
KB
458
459 return col;
c50f92d0
VZ
460#else // other platform
461 return wxNullColour;
462#endif // platforms
c801d85f
KB
463}
464
c50f92d0 465wxString wxColourDatabase::FindName(const wxColour& colour) const
c801d85f 466{
c50f92d0
VZ
467 wxColourDatabase * const self = wxConstCast(this, wxColourDatabase);
468 self->Initialize();
8bbe427f 469
222ed1d6
MB
470 typedef wxStringToColourHashMap::iterator iterator;
471
c50f92d0 472 for ( iterator it = m_map->begin(), en = m_map->end(); it != en; ++it )
a23fd0e1 473 {
c50f92d0 474 if ( *(it->second) == colour )
222ed1d6 475 return it->first;
a23fd0e1 476 }
c801d85f 477
222ed1d6 478 return wxEmptyString;
c801d85f
KB
479}
480
c50f92d0
VZ
481// ----------------------------------------------------------------------------
482// deprecated wxColourDatabase methods
483// ----------------------------------------------------------------------------
484
485wxColour *wxColourDatabase::FindColour(const wxString& name)
486{
98de2b68
DS
487 // This function is deprecated, use Find() instead.
488 // Formerly this function sometimes would return a deletable pointer and
489 // sometimes a non-deletable one (when returning a colour from the database).
490 // Trying to delete the latter anyway results in problems, so probably
491 // nobody ever freed the pointers. Currently it always returns a new
492 // instance, which means there will be memory leaks.
493 wxLogDebug(wxT("wxColourDataBase::FindColour():")
494 wxT(" Please use wxColourDataBase::Find() instead"));
495
492e2a5b
VZ
496 // using a static variable here is not the most elegant solution but unless
497 // we want to make wxStringToColourHashMap public (i.e. move it to the
498 // header) so that we could have a member function returning
499 // wxStringToColourHashMap::iterator, there is really no good way to do it
500 // otherwise
501 //
502 // and knowing that this function is going to disappear in the next release
503 // anyhow I don't want to waste time on this
98de2b68 504
492e2a5b
VZ
505 static wxColour s_col;
506
507 s_col = Find(name);
508 if ( !s_col.Ok() )
c50f92d0
VZ
509 return NULL;
510
98de2b68 511 return new wxColour(s_col);
c50f92d0
VZ
512}
513
514// ============================================================================
515// stock objects
516// ============================================================================
517
7ecb8b06
VZ
518void wxInitializeStockLists()
519{
1c193821 520 wxTheColourDatabase = new wxColourDatabase;
1c193821
JS
521
522 wxTheBrushList = new wxBrushList;
523 wxThePenList = new wxPenList;
524 wxTheFontList = new wxFontList;
525 wxTheBitmapList = new wxBitmapList;
a3622daa 526}
c801d85f 527
a3622daa
VZ
528void wxInitializeStockObjects ()
529{
2049ba38 530#ifdef __WXMOTIF__
c801d85f
KB
531#endif
532#ifdef __X__
46ccb510
JS
533 // TODO
534 // wxFontPool = new XFontPool;
c801d85f
KB
535#endif
536
696e1ea0 537 // why under MSW fonts shouldn't have the standard system size?
5100cabf 538/*
696e1ea0
VZ
539#ifdef __WXMSW__
540 static const int sizeFont = 10;
541#else
696e1ea0 542#endif
5100cabf 543*/
2fe57169 544#if defined(__WXMAC__)
b5c3e56d 545 // retrieve size of system font for all stock fonts
99fd0d71
SC
546 int sizeFont = 12;
547
99fd0d71
SC
548 Str255 fontName ;
549 SInt16 fontSize ;
550 Style fontStyle ;
551
5d3e7b52
WS
552 GetThemeFont(kThemeSystemFont , GetApplicationScript() , fontName , &fontSize , &fontStyle ) ;
553 sizeFont = fontSize ;
ca80fdee 554#ifdef __WXMAC_CLASSIC__
d0bfeea6 555 wxNORMAL_FONT = new wxFont (fontSize, wxMODERN, wxNORMAL, wxNORMAL , false , wxMacMakeStringFromPascal(fontName) );
b5c3e56d
SC
556#else
557 wxNORMAL_FONT = new wxFont () ;
558 wxNORMAL_FONT->MacCreateThemeFont( kThemeSystemFont );
559#endif
2fe57169 560#elif defined(__WXPM__)
5d3e7b52 561 static const int sizeFont = 12;
415ca026 562#else
5d3e7b52
WS
563 wxNORMAL_FONT = new wxFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
564 static const int sizeFont = wxNORMAL_FONT->GetPointSize();
415ca026 565#endif
696e1ea0 566
2fe57169 567#if defined(__WXPM__)
5d3e7b52
WS
568 /*
569 // Basic OS/2 has a fairly limited number of fonts and these are as good
570 // as I can do to get something that looks halfway "wx" normal
571 */
572 wxNORMAL_FONT = new wxFont (sizeFont, wxMODERN, wxNORMAL, wxBOLD);
573 wxSMALL_FONT = new wxFont (sizeFont - 4, wxSWISS, wxNORMAL, wxNORMAL); /* Helv */
574 wxITALIC_FONT = new wxFont (sizeFont, wxROMAN, wxITALIC, wxNORMAL);
575 wxSWISS_FONT = new wxFont (sizeFont, wxSWISS, wxNORMAL, wxNORMAL); /* Helv */
955e5433 576#elif defined(__WXMAC__)
d0bfeea6 577 wxSWISS_FONT = new wxFont (sizeFont, wxSWISS, wxNORMAL, wxNORMAL); /* Helv */
99fd0d71 578 wxITALIC_FONT = new wxFont (sizeFont, wxROMAN, wxITALIC, wxNORMAL);
ca80fdee 579#ifdef __WXMAC_CLASSIC__
5d3e7b52 580 GetThemeFont(kThemeSmallSystemFont , GetApplicationScript() , fontName , &fontSize , &fontStyle ) ;
44c44c82 581 wxSMALL_FONT = new wxFont (fontSize, wxSWISS, wxNORMAL, wxNORMAL , false , wxMacMakeStringFromPascal( fontName ) );
b5c3e56d
SC
582#else
583 wxSMALL_FONT = new wxFont () ;
584 wxSMALL_FONT->MacCreateThemeFont( kThemeSmallSystemFont );
585#endif
2fe57169 586#else
5d3e7b52
WS
587 wxSMALL_FONT = new wxFont (sizeFont - 2, wxSWISS, wxNORMAL, wxNORMAL);
588 wxITALIC_FONT = new wxFont (sizeFont, wxROMAN, wxITALIC, wxNORMAL);
589 wxSWISS_FONT = new wxFont (sizeFont, wxSWISS, wxNORMAL, wxNORMAL);
2fe57169 590#endif
c801d85f 591
5d3e7b52
WS
592 wxRED_PEN = new wxPen (wxT("RED"), 1, wxSOLID);
593 wxCYAN_PEN = new wxPen (wxT("CYAN"), 1, wxSOLID);
594 wxGREEN_PEN = new wxPen (wxT("GREEN"), 1, wxSOLID);
595 wxBLACK_PEN = new wxPen (wxT("BLACK"), 1, wxSOLID);
596 wxWHITE_PEN = new wxPen (wxT("WHITE"), 1, wxSOLID);
597 wxTRANSPARENT_PEN = new wxPen (wxT("BLACK"), 1, wxTRANSPARENT);
598 wxBLACK_DASHED_PEN = new wxPen (wxT("BLACK"), 1, wxSHORT_DASH);
599 wxGREY_PEN = new wxPen (wxT("GREY"), 1, wxSOLID);
600 wxMEDIUM_GREY_PEN = new wxPen (wxT("MEDIUM GREY"), 1, wxSOLID);
601 wxLIGHT_GREY_PEN = new wxPen (wxT("LIGHT GREY"), 1, wxSOLID);
602
603 wxBLUE_BRUSH = new wxBrush (wxT("BLUE"), wxSOLID);
604 wxGREEN_BRUSH = new wxBrush (wxT("GREEN"), wxSOLID);
605 wxWHITE_BRUSH = new wxBrush (wxT("WHITE"), wxSOLID);
606 wxBLACK_BRUSH = new wxBrush (wxT("BLACK"), wxSOLID);
607 wxTRANSPARENT_BRUSH = new wxBrush (wxT("BLACK"), wxTRANSPARENT);
608 wxCYAN_BRUSH = new wxBrush (wxT("CYAN"), wxSOLID);
609 wxRED_BRUSH = new wxBrush (wxT("RED"), wxSOLID);
610 wxGREY_BRUSH = new wxBrush (wxT("GREY"), wxSOLID);
611 wxMEDIUM_GREY_BRUSH = new wxBrush (wxT("MEDIUM GREY"), wxSOLID);
612 wxLIGHT_GREY_BRUSH = new wxBrush (wxT("LIGHT GREY"), wxSOLID);
613
614 wxBLACK = new wxColour (wxT("BLACK"));
615 wxWHITE = new wxColour (wxT("WHITE"));
616 wxRED = new wxColour (wxT("RED"));
617 wxBLUE = new wxColour (wxT("BLUE"));
618 wxGREEN = new wxColour (wxT("GREEN"));
619 wxCYAN = new wxColour (wxT("CYAN"));
620 wxLIGHT_GREY = new wxColour (wxT("LIGHT GREY"));
621
622 wxSTANDARD_CURSOR = new wxCursor (wxCURSOR_ARROW);
623 wxHOURGLASS_CURSOR = new wxCursor (wxCURSOR_WAIT);
624 wxCROSS_CURSOR = new wxCursor (wxCURSOR_CROSS);
c801d85f
KB
625}
626
8bbe427f 627void wxDeleteStockObjects ()
c801d85f 628{
5d3e7b52
WS
629 wxDELETE(wxNORMAL_FONT);
630 wxDELETE(wxSMALL_FONT);
631 wxDELETE(wxITALIC_FONT);
632 wxDELETE(wxSWISS_FONT);
633
634 wxDELETE(wxRED_PEN);
635 wxDELETE(wxCYAN_PEN);
636 wxDELETE(wxGREEN_PEN);
637 wxDELETE(wxBLACK_PEN);
638 wxDELETE(wxWHITE_PEN);
639 wxDELETE(wxTRANSPARENT_PEN);
640 wxDELETE(wxBLACK_DASHED_PEN);
641 wxDELETE(wxGREY_PEN);
642 wxDELETE(wxMEDIUM_GREY_PEN);
643 wxDELETE(wxLIGHT_GREY_PEN);
644
645 wxDELETE(wxBLUE_BRUSH);
646 wxDELETE(wxGREEN_BRUSH);
647 wxDELETE(wxWHITE_BRUSH);
648 wxDELETE(wxBLACK_BRUSH);
649 wxDELETE(wxTRANSPARENT_BRUSH);
650 wxDELETE(wxCYAN_BRUSH);
651 wxDELETE(wxRED_BRUSH);
652 wxDELETE(wxGREY_BRUSH);
653 wxDELETE(wxMEDIUM_GREY_BRUSH);
654 wxDELETE(wxLIGHT_GREY_BRUSH);
655
656 wxDELETE(wxBLACK);
657 wxDELETE(wxWHITE);
658 wxDELETE(wxRED);
659 wxDELETE(wxBLUE);
660 wxDELETE(wxGREEN);
661 wxDELETE(wxCYAN);
662 wxDELETE(wxLIGHT_GREY);
663
664 wxDELETE(wxSTANDARD_CURSOR);
665 wxDELETE(wxHOURGLASS_CURSOR);
666 wxDELETE(wxCROSS_CURSOR);
a3622daa
VZ
667}
668
7ecb8b06
VZ
669void wxDeleteStockLists()
670{
5d3e7b52
WS
671 wxDELETE(wxTheBrushList);
672 wxDELETE(wxThePenList);
673 wxDELETE(wxTheFontList);
674 wxDELETE(wxTheBitmapList);
c801d85f
KB
675}
676
7ecb8b06
VZ
677// ============================================================================
678// wxTheXXXList stuff (semi-obsolete)
679// ============================================================================
680
6a6c0a8b 681wxBitmapList::~wxBitmapList ()
c801d85f 682{
5d3e7b52
WS
683 wxList::compatibility_iterator node = GetFirst ();
684 while (node)
c801d85f 685 {
5d3e7b52
WS
686 wxBitmap *bitmap = (wxBitmap *) node->GetData ();
687 wxList::compatibility_iterator next = node->GetNext ();
688 if (bitmap->GetVisible())
689 delete bitmap;
690 node = next;
c801d85f
KB
691 }
692}
693
694// Pen and Brush lists
6a6c0a8b 695wxPenList::~wxPenList ()
c801d85f 696{
5d3e7b52
WS
697 wxList::compatibility_iterator node = GetFirst ();
698 while (node)
c801d85f 699 {
5d3e7b52
WS
700 wxPen *pen = (wxPen *) node->GetData ();
701 wxList::compatibility_iterator next = node->GetNext ();
702 if (pen->GetVisible())
703 delete pen;
704 node = next;
c801d85f
KB
705 }
706}
707
708void wxPenList::AddPen (wxPen * pen)
709{
5d3e7b52 710 Append (pen);
c801d85f
KB
711}
712
713void wxPenList::RemovePen (wxPen * pen)
714{
5d3e7b52 715 DeleteObject (pen);
c801d85f
KB
716}
717
debe6624 718wxPen *wxPenList::FindOrCreatePen (const wxColour& colour, int width, int style)
c801d85f 719{
222ed1d6 720 for (wxList::compatibility_iterator node = GetFirst (); node; node = node->GetNext ())
13b6e335 721 {
b1d4dd7a 722 wxPen *each_pen = (wxPen *) node->GetData ();
13b6e335
VZ
723 if (each_pen &&
724 each_pen->GetVisible() &&
725 each_pen->GetWidth () == width &&
726 each_pen->GetStyle () == style &&
727 each_pen->GetColour ().Red () == colour.Red () &&
728 each_pen->GetColour ().Green () == colour.Green () &&
729 each_pen->GetColour ().Blue () == colour.Blue ())
730 return each_pen;
731 }
732
733 wxPen *pen = new wxPen (colour, width, style);
734 if ( !pen->Ok() )
c801d85f 735 {
13b6e335
VZ
736 // don't save the invalid pens in the list
737 delete pen;
738
739 return NULL;
c801d85f 740 }
c801d85f 741
7ecb8b06
VZ
742 AddPen(pen);
743
744 // we'll delete it ourselves later
5d3e7b52 745 pen->SetVisible(true);
6d167489 746
13b6e335 747 return pen;
c801d85f
KB
748}
749
6a6c0a8b 750wxBrushList::~wxBrushList ()
c801d85f 751{
5d3e7b52
WS
752 wxList::compatibility_iterator node = GetFirst ();
753 while (node)
c801d85f 754 {
5d3e7b52
WS
755 wxBrush *brush = (wxBrush *) node->GetData ();
756 wxList::compatibility_iterator next = node->GetNext ();
757 if (brush && brush->GetVisible())
758 delete brush;
759 node = next;
c801d85f
KB
760 }
761}
762
763void wxBrushList::AddBrush (wxBrush * brush)
764{
5d3e7b52 765 Append (brush);
c801d85f
KB
766}
767
debe6624 768wxBrush *wxBrushList::FindOrCreateBrush (const wxColour& colour, int style)
c801d85f 769{
222ed1d6 770 for (wxList::compatibility_iterator node = GetFirst (); node; node = node->GetNext ())
c801d85f 771 {
b1d4dd7a 772 wxBrush *each_brush = (wxBrush *) node->GetData ();
13b6e335
VZ
773 if (each_brush &&
774 each_brush->GetVisible() &&
775 each_brush->GetStyle () == style &&
776 each_brush->GetColour ().Red () == colour.Red () &&
777 each_brush->GetColour ().Green () == colour.Green () &&
778 each_brush->GetColour ().Blue () == colour.Blue ())
779 return each_brush;
c801d85f 780 }
6d167489 781
13b6e335
VZ
782 wxBrush *brush = new wxBrush (colour, style);
783
784 if ( !brush->Ok() )
785 {
786 // don't put the brushes we failed to create into the list
787 delete brush;
788
789 return NULL;
790 }
6d167489 791
7ecb8b06
VZ
792 AddBrush(brush);
793
794 // we'll delete it ourselves later
5d3e7b52 795 brush->SetVisible(true);
6d167489 796
13b6e335 797 return brush;
c801d85f
KB
798}
799
c801d85f
KB
800void wxBrushList::RemoveBrush (wxBrush * brush)
801{
5d3e7b52 802 DeleteObject (brush);
c801d85f
KB
803}
804
6a6c0a8b 805wxFontList::~wxFontList ()
c801d85f 806{
222ed1d6 807 wxList::compatibility_iterator node = GetFirst ();
6d167489 808 while (node)
c801d85f 809 {
6d167489
VZ
810 // Only delete objects that are 'visible', i.e.
811 // that have been created using FindOrCreate...,
812 // where the pointers are expected to be shared
813 // (and therefore not deleted by any one part of an app).
b1d4dd7a 814 wxFont *font = (wxFont *) node->GetData ();
222ed1d6 815 wxList::compatibility_iterator next = node->GetNext ();
6d167489
VZ
816 if (font->GetVisible())
817 delete font;
818 node = next;
819 }
c801d85f
KB
820}
821
822void wxFontList::AddFont (wxFont * font)
823{
5d3e7b52 824 Append (font);
c801d85f
KB
825}
826
827void wxFontList::RemoveFont (wxFont * font)
828{
5d3e7b52 829 DeleteObject (font);
c801d85f
KB
830}
831
f6bcfd97
BP
832wxFont *wxFontList::FindOrCreateFont(int pointSize,
833 int family,
834 int style,
835 int weight,
836 bool underline,
837 const wxString& facename,
838 wxFontEncoding encoding)
c801d85f 839{
f6bcfd97 840 wxFont *font = (wxFont *)NULL;
222ed1d6 841 wxList::compatibility_iterator node;
b1d4dd7a 842 for ( node = GetFirst(); node; node = node->GetNext() )
c801d85f 843 {
b1d4dd7a 844 font = (wxFont *)node->GetData();
f6bcfd97
BP
845 if ( font->GetVisible() &&
846 font->Ok() &&
847 font->GetPointSize () == pointSize &&
848 font->GetStyle () == style &&
849 font->GetWeight () == weight &&
850 font->GetUnderlined () == underline )
851 {
852 int fontFamily = font->GetFamily();
853
619d0528 854#if defined(__WXGTK__)
f6bcfd97
BP
855 // under GTK the default family is wxSWISS, so looking for a font
856 // with wxDEFAULT family should return a wxSWISS one instead of
857 // creating a new one
858 bool same = (fontFamily == family) ||
859 (fontFamily == wxSWISS && family == wxDEFAULT);
860#else // !GTK
861 // VZ: but why elsewhere do we require an exact match? mystery...
862 bool same = fontFamily == family;
863#endif // GTK/!GTK
864
865 // empty facename matches anything at all: this is bad because
866 // depending on which fonts are already created, we might get back
867 // a different font if we create it with empty facename, but it is
868 // still better than never matching anything in the cache at all
869 // in this case
8d8fbb9d 870 if ( same && !facename.empty() )
f6bcfd97
BP
871 {
872 const wxString& fontFace = font->GetFaceName();
873
874 // empty facename matches everything
875 same = !fontFace || fontFace == facename;
876 }
877
878 if ( same && (encoding != wxFONTENCODING_DEFAULT) )
879 {
880 // have to match the encoding too
881 same = font->GetEncoding() == encoding;
882 }
883
884 if ( same )
885 {
886 return font;
887 }
888 }
c801d85f 889 }
6d167489 890
f6bcfd97
BP
891 if ( !node )
892 {
893 // font not found, create the new one
894 font = new wxFont(pointSize, family, style, weight,
895 underline, facename, encoding);
896
7ecb8b06
VZ
897 AddFont(font);
898
f6bcfd97 899 // and mark it as being cacheable
5d3e7b52 900 font->SetVisible(true);
f6bcfd97 901 }
6d167489 902
f6bcfd97 903 return font;
c801d85f
KB
904}
905
906void wxBitmapList::AddBitmap(wxBitmap *bitmap)
8bbe427f
VZ
907{
908 Append(bitmap);
909}
910
c801d85f 911void wxBitmapList::RemoveBitmap(wxBitmap *bitmap)
8bbe427f
VZ
912{
913 DeleteObject(bitmap);
914}
c801d85f 915
6a6c0a8b
JS
916wxSize wxGetDisplaySize()
917{
918 int x, y;
919 wxDisplaySize(& x, & y);
920 return wxSize(x, y);
921}
922
ec5d7799
RD
923wxRect wxGetClientDisplayRect()
924{
925 int x, y, width, height;
926 wxClientDisplayRect(&x, &y, &width, &height); // call plat-specific version
927 return wxRect(x, y, width, height);
928}
929
904a68b6
RL
930wxSize wxGetDisplaySizeMM()
931{
932 int x, y;
933 wxDisplaySizeMM(& x, & y);
934 return wxSize(x, y);
935}
936
8bbe427f
VZ
937wxResourceCache::~wxResourceCache ()
938{
222ed1d6 939 wxList::compatibility_iterator node = GetFirst ();
f6bcfd97 940 while (node) {
b1d4dd7a 941 wxObject *item = (wxObject *)node->GetData();
f6bcfd97 942 delete item;
a3622daa 943
b1d4dd7a 944 node = node->GetNext ();
a3622daa 945 }
a3622daa
VZ
946}
947