Remove duplicate colour entries.
[wxWidgets.git] / src / common / gdicmn.cpp
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$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "gdicmn.h"
14 #endif
15
16 #ifdef __VMS
17 #define XtDisplay XTDISPLAY
18 #endif
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #include "wx/event.h"
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"
36 #include "wx/app.h"
37 #include "wx/dc.h"
38 #include "wx/utils.h"
39 #include "wx/settings.h"
40 #include "wx/hashmap.h"
41
42 #include "wx/log.h"
43 #include <string.h>
44
45 #ifdef __WXMSW__
46 #include <windows.h>
47 #endif
48
49 #ifdef __WXMOTIF__
50 #ifdef __VMS__
51 #pragma message disable nosimpint
52 #endif
53 #include <Xm/Xm.h>
54 #ifdef __VMS__
55 #pragma message enable nosimpint
56 #endif
57 #endif
58
59 #ifdef __WXX11__
60 #include "X11/Xlib.h"
61 #endif
62
63 #ifdef __WXMAC__
64 #include "wx/mac/private.h"
65 #include "wx/mac/uma.h"
66 #endif
67 //IMPLEMENT_CLASS(wxColourDatabase, wxList)
68 //IMPLEMENT_DYNAMIC_CLASS(wxFontList, wxList)
69 //IMPLEMENT_DYNAMIC_CLASS(wxPenList, wxList)
70 //IMPLEMENT_DYNAMIC_CLASS(wxBrushList, wxList)
71 //IMPLEMENT_DYNAMIC_CLASS(wxBitmapList, wxList)
72 //IMPLEMENT_DYNAMIC_CLASS(wxResourceCache, wxList)
73
74 IMPLEMENT_ABSTRACT_CLASS(wxDCBase, wxObject)
75
76 wxRect::wxRect(const wxPoint& topLeft, const wxPoint& bottomRight)
77 {
78 x = topLeft.x;
79 y = topLeft.y;
80 width = bottomRight.x - topLeft.x + 1;
81 height = bottomRight.y - topLeft.y + 1;
82
83 if (width < 0)
84 {
85 width = -width;
86 x -= width;
87 }
88
89 if (height < 0)
90 {
91 height = -height;
92 y -= height;
93 }
94 }
95
96 wxRect::wxRect(const wxPoint& point, const wxSize& size)
97 {
98 x = point.x; y = point.y;
99 width = size.x; height = size.y;
100 }
101
102 bool wxRect::operator==(const wxRect& rect) const
103 {
104 return ((x == rect.x) &&
105 (y == rect.y) &&
106 (width == rect.width) &&
107 (height == rect.height));
108 }
109
110 wxRect& wxRect::operator += (const wxRect& rect)
111 {
112 *this = (*this + rect);
113 return ( *this ) ;
114 }
115
116 wxRect wxRect::operator + (const wxRect& rect) const
117 {
118 int x1 = wxMin(this->x, rect.x);
119 int y1 = wxMin(this->y, rect.y);
120 int y2 = wxMax(y+height, rect.height+rect.y);
121 int x2 = wxMax(x+width, rect.width+rect.x);
122 return wxRect(x1, y1, x2-x1, y2-y1);
123 }
124
125 wxRect& wxRect::Inflate(wxCoord dx, wxCoord dy)
126 {
127 x -= dx;
128 y -= dy;
129 width += 2*dx;
130 height += 2*dy;
131
132 // check that we didn't make the rectangle invalid by accident (you almost
133 // never want to have negative coords and never want negative size)
134 if ( x < 0 )
135 x = 0;
136 if ( y < 0 )
137 y = 0;
138
139 // what else can we do?
140 if ( width < 0 )
141 width = 0;
142 if ( height < 0 )
143 height = 0;
144
145 return *this;
146 }
147
148 bool wxRect::Inside(int cx, int cy) const
149 {
150 return ( (cx >= x) && (cy >= y)
151 && ((cy - y) < height)
152 && ((cx - x) < width)
153 );
154 }
155
156 wxRect& wxRect::Intersect(const wxRect& rect)
157 {
158 int x2 = GetRight(),
159 y2 = GetBottom();
160
161 if ( x < rect.x )
162 x = rect.x;
163 if ( y < rect.y )
164 y = rect.y;
165 if ( x2 > rect.GetRight() )
166 x2 = rect.GetRight();
167 if ( y2 > rect.GetBottom() )
168 y2 = rect.GetBottom();
169
170 width = x2 - x + 1;
171 height = y2 - y + 1;
172
173 if ( width <= 0 || height <= 0 )
174 {
175 width =
176 height = 0;
177 }
178
179 return *this;
180 }
181
182 bool wxRect::Intersects(const wxRect& rect) const
183 {
184 wxRect r = Intersect(rect);
185
186 // if there is no intersection, both width and height are 0
187 return r.width != 0;
188 }
189
190 WX_DECLARE_STRING_HASH_MAP( wxColour*, wxStringToColourHashMap );
191
192 wxColourDatabase::wxColourDatabase ()
193 {
194 m_map = new wxStringToColourHashMap;
195 }
196
197 wxColourDatabase::~wxColourDatabase ()
198 {
199 typedef wxStringToColourHashMap::iterator iterator;
200
201 for( iterator it = m_map->begin(), en = m_map->end(); it != en; ++it )
202 delete it->second;
203
204 delete m_map;
205 m_map = NULL;
206 #ifdef __WXPM__
207 delete [] m_palTable;
208 #endif
209 }
210
211 // Colour database stuff
212 void wxColourDatabase::Initialize ()
213 {
214 static const struct wxColourDesc
215 {
216 const wxChar *name;
217 int r,g,b;
218 }
219 wxColourTable[] =
220 {
221 {wxT("AQUAMARINE"),112, 219, 147},
222 {wxT("BLACK"),0, 0, 0},
223 {wxT("BLUE"), 0, 0, 255},
224 {wxT("BLUE VIOLET"), 159, 95, 159},
225 {wxT("BROWN"), 165, 42, 42},
226 {wxT("CADET BLUE"), 95, 159, 159},
227 {wxT("CORAL"), 255, 127, 0},
228 {wxT("CORNFLOWER BLUE"), 66, 66, 111},
229 {wxT("CYAN"), 0, 255, 255},
230 {wxT("DARK GREY"), 47, 47, 47}, // ?
231
232 {wxT("DARK GREEN"), 47, 79, 47},
233 {wxT("DARK OLIVE GREEN"), 79, 79, 47},
234 {wxT("DARK ORCHID"), 153, 50, 204},
235 {wxT("DARK SLATE BLUE"), 107, 35, 142},
236 {wxT("DARK SLATE GREY"), 47, 79, 79},
237 {wxT("DARK TURQUOISE"), 112, 147, 219},
238 {wxT("DIM GREY"), 84, 84, 84},
239 {wxT("FIREBRICK"), 142, 35, 35},
240 {wxT("FOREST GREEN"), 35, 142, 35},
241 {wxT("GOLD"), 204, 127, 50},
242 {wxT("GOLDENROD"), 219, 219, 112},
243 {wxT("GREY"), 128, 128, 128},
244 {wxT("GREEN"), 0, 255, 0},
245 {wxT("GREEN YELLOW"), 147, 219, 112},
246 {wxT("INDIAN RED"), 79, 47, 47},
247 {wxT("KHAKI"), 159, 159, 95},
248 {wxT("LIGHT BLUE"), 191, 216, 216},
249 {wxT("LIGHT GREY"), 192, 192, 192},
250 {wxT("LIGHT STEEL BLUE"), 143, 143, 188},
251 {wxT("LIME GREEN"), 50, 204, 50},
252 {wxT("LIGHT MAGENTA"), 255, 0, 255},
253 {wxT("MAGENTA"), 255, 0, 255},
254 {wxT("MAROON"), 142, 35, 107},
255 {wxT("MEDIUM AQUAMARINE"), 50, 204, 153},
256 {wxT("MEDIUM GREY"), 100, 100, 100},
257 {wxT("MEDIUM BLUE"), 50, 50, 204},
258 {wxT("MEDIUM FOREST GREEN"), 107, 142, 35},
259 {wxT("MEDIUM GOLDENROD"), 234, 234, 173},
260 {wxT("MEDIUM ORCHID"), 147, 112, 219},
261 {wxT("MEDIUM SEA GREEN"), 66, 111, 66},
262 {wxT("MEDIUM SLATE BLUE"), 127, 0, 255},
263 {wxT("MEDIUM SPRING GREEN"), 127, 255, 0},
264 {wxT("MEDIUM TURQUOISE"), 112, 219, 219},
265 {wxT("MEDIUM VIOLET RED"), 219, 112, 147},
266 {wxT("MIDNIGHT BLUE"), 47, 47, 79},
267 {wxT("NAVY"), 35, 35, 142},
268 {wxT("ORANGE"), 204, 50, 50},
269 {wxT("ORANGE RED"), 255, 0, 127},
270 {wxT("ORCHID"), 219, 112, 219},
271 {wxT("PALE GREEN"), 143, 188, 143},
272 {wxT("PINK"), 188, 143, 234},
273 {wxT("PLUM"), 234, 173, 234},
274 {wxT("PURPLE"), 176, 0, 255},
275 {wxT("RED"), 255, 0, 0},
276 {wxT("SALMON"), 111, 66, 66},
277 {wxT("SEA GREEN"), 35, 142, 107},
278 {wxT("SIENNA"), 142, 107, 35},
279 {wxT("SKY BLUE"), 50, 153, 204},
280 {wxT("SLATE BLUE"), 0, 127, 255},
281 {wxT("SPRING GREEN"), 0, 255, 127},
282 {wxT("STEEL BLUE"), 35, 107, 142},
283 {wxT("TAN"), 219, 147, 112},
284 {wxT("THISTLE"), 216, 191, 216},
285 {wxT("TURQUOISE"), 173, 234, 234},
286 {wxT("VIOLET"), 79, 47, 79},
287 {wxT("VIOLET RED"), 204, 50, 153},
288 {wxT("WHEAT"), 216, 216, 191},
289 {wxT("WHITE"), 255, 255, 255},
290 {wxT("YELLOW"), 255, 255, 0},
291 {wxT("YELLOW GREEN"), 153, 204, 50},
292 };
293
294 size_t n;
295
296 for ( n = 0; n < WXSIZEOF(wxColourTable); n++ )
297 {
298 const wxColourDesc& cc = wxColourTable[n];
299 (*m_map)[cc.name] = new wxColour(cc.r,cc.g,cc.b);
300 }
301 #ifdef __WXPM__
302 m_palTable = new long[n];
303 for ( n = 0; n < WXSIZEOF(wxColourTable); n++ )
304 {
305 const wxColourDesc& cc = wxColourTable[n];
306 m_palTable[n] = OS2RGB(cc.r,cc.g,cc.b);
307 }
308 m_nSize = n;
309 #endif
310 }
311
312 /*
313 * Changed by Ian Brown, July 1994.
314 *
315 * When running under X, the Colour Database starts off empty. The X server
316 * is queried for the colour first time after which it is entered into the
317 * database. This allows our client to use the server colour database which
318 * is hopefully gamma corrected for the display being used.
319 */
320
321 wxColour *wxColourDatabase::FindColour(const wxString& colour)
322 {
323 return FindColour(colour, true);
324 }
325
326 wxColour *wxColourDatabase::FindColourNoAdd(const wxString& colour) const
327 {
328 return ((wxColourDatabase*)this)->FindColour(colour, false);
329 }
330
331 wxColour *wxColourDatabase::FindColour(const wxString& colour, bool add)
332 {
333 // VZ: make the comparaison case insensitive and also match both grey and
334 // gray
335 wxString colName = colour;
336 colName.MakeUpper();
337 wxString colName2 = colName;
338 if ( !colName2.Replace(_T("GRAY"), _T("GREY")) )
339 colName2.clear();
340
341 wxStringToColourHashMap::iterator it = m_map->find(colName);
342 if ( it == m_map->end() )
343 it = m_map->find(colName2);
344 if ( it != m_map->end() )
345 return it->second;
346
347 if ( !add )
348 return NULL;
349
350 #ifdef __WXMSW__
351 return NULL;
352 #endif
353 #ifdef __WXPM__
354 return NULL;
355 #endif
356 #ifdef __WXMGL__
357 return NULL;
358 #endif
359
360 // TODO for other implementations. This should really go into
361 // platform-specific directories.
362 #ifdef __WXMAC__
363 return NULL;
364 #endif
365 #ifdef __WXCOCOA__
366 return NULL;
367 #endif
368 #ifdef __WXSTUBS__
369 return NULL;
370 #endif
371
372 #ifdef __WXGTK__
373 wxColour *col = new wxColour( colour );
374
375 if (!(col->Ok()))
376 {
377 delete col;
378 return (wxColour *) NULL;
379 }
380 (*m_map)[colour] = col;
381 return col;
382 #endif
383
384 #ifdef __X__
385 XColor xcolour;
386
387 #ifdef __WXMOTIF__
388 Display *display = XtDisplay((Widget) wxTheApp->GetTopLevelWidget()) ;
389 #endif
390 #ifdef __WXX11__
391 Display* display = (Display*) wxGetDisplay();
392 #endif
393 /* MATTHEW: [4] Use wxGetMainColormap */
394 if (!XParseColor(display, (Colormap) wxTheApp->GetMainColormap((WXDisplay*) display), colour.ToAscii() ,&xcolour))
395 return NULL;
396
397 #if wxUSE_NANOX
398 unsigned char r = (unsigned char)(xcolour.red);
399 unsigned char g = (unsigned char)(xcolour.green);
400 unsigned char b = (unsigned char)(xcolour.blue);
401 #else
402 unsigned char r = (unsigned char)(xcolour.red >> 8);
403 unsigned char g = (unsigned char)(xcolour.green >> 8);
404 unsigned char b = (unsigned char)(xcolour.blue >> 8);
405 #endif
406
407 wxColour *col = new wxColour(r, g, b);
408 (*m_map)[colour] = col;
409
410 return col;
411 #endif // __X__
412 }
413
414 wxString wxColourDatabase::FindName (const wxColour& colour) const
415 {
416 unsigned char red = colour.Red ();
417 unsigned char green = colour.Green ();
418 unsigned char blue = colour.Blue ();
419
420 typedef wxStringToColourHashMap::iterator iterator;
421
422 for (iterator it = m_map->begin(), en = m_map->end(); it != en; ++it )
423 {
424 wxColour *col = it->second;
425
426 if (col->Red () == red && col->Green () == green && col->Blue () == blue)
427 return it->first;
428 }
429
430 return wxEmptyString;
431 }
432
433 void wxInitializeStockLists()
434 {
435 wxTheColourDatabase = new wxColourDatabase;
436 wxTheColourDatabase->Initialize();
437
438 wxTheBrushList = new wxBrushList;
439 wxThePenList = new wxPenList;
440 wxTheFontList = new wxFontList;
441 wxTheBitmapList = new wxBitmapList;
442 }
443
444 void wxInitializeStockObjects ()
445 {
446 #ifdef __WXMOTIF__
447 #endif
448 #ifdef __X__
449 // TODO
450 // wxFontPool = new XFontPool;
451 #endif
452
453 // why under MSW fonts shouldn't have the standard system size?
454 /*
455 #ifdef __WXMSW__
456 static const int sizeFont = 10;
457 #else
458 #endif
459 */
460 #if defined(__WXMAC__)
461 int sizeFont = 12;
462
463 Str255 fontName ;
464 SInt16 fontSize ;
465 Style fontStyle ;
466
467 GetThemeFont(kThemeSystemFont , GetApplicationScript() , fontName , &fontSize , &fontStyle ) ;
468 sizeFont = fontSize ;
469 wxSWISS_FONT = new wxFont (fontSize, wxSWISS, wxNORMAL, wxNORMAL , false , wxMacMakeStringFromPascal(fontName) );
470 #elif defined(__WXPM__)
471 static const int sizeFont = 12;
472 #else
473 wxNORMAL_FONT = new wxFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
474 static const int sizeFont = wxNORMAL_FONT->GetPointSize();
475 #endif
476
477 #if defined(__WXPM__)
478 /*
479 // Basic OS/2 has a fairly limited number of fonts and these are as good
480 // as I can do to get something that looks halfway "wx" normal
481 */
482 wxNORMAL_FONT = new wxFont (sizeFont, wxMODERN, wxNORMAL, wxBOLD);
483 wxSMALL_FONT = new wxFont (sizeFont - 4, wxSWISS, wxNORMAL, wxNORMAL); /* Helv */
484 wxITALIC_FONT = new wxFont (sizeFont, wxROMAN, wxITALIC, wxNORMAL);
485 wxSWISS_FONT = new wxFont (sizeFont, wxSWISS, wxNORMAL, wxNORMAL); /* Helv */
486 #elif defined(__WXMAC__)
487 wxNORMAL_FONT = new wxFont (sizeFont, wxMODERN, wxNORMAL, wxNORMAL);
488 wxITALIC_FONT = new wxFont (sizeFont, wxROMAN, wxITALIC, wxNORMAL);
489 GetThemeFont(kThemeSmallSystemFont , GetApplicationScript() , fontName , &fontSize , &fontStyle ) ;
490 wxSMALL_FONT = new wxFont (fontSize, wxSWISS, wxNORMAL, wxNORMAL , false , wxMacMakeStringFromPascal( fontName ) );
491 #else
492 wxSMALL_FONT = new wxFont (sizeFont - 2, wxSWISS, wxNORMAL, wxNORMAL);
493 wxITALIC_FONT = new wxFont (sizeFont, wxROMAN, wxITALIC, wxNORMAL);
494 wxSWISS_FONT = new wxFont (sizeFont, wxSWISS, wxNORMAL, wxNORMAL);
495 #endif
496
497 wxRED_PEN = new wxPen (wxT("RED"), 1, wxSOLID);
498 wxCYAN_PEN = new wxPen (wxT("CYAN"), 1, wxSOLID);
499 wxGREEN_PEN = new wxPen (wxT("GREEN"), 1, wxSOLID);
500 wxBLACK_PEN = new wxPen (wxT("BLACK"), 1, wxSOLID);
501 wxWHITE_PEN = new wxPen (wxT("WHITE"), 1, wxSOLID);
502 wxTRANSPARENT_PEN = new wxPen (wxT("BLACK"), 1, wxTRANSPARENT);
503 wxBLACK_DASHED_PEN = new wxPen (wxT("BLACK"), 1, wxSHORT_DASH);
504 wxGREY_PEN = new wxPen (wxT("GREY"), 1, wxSOLID);
505 wxMEDIUM_GREY_PEN = new wxPen (wxT("MEDIUM GREY"), 1, wxSOLID);
506 wxLIGHT_GREY_PEN = new wxPen (wxT("LIGHT GREY"), 1, wxSOLID);
507
508 wxBLUE_BRUSH = new wxBrush (wxT("BLUE"), wxSOLID);
509 wxGREEN_BRUSH = new wxBrush (wxT("GREEN"), wxSOLID);
510 wxWHITE_BRUSH = new wxBrush (wxT("WHITE"), wxSOLID);
511 wxBLACK_BRUSH = new wxBrush (wxT("BLACK"), wxSOLID);
512 wxTRANSPARENT_BRUSH = new wxBrush (wxT("BLACK"), wxTRANSPARENT);
513 wxCYAN_BRUSH = new wxBrush (wxT("CYAN"), wxSOLID);
514 wxRED_BRUSH = new wxBrush (wxT("RED"), wxSOLID);
515 wxGREY_BRUSH = new wxBrush (wxT("GREY"), wxSOLID);
516 wxMEDIUM_GREY_BRUSH = new wxBrush (wxT("MEDIUM GREY"), wxSOLID);
517 wxLIGHT_GREY_BRUSH = new wxBrush (wxT("LIGHT GREY"), wxSOLID);
518
519 wxBLACK = new wxColour (wxT("BLACK"));
520 wxWHITE = new wxColour (wxT("WHITE"));
521 wxRED = new wxColour (wxT("RED"));
522 wxBLUE = new wxColour (wxT("BLUE"));
523 wxGREEN = new wxColour (wxT("GREEN"));
524 wxCYAN = new wxColour (wxT("CYAN"));
525 wxLIGHT_GREY = new wxColour (wxT("LIGHT GREY"));
526
527 wxSTANDARD_CURSOR = new wxCursor (wxCURSOR_ARROW);
528 wxHOURGLASS_CURSOR = new wxCursor (wxCURSOR_WAIT);
529 wxCROSS_CURSOR = new wxCursor (wxCURSOR_CROSS);
530 }
531
532 void wxDeleteStockObjects ()
533 {
534 wxDELETE(wxNORMAL_FONT);
535 wxDELETE(wxSMALL_FONT);
536 wxDELETE(wxITALIC_FONT);
537 wxDELETE(wxSWISS_FONT);
538
539 wxDELETE(wxRED_PEN);
540 wxDELETE(wxCYAN_PEN);
541 wxDELETE(wxGREEN_PEN);
542 wxDELETE(wxBLACK_PEN);
543 wxDELETE(wxWHITE_PEN);
544 wxDELETE(wxTRANSPARENT_PEN);
545 wxDELETE(wxBLACK_DASHED_PEN);
546 wxDELETE(wxGREY_PEN);
547 wxDELETE(wxMEDIUM_GREY_PEN);
548 wxDELETE(wxLIGHT_GREY_PEN);
549
550 wxDELETE(wxBLUE_BRUSH);
551 wxDELETE(wxGREEN_BRUSH);
552 wxDELETE(wxWHITE_BRUSH);
553 wxDELETE(wxBLACK_BRUSH);
554 wxDELETE(wxTRANSPARENT_BRUSH);
555 wxDELETE(wxCYAN_BRUSH);
556 wxDELETE(wxRED_BRUSH);
557 wxDELETE(wxGREY_BRUSH);
558 wxDELETE(wxMEDIUM_GREY_BRUSH);
559 wxDELETE(wxLIGHT_GREY_BRUSH);
560
561 wxDELETE(wxBLACK);
562 wxDELETE(wxWHITE);
563 wxDELETE(wxRED);
564 wxDELETE(wxBLUE);
565 wxDELETE(wxGREEN);
566 wxDELETE(wxCYAN);
567 wxDELETE(wxLIGHT_GREY);
568
569 wxDELETE(wxSTANDARD_CURSOR);
570 wxDELETE(wxHOURGLASS_CURSOR);
571 wxDELETE(wxCROSS_CURSOR);
572 }
573
574 void wxDeleteStockLists()
575 {
576 wxDELETE(wxTheBrushList);
577 wxDELETE(wxThePenList);
578 wxDELETE(wxTheFontList);
579 wxDELETE(wxTheBitmapList);
580 }
581
582 // ============================================================================
583 // wxTheXXXList stuff (semi-obsolete)
584 // ============================================================================
585
586 wxBitmapList::wxBitmapList()
587 {
588 }
589
590 wxBitmapList::~wxBitmapList ()
591 {
592 wxList::compatibility_iterator node = GetFirst ();
593 while (node)
594 {
595 wxBitmap *bitmap = (wxBitmap *) node->GetData ();
596 wxList::compatibility_iterator next = node->GetNext ();
597 if (bitmap->GetVisible())
598 delete bitmap;
599 node = next;
600 }
601 }
602
603 // Pen and Brush lists
604 wxPenList::~wxPenList ()
605 {
606 wxList::compatibility_iterator node = GetFirst ();
607 while (node)
608 {
609 wxPen *pen = (wxPen *) node->GetData ();
610 wxList::compatibility_iterator next = node->GetNext ();
611 if (pen->GetVisible())
612 delete pen;
613 node = next;
614 }
615 }
616
617 void wxPenList::AddPen (wxPen * pen)
618 {
619 Append (pen);
620 }
621
622 void wxPenList::RemovePen (wxPen * pen)
623 {
624 DeleteObject (pen);
625 }
626
627 wxPen *wxPenList::FindOrCreatePen (const wxColour& colour, int width, int style)
628 {
629 for (wxList::compatibility_iterator node = GetFirst (); node; node = node->GetNext ())
630 {
631 wxPen *each_pen = (wxPen *) node->GetData ();
632 if (each_pen &&
633 each_pen->GetVisible() &&
634 each_pen->GetWidth () == width &&
635 each_pen->GetStyle () == style &&
636 each_pen->GetColour ().Red () == colour.Red () &&
637 each_pen->GetColour ().Green () == colour.Green () &&
638 each_pen->GetColour ().Blue () == colour.Blue ())
639 return each_pen;
640 }
641
642 wxPen *pen = new wxPen (colour, width, style);
643 if ( !pen->Ok() )
644 {
645 // don't save the invalid pens in the list
646 delete pen;
647
648 return NULL;
649 }
650
651 AddPen(pen);
652
653 // we'll delete it ourselves later
654 pen->SetVisible(TRUE);
655
656 return pen;
657 }
658
659 wxBrushList::~wxBrushList ()
660 {
661 wxList::compatibility_iterator node = GetFirst ();
662 while (node)
663 {
664 wxBrush *brush = (wxBrush *) node->GetData ();
665 wxList::compatibility_iterator next = node->GetNext ();
666 if (brush && brush->GetVisible())
667 delete brush;
668 node = next;
669 }
670 }
671
672 void wxBrushList::AddBrush (wxBrush * brush)
673 {
674 Append (brush);
675 }
676
677 wxBrush *wxBrushList::FindOrCreateBrush (const wxColour& colour, int style)
678 {
679 for (wxList::compatibility_iterator node = GetFirst (); node; node = node->GetNext ())
680 {
681 wxBrush *each_brush = (wxBrush *) node->GetData ();
682 if (each_brush &&
683 each_brush->GetVisible() &&
684 each_brush->GetStyle () == style &&
685 each_brush->GetColour ().Red () == colour.Red () &&
686 each_brush->GetColour ().Green () == colour.Green () &&
687 each_brush->GetColour ().Blue () == colour.Blue ())
688 return each_brush;
689 }
690
691 wxBrush *brush = new wxBrush (colour, style);
692
693 if ( !brush->Ok() )
694 {
695 // don't put the brushes we failed to create into the list
696 delete brush;
697
698 return NULL;
699 }
700
701 AddBrush(brush);
702
703 // we'll delete it ourselves later
704 brush->SetVisible(TRUE);
705
706 return brush;
707 }
708
709 void wxBrushList::RemoveBrush (wxBrush * brush)
710 {
711 DeleteObject (brush);
712 }
713
714 wxFontList::~wxFontList ()
715 {
716 wxList::compatibility_iterator node = GetFirst ();
717 while (node)
718 {
719 // Only delete objects that are 'visible', i.e.
720 // that have been created using FindOrCreate...,
721 // where the pointers are expected to be shared
722 // (and therefore not deleted by any one part of an app).
723 wxFont *font = (wxFont *) node->GetData ();
724 wxList::compatibility_iterator next = node->GetNext ();
725 if (font->GetVisible())
726 delete font;
727 node = next;
728 }
729 }
730
731 void wxFontList::AddFont (wxFont * font)
732 {
733 Append (font);
734 }
735
736 void wxFontList::RemoveFont (wxFont * font)
737 {
738 DeleteObject (font);
739 }
740
741 wxFont *wxFontList::FindOrCreateFont(int pointSize,
742 int family,
743 int style,
744 int weight,
745 bool underline,
746 const wxString& facename,
747 wxFontEncoding encoding)
748 {
749 wxFont *font = (wxFont *)NULL;
750 wxList::compatibility_iterator node;
751 for ( node = GetFirst(); node; node = node->GetNext() )
752 {
753 font = (wxFont *)node->GetData();
754 if ( font->GetVisible() &&
755 font->Ok() &&
756 font->GetPointSize () == pointSize &&
757 font->GetStyle () == style &&
758 font->GetWeight () == weight &&
759 font->GetUnderlined () == underline )
760 {
761 int fontFamily = font->GetFamily();
762
763 #if defined(__WXGTK__)
764 // under GTK the default family is wxSWISS, so looking for a font
765 // with wxDEFAULT family should return a wxSWISS one instead of
766 // creating a new one
767 bool same = (fontFamily == family) ||
768 (fontFamily == wxSWISS && family == wxDEFAULT);
769 #else // !GTK
770 // VZ: but why elsewhere do we require an exact match? mystery...
771 bool same = fontFamily == family;
772 #endif // GTK/!GTK
773
774 // empty facename matches anything at all: this is bad because
775 // depending on which fonts are already created, we might get back
776 // a different font if we create it with empty facename, but it is
777 // still better than never matching anything in the cache at all
778 // in this case
779 if ( same && !!facename )
780 {
781 const wxString& fontFace = font->GetFaceName();
782
783 // empty facename matches everything
784 same = !fontFace || fontFace == facename;
785 }
786
787 if ( same && (encoding != wxFONTENCODING_DEFAULT) )
788 {
789 // have to match the encoding too
790 same = font->GetEncoding() == encoding;
791 }
792
793 if ( same )
794 {
795 return font;
796 }
797 }
798 }
799
800 if ( !node )
801 {
802 // font not found, create the new one
803 font = new wxFont(pointSize, family, style, weight,
804 underline, facename, encoding);
805
806 AddFont(font);
807
808 // and mark it as being cacheable
809 font->SetVisible(TRUE);
810 }
811
812 return font;
813 }
814
815 void wxBitmapList::AddBitmap(wxBitmap *bitmap)
816 {
817 Append(bitmap);
818 }
819
820 void wxBitmapList::RemoveBitmap(wxBitmap *bitmap)
821 {
822 DeleteObject(bitmap);
823 }
824
825 wxSize wxGetDisplaySize()
826 {
827 int x, y;
828 wxDisplaySize(& x, & y);
829 return wxSize(x, y);
830 }
831
832 wxRect wxGetClientDisplayRect()
833 {
834 int x, y, width, height;
835 wxClientDisplayRect(&x, &y, &width, &height); // call plat-specific version
836 return wxRect(x, y, width, height);
837 }
838
839 wxSize wxGetDisplaySizeMM()
840 {
841 int x, y;
842 wxDisplaySizeMM(& x, & y);
843 return wxSize(x, y);
844 }
845
846 wxResourceCache::~wxResourceCache ()
847 {
848 wxList::compatibility_iterator node = GetFirst ();
849 while (node) {
850 wxObject *item = (wxObject *)node->GetData();
851 delete item;
852
853 node = node->GetNext ();
854 }
855 }
856