]>
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"
40 #if !USE_SHARED_LIBRARY
41 IMPLEMENT_CLASS(wxColourDatabase
, wxList
)
42 IMPLEMENT_DYNAMIC_CLASS(wxFontList
, wxList
)
43 IMPLEMENT_DYNAMIC_CLASS(wxPenList
, wxList
)
44 IMPLEMENT_DYNAMIC_CLASS(wxBrushList
, wxList
)
45 IMPLEMENT_DYNAMIC_CLASS(wxBitmapList
, wxList
)
46 IMPLEMENT_DYNAMIC_CLASS(wxResourceCache
, wxList
)
48 IMPLEMENT_DYNAMIC_CLASS(wxRect, wxObject)
49 IMPLEMENT_DYNAMIC_CLASS(wxPoint, wxObject)
50 IMPLEMENT_DYNAMIC_CLASS(wxRealPoint, wxObject)
56 x
= 0; y
= 0; width
= 0; height
= 0;
59 wxRect::wxRect(long xx
, long yy
, long w
, long h
)
61 x
= xx
; y
= yy
; width
= w
; height
= h
;
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 wxRect::wxRect(const wxRect
& rect
)
98 wxRect
& wxRect::operator = (const wxRect
& rect
)
100 x
= rect
.x
; y
= rect
.y
; width
= rect
.width
; height
= rect
.height
;
104 bool wxRect::operator == (const wxRect
& rect
)
106 return ((x
== rect
.x
) &&
108 (width
== rect
.width
) &&
109 (height
== rect
.height
));
112 bool wxRect::operator != (const wxRect
& rect
)
114 return ((x
!= rect
.x
) ||
116 (width
!= rect
.width
) ||
117 (height
!= rect
.height
));
120 wxColourDatabase::wxColourDatabase (int 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 {"AQUAMARINE",112, 219, 147},
156 {"BLUE VIOLET", 159, 95, 159},
157 {"BROWN", 165, 42, 42},
158 {"CADET BLUE", 95, 159, 159},
159 {"CORAL", 255, 127, 0},
160 {"CORNFLOWER BLUE", 66, 66, 111},
161 {"CYAN", 0, 255, 255},
162 {"DARK GREY", 47, 47, 47}, // ?
164 {"DARK GREEN", 47, 79, 47},
165 {"DARK OLIVE GREEN", 79, 79, 47},
166 {"DARK ORCHID", 153, 50, 204},
167 {"DARK SLATE BLUE", 107, 35, 142},
168 {"DARK SLATE GREY", 47, 79, 79},
169 {"DARK TURQUOISE", 112, 147, 219},
170 {"DIM GREY", 84, 84, 84},
171 {"FIREBRICK", 142, 35, 35},
172 {"FOREST GREEN", 35, 142, 35},
173 {"GOLD", 204, 127, 50},
174 {"GOLDENROD", 219, 219, 112},
175 {"GREY", 128, 128, 128},
176 {"GREEN", 0, 255, 0},
177 {"GREEN YELLOW", 147, 219, 112},
178 {"INDIAN RED", 79, 47, 47},
179 {"KHAKI", 159, 159, 95},
180 {"LIGHT BLUE", 191, 216, 216},
181 {"LIGHT GREY", 192, 192, 192},
182 {"LIGHT STEEL BLUE", 143, 143, 188},
183 {"LIME GREEN", 50, 204, 50},
184 {"LIGHT MAGENTA", 255, 0, 255},
185 {"MAGENTA", 255, 0, 255},
186 {"MAROON", 142, 35, 107},
187 {"MEDIUM AQUAMARINE", 50, 204, 153},
188 {"MEDIUM GREY", 100, 100, 100},
189 {"MEDIUM BLUE", 50, 50, 204},
190 {"MEDIUM FOREST GREEN", 107, 142, 35},
191 {"MEDIUM GOLDENROD", 234, 234, 173},
192 {"MEDIUM ORCHID", 147, 112, 219},
193 {"MEDIUM SEA GREEN", 66, 111, 66},
194 {"MEDIUM SLATE BLUE", 127, 0, 255},
195 {"MEDIUM SPRING GREEN", 127, 255, 0},
196 {"MEDIUM TURQUOISE", 112, 219, 219},
197 {"MEDIUM VIOLET RED", 219, 112, 147},
198 {"MIDNIGHT BLUE", 47, 47, 79},
199 {"NAVY", 35, 35, 142},
200 {"ORANGE", 204, 50, 50},
201 {"ORANGE RED", 255, 0, 127},
202 {"ORCHID", 219, 112, 219},
203 {"PALE GREEN", 143, 188, 143},
204 {"PINK", 188, 143, 234},
205 {"PLUM", 234, 173, 234},
206 {"PURPLE", 176, 0, 255},
208 {"SALMON", 111, 66, 66},
209 {"SEA GREEN", 35, 142, 107},
210 {"SIENNA", 142, 107, 35},
211 {"SKY BLUE", 50, 153, 204},
212 {"SLATE BLUE", 0, 127, 255},
213 {"SPRING GREEN", 0, 255, 127},
214 {"STEEL BLUE", 35, 107, 142},
215 {"TAN", 219, 147, 112},
216 {"THISTLE", 216, 191, 216},
217 {"TURQUOISE", 173, 234, 234},
218 {"VIOLET", 79, 47, 79},
219 {"VIOLET RED", 204, 50, 153},
220 {"WHEAT", 216, 216, 191},
221 {"WHITE", 255, 255, 255},
222 {"YELLOW", 255, 255, 0},
223 {"YELLOW GREEN", 153, 204, 50},
226 #if defined(__WXGTK__) || defined(__X__)
227 {"MEDIUM GOLDENROD", 234, 234, 173},
228 {"MEDIUM FOREST GREEN", 107, 142, 35},
229 {"LIGHT MAGENTA", 255, 0, 255},
230 {"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 wxNode
*node
= Find((char *) (const char *)colour
);
256 return (wxColour
*)node
->Data();
262 // TODO for other implementations. This should really go into
263 // platform-specific directories.
270 wxColour
*col
= new wxColour( colour
);
274 return (wxColour
*) NULL
;
276 Append( colour
, col
);
286 Display
*display
= XtDisplay(wxTheApp
->topLevel
) ;
289 Xv_Screen screen
= xv_get(xview_server
, SERVER_NTH_SCREEN
, 0);
290 Xv_opaque root_window
= xv_get(screen
, XV_ROOT
);
291 Display
*display
= (Display
*)xv_get(root_window
, XV_DISPLAY
);
294 /* MATTHEW: [4] Use wxGetMainColormap */
295 if (!XParseColor(display
, wxGetMainColormap(display
), colour
,&xcolour
))
298 unsigned char r
= (unsigned char)(xcolour
.red
>> 8);
299 unsigned char g
= (unsigned char)(xcolour
.green
>> 8);
300 unsigned char b
= (unsigned char)(xcolour
.blue
>> 8);
302 wxColour
*col
= new wxColour(r
, g
, b
);
310 wxString
wxColourDatabase::FindName (const wxColour
& colour
) const
312 unsigned char red
= colour
.Red ();
313 unsigned char green
= colour
.Green ();
314 unsigned char blue
= colour
.Blue ();
316 for (wxNode
* node
= First (); node
; node
= node
->Next ())
318 wxColour
*col
= (wxColour
*) node
->Data ();
320 if (col
->Red () == red
&& col
->Green () == green
&& col
->Blue () == blue
)
322 char *found
= node
->key
.string
;
324 return wxString(found
);
327 return wxString(""); // Not Found
331 void wxInitializeStockLists () {
332 wxTheBrushList
= new wxBrushList
;
333 wxThePenList
= new wxPenList
;
334 wxTheFontList
= new wxFontList
;
335 wxTheBitmapList
= new wxBitmapList
;
338 void wxInitializeStockObjects ()
343 wxFontPool
= new XFontPool
;
346 wxNORMAL_FONT
= new wxFont (12, wxMODERN
, wxNORMAL
, wxNORMAL
);
347 wxSMALL_FONT
= new wxFont (10, wxSWISS
, wxNORMAL
, wxNORMAL
);
348 wxITALIC_FONT
= new wxFont (12, wxROMAN
, wxITALIC
, wxNORMAL
);
349 wxSWISS_FONT
= new wxFont (12, wxSWISS
, wxNORMAL
, wxNORMAL
);
351 wxRED_PEN
= new wxPen ("RED", 1, wxSOLID
);
352 wxCYAN_PEN
= new wxPen ("CYAN", 1, wxSOLID
);
353 wxGREEN_PEN
= new wxPen ("GREEN", 1, wxSOLID
);
354 wxBLACK_PEN
= new wxPen ("BLACK", 1, wxSOLID
);
355 wxWHITE_PEN
= new wxPen ("WHITE", 1, wxSOLID
);
356 wxTRANSPARENT_PEN
= new wxPen ("BLACK", 1, wxTRANSPARENT
);
357 wxBLACK_DASHED_PEN
= new wxPen ("BLACK", 1, wxSHORT_DASH
);
358 wxGREY_PEN
= new wxPen ("GREY", 1, wxSOLID
);
359 wxMEDIUM_GREY_PEN
= new wxPen ("MEDIUM GREY", 1, wxSOLID
);
360 wxLIGHT_GREY_PEN
= new wxPen ("LIGHT GREY", 1, wxSOLID
);
362 wxBLUE_BRUSH
= new wxBrush ("BLUE", wxSOLID
);
363 wxGREEN_BRUSH
= new wxBrush ("GREEN", wxSOLID
);
364 wxWHITE_BRUSH
= new wxBrush ("WHITE", wxSOLID
);
365 wxBLACK_BRUSH
= new wxBrush ("BLACK", wxSOLID
);
366 wxTRANSPARENT_BRUSH
= new wxBrush ("BLACK", wxTRANSPARENT
);
367 wxCYAN_BRUSH
= new wxBrush ("CYAN", wxSOLID
);
368 wxRED_BRUSH
= new wxBrush ("RED", wxSOLID
);
369 wxGREY_BRUSH
= new wxBrush ("GREY", wxSOLID
);
370 wxMEDIUM_GREY_BRUSH
= new wxBrush ("MEDIUM GREY", wxSOLID
);
371 wxLIGHT_GREY_BRUSH
= new wxBrush ("LIGHT GREY", wxSOLID
);
373 wxBLACK
= new wxColour ("BLACK");
374 wxWHITE
= new wxColour ("WHITE");
375 wxRED
= new wxColour ("RED");
376 wxBLUE
= new wxColour ("BLUE");
377 wxGREEN
= new wxColour ("GREEN");
378 wxCYAN
= new wxColour ("CYAN");
379 wxLIGHT_GREY
= new wxColour ("LIGHT GREY");
381 wxSTANDARD_CURSOR
= new wxCursor (wxCURSOR_ARROW
);
382 wxHOURGLASS_CURSOR
= new wxCursor (wxCURSOR_WAIT
);
383 wxCROSS_CURSOR
= new wxCursor (wxCURSOR_CROSS
);
387 wxDeleteStockObjects ()
389 wxDELETE(wxNORMAL_FONT
);
390 wxDELETE(wxSMALL_FONT
);
391 wxDELETE(wxITALIC_FONT
);
392 wxDELETE(wxSWISS_FONT
);
395 wxDELETE(wxCYAN_PEN
);
396 wxDELETE(wxGREEN_PEN
);
397 wxDELETE(wxBLACK_PEN
);
398 wxDELETE(wxWHITE_PEN
);
399 wxDELETE(wxTRANSPARENT_PEN
);
400 wxDELETE(wxBLACK_DASHED_PEN
);
401 wxDELETE(wxGREY_PEN
);
402 wxDELETE(wxMEDIUM_GREY_PEN
);
403 wxDELETE(wxLIGHT_GREY_PEN
);
405 wxDELETE(wxBLUE_BRUSH
);
406 wxDELETE(wxGREEN_BRUSH
);
407 wxDELETE(wxWHITE_BRUSH
);
408 wxDELETE(wxBLACK_BRUSH
);
409 wxDELETE(wxTRANSPARENT_BRUSH
);
410 wxDELETE(wxCYAN_BRUSH
);
411 wxDELETE(wxRED_BRUSH
);
412 wxDELETE(wxGREY_BRUSH
);
413 wxDELETE(wxMEDIUM_GREY_BRUSH
);
414 wxDELETE(wxLIGHT_GREY_BRUSH
);
422 wxDELETE(wxLIGHT_GREY
);
424 wxDELETE(wxSTANDARD_CURSOR
);
425 wxDELETE(wxHOURGLASS_CURSOR
);
426 wxDELETE(wxCROSS_CURSOR
);
429 void wxDeleteStockLists() {
430 wxDELETE(wxTheBrushList
);
431 wxDELETE(wxThePenList
);
432 wxDELETE(wxTheFontList
);
433 wxDELETE(wxTheBitmapList
);
436 wxBitmapList::wxBitmapList ()
440 wxBitmapList::~wxBitmapList ()
442 wxLogDebug("~wxBitmapList: count = %d", Number());
445 wxNode
*node
= First ();
448 wxBitmap
*bitmap
= (wxBitmap
*) node
->Data ();
449 wxNode
*next
= node
->Next ();
450 if (bitmap
->GetVisible())
457 // Pen and Brush lists
458 wxPenList::~wxPenList ()
461 wxNode
*node
= First ();
464 wxPen
*pen
= (wxPen
*) node
->Data ();
465 wxNode
*next
= node
->Next ();
466 if (pen
->GetVisible())
473 void wxPenList::AddPen (wxPen
* pen
)
478 void wxPenList::RemovePen (wxPen
* pen
)
483 wxPen
*wxPenList::FindOrCreatePen (const wxColour
& colour
, int width
, int style
)
485 for (wxNode
* node
= First (); node
; node
= node
->Next ())
487 wxPen
*each_pen
= (wxPen
*) node
->Data ();
488 if (each_pen
&& each_pen
->GetVisible() &&
489 each_pen
->GetWidth () == width
&&
490 each_pen
->GetStyle () == style
&&
491 each_pen
->GetColour ().Red () == colour
.Red () &&
492 each_pen
->GetColour ().Green () == colour
.Green () &&
493 each_pen
->GetColour ().Blue () == colour
.Blue ())
496 wxPen
*pen
= new wxPen (colour
, width
, style
);
498 // Yes, we can return a pointer to this in a later FindOrCreatePen call,
499 // because we created it within FindOrCreatePen. Safeguards against
500 // returning a pointer to an automatic variable and hanging on to it
501 // (dangling pointer).
502 pen
->SetVisible(TRUE
);
506 wxPen
*wxPenList::FindOrCreatePen (const wxString
& colour
, int width
, int style
)
508 wxColour
*the_colour
= wxTheColourDatabase
->FindColour (colour
);
510 return FindOrCreatePen (*the_colour
, width
, style
);
512 return (wxPen
*) NULL
;
515 wxBrushList::~wxBrushList ()
518 wxNode
*node
= First ();
521 wxBrush
*brush
= (wxBrush
*) node
->Data ();
522 wxNode
*next
= node
->Next ();
523 if (brush
->GetVisible())
530 void wxBrushList::AddBrush (wxBrush
* brush
)
535 wxBrush
*wxBrushList::FindOrCreateBrush (const wxColour
& colour
, int style
)
537 for (wxNode
* node
= First (); node
; node
= node
->Next ())
539 wxBrush
*each_brush
= (wxBrush
*) node
->Data ();
540 if (each_brush
&& each_brush
->GetVisible() &&
541 each_brush
->GetStyle () == style
&&
542 each_brush
->GetColour ().Red () == colour
.Red () &&
543 each_brush
->GetColour ().Green () == colour
.Green () &&
544 each_brush
->GetColour ().Blue () == colour
.Blue ())
547 // Yes, we can return a pointer to this in a later FindOrCreateBrush call,
548 // because we created it within FindOrCreateBrush. Safeguards against
549 // returning a pointer to an automatic variable and hanging on to it
550 // (dangling pointer).
551 wxBrush
*brush
= new wxBrush (colour
, style
);
552 brush
->SetVisible(TRUE
);
556 wxBrush
*wxBrushList::FindOrCreateBrush (const wxString
& colour
, int style
)
558 wxColour
*the_colour
= wxTheColourDatabase
->FindColour (colour
);
560 return FindOrCreateBrush (*the_colour
, style
);
562 return (wxBrush
*) NULL
;
565 void wxBrushList::RemoveBrush (wxBrush
* brush
)
567 DeleteObject (brush
);
570 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())
589 void wxFontList::AddFont (wxFont
* font
)
594 void wxFontList::RemoveFont (wxFont
* font
)
600 FindOrCreateFont (int PointSize
, int FamilyOrFontId
, int Style
, int Weight
, bool underline
, const wxString
& Face
)
602 for (wxNode
* node
= First (); node
; node
= node
->Next ())
604 wxFont
*each_font
= (wxFont
*) node
->Data ();
605 if (each_font
&& each_font
->GetVisible() && each_font
->Ok() &&
606 each_font
->GetPointSize () == PointSize
&&
607 each_font
->GetStyle () == Style
&&
608 each_font
->GetWeight () == Weight
&&
609 each_font
->GetUnderlined () == underline
&&
610 #if defined(__X__) || (defined(__WXMSW__) && USE_PORTABLE_FONTS_IN_MSW)
611 each_font
->GetFontId () == FamilyOrFontId
) /* New font system */
613 each_font
->GetFamily () == FamilyOrFontId
&&
614 (!each_font
->GetFaceName() || each_font
->GetFaceName() == Face
))
618 wxFont
*font
= new wxFont (PointSize
, FamilyOrFontId
, Style
, Weight
, underline
, Face
);
619 font
->SetVisible(TRUE
);
623 void wxBitmapList::AddBitmap(wxBitmap
*bitmap
)
625 void wxBitmapList::RemoveBitmap(wxBitmap
*bitmap
)
626 { DeleteObject(bitmap
); }
628 wxSize
wxGetDisplaySize()
631 wxDisplaySize(& x
, & y
);
635 wxResourceCache::wxResourceCache () : wxList() {
638 wxResourceCache::wxResourceCache (const unsigned int the_key_type
) : wxList(the_key_type
) {
641 wxResourceCache::~wxResourceCache () {
642 wxNode
*node
= First ();
644 wxGDIObject
*item
= (wxGDIObject
*)node
->Data();
645 if (item
->IsKindOf(CLASSINFO(wxBrush
))) {
646 wxBrush
*brush
= (wxBrush
*)item
;
650 if (item
->IsKindOf(CLASSINFO(wxFont
))) {
651 wxFont
*font
= (wxFont
*)item
;
655 if (item
->IsKindOf(CLASSINFO(wxBitmap
))) {
656 wxBitmap
*bitmap
= (wxBitmap
*)item
;
660 if (item
->IsKindOf(CLASSINFO(wxColour
))) {
661 wxColour
*colour
= (wxColour
*)item
;
665 wxNode
*next
= node
->Next ();