1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Common GDI classes
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "gdicmn.h"
17 #define XtDisplay XTDISPLAY
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
28 #include "wx/gdicmn.h"
31 #include "wx/bitmap.h"
33 #include "wx/cursor.h"
35 #include "wx/palette.h"
39 #include "wx/settings.h"
50 #pragma message disable nosimpint
54 #pragma message enable nosimpint
62 IMPLEMENT_CLASS(wxColourDatabase
, wxList
)
63 IMPLEMENT_DYNAMIC_CLASS(wxFontList
, wxList
)
64 IMPLEMENT_DYNAMIC_CLASS(wxPenList
, wxList
)
65 IMPLEMENT_DYNAMIC_CLASS(wxBrushList
, wxList
)
66 IMPLEMENT_DYNAMIC_CLASS(wxBitmapList
, wxList
)
67 IMPLEMENT_DYNAMIC_CLASS(wxResourceCache
, wxList
)
69 IMPLEMENT_ABSTRACT_CLASS(wxDCBase
, wxObject
)
71 wxRect::wxRect(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
75 width
= bottomRight
.x
- topLeft
.x
+ 1;
76 height
= bottomRight
.y
- topLeft
.y
+ 1;
91 wxRect::wxRect(const wxPoint
& point
, const wxSize
& size
)
93 x
= point
.x
; y
= point
.y
;
94 width
= size
.x
; height
= size
.y
;
97 bool wxRect::operator==(const wxRect
& rect
) const
99 return ((x
== rect
.x
) &&
101 (width
== rect
.width
) &&
102 (height
== rect
.height
));
105 wxRect
& wxRect::operator += (const wxRect
& rect
)
107 *this = (*this + rect
);
111 wxRect
wxRect::operator + (const wxRect
& rect
) const
113 int x1
= wxMin(this->x
, rect
.x
);
114 int y1
= wxMin(this->y
, rect
.y
);
115 int y2
= wxMax(y
+height
, rect
.height
+rect
.y
);
116 int x2
= wxMax(x
+width
, rect
.width
+rect
.x
);
117 return wxRect(x1
, y1
, x2
-x1
, y2
-y1
);
120 wxRect
& wxRect::Inflate(wxCoord dx
, wxCoord dy
)
127 // check that we didn't make the rectangle invalid by accident (you almost
128 // never want to have negative coords and never want negative size)
134 // what else can we do?
143 bool wxRect::Inside(int cx
, int cy
) const
145 return ( (cx
>= x
) && (cy
>= y
)
146 && ((cy
- y
) < height
)
147 && ((cx
- x
) < width
)
151 wxRect
& wxRect::Intersect(const wxRect
& rect
)
160 if ( x2
> rect
.GetRight() )
161 x2
= rect
.GetRight();
162 if ( y2
> rect
.GetBottom() )
163 y2
= rect
.GetBottom();
168 if ( width
<= 0 || height
<= 0 )
177 bool wxRect::Intersects(const wxRect
& rect
) const
179 wxRect r
= Intersect(rect
);
181 // if there is no intersection, both width and height are 0
185 wxColourDatabase::wxColourDatabase (int type
) : wxList (type
)
189 wxColourDatabase::~wxColourDatabase ()
191 // Cleanup Colour allocated in Initialize()
192 wxNode
*node
= First ();
195 wxColour
*col
= (wxColour
*) node
->Data ();
196 wxNode
*next
= node
->Next ();
201 delete [] m_palTable
;
205 // Colour database stuff
206 void wxColourDatabase::Initialize ()
208 static const struct wxColourDesc
215 {wxT("AQUAMARINE"),112, 219, 147},
216 {wxT("BLACK"),0, 0, 0},
217 {wxT("BLUE"), 0, 0, 255},
218 {wxT("BLUE VIOLET"), 159, 95, 159},
219 {wxT("BROWN"), 165, 42, 42},
220 {wxT("CADET BLUE"), 95, 159, 159},
221 {wxT("CORAL"), 255, 127, 0},
222 {wxT("CORNFLOWER BLUE"), 66, 66, 111},
223 {wxT("CYAN"), 0, 255, 255},
224 {wxT("DARK GREY"), 47, 47, 47}, // ?
226 {wxT("DARK GREEN"), 47, 79, 47},
227 {wxT("DARK OLIVE GREEN"), 79, 79, 47},
228 {wxT("DARK ORCHID"), 153, 50, 204},
229 {wxT("DARK SLATE BLUE"), 107, 35, 142},
230 {wxT("DARK SLATE GREY"), 47, 79, 79},
231 {wxT("DARK TURQUOISE"), 112, 147, 219},
232 {wxT("DIM GREY"), 84, 84, 84},
233 {wxT("FIREBRICK"), 142, 35, 35},
234 {wxT("FOREST GREEN"), 35, 142, 35},
235 {wxT("GOLD"), 204, 127, 50},
236 {wxT("GOLDENROD"), 219, 219, 112},
237 {wxT("GREY"), 128, 128, 128},
238 {wxT("GREEN"), 0, 255, 0},
239 {wxT("GREEN YELLOW"), 147, 219, 112},
240 {wxT("INDIAN RED"), 79, 47, 47},
241 {wxT("KHAKI"), 159, 159, 95},
242 {wxT("LIGHT BLUE"), 191, 216, 216},
243 {wxT("LIGHT GREY"), 192, 192, 192},
244 {wxT("LIGHT STEEL BLUE"), 143, 143, 188},
245 {wxT("LIME GREEN"), 50, 204, 50},
246 {wxT("LIGHT MAGENTA"), 255, 0, 255},
247 {wxT("MAGENTA"), 255, 0, 255},
248 {wxT("MAROON"), 142, 35, 107},
249 {wxT("MEDIUM AQUAMARINE"), 50, 204, 153},
250 {wxT("MEDIUM GREY"), 100, 100, 100},
251 {wxT("MEDIUM BLUE"), 50, 50, 204},
252 {wxT("MEDIUM FOREST GREEN"), 107, 142, 35},
253 {wxT("MEDIUM GOLDENROD"), 234, 234, 173},
254 {wxT("MEDIUM ORCHID"), 147, 112, 219},
255 {wxT("MEDIUM SEA GREEN"), 66, 111, 66},
256 {wxT("MEDIUM SLATE BLUE"), 127, 0, 255},
257 {wxT("MEDIUM SPRING GREEN"), 127, 255, 0},
258 {wxT("MEDIUM TURQUOISE"), 112, 219, 219},
259 {wxT("MEDIUM VIOLET RED"), 219, 112, 147},
260 {wxT("MIDNIGHT BLUE"), 47, 47, 79},
261 {wxT("NAVY"), 35, 35, 142},
262 {wxT("ORANGE"), 204, 50, 50},
263 {wxT("ORANGE RED"), 255, 0, 127},
264 {wxT("ORCHID"), 219, 112, 219},
265 {wxT("PALE GREEN"), 143, 188, 143},
266 {wxT("PINK"), 188, 143, 234},
267 {wxT("PLUM"), 234, 173, 234},
268 {wxT("PURPLE"), 176, 0, 255},
269 {wxT("RED"), 255, 0, 0},
270 {wxT("SALMON"), 111, 66, 66},
271 {wxT("SEA GREEN"), 35, 142, 107},
272 {wxT("SIENNA"), 142, 107, 35},
273 {wxT("SKY BLUE"), 50, 153, 204},
274 {wxT("SLATE BLUE"), 0, 127, 255},
275 {wxT("SPRING GREEN"), 0, 255, 127},
276 {wxT("STEEL BLUE"), 35, 107, 142},
277 {wxT("TAN"), 219, 147, 112},
278 {wxT("THISTLE"), 216, 191, 216},
279 {wxT("TURQUOISE"), 173, 234, 234},
280 {wxT("VIOLET"), 79, 47, 79},
281 {wxT("VIOLET RED"), 204, 50, 153},
282 {wxT("WHEAT"), 216, 216, 191},
283 {wxT("WHITE"), 255, 255, 255},
284 {wxT("YELLOW"), 255, 255, 0},
285 {wxT("YELLOW GREEN"), 153, 204, 50},
286 {wxT("MEDIUM GOLDENROD"), 234, 234, 173},
287 {wxT("MEDIUM FOREST GREEN"), 107, 142, 35},
288 {wxT("LIGHT MAGENTA"), 255, 0, 255},
289 {wxT("MEDIUM GREY"), 100, 100, 100},
294 for ( n
= 0; n
< WXSIZEOF(wxColourTable
); n
++ )
296 const wxColourDesc
& cc
= wxColourTable
[n
];
297 Append(cc
.name
, new wxColour(cc
.r
,cc
.g
,cc
.b
));
300 m_palTable
= new long[n
];
301 for ( n
= 0; n
< WXSIZEOF(wxColourTable
); n
++ )
303 const wxColourDesc
& cc
= wxColourTable
[n
];
304 m_palTable
[n
] = OS2RGB(cc
.r
,cc
.g
,cc
.b
);
311 * Changed by Ian Brown, July 1994.
313 * When running under X, the Colour Database starts off empty. The X server
314 * is queried for the colour first time after which it is entered into the
315 * database. This allows our client to use the server colour database which
316 * is hopefully gamma corrected for the display being used.
319 wxColour
*wxColourDatabase::FindColour(const wxString
& colour
)
321 // VZ: make the comparaison case insensitive and also match both grey and
323 wxString colName
= colour
;
325 wxString colName2
= colName
;
326 if ( !colName2
.Replace(_T("GRAY"), _T("GREY")) )
329 wxNode
*node
= First();
332 const wxChar
*key
= node
->GetKeyString();
333 if ( colName
== key
|| colName2
== key
)
335 return (wxColour
*)node
->Data();
351 // TODO for other implementations. This should really go into
352 // platform-specific directories.
361 wxColour
*col
= new wxColour( colour
);
365 return (wxColour
*) NULL
;
367 Append( colour
, col
);
375 Display
*display
= XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()) ;
378 Display
* display
= (Display
*) wxGetDisplay();
380 /* MATTHEW: [4] Use wxGetMainColormap */
381 if (!XParseColor(display
, (Colormap
) wxTheApp
->GetMainColormap((WXDisplay
*) display
), colour
,&xcolour
))
385 unsigned char r
= (unsigned char)(xcolour
.red
);
386 unsigned char g
= (unsigned char)(xcolour
.green
);
387 unsigned char b
= (unsigned char)(xcolour
.blue
);
389 unsigned char r
= (unsigned char)(xcolour
.red
>> 8);
390 unsigned char g
= (unsigned char)(xcolour
.green
>> 8);
391 unsigned char b
= (unsigned char)(xcolour
.blue
>> 8);
394 wxColour
*col
= new wxColour(r
, g
, b
);
401 wxString
wxColourDatabase::FindName (const wxColour
& colour
) const
405 unsigned char red
= colour
.Red ();
406 unsigned char green
= colour
.Green ();
407 unsigned char blue
= colour
.Blue ();
409 for (wxNode
* node
= First (); node
; node
= node
->Next ())
411 wxColour
*col
= (wxColour
*) node
->Data ();
413 if (col
->Red () == red
&& col
->Green () == green
&& col
->Blue () == blue
)
415 const wxChar
*found
= node
->GetKeyString();
428 void wxInitializeStockLists()
430 wxTheBrushList
= new wxBrushList
;
431 wxThePenList
= new wxPenList
;
432 wxTheFontList
= new wxFontList
;
433 wxTheBitmapList
= new wxBitmapList
;
436 void wxInitializeStockObjects ()
442 // wxFontPool = new XFontPool;
445 // why under MSW fonts shouldn't have the standard system size?
448 static const int sizeFont = 10;
452 #if defined(__WXMAC__)
453 static const int sizeFont
= 12;
454 wxNORMAL_FONT
= new wxFont (sizeFont
, wxMODERN
, wxNORMAL
, wxNORMAL
);
455 #elif defined(__WXPM__)
456 static const int sizeFont
= 12;
458 wxNORMAL_FONT
= new wxFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
459 static const int sizeFont
= wxNORMAL_FONT
->GetPointSize();
462 #if defined(__WXPM__)
464 // Basic OS/2 has a fairly limited number of fonts and these are as good
465 // as I can do to get something that looks halfway "wx" normal
467 wxNORMAL_FONT
= new wxFont (sizeFont
, wxMODERN
, wxNORMAL
, wxNORMAL
); /* System VIO */
468 wxSMALL_FONT
= new wxFont (sizeFont
- 4, wxMODERN
, wxNORMAL
, wxNORMAL
); /* System VIO */
469 wxITALIC_FONT
= new wxFont (sizeFont
, wxROMAN
, wxITALIC
, wxNORMAL
);
470 wxSWISS_FONT
= new wxFont (sizeFont
, wxSWISS
, wxNORMAL
, wxNORMAL
); /* Helv */
472 wxSMALL_FONT
= new wxFont (sizeFont
- 2, wxSWISS
, wxNORMAL
, wxNORMAL
);
473 wxITALIC_FONT
= new wxFont (sizeFont
, wxROMAN
, wxITALIC
, wxNORMAL
);
474 wxSWISS_FONT
= new wxFont (sizeFont
, wxSWISS
, wxNORMAL
, wxNORMAL
);
477 wxRED_PEN
= new wxPen (wxT("RED"), 1, wxSOLID
);
478 wxCYAN_PEN
= new wxPen (wxT("CYAN"), 1, wxSOLID
);
479 wxGREEN_PEN
= new wxPen (wxT("GREEN"), 1, wxSOLID
);
480 wxBLACK_PEN
= new wxPen (wxT("BLACK"), 1, wxSOLID
);
481 wxWHITE_PEN
= new wxPen (wxT("WHITE"), 1, wxSOLID
);
482 wxTRANSPARENT_PEN
= new wxPen (wxT("BLACK"), 1, wxTRANSPARENT
);
483 wxBLACK_DASHED_PEN
= new wxPen (wxT("BLACK"), 1, wxSHORT_DASH
);
484 wxGREY_PEN
= new wxPen (wxT("GREY"), 1, wxSOLID
);
485 wxMEDIUM_GREY_PEN
= new wxPen (wxT("MEDIUM GREY"), 1, wxSOLID
);
486 wxLIGHT_GREY_PEN
= new wxPen (wxT("LIGHT GREY"), 1, wxSOLID
);
488 wxBLUE_BRUSH
= new wxBrush (wxT("BLUE"), wxSOLID
);
489 wxGREEN_BRUSH
= new wxBrush (wxT("GREEN"), wxSOLID
);
490 wxWHITE_BRUSH
= new wxBrush (wxT("WHITE"), wxSOLID
);
491 wxBLACK_BRUSH
= new wxBrush (wxT("BLACK"), wxSOLID
);
492 wxTRANSPARENT_BRUSH
= new wxBrush (wxT("BLACK"), wxTRANSPARENT
);
493 wxCYAN_BRUSH
= new wxBrush (wxT("CYAN"), wxSOLID
);
494 wxRED_BRUSH
= new wxBrush (wxT("RED"), wxSOLID
);
495 wxGREY_BRUSH
= new wxBrush (wxT("GREY"), wxSOLID
);
496 wxMEDIUM_GREY_BRUSH
= new wxBrush (wxT("MEDIUM GREY"), wxSOLID
);
497 wxLIGHT_GREY_BRUSH
= new wxBrush (wxT("LIGHT GREY"), wxSOLID
);
499 wxBLACK
= new wxColour (wxT("BLACK"));
500 wxWHITE
= new wxColour (wxT("WHITE"));
501 wxRED
= new wxColour (wxT("RED"));
502 wxBLUE
= new wxColour (wxT("BLUE"));
503 wxGREEN
= new wxColour (wxT("GREEN"));
504 wxCYAN
= new wxColour (wxT("CYAN"));
505 wxLIGHT_GREY
= new wxColour (wxT("LIGHT GREY"));
507 wxSTANDARD_CURSOR
= new wxCursor (wxCURSOR_ARROW
);
508 wxHOURGLASS_CURSOR
= new wxCursor (wxCURSOR_WAIT
);
509 wxCROSS_CURSOR
= new wxCursor (wxCURSOR_CROSS
);
512 void wxDeleteStockObjects ()
514 wxDELETE(wxNORMAL_FONT
);
515 wxDELETE(wxSMALL_FONT
);
516 wxDELETE(wxITALIC_FONT
);
517 wxDELETE(wxSWISS_FONT
);
520 wxDELETE(wxCYAN_PEN
);
521 wxDELETE(wxGREEN_PEN
);
522 wxDELETE(wxBLACK_PEN
);
523 wxDELETE(wxWHITE_PEN
);
524 wxDELETE(wxTRANSPARENT_PEN
);
525 wxDELETE(wxBLACK_DASHED_PEN
);
526 wxDELETE(wxGREY_PEN
);
527 wxDELETE(wxMEDIUM_GREY_PEN
);
528 wxDELETE(wxLIGHT_GREY_PEN
);
530 wxDELETE(wxBLUE_BRUSH
);
531 wxDELETE(wxGREEN_BRUSH
);
532 wxDELETE(wxWHITE_BRUSH
);
533 wxDELETE(wxBLACK_BRUSH
);
534 wxDELETE(wxTRANSPARENT_BRUSH
);
535 wxDELETE(wxCYAN_BRUSH
);
536 wxDELETE(wxRED_BRUSH
);
537 wxDELETE(wxGREY_BRUSH
);
538 wxDELETE(wxMEDIUM_GREY_BRUSH
);
539 wxDELETE(wxLIGHT_GREY_BRUSH
);
547 wxDELETE(wxLIGHT_GREY
);
549 wxDELETE(wxSTANDARD_CURSOR
);
550 wxDELETE(wxHOURGLASS_CURSOR
);
551 wxDELETE(wxCROSS_CURSOR
);
554 void wxDeleteStockLists()
556 wxDELETE(wxTheBrushList
);
557 wxDELETE(wxThePenList
);
558 wxDELETE(wxTheFontList
);
559 wxDELETE(wxTheBitmapList
);
562 // ============================================================================
563 // wxTheXXXList stuff (semi-obsolete)
564 // ============================================================================
566 wxBitmapList::wxBitmapList()
570 wxBitmapList::~wxBitmapList ()
572 wxNode
*node
= First ();
575 wxBitmap
*bitmap
= (wxBitmap
*) node
->Data ();
576 wxNode
*next
= node
->Next ();
577 if (bitmap
->GetVisible())
583 // Pen and Brush lists
584 wxPenList::~wxPenList ()
586 wxNode
*node
= First ();
589 wxPen
*pen
= (wxPen
*) node
->Data ();
590 wxNode
*next
= node
->Next ();
591 if (pen
->GetVisible())
597 void wxPenList::AddPen (wxPen
* pen
)
602 void wxPenList::RemovePen (wxPen
* pen
)
607 wxPen
*wxPenList::FindOrCreatePen (const wxColour
& colour
, int width
, int style
)
609 for (wxNode
* node
= First (); node
; node
= node
->Next ())
611 wxPen
*each_pen
= (wxPen
*) node
->Data ();
613 each_pen
->GetVisible() &&
614 each_pen
->GetWidth () == width
&&
615 each_pen
->GetStyle () == style
&&
616 each_pen
->GetColour ().Red () == colour
.Red () &&
617 each_pen
->GetColour ().Green () == colour
.Green () &&
618 each_pen
->GetColour ().Blue () == colour
.Blue ())
622 wxPen
*pen
= new wxPen (colour
, width
, style
);
625 // don't save the invalid pens in the list
633 // we'll delete it ourselves later
634 pen
->SetVisible(TRUE
);
639 wxBrushList::~wxBrushList ()
641 wxNode
*node
= First ();
644 wxBrush
*brush
= (wxBrush
*) node
->Data ();
645 wxNode
*next
= node
->Next ();
646 if (brush
&& brush
->GetVisible())
652 void wxBrushList::AddBrush (wxBrush
* brush
)
657 wxBrush
*wxBrushList::FindOrCreateBrush (const wxColour
& colour
, int style
)
659 for (wxNode
* node
= First (); node
; node
= node
->Next ())
661 wxBrush
*each_brush
= (wxBrush
*) node
->Data ();
663 each_brush
->GetVisible() &&
664 each_brush
->GetStyle () == style
&&
665 each_brush
->GetColour ().Red () == colour
.Red () &&
666 each_brush
->GetColour ().Green () == colour
.Green () &&
667 each_brush
->GetColour ().Blue () == colour
.Blue ())
671 wxBrush
*brush
= new wxBrush (colour
, style
);
675 // don't put the brushes we failed to create into the list
683 // we'll delete it ourselves later
684 brush
->SetVisible(TRUE
);
689 void wxBrushList::RemoveBrush (wxBrush
* brush
)
691 DeleteObject (brush
);
694 wxFontList::~wxFontList ()
696 wxNode
*node
= First ();
699 // Only delete objects that are 'visible', i.e.
700 // that have been created using FindOrCreate...,
701 // where the pointers are expected to be shared
702 // (and therefore not deleted by any one part of an app).
703 wxFont
*font
= (wxFont
*) node
->Data ();
704 wxNode
*next
= node
->Next ();
705 if (font
->GetVisible())
711 void wxFontList::AddFont (wxFont
* font
)
716 void wxFontList::RemoveFont (wxFont
* font
)
721 wxFont
*wxFontList::FindOrCreateFont(int pointSize
,
726 const wxString
& facename
,
727 wxFontEncoding encoding
)
729 wxFont
*font
= (wxFont
*)NULL
;
731 for ( node
= First(); node
; node
= node
->Next() )
733 font
= (wxFont
*)node
->Data();
734 if ( font
->GetVisible() &&
736 font
->GetPointSize () == pointSize
&&
737 font
->GetStyle () == style
&&
738 font
->GetWeight () == weight
&&
739 font
->GetUnderlined () == underline
)
741 int fontFamily
= font
->GetFamily();
743 #if defined(__WXGTK__)
744 // under GTK the default family is wxSWISS, so looking for a font
745 // with wxDEFAULT family should return a wxSWISS one instead of
746 // creating a new one
747 bool same
= (fontFamily
== family
) ||
748 (fontFamily
== wxSWISS
&& family
== wxDEFAULT
);
750 // VZ: but why elsewhere do we require an exact match? mystery...
751 bool same
= fontFamily
== family
;
754 // empty facename matches anything at all: this is bad because
755 // depending on which fonts are already created, we might get back
756 // a different font if we create it with empty facename, but it is
757 // still better than never matching anything in the cache at all
759 if ( same
&& !!facename
)
761 const wxString
& fontFace
= font
->GetFaceName();
763 // empty facename matches everything
764 same
= !fontFace
|| fontFace
== facename
;
767 if ( same
&& (encoding
!= wxFONTENCODING_DEFAULT
) )
769 // have to match the encoding too
770 same
= font
->GetEncoding() == encoding
;
782 // font not found, create the new one
783 font
= new wxFont(pointSize
, family
, style
, weight
,
784 underline
, facename
, encoding
);
788 // and mark it as being cacheable
789 font
->SetVisible(TRUE
);
795 void wxBitmapList::AddBitmap(wxBitmap
*bitmap
)
800 void wxBitmapList::RemoveBitmap(wxBitmap
*bitmap
)
802 DeleteObject(bitmap
);
805 wxSize
wxGetDisplaySize()
808 wxDisplaySize(& x
, & y
);
812 wxRect
wxGetClientDisplayRect()
814 int x
, y
, width
, height
;
815 wxClientDisplayRect(&x
, &y
, &width
, &height
); // call plat-specific version
816 return wxRect(x
, y
, width
, height
);
819 wxSize
wxGetDisplaySizeMM()
822 wxDisplaySizeMM(& x
, & y
);
826 wxResourceCache::~wxResourceCache ()
828 wxNode
*node
= First ();
830 wxObject
*item
= (wxObject
*)node
->Data();
833 node
= node
->Next ();