1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/gdicmn.cpp
3 // Purpose: Common GDI classes
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #define XtDisplay XTDISPLAY
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
24 #include "wx/gdicmn.h"
27 #include "wx/bitmap.h"
29 #include "wx/cursor.h"
31 #include "wx/palette.h"
35 #include "wx/settings.h"
36 #include "wx/hashmap.h"
43 #pragma message disable nosimpint
47 #pragma message enable nosimpint
55 #if wxUSE_EXTENDED_RTTI
59 template<> void wxStringReadValue(const wxString
&s
, wxPoint
&data
)
61 wxSscanf(s
, wxT("%d,%d"), &data
.x
, &data
.y
) ;
64 template<> void wxStringWriteValue(wxString
&s
, const wxPoint
&data
)
66 s
= wxString::Format(wxT("%d,%d"), data
.x
, data
.y
) ;
69 wxCUSTOM_TYPE_INFO(wxPoint
, wxToStringConverter
<wxPoint
> , wxFromStringConverter
<wxPoint
>)
71 template<> void wxStringReadValue(const wxString
&s
, wxSize
&data
)
73 wxSscanf(s
, wxT("%d,%d"), &data
.x
, &data
.y
) ;
76 template<> void wxStringWriteValue(wxString
&s
, const wxSize
&data
)
78 s
= wxString::Format(wxT("%d,%d"), data
.x
, data
.y
) ;
81 wxCUSTOM_TYPE_INFO(wxSize
, wxToStringConverter
<wxSize
> , wxFromStringConverter
<wxSize
>)
85 IMPLEMENT_ABSTRACT_CLASS(wxDCBase
, wxObject
)
87 wxRect::wxRect(const wxPoint
& point1
, const wxPoint
& point2
)
91 width
= point2
.x
- point1
.x
;
92 height
= point2
.y
- point1
.y
;
109 bool wxRect::operator==(const wxRect
& rect
) const
111 return ((x
== rect
.x
) &&
113 (width
== rect
.width
) &&
114 (height
== rect
.height
));
117 wxRect
wxRect::operator+(const wxRect
& rect
) const
119 int x1
= wxMin(this->x
, rect
.x
);
120 int y1
= wxMin(this->y
, rect
.y
);
121 int y2
= wxMax(y
+height
, rect
.height
+rect
.y
);
122 int x2
= wxMax(x
+width
, rect
.width
+rect
.x
);
123 return wxRect(x1
, y1
, x2
-x1
, y2
-y1
);
126 wxRect
& wxRect::Union(const wxRect
& rect
)
128 // ignore empty rectangles: union with an empty rectangle shouldn't extend
129 // this one to (0, 0)
130 if ( !width
|| !height
)
134 else if ( rect
.width
&& rect
.height
)
136 int x1
= wxMin(x
, rect
.x
);
137 int y1
= wxMin(y
, rect
.y
);
138 int y2
= wxMax(y
+ height
, rect
.height
+ rect
.y
);
139 int x2
= wxMax(x
+ width
, rect
.width
+ rect
.x
);
146 //else: we're not empty and rect is empty
151 wxRect
& wxRect::Inflate(wxCoord dx
, wxCoord dy
)
155 // Don't allow deflate to eat more width than we have,
156 // a well-defined rectangle cannot have negative width.
162 // The inflate is valid.
169 // Don't allow deflate to eat more height than we have,
170 // a well-defined rectangle cannot have negative height.
176 // The inflate is valid.
184 bool wxRect::Inside(int cx
, int cy
) const
186 return ( (cx
>= x
) && (cy
>= y
)
187 && ((cy
- y
) < height
)
188 && ((cx
- x
) < width
)
192 wxRect
& wxRect::Intersect(const wxRect
& rect
)
201 if ( x2
> rect
.GetRight() )
202 x2
= rect
.GetRight();
203 if ( y2
> rect
.GetBottom() )
204 y2
= rect
.GetBottom();
209 if ( width
<= 0 || height
<= 0 )
218 bool wxRect::Intersects(const wxRect
& rect
) const
220 wxRect r
= Intersect(rect
);
222 // if there is no intersection, both width and height are 0
226 // ============================================================================
228 // ============================================================================
230 // ----------------------------------------------------------------------------
231 // wxColourDatabase ctor/dtor
232 // ----------------------------------------------------------------------------
234 wxColourDatabase::wxColourDatabase ()
236 // will be created on demand in Initialize()
240 wxColourDatabase::~wxColourDatabase ()
244 WX_CLEAR_HASH_MAP(wxStringToColourHashMap
, *m_map
);
250 delete [] m_palTable
;
254 // Colour database stuff
255 void wxColourDatabase::Initialize()
259 // already initialized
263 m_map
= new wxStringToColourHashMap
;
265 static const struct wxColourDesc
272 {wxT("AQUAMARINE"),112, 219, 147},
273 {wxT("BLACK"),0, 0, 0},
274 {wxT("BLUE"), 0, 0, 255},
275 {wxT("BLUE VIOLET"), 159, 95, 159},
276 {wxT("BROWN"), 165, 42, 42},
277 {wxT("CADET BLUE"), 95, 159, 159},
278 {wxT("CORAL"), 255, 127, 0},
279 {wxT("CORNFLOWER BLUE"), 66, 66, 111},
280 {wxT("CYAN"), 0, 255, 255},
281 {wxT("DARK GREY"), 47, 47, 47}, // ?
283 {wxT("DARK GREEN"), 47, 79, 47},
284 {wxT("DARK OLIVE GREEN"), 79, 79, 47},
285 {wxT("DARK ORCHID"), 153, 50, 204},
286 {wxT("DARK SLATE BLUE"), 107, 35, 142},
287 {wxT("DARK SLATE GREY"), 47, 79, 79},
288 {wxT("DARK TURQUOISE"), 112, 147, 219},
289 {wxT("DIM GREY"), 84, 84, 84},
290 {wxT("FIREBRICK"), 142, 35, 35},
291 {wxT("FOREST GREEN"), 35, 142, 35},
292 {wxT("GOLD"), 204, 127, 50},
293 {wxT("GOLDENROD"), 219, 219, 112},
294 {wxT("GREY"), 128, 128, 128},
295 {wxT("GREEN"), 0, 255, 0},
296 {wxT("GREEN YELLOW"), 147, 219, 112},
297 {wxT("INDIAN RED"), 79, 47, 47},
298 {wxT("KHAKI"), 159, 159, 95},
299 {wxT("LIGHT BLUE"), 191, 216, 216},
300 {wxT("LIGHT GREY"), 192, 192, 192},
301 {wxT("LIGHT STEEL BLUE"), 143, 143, 188},
302 {wxT("LIME GREEN"), 50, 204, 50},
303 {wxT("LIGHT MAGENTA"), 255, 0, 255},
304 {wxT("MAGENTA"), 255, 0, 255},
305 {wxT("MAROON"), 142, 35, 107},
306 {wxT("MEDIUM AQUAMARINE"), 50, 204, 153},
307 {wxT("MEDIUM GREY"), 100, 100, 100},
308 {wxT("MEDIUM BLUE"), 50, 50, 204},
309 {wxT("MEDIUM FOREST GREEN"), 107, 142, 35},
310 {wxT("MEDIUM GOLDENROD"), 234, 234, 173},
311 {wxT("MEDIUM ORCHID"), 147, 112, 219},
312 {wxT("MEDIUM SEA GREEN"), 66, 111, 66},
313 {wxT("MEDIUM SLATE BLUE"), 127, 0, 255},
314 {wxT("MEDIUM SPRING GREEN"), 127, 255, 0},
315 {wxT("MEDIUM TURQUOISE"), 112, 219, 219},
316 {wxT("MEDIUM VIOLET RED"), 219, 112, 147},
317 {wxT("MIDNIGHT BLUE"), 47, 47, 79},
318 {wxT("NAVY"), 35, 35, 142},
319 {wxT("ORANGE"), 204, 50, 50},
320 {wxT("ORANGE RED"), 255, 0, 127},
321 {wxT("ORCHID"), 219, 112, 219},
322 {wxT("PALE GREEN"), 143, 188, 143},
323 {wxT("PINK"), 188, 143, 234},
324 {wxT("PLUM"), 234, 173, 234},
325 {wxT("PURPLE"), 176, 0, 255},
326 {wxT("RED"), 255, 0, 0},
327 {wxT("SALMON"), 111, 66, 66},
328 {wxT("SEA GREEN"), 35, 142, 107},
329 {wxT("SIENNA"), 142, 107, 35},
330 {wxT("SKY BLUE"), 50, 153, 204},
331 {wxT("SLATE BLUE"), 0, 127, 255},
332 {wxT("SPRING GREEN"), 0, 255, 127},
333 {wxT("STEEL BLUE"), 35, 107, 142},
334 {wxT("TAN"), 219, 147, 112},
335 {wxT("THISTLE"), 216, 191, 216},
336 {wxT("TURQUOISE"), 173, 234, 234},
337 {wxT("VIOLET"), 79, 47, 79},
338 {wxT("VIOLET RED"), 204, 50, 153},
339 {wxT("WHEAT"), 216, 216, 191},
340 {wxT("WHITE"), 255, 255, 255},
341 {wxT("YELLOW"), 255, 255, 0},
342 {wxT("YELLOW GREEN"), 153, 204, 50}
347 for ( n
= 0; n
< WXSIZEOF(wxColourTable
); n
++ )
349 const wxColourDesc
& cc
= wxColourTable
[n
];
350 (*m_map
)[cc
.name
] = new wxColour(cc
.r
, cc
.g
, cc
.b
);
354 m_palTable
= new long[n
];
355 for ( n
= 0; n
< WXSIZEOF(wxColourTable
); n
++ )
357 const wxColourDesc
& cc
= wxColourTable
[n
];
358 m_palTable
[n
] = OS2RGB(cc
.r
,cc
.g
,cc
.b
);
364 // ----------------------------------------------------------------------------
365 // wxColourDatabase operations
366 // ----------------------------------------------------------------------------
368 void wxColourDatabase::AddColour(const wxString
& name
, const wxColour
& colour
)
372 // canonicalize the colour names before using them as keys: they should be
374 wxString colName
= name
;
377 // ... and we also allow both grey/gray
378 wxString colNameAlt
= colName
;
379 if ( !colNameAlt
.Replace(_T("GRAY"), _T("GREY")) )
381 // but in this case it is not necessary so avoid extra search below
385 wxStringToColourHashMap::iterator it
= m_map
->find(colName
);
386 if ( it
== m_map
->end() && !colNameAlt
.empty() )
387 it
= m_map
->find(colNameAlt
);
388 if ( it
!= m_map
->end() )
390 *(it
->second
) = colour
;
394 (*m_map
)[colName
] = new wxColour(colour
);
398 wxColour
wxColourDatabase::Find(const wxString
& colour
) const
400 wxColourDatabase
* const self
= wxConstCast(this, wxColourDatabase
);
403 // make the comparaison case insensitive and also match both grey and gray
404 wxString colName
= colour
;
406 wxString colNameAlt
= colName
;
407 if ( !colNameAlt
.Replace(_T("GRAY"), _T("GREY")) )
410 wxStringToColourHashMap::iterator it
= m_map
->find(colName
);
411 if ( it
== m_map
->end() && !colNameAlt
.empty() )
412 it
= m_map
->find(colNameAlt
);
413 if ( it
!= m_map
->end() )
414 return *(it
->second
);
416 // we did not find any result in existing colours:
417 // we won't use wxString -> wxColour conversion because the
418 // wxColour::Set(const wxString &) function which does that conversion
419 // internally uses this function (wxColourDatabase::Find) and we want
420 // to avoid infinite recursion !
424 wxString
wxColourDatabase::FindName(const wxColour
& colour
) const
426 wxColourDatabase
* const self
= wxConstCast(this, wxColourDatabase
);
429 typedef wxStringToColourHashMap::iterator iterator
;
431 for ( iterator it
= m_map
->begin(), en
= m_map
->end(); it
!= en
; ++it
)
433 if ( *(it
->second
) == colour
)
437 return wxEmptyString
;
440 // ----------------------------------------------------------------------------
441 // deprecated wxColourDatabase methods
442 // ----------------------------------------------------------------------------
444 #if WXWIN_COMPATIBILITY_2_6
445 wxColour
*wxColourDatabase::FindColour(const wxString
& name
)
447 // This function is deprecated, use Find() instead.
448 // Formerly this function sometimes would return a deletable pointer and
449 // sometimes a non-deletable one (when returning a colour from the database).
450 // Trying to delete the latter anyway results in problems, so probably
451 // nobody ever freed the pointers. Currently it always returns a new
452 // instance, which means there will be memory leaks.
453 wxLogDebug(wxT("wxColourDataBase::FindColour():")
454 wxT(" Please use wxColourDataBase::Find() instead"));
456 // using a static variable here is not the most elegant solution but unless
457 // we want to make wxStringToColourHashMap public (i.e. move it to the
458 // header) so that we could have a member function returning
459 // wxStringToColourHashMap::iterator, there is really no good way to do it
462 // and knowing that this function is going to disappear in the next release
463 // anyhow I don't want to waste time on this
465 static wxColour s_col
;
471 return new wxColour(s_col
);
473 #endif // WXWIN_COMPATIBILITY_2_6
475 // ============================================================================
477 // ============================================================================
479 static wxStockGDI gs_wxStockGDI_instance
;
480 wxStockGDI
* wxStockGDI::ms_instance
= &gs_wxStockGDI_instance
;
481 wxObject
* wxStockGDI::ms_stockObject
[ITEMCOUNT
];
483 wxStockGDI::wxStockGDI()
487 wxStockGDI::~wxStockGDI()
491 void wxStockGDI::DeleteAll()
493 for (unsigned i
= 0; i
< ITEMCOUNT
; i
++)
495 delete ms_stockObject
[i
];
496 ms_stockObject
[i
] = NULL
;
500 const wxBrush
* wxStockGDI::GetBrush(Item item
)
502 wxBrush
* brush
= wx_static_cast(wxBrush
*, ms_stockObject
[item
]);
508 brush
= new wxBrush(*GetColour(COLOUR_BLACK
), wxSOLID
);
511 brush
= new wxBrush(*GetColour(COLOUR_BLUE
), wxSOLID
);
514 brush
= new wxBrush(*GetColour(COLOUR_CYAN
), wxSOLID
);
517 brush
= new wxBrush(*GetColour(COLOUR_GREEN
), wxSOLID
);
520 brush
= new wxBrush(wxColour(wxT("GREY")), wxSOLID
);
522 case BRUSH_LIGHTGREY
:
523 brush
= new wxBrush(*GetColour(COLOUR_LIGHTGREY
), wxSOLID
);
525 case BRUSH_MEDIUMGREY
:
526 brush
= new wxBrush(wxColour(wxT("MEDIUM GREY")), wxSOLID
);
529 brush
= new wxBrush(*GetColour(COLOUR_RED
), wxSOLID
);
531 case BRUSH_TRANSPARENT
:
532 brush
= new wxBrush(*GetColour(COLOUR_BLACK
), wxTRANSPARENT
);
535 brush
= new wxBrush(*GetColour(COLOUR_WHITE
), wxSOLID
);
540 ms_stockObject
[item
] = brush
;
545 const wxColour
* wxStockGDI::GetColour(Item item
)
547 wxColour
* colour
= wx_static_cast(wxColour
*, ms_stockObject
[item
]);
553 colour
= new wxColour(0, 0, 0);
556 colour
= new wxColour(0, 0, 255);
559 colour
= new wxColour(wxT("CYAN"));
562 colour
= new wxColour(0, 255, 0);
564 case COLOUR_LIGHTGREY
:
565 colour
= new wxColour(wxT("LIGHT GREY"));
568 colour
= new wxColour(255, 0, 0);
571 colour
= new wxColour(255, 255, 255);
576 ms_stockObject
[item
] = colour
;
581 const wxCursor
* wxStockGDI::GetCursor(Item item
)
583 wxCursor
* cursor
= wx_static_cast(wxCursor
*, ms_stockObject
[item
]);
589 cursor
= new wxCursor(wxCURSOR_CROSS
);
591 case CURSOR_HOURGLASS
:
592 cursor
= new wxCursor(wxCURSOR_WAIT
);
594 case CURSOR_STANDARD
:
595 cursor
= new wxCursor(wxCURSOR_ARROW
);
600 ms_stockObject
[item
] = cursor
;
605 const wxFont
* wxStockGDI::GetFont(Item item
)
607 wxFont
* font
= wx_static_cast(wxFont
*, ms_stockObject
[item
]);
613 font
= new wxFont(GetFont(FONT_NORMAL
)->GetPointSize(), wxROMAN
, wxITALIC
, wxNORMAL
);
616 font
= new wxFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
619 font
= new wxFont(GetFont(FONT_NORMAL
)->GetPointSize() - 2, wxSWISS
, wxNORMAL
, wxNORMAL
);
622 font
= new wxFont(GetFont(FONT_NORMAL
)->GetPointSize(), wxSWISS
, wxNORMAL
, wxNORMAL
);
627 ms_stockObject
[item
] = font
;
632 const wxPen
* wxStockGDI::GetPen(Item item
)
634 wxPen
* pen
= wx_static_cast(wxPen
*, ms_stockObject
[item
]);
640 pen
= new wxPen(*GetColour(COLOUR_BLACK
), 1, wxSOLID
);
642 case PEN_BLACKDASHED
:
643 pen
= new wxPen(*GetColour(COLOUR_BLACK
), 1, wxSHORT_DASH
);
646 pen
= new wxPen(*GetColour(COLOUR_CYAN
), 1, wxSOLID
);
649 pen
= new wxPen(*GetColour(COLOUR_GREEN
), 1, wxSOLID
);
652 pen
= new wxPen(wxColour(wxT("GREY")), 1, wxSOLID
);
655 pen
= new wxPen(*GetColour(COLOUR_LIGHTGREY
), 1, wxSOLID
);
658 pen
= new wxPen(wxColour(wxT("MEDIUM GREY")), 1, wxSOLID
);
661 pen
= new wxPen(*GetColour(COLOUR_RED
), 1, wxSOLID
);
663 case PEN_TRANSPARENT
:
664 pen
= new wxPen(*GetColour(COLOUR_BLACK
), 1, wxTRANSPARENT
);
667 pen
= new wxPen(*GetColour(COLOUR_WHITE
), 1, wxSOLID
);
672 ms_stockObject
[item
] = pen
;
677 void wxInitializeStockLists()
679 wxTheColourDatabase
= new wxColourDatabase
;
681 wxTheBrushList
= new wxBrushList
;
682 wxThePenList
= new wxPenList
;
683 wxTheFontList
= new wxFontList
;
686 void wxDeleteStockLists()
688 wxDELETE(wxTheBrushList
);
689 wxDELETE(wxThePenList
);
690 wxDELETE(wxTheFontList
);
693 // ============================================================================
694 // wxTheXXXList stuff (semi-obsolete)
695 // ============================================================================
697 wxGDIObjListBase::wxGDIObjListBase()
701 wxGDIObjListBase::~wxGDIObjListBase()
703 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
705 delete wx_static_cast(wxObject
*, node
->GetData());
709 wxPen
*wxPenList::FindOrCreatePen (const wxColour
& colour
, int width
, int style
)
711 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
713 wxPen
*each_pen
= (wxPen
*) node
->GetData ();
715 each_pen
->GetWidth () == width
&&
716 each_pen
->GetStyle () == style
&&
717 each_pen
->GetColour ().Red () == colour
.Red () &&
718 each_pen
->GetColour ().Green () == colour
.Green () &&
719 each_pen
->GetColour ().Blue () == colour
.Blue ())
724 wxPen
penTmp(colour
, width
, style
);
727 pen
= new wxPen(penTmp
);
734 wxBrush
*wxBrushList::FindOrCreateBrush (const wxColour
& colour
, int style
)
736 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
738 wxBrush
*each_brush
= (wxBrush
*) node
->GetData ();
740 each_brush
->GetStyle () == style
&&
741 each_brush
->GetColour ().Red () == colour
.Red () &&
742 each_brush
->GetColour ().Green () == colour
.Green () &&
743 each_brush
->GetColour ().Blue () == colour
.Blue ())
747 wxBrush
* brush
= NULL
;
748 wxBrush
brushTmp(colour
, style
);
751 brush
= new wxBrush(brushTmp
);
758 wxFont
*wxFontList::FindOrCreateFont(int pointSize
,
763 const wxString
& facename
,
764 wxFontEncoding encoding
)
767 wxList::compatibility_iterator node
;
768 for (node
= list
.GetFirst(); node
; node
= node
->GetNext())
770 font
= (wxFont
*)node
->GetData();
772 font
->GetPointSize () == pointSize
&&
773 font
->GetStyle () == style
&&
774 font
->GetWeight () == weight
&&
775 font
->GetUnderlined () == underline
)
777 int fontFamily
= font
->GetFamily();
779 #if defined(__WXGTK__)
780 // under GTK the default family is wxSWISS, so looking for a font
781 // with wxDEFAULT family should return a wxSWISS one instead of
782 // creating a new one
783 bool same
= (fontFamily
== family
) ||
784 (fontFamily
== wxSWISS
&& family
== wxDEFAULT
);
786 // VZ: but why elsewhere do we require an exact match? mystery...
787 bool same
= fontFamily
== family
;
790 // empty facename matches anything at all: this is bad because
791 // depending on which fonts are already created, we might get back
792 // a different font if we create it with empty facename, but it is
793 // still better than never matching anything in the cache at all
795 if ( same
&& !facename
.empty() )
797 const wxString
& fontFace
= font
->GetFaceName();
799 // empty facename matches everything
800 same
= !fontFace
|| fontFace
== facename
;
803 if ( same
&& (encoding
!= wxFONTENCODING_DEFAULT
) )
805 // have to match the encoding too
806 same
= font
->GetEncoding() == encoding
;
816 // font not found, create the new one
818 wxFont
fontTmp(pointSize
, family
, style
, weight
, underline
, facename
, encoding
);
821 font
= new wxFont(fontTmp
);
828 #if WXWIN_COMPATIBILITY_2_6
829 void wxBrushList::AddBrush(wxBrush
*) { }
830 void wxBrushList::RemoveBrush(wxBrush
*) { }
831 void wxFontList::AddFont(wxFont
*) { }
832 void wxFontList::RemoveFont(wxFont
*) { }
833 void wxPenList::AddPen(wxPen
*) { }
834 void wxPenList::RemovePen(wxPen
*) { }
837 wxSize
wxGetDisplaySize()
840 wxDisplaySize(& x
, & y
);
844 wxRect
wxGetClientDisplayRect()
846 int x
, y
, width
, height
;
847 wxClientDisplayRect(&x
, &y
, &width
, &height
); // call plat-specific version
848 return wxRect(x
, y
, width
, height
);
851 wxSize
wxGetDisplaySizeMM()
854 wxDisplaySizeMM(& x
, & y
);
858 wxResourceCache::~wxResourceCache ()
860 wxList::compatibility_iterator node
= GetFirst ();
862 wxObject
*item
= (wxObject
*)node
->GetData();
865 node
= node
->GetNext ();