]>
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 // Don't initialize for X: colours are found
140 // in FindColour below.
148 static cdef table
[]={
151 {wxT("AQUAMARINE"),112, 219, 147},
152 {wxT("BLACK"),0, 0, 0},
153 {wxT("BLUE"), 0, 0, 255},
154 {wxT("BLUE VIOLET"), 159, 95, 159},
155 {wxT("BROWN"), 165, 42, 42},
156 {wxT("CADET BLUE"), 95, 159, 159},
157 {wxT("CORAL"), 255, 127, 0},
158 {wxT("CORNFLOWER BLUE"), 66, 66, 111},
159 {wxT("CYAN"), 0, 255, 255},
160 {wxT("DARK GREY"), 47, 47, 47}, // ?
162 {wxT("DARK GREEN"), 47, 79, 47},
163 {wxT("DARK OLIVE GREEN"), 79, 79, 47},
164 {wxT("DARK ORCHID"), 153, 50, 204},
165 {wxT("DARK SLATE BLUE"), 107, 35, 142},
166 {wxT("DARK SLATE GREY"), 47, 79, 79},
167 {wxT("DARK TURQUOISE"), 112, 147, 219},
168 {wxT("DIM GREY"), 84, 84, 84},
169 {wxT("FIREBRICK"), 142, 35, 35},
170 {wxT("FOREST GREEN"), 35, 142, 35},
171 {wxT("GOLD"), 204, 127, 50},
172 {wxT("GOLDENROD"), 219, 219, 112},
173 {wxT("GREY"), 128, 128, 128},
174 {wxT("GREEN"), 0, 255, 0},
175 {wxT("GREEN YELLOW"), 147, 219, 112},
176 {wxT("INDIAN RED"), 79, 47, 47},
177 {wxT("KHAKI"), 159, 159, 95},
178 {wxT("LIGHT BLUE"), 191, 216, 216},
179 {wxT("LIGHT GREY"), 192, 192, 192},
180 {wxT("LIGHT STEEL BLUE"), 143, 143, 188},
181 {wxT("LIME GREEN"), 50, 204, 50},
182 {wxT("LIGHT MAGENTA"), 255, 0, 255},
183 {wxT("MAGENTA"), 255, 0, 255},
184 {wxT("MAROON"), 142, 35, 107},
185 {wxT("MEDIUM AQUAMARINE"), 50, 204, 153},
186 {wxT("MEDIUM GREY"), 100, 100, 100},
187 {wxT("MEDIUM BLUE"), 50, 50, 204},
188 {wxT("MEDIUM FOREST GREEN"), 107, 142, 35},
189 {wxT("MEDIUM GOLDENROD"), 234, 234, 173},
190 {wxT("MEDIUM ORCHID"), 147, 112, 219},
191 {wxT("MEDIUM SEA GREEN"), 66, 111, 66},
192 {wxT("MEDIUM SLATE BLUE"), 127, 0, 255},
193 {wxT("MEDIUM SPRING GREEN"), 127, 255, 0},
194 {wxT("MEDIUM TURQUOISE"), 112, 219, 219},
195 {wxT("MEDIUM VIOLET RED"), 219, 112, 147},
196 {wxT("MIDNIGHT BLUE"), 47, 47, 79},
197 {wxT("NAVY"), 35, 35, 142},
198 {wxT("ORANGE"), 204, 50, 50},
199 {wxT("ORANGE RED"), 255, 0, 127},
200 {wxT("ORCHID"), 219, 112, 219},
201 {wxT("PALE GREEN"), 143, 188, 143},
202 {wxT("PINK"), 188, 143, 234},
203 {wxT("PLUM"), 234, 173, 234},
204 {wxT("PURPLE"), 176, 0, 255},
205 {wxT("RED"), 255, 0, 0},
206 {wxT("SALMON"), 111, 66, 66},
207 {wxT("SEA GREEN"), 35, 142, 107},
208 {wxT("SIENNA"), 142, 107, 35},
209 {wxT("SKY BLUE"), 50, 153, 204},
210 {wxT("SLATE BLUE"), 0, 127, 255},
211 {wxT("SPRING GREEN"), 0, 255, 127},
212 {wxT("STEEL BLUE"), 35, 107, 142},
213 {wxT("TAN"), 219, 147, 112},
214 {wxT("THISTLE"), 216, 191, 216},
215 {wxT("TURQUOISE"), 173, 234, 234},
216 {wxT("VIOLET"), 79, 47, 79},
217 {wxT("VIOLET RED"), 204, 50, 153},
218 {wxT("WHEAT"), 216, 216, 191},
219 {wxT("WHITE"), 255, 255, 255},
220 {wxT("YELLOW"), 255, 255, 0},
221 {wxT("YELLOW GREEN"), 153, 204, 50},
224 #if defined(__WXGTK__) || defined(__X__)
225 {wxT("MEDIUM GOLDENROD"), 234, 234, 173},
226 {wxT("MEDIUM FOREST GREEN"), 107, 142, 35},
227 {wxT("LIGHT MAGENTA"), 255, 0, 255},
228 {wxT("MEDIUM GREY"), 100, 100, 100},
234 for (i
=0;cc
=table
[i
],cc
.name
!=0;i
++)
236 Append(cc
.name
,new wxColour(cc
.r
,cc
.g
,cc
.b
));
242 * Changed by Ian Brown, July 1994.
244 * When running under X, the Colour Database starts off empty. The X server
245 * is queried for the colour first time after which it is entered into the
246 * database. This allows our client to use the server colour database which
247 * is hopefully gamma corrected for the display being used.
250 wxColour
*wxColourDatabase::FindColour(const wxString
& colour
)
252 // VZ: make the comparaison case insensitive
253 wxString str
= colour
;
256 wxNode
*node
= Find(str
);
258 return (wxColour
*)node
->Data();
267 // TODO for other implementations. This should really go into
268 // platform-specific directories.
278 wxColour
*col
= new wxColour( colour
);
282 return (wxColour
*) NULL
;
284 Append( colour
, col
);
294 Display
*display
= XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()) ;
297 Xv_Screen screen
= xv_get(xview_server
, SERVER_NTH_SCREEN
, 0);
298 Xv_opaque root_window
= xv_get(screen
, XV_ROOT
);
299 Display
*display
= (Display
*)xv_get(root_window
, XV_DISPLAY
);
302 /* MATTHEW: [4] Use wxGetMainColormap */
303 if (!XParseColor(display
, (Colormap
) wxTheApp
->GetMainColormap((WXDisplay
*) display
), colour
,&xcolour
))
306 unsigned char r
= (unsigned char)(xcolour
.red
>> 8);
307 unsigned char g
= (unsigned char)(xcolour
.green
>> 8);
308 unsigned char b
= (unsigned char)(xcolour
.blue
>> 8);
310 wxColour
*col
= new wxColour(r
, g
, b
);
318 wxString
wxColourDatabase::FindName (const wxColour
& colour
) const
322 unsigned char red
= colour
.Red ();
323 unsigned char green
= colour
.Green ();
324 unsigned char blue
= colour
.Blue ();
326 for (wxNode
* node
= First (); node
; node
= node
->Next ())
328 wxColour
*col
= (wxColour
*) node
->Data ();
330 if (col
->Red () == red
&& col
->Green () == green
&& col
->Blue () == blue
)
332 const wxChar
*found
= node
->GetKeyString();
345 void wxInitializeStockLists () {
346 wxTheBrushList
= new wxBrushList
;
347 wxThePenList
= new wxPenList
;
348 wxTheFontList
= new wxFontList
;
349 wxTheBitmapList
= new wxBitmapList
;
352 void wxInitializeStockObjects ()
358 // wxFontPool = new XFontPool;
361 // why under MSW fonts shouldn't have the standard system size?
363 static const int sizeFont
= 10;
365 static const int sizeFont
= 12;
368 wxNORMAL_FONT
= new wxFont (sizeFont
, wxMODERN
, wxNORMAL
, wxNORMAL
);
369 wxSMALL_FONT
= new wxFont (sizeFont
- 2, wxSWISS
, wxNORMAL
, wxNORMAL
);
370 wxITALIC_FONT
= new wxFont (sizeFont
, wxROMAN
, wxITALIC
, wxNORMAL
);
371 wxSWISS_FONT
= new wxFont (sizeFont
, wxSWISS
, wxNORMAL
, wxNORMAL
);
373 wxRED_PEN
= new wxPen ("RED", 1, wxSOLID
);
374 wxCYAN_PEN
= new wxPen ("CYAN", 1, wxSOLID
);
375 wxGREEN_PEN
= new wxPen ("GREEN", 1, wxSOLID
);
376 wxBLACK_PEN
= new wxPen ("BLACK", 1, wxSOLID
);
377 wxWHITE_PEN
= new wxPen ("WHITE", 1, wxSOLID
);
378 wxTRANSPARENT_PEN
= new wxPen ("BLACK", 1, wxTRANSPARENT
);
379 wxBLACK_DASHED_PEN
= new wxPen ("BLACK", 1, wxSHORT_DASH
);
380 wxGREY_PEN
= new wxPen ("GREY", 1, wxSOLID
);
381 wxMEDIUM_GREY_PEN
= new wxPen ("MEDIUM GREY", 1, wxSOLID
);
382 wxLIGHT_GREY_PEN
= new wxPen ("LIGHT GREY", 1, wxSOLID
);
384 wxBLUE_BRUSH
= new wxBrush ("BLUE", wxSOLID
);
385 wxGREEN_BRUSH
= new wxBrush ("GREEN", wxSOLID
);
386 wxWHITE_BRUSH
= new wxBrush ("WHITE", wxSOLID
);
387 wxBLACK_BRUSH
= new wxBrush ("BLACK", wxSOLID
);
388 wxTRANSPARENT_BRUSH
= new wxBrush ("BLACK", wxTRANSPARENT
);
389 wxCYAN_BRUSH
= new wxBrush ("CYAN", wxSOLID
);
390 wxRED_BRUSH
= new wxBrush ("RED", wxSOLID
);
391 wxGREY_BRUSH
= new wxBrush ("GREY", wxSOLID
);
392 wxMEDIUM_GREY_BRUSH
= new wxBrush ("MEDIUM GREY", wxSOLID
);
393 wxLIGHT_GREY_BRUSH
= new wxBrush ("LIGHT GREY", wxSOLID
);
395 wxBLACK
= new wxColour ("BLACK");
396 wxWHITE
= new wxColour ("WHITE");
397 wxRED
= new wxColour ("RED");
398 wxBLUE
= new wxColour ("BLUE");
399 wxGREEN
= new wxColour ("GREEN");
400 wxCYAN
= new wxColour ("CYAN");
401 wxLIGHT_GREY
= new wxColour ("LIGHT GREY");
403 wxSTANDARD_CURSOR
= new wxCursor (wxCURSOR_ARROW
);
404 wxHOURGLASS_CURSOR
= new wxCursor (wxCURSOR_WAIT
);
405 wxCROSS_CURSOR
= new wxCursor (wxCURSOR_CROSS
);
408 void wxDeleteStockObjects ()
410 wxDELETE(wxNORMAL_FONT
);
411 wxDELETE(wxSMALL_FONT
);
412 wxDELETE(wxITALIC_FONT
);
413 wxDELETE(wxSWISS_FONT
);
416 wxDELETE(wxCYAN_PEN
);
417 wxDELETE(wxGREEN_PEN
);
418 wxDELETE(wxBLACK_PEN
);
419 wxDELETE(wxWHITE_PEN
);
420 wxDELETE(wxTRANSPARENT_PEN
);
421 wxDELETE(wxBLACK_DASHED_PEN
);
422 wxDELETE(wxGREY_PEN
);
423 wxDELETE(wxMEDIUM_GREY_PEN
);
424 wxDELETE(wxLIGHT_GREY_PEN
);
426 wxDELETE(wxBLUE_BRUSH
);
427 wxDELETE(wxGREEN_BRUSH
);
428 wxDELETE(wxWHITE_BRUSH
);
429 wxDELETE(wxBLACK_BRUSH
);
430 wxDELETE(wxTRANSPARENT_BRUSH
);
431 wxDELETE(wxCYAN_BRUSH
);
432 wxDELETE(wxRED_BRUSH
);
433 wxDELETE(wxGREY_BRUSH
);
434 wxDELETE(wxMEDIUM_GREY_BRUSH
);
435 wxDELETE(wxLIGHT_GREY_BRUSH
);
443 wxDELETE(wxLIGHT_GREY
);
445 wxDELETE(wxSTANDARD_CURSOR
);
446 wxDELETE(wxHOURGLASS_CURSOR
);
447 wxDELETE(wxCROSS_CURSOR
);
450 void wxDeleteStockLists() {
451 wxDELETE(wxTheBrushList
);
452 wxDELETE(wxThePenList
);
453 wxDELETE(wxTheFontList
);
454 wxDELETE(wxTheBitmapList
);
457 wxBitmapList::wxBitmapList ()
461 wxBitmapList::~wxBitmapList ()
463 wxNode
*node
= First ();
466 wxBitmap
*bitmap
= (wxBitmap
*) node
->Data ();
467 wxNode
*next
= node
->Next ();
468 if (bitmap
->GetVisible())
474 // Pen and Brush lists
475 wxPenList::~wxPenList ()
477 wxNode
*node
= First ();
480 wxPen
*pen
= (wxPen
*) node
->Data ();
481 wxNode
*next
= node
->Next ();
482 if (pen
->GetVisible())
488 void wxPenList::AddPen (wxPen
* pen
)
493 void wxPenList::RemovePen (wxPen
* pen
)
498 wxPen
*wxPenList::FindOrCreatePen (const wxColour
& colour
, int width
, int style
)
500 for (wxNode
* node
= First (); node
; node
= node
->Next ())
502 wxPen
*each_pen
= (wxPen
*) node
->Data ();
504 each_pen
->GetVisible() &&
505 each_pen
->GetWidth () == width
&&
506 each_pen
->GetStyle () == style
&&
507 each_pen
->GetColour ().Red () == colour
.Red () &&
508 each_pen
->GetColour ().Green () == colour
.Green () &&
509 each_pen
->GetColour ().Blue () == colour
.Blue ())
512 wxPen
*pen
= new wxPen (colour
, width
, style
);
514 // Yes, we can return a pointer to this in a later FindOrCreatePen call,
515 // because we created it within FindOrCreatePen. Safeguards against
516 // returning a pointer to an automatic variable and hanging on to it
517 // (dangling pointer).
518 pen
->SetVisible(TRUE
);
523 wxBrushList::~wxBrushList ()
525 wxNode
*node
= First ();
528 wxBrush
*brush
= (wxBrush
*) node
->Data ();
529 wxNode
*next
= node
->Next ();
530 if (brush
->GetVisible())
536 void wxBrushList::AddBrush (wxBrush
* brush
)
541 wxBrush
*wxBrushList::FindOrCreateBrush (const wxColour
& colour
, int style
)
543 for (wxNode
* node
= First (); node
; node
= node
->Next ())
545 wxBrush
*each_brush
= (wxBrush
*) node
->Data ();
547 each_brush
->GetVisible() &&
548 each_brush
->GetStyle () == style
&&
549 each_brush
->GetColour ().Red () == colour
.Red () &&
550 each_brush
->GetColour ().Green () == colour
.Green () &&
551 each_brush
->GetColour ().Blue () == colour
.Blue ())
555 // Yes, we can return a pointer to this in a later FindOrCreateBrush call,
556 // because we created it within FindOrCreateBrush. Safeguards against
557 // returning a pointer to an automatic variable and hanging on to it
558 // (dangling pointer).
559 wxBrush
*brush
= new wxBrush (colour
, style
);
561 brush
->SetVisible(TRUE
);
566 void wxBrushList::RemoveBrush (wxBrush
* brush
)
568 DeleteObject (brush
);
571 wxFontList::~wxFontList ()
573 wxNode
*node
= First ();
576 // Only delete objects that are 'visible', i.e.
577 // that have been created using FindOrCreate...,
578 // where the pointers are expected to be shared
579 // (and therefore not deleted by any one part of an app).
580 wxFont
*font
= (wxFont
*) node
->Data ();
581 wxNode
*next
= node
->Next ();
582 if (font
->GetVisible())
588 void wxFontList::AddFont (wxFont
* font
)
593 void wxFontList::RemoveFont (wxFont
* font
)
599 FindOrCreateFont (int PointSize
, int FamilyOrFontId
, int Style
, int Weight
, bool underline
, const wxString
& Face
, wxFontEncoding encoding
)
601 for (wxNode
* node
= First (); node
; node
= node
->Next ())
603 wxFont
*each_font
= (wxFont
*) node
->Data ();
605 each_font
->GetVisible() &&
607 each_font
->GetPointSize () == PointSize
&&
608 each_font
->GetStyle () == Style
&&
609 each_font
->GetWeight () == Weight
&&
610 each_font
->GetUnderlined () == underline
&&
611 #if defined(__WXGTK__)
612 (each_font
->GetFamily() == FamilyOrFontId
||
613 (each_font
->GetFamily() == wxSWISS
&& FamilyOrFontId
== wxDEFAULT
)) &&
615 each_font
->GetFamily() == FamilyOrFontId
&&
617 ((each_font
->GetFaceName() == wxT("")) || each_font
->GetFaceName() == Face
) &&
618 (encoding
== wxFONTENCODING_DEFAULT
|| each_font
->GetEncoding() == encoding
))
621 wxFont
*font
= new wxFont (PointSize
, FamilyOrFontId
, Style
, Weight
, underline
, Face
, encoding
);
623 font
->SetVisible(TRUE
);
628 void wxBitmapList::AddBitmap(wxBitmap
*bitmap
)
633 void wxBitmapList::RemoveBitmap(wxBitmap
*bitmap
)
635 DeleteObject(bitmap
);
638 wxSize
wxGetDisplaySize()
641 wxDisplaySize(& x
, & y
);
645 wxResourceCache::~wxResourceCache ()
647 wxNode
*node
= First ();
649 wxGDIObject
*item
= (wxGDIObject
*)node
->Data();
650 if (item
->IsKindOf(CLASSINFO(wxBrush
))) {
651 wxBrush
*brush
= (wxBrush
*)item
;
655 if (item
->IsKindOf(CLASSINFO(wxFont
))) {
656 wxFont
*font
= (wxFont
*)item
;
660 if (item
->IsKindOf(CLASSINFO(wxBitmap
))) {
661 wxBitmap
*bitmap
= (wxBitmap
*)item
;
665 if (item
->IsKindOf(CLASSINFO(wxColour
))) {
666 wxColour
*colour
= (wxColour
*)item
;
670 wxNode
*next
= node
->Next ();