]>
git.saurik.com Git - wxWidgets.git/blob - src/common/gdicmn.cpp
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"
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"
45 #pragma message disable nosimpint
49 #pragma message enable nosimpint
53 IMPLEMENT_CLASS(wxColourDatabase
, wxList
)
54 IMPLEMENT_DYNAMIC_CLASS(wxFontList
, wxList
)
55 IMPLEMENT_DYNAMIC_CLASS(wxPenList
, wxList
)
56 IMPLEMENT_DYNAMIC_CLASS(wxBrushList
, wxList
)
57 IMPLEMENT_DYNAMIC_CLASS(wxBitmapList
, wxList
)
58 IMPLEMENT_DYNAMIC_CLASS(wxResourceCache
, wxList
)
60 IMPLEMENT_ABSTRACT_CLASS(wxDCBase
, wxObject
)
62 wxRect::wxRect(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
66 width
= bottomRight
.x
- topLeft
.x
;
67 height
= bottomRight
.y
- topLeft
.y
;
82 wxRect::wxRect(const wxPoint
& point
, const wxSize
& size
)
84 x
= point
.x
; y
= point
.y
;
85 width
= size
.x
; height
= size
.y
;
88 bool wxRect::operator==(const wxRect
& rect
) const
90 return ((x
== rect
.x
) &&
92 (width
== rect
.width
) &&
93 (height
== rect
.height
));
96 wxRect
& wxRect::operator += (const wxRect
& rect
)
98 *this = (*this + rect
);
102 wxRect
wxRect::operator + (const wxRect
& rect
) const
104 int x1
= wxMin(this->x
, rect
.x
);
105 int y1
= wxMin(this->y
, rect
.y
);
106 int y2
= wxMax(y
+height
, rect
.height
+rect
.y
);
107 int x2
= wxMax(x
+width
, rect
.width
+rect
.x
);
108 return wxRect(x1
, y1
, x2
-x1
, y2
-y1
);
111 bool wxRect::Inside(int cx
, int cy
) const
113 return ( (cx
>= x
) && (cy
>= y
)
114 && ((cy
- y
) < height
)
115 && ((cx
- x
) < width
)
119 wxColourDatabase::wxColourDatabase (int type
) : wxList (type
)
123 wxColourDatabase::~wxColourDatabase ()
125 // Cleanup Colour allocated in Initialize()
126 wxNode
*node
= First ();
129 wxColour
*col
= (wxColour
*) node
->Data ();
130 wxNode
*next
= node
->Next ();
136 // Colour database stuff
137 void wxColourDatabase::Initialize ()
139 static const struct wxColourDesc
146 {wxT("AQUAMARINE"),112, 219, 147},
147 {wxT("BLACK"),0, 0, 0},
148 {wxT("BLUE"), 0, 0, 255},
149 {wxT("BLUE VIOLET"), 159, 95, 159},
150 {wxT("BROWN"), 165, 42, 42},
151 {wxT("CADET BLUE"), 95, 159, 159},
152 {wxT("CORAL"), 255, 127, 0},
153 {wxT("CORNFLOWER BLUE"), 66, 66, 111},
154 {wxT("CYAN"), 0, 255, 255},
155 {wxT("DARK GREY"), 47, 47, 47}, // ?
157 {wxT("DARK GREEN"), 47, 79, 47},
158 {wxT("DARK OLIVE GREEN"), 79, 79, 47},
159 {wxT("DARK ORCHID"), 153, 50, 204},
160 {wxT("DARK SLATE BLUE"), 107, 35, 142},
161 {wxT("DARK SLATE GREY"), 47, 79, 79},
162 {wxT("DARK TURQUOISE"), 112, 147, 219},
163 {wxT("DIM GREY"), 84, 84, 84},
164 {wxT("FIREBRICK"), 142, 35, 35},
165 {wxT("FOREST GREEN"), 35, 142, 35},
166 {wxT("GOLD"), 204, 127, 50},
167 {wxT("GOLDENROD"), 219, 219, 112},
168 {wxT("GREY"), 128, 128, 128},
169 {wxT("GREEN"), 0, 255, 0},
170 {wxT("GREEN YELLOW"), 147, 219, 112},
171 {wxT("INDIAN RED"), 79, 47, 47},
172 {wxT("KHAKI"), 159, 159, 95},
173 {wxT("LIGHT BLUE"), 191, 216, 216},
174 {wxT("LIGHT GREY"), 192, 192, 192},
175 {wxT("LIGHT STEEL BLUE"), 143, 143, 188},
176 {wxT("LIME GREEN"), 50, 204, 50},
177 {wxT("LIGHT MAGENTA"), 255, 0, 255},
178 {wxT("MAGENTA"), 255, 0, 255},
179 {wxT("MAROON"), 142, 35, 107},
180 {wxT("MEDIUM AQUAMARINE"), 50, 204, 153},
181 {wxT("MEDIUM GREY"), 100, 100, 100},
182 {wxT("MEDIUM BLUE"), 50, 50, 204},
183 {wxT("MEDIUM FOREST GREEN"), 107, 142, 35},
184 {wxT("MEDIUM GOLDENROD"), 234, 234, 173},
185 {wxT("MEDIUM ORCHID"), 147, 112, 219},
186 {wxT("MEDIUM SEA GREEN"), 66, 111, 66},
187 {wxT("MEDIUM SLATE BLUE"), 127, 0, 255},
188 {wxT("MEDIUM SPRING GREEN"), 127, 255, 0},
189 {wxT("MEDIUM TURQUOISE"), 112, 219, 219},
190 {wxT("MEDIUM VIOLET RED"), 219, 112, 147},
191 {wxT("MIDNIGHT BLUE"), 47, 47, 79},
192 {wxT("NAVY"), 35, 35, 142},
193 {wxT("ORANGE"), 204, 50, 50},
194 {wxT("ORANGE RED"), 255, 0, 127},
195 {wxT("ORCHID"), 219, 112, 219},
196 {wxT("PALE GREEN"), 143, 188, 143},
197 {wxT("PINK"), 188, 143, 234},
198 {wxT("PLUM"), 234, 173, 234},
199 {wxT("PURPLE"), 176, 0, 255},
200 {wxT("RED"), 255, 0, 0},
201 {wxT("SALMON"), 111, 66, 66},
202 {wxT("SEA GREEN"), 35, 142, 107},
203 {wxT("SIENNA"), 142, 107, 35},
204 {wxT("SKY BLUE"), 50, 153, 204},
205 {wxT("SLATE BLUE"), 0, 127, 255},
206 {wxT("SPRING GREEN"), 0, 255, 127},
207 {wxT("STEEL BLUE"), 35, 107, 142},
208 {wxT("TAN"), 219, 147, 112},
209 {wxT("THISTLE"), 216, 191, 216},
210 {wxT("TURQUOISE"), 173, 234, 234},
211 {wxT("VIOLET"), 79, 47, 79},
212 {wxT("VIOLET RED"), 204, 50, 153},
213 {wxT("WHEAT"), 216, 216, 191},
214 {wxT("WHITE"), 255, 255, 255},
215 {wxT("YELLOW"), 255, 255, 0},
216 {wxT("YELLOW GREEN"), 153, 204, 50},
217 {wxT("MEDIUM GOLDENROD"), 234, 234, 173},
218 {wxT("MEDIUM FOREST GREEN"), 107, 142, 35},
219 {wxT("LIGHT MAGENTA"), 255, 0, 255},
220 {wxT("MEDIUM GREY"), 100, 100, 100},
223 for ( size_t n
= 0; n
< WXSIZEOF(wxColourTable
); n
++ )
225 const wxColourDesc
& cc
= wxColourTable
[n
];
226 Append(cc
.name
, new wxColour(cc
.r
,cc
.g
,cc
.b
));
231 * Changed by Ian Brown, July 1994.
233 * When running under X, the Colour Database starts off empty. The X server
234 * is queried for the colour first time after which it is entered into the
235 * database. This allows our client to use the server colour database which
236 * is hopefully gamma corrected for the display being used.
239 wxColour
*wxColourDatabase::FindColour(const wxString
& colour
)
241 // VZ: make the comparaison case insensitive and also match both grey and
243 wxString colName
= colour
;
245 wxString colName2
= colName
;
246 if ( !colName2
.Replace(_T("GRAY"), _T("GREY")) )
249 wxNode
*node
= First();
252 const wxChar
*key
= node
->GetKeyString();
253 if ( colName
== key
|| colName2
== key
)
255 return (wxColour
*)node
->Data();
268 // TODO for other implementations. This should really go into
269 // platform-specific directories.
278 wxColour
*col
= new wxColour( colour
);
282 return (wxColour
*) NULL
;
284 Append( colour
, col
);
292 Display
*display
= XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()) ;
295 Xv_Screen screen
= xv_get(xview_server
, SERVER_NTH_SCREEN
, 0);
296 Xv_opaque root_window
= xv_get(screen
, XV_ROOT
);
297 Display
*display
= (Display
*)xv_get(root_window
, XV_DISPLAY
);
300 /* MATTHEW: [4] Use wxGetMainColormap */
301 if (!XParseColor(display
, (Colormap
) wxTheApp
->GetMainColormap((WXDisplay
*) display
), colour
,&xcolour
))
304 unsigned char r
= (unsigned char)(xcolour
.red
>> 8);
305 unsigned char g
= (unsigned char)(xcolour
.green
>> 8);
306 unsigned char b
= (unsigned char)(xcolour
.blue
>> 8);
308 wxColour
*col
= new wxColour(r
, g
, b
);
315 wxString
wxColourDatabase::FindName (const wxColour
& colour
) const
319 unsigned char red
= colour
.Red ();
320 unsigned char green
= colour
.Green ();
321 unsigned char blue
= colour
.Blue ();
323 for (wxNode
* node
= First (); node
; node
= node
->Next ())
325 wxColour
*col
= (wxColour
*) node
->Data ();
327 if (col
->Red () == red
&& col
->Green () == green
&& col
->Blue () == blue
)
329 const wxChar
*found
= node
->GetKeyString();
342 void wxInitializeStockLists () {
343 wxTheBrushList
= new wxBrushList
;
344 wxThePenList
= new wxPenList
;
345 wxTheFontList
= new wxFontList
;
346 wxTheBitmapList
= new wxBitmapList
;
349 void wxInitializeStockObjects ()
355 // wxFontPool = new XFontPool;
358 // why under MSW fonts shouldn't have the standard system size?
360 static const int sizeFont
= 10;
362 static const int sizeFont
= 12;
365 wxNORMAL_FONT
= new wxFont (sizeFont
, wxMODERN
, wxNORMAL
, wxNORMAL
);
366 wxSMALL_FONT
= new wxFont (sizeFont
- 2, wxSWISS
, wxNORMAL
, wxNORMAL
);
367 wxITALIC_FONT
= new wxFont (sizeFont
, wxROMAN
, wxITALIC
, wxNORMAL
);
368 wxSWISS_FONT
= new wxFont (sizeFont
, wxSWISS
, wxNORMAL
, wxNORMAL
);
370 wxRED_PEN
= new wxPen ("RED", 1, wxSOLID
);
371 wxCYAN_PEN
= new wxPen ("CYAN", 1, wxSOLID
);
372 wxGREEN_PEN
= new wxPen ("GREEN", 1, wxSOLID
);
373 wxBLACK_PEN
= new wxPen ("BLACK", 1, wxSOLID
);
374 wxWHITE_PEN
= new wxPen ("WHITE", 1, wxSOLID
);
375 wxTRANSPARENT_PEN
= new wxPen ("BLACK", 1, wxTRANSPARENT
);
376 wxBLACK_DASHED_PEN
= new wxPen ("BLACK", 1, wxSHORT_DASH
);
377 wxGREY_PEN
= new wxPen ("GREY", 1, wxSOLID
);
378 wxMEDIUM_GREY_PEN
= new wxPen ("MEDIUM GREY", 1, wxSOLID
);
379 wxLIGHT_GREY_PEN
= new wxPen ("LIGHT GREY", 1, wxSOLID
);
381 wxBLUE_BRUSH
= new wxBrush ("BLUE", wxSOLID
);
382 wxGREEN_BRUSH
= new wxBrush ("GREEN", wxSOLID
);
383 wxWHITE_BRUSH
= new wxBrush ("WHITE", wxSOLID
);
384 wxBLACK_BRUSH
= new wxBrush ("BLACK", wxSOLID
);
385 wxTRANSPARENT_BRUSH
= new wxBrush ("BLACK", wxTRANSPARENT
);
386 wxCYAN_BRUSH
= new wxBrush ("CYAN", wxSOLID
);
387 wxRED_BRUSH
= new wxBrush ("RED", wxSOLID
);
388 wxGREY_BRUSH
= new wxBrush ("GREY", wxSOLID
);
389 wxMEDIUM_GREY_BRUSH
= new wxBrush ("MEDIUM GREY", wxSOLID
);
390 wxLIGHT_GREY_BRUSH
= new wxBrush ("LIGHT GREY", wxSOLID
);
392 wxBLACK
= new wxColour ("BLACK");
393 wxWHITE
= new wxColour ("WHITE");
394 wxRED
= new wxColour ("RED");
395 wxBLUE
= new wxColour ("BLUE");
396 wxGREEN
= new wxColour ("GREEN");
397 wxCYAN
= new wxColour ("CYAN");
398 wxLIGHT_GREY
= new wxColour ("LIGHT GREY");
400 wxSTANDARD_CURSOR
= new wxCursor (wxCURSOR_ARROW
);
401 wxHOURGLASS_CURSOR
= new wxCursor (wxCURSOR_WAIT
);
402 wxCROSS_CURSOR
= new wxCursor (wxCURSOR_CROSS
);
405 void wxDeleteStockObjects ()
407 wxDELETE(wxNORMAL_FONT
);
408 wxDELETE(wxSMALL_FONT
);
409 wxDELETE(wxITALIC_FONT
);
410 wxDELETE(wxSWISS_FONT
);
413 wxDELETE(wxCYAN_PEN
);
414 wxDELETE(wxGREEN_PEN
);
415 wxDELETE(wxBLACK_PEN
);
416 wxDELETE(wxWHITE_PEN
);
417 wxDELETE(wxTRANSPARENT_PEN
);
418 wxDELETE(wxBLACK_DASHED_PEN
);
419 wxDELETE(wxGREY_PEN
);
420 wxDELETE(wxMEDIUM_GREY_PEN
);
421 wxDELETE(wxLIGHT_GREY_PEN
);
423 wxDELETE(wxBLUE_BRUSH
);
424 wxDELETE(wxGREEN_BRUSH
);
425 wxDELETE(wxWHITE_BRUSH
);
426 wxDELETE(wxBLACK_BRUSH
);
427 wxDELETE(wxTRANSPARENT_BRUSH
);
428 wxDELETE(wxCYAN_BRUSH
);
429 wxDELETE(wxRED_BRUSH
);
430 wxDELETE(wxGREY_BRUSH
);
431 wxDELETE(wxMEDIUM_GREY_BRUSH
);
432 wxDELETE(wxLIGHT_GREY_BRUSH
);
440 wxDELETE(wxLIGHT_GREY
);
442 wxDELETE(wxSTANDARD_CURSOR
);
443 wxDELETE(wxHOURGLASS_CURSOR
);
444 wxDELETE(wxCROSS_CURSOR
);
447 void wxDeleteStockLists() {
448 wxDELETE(wxTheBrushList
);
449 wxDELETE(wxThePenList
);
450 wxDELETE(wxTheFontList
);
451 wxDELETE(wxTheBitmapList
);
454 wxBitmapList::wxBitmapList ()
458 wxBitmapList::~wxBitmapList ()
460 wxNode
*node
= First ();
463 wxBitmap
*bitmap
= (wxBitmap
*) node
->Data ();
464 wxNode
*next
= node
->Next ();
465 if (bitmap
->GetVisible())
471 // Pen and Brush lists
472 wxPenList::~wxPenList ()
474 wxNode
*node
= First ();
477 wxPen
*pen
= (wxPen
*) node
->Data ();
478 wxNode
*next
= node
->Next ();
479 if (pen
->GetVisible())
485 void wxPenList::AddPen (wxPen
* pen
)
490 void wxPenList::RemovePen (wxPen
* pen
)
495 wxPen
*wxPenList::FindOrCreatePen (const wxColour
& colour
, int width
, int style
)
497 for (wxNode
* node
= First (); node
; node
= node
->Next ())
499 wxPen
*each_pen
= (wxPen
*) node
->Data ();
501 each_pen
->GetVisible() &&
502 each_pen
->GetWidth () == width
&&
503 each_pen
->GetStyle () == style
&&
504 each_pen
->GetColour ().Red () == colour
.Red () &&
505 each_pen
->GetColour ().Green () == colour
.Green () &&
506 each_pen
->GetColour ().Blue () == colour
.Blue ())
509 wxPen
*pen
= new wxPen (colour
, width
, style
);
511 // Yes, we can return a pointer to this in a later FindOrCreatePen call,
512 // because we created it within FindOrCreatePen. Safeguards against
513 // returning a pointer to an automatic variable and hanging on to it
514 // (dangling pointer).
515 pen
->SetVisible(TRUE
);
520 wxBrushList::~wxBrushList ()
522 wxNode
*node
= First ();
525 wxBrush
*brush
= (wxBrush
*) node
->Data ();
526 wxNode
*next
= node
->Next ();
527 if (brush
->GetVisible())
533 void wxBrushList::AddBrush (wxBrush
* brush
)
538 wxBrush
*wxBrushList::FindOrCreateBrush (const wxColour
& colour
, int style
)
540 for (wxNode
* node
= First (); node
; node
= node
->Next ())
542 wxBrush
*each_brush
= (wxBrush
*) node
->Data ();
544 each_brush
->GetVisible() &&
545 each_brush
->GetStyle () == style
&&
546 each_brush
->GetColour ().Red () == colour
.Red () &&
547 each_brush
->GetColour ().Green () == colour
.Green () &&
548 each_brush
->GetColour ().Blue () == colour
.Blue ())
552 // Yes, we can return a pointer to this in a later FindOrCreateBrush call,
553 // because we created it within FindOrCreateBrush. Safeguards against
554 // returning a pointer to an automatic variable and hanging on to it
555 // (dangling pointer).
556 wxBrush
*brush
= new wxBrush (colour
, style
);
558 brush
->SetVisible(TRUE
);
563 void wxBrushList::RemoveBrush (wxBrush
* brush
)
565 DeleteObject (brush
);
568 wxFontList::~wxFontList ()
570 wxNode
*node
= First ();
573 // Only delete objects that are 'visible', i.e.
574 // that have been created using FindOrCreate...,
575 // where the pointers are expected to be shared
576 // (and therefore not deleted by any one part of an app).
577 wxFont
*font
= (wxFont
*) node
->Data ();
578 wxNode
*next
= node
->Next ();
579 if (font
->GetVisible())
585 void wxFontList::AddFont (wxFont
* font
)
590 void wxFontList::RemoveFont (wxFont
* font
)
595 wxFont
*wxFontList::FindOrCreateFont(int pointSize
,
600 const wxString
& facename
,
601 wxFontEncoding encoding
)
603 wxFont
*font
= (wxFont
*)NULL
;
605 for ( node
= First(); node
; node
= node
->Next() )
607 font
= (wxFont
*)node
->Data();
608 if ( font
->GetVisible() &&
610 font
->GetPointSize () == pointSize
&&
611 font
->GetStyle () == style
&&
612 font
->GetWeight () == weight
&&
613 font
->GetUnderlined () == underline
)
615 int fontFamily
= font
->GetFamily();
617 #if defined(__WXGTK__)
618 // under GTK the default family is wxSWISS, so looking for a font
619 // with wxDEFAULT family should return a wxSWISS one instead of
620 // creating a new one
621 bool same
= (fontFamily
== family
) ||
622 (fontFamily
== wxSWISS
&& family
== wxDEFAULT
);
624 // VZ: but why elsewhere do we require an exact match? mystery...
625 bool same
= fontFamily
== family
;
628 // empty facename matches anything at all: this is bad because
629 // depending on which fonts are already created, we might get back
630 // a different font if we create it with empty facename, but it is
631 // still better than never matching anything in the cache at all
633 if ( same
&& !!facename
)
635 const wxString
& fontFace
= font
->GetFaceName();
637 // empty facename matches everything
638 same
= !fontFace
|| fontFace
== facename
;
641 if ( same
&& (encoding
!= wxFONTENCODING_DEFAULT
) )
643 // have to match the encoding too
644 same
= font
->GetEncoding() == encoding
;
656 // font not found, create the new one
657 font
= new wxFont(pointSize
, family
, style
, weight
,
658 underline
, facename
, encoding
);
660 // and mark it as being cacheable
661 font
->SetVisible(TRUE
);
667 void wxBitmapList::AddBitmap(wxBitmap
*bitmap
)
672 void wxBitmapList::RemoveBitmap(wxBitmap
*bitmap
)
674 DeleteObject(bitmap
);
677 wxSize
wxGetDisplaySize()
680 wxDisplaySize(& x
, & y
);
684 wxResourceCache::~wxResourceCache ()
686 wxNode
*node
= First ();
688 wxObject
*item
= (wxObject
*)node
->Data();
691 node
= node
->Next ();