]>
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 {_T("AQUAMARINE"),112, 219, 147},
159 {_T("BLACK"),0, 0, 0},
160 {_T("BLUE"), 0, 0, 255},
161 {_T("BLUE VIOLET"), 159, 95, 159},
162 {_T("BROWN"), 165, 42, 42},
163 {_T("CADET BLUE"), 95, 159, 159},
164 {_T("CORAL"), 255, 127, 0},
165 {_T("CORNFLOWER BLUE"), 66, 66, 111},
166 {_T("CYAN"), 0, 255, 255},
167 {_T("DARK GREY"), 47, 47, 47}, // ?
169 {_T("DARK GREEN"), 47, 79, 47},
170 {_T("DARK OLIVE GREEN"), 79, 79, 47},
171 {_T("DARK ORCHID"), 153, 50, 204},
172 {_T("DARK SLATE BLUE"), 107, 35, 142},
173 {_T("DARK SLATE GREY"), 47, 79, 79},
174 {_T("DARK TURQUOISE"), 112, 147, 219},
175 {_T("DIM GREY"), 84, 84, 84},
176 {_T("FIREBRICK"), 142, 35, 35},
177 {_T("FOREST GREEN"), 35, 142, 35},
178 {_T("GOLD"), 204, 127, 50},
179 {_T("GOLDENROD"), 219, 219, 112},
180 {_T("GREY"), 128, 128, 128},
181 {_T("GREEN"), 0, 255, 0},
182 {_T("GREEN YELLOW"), 147, 219, 112},
183 {_T("INDIAN RED"), 79, 47, 47},
184 {_T("KHAKI"), 159, 159, 95},
185 {_T("LIGHT BLUE"), 191, 216, 216},
186 {_T("LIGHT GREY"), 192, 192, 192},
187 {_T("LIGHT STEEL BLUE"), 143, 143, 188},
188 {_T("LIME GREEN"), 50, 204, 50},
189 {_T("LIGHT MAGENTA"), 255, 0, 255},
190 {_T("MAGENTA"), 255, 0, 255},
191 {_T("MAROON"), 142, 35, 107},
192 {_T("MEDIUM AQUAMARINE"), 50, 204, 153},
193 {_T("MEDIUM GREY"), 100, 100, 100},
194 {_T("MEDIUM BLUE"), 50, 50, 204},
195 {_T("MEDIUM FOREST GREEN"), 107, 142, 35},
196 {_T("MEDIUM GOLDENROD"), 234, 234, 173},
197 {_T("MEDIUM ORCHID"), 147, 112, 219},
198 {_T("MEDIUM SEA GREEN"), 66, 111, 66},
199 {_T("MEDIUM SLATE BLUE"), 127, 0, 255},
200 {_T("MEDIUM SPRING GREEN"), 127, 255, 0},
201 {_T("MEDIUM TURQUOISE"), 112, 219, 219},
202 {_T("MEDIUM VIOLET RED"), 219, 112, 147},
203 {_T("MIDNIGHT BLUE"), 47, 47, 79},
204 {_T("NAVY"), 35, 35, 142},
205 {_T("ORANGE"), 204, 50, 50},
206 {_T("ORANGE RED"), 255, 0, 127},
207 {_T("ORCHID"), 219, 112, 219},
208 {_T("PALE GREEN"), 143, 188, 143},
209 {_T("PINK"), 188, 143, 234},
210 {_T("PLUM"), 234, 173, 234},
211 {_T("PURPLE"), 176, 0, 255},
212 {_T("RED"), 255, 0, 0},
213 {_T("SALMON"), 111, 66, 66},
214 {_T("SEA GREEN"), 35, 142, 107},
215 {_T("SIENNA"), 142, 107, 35},
216 {_T("SKY BLUE"), 50, 153, 204},
217 {_T("SLATE BLUE"), 0, 127, 255},
218 {_T("SPRING GREEN"), 0, 255, 127},
219 {_T("STEEL BLUE"), 35, 107, 142},
220 {_T("TAN"), 219, 147, 112},
221 {_T("THISTLE"), 216, 191, 216},
222 {_T("TURQUOISE"), 173, 234, 234},
223 {_T("VIOLET"), 79, 47, 79},
224 {_T("VIOLET RED"), 204, 50, 153},
225 {_T("WHEAT"), 216, 216, 191},
226 {_T("WHITE"), 255, 255, 255},
227 {_T("YELLOW"), 255, 255, 0},
228 {_T("YELLOW GREEN"), 153, 204, 50},
231 #if defined(__WXGTK__) || defined(__X__)
232 {_T("MEDIUM GOLDENROD"), 234, 234, 173},
233 {_T("MEDIUM FOREST GREEN"), 107, 142, 35},
234 {_T("LIGHT MAGENTA"), 255, 0, 255},
235 {_T("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.
282 wxColour
*col
= new wxColour( colour
);
286 return (wxColour
*) NULL
;
288 Append( colour
, col
);
298 Display
*display
= XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()) ;
301 Xv_Screen screen
= xv_get(xview_server
, SERVER_NTH_SCREEN
, 0);
302 Xv_opaque root_window
= xv_get(screen
, XV_ROOT
);
303 Display
*display
= (Display
*)xv_get(root_window
, XV_DISPLAY
);
306 /* MATTHEW: [4] Use wxGetMainColormap */
307 if (!XParseColor(display
, (Colormap
) wxTheApp
->GetMainColormap((WXDisplay
*) display
), colour
,&xcolour
))
310 unsigned char r
= (unsigned char)(xcolour
.red
>> 8);
311 unsigned char g
= (unsigned char)(xcolour
.green
>> 8);
312 unsigned char b
= (unsigned char)(xcolour
.blue
>> 8);
314 wxColour
*col
= new wxColour(r
, g
, b
);
322 wxString
wxColourDatabase::FindName (const wxColour
& colour
) const
324 unsigned char red
= colour
.Red ();
325 unsigned char green
= colour
.Green ();
326 unsigned char blue
= colour
.Blue ();
328 for (wxNode
* node
= First (); node
; node
= node
->Next ())
330 wxColour
*col
= (wxColour
*) node
->Data ();
332 if (col
->Red () == red
&& col
->Green () == green
&& col
->Blue () == blue
)
334 const wxChar
*found
= node
->GetKeyString();
336 return wxString(found
);
339 return wxString(""); // Not Found
343 void wxInitializeStockLists () {
344 wxTheBrushList
= new wxBrushList
;
345 wxThePenList
= new wxPenList
;
346 wxTheFontList
= new wxFontList
;
347 wxTheBitmapList
= new wxBitmapList
;
350 void wxInitializeStockObjects ()
356 // wxFontPool = new XFontPool;
359 wxNORMAL_FONT
= new wxFont (12, wxMODERN
, wxNORMAL
, wxNORMAL
);
360 wxSMALL_FONT
= new wxFont (10, wxSWISS
, wxNORMAL
, wxNORMAL
);
361 wxITALIC_FONT
= new wxFont (12, wxROMAN
, wxITALIC
, wxNORMAL
);
362 wxSWISS_FONT
= new wxFont (12, wxSWISS
, wxNORMAL
, wxNORMAL
);
364 wxRED_PEN
= new wxPen ("RED", 1, wxSOLID
);
365 wxCYAN_PEN
= new wxPen ("CYAN", 1, wxSOLID
);
366 wxGREEN_PEN
= new wxPen ("GREEN", 1, wxSOLID
);
367 wxBLACK_PEN
= new wxPen ("BLACK", 1, wxSOLID
);
368 wxWHITE_PEN
= new wxPen ("WHITE", 1, wxSOLID
);
369 wxTRANSPARENT_PEN
= new wxPen ("BLACK", 1, wxTRANSPARENT
);
370 wxBLACK_DASHED_PEN
= new wxPen ("BLACK", 1, wxSHORT_DASH
);
371 wxGREY_PEN
= new wxPen ("GREY", 1, wxSOLID
);
372 wxMEDIUM_GREY_PEN
= new wxPen ("MEDIUM GREY", 1, wxSOLID
);
373 wxLIGHT_GREY_PEN
= new wxPen ("LIGHT GREY", 1, wxSOLID
);
375 wxBLUE_BRUSH
= new wxBrush ("BLUE", wxSOLID
);
376 wxGREEN_BRUSH
= new wxBrush ("GREEN", wxSOLID
);
377 wxWHITE_BRUSH
= new wxBrush ("WHITE", wxSOLID
);
378 wxBLACK_BRUSH
= new wxBrush ("BLACK", wxSOLID
);
379 wxTRANSPARENT_BRUSH
= new wxBrush ("BLACK", wxTRANSPARENT
);
380 wxCYAN_BRUSH
= new wxBrush ("CYAN", wxSOLID
);
381 wxRED_BRUSH
= new wxBrush ("RED", wxSOLID
);
382 wxGREY_BRUSH
= new wxBrush ("GREY", wxSOLID
);
383 wxMEDIUM_GREY_BRUSH
= new wxBrush ("MEDIUM GREY", wxSOLID
);
384 wxLIGHT_GREY_BRUSH
= new wxBrush ("LIGHT GREY", wxSOLID
);
386 wxBLACK
= new wxColour ("BLACK");
387 wxWHITE
= new wxColour ("WHITE");
388 wxRED
= new wxColour ("RED");
389 wxBLUE
= new wxColour ("BLUE");
390 wxGREEN
= new wxColour ("GREEN");
391 wxCYAN
= new wxColour ("CYAN");
392 wxLIGHT_GREY
= new wxColour ("LIGHT GREY");
394 wxSTANDARD_CURSOR
= new wxCursor (wxCURSOR_ARROW
);
395 wxHOURGLASS_CURSOR
= new wxCursor (wxCURSOR_WAIT
);
396 wxCROSS_CURSOR
= new wxCursor (wxCURSOR_CROSS
);
399 void wxDeleteStockObjects ()
401 wxDELETE(wxNORMAL_FONT
);
402 wxDELETE(wxSMALL_FONT
);
403 wxDELETE(wxITALIC_FONT
);
404 wxDELETE(wxSWISS_FONT
);
407 wxDELETE(wxCYAN_PEN
);
408 wxDELETE(wxGREEN_PEN
);
409 wxDELETE(wxBLACK_PEN
);
410 wxDELETE(wxWHITE_PEN
);
411 wxDELETE(wxTRANSPARENT_PEN
);
412 wxDELETE(wxBLACK_DASHED_PEN
);
413 wxDELETE(wxGREY_PEN
);
414 wxDELETE(wxMEDIUM_GREY_PEN
);
415 wxDELETE(wxLIGHT_GREY_PEN
);
417 wxDELETE(wxBLUE_BRUSH
);
418 wxDELETE(wxGREEN_BRUSH
);
419 wxDELETE(wxWHITE_BRUSH
);
420 wxDELETE(wxBLACK_BRUSH
);
421 wxDELETE(wxTRANSPARENT_BRUSH
);
422 wxDELETE(wxCYAN_BRUSH
);
423 wxDELETE(wxRED_BRUSH
);
424 wxDELETE(wxGREY_BRUSH
);
425 wxDELETE(wxMEDIUM_GREY_BRUSH
);
426 wxDELETE(wxLIGHT_GREY_BRUSH
);
434 wxDELETE(wxLIGHT_GREY
);
436 wxDELETE(wxSTANDARD_CURSOR
);
437 wxDELETE(wxHOURGLASS_CURSOR
);
438 wxDELETE(wxCROSS_CURSOR
);
441 void wxDeleteStockLists() {
442 wxDELETE(wxTheBrushList
);
443 wxDELETE(wxThePenList
);
444 wxDELETE(wxTheFontList
);
445 wxDELETE(wxTheBitmapList
);
448 wxBitmapList::wxBitmapList ()
452 wxBitmapList::~wxBitmapList ()
454 #if defined(__WXMSW__) || defined(__WXMOTIF__)
455 wxNode
*node
= First ();
458 wxBitmap
*bitmap
= (wxBitmap
*) node
->Data ();
459 wxNode
*next
= node
->Next ();
460 if (bitmap
->GetVisible())
467 // Pen and Brush lists
468 wxPenList::~wxPenList ()
470 #if defined(__WXMSW__) || defined(__WXMOTIF__)
471 wxNode
*node
= First ();
474 wxPen
*pen
= (wxPen
*) node
->Data ();
475 wxNode
*next
= node
->Next ();
476 if (pen
->GetVisible())
483 void wxPenList::AddPen (wxPen
* pen
)
488 void wxPenList::RemovePen (wxPen
* pen
)
493 wxPen
*wxPenList::FindOrCreatePen (const wxColour
& colour
, int width
, int style
)
495 for (wxNode
* node
= First (); node
; node
= node
->Next ())
497 wxPen
*each_pen
= (wxPen
*) node
->Data ();
498 if (each_pen
&& each_pen
->GetVisible() &&
499 each_pen
->GetWidth () == width
&&
500 each_pen
->GetStyle () == style
&&
501 each_pen
->GetColour ().Red () == colour
.Red () &&
502 each_pen
->GetColour ().Green () == colour
.Green () &&
503 each_pen
->GetColour ().Blue () == colour
.Blue ())
506 wxPen
*pen
= new wxPen (colour
, width
, style
);
508 // Yes, we can return a pointer to this in a later FindOrCreatePen call,
509 // because we created it within FindOrCreatePen. Safeguards against
510 // returning a pointer to an automatic variable and hanging on to it
511 // (dangling pointer).
512 pen
->SetVisible(TRUE
);
516 wxBrushList::~wxBrushList ()
518 #if defined(__WXMSW__) || defined(__WXMOTIF__)
519 wxNode
*node
= First ();
522 wxBrush
*brush
= (wxBrush
*) node
->Data ();
523 wxNode
*next
= node
->Next ();
524 if (brush
->GetVisible())
531 void wxBrushList::AddBrush (wxBrush
* brush
)
536 wxBrush
*wxBrushList::FindOrCreateBrush (const wxColour
& colour
, int style
)
538 for (wxNode
* node
= First (); node
; node
= node
->Next ())
540 wxBrush
*each_brush
= (wxBrush
*) node
->Data ();
541 if (each_brush
&& each_brush
->GetVisible() &&
542 each_brush
->GetStyle () == style
&&
543 each_brush
->GetColour ().Red () == colour
.Red () &&
544 each_brush
->GetColour ().Green () == colour
.Green () &&
545 each_brush
->GetColour ().Blue () == colour
.Blue ())
548 // Yes, we can return a pointer to this in a later FindOrCreateBrush call,
549 // because we created it within FindOrCreateBrush. Safeguards against
550 // returning a pointer to an automatic variable and hanging on to it
551 // (dangling pointer).
552 wxBrush
*brush
= new wxBrush (colour
, style
);
553 brush
->SetVisible(TRUE
);
557 void wxBrushList::RemoveBrush (wxBrush
* brush
)
559 DeleteObject (brush
);
562 wxFontList::~wxFontList ()
564 wxNode
*node
= First ();
567 // Only delete objects that are 'visible', i.e.
568 // that have been created using FindOrCreate...,
569 // where the pointers are expected to be shared
570 // (and therefore not deleted by any one part of an app).
571 wxFont
*font
= (wxFont
*) node
->Data ();
572 wxNode
*next
= node
->Next ();
573 if (font
->GetVisible())
579 void wxFontList::AddFont (wxFont
* font
)
584 void wxFontList::RemoveFont (wxFont
* font
)
590 FindOrCreateFont (int PointSize
, int FamilyOrFontId
, int Style
, int Weight
, bool underline
, const wxString
& Face
)
592 for (wxNode
* node
= First (); node
; node
= node
->Next ())
594 wxFont
*each_font
= (wxFont
*) node
->Data ();
595 if (each_font
&& each_font
->GetVisible() && each_font
->Ok() &&
596 each_font
->GetPointSize () == PointSize
&&
597 each_font
->GetStyle () == Style
&&
598 each_font
->GetWeight () == Weight
&&
599 each_font
->GetUnderlined () == underline
&&
601 // each_font->GetFontId () == FamilyOrFontId) /* New font system */
603 each_font
->GetFamily () == FamilyOrFontId
&&
604 ((each_font
->GetFaceName() == _T("")) || each_font
->GetFaceName() == Face
))
608 wxFont
*font
= new wxFont (PointSize
, FamilyOrFontId
, Style
, Weight
, underline
, Face
);
609 font
->SetVisible(TRUE
);
613 void wxBitmapList::AddBitmap(wxBitmap
*bitmap
)
618 void wxBitmapList::RemoveBitmap(wxBitmap
*bitmap
)
620 DeleteObject(bitmap
);
623 wxSize
wxGetDisplaySize()
626 wxDisplaySize(& x
, & y
);
630 wxResourceCache::~wxResourceCache ()
632 wxNode
*node
= First ();
634 wxGDIObject
*item
= (wxGDIObject
*)node
->Data();
635 if (item
->IsKindOf(CLASSINFO(wxBrush
))) {
636 wxBrush
*brush
= (wxBrush
*)item
;
640 if (item
->IsKindOf(CLASSINFO(wxFont
))) {
641 wxFont
*font
= (wxFont
*)item
;
645 if (item
->IsKindOf(CLASSINFO(wxBitmap
))) {
646 wxBitmap
*bitmap
= (wxBitmap
*)item
;
650 if (item
->IsKindOf(CLASSINFO(wxColour
))) {
651 wxColour
*colour
= (wxColour
*)item
;
655 wxNode
*next
= node
->Next ();