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