]>
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"
47 #if !USE_SHARED_LIBRARY
48 IMPLEMENT_CLASS(wxColourDatabase
, wxList
)
49 IMPLEMENT_DYNAMIC_CLASS(wxFontList
, wxList
)
50 IMPLEMENT_DYNAMIC_CLASS(wxPenList
, wxList
)
51 IMPLEMENT_DYNAMIC_CLASS(wxBrushList
, wxList
)
52 IMPLEMENT_DYNAMIC_CLASS(wxBitmapList
, wxList
)
53 IMPLEMENT_DYNAMIC_CLASS(wxResourceCache
, wxList
)
55 IMPLEMENT_ABSTRACT_CLASS(wxDCBase
, wxObject
)
58 wxRect::wxRect(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
62 width
= bottomRight
.x
- topLeft
.x
;
63 height
= bottomRight
.y
- topLeft
.y
;
78 wxRect::wxRect(const wxPoint
& point
, const wxSize
& size
)
80 x
= point
.x
; y
= point
.y
;
81 width
= size
.x
; height
= size
.y
;
84 bool wxRect::operator==(const wxRect
& rect
) const
86 return ((x
== rect
.x
) &&
88 (width
== rect
.width
) &&
89 (height
== rect
.height
));
92 const wxRect
& wxRect::operator += (const wxRect
& rect
)
94 *this = (*this + rect
);
98 wxRect
wxRect::operator + (const wxRect
& rect
) const
100 int x1
= wxMin(this->x
, rect
.x
);
101 int y1
= wxMin(this->y
, rect
.y
);
102 int y2
= wxMax(y
+height
, rect
.height
+rect
.y
);
103 int x2
= wxMax(x
+width
, rect
.width
+rect
.x
);
104 return wxRect(x1
, y1
, x2
-x1
, y2
-y1
);
107 bool wxRect::Inside(int cx
, int cy
) const
109 return ( (cx
>= x
) && (cy
>= y
)
110 && ((cy
- y
) < height
)
111 && ((cx
- x
) < width
)
115 wxColourDatabase::wxColourDatabase (int type
) : wxList (type
)
119 wxColourDatabase::~wxColourDatabase ()
121 // Cleanup Colour allocated in Initialize()
122 wxNode
*node
= First ();
125 wxColour
*col
= (wxColour
*) node
->Data ();
126 wxNode
*next
= node
->Next ();
132 // Colour database stuff
133 void wxColourDatabase::Initialize ()
135 // Don't initialize for X: colours are found
136 // in FindColour below.
144 static cdef table
[]={
147 {T("AQUAMARINE"),112, 219, 147},
148 {T("BLACK"),0, 0, 0},
149 {T("BLUE"), 0, 0, 255},
150 {T("BLUE VIOLET("), 159, 95, 159},
151 {T("BROWN"), 165, 42, 42},
152 {T("CADET BLUE"), 95, 159, 159},
153 {T("CORAL"), 255, 127, 0},
154 {T("CORNFLOWER BLUE"), 66, 66, 111},
155 {T("CYAN"), 0, 255, 255},
156 {T("DARK GREY"), 47, 47, 47}, // ?
158 {T("DARK GREEN"), 47, 79, 47},
159 {T("DARK OLIVE GREEN"), 79, 79, 47},
160 {T("DARK ORCHID"), 153, 50, 204},
161 {T("DARK SLATE BLUE"), 107, 35, 142},
162 {T("DARK SLATE GREY"), 47, 79, 79},
163 {T("DARK TURQUOISE"), 112, 147, 219},
164 {T("DIM GREY"), 84, 84, 84},
165 {T("FIREBRICK"), 142, 35, 35},
166 {T("FOREST GREEN"), 35, 142, 35},
167 {T("GOLD"), 204, 127, 50},
168 {T("GOLDENROD"), 219, 219, 112},
169 {T("GREY"), 128, 128, 128},
170 {T("GREEN"), 0, 255, 0},
171 {T("GREEN YELLOW"), 147, 219, 112},
172 {T("INDIAN RED"), 79, 47, 47},
173 {T("KHAKI"), 159, 159, 95},
174 {T("LIGHT BLUE"), 191, 216, 216},
175 {T("LIGHT GREY"), 192, 192, 192},
176 {T("LIGHT STEEL BLUE"), 143, 143, 188},
177 {T("LIME GREEN"), 50, 204, 50},
178 {T("LIGHT MAGENTA"), 255, 0, 255},
179 {T("MAGENTA"), 255, 0, 255},
180 {T("MAROON"), 142, 35, 107},
181 {T("MEDIUM AQUAMARINE"), 50, 204, 153},
182 {T("MEDIUM GREY"), 100, 100, 100},
183 {T("MEDIUM BLUE"), 50, 50, 204},
184 {T("MEDIUM FOREST GREEN"), 107, 142, 35},
185 {T("MEDIUM GOLDENROD"), 234, 234, 173},
186 {T("MEDIUM ORCHID"), 147, 112, 219},
187 {T("MEDIUM SEA GREEN"), 66, 111, 66},
188 {T("MEDIUM SLATE BLUE"), 127, 0, 255},
189 {T("MEDIUM SPRING GREEN"), 127, 255, 0},
190 {T("MEDIUM TURQUOISE"), 112, 219, 219},
191 {T("MEDIUM VIOLET RED"), 219, 112, 147},
192 {T("MIDNIGHT BLUE"), 47, 47, 79},
193 {T("NAVY"), 35, 35, 142},
194 {T("ORANGE"), 204, 50, 50},
195 {T("ORANGE RED"), 255, 0, 127},
196 {T("ORCHID"), 219, 112, 219},
197 {T("PALE GREEN"), 143, 188, 143},
198 {T("PINK"), 188, 143, 234},
199 {T("PLUM"), 234, 173, 234},
200 {T("PURPLE"), 176, 0, 255},
201 {T("RED"), 255, 0, 0},
202 {T("SALMON"), 111, 66, 66},
203 {T("SEA GREEN"), 35, 142, 107},
204 {T("SIENNA"), 142, 107, 35},
205 {T("SKY BLUE"), 50, 153, 204},
206 {T("SLATE BLUE"), 0, 127, 255},
207 {T("SPRING GREEN"), 0, 255, 127},
208 {T("STEEL BLUE"), 35, 107, 142},
209 {T("TAN"), 219, 147, 112},
210 {T("THISTLE"), 216, 191, 216},
211 {T("TURQUOISE"), 173, 234, 234},
212 {T("VIOLET("), 79, 47, 79},
213 {T("VIOLET RED"), 204, 50, 153},
214 {T("WHEAT("), 216, 216, 191},
215 {T("WHITE"), 255, 255, 255},
216 {T("YELLOW"), 255, 255, 0},
217 {T("YELLOW GREEN"), 153, 204, 50},
220 #if defined(__WXGTK__) || defined(__X__)
221 {T("MEDIUM GOLDENROD"), 234, 234, 173},
222 {T("MEDIUM FOREST GREEN"), 107, 142, 35},
223 {T("LIGHT MAGENTA"), 255, 0, 255},
224 {T("MEDIUM GREY"), 100, 100, 100},
230 for (i
=0;cc
=table
[i
],cc
.name
!=0;i
++)
232 Append(cc
.name
,new wxColour(cc
.r
,cc
.g
,cc
.b
));
238 * Changed by Ian Brown, July 1994.
240 * When running under X, the Colour Database starts off empty. The X server
241 * is queried for the colour first time after which it is entered into the
242 * database. This allows our client to use the server colour database which
243 * is hopefully gamma corrected for the display being used.
246 wxColour
*wxColourDatabase::FindColour(const wxString
& colour
)
248 // VZ: make the comparaison case insensitive
249 wxString str
= colour
;
252 wxNode
*node
= Find(str
);
254 return (wxColour
*)node
->Data();
263 // TODO for other implementations. This should really go into
264 // platform-specific directories.
274 wxColour
*col
= new wxColour( colour
);
278 return (wxColour
*) NULL
;
280 Append( colour
, col
);
290 Display
*display
= XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()) ;
293 Xv_Screen screen
= xv_get(xview_server
, SERVER_NTH_SCREEN
, 0);
294 Xv_opaque root_window
= xv_get(screen
, XV_ROOT
);
295 Display
*display
= (Display
*)xv_get(root_window
, XV_DISPLAY
);
298 /* MATTHEW: [4] Use wxGetMainColormap */
299 if (!XParseColor(display
, (Colormap
) wxTheApp
->GetMainColormap((WXDisplay
*) display
), colour
,&xcolour
))
302 unsigned char r
= (unsigned char)(xcolour
.red
>> 8);
303 unsigned char g
= (unsigned char)(xcolour
.green
>> 8);
304 unsigned char b
= (unsigned char)(xcolour
.blue
>> 8);
306 wxColour
*col
= new wxColour(r
, g
, b
);
314 wxString
wxColourDatabase::FindName (const wxColour
& colour
) const
318 unsigned char red
= colour
.Red ();
319 unsigned char green
= colour
.Green ();
320 unsigned char blue
= colour
.Blue ();
322 for (wxNode
* node
= First (); node
; node
= node
->Next ())
324 wxColour
*col
= (wxColour
*) node
->Data ();
326 if (col
->Red () == red
&& col
->Green () == green
&& col
->Blue () == blue
)
328 const wxChar
*found
= node
->GetKeyString();
341 void wxInitializeStockLists () {
342 wxTheBrushList
= new wxBrushList
;
343 wxThePenList
= new wxPenList
;
344 wxTheFontList
= new wxFontList
;
345 wxTheBitmapList
= new wxBitmapList
;
348 void wxInitializeStockObjects ()
354 // wxFontPool = new XFontPool;
357 wxNORMAL_FONT
= new wxFont (12, wxMODERN
, wxNORMAL
, wxNORMAL
);
358 wxSMALL_FONT
= new wxFont (10, wxSWISS
, wxNORMAL
, wxNORMAL
);
359 wxITALIC_FONT
= new wxFont (12, wxROMAN
, wxITALIC
, wxNORMAL
);
360 wxSWISS_FONT
= new wxFont (12, wxSWISS
, wxNORMAL
, wxNORMAL
);
362 wxRED_PEN
= new wxPen ("RED", 1, wxSOLID
);
363 wxCYAN_PEN
= new wxPen ("CYAN", 1, wxSOLID
);
364 wxGREEN_PEN
= new wxPen ("GREEN", 1, wxSOLID
);
365 wxBLACK_PEN
= new wxPen ("BLACK", 1, wxSOLID
);
366 wxWHITE_PEN
= new wxPen ("WHITE", 1, wxSOLID
);
367 wxTRANSPARENT_PEN
= new wxPen ("BLACK", 1, wxTRANSPARENT
);
368 wxBLACK_DASHED_PEN
= new wxPen ("BLACK", 1, wxSHORT_DASH
);
369 wxGREY_PEN
= new wxPen ("GREY", 1, wxSOLID
);
370 wxMEDIUM_GREY_PEN
= new wxPen ("MEDIUM GREY", 1, wxSOLID
);
371 wxLIGHT_GREY_PEN
= new wxPen ("LIGHT GREY", 1, wxSOLID
);
373 wxBLUE_BRUSH
= new wxBrush ("BLUE", wxSOLID
);
374 wxGREEN_BRUSH
= new wxBrush ("GREEN", wxSOLID
);
375 wxWHITE_BRUSH
= new wxBrush ("WHITE", wxSOLID
);
376 wxBLACK_BRUSH
= new wxBrush ("BLACK", wxSOLID
);
377 wxTRANSPARENT_BRUSH
= new wxBrush ("BLACK", wxTRANSPARENT
);
378 wxCYAN_BRUSH
= new wxBrush ("CYAN", wxSOLID
);
379 wxRED_BRUSH
= new wxBrush ("RED", wxSOLID
);
380 wxGREY_BRUSH
= new wxBrush ("GREY", wxSOLID
);
381 wxMEDIUM_GREY_BRUSH
= new wxBrush ("MEDIUM GREY", wxSOLID
);
382 wxLIGHT_GREY_BRUSH
= new wxBrush ("LIGHT GREY", wxSOLID
);
384 wxBLACK
= new wxColour ("BLACK");
385 wxWHITE
= new wxColour ("WHITE");
386 wxRED
= new wxColour ("RED");
387 wxBLUE
= new wxColour ("BLUE");
388 wxGREEN
= new wxColour ("GREEN");
389 wxCYAN
= new wxColour ("CYAN");
390 wxLIGHT_GREY
= new wxColour ("LIGHT GREY");
392 wxSTANDARD_CURSOR
= new wxCursor (wxCURSOR_ARROW
);
393 wxHOURGLASS_CURSOR
= new wxCursor (wxCURSOR_WAIT
);
394 wxCROSS_CURSOR
= new wxCursor (wxCURSOR_CROSS
);
397 void wxDeleteStockObjects ()
399 wxDELETE(wxNORMAL_FONT
);
400 wxDELETE(wxSMALL_FONT
);
401 wxDELETE(wxITALIC_FONT
);
402 wxDELETE(wxSWISS_FONT
);
405 wxDELETE(wxCYAN_PEN
);
406 wxDELETE(wxGREEN_PEN
);
407 wxDELETE(wxBLACK_PEN
);
408 wxDELETE(wxWHITE_PEN
);
409 wxDELETE(wxTRANSPARENT_PEN
);
410 wxDELETE(wxBLACK_DASHED_PEN
);
411 wxDELETE(wxGREY_PEN
);
412 wxDELETE(wxMEDIUM_GREY_PEN
);
413 wxDELETE(wxLIGHT_GREY_PEN
);
415 wxDELETE(wxBLUE_BRUSH
);
416 wxDELETE(wxGREEN_BRUSH
);
417 wxDELETE(wxWHITE_BRUSH
);
418 wxDELETE(wxBLACK_BRUSH
);
419 wxDELETE(wxTRANSPARENT_BRUSH
);
420 wxDELETE(wxCYAN_BRUSH
);
421 wxDELETE(wxRED_BRUSH
);
422 wxDELETE(wxGREY_BRUSH
);
423 wxDELETE(wxMEDIUM_GREY_BRUSH
);
424 wxDELETE(wxLIGHT_GREY_BRUSH
);
432 wxDELETE(wxLIGHT_GREY
);
434 wxDELETE(wxSTANDARD_CURSOR
);
435 wxDELETE(wxHOURGLASS_CURSOR
);
436 wxDELETE(wxCROSS_CURSOR
);
439 void wxDeleteStockLists() {
440 wxDELETE(wxTheBrushList
);
441 wxDELETE(wxThePenList
);
442 wxDELETE(wxTheFontList
);
443 wxDELETE(wxTheBitmapList
);
446 wxBitmapList::wxBitmapList ()
450 wxBitmapList::~wxBitmapList ()
452 #if defined(__WXMSW__) || defined(__WXMOTIF__)
453 wxNode
*node
= First ();
456 wxBitmap
*bitmap
= (wxBitmap
*) node
->Data ();
457 wxNode
*next
= node
->Next ();
458 if (bitmap
->GetVisible())
465 // Pen and Brush lists
466 wxPenList::~wxPenList ()
468 #if defined(__WXMSW__) || defined(__WXMOTIF__)
469 wxNode
*node
= First ();
472 wxPen
*pen
= (wxPen
*) node
->Data ();
473 wxNode
*next
= node
->Next ();
474 if (pen
->GetVisible())
481 void wxPenList::AddPen (wxPen
* pen
)
486 void wxPenList::RemovePen (wxPen
* pen
)
491 wxPen
*wxPenList::FindOrCreatePen (const wxColour
& colour
, int width
, int style
)
493 for (wxNode
* node
= First (); node
; node
= node
->Next ())
495 wxPen
*each_pen
= (wxPen
*) node
->Data ();
496 if (each_pen
&& each_pen
->GetVisible() &&
497 each_pen
->GetWidth () == width
&&
498 each_pen
->GetStyle () == style
&&
499 each_pen
->GetColour ().Red () == colour
.Red () &&
500 each_pen
->GetColour ().Green () == colour
.Green () &&
501 each_pen
->GetColour ().Blue () == colour
.Blue ())
504 wxPen
*pen
= new wxPen (colour
, width
, style
);
506 // Yes, we can return a pointer to this in a later FindOrCreatePen call,
507 // because we created it within FindOrCreatePen. Safeguards against
508 // returning a pointer to an automatic variable and hanging on to it
509 // (dangling pointer).
510 pen
->SetVisible(TRUE
);
514 wxBrushList::~wxBrushList ()
516 #if defined(__WXMSW__) || defined(__WXMOTIF__)
517 wxNode
*node
= First ();
520 wxBrush
*brush
= (wxBrush
*) node
->Data ();
521 wxNode
*next
= node
->Next ();
522 if (brush
->GetVisible())
529 void wxBrushList::AddBrush (wxBrush
* brush
)
534 wxBrush
*wxBrushList::FindOrCreateBrush (const wxColour
& colour
, int style
)
536 for (wxNode
* node
= First (); node
; node
= node
->Next ())
538 wxBrush
*each_brush
= (wxBrush
*) node
->Data ();
539 if (each_brush
&& each_brush
->GetVisible() &&
540 each_brush
->GetStyle () == style
&&
541 each_brush
->GetColour ().Red () == colour
.Red () &&
542 each_brush
->GetColour ().Green () == colour
.Green () &&
543 each_brush
->GetColour ().Blue () == colour
.Blue ())
546 // Yes, we can return a pointer to this in a later FindOrCreateBrush call,
547 // because we created it within FindOrCreateBrush. Safeguards against
548 // returning a pointer to an automatic variable and hanging on to it
549 // (dangling pointer).
550 wxBrush
*brush
= new wxBrush (colour
, style
);
551 brush
->SetVisible(TRUE
);
555 void wxBrushList::RemoveBrush (wxBrush
* brush
)
557 DeleteObject (brush
);
560 wxFontList::~wxFontList ()
562 wxNode
*node
= First ();
565 // Only delete objects that are 'visible', i.e.
566 // that have been created using FindOrCreate...,
567 // where the pointers are expected to be shared
568 // (and therefore not deleted by any one part of an app).
569 wxFont
*font
= (wxFont
*) node
->Data ();
570 wxNode
*next
= node
->Next ();
571 if (font
->GetVisible())
577 void wxFontList::AddFont (wxFont
* font
)
582 void wxFontList::RemoveFont (wxFont
* font
)
588 FindOrCreateFont (int PointSize
, int FamilyOrFontId
, int Style
, int Weight
, bool underline
, const wxString
& Face
)
590 for (wxNode
* node
= First (); node
; node
= node
->Next ())
592 wxFont
*each_font
= (wxFont
*) node
->Data ();
593 if (each_font
&& each_font
->GetVisible() && each_font
->Ok() &&
594 each_font
->GetPointSize () == PointSize
&&
595 each_font
->GetStyle () == Style
&&
596 each_font
->GetWeight () == Weight
&&
597 each_font
->GetUnderlined () == underline
&&
599 // each_font->GetFontId () == FamilyOrFontId) /* New font system */
601 #if defined(__WXGTK__)
602 (each_font
->GetFamily() == FamilyOrFontId
||
603 (each_font
->GetFamily() == wxSWISS
&& FamilyOrFontId
== wxDEFAULT
)) &&
605 each_font
->GetFamily() == FamilyOrFontId
&&
607 ((each_font
->GetFaceName() == T("")) || each_font
->GetFaceName() == Face
))
611 wxFont
*font
= new wxFont (PointSize
, FamilyOrFontId
, Style
, Weight
, underline
, Face
);
612 font
->SetVisible(TRUE
);
616 void wxBitmapList::AddBitmap(wxBitmap
*bitmap
)
621 void wxBitmapList::RemoveBitmap(wxBitmap
*bitmap
)
623 DeleteObject(bitmap
);
626 wxSize
wxGetDisplaySize()
629 wxDisplaySize(& x
, & y
);
633 wxResourceCache::~wxResourceCache ()
635 wxNode
*node
= First ();
637 wxGDIObject
*item
= (wxGDIObject
*)node
->Data();
638 if (item
->IsKindOf(CLASSINFO(wxBrush
))) {
639 wxBrush
*brush
= (wxBrush
*)item
;
643 if (item
->IsKindOf(CLASSINFO(wxFont
))) {
644 wxFont
*font
= (wxFont
*)item
;
648 if (item
->IsKindOf(CLASSINFO(wxBitmap
))) {
649 wxBitmap
*bitmap
= (wxBitmap
*)item
;
653 if (item
->IsKindOf(CLASSINFO(wxColour
))) {
654 wxColour
*colour
= (wxColour
*)item
;
658 wxNode
*next
= node
->Next ();