]>
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 #if !USE_SHARED_LIBRARY
46 IMPLEMENT_CLASS(wxColourDatabase
, wxList
)
47 IMPLEMENT_DYNAMIC_CLASS(wxFontList
, wxList
)
48 IMPLEMENT_DYNAMIC_CLASS(wxPenList
, wxList
)
49 IMPLEMENT_DYNAMIC_CLASS(wxBrushList
, wxList
)
50 IMPLEMENT_DYNAMIC_CLASS(wxBitmapList
, wxList
)
51 IMPLEMENT_DYNAMIC_CLASS(wxResourceCache
, wxList
)
53 IMPLEMENT_DYNAMIC_CLASS(wxRect, wxObject)
54 IMPLEMENT_DYNAMIC_CLASS(wxPoint, wxObject)
55 IMPLEMENT_DYNAMIC_CLASS(wxRealPoint, wxObject)
61 x
= 0; y
= 0; width
= 0; height
= 0;
64 wxRect::wxRect(long xx
, long yy
, long w
, long h
)
66 x
= xx
; y
= yy
; width
= w
; height
= h
;
69 wxRect::wxRect(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
73 width
= bottomRight
.x
- topLeft
.x
;
74 height
= bottomRight
.y
- topLeft
.y
;
89 wxRect::wxRect(const wxPoint
& point
, const wxSize
& size
)
91 x
= point
.x
; y
= point
.y
;
92 width
= size
.x
; height
= size
.y
;
95 wxRect::wxRect(const wxRect
& rect
)
100 height
= rect
.height
;
103 wxRect
& wxRect::operator = (const wxRect
& rect
)
105 x
= rect
.x
; y
= rect
.y
; width
= rect
.width
; height
= rect
.height
;
109 bool wxRect::operator == (const wxRect
& rect
)
111 return ((x
== rect
.x
) &&
113 (width
== rect
.width
) &&
114 (height
== rect
.height
));
117 bool wxRect::operator != (const wxRect
& rect
)
119 return ((x
!= rect
.x
) ||
121 (width
!= rect
.width
) ||
122 (height
!= rect
.height
));
125 wxColourDatabase::wxColourDatabase (int type
):
130 wxColourDatabase::~wxColourDatabase ()
132 // Cleanup Colour allocated in Initialize()
133 wxNode
*node
= First ();
136 wxColour
*col
= (wxColour
*) node
->Data ();
137 wxNode
*next
= node
->Next ();
143 // Colour database stuff
144 void wxColourDatabase::Initialize ()
146 // Don't initialize for X: colours are found
147 // in FindColour below.
155 static cdef table
[]={
158 {"AQUAMARINE",112, 219, 147},
161 {"BLUE VIOLET", 159, 95, 159},
162 {"BROWN", 165, 42, 42},
163 {"CADET BLUE", 95, 159, 159},
164 {"CORAL", 255, 127, 0},
165 {"CORNFLOWER BLUE", 66, 66, 111},
166 {"CYAN", 0, 255, 255},
167 {"DARK GREY", 47, 47, 47}, // ?
169 {"DARK GREEN", 47, 79, 47},
170 {"DARK OLIVE GREEN", 79, 79, 47},
171 {"DARK ORCHID", 153, 50, 204},
172 {"DARK SLATE BLUE", 107, 35, 142},
173 {"DARK SLATE GREY", 47, 79, 79},
174 {"DARK TURQUOISE", 112, 147, 219},
175 {"DIM GREY", 84, 84, 84},
176 {"FIREBRICK", 142, 35, 35},
177 {"FOREST GREEN", 35, 142, 35},
178 {"GOLD", 204, 127, 50},
179 {"GOLDENROD", 219, 219, 112},
180 {"GREY", 128, 128, 128},
181 {"GREEN", 0, 255, 0},
182 {"GREEN YELLOW", 147, 219, 112},
183 {"INDIAN RED", 79, 47, 47},
184 {"KHAKI", 159, 159, 95},
185 {"LIGHT BLUE", 191, 216, 216},
186 {"LIGHT GREY", 192, 192, 192},
187 {"LIGHT STEEL BLUE", 143, 143, 188},
188 {"LIME GREEN", 50, 204, 50},
189 {"LIGHT MAGENTA", 255, 0, 255},
190 {"MAGENTA", 255, 0, 255},
191 {"MAROON", 142, 35, 107},
192 {"MEDIUM AQUAMARINE", 50, 204, 153},
193 {"MEDIUM GREY", 100, 100, 100},
194 {"MEDIUM BLUE", 50, 50, 204},
195 {"MEDIUM FOREST GREEN", 107, 142, 35},
196 {"MEDIUM GOLDENROD", 234, 234, 173},
197 {"MEDIUM ORCHID", 147, 112, 219},
198 {"MEDIUM SEA GREEN", 66, 111, 66},
199 {"MEDIUM SLATE BLUE", 127, 0, 255},
200 {"MEDIUM SPRING GREEN", 127, 255, 0},
201 {"MEDIUM TURQUOISE", 112, 219, 219},
202 {"MEDIUM VIOLET RED", 219, 112, 147},
203 {"MIDNIGHT BLUE", 47, 47, 79},
204 {"NAVY", 35, 35, 142},
205 {"ORANGE", 204, 50, 50},
206 {"ORANGE RED", 255, 0, 127},
207 {"ORCHID", 219, 112, 219},
208 {"PALE GREEN", 143, 188, 143},
209 {"PINK", 188, 143, 234},
210 {"PLUM", 234, 173, 234},
211 {"PURPLE", 176, 0, 255},
213 {"SALMON", 111, 66, 66},
214 {"SEA GREEN", 35, 142, 107},
215 {"SIENNA", 142, 107, 35},
216 {"SKY BLUE", 50, 153, 204},
217 {"SLATE BLUE", 0, 127, 255},
218 {"SPRING GREEN", 0, 255, 127},
219 {"STEEL BLUE", 35, 107, 142},
220 {"TAN", 219, 147, 112},
221 {"THISTLE", 216, 191, 216},
222 {"TURQUOISE", 173, 234, 234},
223 {"VIOLET", 79, 47, 79},
224 {"VIOLET RED", 204, 50, 153},
225 {"WHEAT", 216, 216, 191},
226 {"WHITE", 255, 255, 255},
227 {"YELLOW", 255, 255, 0},
228 {"YELLOW GREEN", 153, 204, 50},
231 #if defined(__WXGTK__) || defined(__X__)
232 {"MEDIUM GOLDENROD", 234, 234, 173},
233 {"MEDIUM FOREST GREEN", 107, 142, 35},
234 {"LIGHT MAGENTA", 255, 0, 255},
235 {"MEDIUM GREY", 100, 100, 100},
241 for (i
=0;cc
=table
[i
],cc
.name
!=0;i
++)
243 Append(cc
.name
,new wxColour(cc
.r
,cc
.g
,cc
.b
));
249 * Changed by Ian Brown, July 1994.
251 * When running under X, the Colour Database starts off empty. The X server
252 * is queried for the colour first time after which it is entered into the
253 * database. This allows our client to use the server colour database which
254 * is hopefully gamma corrected for the display being used.
257 wxColour
*wxColourDatabase::FindColour(const wxString
& colour
)
259 // VZ: make the comparaison case insensitive
260 wxString str
= colour
;
263 wxNode
*node
= Find(str
);
265 return (wxColour
*)node
->Data();
271 // TODO for other implementations. This should really go into
272 // platform-specific directories.
279 wxColour
*col
= new wxColour( colour
);
283 return (wxColour
*) NULL
;
285 Append( colour
, col
);
295 Display
*display
= XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()) ;
298 Xv_Screen screen
= xv_get(xview_server
, SERVER_NTH_SCREEN
, 0);
299 Xv_opaque root_window
= xv_get(screen
, XV_ROOT
);
300 Display
*display
= (Display
*)xv_get(root_window
, XV_DISPLAY
);
303 /* MATTHEW: [4] Use wxGetMainColormap */
304 if (!XParseColor(display
, (Colormap
) wxTheApp
->GetMainColormap((WXDisplay
*) display
), colour
,&xcolour
))
307 unsigned char r
= (unsigned char)(xcolour
.red
>> 8);
308 unsigned char g
= (unsigned char)(xcolour
.green
>> 8);
309 unsigned char b
= (unsigned char)(xcolour
.blue
>> 8);
311 wxColour
*col
= new wxColour(r
, g
, b
);
319 wxString
wxColourDatabase::FindName (const wxColour
& colour
) const
321 unsigned char red
= colour
.Red ();
322 unsigned char green
= colour
.Green ();
323 unsigned char blue
= colour
.Blue ();
325 for (wxNode
* node
= First (); node
; node
= node
->Next ())
327 wxColour
*col
= (wxColour
*) node
->Data ();
329 if (col
->Red () == red
&& col
->Green () == green
&& col
->Blue () == blue
)
331 const char *found
= node
->GetKeyString();
333 return wxString(found
);
336 return wxString(""); // Not Found
340 void wxInitializeStockLists () {
341 wxTheBrushList
= new wxBrushList
;
342 wxThePenList
= new wxPenList
;
343 wxTheFontList
= new wxFontList
;
344 wxTheBitmapList
= new wxBitmapList
;
347 void wxInitializeStockObjects ()
353 // wxFontPool = new XFontPool;
356 wxNORMAL_FONT
= new wxFont (12, wxMODERN
, wxNORMAL
, wxNORMAL
);
357 wxSMALL_FONT
= new wxFont (10, wxSWISS
, wxNORMAL
, wxNORMAL
);
358 wxITALIC_FONT
= new wxFont (12, wxROMAN
, wxITALIC
, wxNORMAL
);
359 wxSWISS_FONT
= new wxFont (12, wxSWISS
, wxNORMAL
, wxNORMAL
);
361 wxRED_PEN
= new wxPen ("RED", 1, wxSOLID
);
362 wxCYAN_PEN
= new wxPen ("CYAN", 1, wxSOLID
);
363 wxGREEN_PEN
= new wxPen ("GREEN", 1, wxSOLID
);
364 wxBLACK_PEN
= new wxPen ("BLACK", 1, wxSOLID
);
365 wxWHITE_PEN
= new wxPen ("WHITE", 1, wxSOLID
);
366 wxTRANSPARENT_PEN
= new wxPen ("BLACK", 1, wxTRANSPARENT
);
367 wxBLACK_DASHED_PEN
= new wxPen ("BLACK", 1, wxSHORT_DASH
);
368 wxGREY_PEN
= new wxPen ("GREY", 1, wxSOLID
);
369 wxMEDIUM_GREY_PEN
= new wxPen ("MEDIUM GREY", 1, wxSOLID
);
370 wxLIGHT_GREY_PEN
= new wxPen ("LIGHT GREY", 1, wxSOLID
);
372 wxBLUE_BRUSH
= new wxBrush ("BLUE", wxSOLID
);
373 wxGREEN_BRUSH
= new wxBrush ("GREEN", wxSOLID
);
374 wxWHITE_BRUSH
= new wxBrush ("WHITE", wxSOLID
);
375 wxBLACK_BRUSH
= new wxBrush ("BLACK", wxSOLID
);
376 wxTRANSPARENT_BRUSH
= new wxBrush ("BLACK", wxTRANSPARENT
);
377 wxCYAN_BRUSH
= new wxBrush ("CYAN", wxSOLID
);
378 wxRED_BRUSH
= new wxBrush ("RED", wxSOLID
);
379 wxGREY_BRUSH
= new wxBrush ("GREY", wxSOLID
);
380 wxMEDIUM_GREY_BRUSH
= new wxBrush ("MEDIUM GREY", wxSOLID
);
381 wxLIGHT_GREY_BRUSH
= new wxBrush ("LIGHT GREY", wxSOLID
);
383 wxBLACK
= new wxColour ("BLACK");
384 wxWHITE
= new wxColour ("WHITE");
385 wxRED
= new wxColour ("RED");
386 wxBLUE
= new wxColour ("BLUE");
387 wxGREEN
= new wxColour ("GREEN");
388 wxCYAN
= new wxColour ("CYAN");
389 wxLIGHT_GREY
= new wxColour ("LIGHT GREY");
391 wxSTANDARD_CURSOR
= new wxCursor (wxCURSOR_ARROW
);
392 wxHOURGLASS_CURSOR
= new wxCursor (wxCURSOR_WAIT
);
393 wxCROSS_CURSOR
= new wxCursor (wxCURSOR_CROSS
);
396 void wxDeleteStockObjects ()
398 wxDELETE(wxNORMAL_FONT
);
399 wxDELETE(wxSMALL_FONT
);
400 wxDELETE(wxITALIC_FONT
);
401 wxDELETE(wxSWISS_FONT
);
404 wxDELETE(wxCYAN_PEN
);
405 wxDELETE(wxGREEN_PEN
);
406 wxDELETE(wxBLACK_PEN
);
407 wxDELETE(wxWHITE_PEN
);
408 wxDELETE(wxTRANSPARENT_PEN
);
409 wxDELETE(wxBLACK_DASHED_PEN
);
410 wxDELETE(wxGREY_PEN
);
411 wxDELETE(wxMEDIUM_GREY_PEN
);
412 wxDELETE(wxLIGHT_GREY_PEN
);
414 wxDELETE(wxBLUE_BRUSH
);
415 wxDELETE(wxGREEN_BRUSH
);
416 wxDELETE(wxWHITE_BRUSH
);
417 wxDELETE(wxBLACK_BRUSH
);
418 wxDELETE(wxTRANSPARENT_BRUSH
);
419 wxDELETE(wxCYAN_BRUSH
);
420 wxDELETE(wxRED_BRUSH
);
421 wxDELETE(wxGREY_BRUSH
);
422 wxDELETE(wxMEDIUM_GREY_BRUSH
);
423 wxDELETE(wxLIGHT_GREY_BRUSH
);
431 wxDELETE(wxLIGHT_GREY
);
433 wxDELETE(wxSTANDARD_CURSOR
);
434 wxDELETE(wxHOURGLASS_CURSOR
);
435 wxDELETE(wxCROSS_CURSOR
);
438 void wxDeleteStockLists() {
439 wxDELETE(wxTheBrushList
);
440 wxDELETE(wxThePenList
);
441 wxDELETE(wxTheFontList
);
442 wxDELETE(wxTheBitmapList
);
445 wxBitmapList::wxBitmapList ()
449 wxBitmapList::~wxBitmapList ()
451 #if defined(__WXMSW__) || defined(__WXMOTIF__)
452 wxNode
*node
= First ();
455 wxBitmap
*bitmap
= (wxBitmap
*) node
->Data ();
456 wxNode
*next
= node
->Next ();
457 if (bitmap
->GetVisible())
464 // Pen and Brush lists
465 wxPenList::~wxPenList ()
467 #if defined(__WXMSW__) || defined(__WXMOTIF__)
468 wxNode
*node
= First ();
471 wxPen
*pen
= (wxPen
*) node
->Data ();
472 wxNode
*next
= node
->Next ();
473 if (pen
->GetVisible())
480 void wxPenList::AddPen (wxPen
* pen
)
485 void wxPenList::RemovePen (wxPen
* pen
)
490 wxPen
*wxPenList::FindOrCreatePen (const wxColour
& colour
, int width
, int style
)
492 for (wxNode
* node
= First (); node
; node
= node
->Next ())
494 wxPen
*each_pen
= (wxPen
*) node
->Data ();
495 if (each_pen
&& each_pen
->GetVisible() &&
496 each_pen
->GetWidth () == width
&&
497 each_pen
->GetStyle () == style
&&
498 each_pen
->GetColour ().Red () == colour
.Red () &&
499 each_pen
->GetColour ().Green () == colour
.Green () &&
500 each_pen
->GetColour ().Blue () == colour
.Blue ())
503 wxPen
*pen
= new wxPen (colour
, width
, style
);
505 // Yes, we can return a pointer to this in a later FindOrCreatePen call,
506 // because we created it within FindOrCreatePen. Safeguards against
507 // returning a pointer to an automatic variable and hanging on to it
508 // (dangling pointer).
509 pen
->SetVisible(TRUE
);
513 wxBrushList::~wxBrushList ()
515 #if defined(__WXMSW__) || defined(__WXMOTIF__)
516 wxNode
*node
= First ();
519 wxBrush
*brush
= (wxBrush
*) node
->Data ();
520 wxNode
*next
= node
->Next ();
521 if (brush
->GetVisible())
528 void wxBrushList::AddBrush (wxBrush
* brush
)
533 wxBrush
*wxBrushList::FindOrCreateBrush (const wxColour
& colour
, int style
)
535 for (wxNode
* node
= First (); node
; node
= node
->Next ())
537 wxBrush
*each_brush
= (wxBrush
*) node
->Data ();
538 if (each_brush
&& each_brush
->GetVisible() &&
539 each_brush
->GetStyle () == style
&&
540 each_brush
->GetColour ().Red () == colour
.Red () &&
541 each_brush
->GetColour ().Green () == colour
.Green () &&
542 each_brush
->GetColour ().Blue () == colour
.Blue ())
545 // Yes, we can return a pointer to this in a later FindOrCreateBrush call,
546 // because we created it within FindOrCreateBrush. Safeguards against
547 // returning a pointer to an automatic variable and hanging on to it
548 // (dangling pointer).
549 wxBrush
*brush
= new wxBrush (colour
, style
);
550 brush
->SetVisible(TRUE
);
554 void wxBrushList::RemoveBrush (wxBrush
* brush
)
556 DeleteObject (brush
);
559 wxFontList::~wxFontList ()
561 wxNode
*node
= First ();
564 // Only delete objects that are 'visible', i.e.
565 // that have been created using FindOrCreate...,
566 // where the pointers are expected to be shared
567 // (and therefore not deleted by any one part of an app).
568 wxFont
*font
= (wxFont
*) node
->Data ();
569 wxNode
*next
= node
->Next ();
570 if (font
->GetVisible())
576 void wxFontList::AddFont (wxFont
* font
)
581 void wxFontList::RemoveFont (wxFont
* font
)
587 FindOrCreateFont (int PointSize
, int FamilyOrFontId
, int Style
, int Weight
, bool underline
, const wxString
& Face
)
589 for (wxNode
* node
= First (); node
; node
= node
->Next ())
591 wxFont
*each_font
= (wxFont
*) node
->Data ();
592 if (each_font
&& each_font
->GetVisible() && each_font
->Ok() &&
593 each_font
->GetPointSize () == PointSize
&&
594 each_font
->GetStyle () == Style
&&
595 each_font
->GetWeight () == Weight
&&
596 each_font
->GetUnderlined () == underline
&&
598 // each_font->GetFontId () == FamilyOrFontId) /* New font system */
600 each_font
->GetFamily () == FamilyOrFontId
&&
601 (!each_font
->GetFaceName() || each_font
->GetFaceName() == Face
))
605 wxFont
*font
= new wxFont (PointSize
, FamilyOrFontId
, Style
, Weight
, underline
, Face
);
606 font
->SetVisible(TRUE
);
610 void wxBitmapList::AddBitmap(wxBitmap
*bitmap
)
615 void wxBitmapList::RemoveBitmap(wxBitmap
*bitmap
)
617 DeleteObject(bitmap
);
620 wxSize
wxGetDisplaySize()
623 wxDisplaySize(& x
, & y
);
627 wxResourceCache::~wxResourceCache ()
629 wxNode
*node
= First ();
631 wxGDIObject
*item
= (wxGDIObject
*)node
->Data();
632 if (item
->IsKindOf(CLASSINFO(wxBrush
))) {
633 wxBrush
*brush
= (wxBrush
*)item
;
637 if (item
->IsKindOf(CLASSINFO(wxFont
))) {
638 wxFont
*font
= (wxFont
*)item
;
642 if (item
->IsKindOf(CLASSINFO(wxBitmap
))) {
643 wxBitmap
*bitmap
= (wxBitmap
*)item
;
647 if (item
->IsKindOf(CLASSINFO(wxColour
))) {
648 wxColour
*colour
= (wxColour
*)item
;
652 wxNode
*next
= node
->Next ();