STL-ification patch for wxMSW and wxGTK.
[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 {wxT("MEDIUM GOLDENROD"), 234, 234, 173},
293 {wxT("MEDIUM FOREST GREEN"), 107, 142, 35},
294 {wxT("LIGHT MAGENTA"), 255, 0, 255},
295 {wxT("MEDIUM GREY"), 100, 100, 100},
296 };
297
298 size_t n;
299
300 for ( n = 0; n < WXSIZEOF(wxColourTable); n++ )
301 {
302 const wxColourDesc& cc = wxColourTable[n];
303 (*m_map)[cc.name] = new wxColour(cc.r,cc.g,cc.b);
304 }
305 #ifdef __WXPM__
306 m_palTable = new long[n];
307 for ( n = 0; n < WXSIZEOF(wxColourTable); n++ )
308 {
309 const wxColourDesc& cc = wxColourTable[n];
310 m_palTable[n] = OS2RGB(cc.r,cc.g,cc.b);
311 }
312 m_nSize = n;
313 #endif
314 }
315
316 /*
317 * Changed by Ian Brown, July 1994.
318 *
319 * When running under X, the Colour Database starts off empty. The X server
320 * is queried for the colour first time after which it is entered into the
321 * database. This allows our client to use the server colour database which
322 * is hopefully gamma corrected for the display being used.
323 */
324
325 wxColour *wxColourDatabase::FindColour(const wxString& colour)
326 {
327 return FindColour(colour, true);
328 }
329
330 wxColour *wxColourDatabase::FindColourNoAdd(const wxString& colour) const
331 {
332 return ((wxColourDatabase*)this)->FindColour(colour, false);
333 }
334
335 wxColour *wxColourDatabase::FindColour(const wxString& colour, bool add)
336 {
337 // VZ: make the comparaison case insensitive and also match both grey and
338 // gray
339 wxString colName = colour;
340 colName.MakeUpper();
341 wxString colName2 = colName;
342 if ( !colName2.Replace(_T("GRAY"), _T("GREY")) )
343 colName2.clear();
344
345 wxStringToColourHashMap::iterator it = m_map->find(colName);
346 if ( it == m_map->end() )
347 it = m_map->find(colName2);
348 if ( it != m_map->end() )
349 return it->second;
350
351 if ( !add )
352 return NULL;
353
354 #ifdef __WXMSW__
355 return NULL;
356 #endif
357 #ifdef __WXPM__
358 return NULL;
359 #endif
360 #ifdef __WXMGL__
361 return NULL;
362 #endif
363
364 // TODO for other implementations. This should really go into
365 // platform-specific directories.
366 #ifdef __WXMAC__
367 return NULL;
368 #endif
369 #ifdef __WXCOCOA__
370 return NULL;
371 #endif
372 #ifdef __WXSTUBS__
373 return NULL;
374 #endif
375
376 #ifdef __WXGTK__
377 wxColour *col = new wxColour( colour );
378
379 if (!(col->Ok()))
380 {
381 delete col;
382 return (wxColour *) NULL;
383 }
384 (*m_map)[colour] = col;
385 return col;
386 #endif
387
388 #ifdef __X__
389 XColor xcolour;
390
391 #ifdef __WXMOTIF__
392 Display *display = XtDisplay((Widget) wxTheApp->GetTopLevelWidget()) ;
393 #endif
394 #ifdef __WXX11__
395 Display* display = (Display*) wxGetDisplay();
396 #endif
397 /* MATTHEW: [4] Use wxGetMainColormap */
398 if (!XParseColor(display, (Colormap) wxTheApp->GetMainColormap((WXDisplay*) display), colour.ToAscii() ,&xcolour))
399 return NULL;
400
401 #if wxUSE_NANOX
402 unsigned char r = (unsigned char)(xcolour.red);
403 unsigned char g = (unsigned char)(xcolour.green);
404 unsigned char b = (unsigned char)(xcolour.blue);
405 #else
406 unsigned char r = (unsigned char)(xcolour.red >> 8);
407 unsigned char g = (unsigned char)(xcolour.green >> 8);
408 unsigned char b = (unsigned char)(xcolour.blue >> 8);
409 #endif
410
411 wxColour *col = new wxColour(r, g, b);
412 (*m_map)[colour] = col;
413
414 return col;
415 #endif // __X__
416 }
417
418 wxString wxColourDatabase::FindName (const wxColour& colour) const
419 {
420 unsigned char red = colour.Red ();
421 unsigned char green = colour.Green ();
422 unsigned char blue = colour.Blue ();
423
424 typedef wxStringToColourHashMap::iterator iterator;
425
426 for (iterator it = m_map->begin(), en = m_map->end(); it != en; ++it )
427 {
428 wxColour *col = it->second;
429
430 if (col->Red () == red && col->Green () == green && col->Blue () == blue)
431 return it->first;
432 }
433
434 return wxEmptyString;
435 }
436
437 void wxInitializeStockLists()
438 {
439 wxTheBrushList = new wxBrushList;
440 wxThePenList = new wxPenList;
441 wxTheFontList = new wxFontList;
442 wxTheBitmapList = new wxBitmapList;
443 }
444
445 void wxInitializeStockObjects ()
446 {
447 #ifdef __WXMOTIF__
448 #endif
449 #ifdef __X__
450 // TODO
451 // wxFontPool = new XFontPool;
452 #endif
453
454 // why under MSW fonts shouldn't have the standard system size?
455 /*
456 #ifdef __WXMSW__
457 static const int sizeFont = 10;
458 #else
459 #endif
460 */
461 #if defined(__WXMAC__)
462 int sizeFont = 12;
463
464 Str255 fontName ;
465 SInt16 fontSize ;
466 Style fontStyle ;
467
468 GetThemeFont(kThemeSystemFont , GetApplicationScript() , fontName , &fontSize , &fontStyle ) ;
469 sizeFont = fontSize ;
470 wxSWISS_FONT = new wxFont (fontSize, wxSWISS, wxNORMAL, wxNORMAL , false , wxMacMakeStringFromPascal(fontName) );
471 #elif defined(__WXPM__)
472 static const int sizeFont = 12;
473 #else
474 wxNORMAL_FONT = new wxFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
475 static const int sizeFont = wxNORMAL_FONT->GetPointSize();
476 #endif
477
478 #if defined(__WXPM__)
479 /*
480 // Basic OS/2 has a fairly limited number of fonts and these are as good
481 // as I can do to get something that looks halfway "wx" normal
482 */
483 wxNORMAL_FONT = new wxFont (sizeFont, wxMODERN, wxNORMAL, wxBOLD);
484 wxSMALL_FONT = new wxFont (sizeFont - 4, wxSWISS, wxNORMAL, wxNORMAL); /* Helv */
485 wxITALIC_FONT = new wxFont (sizeFont, wxROMAN, wxITALIC, wxNORMAL);
486 wxSWISS_FONT = new wxFont (sizeFont, wxSWISS, wxNORMAL, wxNORMAL); /* Helv */
487 #elif defined(__WXMAC__)
488 wxNORMAL_FONT = new wxFont (sizeFont, wxMODERN, wxNORMAL, wxNORMAL);
489 wxITALIC_FONT = new wxFont (sizeFont, wxROMAN, wxITALIC, wxNORMAL);
490 GetThemeFont(kThemeSmallSystemFont , GetApplicationScript() , fontName , &fontSize , &fontStyle ) ;
491 wxSMALL_FONT = new wxFont (fontSize, wxSWISS, wxNORMAL, wxNORMAL , false , wxMacMakeStringFromPascal( fontName ) );
492 #else
493 wxSMALL_FONT = new wxFont (sizeFont - 2, wxSWISS, wxNORMAL, wxNORMAL);
494 wxITALIC_FONT = new wxFont (sizeFont, wxROMAN, wxITALIC, wxNORMAL);
495 wxSWISS_FONT = new wxFont (sizeFont, wxSWISS, wxNORMAL, wxNORMAL);
496 #endif
497
498 wxRED_PEN = new wxPen (wxT("RED"), 1, wxSOLID);
499 wxCYAN_PEN = new wxPen (wxT("CYAN"), 1, wxSOLID);
500 wxGREEN_PEN = new wxPen (wxT("GREEN"), 1, wxSOLID);
501 wxBLACK_PEN = new wxPen (wxT("BLACK"), 1, wxSOLID);
502 wxWHITE_PEN = new wxPen (wxT("WHITE"), 1, wxSOLID);
503 wxTRANSPARENT_PEN = new wxPen (wxT("BLACK"), 1, wxTRANSPARENT);
504 wxBLACK_DASHED_PEN = new wxPen (wxT("BLACK"), 1, wxSHORT_DASH);
505 wxGREY_PEN = new wxPen (wxT("GREY"), 1, wxSOLID);
506 wxMEDIUM_GREY_PEN = new wxPen (wxT("MEDIUM GREY"), 1, wxSOLID);
507 wxLIGHT_GREY_PEN = new wxPen (wxT("LIGHT GREY"), 1, wxSOLID);
508
509 wxBLUE_BRUSH = new wxBrush (wxT("BLUE"), wxSOLID);
510 wxGREEN_BRUSH = new wxBrush (wxT("GREEN"), wxSOLID);
511 wxWHITE_BRUSH = new wxBrush (wxT("WHITE"), wxSOLID);
512 wxBLACK_BRUSH = new wxBrush (wxT("BLACK"), wxSOLID);
513 wxTRANSPARENT_BRUSH = new wxBrush (wxT("BLACK"), wxTRANSPARENT);
514 wxCYAN_BRUSH = new wxBrush (wxT("CYAN"), wxSOLID);
515 wxRED_BRUSH = new wxBrush (wxT("RED"), wxSOLID);
516 wxGREY_BRUSH = new wxBrush (wxT("GREY"), wxSOLID);
517 wxMEDIUM_GREY_BRUSH = new wxBrush (wxT("MEDIUM GREY"), wxSOLID);
518 wxLIGHT_GREY_BRUSH = new wxBrush (wxT("LIGHT GREY"), wxSOLID);
519
520 wxBLACK = new wxColour (wxT("BLACK"));
521 wxWHITE = new wxColour (wxT("WHITE"));
522 wxRED = new wxColour (wxT("RED"));
523 wxBLUE = new wxColour (wxT("BLUE"));
524 wxGREEN = new wxColour (wxT("GREEN"));
525 wxCYAN = new wxColour (wxT("CYAN"));
526 wxLIGHT_GREY = new wxColour (wxT("LIGHT GREY"));
527
528 wxSTANDARD_CURSOR = new wxCursor (wxCURSOR_ARROW);
529 wxHOURGLASS_CURSOR = new wxCursor (wxCURSOR_WAIT);
530 wxCROSS_CURSOR = new wxCursor (wxCURSOR_CROSS);
531 }
532
533 void wxDeleteStockObjects ()
534 {
535 wxDELETE(wxNORMAL_FONT);
536 wxDELETE(wxSMALL_FONT);
537 wxDELETE(wxITALIC_FONT);
538 wxDELETE(wxSWISS_FONT);
539
540 wxDELETE(wxRED_PEN);
541 wxDELETE(wxCYAN_PEN);
542 wxDELETE(wxGREEN_PEN);
543 wxDELETE(wxBLACK_PEN);
544 wxDELETE(wxWHITE_PEN);
545 wxDELETE(wxTRANSPARENT_PEN);
546 wxDELETE(wxBLACK_DASHED_PEN);
547 wxDELETE(wxGREY_PEN);
548 wxDELETE(wxMEDIUM_GREY_PEN);
549 wxDELETE(wxLIGHT_GREY_PEN);
550
551 wxDELETE(wxBLUE_BRUSH);
552 wxDELETE(wxGREEN_BRUSH);
553 wxDELETE(wxWHITE_BRUSH);
554 wxDELETE(wxBLACK_BRUSH);
555 wxDELETE(wxTRANSPARENT_BRUSH);
556 wxDELETE(wxCYAN_BRUSH);
557 wxDELETE(wxRED_BRUSH);
558 wxDELETE(wxGREY_BRUSH);
559 wxDELETE(wxMEDIUM_GREY_BRUSH);
560 wxDELETE(wxLIGHT_GREY_BRUSH);
561
562 wxDELETE(wxBLACK);
563 wxDELETE(wxWHITE);
564 wxDELETE(wxRED);
565 wxDELETE(wxBLUE);
566 wxDELETE(wxGREEN);
567 wxDELETE(wxCYAN);
568 wxDELETE(wxLIGHT_GREY);
569
570 wxDELETE(wxSTANDARD_CURSOR);
571 wxDELETE(wxHOURGLASS_CURSOR);
572 wxDELETE(wxCROSS_CURSOR);
573 }
574
575 void wxDeleteStockLists()
576 {
577 wxDELETE(wxTheBrushList);
578 wxDELETE(wxThePenList);
579 wxDELETE(wxTheFontList);
580 wxDELETE(wxTheBitmapList);
581 }
582
583 // ============================================================================
584 // wxTheXXXList stuff (semi-obsolete)
585 // ============================================================================
586
587 wxBitmapList::wxBitmapList()
588 {
589 }
590
591 wxBitmapList::~wxBitmapList ()
592 {
593 wxList::compatibility_iterator node = GetFirst ();
594 while (node)
595 {
596 wxBitmap *bitmap = (wxBitmap *) node->GetData ();
597 wxList::compatibility_iterator next = node->GetNext ();
598 if (bitmap->GetVisible())
599 delete bitmap;
600 node = next;
601 }
602 }
603
604 // Pen and Brush lists
605 wxPenList::~wxPenList ()
606 {
607 wxList::compatibility_iterator node = GetFirst ();
608 while (node)
609 {
610 wxPen *pen = (wxPen *) node->GetData ();
611 wxList::compatibility_iterator next = node->GetNext ();
612 if (pen->GetVisible())
613 delete pen;
614 node = next;
615 }
616 }
617
618 void wxPenList::AddPen (wxPen * pen)
619 {
620 Append (pen);
621 }
622
623 void wxPenList::RemovePen (wxPen * pen)
624 {
625 DeleteObject (pen);
626 }
627
628 wxPen *wxPenList::FindOrCreatePen (const wxColour& colour, int width, int style)
629 {
630 for (wxList::compatibility_iterator node = GetFirst (); node; node = node->GetNext ())
631 {
632 wxPen *each_pen = (wxPen *) node->GetData ();
633 if (each_pen &&
634 each_pen->GetVisible() &&
635 each_pen->GetWidth () == width &&
636 each_pen->GetStyle () == style &&
637 each_pen->GetColour ().Red () == colour.Red () &&
638 each_pen->GetColour ().Green () == colour.Green () &&
639 each_pen->GetColour ().Blue () == colour.Blue ())
640 return each_pen;
641 }
642
643 wxPen *pen = new wxPen (colour, width, style);
644 if ( !pen->Ok() )
645 {
646 // don't save the invalid pens in the list
647 delete pen;
648
649 return NULL;
650 }
651
652 AddPen(pen);
653
654 // we'll delete it ourselves later
655 pen->SetVisible(TRUE);
656
657 return pen;
658 }
659
660 wxBrushList::~wxBrushList ()
661 {
662 wxList::compatibility_iterator node = GetFirst ();
663 while (node)
664 {
665 wxBrush *brush = (wxBrush *) node->GetData ();
666 wxList::compatibility_iterator next = node->GetNext ();
667 if (brush && brush->GetVisible())
668 delete brush;
669 node = next;
670 }
671 }
672
673 void wxBrushList::AddBrush (wxBrush * brush)
674 {
675 Append (brush);
676 }
677
678 wxBrush *wxBrushList::FindOrCreateBrush (const wxColour& colour, int style)
679 {
680 for (wxList::compatibility_iterator node = GetFirst (); node; node = node->GetNext ())
681 {
682 wxBrush *each_brush = (wxBrush *) node->GetData ();
683 if (each_brush &&
684 each_brush->GetVisible() &&
685 each_brush->GetStyle () == style &&
686 each_brush->GetColour ().Red () == colour.Red () &&
687 each_brush->GetColour ().Green () == colour.Green () &&
688 each_brush->GetColour ().Blue () == colour.Blue ())
689 return each_brush;
690 }
691
692 wxBrush *brush = new wxBrush (colour, style);
693
694 if ( !brush->Ok() )
695 {
696 // don't put the brushes we failed to create into the list
697 delete brush;
698
699 return NULL;
700 }
701
702 AddBrush(brush);
703
704 // we'll delete it ourselves later
705 brush->SetVisible(TRUE);
706
707 return brush;
708 }
709
710 void wxBrushList::RemoveBrush (wxBrush * brush)
711 {
712 DeleteObject (brush);
713 }
714
715 wxFontList::~wxFontList ()
716 {
717 wxList::compatibility_iterator node = GetFirst ();
718 while (node)
719 {
720 // Only delete objects that are 'visible', i.e.
721 // that have been created using FindOrCreate...,
722 // where the pointers are expected to be shared
723 // (and therefore not deleted by any one part of an app).
724 wxFont *font = (wxFont *) node->GetData ();
725 wxList::compatibility_iterator next = node->GetNext ();
726 if (font->GetVisible())
727 delete font;
728 node = next;
729 }
730 }
731
732 void wxFontList::AddFont (wxFont * font)
733 {
734 Append (font);
735 }
736
737 void wxFontList::RemoveFont (wxFont * font)
738 {
739 DeleteObject (font);
740 }
741
742 wxFont *wxFontList::FindOrCreateFont(int pointSize,
743 int family,
744 int style,
745 int weight,
746 bool underline,
747 const wxString& facename,
748 wxFontEncoding encoding)
749 {
750 wxFont *font = (wxFont *)NULL;
751 wxList::compatibility_iterator node;
752 for ( node = GetFirst(); node; node = node->GetNext() )
753 {
754 font = (wxFont *)node->GetData();
755 if ( font->GetVisible() &&
756 font->Ok() &&
757 font->GetPointSize () == pointSize &&
758 font->GetStyle () == style &&
759 font->GetWeight () == weight &&
760 font->GetUnderlined () == underline )
761 {
762 int fontFamily = font->GetFamily();
763
764 #if defined(__WXGTK__)
765 // under GTK the default family is wxSWISS, so looking for a font
766 // with wxDEFAULT family should return a wxSWISS one instead of
767 // creating a new one
768 bool same = (fontFamily == family) ||
769 (fontFamily == wxSWISS && family == wxDEFAULT);
770 #else // !GTK
771 // VZ: but why elsewhere do we require an exact match? mystery...
772 bool same = fontFamily == family;
773 #endif // GTK/!GTK
774
775 // empty facename matches anything at all: this is bad because
776 // depending on which fonts are already created, we might get back
777 // a different font if we create it with empty facename, but it is
778 // still better than never matching anything in the cache at all
779 // in this case
780 if ( same && !!facename )
781 {
782 const wxString& fontFace = font->GetFaceName();
783
784 // empty facename matches everything
785 same = !fontFace || fontFace == facename;
786 }
787
788 if ( same && (encoding != wxFONTENCODING_DEFAULT) )
789 {
790 // have to match the encoding too
791 same = font->GetEncoding() == encoding;
792 }
793
794 if ( same )
795 {
796 return font;
797 }
798 }
799 }
800
801 if ( !node )
802 {
803 // font not found, create the new one
804 font = new wxFont(pointSize, family, style, weight,
805 underline, facename, encoding);
806
807 AddFont(font);
808
809 // and mark it as being cacheable
810 font->SetVisible(TRUE);
811 }
812
813 return font;
814 }
815
816 void wxBitmapList::AddBitmap(wxBitmap *bitmap)
817 {
818 Append(bitmap);
819 }
820
821 void wxBitmapList::RemoveBitmap(wxBitmap *bitmap)
822 {
823 DeleteObject(bitmap);
824 }
825
826 wxSize wxGetDisplaySize()
827 {
828 int x, y;
829 wxDisplaySize(& x, & y);
830 return wxSize(x, y);
831 }
832
833 wxRect wxGetClientDisplayRect()
834 {
835 int x, y, width, height;
836 wxClientDisplayRect(&x, &y, &width, &height); // call plat-specific version
837 return wxRect(x, y, width, height);
838 }
839
840 wxSize wxGetDisplaySizeMM()
841 {
842 int x, y;
843 wxDisplaySizeMM(& x, & y);
844 return wxSize(x, y);
845 }
846
847 wxResourceCache::~wxResourceCache ()
848 {
849 wxList::compatibility_iterator node = GetFirst ();
850 while (node) {
851 wxObject *item = (wxObject *)node->GetData();
852 delete item;
853
854 node = node->GetNext ();
855 }
856 }
857