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"
23 #include "wx/gdicmn.h"
33 #include "wx/palette.h"
35 #include "wx/cursor.h"
38 #include "wx/bitmap.h"
40 #include "wx/settings.h"
41 #include "wx/hashmap.h"
47 #pragma message disable nosimpint
51 #pragma message enable nosimpint
59 #if wxUSE_EXTENDED_RTTI
63 template<> void wxStringReadValue(const wxString
&s
, wxPoint
&data
)
65 wxSscanf(s
, wxT("%d,%d"), &data
.x
, &data
.y
) ;
68 template<> void wxStringWriteValue(wxString
&s
, const wxPoint
&data
)
70 s
= wxString::Format(wxT("%d,%d"), data
.x
, data
.y
) ;
73 wxCUSTOM_TYPE_INFO(wxPoint
, wxToStringConverter
<wxPoint
> , wxFromStringConverter
<wxPoint
>)
75 template<> void wxStringReadValue(const wxString
&s
, wxSize
&data
)
77 wxSscanf(s
, wxT("%d,%d"), &data
.x
, &data
.y
) ;
80 template<> void wxStringWriteValue(wxString
&s
, const wxSize
&data
)
82 s
= wxString::Format(wxT("%d,%d"), data
.x
, data
.y
) ;
85 wxCUSTOM_TYPE_INFO(wxSize
, wxToStringConverter
<wxSize
> , wxFromStringConverter
<wxSize
>)
89 IMPLEMENT_ABSTRACT_CLASS(wxDCBase
, wxObject
)
91 wxRect::wxRect(const wxPoint
& point1
, const wxPoint
& point2
)
95 width
= point2
.x
- point1
.x
;
96 height
= point2
.y
- point1
.y
;
113 bool wxRect::operator==(const wxRect
& rect
) const
115 return ((x
== rect
.x
) &&
117 (width
== rect
.width
) &&
118 (height
== rect
.height
));
121 wxRect
wxRect::operator+(const wxRect
& rect
) const
123 int x1
= wxMin(this->x
, rect
.x
);
124 int y1
= wxMin(this->y
, rect
.y
);
125 int y2
= wxMax(y
+height
, rect
.height
+rect
.y
);
126 int x2
= wxMax(x
+width
, rect
.width
+rect
.x
);
127 return wxRect(x1
, y1
, x2
-x1
, y2
-y1
);
130 wxRect
& wxRect::Union(const wxRect
& rect
)
132 // ignore empty rectangles: union with an empty rectangle shouldn't extend
133 // this one to (0, 0)
134 if ( !width
|| !height
)
138 else if ( rect
.width
&& rect
.height
)
140 int x1
= wxMin(x
, rect
.x
);
141 int y1
= wxMin(y
, rect
.y
);
142 int y2
= wxMax(y
+ height
, rect
.height
+ rect
.y
);
143 int x2
= wxMax(x
+ width
, rect
.width
+ rect
.x
);
150 //else: we're not empty and rect is empty
155 wxRect
& wxRect::Inflate(wxCoord dx
, wxCoord dy
)
159 // Don't allow deflate to eat more width than we have,
160 // a well-defined rectangle cannot have negative width.
166 // The inflate is valid.
173 // Don't allow deflate to eat more height than we have,
174 // a well-defined rectangle cannot have negative height.
180 // The inflate is valid.
188 bool wxRect::Inside(int cx
, int cy
) const
190 return ( (cx
>= x
) && (cy
>= y
)
191 && ((cy
- y
) < height
)
192 && ((cx
- x
) < width
)
196 wxRect
& wxRect::Intersect(const wxRect
& rect
)
205 if ( x2
> rect
.GetRight() )
206 x2
= rect
.GetRight();
207 if ( y2
> rect
.GetBottom() )
208 y2
= rect
.GetBottom();
213 if ( width
<= 0 || height
<= 0 )
222 bool wxRect::Intersects(const wxRect
& rect
) const
224 wxRect r
= Intersect(rect
);
226 // if there is no intersection, both width and height are 0
230 // ============================================================================
232 // ============================================================================
234 // ----------------------------------------------------------------------------
235 // wxColourDatabase ctor/dtor
236 // ----------------------------------------------------------------------------
238 wxColourDatabase::wxColourDatabase ()
240 // will be created on demand in Initialize()
244 wxColourDatabase::~wxColourDatabase ()
248 WX_CLEAR_HASH_MAP(wxStringToColourHashMap
, *m_map
);
254 delete [] m_palTable
;
258 // Colour database stuff
259 void wxColourDatabase::Initialize()
263 // already initialized
267 m_map
= new wxStringToColourHashMap
;
269 static const struct wxColourDesc
276 {wxT("AQUAMARINE"),112, 219, 147},
277 {wxT("BLACK"),0, 0, 0},
278 {wxT("BLUE"), 0, 0, 255},
279 {wxT("BLUE VIOLET"), 159, 95, 159},
280 {wxT("BROWN"), 165, 42, 42},
281 {wxT("CADET BLUE"), 95, 159, 159},
282 {wxT("CORAL"), 255, 127, 0},
283 {wxT("CORNFLOWER BLUE"), 66, 66, 111},
284 {wxT("CYAN"), 0, 255, 255},
285 {wxT("DARK GREY"), 47, 47, 47}, // ?
287 {wxT("DARK GREEN"), 47, 79, 47},
288 {wxT("DARK OLIVE GREEN"), 79, 79, 47},
289 {wxT("DARK ORCHID"), 153, 50, 204},
290 {wxT("DARK SLATE BLUE"), 107, 35, 142},
291 {wxT("DARK SLATE GREY"), 47, 79, 79},
292 {wxT("DARK TURQUOISE"), 112, 147, 219},
293 {wxT("DIM GREY"), 84, 84, 84},
294 {wxT("FIREBRICK"), 142, 35, 35},
295 {wxT("FOREST GREEN"), 35, 142, 35},
296 {wxT("GOLD"), 204, 127, 50},
297 {wxT("GOLDENROD"), 219, 219, 112},
298 {wxT("GREY"), 128, 128, 128},
299 {wxT("GREEN"), 0, 255, 0},
300 {wxT("GREEN YELLOW"), 147, 219, 112},
301 {wxT("INDIAN RED"), 79, 47, 47},
302 {wxT("KHAKI"), 159, 159, 95},
303 {wxT("LIGHT BLUE"), 191, 216, 216},
304 {wxT("LIGHT GREY"), 192, 192, 192},
305 {wxT("LIGHT STEEL BLUE"), 143, 143, 188},
306 {wxT("LIME GREEN"), 50, 204, 50},
307 {wxT("LIGHT MAGENTA"), 255, 0, 255},
308 {wxT("MAGENTA"), 255, 0, 255},
309 {wxT("MAROON"), 142, 35, 107},
310 {wxT("MEDIUM AQUAMARINE"), 50, 204, 153},
311 {wxT("MEDIUM GREY"), 100, 100, 100},
312 {wxT("MEDIUM BLUE"), 50, 50, 204},
313 {wxT("MEDIUM FOREST GREEN"), 107, 142, 35},
314 {wxT("MEDIUM GOLDENROD"), 234, 234, 173},
315 {wxT("MEDIUM ORCHID"), 147, 112, 219},
316 {wxT("MEDIUM SEA GREEN"), 66, 111, 66},
317 {wxT("MEDIUM SLATE BLUE"), 127, 0, 255},
318 {wxT("MEDIUM SPRING GREEN"), 127, 255, 0},
319 {wxT("MEDIUM TURQUOISE"), 112, 219, 219},
320 {wxT("MEDIUM VIOLET RED"), 219, 112, 147},
321 {wxT("MIDNIGHT BLUE"), 47, 47, 79},
322 {wxT("NAVY"), 35, 35, 142},
323 {wxT("ORANGE"), 204, 50, 50},
324 {wxT("ORANGE RED"), 255, 0, 127},
325 {wxT("ORCHID"), 219, 112, 219},
326 {wxT("PALE GREEN"), 143, 188, 143},
327 {wxT("PINK"), 188, 143, 234},
328 {wxT("PLUM"), 234, 173, 234},
329 {wxT("PURPLE"), 176, 0, 255},
330 {wxT("RED"), 255, 0, 0},
331 {wxT("SALMON"), 111, 66, 66},
332 {wxT("SEA GREEN"), 35, 142, 107},
333 {wxT("SIENNA"), 142, 107, 35},
334 {wxT("SKY BLUE"), 50, 153, 204},
335 {wxT("SLATE BLUE"), 0, 127, 255},
336 {wxT("SPRING GREEN"), 0, 255, 127},
337 {wxT("STEEL BLUE"), 35, 107, 142},
338 {wxT("TAN"), 219, 147, 112},
339 {wxT("THISTLE"), 216, 191, 216},
340 {wxT("TURQUOISE"), 173, 234, 234},
341 {wxT("VIOLET"), 79, 47, 79},
342 {wxT("VIOLET RED"), 204, 50, 153},
343 {wxT("WHEAT"), 216, 216, 191},
344 {wxT("WHITE"), 255, 255, 255},
345 {wxT("YELLOW"), 255, 255, 0},
346 {wxT("YELLOW GREEN"), 153, 204, 50}
351 for ( n
= 0; n
< WXSIZEOF(wxColourTable
); n
++ )
353 const wxColourDesc
& cc
= wxColourTable
[n
];
354 (*m_map
)[cc
.name
] = new wxColour(cc
.r
, cc
.g
, cc
.b
);
358 m_palTable
= new long[n
];
359 for ( n
= 0; n
< WXSIZEOF(wxColourTable
); n
++ )
361 const wxColourDesc
& cc
= wxColourTable
[n
];
362 m_palTable
[n
] = OS2RGB(cc
.r
,cc
.g
,cc
.b
);
368 // ----------------------------------------------------------------------------
369 // wxColourDatabase operations
370 // ----------------------------------------------------------------------------
372 void wxColourDatabase::AddColour(const wxString
& name
, const wxColour
& colour
)
376 // canonicalize the colour names before using them as keys: they should be
378 wxString colName
= name
;
381 // ... and we also allow both grey/gray
382 wxString colNameAlt
= colName
;
383 if ( !colNameAlt
.Replace(_T("GRAY"), _T("GREY")) )
385 // but in this case it is not necessary so avoid extra search below
389 wxStringToColourHashMap::iterator it
= m_map
->find(colName
);
390 if ( it
== m_map
->end() && !colNameAlt
.empty() )
391 it
= m_map
->find(colNameAlt
);
392 if ( it
!= m_map
->end() )
394 *(it
->second
) = colour
;
398 (*m_map
)[colName
] = new wxColour(colour
);
402 wxColour
wxColourDatabase::Find(const wxString
& colour
) const
404 wxColourDatabase
* const self
= wxConstCast(this, wxColourDatabase
);
407 // make the comparaison case insensitive and also match both grey and gray
408 wxString colName
= colour
;
410 wxString colNameAlt
= colName
;
411 if ( !colNameAlt
.Replace(_T("GRAY"), _T("GREY")) )
414 wxStringToColourHashMap::iterator it
= m_map
->find(colName
);
415 if ( it
== m_map
->end() && !colNameAlt
.empty() )
416 it
= m_map
->find(colNameAlt
);
417 if ( it
!= m_map
->end() )
418 return *(it
->second
);
420 // we did not find any result in existing colours:
421 // we won't use wxString -> wxColour conversion because the
422 // wxColour::Set(const wxString &) function which does that conversion
423 // internally uses this function (wxColourDatabase::Find) and we want
424 // to avoid infinite recursion !
428 wxString
wxColourDatabase::FindName(const wxColour
& colour
) const
430 wxColourDatabase
* const self
= wxConstCast(this, wxColourDatabase
);
433 typedef wxStringToColourHashMap::iterator iterator
;
435 for ( iterator it
= m_map
->begin(), en
= m_map
->end(); it
!= en
; ++it
)
437 if ( *(it
->second
) == colour
)
441 return wxEmptyString
;
444 // ----------------------------------------------------------------------------
445 // deprecated wxColourDatabase methods
446 // ----------------------------------------------------------------------------
448 #if WXWIN_COMPATIBILITY_2_6
449 wxColour
*wxColourDatabase::FindColour(const wxString
& name
)
451 // This function is deprecated, use Find() instead.
452 // Formerly this function sometimes would return a deletable pointer and
453 // sometimes a non-deletable one (when returning a colour from the database).
454 // Trying to delete the latter anyway results in problems, so probably
455 // nobody ever freed the pointers. Currently it always returns a new
456 // instance, which means there will be memory leaks.
457 wxLogDebug(wxT("wxColourDataBase::FindColour():")
458 wxT(" Please use wxColourDataBase::Find() instead"));
460 // using a static variable here is not the most elegant solution but unless
461 // we want to make wxStringToColourHashMap public (i.e. move it to the
462 // header) so that we could have a member function returning
463 // wxStringToColourHashMap::iterator, there is really no good way to do it
466 // and knowing that this function is going to disappear in the next release
467 // anyhow I don't want to waste time on this
469 static wxColour s_col
;
475 return new wxColour(s_col
);
477 #endif // WXWIN_COMPATIBILITY_2_6
479 // ============================================================================
481 // ============================================================================
483 static wxStockGDI gs_wxStockGDI_instance
;
484 wxStockGDI
* wxStockGDI::ms_instance
= &gs_wxStockGDI_instance
;
485 wxObject
* wxStockGDI::ms_stockObject
[ITEMCOUNT
];
487 wxStockGDI::wxStockGDI()
491 wxStockGDI::~wxStockGDI()
495 void wxStockGDI::DeleteAll()
497 for (unsigned i
= 0; i
< ITEMCOUNT
; i
++)
499 delete ms_stockObject
[i
];
500 ms_stockObject
[i
] = NULL
;
504 const wxBrush
* wxStockGDI::GetBrush(Item item
)
506 wxBrush
* brush
= wx_static_cast(wxBrush
*, ms_stockObject
[item
]);
512 brush
= new wxBrush(*GetColour(COLOUR_BLACK
), wxSOLID
);
515 brush
= new wxBrush(*GetColour(COLOUR_BLUE
), wxSOLID
);
518 brush
= new wxBrush(*GetColour(COLOUR_CYAN
), wxSOLID
);
521 brush
= new wxBrush(*GetColour(COLOUR_GREEN
), wxSOLID
);
524 brush
= new wxBrush(wxColour(wxT("GREY")), wxSOLID
);
526 case BRUSH_LIGHTGREY
:
527 brush
= new wxBrush(*GetColour(COLOUR_LIGHTGREY
), wxSOLID
);
529 case BRUSH_MEDIUMGREY
:
530 brush
= new wxBrush(wxColour(wxT("MEDIUM GREY")), wxSOLID
);
533 brush
= new wxBrush(*GetColour(COLOUR_RED
), wxSOLID
);
535 case BRUSH_TRANSPARENT
:
536 brush
= new wxBrush(*GetColour(COLOUR_BLACK
), wxTRANSPARENT
);
539 brush
= new wxBrush(*GetColour(COLOUR_WHITE
), wxSOLID
);
544 ms_stockObject
[item
] = brush
;
549 const wxColour
* wxStockGDI::GetColour(Item item
)
551 wxColour
* colour
= wx_static_cast(wxColour
*, ms_stockObject
[item
]);
557 colour
= new wxColour(0, 0, 0);
560 colour
= new wxColour(0, 0, 255);
563 colour
= new wxColour(wxT("CYAN"));
566 colour
= new wxColour(0, 255, 0);
568 case COLOUR_LIGHTGREY
:
569 colour
= new wxColour(wxT("LIGHT GREY"));
572 colour
= new wxColour(255, 0, 0);
575 colour
= new wxColour(255, 255, 255);
580 ms_stockObject
[item
] = colour
;
585 const wxCursor
* wxStockGDI::GetCursor(Item item
)
587 wxCursor
* cursor
= wx_static_cast(wxCursor
*, ms_stockObject
[item
]);
593 cursor
= new wxCursor(wxCURSOR_CROSS
);
595 case CURSOR_HOURGLASS
:
596 cursor
= new wxCursor(wxCURSOR_WAIT
);
598 case CURSOR_STANDARD
:
599 cursor
= new wxCursor(wxCURSOR_ARROW
);
604 ms_stockObject
[item
] = cursor
;
609 const wxFont
* wxStockGDI::GetFont(Item item
)
611 wxFont
* font
= wx_static_cast(wxFont
*, ms_stockObject
[item
]);
617 font
= new wxFont(GetFont(FONT_NORMAL
)->GetPointSize(), wxROMAN
, wxITALIC
, wxNORMAL
);
620 font
= new wxFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
623 font
= new wxFont(GetFont(FONT_NORMAL
)->GetPointSize() - 2, wxSWISS
, wxNORMAL
, wxNORMAL
);
626 font
= new wxFont(GetFont(FONT_NORMAL
)->GetPointSize(), wxSWISS
, wxNORMAL
, wxNORMAL
);
631 ms_stockObject
[item
] = font
;
636 const wxPen
* wxStockGDI::GetPen(Item item
)
638 wxPen
* pen
= wx_static_cast(wxPen
*, ms_stockObject
[item
]);
644 pen
= new wxPen(*GetColour(COLOUR_BLACK
), 1, wxSOLID
);
646 case PEN_BLACKDASHED
:
647 pen
= new wxPen(*GetColour(COLOUR_BLACK
), 1, wxSHORT_DASH
);
650 pen
= new wxPen(*GetColour(COLOUR_CYAN
), 1, wxSOLID
);
653 pen
= new wxPen(*GetColour(COLOUR_GREEN
), 1, wxSOLID
);
656 pen
= new wxPen(wxColour(wxT("GREY")), 1, wxSOLID
);
659 pen
= new wxPen(*GetColour(COLOUR_LIGHTGREY
), 1, wxSOLID
);
662 pen
= new wxPen(wxColour(wxT("MEDIUM GREY")), 1, wxSOLID
);
665 pen
= new wxPen(*GetColour(COLOUR_RED
), 1, wxSOLID
);
667 case PEN_TRANSPARENT
:
668 pen
= new wxPen(*GetColour(COLOUR_BLACK
), 1, wxTRANSPARENT
);
671 pen
= new wxPen(*GetColour(COLOUR_WHITE
), 1, wxSOLID
);
676 ms_stockObject
[item
] = pen
;
681 void wxInitializeStockLists()
683 wxTheColourDatabase
= new wxColourDatabase
;
685 wxTheBrushList
= new wxBrushList
;
686 wxThePenList
= new wxPenList
;
687 wxTheFontList
= new wxFontList
;
690 void wxDeleteStockLists()
692 wxDELETE(wxTheBrushList
);
693 wxDELETE(wxThePenList
);
694 wxDELETE(wxTheFontList
);
697 // ============================================================================
698 // wxTheXXXList stuff (semi-obsolete)
699 // ============================================================================
701 wxGDIObjListBase::wxGDIObjListBase()
705 wxGDIObjListBase::~wxGDIObjListBase()
707 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
709 delete wx_static_cast(wxObject
*, node
->GetData());
713 wxPen
*wxPenList::FindOrCreatePen (const wxColour
& colour
, int width
, int style
)
715 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
717 wxPen
*each_pen
= (wxPen
*) node
->GetData ();
719 each_pen
->GetWidth () == width
&&
720 each_pen
->GetStyle () == style
&&
721 each_pen
->GetColour ().Red () == colour
.Red () &&
722 each_pen
->GetColour ().Green () == colour
.Green () &&
723 each_pen
->GetColour ().Blue () == colour
.Blue ())
728 wxPen
penTmp(colour
, width
, style
);
731 pen
= new wxPen(penTmp
);
738 wxBrush
*wxBrushList::FindOrCreateBrush (const wxColour
& colour
, int style
)
740 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
742 wxBrush
*each_brush
= (wxBrush
*) node
->GetData ();
744 each_brush
->GetStyle () == style
&&
745 each_brush
->GetColour ().Red () == colour
.Red () &&
746 each_brush
->GetColour ().Green () == colour
.Green () &&
747 each_brush
->GetColour ().Blue () == colour
.Blue ())
751 wxBrush
* brush
= NULL
;
752 wxBrush
brushTmp(colour
, style
);
755 brush
= new wxBrush(brushTmp
);
762 wxFont
*wxFontList::FindOrCreateFont(int pointSize
,
767 const wxString
& facename
,
768 wxFontEncoding encoding
)
771 wxList::compatibility_iterator node
;
772 for (node
= list
.GetFirst(); node
; node
= node
->GetNext())
774 font
= (wxFont
*)node
->GetData();
776 font
->GetPointSize () == pointSize
&&
777 font
->GetStyle () == style
&&
778 font
->GetWeight () == weight
&&
779 font
->GetUnderlined () == underline
)
781 int fontFamily
= font
->GetFamily();
783 #if defined(__WXGTK__)
784 // under GTK the default family is wxSWISS, so looking for a font
785 // with wxDEFAULT family should return a wxSWISS one instead of
786 // creating a new one
787 bool same
= (fontFamily
== family
) ||
788 (fontFamily
== wxSWISS
&& family
== wxDEFAULT
);
790 // VZ: but why elsewhere do we require an exact match? mystery...
791 bool same
= fontFamily
== family
;
794 // empty facename matches anything at all: this is bad because
795 // depending on which fonts are already created, we might get back
796 // a different font if we create it with empty facename, but it is
797 // still better than never matching anything in the cache at all
799 if ( same
&& !facename
.empty() )
801 const wxString
& fontFace
= font
->GetFaceName();
803 // empty facename matches everything
804 same
= !fontFace
|| fontFace
== facename
;
807 if ( same
&& (encoding
!= wxFONTENCODING_DEFAULT
) )
809 // have to match the encoding too
810 same
= font
->GetEncoding() == encoding
;
820 // font not found, create the new one
822 wxFont
fontTmp(pointSize
, family
, style
, weight
, underline
, facename
, encoding
);
825 font
= new wxFont(fontTmp
);
832 #if WXWIN_COMPATIBILITY_2_6
833 void wxBrushList::AddBrush(wxBrush
*) { }
834 void wxBrushList::RemoveBrush(wxBrush
*) { }
835 void wxFontList::AddFont(wxFont
*) { }
836 void wxFontList::RemoveFont(wxFont
*) { }
837 void wxPenList::AddPen(wxPen
*) { }
838 void wxPenList::RemovePen(wxPen
*) { }
841 wxSize
wxGetDisplaySize()
844 wxDisplaySize(& x
, & y
);
848 wxRect
wxGetClientDisplayRect()
850 int x
, y
, width
, height
;
851 wxClientDisplayRect(&x
, &y
, &width
, &height
); // call plat-specific version
852 return wxRect(x
, y
, width
, height
);
855 wxSize
wxGetDisplaySizeMM()
858 wxDisplaySizeMM(& x
, & y
);
862 wxResourceCache::~wxResourceCache ()
864 wxList::compatibility_iterator node
= GetFirst ();
866 wxObject
*item
= (wxObject
*)node
->GetData();
869 node
= node
->GetNext ();