More WinCE mods.
[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 wxTheColourDatabase = new wxColourDatabase;
440 wxTheColourDatabase->Initialize();
441
442 wxTheBrushList = new wxBrushList;
443 wxThePenList = new wxPenList;
444 wxTheFontList = new wxFontList;
445 wxTheBitmapList = new wxBitmapList;
446 }
447
448 void wxInitializeStockObjects ()
449 {
450 #ifdef __WXMOTIF__
451 #endif
452 #ifdef __X__
453 // TODO
454 // wxFontPool = new XFontPool;
455 #endif
456
457 // why under MSW fonts shouldn't have the standard system size?
458 /*
459 #ifdef __WXMSW__
460 static const int sizeFont = 10;
461 #else
462 #endif
463 */
464 #if defined(__WXMAC__)
465 int sizeFont = 12;
466
467 Str255 fontName ;
468 SInt16 fontSize ;
469 Style fontStyle ;
470
471 GetThemeFont(kThemeSystemFont , GetApplicationScript() , fontName , &fontSize , &fontStyle ) ;
472 sizeFont = fontSize ;
473 wxSWISS_FONT = new wxFont (fontSize, wxSWISS, wxNORMAL, wxNORMAL , false , wxMacMakeStringFromPascal(fontName) );
474 #elif defined(__WXPM__)
475 static const int sizeFont = 12;
476 #else
477 wxNORMAL_FONT = new wxFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
478 static const int sizeFont = wxNORMAL_FONT->GetPointSize();
479 #endif
480
481 #if defined(__WXPM__)
482 /*
483 // Basic OS/2 has a fairly limited number of fonts and these are as good
484 // as I can do to get something that looks halfway "wx" normal
485 */
486 wxNORMAL_FONT = new wxFont (sizeFont, wxMODERN, wxNORMAL, wxBOLD);
487 wxSMALL_FONT = new wxFont (sizeFont - 4, wxSWISS, wxNORMAL, wxNORMAL); /* Helv */
488 wxITALIC_FONT = new wxFont (sizeFont, wxROMAN, wxITALIC, wxNORMAL);
489 wxSWISS_FONT = new wxFont (sizeFont, wxSWISS, wxNORMAL, wxNORMAL); /* Helv */
490 #elif defined(__WXMAC__)
491 wxNORMAL_FONT = new wxFont (sizeFont, wxMODERN, wxNORMAL, wxNORMAL);
492 wxITALIC_FONT = new wxFont (sizeFont, wxROMAN, wxITALIC, wxNORMAL);
493 GetThemeFont(kThemeSmallSystemFont , GetApplicationScript() , fontName , &fontSize , &fontStyle ) ;
494 wxSMALL_FONT = new wxFont (fontSize, wxSWISS, wxNORMAL, wxNORMAL , false , wxMacMakeStringFromPascal( fontName ) );
495 #else
496 wxSMALL_FONT = new wxFont (sizeFont - 2, wxSWISS, wxNORMAL, wxNORMAL);
497 wxITALIC_FONT = new wxFont (sizeFont, wxROMAN, wxITALIC, wxNORMAL);
498 wxSWISS_FONT = new wxFont (sizeFont, wxSWISS, wxNORMAL, wxNORMAL);
499 #endif
500
501 wxRED_PEN = new wxPen (wxT("RED"), 1, wxSOLID);
502 wxCYAN_PEN = new wxPen (wxT("CYAN"), 1, wxSOLID);
503 wxGREEN_PEN = new wxPen (wxT("GREEN"), 1, wxSOLID);
504 wxBLACK_PEN = new wxPen (wxT("BLACK"), 1, wxSOLID);
505 wxWHITE_PEN = new wxPen (wxT("WHITE"), 1, wxSOLID);
506 wxTRANSPARENT_PEN = new wxPen (wxT("BLACK"), 1, wxTRANSPARENT);
507 wxBLACK_DASHED_PEN = new wxPen (wxT("BLACK"), 1, wxSHORT_DASH);
508 wxGREY_PEN = new wxPen (wxT("GREY"), 1, wxSOLID);
509 wxMEDIUM_GREY_PEN = new wxPen (wxT("MEDIUM GREY"), 1, wxSOLID);
510 wxLIGHT_GREY_PEN = new wxPen (wxT("LIGHT GREY"), 1, wxSOLID);
511
512 wxBLUE_BRUSH = new wxBrush (wxT("BLUE"), wxSOLID);
513 wxGREEN_BRUSH = new wxBrush (wxT("GREEN"), wxSOLID);
514 wxWHITE_BRUSH = new wxBrush (wxT("WHITE"), wxSOLID);
515 wxBLACK_BRUSH = new wxBrush (wxT("BLACK"), wxSOLID);
516 wxTRANSPARENT_BRUSH = new wxBrush (wxT("BLACK"), wxTRANSPARENT);
517 wxCYAN_BRUSH = new wxBrush (wxT("CYAN"), wxSOLID);
518 wxRED_BRUSH = new wxBrush (wxT("RED"), wxSOLID);
519 wxGREY_BRUSH = new wxBrush (wxT("GREY"), wxSOLID);
520 wxMEDIUM_GREY_BRUSH = new wxBrush (wxT("MEDIUM GREY"), wxSOLID);
521 wxLIGHT_GREY_BRUSH = new wxBrush (wxT("LIGHT GREY"), wxSOLID);
522
523 wxBLACK = new wxColour (wxT("BLACK"));
524 wxWHITE = new wxColour (wxT("WHITE"));
525 wxRED = new wxColour (wxT("RED"));
526 wxBLUE = new wxColour (wxT("BLUE"));
527 wxGREEN = new wxColour (wxT("GREEN"));
528 wxCYAN = new wxColour (wxT("CYAN"));
529 wxLIGHT_GREY = new wxColour (wxT("LIGHT GREY"));
530
531 wxSTANDARD_CURSOR = new wxCursor (wxCURSOR_ARROW);
532 wxHOURGLASS_CURSOR = new wxCursor (wxCURSOR_WAIT);
533 wxCROSS_CURSOR = new wxCursor (wxCURSOR_CROSS);
534 }
535
536 void wxDeleteStockObjects ()
537 {
538 wxDELETE(wxNORMAL_FONT);
539 wxDELETE(wxSMALL_FONT);
540 wxDELETE(wxITALIC_FONT);
541 wxDELETE(wxSWISS_FONT);
542
543 wxDELETE(wxRED_PEN);
544 wxDELETE(wxCYAN_PEN);
545 wxDELETE(wxGREEN_PEN);
546 wxDELETE(wxBLACK_PEN);
547 wxDELETE(wxWHITE_PEN);
548 wxDELETE(wxTRANSPARENT_PEN);
549 wxDELETE(wxBLACK_DASHED_PEN);
550 wxDELETE(wxGREY_PEN);
551 wxDELETE(wxMEDIUM_GREY_PEN);
552 wxDELETE(wxLIGHT_GREY_PEN);
553
554 wxDELETE(wxBLUE_BRUSH);
555 wxDELETE(wxGREEN_BRUSH);
556 wxDELETE(wxWHITE_BRUSH);
557 wxDELETE(wxBLACK_BRUSH);
558 wxDELETE(wxTRANSPARENT_BRUSH);
559 wxDELETE(wxCYAN_BRUSH);
560 wxDELETE(wxRED_BRUSH);
561 wxDELETE(wxGREY_BRUSH);
562 wxDELETE(wxMEDIUM_GREY_BRUSH);
563 wxDELETE(wxLIGHT_GREY_BRUSH);
564
565 wxDELETE(wxBLACK);
566 wxDELETE(wxWHITE);
567 wxDELETE(wxRED);
568 wxDELETE(wxBLUE);
569 wxDELETE(wxGREEN);
570 wxDELETE(wxCYAN);
571 wxDELETE(wxLIGHT_GREY);
572
573 wxDELETE(wxSTANDARD_CURSOR);
574 wxDELETE(wxHOURGLASS_CURSOR);
575 wxDELETE(wxCROSS_CURSOR);
576 }
577
578 void wxDeleteStockLists()
579 {
580 wxDELETE(wxTheBrushList);
581 wxDELETE(wxThePenList);
582 wxDELETE(wxTheFontList);
583 wxDELETE(wxTheBitmapList);
584 }
585
586 // ============================================================================
587 // wxTheXXXList stuff (semi-obsolete)
588 // ============================================================================
589
590 wxBitmapList::wxBitmapList()
591 {
592 }
593
594 wxBitmapList::~wxBitmapList ()
595 {
596 wxList::compatibility_iterator node = GetFirst ();
597 while (node)
598 {
599 wxBitmap *bitmap = (wxBitmap *) node->GetData ();
600 wxList::compatibility_iterator next = node->GetNext ();
601 if (bitmap->GetVisible())
602 delete bitmap;
603 node = next;
604 }
605 }
606
607 // Pen and Brush lists
608 wxPenList::~wxPenList ()
609 {
610 wxList::compatibility_iterator node = GetFirst ();
611 while (node)
612 {
613 wxPen *pen = (wxPen *) node->GetData ();
614 wxList::compatibility_iterator next = node->GetNext ();
615 if (pen->GetVisible())
616 delete pen;
617 node = next;
618 }
619 }
620
621 void wxPenList::AddPen (wxPen * pen)
622 {
623 Append (pen);
624 }
625
626 void wxPenList::RemovePen (wxPen * pen)
627 {
628 DeleteObject (pen);
629 }
630
631 wxPen *wxPenList::FindOrCreatePen (const wxColour& colour, int width, int style)
632 {
633 for (wxList::compatibility_iterator node = GetFirst (); node; node = node->GetNext ())
634 {
635 wxPen *each_pen = (wxPen *) node->GetData ();
636 if (each_pen &&
637 each_pen->GetVisible() &&
638 each_pen->GetWidth () == width &&
639 each_pen->GetStyle () == style &&
640 each_pen->GetColour ().Red () == colour.Red () &&
641 each_pen->GetColour ().Green () == colour.Green () &&
642 each_pen->GetColour ().Blue () == colour.Blue ())
643 return each_pen;
644 }
645
646 wxPen *pen = new wxPen (colour, width, style);
647 if ( !pen->Ok() )
648 {
649 // don't save the invalid pens in the list
650 delete pen;
651
652 return NULL;
653 }
654
655 AddPen(pen);
656
657 // we'll delete it ourselves later
658 pen->SetVisible(TRUE);
659
660 return pen;
661 }
662
663 wxBrushList::~wxBrushList ()
664 {
665 wxList::compatibility_iterator node = GetFirst ();
666 while (node)
667 {
668 wxBrush *brush = (wxBrush *) node->GetData ();
669 wxList::compatibility_iterator next = node->GetNext ();
670 if (brush && brush->GetVisible())
671 delete brush;
672 node = next;
673 }
674 }
675
676 void wxBrushList::AddBrush (wxBrush * brush)
677 {
678 Append (brush);
679 }
680
681 wxBrush *wxBrushList::FindOrCreateBrush (const wxColour& colour, int style)
682 {
683 for (wxList::compatibility_iterator node = GetFirst (); node; node = node->GetNext ())
684 {
685 wxBrush *each_brush = (wxBrush *) node->GetData ();
686 if (each_brush &&
687 each_brush->GetVisible() &&
688 each_brush->GetStyle () == style &&
689 each_brush->GetColour ().Red () == colour.Red () &&
690 each_brush->GetColour ().Green () == colour.Green () &&
691 each_brush->GetColour ().Blue () == colour.Blue ())
692 return each_brush;
693 }
694
695 wxBrush *brush = new wxBrush (colour, style);
696
697 if ( !brush->Ok() )
698 {
699 // don't put the brushes we failed to create into the list
700 delete brush;
701
702 return NULL;
703 }
704
705 AddBrush(brush);
706
707 // we'll delete it ourselves later
708 brush->SetVisible(TRUE);
709
710 return brush;
711 }
712
713 void wxBrushList::RemoveBrush (wxBrush * brush)
714 {
715 DeleteObject (brush);
716 }
717
718 wxFontList::~wxFontList ()
719 {
720 wxList::compatibility_iterator node = GetFirst ();
721 while (node)
722 {
723 // Only delete objects that are 'visible', i.e.
724 // that have been created using FindOrCreate...,
725 // where the pointers are expected to be shared
726 // (and therefore not deleted by any one part of an app).
727 wxFont *font = (wxFont *) node->GetData ();
728 wxList::compatibility_iterator next = node->GetNext ();
729 if (font->GetVisible())
730 delete font;
731 node = next;
732 }
733 }
734
735 void wxFontList::AddFont (wxFont * font)
736 {
737 Append (font);
738 }
739
740 void wxFontList::RemoveFont (wxFont * font)
741 {
742 DeleteObject (font);
743 }
744
745 wxFont *wxFontList::FindOrCreateFont(int pointSize,
746 int family,
747 int style,
748 int weight,
749 bool underline,
750 const wxString& facename,
751 wxFontEncoding encoding)
752 {
753 wxFont *font = (wxFont *)NULL;
754 wxList::compatibility_iterator node;
755 for ( node = GetFirst(); node; node = node->GetNext() )
756 {
757 font = (wxFont *)node->GetData();
758 if ( font->GetVisible() &&
759 font->Ok() &&
760 font->GetPointSize () == pointSize &&
761 font->GetStyle () == style &&
762 font->GetWeight () == weight &&
763 font->GetUnderlined () == underline )
764 {
765 int fontFamily = font->GetFamily();
766
767 #if defined(__WXGTK__)
768 // under GTK the default family is wxSWISS, so looking for a font
769 // with wxDEFAULT family should return a wxSWISS one instead of
770 // creating a new one
771 bool same = (fontFamily == family) ||
772 (fontFamily == wxSWISS && family == wxDEFAULT);
773 #else // !GTK
774 // VZ: but why elsewhere do we require an exact match? mystery...
775 bool same = fontFamily == family;
776 #endif // GTK/!GTK
777
778 // empty facename matches anything at all: this is bad because
779 // depending on which fonts are already created, we might get back
780 // a different font if we create it with empty facename, but it is
781 // still better than never matching anything in the cache at all
782 // in this case
783 if ( same && !!facename )
784 {
785 const wxString& fontFace = font->GetFaceName();
786
787 // empty facename matches everything
788 same = !fontFace || fontFace == facename;
789 }
790
791 if ( same && (encoding != wxFONTENCODING_DEFAULT) )
792 {
793 // have to match the encoding too
794 same = font->GetEncoding() == encoding;
795 }
796
797 if ( same )
798 {
799 return font;
800 }
801 }
802 }
803
804 if ( !node )
805 {
806 // font not found, create the new one
807 font = new wxFont(pointSize, family, style, weight,
808 underline, facename, encoding);
809
810 AddFont(font);
811
812 // and mark it as being cacheable
813 font->SetVisible(TRUE);
814 }
815
816 return font;
817 }
818
819 void wxBitmapList::AddBitmap(wxBitmap *bitmap)
820 {
821 Append(bitmap);
822 }
823
824 void wxBitmapList::RemoveBitmap(wxBitmap *bitmap)
825 {
826 DeleteObject(bitmap);
827 }
828
829 wxSize wxGetDisplaySize()
830 {
831 int x, y;
832 wxDisplaySize(& x, & y);
833 return wxSize(x, y);
834 }
835
836 wxRect wxGetClientDisplayRect()
837 {
838 int x, y, width, height;
839 wxClientDisplayRect(&x, &y, &width, &height); // call plat-specific version
840 return wxRect(x, y, width, height);
841 }
842
843 wxSize wxGetDisplaySizeMM()
844 {
845 int x, y;
846 wxDisplaySizeMM(& x, & y);
847 return wxSize(x, y);
848 }
849
850 wxResourceCache::~wxResourceCache ()
851 {
852 wxList::compatibility_iterator node = GetFirst ();
853 while (node) {
854 wxObject *item = (wxObject *)node->GetData();
855 delete item;
856
857 node = node->GetNext ();
858 }
859 }
860