]>
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"
23 #include "wx/gdicmn.h"
26 #include "wx/bitmap.h"
28 #include "wx/cursor.h"
30 #include "wx/palette.h"
38 #if !USE_SHARED_LIBRARY
39 IMPLEMENT_CLASS(wxColourDatabase
, wxList
)
40 IMPLEMENT_DYNAMIC_CLASS(wxFontList
, wxList
)
41 IMPLEMENT_DYNAMIC_CLASS(wxPenList
, wxList
)
42 IMPLEMENT_DYNAMIC_CLASS(wxBrushList
, wxList
)
43 IMPLEMENT_DYNAMIC_CLASS(wxBitmapList
, wxList
)
44 IMPLEMENT_DYNAMIC_CLASS(wxResourceCache
, wxList
)
46 IMPLEMENT_DYNAMIC_CLASS(wxRect, wxObject)
47 IMPLEMENT_DYNAMIC_CLASS(wxPoint, wxObject)
48 IMPLEMENT_DYNAMIC_CLASS(wxRealPoint, wxObject)
54 x
= 0; y
= 0; width
= 0; height
= 0;
57 wxRect::wxRect(long xx
, long yy
, long w
, long h
)
59 x
= xx
; y
= yy
; width
= w
; height
= h
;
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 wxRect::wxRect(const wxRect
& rect
)
96 wxRect
& wxRect::operator = (const wxRect
& rect
)
98 x
= rect
.x
; y
= rect
.y
; width
= rect
.width
; height
= rect
.height
;
102 bool wxRect::operator == (const wxRect
& rect
)
104 return ((x
== rect
.x
) &&
106 (width
== rect
.width
) &&
107 (height
== rect
.height
));
110 bool wxRect::operator != (const wxRect
& rect
)
112 return ((x
!= rect
.x
) ||
114 (width
!= rect
.width
) ||
115 (height
!= rect
.height
));
118 wxColourDatabase::wxColourDatabase (int 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 {"AQUAMARINE",112, 219, 147},
154 {"BLUE VIOLET", 159, 95, 159},
155 {"BROWN", 165, 42, 42},
156 {"CADET BLUE", 95, 159, 159},
157 {"CORAL", 255, 127, 0},
158 {"CORNFLOWER BLUE", 66, 66, 111},
159 {"CYAN", 0, 255, 255},
160 {"DARK GREY", 47, 47, 47}, // ?
162 {"DARK GREEN", 47, 79, 47},
163 {"DARK OLIVE GREEN", 79, 79, 47},
164 {"DARK ORCHID", 153, 50, 204},
165 {"DARK SLATE BLUE", 107, 35, 142},
166 {"DARK SLATE GREY", 47, 79, 79},
167 {"DARK TURQUOISE", 112, 147, 219},
168 {"DIM GREY", 84, 84, 84},
169 {"FIREBRICK", 142, 35, 35},
170 {"FOREST GREEN", 35, 142, 35},
171 {"GOLD", 204, 127, 50},
172 {"GOLDENROD", 219, 219, 112},
173 {"GREY", 128, 128, 128},
174 {"GREEN", 0, 255, 0},
175 {"GREEN YELLOW", 147, 219, 112},
176 {"INDIAN RED", 79, 47, 47},
177 {"KHAKI", 159, 159, 95},
178 {"LIGHT BLUE", 191, 216, 216},
179 {"LIGHT GREY", 192, 192, 192},
180 {"LIGHT STEEL BLUE", 143, 143, 188},
181 {"LIME GREEN", 50, 204, 50},
182 {"LIGHT MAGENTA", 255, 0, 255},
183 {"MAGENTA", 255, 0, 255},
184 {"MAROON", 142, 35, 107},
185 {"MEDIUM AQUAMARINE", 50, 204, 153},
186 {"MEDIUM GREY", 100, 100, 100},
187 {"MEDIUM BLUE", 50, 50, 204},
188 {"MEDIUM FOREST GREEN", 107, 142, 35},
189 {"MEDIUM GOLDENROD", 234, 234, 173},
190 {"MEDIUM ORCHID", 147, 112, 219},
191 {"MEDIUM SEA GREEN", 66, 111, 66},
192 {"MEDIUM SLATE BLUE", 127, 0, 255},
193 {"MEDIUM SPRING GREEN", 127, 255, 0},
194 {"MEDIUM TURQUOISE", 112, 219, 219},
195 {"MEDIUM VIOLET RED", 219, 112, 147},
196 {"MIDNIGHT BLUE", 47, 47, 79},
197 {"NAVY", 35, 35, 142},
198 {"ORANGE", 204, 50, 50},
199 {"ORANGE RED", 255, 0, 127},
200 {"ORCHID", 219, 112, 219},
201 {"PALE GREEN", 143, 188, 143},
202 {"PINK", 188, 143, 234},
203 {"PLUM", 234, 173, 234},
204 {"PURPLE", 176, 0, 255},
206 {"SALMON", 111, 66, 66},
207 {"SEA GREEN", 35, 142, 107},
208 {"SIENNA", 142, 107, 35},
209 {"SKY BLUE", 50, 153, 204},
210 {"SLATE BLUE", 0, 127, 255},
211 {"SPRING GREEN", 0, 255, 127},
212 {"STEEL BLUE", 35, 107, 142},
213 {"TAN", 219, 147, 112},
214 {"THISTLE", 216, 191, 216},
215 {"TURQUOISE", 173, 234, 234},
216 {"VIOLET", 79, 47, 79},
217 {"VIOLET RED", 204, 50, 153},
218 {"WHEAT", 216, 216, 191},
219 {"WHITE", 255, 255, 255},
220 {"YELLOW", 255, 255, 0},
221 {"YELLOW GREEN", 153, 204, 50},
224 #if defined(__WXGTK__) || defined(__X__)
225 {"MEDIUM GOLDENROD", 234, 234, 173},
226 {"MEDIUM FOREST GREEN", 107, 142, 35},
227 {"LIGHT MAGENTA", 255, 0, 255},
228 {"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 wxNode
*node
= Find((char *) (const char *)colour
);
254 return (wxColour
*)node
->Data();
262 wxColour
*col
= new wxColour( colour
);
268 Append( colour
, col
);
278 Display
*display
= XtDisplay(wxTheApp
->topLevel
) ;
281 Xv_Screen screen
= xv_get(xview_server
, SERVER_NTH_SCREEN
, 0);
282 Xv_opaque root_window
= xv_get(screen
, XV_ROOT
);
283 Display
*display
= (Display
*)xv_get(root_window
, XV_DISPLAY
);
286 /* MATTHEW: [4] Use wxGetMainColormap */
287 if (!XParseColor(display
, wxGetMainColormap(display
), colour
,&xcolour
))
290 unsigned char r
= (unsigned char)(xcolour
.red
>> 8);
291 unsigned char g
= (unsigned char)(xcolour
.green
>> 8);
292 unsigned char b
= (unsigned char)(xcolour
.blue
>> 8);
294 wxColour
*col
= new wxColour(r
, g
, b
);
302 wxString
wxColourDatabase::FindName (const wxColour
& colour
) const
304 unsigned char red
= colour
.Red ();
305 unsigned char green
= colour
.Green ();
306 unsigned char blue
= colour
.Blue ();
308 for (wxNode
* node
= First (); node
; node
= node
->Next ())
310 wxColour
*col
= (wxColour
*) node
->Data ();
312 if (col
->Red () == red
&& col
->Green () == green
&& col
->Blue () == blue
)
314 char *found
= node
->key
.string
;
316 return wxString(found
);
319 return wxString(""); // Not Found
323 void wxInitializeStockLists () {
324 wxTheBrushList
= new wxBrushList
;
325 wxThePenList
= new wxPenList
;
326 wxTheFontList
= new wxFontList
;
327 wxTheBitmapList
= new wxBitmapList
;
330 void wxInitializeStockObjects ()
335 wxFontPool
= new XFontPool
;
338 wxNORMAL_FONT
= new wxFont (12, wxMODERN
, wxNORMAL
, wxNORMAL
);
339 wxSMALL_FONT
= new wxFont (10, wxSWISS
, wxNORMAL
, wxNORMAL
);
340 wxITALIC_FONT
= new wxFont (12, wxROMAN
, wxITALIC
, wxNORMAL
);
341 wxSWISS_FONT
= new wxFont (12, wxSWISS
, wxNORMAL
, wxNORMAL
);
343 wxRED_PEN
= new wxPen ("RED", 1, wxSOLID
);
344 wxCYAN_PEN
= new wxPen ("CYAN", 1, wxSOLID
);
345 wxGREEN_PEN
= new wxPen ("GREEN", 1, wxSOLID
);
346 wxBLACK_PEN
= new wxPen ("BLACK", 1, wxSOLID
);
347 wxWHITE_PEN
= new wxPen ("WHITE", 1, wxSOLID
);
348 wxTRANSPARENT_PEN
= new wxPen ("BLACK", 1, wxTRANSPARENT
);
349 wxBLACK_DASHED_PEN
= new wxPen ("BLACK", 1, wxSHORT_DASH
);
350 wxGREY_PEN
= new wxPen ("GREY", 1, wxSOLID
);
351 wxMEDIUM_GREY_PEN
= new wxPen ("MEDIUM GREY", 1, wxSOLID
);
352 wxLIGHT_GREY_PEN
= new wxPen ("LIGHT GREY", 1, wxSOLID
);
354 wxBLUE_BRUSH
= new wxBrush ("BLUE", wxSOLID
);
355 wxGREEN_BRUSH
= new wxBrush ("GREEN", wxSOLID
);
356 wxWHITE_BRUSH
= new wxBrush ("WHITE", wxSOLID
);
357 wxBLACK_BRUSH
= new wxBrush ("BLACK", wxSOLID
);
358 wxTRANSPARENT_BRUSH
= new wxBrush ("BLACK", wxTRANSPARENT
);
359 wxCYAN_BRUSH
= new wxBrush ("CYAN", wxSOLID
);
360 wxRED_BRUSH
= new wxBrush ("RED", wxSOLID
);
361 wxGREY_BRUSH
= new wxBrush ("GREY", wxSOLID
);
362 wxMEDIUM_GREY_BRUSH
= new wxBrush ("MEDIUM GREY", wxSOLID
);
363 wxLIGHT_GREY_BRUSH
= new wxBrush ("LIGHT GREY", wxSOLID
);
365 wxBLACK
= new wxColour ("BLACK");
366 wxWHITE
= new wxColour ("WHITE");
367 wxRED
= new wxColour ("RED");
368 wxBLUE
= new wxColour ("BLUE");
369 wxGREEN
= new wxColour ("GREEN");
370 wxCYAN
= new wxColour ("CYAN");
371 wxLIGHT_GREY
= new wxColour ("LIGHT GREY");
373 wxSTANDARD_CURSOR
= new wxCursor (wxCURSOR_ARROW
);
374 wxHOURGLASS_CURSOR
= new wxCursor (wxCURSOR_WAIT
);
375 wxCROSS_CURSOR
= new wxCursor (wxCURSOR_CROSS
);
379 wxDeleteStockObjects ()
382 wxDELETE(wxNORMAL_FONT
);
383 wxDELETE(wxSMALL_FONT
);
384 wxDELETE(wxITALIC_FONT
);
385 wxDELETE(wxSWISS_FONT
);
388 wxDELETE(wxCYAN_PEN
);
389 wxDELETE(wxGREEN_PEN
);
390 wxDELETE(wxBLACK_PEN
);
391 wxDELETE(wxWHITE_PEN
);
392 wxDELETE(wxTRANSPARENT_PEN
);
393 wxDELETE(wxBLACK_DASHED_PEN
);
394 wxDELETE(wxGREY_PEN
);
395 wxDELETE(wxMEDIUM_GREY_PEN
);
396 wxDELETE(wxLIGHT_GREY_PEN
);
398 wxDELETE(wxBLUE_BRUSH
);
399 wxDELETE(wxGREEN_BRUSH
);
400 wxDELETE(wxWHITE_BRUSH
);
401 wxDELETE(wxBLACK_BRUSH
);
402 wxDELETE(wxTRANSPARENT_BRUSH
);
403 wxDELETE(wxCYAN_BRUSH
);
404 wxDELETE(wxRED_BRUSH
);
405 wxDELETE(wxGREY_BRUSH
);
406 wxDELETE(wxMEDIUM_GREY_BRUSH
);
407 wxDELETE(wxLIGHT_GREY_BRUSH
);
415 wxDELETE(wxLIGHT_GREY
);
417 wxDELETE(wxSTANDARD_CURSOR
);
418 wxDELETE(wxHOURGLASS_CURSOR
);
419 wxDELETE(wxCROSS_CURSOR
);
422 void wxDeleteStockLists() {
423 wxDELETE(wxTheBrushList
);
424 wxDELETE(wxThePenList
);
425 wxDELETE(wxTheFontList
);
426 wxDELETE(wxTheBitmapList
);
429 wxBitmapList::wxBitmapList ()
433 wxBitmapList::~wxBitmapList ()
435 wxNode
*node
= First ();
438 wxBitmap
*bitmap
= (wxBitmap
*) node
->Data ();
439 wxNode
*next
= node
->Next ();
441 // bitmap->FreeResource(TRUE);
446 // Pen and Brush lists
447 wxPenList::~wxPenList ()
449 wxNode
*node
= First ();
452 wxPen
*pen
= (wxPen
*) node
->Data ();
453 wxNode
*next
= node
->Next ();
455 // pen->FreeResource(TRUE);
460 void wxPenList::AddPen (wxPen
* pen
)
465 void wxPenList::RemovePen (wxPen
* pen
)
470 wxPen
*wxPenList::FindOrCreatePen (const wxColour
& colour
, int width
, int style
)
472 for (wxNode
* node
= First (); node
; node
= node
->Next ())
474 wxPen
*each_pen
= (wxPen
*) node
->Data ();
475 if (each_pen
&& each_pen
->GetVisible() &&
476 each_pen
->GetWidth () == width
&&
477 each_pen
->GetStyle () == style
&&
478 each_pen
->GetColour ().Red () == colour
.Red () &&
479 each_pen
->GetColour ().Green () == colour
.Green () &&
480 each_pen
->GetColour ().Blue () == colour
.Blue ())
483 wxPen
*pen
= new wxPen (colour
, width
, style
);
485 // Yes, we can return a pointer to this in a later FindOrCreatePen call,
486 // because we created it within FindOrCreatePen. Safeguards against
487 // returning a pointer to an automatic variable and hanging on to it
488 // (dangling pointer).
489 pen
->SetVisible(TRUE
);
493 wxPen
*wxPenList::FindOrCreatePen (const wxString
& colour
, int width
, int style
)
495 wxColour
*the_colour
= wxTheColourDatabase
->FindColour (colour
);
497 return FindOrCreatePen (*the_colour
, width
, style
);
502 wxBrushList::~wxBrushList ()
504 wxNode
*node
= First ();
507 wxBrush
*brush
= (wxBrush
*) node
->Data ();
508 wxNode
*next
= node
->Next ();
514 void wxBrushList::AddBrush (wxBrush
* brush
)
519 wxBrush
*wxBrushList::FindOrCreateBrush (const wxColour
& colour
, int style
)
521 for (wxNode
* node
= First (); node
; node
= node
->Next ())
523 wxBrush
*each_brush
= (wxBrush
*) node
->Data ();
524 if (each_brush
&& each_brush
->GetVisible() &&
525 each_brush
->GetStyle () == style
&&
526 each_brush
->GetColour ().Red () == colour
.Red () &&
527 each_brush
->GetColour ().Green () == colour
.Green () &&
528 each_brush
->GetColour ().Blue () == colour
.Blue ())
531 // Yes, we can return a pointer to this in a later FindOrCreateBrush call,
532 // because we created it within FindOrCreateBrush. Safeguards against
533 // returning a pointer to an automatic variable and hanging on to it
534 // (dangling pointer).
535 wxBrush
*brush
= new wxBrush (colour
, style
);
536 brush
->SetVisible(TRUE
);
540 wxBrush
*wxBrushList::FindOrCreateBrush (const wxString
& colour
, int style
)
542 wxColour
*the_colour
= wxTheColourDatabase
->FindColour (colour
);
544 return FindOrCreateBrush (*the_colour
, style
);
549 void wxBrushList::RemoveBrush (wxBrush
* brush
)
551 DeleteObject (brush
);
554 wxFontList::~wxFontList ()
557 wxNode
*node
= First ();
561 wxFont *font = (wxFont *) node->Data ();
562 wxNode *next = node->Next ();
566 // New for 2.0: don't delete the font (it may be a member
567 // of a wxDC, for example)
568 wxFont
*font
= (wxFont
*) node
->Data ();
569 wxNode
*next
= node
->Next ();
571 // Force the font to be deleted
572 font
->FreeResource(TRUE
);
578 void wxFontList::AddFont (wxFont
* font
)
583 void wxFontList::RemoveFont (wxFont
* font
)
589 FindOrCreateFont (int PointSize
, int FamilyOrFontId
, int Style
, int Weight
, bool underline
, const wxString
& Face
)
591 for (wxNode
* node
= First (); node
; node
= node
->Next ())
593 wxFont
*each_font
= (wxFont
*) node
->Data ();
594 if (each_font
&& each_font
->GetVisible() && each_font
->Ok() &&
595 each_font
->GetPointSize () == PointSize
&&
596 each_font
->GetStyle () == Style
&&
597 each_font
->GetWeight () == Weight
&&
598 each_font
->GetUnderlined () == underline
&&
599 #if defined(__X__) || (defined(__WXMSW__) && USE_PORTABLE_FONTS_IN_MSW)
600 each_font
->GetFontId () == FamilyOrFontId
) /* New font system */
602 each_font
->GetFamily () == FamilyOrFontId
&&
603 (!each_font
->GetFaceName() || each_font
->GetFaceName() == Face
))
607 wxFont
*font
= new wxFont (PointSize
, FamilyOrFontId
, Style
, Weight
, underline
, Face
);
608 font
->SetVisible(TRUE
);
612 void wxBitmapList::AddBitmap(wxBitmap
*bitmap
)
614 void wxBitmapList::RemoveBitmap(wxBitmap
*bitmap
)
615 { DeleteObject(bitmap
); }
617 wxSize
wxGetDisplaySize()
620 wxDisplaySize(& x
, & y
);
624 wxResourceCache::wxResourceCache () : wxList() {
627 wxResourceCache::wxResourceCache (const unsigned int the_key_type
) : wxList(the_key_type
) {
630 wxResourceCache::~wxResourceCache () {
631 wxNode
*node
= First ();
633 wxGDIObject
*item
= (wxGDIObject
*)node
->Data();
634 if (item
->IsKindOf(CLASSINFO(wxBrush
))) {
635 wxBrush
*brush
= (wxBrush
*)item
;
639 if (item
->IsKindOf(CLASSINFO(wxFont
))) {
640 wxFont
*font
= (wxFont
*)item
;
644 if (item
->IsKindOf(CLASSINFO(wxBitmap
))) {
645 wxBitmap
*bitmap
= (wxBitmap
*)item
;
649 if (item
->IsKindOf(CLASSINFO(wxColour
))) {
650 wxColour
*colour
= (wxColour
*)item
;
654 wxNode
*next
= node
->Next ();