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