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 // first look among the existing colours
405 // make the comparaison case insensitive and also match both grey and gray
406 wxString colName
= colour
;
408 wxString colNameAlt
= colName
;
409 if ( !colNameAlt
.Replace(_T("GRAY"), _T("GREY")) )
412 wxStringToColourHashMap::iterator it
= m_map
->find(colName
);
413 if ( it
== m_map
->end() && !colNameAlt
.empty() )
414 it
= m_map
->find(colNameAlt
);
415 if ( it
!= m_map
->end() )
416 return *(it
->second
);
418 // if we didn't find it, query the system, maybe it knows about it
419 #if defined(__WXGTK__) || defined(__X__)
420 wxColour col
= wxColour::CreateByName(colour
);
425 self
->AddColour(colour
, col
);
430 // TODO: move this to wxColour::CreateByName()
434 Display
*display
= XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()) ;
437 Display
* display
= (Display
*) wxGetDisplay();
439 /* MATTHEW: [4] Use wxGetMainColormap */
440 if (!XParseColor(display
, (Colormap
) wxTheApp
->GetMainColormap((WXDisplay
*) display
), colour
.ToAscii() ,&xcolour
))
444 unsigned char r
= (unsigned char)(xcolour
.red
);
445 unsigned char g
= (unsigned char)(xcolour
.green
);
446 unsigned char b
= (unsigned char)(xcolour
.blue
);
448 unsigned char r
= (unsigned char)(xcolour
.red
>> 8);
449 unsigned char g
= (unsigned char)(xcolour
.green
>> 8);
450 unsigned char b
= (unsigned char)(xcolour
.blue
>> 8);
453 wxColour
col(r
, g
, b
);
454 AddColour(colour
, col
);
457 #else // other platform
462 wxString
wxColourDatabase::FindName(const wxColour
& colour
) const
464 wxColourDatabase
* const self
= wxConstCast(this, wxColourDatabase
);
467 typedef wxStringToColourHashMap::iterator iterator
;
469 for ( iterator it
= m_map
->begin(), en
= m_map
->end(); it
!= en
; ++it
)
471 if ( *(it
->second
) == colour
)
475 return wxEmptyString
;
478 // ----------------------------------------------------------------------------
479 // deprecated wxColourDatabase methods
480 // ----------------------------------------------------------------------------
482 #if WXWIN_COMPATIBILITY_2_6
483 wxColour
*wxColourDatabase::FindColour(const wxString
& name
)
485 // This function is deprecated, use Find() instead.
486 // Formerly this function sometimes would return a deletable pointer and
487 // sometimes a non-deletable one (when returning a colour from the database).
488 // Trying to delete the latter anyway results in problems, so probably
489 // nobody ever freed the pointers. Currently it always returns a new
490 // instance, which means there will be memory leaks.
491 wxLogDebug(wxT("wxColourDataBase::FindColour():")
492 wxT(" Please use wxColourDataBase::Find() instead"));
494 // using a static variable here is not the most elegant solution but unless
495 // we want to make wxStringToColourHashMap public (i.e. move it to the
496 // header) so that we could have a member function returning
497 // wxStringToColourHashMap::iterator, there is really no good way to do it
500 // and knowing that this function is going to disappear in the next release
501 // anyhow I don't want to waste time on this
503 static wxColour s_col
;
509 return new wxColour(s_col
);
511 #endif // WXWIN_COMPATIBILITY_2_6
513 // ============================================================================
515 // ============================================================================
517 static wxStockGDI gs_wxStockGDI_instance
;
518 wxStockGDI
* wxStockGDI::ms_instance
= &gs_wxStockGDI_instance
;
519 wxObject
* wxStockGDI::ms_stockObject
[ITEMCOUNT
];
521 wxStockGDI::wxStockGDI()
525 wxStockGDI::~wxStockGDI()
529 void wxStockGDI::DeleteAll()
531 for (unsigned i
= 0; i
< ITEMCOUNT
; i
++)
533 delete ms_stockObject
[i
];
534 ms_stockObject
[i
] = NULL
;
538 const wxBrush
* wxStockGDI::GetBrush(Item item
)
540 wxBrush
* brush
= wx_static_cast(wxBrush
*, ms_stockObject
[item
]);
546 brush
= new wxBrush(*GetColour(COLOUR_BLACK
), wxSOLID
);
549 brush
= new wxBrush(*GetColour(COLOUR_BLUE
), wxSOLID
);
552 brush
= new wxBrush(*GetColour(COLOUR_CYAN
), wxSOLID
);
555 brush
= new wxBrush(*GetColour(COLOUR_GREEN
), wxSOLID
);
558 brush
= new wxBrush(wxColour(wxT("GREY")), wxSOLID
);
560 case BRUSH_LIGHTGREY
:
561 brush
= new wxBrush(*GetColour(COLOUR_LIGHTGREY
), wxSOLID
);
563 case BRUSH_MEDIUMGREY
:
564 brush
= new wxBrush(wxColour(wxT("MEDIUM GREY")), wxSOLID
);
567 brush
= new wxBrush(*GetColour(COLOUR_RED
), wxSOLID
);
569 case BRUSH_TRANSPARENT
:
570 brush
= new wxBrush(*GetColour(COLOUR_BLACK
), wxTRANSPARENT
);
573 brush
= new wxBrush(*GetColour(COLOUR_WHITE
), wxSOLID
);
578 ms_stockObject
[item
] = brush
;
583 const wxColour
* wxStockGDI::GetColour(Item item
)
585 wxColour
* colour
= wx_static_cast(wxColour
*, ms_stockObject
[item
]);
591 colour
= new wxColour(0, 0, 0);
594 colour
= new wxColour(0, 0, 255);
597 colour
= new wxColour(wxT("CYAN"));
600 colour
= new wxColour(0, 255, 0);
602 case COLOUR_LIGHTGREY
:
603 colour
= new wxColour(wxT("LIGHT GREY"));
606 colour
= new wxColour(255, 0, 0);
609 colour
= new wxColour(255, 255, 255);
614 ms_stockObject
[item
] = colour
;
619 const wxCursor
* wxStockGDI::GetCursor(Item item
)
621 wxCursor
* cursor
= wx_static_cast(wxCursor
*, ms_stockObject
[item
]);
627 cursor
= new wxCursor(wxCURSOR_CROSS
);
629 case CURSOR_HOURGLASS
:
630 cursor
= new wxCursor(wxCURSOR_WAIT
);
632 case CURSOR_STANDARD
:
633 cursor
= new wxCursor(wxCURSOR_ARROW
);
638 ms_stockObject
[item
] = cursor
;
643 const wxFont
* wxStockGDI::GetFont(Item item
)
645 wxFont
* font
= wx_static_cast(wxFont
*, ms_stockObject
[item
]);
651 font
= new wxFont(GetFont(FONT_NORMAL
)->GetPointSize(), wxROMAN
, wxITALIC
, wxNORMAL
);
654 font
= new wxFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
657 font
= new wxFont(GetFont(FONT_NORMAL
)->GetPointSize() - 2, wxSWISS
, wxNORMAL
, wxNORMAL
);
660 font
= new wxFont(GetFont(FONT_NORMAL
)->GetPointSize(), wxSWISS
, wxNORMAL
, wxNORMAL
);
665 ms_stockObject
[item
] = font
;
670 const wxPen
* wxStockGDI::GetPen(Item item
)
672 wxPen
* pen
= wx_static_cast(wxPen
*, ms_stockObject
[item
]);
678 pen
= new wxPen(*GetColour(COLOUR_BLACK
), 1, wxSOLID
);
680 case PEN_BLACKDASHED
:
681 pen
= new wxPen(*GetColour(COLOUR_BLACK
), 1, wxSHORT_DASH
);
684 pen
= new wxPen(*GetColour(COLOUR_CYAN
), 1, wxSOLID
);
687 pen
= new wxPen(*GetColour(COLOUR_GREEN
), 1, wxSOLID
);
690 pen
= new wxPen(wxColour(wxT("GREY")), 1, wxSOLID
);
693 pen
= new wxPen(*GetColour(COLOUR_LIGHTGREY
), 1, wxSOLID
);
696 pen
= new wxPen(wxColour(wxT("MEDIUM GREY")), 1, wxSOLID
);
699 pen
= new wxPen(*GetColour(COLOUR_RED
), 1, wxSOLID
);
701 case PEN_TRANSPARENT
:
702 pen
= new wxPen(*GetColour(COLOUR_BLACK
), 1, wxTRANSPARENT
);
705 pen
= new wxPen(*GetColour(COLOUR_WHITE
), 1, wxSOLID
);
710 ms_stockObject
[item
] = pen
;
715 void wxInitializeStockLists()
717 wxTheColourDatabase
= new wxColourDatabase
;
719 wxTheBrushList
= new wxBrushList
;
720 wxThePenList
= new wxPenList
;
721 wxTheFontList
= new wxFontList
;
724 void wxDeleteStockLists()
726 wxDELETE(wxTheBrushList
);
727 wxDELETE(wxThePenList
);
728 wxDELETE(wxTheFontList
);
731 // ============================================================================
732 // wxTheXXXList stuff (semi-obsolete)
733 // ============================================================================
735 wxGDIObjListBase::wxGDIObjListBase()
739 wxGDIObjListBase::~wxGDIObjListBase()
741 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
743 delete wx_static_cast(wxObject
*, node
->GetData());
747 wxPen
*wxPenList::FindOrCreatePen (const wxColour
& colour
, int width
, int style
)
749 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
751 wxPen
*each_pen
= (wxPen
*) node
->GetData ();
753 each_pen
->GetWidth () == width
&&
754 each_pen
->GetStyle () == style
&&
755 each_pen
->GetColour ().Red () == colour
.Red () &&
756 each_pen
->GetColour ().Green () == colour
.Green () &&
757 each_pen
->GetColour ().Blue () == colour
.Blue ())
762 wxPen
penTmp(colour
, width
, style
);
765 pen
= new wxPen(penTmp
);
772 wxBrush
*wxBrushList::FindOrCreateBrush (const wxColour
& colour
, int style
)
774 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
776 wxBrush
*each_brush
= (wxBrush
*) node
->GetData ();
778 each_brush
->GetStyle () == style
&&
779 each_brush
->GetColour ().Red () == colour
.Red () &&
780 each_brush
->GetColour ().Green () == colour
.Green () &&
781 each_brush
->GetColour ().Blue () == colour
.Blue ())
785 wxBrush
* brush
= NULL
;
786 wxBrush
brushTmp(colour
, style
);
789 brush
= new wxBrush(brushTmp
);
796 wxFont
*wxFontList::FindOrCreateFont(int pointSize
,
801 const wxString
& facename
,
802 wxFontEncoding encoding
)
805 wxList::compatibility_iterator node
;
806 for (node
= list
.GetFirst(); node
; node
= node
->GetNext())
808 font
= (wxFont
*)node
->GetData();
810 font
->GetPointSize () == pointSize
&&
811 font
->GetStyle () == style
&&
812 font
->GetWeight () == weight
&&
813 font
->GetUnderlined () == underline
)
815 int fontFamily
= font
->GetFamily();
817 #if defined(__WXGTK__)
818 // under GTK the default family is wxSWISS, so looking for a font
819 // with wxDEFAULT family should return a wxSWISS one instead of
820 // creating a new one
821 bool same
= (fontFamily
== family
) ||
822 (fontFamily
== wxSWISS
&& family
== wxDEFAULT
);
824 // VZ: but why elsewhere do we require an exact match? mystery...
825 bool same
= fontFamily
== family
;
828 // empty facename matches anything at all: this is bad because
829 // depending on which fonts are already created, we might get back
830 // a different font if we create it with empty facename, but it is
831 // still better than never matching anything in the cache at all
833 if ( same
&& !facename
.empty() )
835 const wxString
& fontFace
= font
->GetFaceName();
837 // empty facename matches everything
838 same
= !fontFace
|| fontFace
== facename
;
841 if ( same
&& (encoding
!= wxFONTENCODING_DEFAULT
) )
843 // have to match the encoding too
844 same
= font
->GetEncoding() == encoding
;
854 // font not found, create the new one
856 wxFont
fontTmp(pointSize
, family
, style
, weight
, underline
, facename
, encoding
);
859 font
= new wxFont(fontTmp
);
866 #if WXWIN_COMPATIBILITY_2_6
867 void wxBrushList::AddBrush(wxBrush
*) { }
868 void wxBrushList::RemoveBrush(wxBrush
*) { }
869 void wxFontList::AddFont(wxFont
*) { }
870 void wxFontList::RemoveFont(wxFont
*) { }
871 void wxPenList::AddPen(wxPen
*) { }
872 void wxPenList::RemovePen(wxPen
*) { }
875 wxSize
wxGetDisplaySize()
878 wxDisplaySize(& x
, & y
);
882 wxRect
wxGetClientDisplayRect()
884 int x
, y
, width
, height
;
885 wxClientDisplayRect(&x
, &y
, &width
, &height
); // call plat-specific version
886 return wxRect(x
, y
, width
, height
);
889 wxSize
wxGetDisplaySizeMM()
892 wxDisplaySizeMM(& x
, & y
);
896 wxResourceCache::~wxResourceCache ()
898 wxList::compatibility_iterator node
= GetFirst ();
900 wxObject
*item
= (wxObject
*)node
->GetData();
903 node
= node
->GetNext ();