]>
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"
17 #define XtDisplay XTDISPLAY
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
28 #include "wx/gdicmn.h"
31 #include "wx/bitmap.h"
33 #include "wx/cursor.h"
35 #include "wx/palette.h"
39 #include "wx/settings.h"
50 #pragma message disable nosimpint
54 #pragma message enable nosimpint
58 IMPLEMENT_CLASS(wxColourDatabase
, wxList
)
59 IMPLEMENT_DYNAMIC_CLASS(wxFontList
, wxList
)
60 IMPLEMENT_DYNAMIC_CLASS(wxPenList
, wxList
)
61 IMPLEMENT_DYNAMIC_CLASS(wxBrushList
, wxList
)
62 IMPLEMENT_DYNAMIC_CLASS(wxBitmapList
, wxList
)
63 IMPLEMENT_DYNAMIC_CLASS(wxResourceCache
, wxList
)
65 IMPLEMENT_ABSTRACT_CLASS(wxDCBase
, wxObject
)
67 wxRect::wxRect(const wxPoint
& topLeft
, const wxPoint
& bottomRight
)
71 width
= bottomRight
.x
- topLeft
.x
+ 1;
72 height
= bottomRight
.y
- topLeft
.y
+ 1;
87 wxRect::wxRect(const wxPoint
& point
, const wxSize
& size
)
89 x
= point
.x
; y
= point
.y
;
90 width
= size
.x
; height
= size
.y
;
93 bool wxRect::operator==(const wxRect
& rect
) const
95 return ((x
== rect
.x
) &&
97 (width
== rect
.width
) &&
98 (height
== rect
.height
));
101 wxRect
& wxRect::operator += (const wxRect
& rect
)
103 *this = (*this + rect
);
107 wxRect
wxRect::operator + (const wxRect
& rect
) const
109 int x1
= wxMin(this->x
, rect
.x
);
110 int y1
= wxMin(this->y
, rect
.y
);
111 int y2
= wxMax(y
+height
, rect
.height
+rect
.y
);
112 int x2
= wxMax(x
+width
, rect
.width
+rect
.x
);
113 return wxRect(x1
, y1
, x2
-x1
, y2
-y1
);
116 bool wxRect::Inside(int cx
, int cy
) const
118 return ( (cx
>= x
) && (cy
>= y
)
119 && ((cy
- y
) < height
)
120 && ((cx
- x
) < width
)
124 wxColourDatabase::wxColourDatabase (int type
) : wxList (type
)
128 wxColourDatabase::~wxColourDatabase ()
130 // Cleanup Colour allocated in Initialize()
131 wxNode
*node
= First ();
134 wxColour
*col
= (wxColour
*) node
->Data ();
135 wxNode
*next
= node
->Next ();
141 // Colour database stuff
142 void wxColourDatabase::Initialize ()
144 static const struct wxColourDesc
151 {wxT("AQUAMARINE"),112, 219, 147},
152 {wxT("BLACK"),0, 0, 0},
153 {wxT("BLUE"), 0, 0, 255},
154 {wxT("BLUE VIOLET"), 159, 95, 159},
155 {wxT("BROWN"), 165, 42, 42},
156 {wxT("CADET BLUE"), 95, 159, 159},
157 {wxT("CORAL"), 255, 127, 0},
158 {wxT("CORNFLOWER BLUE"), 66, 66, 111},
159 {wxT("CYAN"), 0, 255, 255},
160 {wxT("DARK GREY"), 47, 47, 47}, // ?
162 {wxT("DARK GREEN"), 47, 79, 47},
163 {wxT("DARK OLIVE GREEN"), 79, 79, 47},
164 {wxT("DARK ORCHID"), 153, 50, 204},
165 {wxT("DARK SLATE BLUE"), 107, 35, 142},
166 {wxT("DARK SLATE GREY"), 47, 79, 79},
167 {wxT("DARK TURQUOISE"), 112, 147, 219},
168 {wxT("DIM GREY"), 84, 84, 84},
169 {wxT("FIREBRICK"), 142, 35, 35},
170 {wxT("FOREST GREEN"), 35, 142, 35},
171 {wxT("GOLD"), 204, 127, 50},
172 {wxT("GOLDENROD"), 219, 219, 112},
173 {wxT("GREY"), 128, 128, 128},
174 {wxT("GREEN"), 0, 255, 0},
175 {wxT("GREEN YELLOW"), 147, 219, 112},
176 {wxT("INDIAN RED"), 79, 47, 47},
177 {wxT("KHAKI"), 159, 159, 95},
178 {wxT("LIGHT BLUE"), 191, 216, 216},
179 {wxT("LIGHT GREY"), 192, 192, 192},
180 {wxT("LIGHT STEEL BLUE"), 143, 143, 188},
181 {wxT("LIME GREEN"), 50, 204, 50},
182 {wxT("LIGHT MAGENTA"), 255, 0, 255},
183 {wxT("MAGENTA"), 255, 0, 255},
184 {wxT("MAROON"), 142, 35, 107},
185 {wxT("MEDIUM AQUAMARINE"), 50, 204, 153},
186 {wxT("MEDIUM GREY"), 100, 100, 100},
187 {wxT("MEDIUM BLUE"), 50, 50, 204},
188 {wxT("MEDIUM FOREST GREEN"), 107, 142, 35},
189 {wxT("MEDIUM GOLDENROD"), 234, 234, 173},
190 {wxT("MEDIUM ORCHID"), 147, 112, 219},
191 {wxT("MEDIUM SEA GREEN"), 66, 111, 66},
192 {wxT("MEDIUM SLATE BLUE"), 127, 0, 255},
193 {wxT("MEDIUM SPRING GREEN"), 127, 255, 0},
194 {wxT("MEDIUM TURQUOISE"), 112, 219, 219},
195 {wxT("MEDIUM VIOLET RED"), 219, 112, 147},
196 {wxT("MIDNIGHT BLUE"), 47, 47, 79},
197 {wxT("NAVY"), 35, 35, 142},
198 {wxT("ORANGE"), 204, 50, 50},
199 {wxT("ORANGE RED"), 255, 0, 127},
200 {wxT("ORCHID"), 219, 112, 219},
201 {wxT("PALE GREEN"), 143, 188, 143},
202 {wxT("PINK"), 188, 143, 234},
203 {wxT("PLUM"), 234, 173, 234},
204 {wxT("PURPLE"), 176, 0, 255},
205 {wxT("RED"), 255, 0, 0},
206 {wxT("SALMON"), 111, 66, 66},
207 {wxT("SEA GREEN"), 35, 142, 107},
208 {wxT("SIENNA"), 142, 107, 35},
209 {wxT("SKY BLUE"), 50, 153, 204},
210 {wxT("SLATE BLUE"), 0, 127, 255},
211 {wxT("SPRING GREEN"), 0, 255, 127},
212 {wxT("STEEL BLUE"), 35, 107, 142},
213 {wxT("TAN"), 219, 147, 112},
214 {wxT("THISTLE"), 216, 191, 216},
215 {wxT("TURQUOISE"), 173, 234, 234},
216 {wxT("VIOLET"), 79, 47, 79},
217 {wxT("VIOLET RED"), 204, 50, 153},
218 {wxT("WHEAT"), 216, 216, 191},
219 {wxT("WHITE"), 255, 255, 255},
220 {wxT("YELLOW"), 255, 255, 0},
221 {wxT("YELLOW GREEN"), 153, 204, 50},
222 {wxT("MEDIUM GOLDENROD"), 234, 234, 173},
223 {wxT("MEDIUM FOREST GREEN"), 107, 142, 35},
224 {wxT("LIGHT MAGENTA"), 255, 0, 255},
225 {wxT("MEDIUM GREY"), 100, 100, 100},
228 for ( size_t n
= 0; n
< WXSIZEOF(wxColourTable
); n
++ )
230 const wxColourDesc
& cc
= wxColourTable
[n
];
231 Append(cc
.name
, new wxColour(cc
.r
,cc
.g
,cc
.b
));
236 * Changed by Ian Brown, July 1994.
238 * When running under X, the Colour Database starts off empty. The X server
239 * is queried for the colour first time after which it is entered into the
240 * database. This allows our client to use the server colour database which
241 * is hopefully gamma corrected for the display being used.
244 wxColour
*wxColourDatabase::FindColour(const wxString
& colour
)
246 // VZ: make the comparaison case insensitive and also match both grey and
248 wxString colName
= colour
;
250 wxString colName2
= colName
;
251 if ( !colName2
.Replace(_T("GRAY"), _T("GREY")) )
254 wxNode
*node
= First();
257 const wxChar
*key
= node
->GetKeyString();
258 if ( colName
== key
|| colName2
== key
)
260 return (wxColour
*)node
->Data();
273 // TODO for other implementations. This should really go into
274 // platform-specific directories.
283 wxColour
*col
= new wxColour( colour
);
287 return (wxColour
*) NULL
;
289 Append( colour
, col
);
297 Display
*display
= XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()) ;
300 Xv_Screen screen
= xv_get(xview_server
, SERVER_NTH_SCREEN
, 0);
301 Xv_opaque root_window
= xv_get(screen
, XV_ROOT
);
302 Display
*display
= (Display
*)xv_get(root_window
, XV_DISPLAY
);
305 /* MATTHEW: [4] Use wxGetMainColormap */
306 if (!XParseColor(display
, (Colormap
) wxTheApp
->GetMainColormap((WXDisplay
*) display
), colour
,&xcolour
))
309 unsigned char r
= (unsigned char)(xcolour
.red
>> 8);
310 unsigned char g
= (unsigned char)(xcolour
.green
>> 8);
311 unsigned char b
= (unsigned char)(xcolour
.blue
>> 8);
313 wxColour
*col
= new wxColour(r
, g
, b
);
320 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();
347 void wxInitializeStockLists () {
348 wxTheBrushList
= new wxBrushList
;
349 wxThePenList
= new wxPenList
;
350 wxTheFontList
= new wxFontList
;
351 wxTheBitmapList
= new wxBitmapList
;
354 void wxInitializeStockObjects ()
360 // wxFontPool = new XFontPool;
363 // why under MSW fonts shouldn't have the standard system size?
366 static const int sizeFont = 10;
368 static const int sizeFont = 12;
372 // wxNORMAL_FONT = new wxFont (sizeFont, wxMODERN, wxNORMAL, wxNORMAL);
373 wxNORMAL_FONT
= new wxFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
374 static const int sizeFont
= wxNORMAL_FONT
->GetPointSize();
376 wxSMALL_FONT
= new wxFont (sizeFont
- 2, wxSWISS
, wxNORMAL
, wxNORMAL
);
377 wxITALIC_FONT
= new wxFont (sizeFont
, wxROMAN
, wxITALIC
, wxNORMAL
);
378 wxSWISS_FONT
= new wxFont (sizeFont
, wxSWISS
, wxNORMAL
, wxNORMAL
);
380 wxRED_PEN
= new wxPen ("RED", 1, wxSOLID
);
381 wxCYAN_PEN
= new wxPen ("CYAN", 1, wxSOLID
);
382 wxGREEN_PEN
= new wxPen ("GREEN", 1, wxSOLID
);
383 wxBLACK_PEN
= new wxPen ("BLACK", 1, wxSOLID
);
384 wxWHITE_PEN
= new wxPen ("WHITE", 1, wxSOLID
);
385 wxTRANSPARENT_PEN
= new wxPen ("BLACK", 1, wxTRANSPARENT
);
386 wxBLACK_DASHED_PEN
= new wxPen ("BLACK", 1, wxSHORT_DASH
);
387 wxGREY_PEN
= new wxPen ("GREY", 1, wxSOLID
);
388 wxMEDIUM_GREY_PEN
= new wxPen ("MEDIUM GREY", 1, wxSOLID
);
389 wxLIGHT_GREY_PEN
= new wxPen ("LIGHT GREY", 1, wxSOLID
);
391 wxBLUE_BRUSH
= new wxBrush ("BLUE", wxSOLID
);
392 wxGREEN_BRUSH
= new wxBrush ("GREEN", wxSOLID
);
393 wxWHITE_BRUSH
= new wxBrush ("WHITE", wxSOLID
);
394 wxBLACK_BRUSH
= new wxBrush ("BLACK", wxSOLID
);
395 wxTRANSPARENT_BRUSH
= new wxBrush ("BLACK", wxTRANSPARENT
);
396 wxCYAN_BRUSH
= new wxBrush ("CYAN", wxSOLID
);
397 wxRED_BRUSH
= new wxBrush ("RED", wxSOLID
);
398 wxGREY_BRUSH
= new wxBrush ("GREY", wxSOLID
);
399 wxMEDIUM_GREY_BRUSH
= new wxBrush ("MEDIUM GREY", wxSOLID
);
400 wxLIGHT_GREY_BRUSH
= new wxBrush ("LIGHT GREY", wxSOLID
);
402 wxBLACK
= new wxColour ("BLACK");
403 wxWHITE
= new wxColour ("WHITE");
404 wxRED
= new wxColour ("RED");
405 wxBLUE
= new wxColour ("BLUE");
406 wxGREEN
= new wxColour ("GREEN");
407 wxCYAN
= new wxColour ("CYAN");
408 wxLIGHT_GREY
= new wxColour ("LIGHT GREY");
410 wxSTANDARD_CURSOR
= new wxCursor (wxCURSOR_ARROW
);
411 wxHOURGLASS_CURSOR
= new wxCursor (wxCURSOR_WAIT
);
412 wxCROSS_CURSOR
= new wxCursor (wxCURSOR_CROSS
);
415 void wxDeleteStockObjects ()
417 wxDELETE(wxNORMAL_FONT
);
418 wxDELETE(wxSMALL_FONT
);
419 wxDELETE(wxITALIC_FONT
);
420 wxDELETE(wxSWISS_FONT
);
423 wxDELETE(wxCYAN_PEN
);
424 wxDELETE(wxGREEN_PEN
);
425 wxDELETE(wxBLACK_PEN
);
426 wxDELETE(wxWHITE_PEN
);
427 wxDELETE(wxTRANSPARENT_PEN
);
428 wxDELETE(wxBLACK_DASHED_PEN
);
429 wxDELETE(wxGREY_PEN
);
430 wxDELETE(wxMEDIUM_GREY_PEN
);
431 wxDELETE(wxLIGHT_GREY_PEN
);
433 wxDELETE(wxBLUE_BRUSH
);
434 wxDELETE(wxGREEN_BRUSH
);
435 wxDELETE(wxWHITE_BRUSH
);
436 wxDELETE(wxBLACK_BRUSH
);
437 wxDELETE(wxTRANSPARENT_BRUSH
);
438 wxDELETE(wxCYAN_BRUSH
);
439 wxDELETE(wxRED_BRUSH
);
440 wxDELETE(wxGREY_BRUSH
);
441 wxDELETE(wxMEDIUM_GREY_BRUSH
);
442 wxDELETE(wxLIGHT_GREY_BRUSH
);
450 wxDELETE(wxLIGHT_GREY
);
452 wxDELETE(wxSTANDARD_CURSOR
);
453 wxDELETE(wxHOURGLASS_CURSOR
);
454 wxDELETE(wxCROSS_CURSOR
);
457 void wxDeleteStockLists() {
458 wxDELETE(wxTheBrushList
);
459 wxDELETE(wxThePenList
);
460 wxDELETE(wxTheFontList
);
461 wxDELETE(wxTheBitmapList
);
464 wxBitmapList::wxBitmapList ()
468 wxBitmapList::~wxBitmapList ()
470 wxNode
*node
= First ();
473 wxBitmap
*bitmap
= (wxBitmap
*) node
->Data ();
474 wxNode
*next
= node
->Next ();
475 if (bitmap
->GetVisible())
481 // Pen and Brush lists
482 wxPenList::~wxPenList ()
484 wxNode
*node
= First ();
487 wxPen
*pen
= (wxPen
*) node
->Data ();
488 wxNode
*next
= node
->Next ();
489 if (pen
->GetVisible())
495 void wxPenList::AddPen (wxPen
* pen
)
500 void wxPenList::RemovePen (wxPen
* pen
)
505 wxPen
*wxPenList::FindOrCreatePen (const wxColour
& colour
, int width
, int style
)
507 for (wxNode
* node
= First (); node
; node
= node
->Next ())
509 wxPen
*each_pen
= (wxPen
*) node
->Data ();
511 each_pen
->GetVisible() &&
512 each_pen
->GetWidth () == width
&&
513 each_pen
->GetStyle () == style
&&
514 each_pen
->GetColour ().Red () == colour
.Red () &&
515 each_pen
->GetColour ().Green () == colour
.Green () &&
516 each_pen
->GetColour ().Blue () == colour
.Blue ())
519 wxPen
*pen
= new wxPen (colour
, width
, style
);
521 // Yes, we can return a pointer to this in a later FindOrCreatePen call,
522 // because we created it within FindOrCreatePen. Safeguards against
523 // returning a pointer to an automatic variable and hanging on to it
524 // (dangling pointer).
525 pen
->SetVisible(TRUE
);
530 wxBrushList::~wxBrushList ()
532 wxNode
*node
= First ();
535 wxBrush
*brush
= (wxBrush
*) node
->Data ();
536 wxNode
*next
= node
->Next ();
537 if (brush
->GetVisible())
543 void wxBrushList::AddBrush (wxBrush
* brush
)
548 wxBrush
*wxBrushList::FindOrCreateBrush (const wxColour
& colour
, int style
)
550 for (wxNode
* node
= First (); node
; node
= node
->Next ())
552 wxBrush
*each_brush
= (wxBrush
*) node
->Data ();
554 each_brush
->GetVisible() &&
555 each_brush
->GetStyle () == style
&&
556 each_brush
->GetColour ().Red () == colour
.Red () &&
557 each_brush
->GetColour ().Green () == colour
.Green () &&
558 each_brush
->GetColour ().Blue () == colour
.Blue ())
562 // Yes, we can return a pointer to this in a later FindOrCreateBrush call,
563 // because we created it within FindOrCreateBrush. Safeguards against
564 // returning a pointer to an automatic variable and hanging on to it
565 // (dangling pointer).
566 wxBrush
*brush
= new wxBrush (colour
, style
);
568 brush
->SetVisible(TRUE
);
573 void wxBrushList::RemoveBrush (wxBrush
* brush
)
575 DeleteObject (brush
);
578 wxFontList::~wxFontList ()
580 wxNode
*node
= First ();
583 // Only delete objects that are 'visible', i.e.
584 // that have been created using FindOrCreate...,
585 // where the pointers are expected to be shared
586 // (and therefore not deleted by any one part of an app).
587 wxFont
*font
= (wxFont
*) node
->Data ();
588 wxNode
*next
= node
->Next ();
589 if (font
->GetVisible())
595 void wxFontList::AddFont (wxFont
* font
)
600 void wxFontList::RemoveFont (wxFont
* font
)
605 wxFont
*wxFontList::FindOrCreateFont(int pointSize
,
610 const wxString
& facename
,
611 wxFontEncoding encoding
)
613 wxFont
*font
= (wxFont
*)NULL
;
615 for ( node
= First(); node
; node
= node
->Next() )
617 font
= (wxFont
*)node
->Data();
618 if ( font
->GetVisible() &&
620 font
->GetPointSize () == pointSize
&&
621 font
->GetStyle () == style
&&
622 font
->GetWeight () == weight
&&
623 font
->GetUnderlined () == underline
)
625 int fontFamily
= font
->GetFamily();
627 #if defined(__WXGTK__)
628 // under GTK the default family is wxSWISS, so looking for a font
629 // with wxDEFAULT family should return a wxSWISS one instead of
630 // creating a new one
631 bool same
= (fontFamily
== family
) ||
632 (fontFamily
== wxSWISS
&& family
== wxDEFAULT
);
634 // VZ: but why elsewhere do we require an exact match? mystery...
635 bool same
= fontFamily
== family
;
638 // empty facename matches anything at all: this is bad because
639 // depending on which fonts are already created, we might get back
640 // a different font if we create it with empty facename, but it is
641 // still better than never matching anything in the cache at all
643 if ( same
&& !!facename
)
645 const wxString
& fontFace
= font
->GetFaceName();
647 // empty facename matches everything
648 same
= !fontFace
|| fontFace
== facename
;
651 if ( same
&& (encoding
!= wxFONTENCODING_DEFAULT
) )
653 // have to match the encoding too
654 same
= font
->GetEncoding() == encoding
;
666 // font not found, create the new one
667 font
= new wxFont(pointSize
, family
, style
, weight
,
668 underline
, facename
, encoding
);
670 // and mark it as being cacheable
671 font
->SetVisible(TRUE
);
677 void wxBitmapList::AddBitmap(wxBitmap
*bitmap
)
682 void wxBitmapList::RemoveBitmap(wxBitmap
*bitmap
)
684 DeleteObject(bitmap
);
687 wxSize
wxGetDisplaySize()
690 wxDisplaySize(& x
, & y
);
694 wxResourceCache::~wxResourceCache ()
696 wxNode
*node
= First ();
698 wxObject
*item
= (wxObject
*)node
->Data();
701 node
= node
->Next ();