]>
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"
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"
49 #pragma message disable nosimpint
53 #pragma message enable nosimpint
57 IMPLEMENT_CLASS(wxColourDatabase
, wxList
)
58 IMPLEMENT_DYNAMIC_CLASS(wxFontList
, wxList
)
59 IMPLEMENT_DYNAMIC_CLASS(wxPenList
, wxList
)
60 IMPLEMENT_DYNAMIC_CLASS(wxBrushList
, wxList
)
61 IMPLEMENT_DYNAMIC_CLASS(wxBitmapList
, wxList
)
62 IMPLEMENT_DYNAMIC_CLASS(wxResourceCache
, wxList
)
64 IMPLEMENT_ABSTRACT_CLASS(wxDCBase
, wxObject
)
66 wxRect::wxRect(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
70 width
= bottomRight
.x
- topLeft
.x
+ 1;
71 height
= bottomRight
.y
- topLeft
.y
+ 1;
86 wxRect::wxRect(const wxPoint
& point
, const wxSize
& size
)
88 x
= point
.x
; y
= point
.y
;
89 width
= size
.x
; height
= size
.y
;
92 bool wxRect::operator==(const wxRect
& rect
) const
94 return ((x
== rect
.x
) &&
96 (width
== rect
.width
) &&
97 (height
== rect
.height
));
100 wxRect
& wxRect::operator += (const wxRect
& rect
)
102 *this = (*this + rect
);
106 wxRect
wxRect::operator + (const wxRect
& rect
) const
108 int x1
= wxMin(this->x
, rect
.x
);
109 int y1
= wxMin(this->y
, rect
.y
);
110 int y2
= wxMax(y
+height
, rect
.height
+rect
.y
);
111 int x2
= wxMax(x
+width
, rect
.width
+rect
.x
);
112 return wxRect(x1
, y1
, x2
-x1
, y2
-y1
);
115 bool wxRect::Inside(int cx
, int cy
) const
117 return ( (cx
>= x
) && (cy
>= y
)
118 && ((cy
- y
) < height
)
119 && ((cx
- x
) < width
)
123 wxColourDatabase::wxColourDatabase (int type
) : wxList (type
)
127 wxColourDatabase::~wxColourDatabase ()
129 // Cleanup Colour allocated in Initialize()
130 wxNode
*node
= First ();
133 wxColour
*col
= (wxColour
*) node
->Data ();
134 wxNode
*next
= node
->Next ();
140 // Colour database stuff
141 void wxColourDatabase::Initialize ()
143 static const struct wxColourDesc
150 {wxT("AQUAMARINE"),112, 219, 147},
151 {wxT("BLACK"),0, 0, 0},
152 {wxT("BLUE"), 0, 0, 255},
153 {wxT("BLUE VIOLET"), 159, 95, 159},
154 {wxT("BROWN"), 165, 42, 42},
155 {wxT("CADET BLUE"), 95, 159, 159},
156 {wxT("CORAL"), 255, 127, 0},
157 {wxT("CORNFLOWER BLUE"), 66, 66, 111},
158 {wxT("CYAN"), 0, 255, 255},
159 {wxT("DARK GREY"), 47, 47, 47}, // ?
161 {wxT("DARK GREEN"), 47, 79, 47},
162 {wxT("DARK OLIVE GREEN"), 79, 79, 47},
163 {wxT("DARK ORCHID"), 153, 50, 204},
164 {wxT("DARK SLATE BLUE"), 107, 35, 142},
165 {wxT("DARK SLATE GREY"), 47, 79, 79},
166 {wxT("DARK TURQUOISE"), 112, 147, 219},
167 {wxT("DIM GREY"), 84, 84, 84},
168 {wxT("FIREBRICK"), 142, 35, 35},
169 {wxT("FOREST GREEN"), 35, 142, 35},
170 {wxT("GOLD"), 204, 127, 50},
171 {wxT("GOLDENROD"), 219, 219, 112},
172 {wxT("GREY"), 128, 128, 128},
173 {wxT("GREEN"), 0, 255, 0},
174 {wxT("GREEN YELLOW"), 147, 219, 112},
175 {wxT("INDIAN RED"), 79, 47, 47},
176 {wxT("KHAKI"), 159, 159, 95},
177 {wxT("LIGHT BLUE"), 191, 216, 216},
178 {wxT("LIGHT GREY"), 192, 192, 192},
179 {wxT("LIGHT STEEL BLUE"), 143, 143, 188},
180 {wxT("LIME GREEN"), 50, 204, 50},
181 {wxT("LIGHT MAGENTA"), 255, 0, 255},
182 {wxT("MAGENTA"), 255, 0, 255},
183 {wxT("MAROON"), 142, 35, 107},
184 {wxT("MEDIUM AQUAMARINE"), 50, 204, 153},
185 {wxT("MEDIUM GREY"), 100, 100, 100},
186 {wxT("MEDIUM BLUE"), 50, 50, 204},
187 {wxT("MEDIUM FOREST GREEN"), 107, 142, 35},
188 {wxT("MEDIUM GOLDENROD"), 234, 234, 173},
189 {wxT("MEDIUM ORCHID"), 147, 112, 219},
190 {wxT("MEDIUM SEA GREEN"), 66, 111, 66},
191 {wxT("MEDIUM SLATE BLUE"), 127, 0, 255},
192 {wxT("MEDIUM SPRING GREEN"), 127, 255, 0},
193 {wxT("MEDIUM TURQUOISE"), 112, 219, 219},
194 {wxT("MEDIUM VIOLET RED"), 219, 112, 147},
195 {wxT("MIDNIGHT BLUE"), 47, 47, 79},
196 {wxT("NAVY"), 35, 35, 142},
197 {wxT("ORANGE"), 204, 50, 50},
198 {wxT("ORANGE RED"), 255, 0, 127},
199 {wxT("ORCHID"), 219, 112, 219},
200 {wxT("PALE GREEN"), 143, 188, 143},
201 {wxT("PINK"), 188, 143, 234},
202 {wxT("PLUM"), 234, 173, 234},
203 {wxT("PURPLE"), 176, 0, 255},
204 {wxT("RED"), 255, 0, 0},
205 {wxT("SALMON"), 111, 66, 66},
206 {wxT("SEA GREEN"), 35, 142, 107},
207 {wxT("SIENNA"), 142, 107, 35},
208 {wxT("SKY BLUE"), 50, 153, 204},
209 {wxT("SLATE BLUE"), 0, 127, 255},
210 {wxT("SPRING GREEN"), 0, 255, 127},
211 {wxT("STEEL BLUE"), 35, 107, 142},
212 {wxT("TAN"), 219, 147, 112},
213 {wxT("THISTLE"), 216, 191, 216},
214 {wxT("TURQUOISE"), 173, 234, 234},
215 {wxT("VIOLET"), 79, 47, 79},
216 {wxT("VIOLET RED"), 204, 50, 153},
217 {wxT("WHEAT"), 216, 216, 191},
218 {wxT("WHITE"), 255, 255, 255},
219 {wxT("YELLOW"), 255, 255, 0},
220 {wxT("YELLOW GREEN"), 153, 204, 50},
221 {wxT("MEDIUM GOLDENROD"), 234, 234, 173},
222 {wxT("MEDIUM FOREST GREEN"), 107, 142, 35},
223 {wxT("LIGHT MAGENTA"), 255, 0, 255},
224 {wxT("MEDIUM GREY"), 100, 100, 100},
227 for ( size_t n
= 0; n
< WXSIZEOF(wxColourTable
); n
++ )
229 const wxColourDesc
& cc
= wxColourTable
[n
];
230 Append(cc
.name
, new wxColour(cc
.r
,cc
.g
,cc
.b
));
235 * Changed by Ian Brown, July 1994.
237 * When running under X, the Colour Database starts off empty. The X server
238 * is queried for the colour first time after which it is entered into the
239 * database. This allows our client to use the server colour database which
240 * is hopefully gamma corrected for the display being used.
243 wxColour
*wxColourDatabase::FindColour(const wxString
& colour
)
245 // VZ: make the comparaison case insensitive and also match both grey and
247 wxString colName
= colour
;
249 wxString colName2
= colName
;
250 if ( !colName2
.Replace(_T("GRAY"), _T("GREY")) )
253 wxNode
*node
= First();
256 const wxChar
*key
= node
->GetKeyString();
257 if ( colName
== key
|| colName2
== key
)
259 return (wxColour
*)node
->Data();
272 // TODO for other implementations. This should really go into
273 // platform-specific directories.
282 wxColour
*col
= new wxColour( colour
);
286 return (wxColour
*) NULL
;
288 Append( colour
, col
);
296 Display
*display
= XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()) ;
299 Xv_Screen screen
= xv_get(xview_server
, SERVER_NTH_SCREEN
, 0);
300 Xv_opaque root_window
= xv_get(screen
, XV_ROOT
);
301 Display
*display
= (Display
*)xv_get(root_window
, XV_DISPLAY
);
304 /* MATTHEW: [4] Use wxGetMainColormap */
305 if (!XParseColor(display
, (Colormap
) wxTheApp
->GetMainColormap((WXDisplay
*) display
), colour
,&xcolour
))
308 unsigned char r
= (unsigned char)(xcolour
.red
>> 8);
309 unsigned char g
= (unsigned char)(xcolour
.green
>> 8);
310 unsigned char b
= (unsigned char)(xcolour
.blue
>> 8);
312 wxColour
*col
= new wxColour(r
, g
, b
);
319 wxString
wxColourDatabase::FindName (const wxColour
& colour
) const
323 unsigned char red
= colour
.Red ();
324 unsigned char green
= colour
.Green ();
325 unsigned char blue
= colour
.Blue ();
327 for (wxNode
* node
= First (); node
; node
= node
->Next ())
329 wxColour
*col
= (wxColour
*) node
->Data ();
331 if (col
->Red () == red
&& col
->Green () == green
&& col
->Blue () == blue
)
333 const wxChar
*found
= node
->GetKeyString();
346 void wxInitializeStockLists () {
347 wxTheBrushList
= new wxBrushList
;
348 wxThePenList
= new wxPenList
;
349 wxTheFontList
= new wxFontList
;
350 wxTheBitmapList
= new wxBitmapList
;
353 void wxInitializeStockObjects ()
359 // wxFontPool = new XFontPool;
362 // why under MSW fonts shouldn't have the standard system size?
364 static const int sizeFont
= 10;
366 static const int sizeFont
= 12;
369 wxNORMAL_FONT
= new wxFont (sizeFont
, wxMODERN
, wxNORMAL
, wxNORMAL
);
370 wxSMALL_FONT
= new wxFont (sizeFont
- 2, wxSWISS
, wxNORMAL
, wxNORMAL
);
371 wxITALIC_FONT
= new wxFont (sizeFont
, wxROMAN
, wxITALIC
, wxNORMAL
);
372 wxSWISS_FONT
= new wxFont (sizeFont
, wxSWISS
, wxNORMAL
, wxNORMAL
);
374 wxRED_PEN
= new wxPen ("RED", 1, wxSOLID
);
375 wxCYAN_PEN
= new wxPen ("CYAN", 1, wxSOLID
);
376 wxGREEN_PEN
= new wxPen ("GREEN", 1, wxSOLID
);
377 wxBLACK_PEN
= new wxPen ("BLACK", 1, wxSOLID
);
378 wxWHITE_PEN
= new wxPen ("WHITE", 1, wxSOLID
);
379 wxTRANSPARENT_PEN
= new wxPen ("BLACK", 1, wxTRANSPARENT
);
380 wxBLACK_DASHED_PEN
= new wxPen ("BLACK", 1, wxSHORT_DASH
);
381 wxGREY_PEN
= new wxPen ("GREY", 1, wxSOLID
);
382 wxMEDIUM_GREY_PEN
= new wxPen ("MEDIUM GREY", 1, wxSOLID
);
383 wxLIGHT_GREY_PEN
= new wxPen ("LIGHT GREY", 1, wxSOLID
);
385 wxBLUE_BRUSH
= new wxBrush ("BLUE", wxSOLID
);
386 wxGREEN_BRUSH
= new wxBrush ("GREEN", wxSOLID
);
387 wxWHITE_BRUSH
= new wxBrush ("WHITE", wxSOLID
);
388 wxBLACK_BRUSH
= new wxBrush ("BLACK", wxSOLID
);
389 wxTRANSPARENT_BRUSH
= new wxBrush ("BLACK", wxTRANSPARENT
);
390 wxCYAN_BRUSH
= new wxBrush ("CYAN", wxSOLID
);
391 wxRED_BRUSH
= new wxBrush ("RED", wxSOLID
);
392 wxGREY_BRUSH
= new wxBrush ("GREY", wxSOLID
);
393 wxMEDIUM_GREY_BRUSH
= new wxBrush ("MEDIUM GREY", wxSOLID
);
394 wxLIGHT_GREY_BRUSH
= new wxBrush ("LIGHT GREY", wxSOLID
);
396 wxBLACK
= new wxColour ("BLACK");
397 wxWHITE
= new wxColour ("WHITE");
398 wxRED
= new wxColour ("RED");
399 wxBLUE
= new wxColour ("BLUE");
400 wxGREEN
= new wxColour ("GREEN");
401 wxCYAN
= new wxColour ("CYAN");
402 wxLIGHT_GREY
= new wxColour ("LIGHT GREY");
404 wxSTANDARD_CURSOR
= new wxCursor (wxCURSOR_ARROW
);
405 wxHOURGLASS_CURSOR
= new wxCursor (wxCURSOR_WAIT
);
406 wxCROSS_CURSOR
= new wxCursor (wxCURSOR_CROSS
);
409 void wxDeleteStockObjects ()
411 wxDELETE(wxNORMAL_FONT
);
412 wxDELETE(wxSMALL_FONT
);
413 wxDELETE(wxITALIC_FONT
);
414 wxDELETE(wxSWISS_FONT
);
417 wxDELETE(wxCYAN_PEN
);
418 wxDELETE(wxGREEN_PEN
);
419 wxDELETE(wxBLACK_PEN
);
420 wxDELETE(wxWHITE_PEN
);
421 wxDELETE(wxTRANSPARENT_PEN
);
422 wxDELETE(wxBLACK_DASHED_PEN
);
423 wxDELETE(wxGREY_PEN
);
424 wxDELETE(wxMEDIUM_GREY_PEN
);
425 wxDELETE(wxLIGHT_GREY_PEN
);
427 wxDELETE(wxBLUE_BRUSH
);
428 wxDELETE(wxGREEN_BRUSH
);
429 wxDELETE(wxWHITE_BRUSH
);
430 wxDELETE(wxBLACK_BRUSH
);
431 wxDELETE(wxTRANSPARENT_BRUSH
);
432 wxDELETE(wxCYAN_BRUSH
);
433 wxDELETE(wxRED_BRUSH
);
434 wxDELETE(wxGREY_BRUSH
);
435 wxDELETE(wxMEDIUM_GREY_BRUSH
);
436 wxDELETE(wxLIGHT_GREY_BRUSH
);
444 wxDELETE(wxLIGHT_GREY
);
446 wxDELETE(wxSTANDARD_CURSOR
);
447 wxDELETE(wxHOURGLASS_CURSOR
);
448 wxDELETE(wxCROSS_CURSOR
);
451 void wxDeleteStockLists() {
452 wxDELETE(wxTheBrushList
);
453 wxDELETE(wxThePenList
);
454 wxDELETE(wxTheFontList
);
455 wxDELETE(wxTheBitmapList
);
458 wxBitmapList::wxBitmapList ()
462 wxBitmapList::~wxBitmapList ()
464 wxNode
*node
= First ();
467 wxBitmap
*bitmap
= (wxBitmap
*) node
->Data ();
468 wxNode
*next
= node
->Next ();
469 if (bitmap
->GetVisible())
475 // Pen and Brush lists
476 wxPenList::~wxPenList ()
478 wxNode
*node
= First ();
481 wxPen
*pen
= (wxPen
*) node
->Data ();
482 wxNode
*next
= node
->Next ();
483 if (pen
->GetVisible())
489 void wxPenList::AddPen (wxPen
* pen
)
494 void wxPenList::RemovePen (wxPen
* pen
)
499 wxPen
*wxPenList::FindOrCreatePen (const wxColour
& colour
, int width
, int style
)
501 for (wxNode
* node
= First (); node
; node
= node
->Next ())
503 wxPen
*each_pen
= (wxPen
*) node
->Data ();
505 each_pen
->GetVisible() &&
506 each_pen
->GetWidth () == width
&&
507 each_pen
->GetStyle () == style
&&
508 each_pen
->GetColour ().Red () == colour
.Red () &&
509 each_pen
->GetColour ().Green () == colour
.Green () &&
510 each_pen
->GetColour ().Blue () == colour
.Blue ())
513 wxPen
*pen
= new wxPen (colour
, width
, style
);
515 // Yes, we can return a pointer to this in a later FindOrCreatePen call,
516 // because we created it within FindOrCreatePen. Safeguards against
517 // returning a pointer to an automatic variable and hanging on to it
518 // (dangling pointer).
519 pen
->SetVisible(TRUE
);
524 wxBrushList::~wxBrushList ()
526 wxNode
*node
= First ();
529 wxBrush
*brush
= (wxBrush
*) node
->Data ();
530 wxNode
*next
= node
->Next ();
531 if (brush
->GetVisible())
537 void wxBrushList::AddBrush (wxBrush
* brush
)
542 wxBrush
*wxBrushList::FindOrCreateBrush (const wxColour
& colour
, int style
)
544 for (wxNode
* node
= First (); node
; node
= node
->Next ())
546 wxBrush
*each_brush
= (wxBrush
*) node
->Data ();
548 each_brush
->GetVisible() &&
549 each_brush
->GetStyle () == style
&&
550 each_brush
->GetColour ().Red () == colour
.Red () &&
551 each_brush
->GetColour ().Green () == colour
.Green () &&
552 each_brush
->GetColour ().Blue () == colour
.Blue ())
556 // Yes, we can return a pointer to this in a later FindOrCreateBrush call,
557 // because we created it within FindOrCreateBrush. Safeguards against
558 // returning a pointer to an automatic variable and hanging on to it
559 // (dangling pointer).
560 wxBrush
*brush
= new wxBrush (colour
, style
);
562 brush
->SetVisible(TRUE
);
567 void wxBrushList::RemoveBrush (wxBrush
* brush
)
569 DeleteObject (brush
);
572 wxFontList::~wxFontList ()
574 wxNode
*node
= First ();
577 // Only delete objects that are 'visible', i.e.
578 // that have been created using FindOrCreate...,
579 // where the pointers are expected to be shared
580 // (and therefore not deleted by any one part of an app).
581 wxFont
*font
= (wxFont
*) node
->Data ();
582 wxNode
*next
= node
->Next ();
583 if (font
->GetVisible())
589 void wxFontList::AddFont (wxFont
* font
)
594 void wxFontList::RemoveFont (wxFont
* font
)
599 wxFont
*wxFontList::FindOrCreateFont(int pointSize
,
604 const wxString
& facename
,
605 wxFontEncoding encoding
)
607 wxFont
*font
= (wxFont
*)NULL
;
609 for ( node
= First(); node
; node
= node
->Next() )
611 font
= (wxFont
*)node
->Data();
612 if ( font
->GetVisible() &&
614 font
->GetPointSize () == pointSize
&&
615 font
->GetStyle () == style
&&
616 font
->GetWeight () == weight
&&
617 font
->GetUnderlined () == underline
)
619 int fontFamily
= font
->GetFamily();
621 #if defined(__WXGTK__)
622 // under GTK the default family is wxSWISS, so looking for a font
623 // with wxDEFAULT family should return a wxSWISS one instead of
624 // creating a new one
625 bool same
= (fontFamily
== family
) ||
626 (fontFamily
== wxSWISS
&& family
== wxDEFAULT
);
628 // VZ: but why elsewhere do we require an exact match? mystery...
629 bool same
= fontFamily
== family
;
632 // empty facename matches anything at all: this is bad because
633 // depending on which fonts are already created, we might get back
634 // a different font if we create it with empty facename, but it is
635 // still better than never matching anything in the cache at all
637 if ( same
&& !!facename
)
639 const wxString
& fontFace
= font
->GetFaceName();
641 // empty facename matches everything
642 same
= !fontFace
|| fontFace
== facename
;
645 if ( same
&& (encoding
!= wxFONTENCODING_DEFAULT
) )
647 // have to match the encoding too
648 same
= font
->GetEncoding() == encoding
;
660 // font not found, create the new one
661 font
= new wxFont(pointSize
, family
, style
, weight
,
662 underline
, facename
, encoding
);
664 // and mark it as being cacheable
665 font
->SetVisible(TRUE
);
671 void wxBitmapList::AddBitmap(wxBitmap
*bitmap
)
676 void wxBitmapList::RemoveBitmap(wxBitmap
*bitmap
)
678 DeleteObject(bitmap
);
681 wxSize
wxGetDisplaySize()
684 wxDisplaySize(& x
, & y
);
688 wxResourceCache::~wxResourceCache ()
690 wxNode
*node
= First ();
692 wxObject
*item
= (wxObject
*)node
->Data();
695 node
= node
->Next ();