]> git.saurik.com Git - wxWidgets.git/blame - src/common/gdicmn.cpp
Use wxWindowID in wxNewId() and related functions.
[wxWidgets.git] / src / common / gdicmn.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
ca3e85cf 2// Name: src/common/gdicmn.cpp
c801d85f
KB
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
c801d85f
KB
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
670f9935 16 #pragma hdrstop
c801d85f
KB
17#endif
18
19#include "wx/gdicmn.h"
5819f832 20#include "wx/gdiobj.h"
e4db172a
WS
21
22#ifndef WX_PRECOMP
23 #include "wx/log.h"
f5590243 24 #include "wx/pen.h"
c64755ed 25 #include "wx/brush.h"
559a723c 26 #include "wx/palette.h"
923d28da 27 #include "wx/icon.h"
52734360 28 #include "wx/iconbndl.h"
c8326d64 29 #include "wx/cursor.h"
9eddec69 30 #include "wx/settings.h"
0bca0373 31 #include "wx/bitmap.h"
7cf41a5d 32 #include "wx/colour.h"
48a1108e 33 #include "wx/font.h"
d43be80d 34 #include "wx/math.h"
e4db172a
WS
35#endif
36
5819f832 37
8f884a0d 38IMPLEMENT_ABSTRACT_CLASS(wxGDIObject, wxObject)
5819f832
VS
39
40
e7445ff8
PC
41WXDLLIMPEXP_DATA_CORE(wxBrushList*) wxTheBrushList;
42WXDLLIMPEXP_DATA_CORE(wxFontList*) wxTheFontList;
43WXDLLIMPEXP_DATA_CORE(wxPenList*) wxThePenList;
44
45WXDLLIMPEXP_DATA_CORE(wxColourDatabase*) wxTheColourDatabase;
46
47WXDLLIMPEXP_DATA_CORE(wxBitmap) wxNullBitmap;
48WXDLLIMPEXP_DATA_CORE(wxBrush) wxNullBrush;
49WXDLLIMPEXP_DATA_CORE(wxColour) wxNullColour;
50WXDLLIMPEXP_DATA_CORE(wxCursor) wxNullCursor;
51WXDLLIMPEXP_DATA_CORE(wxFont) wxNullFont;
52WXDLLIMPEXP_DATA_CORE(wxIcon) wxNullIcon;
53WXDLLIMPEXP_DATA_CORE(wxPen) wxNullPen;
54#if wxUSE_PALETTE
55WXDLLIMPEXP_DATA_CORE(wxPalette) wxNullPalette;
46ccb510 56#endif
52734360 57WXDLLIMPEXP_DATA_CORE(wxIconBundle) wxNullIconBundle;
46ccb510 58
e7445ff8
PC
59const wxSize wxDefaultSize(wxDefaultCoord, wxDefaultCoord);
60const wxPoint wxDefaultPosition(wxDefaultCoord, wxDefaultCoord);
7266b672 61
b0d7707b 62#include "wx/listimpl.cpp"
a76c2f37 63WX_DEFINE_LIST(wxPointList)
b0d7707b
RR
64
65
cb73e600
SC
66#if wxUSE_EXTENDED_RTTI
67
68// wxPoint
69
70template<> void wxStringReadValue(const wxString &s , wxPoint &data )
71{
5d3e7b52 72 wxSscanf(s, wxT("%d,%d"), &data.x , &data.y ) ;
cb73e600
SC
73}
74
75template<> void wxStringWriteValue(wxString &s , const wxPoint &data )
76{
5d3e7b52 77 s = wxString::Format(wxT("%d,%d"), data.x , data.y ) ;
cb73e600
SC
78}
79
8805dbab 80wxCUSTOM_TYPE_INFO(wxPoint, wxToStringConverter<wxPoint> , wxFromStringConverter<wxPoint>)
cb73e600
SC
81
82template<> void wxStringReadValue(const wxString &s , wxSize &data )
83{
5d3e7b52 84 wxSscanf(s, wxT("%d,%d"), &data.x , &data.y ) ;
cb73e600
SC
85}
86
87template<> void wxStringWriteValue(wxString &s , const wxSize &data )
88{
5d3e7b52 89 s = wxString::Format(wxT("%d,%d"), data.x , data.y ) ;
cb73e600
SC
90}
91
8805dbab 92wxCUSTOM_TYPE_INFO(wxSize, wxToStringConverter<wxSize> , wxFromStringConverter<wxSize>)
cb73e600
SC
93
94#endif
95
d7a7546b 96wxRect::wxRect(const wxPoint& point1, const wxPoint& point2)
c801d85f 97{
5d3e7b52
WS
98 x = point1.x;
99 y = point1.y;
100 width = point2.x - point1.x;
101 height = point2.y - point1.y;
8bbe427f 102
5d3e7b52
WS
103 if (width < 0)
104 {
105 width = -width;
106 x = point2.x;
107 }
108 width++;
8bbe427f 109
5d3e7b52
WS
110 if (height < 0)
111 {
112 height = -height;
113 y = point2.y;
114 }
115 height++;
c801d85f
KB
116}
117
df83b840
VZ
118wxRect& wxRect::Union(const wxRect& rect)
119{
c71ce675
VZ
120 // ignore empty rectangles: union with an empty rectangle shouldn't extend
121 // this one to (0, 0)
122 if ( !width || !height )
123 {
124 *this = rect;
125 }
126 else if ( rect.width && rect.height )
df83b840
VZ
127 {
128 int x1 = wxMin(x, rect.x);
129 int y1 = wxMin(y, rect.y);
130 int y2 = wxMax(y + height, rect.height + rect.y);
131 int x2 = wxMax(x + width, rect.width + rect.x);
132
133 x = x1;
134 y = y1;
135 width = x2 - x1;
136 height = y2 - y1;
137 }
c71ce675 138 //else: we're not empty and rect is empty
df83b840
VZ
139
140 return *this;
141}
142
1e6feb95
VZ
143wxRect& wxRect::Inflate(wxCoord dx, wxCoord dy)
144{
8673a125
VZ
145 if (-2*dx>width)
146 {
147 // Don't allow deflate to eat more width than we have,
148 // a well-defined rectangle cannot have negative width.
149 x+=width/2;
150 width=0;
151 }
152 else
153 {
154 // The inflate is valid.
155 x-=dx;
156 width+=2*dx;
157 }
158
159 if (-2*dy>height)
160 {
161 // Don't allow deflate to eat more height than we have,
162 // a well-defined rectangle cannot have negative height.
163 y+=height/2;
164 height=0;
165 }
166 else
167 {
168 // The inflate is valid.
169 y-=dy;
170 height+=2*dy;
171 }
1e6feb95
VZ
172
173 return *this;
174}
175
22a35096 176bool wxRect::Contains(int cx, int cy) const
dcb44466 177{
45816ddd
VZ
178 return ( (cx >= x) && (cy >= y)
179 && ((cy - y) < height)
180 && ((cx - x) < width)
181 );
dcb44466
JS
182}
183
22a35096 184bool wxRect::Contains(const wxRect& rect) const
869b59fc 185{
22a35096 186 return Contains(rect.GetTopLeft()) && Contains(rect.GetBottomRight());
869b59fc
VS
187}
188
1e6feb95
VZ
189wxRect& wxRect::Intersect(const wxRect& rect)
190{
191 int x2 = GetRight(),
192 y2 = GetBottom();
193
194 if ( x < rect.x )
195 x = rect.x;
196 if ( y < rect.y )
197 y = rect.y;
198 if ( x2 > rect.GetRight() )
199 x2 = rect.GetRight();
200 if ( y2 > rect.GetBottom() )
201 y2 = rect.GetBottom();
202
203 width = x2 - x + 1;
204 height = y2 - y + 1;
205
206 if ( width <= 0 || height <= 0 )
207 {
208 width =
209 height = 0;
210 }
211
212 return *this;
213}
214
215bool wxRect::Intersects(const wxRect& rect) const
216{
217 wxRect r = Intersect(rect);
218
219 // if there is no intersection, both width and height are 0
220 return r.width != 0;
221}
222
bc5e942b
VZ
223wxRect& wxRect::operator+=(const wxRect& rect)
224{
225 *this = *this + rect;
226 return *this;
227}
228
229
230wxRect& wxRect::operator*=(const wxRect& rect)
231{
232 *this = *this * rect;
233 return *this;
234}
235
236
237wxRect operator+(const wxRect& r1, const wxRect& r2)
238{
239 int x1 = wxMin(r1.x, r2.x);
240 int y1 = wxMin(r1.y, r2.y);
241 int y2 = wxMax(r1.y+r1.height, r2.height+r2.y);
242 int x2 = wxMax(r1.x+r1.width, r2.width+r2.x);
243 return wxRect(x1, y1, x2-x1, y2-y1);
244}
245
246wxRect operator*(const wxRect& r1, const wxRect& r2)
247{
248 int x1 = wxMax(r1.x, r2.x);
249 int y1 = wxMax(r1.y, r2.y);
250 int y2 = wxMin(r1.y+r1.height, r2.height+r2.y);
251 int x2 = wxMin(r1.x+r1.width, r2.width+r2.x);
252 return wxRect(x1, y1, x2-x1, y2-y1);
253}
254
a5664fd6 255wxRealPoint::wxRealPoint(const wxPoint& pt)
ce00f59b 256 : x(pt.x), y(pt.y)
a5664fd6
FM
257{
258}
259
c50f92d0
VZ
260// ============================================================================
261// wxColourDatabase
262// ============================================================================
263
c50f92d0
VZ
264// ----------------------------------------------------------------------------
265// wxColourDatabase ctor/dtor
266// ----------------------------------------------------------------------------
222ed1d6
MB
267
268wxColourDatabase::wxColourDatabase ()
c801d85f 269{
c50f92d0
VZ
270 // will be created on demand in Initialize()
271 m_map = NULL;
c801d85f
KB
272}
273
6a6c0a8b 274wxColourDatabase::~wxColourDatabase ()
c801d85f 275{
c50f92d0
VZ
276 if ( m_map )
277 {
278 WX_CLEAR_HASH_MAP(wxStringToColourHashMap, *m_map);
279
280 delete m_map;
281 }
222ed1d6 282
19bf0c69
DW
283#ifdef __WXPM__
284 delete [] m_palTable;
285#endif
c801d85f
KB
286}
287
288// Colour database stuff
c50f92d0 289void wxColourDatabase::Initialize()
c801d85f 290{
c50f92d0
VZ
291 if ( m_map )
292 {
293 // already initialized
294 return;
295 }
296
297 m_map = new wxStringToColourHashMap;
298
f6bcfd97
BP
299 static const struct wxColourDesc
300 {
301 const wxChar *name;
5c519b6c 302 unsigned char r,g,b;
f6bcfd97
BP
303 }
304 wxColourTable[] =
305 {
5e2ab1ea 306 {wxT("AQUAMARINE"),112, 219, 147},
f6bcfd97
BP
307 {wxT("BLACK"),0, 0, 0},
308 {wxT("BLUE"), 0, 0, 255},
5e2ab1ea 309 {wxT("BLUE VIOLET"), 159, 95, 159},
f6bcfd97 310 {wxT("BROWN"), 165, 42, 42},
5e2ab1ea
VZ
311 {wxT("CADET BLUE"), 95, 159, 159},
312 {wxT("CORAL"), 255, 127, 0},
313 {wxT("CORNFLOWER BLUE"), 66, 66, 111},
f6bcfd97 314 {wxT("CYAN"), 0, 255, 255},
5e2ab1ea
VZ
315 {wxT("DARK GREY"), 47, 47, 47}, // ?
316
317 {wxT("DARK GREEN"), 47, 79, 47},
318 {wxT("DARK OLIVE GREEN"), 79, 79, 47},
f6bcfd97 319 {wxT("DARK ORCHID"), 153, 50, 204},
5e2ab1ea 320 {wxT("DARK SLATE BLUE"), 107, 35, 142},
f6bcfd97 321 {wxT("DARK SLATE GREY"), 47, 79, 79},
5e2ab1ea
VZ
322 {wxT("DARK TURQUOISE"), 112, 147, 219},
323 {wxT("DIM GREY"), 84, 84, 84},
324 {wxT("FIREBRICK"), 142, 35, 35},
325 {wxT("FOREST GREEN"), 35, 142, 35},
326 {wxT("GOLD"), 204, 127, 50},
327 {wxT("GOLDENROD"), 219, 219, 112},
328 {wxT("GREY"), 128, 128, 128},
f6bcfd97 329 {wxT("GREEN"), 0, 255, 0},
5e2ab1ea
VZ
330 {wxT("GREEN YELLOW"), 147, 219, 112},
331 {wxT("INDIAN RED"), 79, 47, 47},
332 {wxT("KHAKI"), 159, 159, 95},
333 {wxT("LIGHT BLUE"), 191, 216, 216},
334 {wxT("LIGHT GREY"), 192, 192, 192},
335 {wxT("LIGHT STEEL BLUE"), 143, 143, 188},
336 {wxT("LIME GREEN"), 50, 204, 50},
337 {wxT("LIGHT MAGENTA"), 255, 0, 255},
f6bcfd97 338 {wxT("MAGENTA"), 255, 0, 255},
5e2ab1ea
VZ
339 {wxT("MAROON"), 142, 35, 107},
340 {wxT("MEDIUM AQUAMARINE"), 50, 204, 153},
341 {wxT("MEDIUM GREY"), 100, 100, 100},
342 {wxT("MEDIUM BLUE"), 50, 50, 204},
343 {wxT("MEDIUM FOREST GREEN"), 107, 142, 35},
344 {wxT("MEDIUM GOLDENROD"), 234, 234, 173},
345 {wxT("MEDIUM ORCHID"), 147, 112, 219},
346 {wxT("MEDIUM SEA GREEN"), 66, 111, 66},
347 {wxT("MEDIUM SLATE BLUE"), 127, 0, 255},
348 {wxT("MEDIUM SPRING GREEN"), 127, 255, 0},
349 {wxT("MEDIUM TURQUOISE"), 112, 219, 219},
350 {wxT("MEDIUM VIOLET RED"), 219, 112, 147},
351 {wxT("MIDNIGHT BLUE"), 47, 47, 79},
352 {wxT("NAVY"), 35, 35, 142},
353 {wxT("ORANGE"), 204, 50, 50},
354 {wxT("ORANGE RED"), 255, 0, 127},
355 {wxT("ORCHID"), 219, 112, 219},
356 {wxT("PALE GREEN"), 143, 188, 143},
59571b71 357 {wxT("PINK"), 255, 192, 203},
5e2ab1ea
VZ
358 {wxT("PLUM"), 234, 173, 234},
359 {wxT("PURPLE"), 176, 0, 255},
f6bcfd97 360 {wxT("RED"), 255, 0, 0},
5e2ab1ea
VZ
361 {wxT("SALMON"), 111, 66, 66},
362 {wxT("SEA GREEN"), 35, 142, 107},
363 {wxT("SIENNA"), 142, 107, 35},
364 {wxT("SKY BLUE"), 50, 153, 204},
365 {wxT("SLATE BLUE"), 0, 127, 255},
f6bcfd97 366 {wxT("SPRING GREEN"), 0, 255, 127},
5e2ab1ea
VZ
367 {wxT("STEEL BLUE"), 35, 107, 142},
368 {wxT("TAN"), 219, 147, 112},
f6bcfd97 369 {wxT("THISTLE"), 216, 191, 216},
5e2ab1ea
VZ
370 {wxT("TURQUOISE"), 173, 234, 234},
371 {wxT("VIOLET"), 79, 47, 79},
372 {wxT("VIOLET RED"), 204, 50, 153},
373 {wxT("WHEAT"), 216, 216, 191},
f6bcfd97
BP
374 {wxT("WHITE"), 255, 255, 255},
375 {wxT("YELLOW"), 255, 255, 0},
aad6765c 376 {wxT("YELLOW GREEN"), 153, 204, 50}
f6bcfd97
BP
377 };
378
c50f92d0 379 size_t n;
bf2c4b94
DW
380
381 for ( n = 0; n < WXSIZEOF(wxColourTable); n++ )
f6bcfd97
BP
382 {
383 const wxColourDesc& cc = wxColourTable[n];
c50f92d0 384 (*m_map)[cc.name] = new wxColour(cc.r, cc.g, cc.b);
f6bcfd97 385 }
c50f92d0 386
19bf0c69
DW
387#ifdef __WXPM__
388 m_palTable = new long[n];
389 for ( n = 0; n < WXSIZEOF(wxColourTable); n++ )
390 {
391 const wxColourDesc& cc = wxColourTable[n];
392 m_palTable[n] = OS2RGB(cc.r,cc.g,cc.b);
393 }
394 m_nSize = n;
395#endif
c801d85f
KB
396}
397
c50f92d0
VZ
398// ----------------------------------------------------------------------------
399// wxColourDatabase operations
400// ----------------------------------------------------------------------------
222ed1d6 401
c50f92d0 402void wxColourDatabase::AddColour(const wxString& name, const wxColour& colour)
222ed1d6 403{
c50f92d0 404 Initialize();
222ed1d6 405
c50f92d0
VZ
406 // canonicalize the colour names before using them as keys: they should be
407 // in upper case
8f520a56
MB
408 wxString colName = name;
409 colName.MakeUpper();
c50f92d0
VZ
410
411 // ... and we also allow both grey/gray
412 wxString colNameAlt = colName;
9a83f860 413 if ( !colNameAlt.Replace(wxT("GRAY"), wxT("GREY")) )
c50f92d0
VZ
414 {
415 // but in this case it is not necessary so avoid extra search below
416 colNameAlt.clear();
417 }
8f520a56
MB
418
419 wxStringToColourHashMap::iterator it = m_map->find(colName);
c50f92d0
VZ
420 if ( it == m_map->end() && !colNameAlt.empty() )
421 it = m_map->find(colNameAlt);
8f520a56
MB
422 if ( it != m_map->end() )
423 {
c50f92d0
VZ
424 *(it->second) = colour;
425 }
426 else // new colour
427 {
5767e836 428 (*m_map)[colName] = new wxColour(colour);
8f520a56 429 }
8f520a56
MB
430}
431
c50f92d0 432wxColour wxColourDatabase::Find(const wxString& colour) const
c801d85f 433{
c50f92d0
VZ
434 wxColourDatabase * const self = wxConstCast(this, wxColourDatabase);
435 self->Initialize();
436
c50f92d0 437 // make the comparaison case insensitive and also match both grey and gray
f6bcfd97
BP
438 wxString colName = colour;
439 colName.MakeUpper();
c50f92d0 440 wxString colNameAlt = colName;
9a83f860 441 if ( !colNameAlt.Replace(wxT("GRAY"), wxT("GREY")) )
c50f92d0 442 colNameAlt.clear();
f6bcfd97 443
222ed1d6 444 wxStringToColourHashMap::iterator it = m_map->find(colName);
c50f92d0
VZ
445 if ( it == m_map->end() && !colNameAlt.empty() )
446 it = m_map->find(colNameAlt);
222ed1d6 447 if ( it != m_map->end() )
c50f92d0 448 return *(it->second);
34138703 449
40989e46
WS
450 // we did not find any result in existing colours:
451 // we won't use wxString -> wxColour conversion because the
452 // wxColour::Set(const wxString &) function which does that conversion
453 // internally uses this function (wxColourDatabase::Find) and we want
454 // to avoid infinite recursion !
c50f92d0 455 return wxNullColour;
c801d85f
KB
456}
457
c50f92d0 458wxString wxColourDatabase::FindName(const wxColour& colour) const
c801d85f 459{
c50f92d0
VZ
460 wxColourDatabase * const self = wxConstCast(this, wxColourDatabase);
461 self->Initialize();
8bbe427f 462
222ed1d6
MB
463 typedef wxStringToColourHashMap::iterator iterator;
464
c50f92d0 465 for ( iterator it = m_map->begin(), en = m_map->end(); it != en; ++it )
a23fd0e1 466 {
c50f92d0 467 if ( *(it->second) == colour )
222ed1d6 468 return it->first;
a23fd0e1 469 }
c801d85f 470
222ed1d6 471 return wxEmptyString;
c801d85f
KB
472}
473
c50f92d0
VZ
474// ----------------------------------------------------------------------------
475// deprecated wxColourDatabase methods
476// ----------------------------------------------------------------------------
477
ca3e85cf 478#if WXWIN_COMPATIBILITY_2_6
c50f92d0
VZ
479wxColour *wxColourDatabase::FindColour(const wxString& name)
480{
98de2b68
DS
481 // This function is deprecated, use Find() instead.
482 // Formerly this function sometimes would return a deletable pointer and
483 // sometimes a non-deletable one (when returning a colour from the database).
484 // Trying to delete the latter anyway results in problems, so probably
485 // nobody ever freed the pointers. Currently it always returns a new
486 // instance, which means there will be memory leaks.
487 wxLogDebug(wxT("wxColourDataBase::FindColour():")
488 wxT(" Please use wxColourDataBase::Find() instead"));
489
492e2a5b
VZ
490 // using a static variable here is not the most elegant solution but unless
491 // we want to make wxStringToColourHashMap public (i.e. move it to the
492 // header) so that we could have a member function returning
493 // wxStringToColourHashMap::iterator, there is really no good way to do it
494 // otherwise
495 //
496 // and knowing that this function is going to disappear in the next release
497 // anyhow I don't want to waste time on this
98de2b68 498
492e2a5b
VZ
499 static wxColour s_col;
500
501 s_col = Find(name);
a1b806b9 502 if ( !s_col.IsOk() )
c50f92d0
VZ
503 return NULL;
504
98de2b68 505 return new wxColour(s_col);
c50f92d0 506}
ca3e85cf 507#endif // WXWIN_COMPATIBILITY_2_6
c50f92d0
VZ
508
509// ============================================================================
510// stock objects
511// ============================================================================
512
f516d986
VZ
513static wxStockGDI gs_wxStockGDI_instance;
514wxStockGDI* wxStockGDI::ms_instance = &gs_wxStockGDI_instance;
515wxObject* wxStockGDI::ms_stockObject[ITEMCOUNT];
516
517wxStockGDI::wxStockGDI()
7ecb8b06 518{
f516d986 519}
1c193821 520
f516d986
VZ
521wxStockGDI::~wxStockGDI()
522{
a3622daa 523}
c801d85f 524
f516d986 525void wxStockGDI::DeleteAll()
a3622daa 526{
f516d986
VZ
527 for (unsigned i = 0; i < ITEMCOUNT; i++)
528 {
5276b0a5 529 wxDELETE(ms_stockObject[i]);
f516d986
VZ
530 }
531}
c801d85f 532
f516d986
VZ
533const wxBrush* wxStockGDI::GetBrush(Item item)
534{
5c33522f 535 wxBrush* brush = static_cast<wxBrush*>(ms_stockObject[item]);
f516d986
VZ
536 if (brush == NULL)
537 {
538 switch (item)
539 {
540 case BRUSH_BLACK:
04ee05f9 541 brush = new wxBrush(*GetColour(COLOUR_BLACK), wxBRUSHSTYLE_SOLID);
f516d986
VZ
542 break;
543 case BRUSH_BLUE:
04ee05f9 544 brush = new wxBrush(*GetColour(COLOUR_BLUE), wxBRUSHSTYLE_SOLID);
f516d986
VZ
545 break;
546 case BRUSH_CYAN:
04ee05f9 547 brush = new wxBrush(*GetColour(COLOUR_CYAN), wxBRUSHSTYLE_SOLID);
f516d986
VZ
548 break;
549 case BRUSH_GREEN:
04ee05f9 550 brush = new wxBrush(*GetColour(COLOUR_GREEN), wxBRUSHSTYLE_SOLID);
f516d986 551 break;
86028025
FM
552 case BRUSH_YELLOW:
553 brush = new wxBrush(*GetColour(COLOUR_YELLOW), wxBRUSHSTYLE_SOLID);
554 break;
f516d986 555 case BRUSH_GREY:
04ee05f9 556 brush = new wxBrush(wxColour(wxT("GREY")), wxBRUSHSTYLE_SOLID);
f516d986
VZ
557 break;
558 case BRUSH_LIGHTGREY:
04ee05f9 559 brush = new wxBrush(*GetColour(COLOUR_LIGHTGREY), wxBRUSHSTYLE_SOLID);
f516d986
VZ
560 break;
561 case BRUSH_MEDIUMGREY:
04ee05f9 562 brush = new wxBrush(wxColour(wxT("MEDIUM GREY")), wxBRUSHSTYLE_SOLID);
f516d986
VZ
563 break;
564 case BRUSH_RED:
04ee05f9 565 brush = new wxBrush(*GetColour(COLOUR_RED), wxBRUSHSTYLE_SOLID);
f516d986
VZ
566 break;
567 case BRUSH_TRANSPARENT:
04ee05f9 568 brush = new wxBrush(*GetColour(COLOUR_BLACK), wxBRUSHSTYLE_TRANSPARENT);
f516d986
VZ
569 break;
570 case BRUSH_WHITE:
04ee05f9 571 brush = new wxBrush(*GetColour(COLOUR_WHITE), wxBRUSHSTYLE_SOLID);
f516d986
VZ
572 break;
573 default:
574 wxFAIL;
575 }
576 ms_stockObject[item] = brush;
577 }
578 return brush;
579}
696e1ea0 580
f516d986
VZ
581const wxColour* wxStockGDI::GetColour(Item item)
582{
5c33522f 583 wxColour* colour = static_cast<wxColour*>(ms_stockObject[item]);
f516d986
VZ
584 if (colour == NULL)
585 {
586 switch (item)
587 {
588 case COLOUR_BLACK:
589 colour = new wxColour(0, 0, 0);
590 break;
591 case COLOUR_BLUE:
592 colour = new wxColour(0, 0, 255);
593 break;
594 case COLOUR_CYAN:
595 colour = new wxColour(wxT("CYAN"));
596 break;
597 case COLOUR_GREEN:
598 colour = new wxColour(0, 255, 0);
599 break;
86028025
FM
600 case COLOUR_YELLOW:
601 colour = new wxColour(255, 255, 0);
602 break;
f516d986
VZ
603 case COLOUR_LIGHTGREY:
604 colour = new wxColour(wxT("LIGHT GREY"));
605 break;
606 case COLOUR_RED:
607 colour = new wxColour(255, 0, 0);
608 break;
609 case COLOUR_WHITE:
610 colour = new wxColour(255, 255, 255);
611 break;
612 default:
613 wxFAIL;
614 }
615 ms_stockObject[item] = colour;
616 }
617 return colour;
618}
619
620const wxCursor* wxStockGDI::GetCursor(Item item)
621{
5c33522f 622 wxCursor* cursor = static_cast<wxCursor*>(ms_stockObject[item]);
f516d986
VZ
623 if (cursor == NULL)
624 {
625 switch (item)
626 {
627 case CURSOR_CROSS:
628 cursor = new wxCursor(wxCURSOR_CROSS);
629 break;
630 case CURSOR_HOURGLASS:
631 cursor = new wxCursor(wxCURSOR_WAIT);
632 break;
633 case CURSOR_STANDARD:
634 cursor = new wxCursor(wxCURSOR_ARROW);
635 break;
636 default:
637 wxFAIL;
638 }
639 ms_stockObject[item] = cursor;
640 }
641 return cursor;
642}
643
644const wxFont* wxStockGDI::GetFont(Item item)
645{
5c33522f 646 wxFont* font = static_cast<wxFont*>(ms_stockObject[item]);
f516d986
VZ
647 if (font == NULL)
648 {
649 switch (item)
650 {
651 case FONT_ITALIC:
652 font = new wxFont(GetFont(FONT_NORMAL)->GetPointSize(), wxROMAN, wxITALIC, wxNORMAL);
653 break;
654 case FONT_NORMAL:
655 font = new wxFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
656 break;
657 case FONT_SMALL:
85a92283
VZ
658 font = new wxFont(GetFont(FONT_NORMAL)->GetPointSize()
659 // Using the font 2 points smaller than the normal one
660 // results in font so small as to be unreadable under MSW.
661 // We might want to actually use -1 under the other
662 // platforms too but for now be conservative and keep -2
663 // there for compatibility with the old behaviour as the
664 // small font seems to be readable enough there as it is.
665#ifdef __WXMSW__
666 - 1,
667#else
668 - 2,
669#endif
670 wxSWISS, wxNORMAL, wxNORMAL);
f516d986
VZ
671 break;
672 case FONT_SWISS:
673 font = new wxFont(GetFont(FONT_NORMAL)->GetPointSize(), wxSWISS, wxNORMAL, wxNORMAL);
674 break;
675 default:
676 wxFAIL;
677 }
678 ms_stockObject[item] = font;
679 }
680 return font;
681}
c801d85f 682
f516d986
VZ
683const wxPen* wxStockGDI::GetPen(Item item)
684{
5c33522f 685 wxPen* pen = static_cast<wxPen*>(ms_stockObject[item]);
f516d986
VZ
686 if (pen == NULL)
687 {
688 switch (item)
689 {
690 case PEN_BLACK:
82cddbd9 691 pen = new wxPen(*GetColour(COLOUR_BLACK), 1, wxPENSTYLE_SOLID);
f516d986
VZ
692 break;
693 case PEN_BLACKDASHED:
82cddbd9 694 pen = new wxPen(*GetColour(COLOUR_BLACK), 1, wxPENSTYLE_SHORT_DASH);
f516d986 695 break;
d8184587
FM
696 case PEN_BLUE:
697 pen = new wxPen(*GetColour(COLOUR_BLUE), 1, wxPENSTYLE_SOLID);
698 break;
f516d986 699 case PEN_CYAN:
82cddbd9 700 pen = new wxPen(*GetColour(COLOUR_CYAN), 1, wxPENSTYLE_SOLID);
f516d986
VZ
701 break;
702 case PEN_GREEN:
82cddbd9 703 pen = new wxPen(*GetColour(COLOUR_GREEN), 1, wxPENSTYLE_SOLID);
f516d986 704 break;
86028025
FM
705 case PEN_YELLOW:
706 pen = new wxPen(*GetColour(COLOUR_YELLOW), 1, wxPENSTYLE_SOLID);
707 break;
f516d986 708 case PEN_GREY:
82cddbd9 709 pen = new wxPen(wxColour(wxT("GREY")), 1, wxPENSTYLE_SOLID);
f516d986
VZ
710 break;
711 case PEN_LIGHTGREY:
82cddbd9 712 pen = new wxPen(*GetColour(COLOUR_LIGHTGREY), 1, wxPENSTYLE_SOLID);
f516d986
VZ
713 break;
714 case PEN_MEDIUMGREY:
82cddbd9 715 pen = new wxPen(wxColour(wxT("MEDIUM GREY")), 1, wxPENSTYLE_SOLID);
f516d986
VZ
716 break;
717 case PEN_RED:
82cddbd9 718 pen = new wxPen(*GetColour(COLOUR_RED), 1, wxPENSTYLE_SOLID);
f516d986
VZ
719 break;
720 case PEN_TRANSPARENT:
82cddbd9 721 pen = new wxPen(*GetColour(COLOUR_BLACK), 1, wxPENSTYLE_TRANSPARENT);
f516d986
VZ
722 break;
723 case PEN_WHITE:
82cddbd9 724 pen = new wxPen(*GetColour(COLOUR_WHITE), 1, wxPENSTYLE_SOLID);
f516d986
VZ
725 break;
726 default:
727 wxFAIL;
728 }
729 ms_stockObject[item] = pen;
730 }
731 return pen;
c801d85f
KB
732}
733
f516d986 734void wxInitializeStockLists()
c801d85f 735{
f516d986
VZ
736 wxTheColourDatabase = new wxColourDatabase;
737
738 wxTheBrushList = new wxBrushList;
739 wxThePenList = new wxPenList;
740 wxTheFontList = new wxFontList;
a3622daa
VZ
741}
742
7ecb8b06
VZ
743void wxDeleteStockLists()
744{
5d3e7b52
WS
745 wxDELETE(wxTheBrushList);
746 wxDELETE(wxThePenList);
747 wxDELETE(wxTheFontList);
41d0b41d
FM
748
749 // wxTheColourDatabase is cleaned up by wxAppBase::CleanUp()
c801d85f
KB
750}
751
7ecb8b06
VZ
752// ============================================================================
753// wxTheXXXList stuff (semi-obsolete)
754// ============================================================================
755
1de8d512 756wxGDIObjListBase::wxGDIObjListBase()
c801d85f 757{
c801d85f
KB
758}
759
1de8d512 760wxGDIObjListBase::~wxGDIObjListBase()
c801d85f 761{
1de8d512 762 for (wxList::compatibility_iterator node = list.GetFirst(); node; node = node->GetNext())
c801d85f 763 {
5c33522f 764 delete static_cast<wxObject*>(node->GetData());
c801d85f
KB
765 }
766}
767
82cddbd9 768wxPen *wxPenList::FindOrCreatePen (const wxColour& colour, int width, wxPenStyle style)
c801d85f 769{
b685c0a6
VZ
770 for ( wxList::compatibility_iterator node = list.GetFirst();
771 node;
772 node = node->GetNext() )
13b6e335 773 {
b685c0a6
VZ
774 wxPen * const pen = (wxPen *) node->GetData();
775 if ( pen->GetWidth () == width &&
776 pen->GetStyle () == style &&
777 pen->GetColour() == colour )
778 return pen;
13b6e335
VZ
779 }
780
1de8d512
VZ
781 wxPen* pen = NULL;
782 wxPen penTmp(colour, width, style);
a1b806b9 783 if (penTmp.IsOk())
c801d85f 784 {
1de8d512
VZ
785 pen = new wxPen(penTmp);
786 list.Append(pen);
c801d85f 787 }
c801d85f 788
13b6e335 789 return pen;
c801d85f
KB
790}
791
3e6858cd 792wxBrush *wxBrushList::FindOrCreateBrush (const wxColour& colour, wxBrushStyle style)
c801d85f 793{
b685c0a6
VZ
794 for ( wxList::compatibility_iterator node = list.GetFirst();
795 node;
796 node = node->GetNext() )
c801d85f 797 {
b685c0a6 798 wxBrush * const brush = (wxBrush *) node->GetData ();
3e6858cd 799 if ( brush->GetStyle() == style && brush->GetColour() == colour )
b685c0a6 800 return brush;
c801d85f 801 }
6d167489 802
1de8d512
VZ
803 wxBrush* brush = NULL;
804 wxBrush brushTmp(colour, style);
a1b806b9 805 if (brushTmp.IsOk())
13b6e335 806 {
1de8d512
VZ
807 brush = new wxBrush(brushTmp);
808 list.Append(brush);
13b6e335 809 }
6d167489 810
13b6e335 811 return brush;
c801d85f
KB
812}
813
f6bcfd97 814wxFont *wxFontList::FindOrCreateFont(int pointSize,
82cddbd9
FM
815 wxFontFamily family,
816 wxFontStyle style,
817 wxFontWeight weight,
f6bcfd97
BP
818 bool underline,
819 const wxString& facename,
820 wxFontEncoding encoding)
c801d85f 821{
1de8d512 822 wxFont *font;
222ed1d6 823 wxList::compatibility_iterator node;
1de8d512 824 for (node = list.GetFirst(); node; node = node->GetNext())
c801d85f 825 {
b1d4dd7a 826 font = (wxFont *)node->GetData();
1de8d512 827 if (
f6bcfd97
BP
828 font->GetPointSize () == pointSize &&
829 font->GetStyle () == style &&
830 font->GetWeight () == weight &&
831 font->GetUnderlined () == underline )
832 {
777819af 833 wxFontFamily fontFamily = (wxFontFamily)font->GetFamily();
f6bcfd97 834
619d0528 835#if defined(__WXGTK__)
f6bcfd97
BP
836 // under GTK the default family is wxSWISS, so looking for a font
837 // with wxDEFAULT family should return a wxSWISS one instead of
838 // creating a new one
839 bool same = (fontFamily == family) ||
04ee05f9 840 (fontFamily == wxFONTFAMILY_SWISS && family == wxFONTFAMILY_DEFAULT);
f6bcfd97
BP
841#else // !GTK
842 // VZ: but why elsewhere do we require an exact match? mystery...
843 bool same = fontFamily == family;
844#endif // GTK/!GTK
845
846 // empty facename matches anything at all: this is bad because
847 // depending on which fonts are already created, we might get back
848 // a different font if we create it with empty facename, but it is
849 // still better than never matching anything in the cache at all
850 // in this case
8d8fbb9d 851 if ( same && !facename.empty() )
f6bcfd97
BP
852 {
853 const wxString& fontFace = font->GetFaceName();
854
855 // empty facename matches everything
856 same = !fontFace || fontFace == facename;
857 }
858
859 if ( same && (encoding != wxFONTENCODING_DEFAULT) )
860 {
861 // have to match the encoding too
862 same = font->GetEncoding() == encoding;
863 }
864
865 if ( same )
866 {
867 return font;
868 }
869 }
c801d85f 870 }
6d167489 871
1de8d512
VZ
872 // font not found, create the new one
873 font = NULL;
874 wxFont fontTmp(pointSize, family, style, weight, underline, facename, encoding);
a1b806b9 875 if (fontTmp.IsOk())
f6bcfd97 876 {
1de8d512
VZ
877 font = new wxFont(fontTmp);
878 list.Append(font);
f6bcfd97 879 }
6d167489 880
f6bcfd97 881 return font;
c801d85f
KB
882}
883
1de8d512
VZ
884#if WXWIN_COMPATIBILITY_2_6
885void wxBrushList::AddBrush(wxBrush*) { }
886void wxBrushList::RemoveBrush(wxBrush*) { }
887void wxFontList::AddFont(wxFont*) { }
888void wxFontList::RemoveFont(wxFont*) { }
889void wxPenList::AddPen(wxPen*) { }
890void wxPenList::RemovePen(wxPen*) { }
891#endif
c801d85f 892
6a6c0a8b
JS
893wxSize wxGetDisplaySize()
894{
895 int x, y;
896 wxDisplaySize(& x, & y);
897 return wxSize(x, y);
898}
899
ec5d7799
RD
900wxRect wxGetClientDisplayRect()
901{
902 int x, y, width, height;
903 wxClientDisplayRect(&x, &y, &width, &height); // call plat-specific version
904 return wxRect(x, y, width, height);
905}
906
904a68b6
RL
907wxSize wxGetDisplaySizeMM()
908{
909 int x, y;
910 wxDisplaySizeMM(& x, & y);
911 return wxSize(x, y);
912}
913
40fcf546
VS
914wxSize wxGetDisplayPPI()
915{
916 const wxSize pixels = wxGetDisplaySize();
917 const wxSize mm = wxGetDisplaySizeMM();
918
d43be80d
VZ
919 return wxSize((int)((pixels.x * inches2mm) / mm.x),
920 (int)((pixels.y * inches2mm) / mm.y));
40fcf546
VS
921}
922
8bbe427f
VZ
923wxResourceCache::~wxResourceCache ()
924{
222ed1d6 925 wxList::compatibility_iterator node = GetFirst ();
f6bcfd97 926 while (node) {
b1d4dd7a 927 wxObject *item = (wxObject *)node->GetData();
f6bcfd97 928 delete item;
a3622daa 929
b1d4dd7a 930 node = node->GetNext ();
a3622daa 931 }
a3622daa 932}