]>
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();
260 // TODO for other implementations. This should really go into
261 // platform-specific directories.
268 wxColour
*col
= new wxColour( colour
);
274 Append( colour
, col
);
284 Display
*display
= XtDisplay(wxTheApp
->topLevel
) ;
287 Xv_Screen screen
= xv_get(xview_server
, SERVER_NTH_SCREEN
, 0);
288 Xv_opaque root_window
= xv_get(screen
, XV_ROOT
);
289 Display
*display
= (Display
*)xv_get(root_window
, XV_DISPLAY
);
292 /* MATTHEW: [4] Use wxGetMainColormap */
293 if (!XParseColor(display
, wxGetMainColormap(display
), colour
,&xcolour
))
296 unsigned char r
= (unsigned char)(xcolour
.red
>> 8);
297 unsigned char g
= (unsigned char)(xcolour
.green
>> 8);
298 unsigned char b
= (unsigned char)(xcolour
.blue
>> 8);
300 wxColour
*col
= new wxColour(r
, g
, b
);
308 wxString
wxColourDatabase::FindName (const wxColour
& colour
) const
310 unsigned char red
= colour
.Red ();
311 unsigned char green
= colour
.Green ();
312 unsigned char blue
= colour
.Blue ();
314 for (wxNode
* node
= First (); node
; node
= node
->Next ())
316 wxColour
*col
= (wxColour
*) node
->Data ();
318 if (col
->Red () == red
&& col
->Green () == green
&& col
->Blue () == blue
)
320 char *found
= node
->key
.string
;
322 return wxString(found
);
325 return wxString(""); // Not Found
329 void wxInitializeStockLists () {
330 wxTheBrushList
= new wxBrushList
;
331 wxThePenList
= new wxPenList
;
332 wxTheFontList
= new wxFontList
;
333 wxTheBitmapList
= new wxBitmapList
;
336 void wxInitializeStockObjects ()
341 wxFontPool
= new XFontPool
;
344 wxNORMAL_FONT
= new wxFont (12, wxMODERN
, wxNORMAL
, wxNORMAL
);
345 wxSMALL_FONT
= new wxFont (10, wxSWISS
, wxNORMAL
, wxNORMAL
);
346 wxITALIC_FONT
= new wxFont (12, wxROMAN
, wxITALIC
, wxNORMAL
);
347 wxSWISS_FONT
= new wxFont (12, wxSWISS
, wxNORMAL
, wxNORMAL
);
349 wxRED_PEN
= new wxPen ("RED", 1, wxSOLID
);
350 wxCYAN_PEN
= new wxPen ("CYAN", 1, wxSOLID
);
351 wxGREEN_PEN
= new wxPen ("GREEN", 1, wxSOLID
);
352 wxBLACK_PEN
= new wxPen ("BLACK", 1, wxSOLID
);
353 wxWHITE_PEN
= new wxPen ("WHITE", 1, wxSOLID
);
354 wxTRANSPARENT_PEN
= new wxPen ("BLACK", 1, wxTRANSPARENT
);
355 wxBLACK_DASHED_PEN
= new wxPen ("BLACK", 1, wxSHORT_DASH
);
356 wxGREY_PEN
= new wxPen ("GREY", 1, wxSOLID
);
357 wxMEDIUM_GREY_PEN
= new wxPen ("MEDIUM GREY", 1, wxSOLID
);
358 wxLIGHT_GREY_PEN
= new wxPen ("LIGHT GREY", 1, wxSOLID
);
360 wxBLUE_BRUSH
= new wxBrush ("BLUE", wxSOLID
);
361 wxGREEN_BRUSH
= new wxBrush ("GREEN", wxSOLID
);
362 wxWHITE_BRUSH
= new wxBrush ("WHITE", wxSOLID
);
363 wxBLACK_BRUSH
= new wxBrush ("BLACK", wxSOLID
);
364 wxTRANSPARENT_BRUSH
= new wxBrush ("BLACK", wxTRANSPARENT
);
365 wxCYAN_BRUSH
= new wxBrush ("CYAN", wxSOLID
);
366 wxRED_BRUSH
= new wxBrush ("RED", wxSOLID
);
367 wxGREY_BRUSH
= new wxBrush ("GREY", wxSOLID
);
368 wxMEDIUM_GREY_BRUSH
= new wxBrush ("MEDIUM GREY", wxSOLID
);
369 wxLIGHT_GREY_BRUSH
= new wxBrush ("LIGHT GREY", wxSOLID
);
371 wxBLACK
= new wxColour ("BLACK");
372 wxWHITE
= new wxColour ("WHITE");
373 wxRED
= new wxColour ("RED");
374 wxBLUE
= new wxColour ("BLUE");
375 wxGREEN
= new wxColour ("GREEN");
376 wxCYAN
= new wxColour ("CYAN");
377 wxLIGHT_GREY
= new wxColour ("LIGHT GREY");
379 wxSTANDARD_CURSOR
= new wxCursor (wxCURSOR_ARROW
);
380 wxHOURGLASS_CURSOR
= new wxCursor (wxCURSOR_WAIT
);
381 wxCROSS_CURSOR
= new wxCursor (wxCURSOR_CROSS
);
385 wxDeleteStockObjects ()
387 wxDELETE(wxNORMAL_FONT
);
388 wxDELETE(wxSMALL_FONT
);
389 wxDELETE(wxITALIC_FONT
);
390 wxDELETE(wxSWISS_FONT
);
393 wxDELETE(wxCYAN_PEN
);
394 wxDELETE(wxGREEN_PEN
);
395 wxDELETE(wxBLACK_PEN
);
396 wxDELETE(wxWHITE_PEN
);
397 wxDELETE(wxTRANSPARENT_PEN
);
398 wxDELETE(wxBLACK_DASHED_PEN
);
399 wxDELETE(wxGREY_PEN
);
400 wxDELETE(wxMEDIUM_GREY_PEN
);
401 wxDELETE(wxLIGHT_GREY_PEN
);
403 wxDELETE(wxBLUE_BRUSH
);
404 wxDELETE(wxGREEN_BRUSH
);
405 wxDELETE(wxWHITE_BRUSH
);
406 wxDELETE(wxBLACK_BRUSH
);
407 wxDELETE(wxTRANSPARENT_BRUSH
);
408 wxDELETE(wxCYAN_BRUSH
);
409 wxDELETE(wxRED_BRUSH
);
410 wxDELETE(wxGREY_BRUSH
);
411 wxDELETE(wxMEDIUM_GREY_BRUSH
);
412 wxDELETE(wxLIGHT_GREY_BRUSH
);
420 wxDELETE(wxLIGHT_GREY
);
422 wxDELETE(wxSTANDARD_CURSOR
);
423 wxDELETE(wxHOURGLASS_CURSOR
);
424 wxDELETE(wxCROSS_CURSOR
);
427 void wxDeleteStockLists() {
428 wxDELETE(wxTheBrushList
);
429 wxDELETE(wxThePenList
);
430 wxDELETE(wxTheFontList
);
431 wxDELETE(wxTheBitmapList
);
434 wxBitmapList::wxBitmapList ()
438 wxBitmapList::~wxBitmapList ()
440 printf( "Count: %d.\n", Number() );
442 wxNode
*node
= First ();
445 wxBitmap
*bitmap
= (wxBitmap
*) node
->Data ();
446 wxNode
*next
= node
->Next ();
448 // bitmap->FreeResource(TRUE);
453 // Pen and Brush lists
454 wxPenList::~wxPenList ()
456 wxNode
*node
= First ();
459 wxPen
*pen
= (wxPen
*) node
->Data ();
460 wxNode
*next
= node
->Next ();
462 // pen->FreeResource(TRUE);
467 void wxPenList::AddPen (wxPen
* pen
)
472 void wxPenList::RemovePen (wxPen
* pen
)
477 wxPen
*wxPenList::FindOrCreatePen (const wxColour
& colour
, int width
, int style
)
479 for (wxNode
* node
= First (); node
; node
= node
->Next ())
481 wxPen
*each_pen
= (wxPen
*) node
->Data ();
482 if (each_pen
&& each_pen
->GetVisible() &&
483 each_pen
->GetWidth () == width
&&
484 each_pen
->GetStyle () == style
&&
485 each_pen
->GetColour ().Red () == colour
.Red () &&
486 each_pen
->GetColour ().Green () == colour
.Green () &&
487 each_pen
->GetColour ().Blue () == colour
.Blue ())
490 wxPen
*pen
= new wxPen (colour
, width
, style
);
492 // Yes, we can return a pointer to this in a later FindOrCreatePen call,
493 // because we created it within FindOrCreatePen. Safeguards against
494 // returning a pointer to an automatic variable and hanging on to it
495 // (dangling pointer).
496 pen
->SetVisible(TRUE
);
500 wxPen
*wxPenList::FindOrCreatePen (const wxString
& colour
, int width
, int style
)
502 wxColour
*the_colour
= wxTheColourDatabase
->FindColour (colour
);
504 return FindOrCreatePen (*the_colour
, width
, style
);
509 wxBrushList::~wxBrushList ()
511 wxNode
*node
= First ();
514 wxBrush
*brush
= (wxBrush
*) node
->Data ();
515 wxNode
*next
= node
->Next ();
521 void wxBrushList::AddBrush (wxBrush
* brush
)
526 wxBrush
*wxBrushList::FindOrCreateBrush (const wxColour
& colour
, int style
)
528 for (wxNode
* node
= First (); node
; node
= node
->Next ())
530 wxBrush
*each_brush
= (wxBrush
*) node
->Data ();
531 if (each_brush
&& each_brush
->GetVisible() &&
532 each_brush
->GetStyle () == style
&&
533 each_brush
->GetColour ().Red () == colour
.Red () &&
534 each_brush
->GetColour ().Green () == colour
.Green () &&
535 each_brush
->GetColour ().Blue () == colour
.Blue ())
538 // Yes, we can return a pointer to this in a later FindOrCreateBrush call,
539 // because we created it within FindOrCreateBrush. Safeguards against
540 // returning a pointer to an automatic variable and hanging on to it
541 // (dangling pointer).
542 wxBrush
*brush
= new wxBrush (colour
, style
);
543 brush
->SetVisible(TRUE
);
547 wxBrush
*wxBrushList::FindOrCreateBrush (const wxString
& colour
, int style
)
549 wxColour
*the_colour
= wxTheColourDatabase
->FindColour (colour
);
551 return FindOrCreateBrush (*the_colour
, style
);
556 void wxBrushList::RemoveBrush (wxBrush
* brush
)
558 DeleteObject (brush
);
561 wxFontList::~wxFontList ()
564 wxNode
*node
= First ();
568 wxFont *font = (wxFont *) node->Data ();
569 wxNode *next = node->Next ();
573 // New for 2.0: don't delete the font (it may be a member
574 // of a wxDC, for example)
575 wxFont
*font
= (wxFont
*) node
->Data ();
576 wxNode
*next
= node
->Next ();
578 // Force the font to be deleted
579 font
->FreeResource(TRUE
);
585 void wxFontList::AddFont (wxFont
* font
)
590 void wxFontList::RemoveFont (wxFont
* font
)
596 FindOrCreateFont (int PointSize
, int FamilyOrFontId
, int Style
, int Weight
, bool underline
, const wxString
& Face
)
598 for (wxNode
* node
= First (); node
; node
= node
->Next ())
600 wxFont
*each_font
= (wxFont
*) node
->Data ();
601 if (each_font
&& each_font
->GetVisible() && each_font
->Ok() &&
602 each_font
->GetPointSize () == PointSize
&&
603 each_font
->GetStyle () == Style
&&
604 each_font
->GetWeight () == Weight
&&
605 each_font
->GetUnderlined () == underline
&&
606 #if defined(__X__) || (defined(__WXMSW__) && USE_PORTABLE_FONTS_IN_MSW)
607 each_font
->GetFontId () == FamilyOrFontId
) /* New font system */
609 each_font
->GetFamily () == FamilyOrFontId
&&
610 (!each_font
->GetFaceName() || each_font
->GetFaceName() == Face
))
614 wxFont
*font
= new wxFont (PointSize
, FamilyOrFontId
, Style
, Weight
, underline
, Face
);
615 font
->SetVisible(TRUE
);
619 void wxBitmapList::AddBitmap(wxBitmap
*bitmap
)
621 void wxBitmapList::RemoveBitmap(wxBitmap
*bitmap
)
622 { DeleteObject(bitmap
); }
624 wxSize
wxGetDisplaySize()
627 wxDisplaySize(& x
, & y
);
631 wxResourceCache::wxResourceCache () : wxList() {
634 wxResourceCache::wxResourceCache (const unsigned int the_key_type
) : wxList(the_key_type
) {
637 wxResourceCache::~wxResourceCache () {
638 wxNode
*node
= First ();
640 wxGDIObject
*item
= (wxGDIObject
*)node
->Data();
641 if (item
->IsKindOf(CLASSINFO(wxBrush
))) {
642 wxBrush
*brush
= (wxBrush
*)item
;
646 if (item
->IsKindOf(CLASSINFO(wxFont
))) {
647 wxFont
*font
= (wxFont
*)item
;
651 if (item
->IsKindOf(CLASSINFO(wxBitmap
))) {
652 wxBitmap
*bitmap
= (wxBitmap
*)item
;
656 if (item
->IsKindOf(CLASSINFO(wxColour
))) {
657 wxColour
*colour
= (wxColour
*)item
;
661 wxNode
*next
= node
->Next ();