]>
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 #if !USE_SHARED_LIBRARY
54 IMPLEMENT_CLASS(wxColourDatabase
, wxList
)
55 IMPLEMENT_DYNAMIC_CLASS(wxFontList
, wxList
)
56 IMPLEMENT_DYNAMIC_CLASS(wxPenList
, wxList
)
57 IMPLEMENT_DYNAMIC_CLASS(wxBrushList
, wxList
)
58 IMPLEMENT_DYNAMIC_CLASS(wxBitmapList
, wxList
)
59 IMPLEMENT_DYNAMIC_CLASS(wxResourceCache
, wxList
)
61 IMPLEMENT_ABSTRACT_CLASS(wxDCBase
, wxObject
)
64 wxRect::wxRect(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
68 width
= bottomRight
.x
- topLeft
.x
;
69 height
= bottomRight
.y
- topLeft
.y
;
84 wxRect::wxRect(const wxPoint
& point
, const wxSize
& size
)
86 x
= point
.x
; y
= point
.y
;
87 width
= size
.x
; height
= size
.y
;
90 bool wxRect::operator==(const wxRect
& rect
) const
92 return ((x
== rect
.x
) &&
94 (width
== rect
.width
) &&
95 (height
== rect
.height
));
98 const wxRect
& wxRect::operator += (const wxRect
& rect
)
100 *this = (*this + rect
);
104 wxRect
wxRect::operator + (const wxRect
& rect
) const
106 int x1
= wxMin(this->x
, rect
.x
);
107 int y1
= wxMin(this->y
, rect
.y
);
108 int y2
= wxMax(y
+height
, rect
.height
+rect
.y
);
109 int x2
= wxMax(x
+width
, rect
.width
+rect
.x
);
110 return wxRect(x1
, y1
, x2
-x1
, y2
-y1
);
113 bool wxRect::Inside(int cx
, int cy
) const
115 return ( (cx
>= x
) && (cy
>= y
)
116 && ((cy
- y
) < height
)
117 && ((cx
- x
) < width
)
121 wxColourDatabase::wxColourDatabase (int type
) : wxList (type
)
125 wxColourDatabase::~wxColourDatabase ()
127 // Cleanup Colour allocated in Initialize()
128 wxNode
*node
= First ();
131 wxColour
*col
= (wxColour
*) node
->Data ();
132 wxNode
*next
= node
->Next ();
138 // Colour database stuff
139 void wxColourDatabase::Initialize ()
141 // Don't initialize for X: colours are found
142 // in FindColour below.
150 static cdef table
[]={
153 {wxT("AQUAMARINE"),112, 219, 147},
154 {wxT("BLACK"),0, 0, 0},
155 {wxT("BLUE"), 0, 0, 255},
156 {wxT("BLUE VIOLET"), 159, 95, 159},
157 {wxT("BROWN"), 165, 42, 42},
158 {wxT("CADET BLUE"), 95, 159, 159},
159 {wxT("CORAL"), 255, 127, 0},
160 {wxT("CORNFLOWER BLUE"), 66, 66, 111},
161 {wxT("CYAN"), 0, 255, 255},
162 {wxT("DARK GREY"), 47, 47, 47}, // ?
164 {wxT("DARK GREEN"), 47, 79, 47},
165 {wxT("DARK OLIVE GREEN"), 79, 79, 47},
166 {wxT("DARK ORCHID"), 153, 50, 204},
167 {wxT("DARK SLATE BLUE"), 107, 35, 142},
168 {wxT("DARK SLATE GREY"), 47, 79, 79},
169 {wxT("DARK TURQUOISE"), 112, 147, 219},
170 {wxT("DIM GREY"), 84, 84, 84},
171 {wxT("FIREBRICK"), 142, 35, 35},
172 {wxT("FOREST GREEN"), 35, 142, 35},
173 {wxT("GOLD"), 204, 127, 50},
174 {wxT("GOLDENROD"), 219, 219, 112},
175 {wxT("GREY"), 128, 128, 128},
176 {wxT("GREEN"), 0, 255, 0},
177 {wxT("GREEN YELLOW"), 147, 219, 112},
178 {wxT("INDIAN RED"), 79, 47, 47},
179 {wxT("KHAKI"), 159, 159, 95},
180 {wxT("LIGHT BLUE"), 191, 216, 216},
181 {wxT("LIGHT GREY"), 192, 192, 192},
182 {wxT("LIGHT STEEL BLUE"), 143, 143, 188},
183 {wxT("LIME GREEN"), 50, 204, 50},
184 {wxT("LIGHT MAGENTA"), 255, 0, 255},
185 {wxT("MAGENTA"), 255, 0, 255},
186 {wxT("MAROON"), 142, 35, 107},
187 {wxT("MEDIUM AQUAMARINE"), 50, 204, 153},
188 {wxT("MEDIUM GREY"), 100, 100, 100},
189 {wxT("MEDIUM BLUE"), 50, 50, 204},
190 {wxT("MEDIUM FOREST GREEN"), 107, 142, 35},
191 {wxT("MEDIUM GOLDENROD"), 234, 234, 173},
192 {wxT("MEDIUM ORCHID"), 147, 112, 219},
193 {wxT("MEDIUM SEA GREEN"), 66, 111, 66},
194 {wxT("MEDIUM SLATE BLUE"), 127, 0, 255},
195 {wxT("MEDIUM SPRING GREEN"), 127, 255, 0},
196 {wxT("MEDIUM TURQUOISE"), 112, 219, 219},
197 {wxT("MEDIUM VIOLET RED"), 219, 112, 147},
198 {wxT("MIDNIGHT BLUE"), 47, 47, 79},
199 {wxT("NAVY"), 35, 35, 142},
200 {wxT("ORANGE"), 204, 50, 50},
201 {wxT("ORANGE RED"), 255, 0, 127},
202 {wxT("ORCHID"), 219, 112, 219},
203 {wxT("PALE GREEN"), 143, 188, 143},
204 {wxT("PINK"), 188, 143, 234},
205 {wxT("PLUM"), 234, 173, 234},
206 {wxT("PURPLE"), 176, 0, 255},
207 {wxT("RED"), 255, 0, 0},
208 {wxT("SALMON"), 111, 66, 66},
209 {wxT("SEA GREEN"), 35, 142, 107},
210 {wxT("SIENNA"), 142, 107, 35},
211 {wxT("SKY BLUE"), 50, 153, 204},
212 {wxT("SLATE BLUE"), 0, 127, 255},
213 {wxT("SPRING GREEN"), 0, 255, 127},
214 {wxT("STEEL BLUE"), 35, 107, 142},
215 {wxT("TAN"), 219, 147, 112},
216 {wxT("THISTLE"), 216, 191, 216},
217 {wxT("TURQUOISE"), 173, 234, 234},
218 {wxT("VIOLET"), 79, 47, 79},
219 {wxT("VIOLET RED"), 204, 50, 153},
220 {wxT("WHEAT"), 216, 216, 191},
221 {wxT("WHITE"), 255, 255, 255},
222 {wxT("YELLOW"), 255, 255, 0},
223 {wxT("YELLOW GREEN"), 153, 204, 50},
226 #if defined(__WXGTK__) || defined(__X__)
227 {wxT("MEDIUM GOLDENROD"), 234, 234, 173},
228 {wxT("MEDIUM FOREST GREEN"), 107, 142, 35},
229 {wxT("LIGHT MAGENTA"), 255, 0, 255},
230 {wxT("MEDIUM GREY"), 100, 100, 100},
236 for (i
=0;cc
=table
[i
],cc
.name
!=0;i
++)
238 Append(cc
.name
,new wxColour(cc
.r
,cc
.g
,cc
.b
));
244 * Changed by Ian Brown, July 1994.
246 * When running under X, the Colour Database starts off empty. The X server
247 * is queried for the colour first time after which it is entered into the
248 * database. This allows our client to use the server colour database which
249 * is hopefully gamma corrected for the display being used.
252 wxColour
*wxColourDatabase::FindColour(const wxString
& colour
)
254 // VZ: make the comparaison case insensitive
255 wxString str
= colour
;
258 wxNode
*node
= Find(str
);
260 return (wxColour
*)node
->Data();
269 // TODO for other implementations. This should really go into
270 // platform-specific directories.
280 wxColour
*col
= new wxColour( colour
);
284 return (wxColour
*) NULL
;
286 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
);
320 wxString
wxColourDatabase::FindName (const wxColour
& colour
) const
324 unsigned char red
= colour
.Red ();
325 unsigned char green
= colour
.Green ();
326 unsigned char blue
= colour
.Blue ();
328 for (wxNode
* node
= First (); node
; node
= node
->Next ())
330 wxColour
*col
= (wxColour
*) node
->Data ();
332 if (col
->Red () == red
&& col
->Green () == green
&& col
->Blue () == blue
)
334 const wxChar
*found
= node
->GetKeyString();
347 void wxInitializeStockLists () {
348 wxTheBrushList
= new wxBrushList
;
349 wxThePenList
= new wxPenList
;
350 wxTheFontList
= new wxFontList
;
351 wxTheBitmapList
= new wxBitmapList
;
354 void wxInitializeStockObjects ()
360 // wxFontPool = new XFontPool;
363 // why under MSW fonts shouldn't have the standard system size?
365 static const int sizeFont
= 10;
367 static const int sizeFont
= 12;
370 wxNORMAL_FONT
= new wxFont (sizeFont
, wxMODERN
, wxNORMAL
, wxNORMAL
);
371 wxSMALL_FONT
= new wxFont (sizeFont
- 2, wxSWISS
, wxNORMAL
, wxNORMAL
);
372 wxITALIC_FONT
= new wxFont (sizeFont
, wxROMAN
, wxITALIC
, wxNORMAL
);
373 wxSWISS_FONT
= new wxFont (sizeFont
, wxSWISS
, wxNORMAL
, wxNORMAL
);
375 wxRED_PEN
= new wxPen ("RED", 1, wxSOLID
);
376 wxCYAN_PEN
= new wxPen ("CYAN", 1, wxSOLID
);
377 wxGREEN_PEN
= new wxPen ("GREEN", 1, wxSOLID
);
378 wxBLACK_PEN
= new wxPen ("BLACK", 1, wxSOLID
);
379 wxWHITE_PEN
= new wxPen ("WHITE", 1, wxSOLID
);
380 wxTRANSPARENT_PEN
= new wxPen ("BLACK", 1, wxTRANSPARENT
);
381 wxBLACK_DASHED_PEN
= new wxPen ("BLACK", 1, wxSHORT_DASH
);
382 wxGREY_PEN
= new wxPen ("GREY", 1, wxSOLID
);
383 wxMEDIUM_GREY_PEN
= new wxPen ("MEDIUM GREY", 1, wxSOLID
);
384 wxLIGHT_GREY_PEN
= new wxPen ("LIGHT GREY", 1, wxSOLID
);
386 wxBLUE_BRUSH
= new wxBrush ("BLUE", wxSOLID
);
387 wxGREEN_BRUSH
= new wxBrush ("GREEN", wxSOLID
);
388 wxWHITE_BRUSH
= new wxBrush ("WHITE", wxSOLID
);
389 wxBLACK_BRUSH
= new wxBrush ("BLACK", wxSOLID
);
390 wxTRANSPARENT_BRUSH
= new wxBrush ("BLACK", wxTRANSPARENT
);
391 wxCYAN_BRUSH
= new wxBrush ("CYAN", wxSOLID
);
392 wxRED_BRUSH
= new wxBrush ("RED", wxSOLID
);
393 wxGREY_BRUSH
= new wxBrush ("GREY", wxSOLID
);
394 wxMEDIUM_GREY_BRUSH
= new wxBrush ("MEDIUM GREY", wxSOLID
);
395 wxLIGHT_GREY_BRUSH
= new wxBrush ("LIGHT GREY", wxSOLID
);
397 wxBLACK
= new wxColour ("BLACK");
398 wxWHITE
= new wxColour ("WHITE");
399 wxRED
= new wxColour ("RED");
400 wxBLUE
= new wxColour ("BLUE");
401 wxGREEN
= new wxColour ("GREEN");
402 wxCYAN
= new wxColour ("CYAN");
403 wxLIGHT_GREY
= new wxColour ("LIGHT GREY");
405 wxSTANDARD_CURSOR
= new wxCursor (wxCURSOR_ARROW
);
406 wxHOURGLASS_CURSOR
= new wxCursor (wxCURSOR_WAIT
);
407 wxCROSS_CURSOR
= new wxCursor (wxCURSOR_CROSS
);
410 void wxDeleteStockObjects ()
412 wxDELETE(wxNORMAL_FONT
);
413 wxDELETE(wxSMALL_FONT
);
414 wxDELETE(wxITALIC_FONT
);
415 wxDELETE(wxSWISS_FONT
);
418 wxDELETE(wxCYAN_PEN
);
419 wxDELETE(wxGREEN_PEN
);
420 wxDELETE(wxBLACK_PEN
);
421 wxDELETE(wxWHITE_PEN
);
422 wxDELETE(wxTRANSPARENT_PEN
);
423 wxDELETE(wxBLACK_DASHED_PEN
);
424 wxDELETE(wxGREY_PEN
);
425 wxDELETE(wxMEDIUM_GREY_PEN
);
426 wxDELETE(wxLIGHT_GREY_PEN
);
428 wxDELETE(wxBLUE_BRUSH
);
429 wxDELETE(wxGREEN_BRUSH
);
430 wxDELETE(wxWHITE_BRUSH
);
431 wxDELETE(wxBLACK_BRUSH
);
432 wxDELETE(wxTRANSPARENT_BRUSH
);
433 wxDELETE(wxCYAN_BRUSH
);
434 wxDELETE(wxRED_BRUSH
);
435 wxDELETE(wxGREY_BRUSH
);
436 wxDELETE(wxMEDIUM_GREY_BRUSH
);
437 wxDELETE(wxLIGHT_GREY_BRUSH
);
445 wxDELETE(wxLIGHT_GREY
);
447 wxDELETE(wxSTANDARD_CURSOR
);
448 wxDELETE(wxHOURGLASS_CURSOR
);
449 wxDELETE(wxCROSS_CURSOR
);
452 void wxDeleteStockLists() {
453 wxDELETE(wxTheBrushList
);
454 wxDELETE(wxThePenList
);
455 wxDELETE(wxTheFontList
);
456 wxDELETE(wxTheBitmapList
);
459 wxBitmapList::wxBitmapList ()
463 wxBitmapList::~wxBitmapList ()
465 wxNode
*node
= First ();
468 wxBitmap
*bitmap
= (wxBitmap
*) node
->Data ();
469 wxNode
*next
= node
->Next ();
470 if (bitmap
->GetVisible())
476 // Pen and Brush lists
477 wxPenList::~wxPenList ()
479 wxNode
*node
= First ();
482 wxPen
*pen
= (wxPen
*) node
->Data ();
483 wxNode
*next
= node
->Next ();
484 if (pen
->GetVisible())
490 void wxPenList::AddPen (wxPen
* pen
)
495 void wxPenList::RemovePen (wxPen
* pen
)
500 wxPen
*wxPenList::FindOrCreatePen (const wxColour
& colour
, int width
, int style
)
502 for (wxNode
* node
= First (); node
; node
= node
->Next ())
504 wxPen
*each_pen
= (wxPen
*) node
->Data ();
506 each_pen
->GetVisible() &&
507 each_pen
->GetWidth () == width
&&
508 each_pen
->GetStyle () == style
&&
509 each_pen
->GetColour ().Red () == colour
.Red () &&
510 each_pen
->GetColour ().Green () == colour
.Green () &&
511 each_pen
->GetColour ().Blue () == colour
.Blue ())
514 wxPen
*pen
= new wxPen (colour
, width
, style
);
516 // Yes, we can return a pointer to this in a later FindOrCreatePen call,
517 // because we created it within FindOrCreatePen. Safeguards against
518 // returning a pointer to an automatic variable and hanging on to it
519 // (dangling pointer).
520 pen
->SetVisible(TRUE
);
525 wxBrushList::~wxBrushList ()
527 wxNode
*node
= First ();
530 wxBrush
*brush
= (wxBrush
*) node
->Data ();
531 wxNode
*next
= node
->Next ();
532 if (brush
->GetVisible())
538 void wxBrushList::AddBrush (wxBrush
* brush
)
543 wxBrush
*wxBrushList::FindOrCreateBrush (const wxColour
& colour
, int style
)
545 for (wxNode
* node
= First (); node
; node
= node
->Next ())
547 wxBrush
*each_brush
= (wxBrush
*) node
->Data ();
549 each_brush
->GetVisible() &&
550 each_brush
->GetStyle () == style
&&
551 each_brush
->GetColour ().Red () == colour
.Red () &&
552 each_brush
->GetColour ().Green () == colour
.Green () &&
553 each_brush
->GetColour ().Blue () == colour
.Blue ())
557 // Yes, we can return a pointer to this in a later FindOrCreateBrush call,
558 // because we created it within FindOrCreateBrush. Safeguards against
559 // returning a pointer to an automatic variable and hanging on to it
560 // (dangling pointer).
561 wxBrush
*brush
= new wxBrush (colour
, style
);
563 brush
->SetVisible(TRUE
);
568 void wxBrushList::RemoveBrush (wxBrush
* brush
)
570 DeleteObject (brush
);
573 wxFontList::~wxFontList ()
575 wxNode
*node
= First ();
578 // Only delete objects that are 'visible', i.e.
579 // that have been created using FindOrCreate...,
580 // where the pointers are expected to be shared
581 // (and therefore not deleted by any one part of an app).
582 wxFont
*font
= (wxFont
*) node
->Data ();
583 wxNode
*next
= node
->Next ();
584 if (font
->GetVisible())
590 void wxFontList::AddFont (wxFont
* font
)
595 void wxFontList::RemoveFont (wxFont
* font
)
601 FindOrCreateFont (int PointSize
, int FamilyOrFontId
, int Style
, int Weight
, bool underline
, const wxString
& Face
, wxFontEncoding encoding
)
603 for (wxNode
* node
= First (); node
; node
= node
->Next ())
605 wxFont
*each_font
= (wxFont
*) node
->Data ();
607 each_font
->GetVisible() &&
609 each_font
->GetPointSize () == PointSize
&&
610 each_font
->GetStyle () == Style
&&
611 each_font
->GetWeight () == Weight
&&
612 each_font
->GetUnderlined () == underline
&&
613 #if defined(__WXGTK__)
614 (each_font
->GetFamily() == FamilyOrFontId
||
615 (each_font
->GetFamily() == wxSWISS
&& FamilyOrFontId
== wxDEFAULT
)) &&
617 each_font
->GetFamily() == FamilyOrFontId
&&
619 ((each_font
->GetFaceName() == wxT("")) || each_font
->GetFaceName() == Face
) &&
620 (encoding
== wxFONTENCODING_DEFAULT
|| each_font
->GetEncoding() == encoding
))
623 wxFont
*font
= new wxFont (PointSize
, FamilyOrFontId
, Style
, Weight
, underline
, Face
, encoding
);
625 font
->SetVisible(TRUE
);
630 void wxBitmapList::AddBitmap(wxBitmap
*bitmap
)
635 void wxBitmapList::RemoveBitmap(wxBitmap
*bitmap
)
637 DeleteObject(bitmap
);
640 wxSize
wxGetDisplaySize()
643 wxDisplaySize(& x
, & y
);
647 wxResourceCache::~wxResourceCache ()
649 wxNode
*node
= First ();
651 wxGDIObject
*item
= (wxGDIObject
*)node
->Data();
652 if (item
->IsKindOf(CLASSINFO(wxBrush
))) {
653 wxBrush
*brush
= (wxBrush
*)item
;
657 if (item
->IsKindOf(CLASSINFO(wxFont
))) {
658 wxFont
*font
= (wxFont
*)item
;
662 if (item
->IsKindOf(CLASSINFO(wxBitmap
))) {
663 wxBitmap
*bitmap
= (wxBitmap
*)item
;
667 if (item
->IsKindOf(CLASSINFO(wxColour
))) {
668 wxColour
*colour
= (wxColour
*)item
;
672 wxNode
*next
= node
->Next ();