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