]>
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 ();
140 delete [] m_palTable
;
144 // Colour database stuff
145 void wxColourDatabase::Initialize ()
147 static const struct wxColourDesc
154 {wxT("AQUAMARINE"),112, 219, 147},
155 {wxT("BLACK"),0, 0, 0},
156 {wxT("BLUE"), 0, 0, 255},
157 {wxT("BLUE VIOLET"), 159, 95, 159},
158 {wxT("BROWN"), 165, 42, 42},
159 {wxT("CADET BLUE"), 95, 159, 159},
160 {wxT("CORAL"), 255, 127, 0},
161 {wxT("CORNFLOWER BLUE"), 66, 66, 111},
162 {wxT("CYAN"), 0, 255, 255},
163 {wxT("DARK GREY"), 47, 47, 47}, // ?
165 {wxT("DARK GREEN"), 47, 79, 47},
166 {wxT("DARK OLIVE GREEN"), 79, 79, 47},
167 {wxT("DARK ORCHID"), 153, 50, 204},
168 {wxT("DARK SLATE BLUE"), 107, 35, 142},
169 {wxT("DARK SLATE GREY"), 47, 79, 79},
170 {wxT("DARK TURQUOISE"), 112, 147, 219},
171 {wxT("DIM GREY"), 84, 84, 84},
172 {wxT("FIREBRICK"), 142, 35, 35},
173 {wxT("FOREST GREEN"), 35, 142, 35},
174 {wxT("GOLD"), 204, 127, 50},
175 {wxT("GOLDENROD"), 219, 219, 112},
176 {wxT("GREY"), 128, 128, 128},
177 {wxT("GREEN"), 0, 255, 0},
178 {wxT("GREEN YELLOW"), 147, 219, 112},
179 {wxT("INDIAN RED"), 79, 47, 47},
180 {wxT("KHAKI"), 159, 159, 95},
181 {wxT("LIGHT BLUE"), 191, 216, 216},
182 {wxT("LIGHT GREY"), 192, 192, 192},
183 {wxT("LIGHT STEEL BLUE"), 143, 143, 188},
184 {wxT("LIME GREEN"), 50, 204, 50},
185 {wxT("LIGHT MAGENTA"), 255, 0, 255},
186 {wxT("MAGENTA"), 255, 0, 255},
187 {wxT("MAROON"), 142, 35, 107},
188 {wxT("MEDIUM AQUAMARINE"), 50, 204, 153},
189 {wxT("MEDIUM GREY"), 100, 100, 100},
190 {wxT("MEDIUM BLUE"), 50, 50, 204},
191 {wxT("MEDIUM FOREST GREEN"), 107, 142, 35},
192 {wxT("MEDIUM GOLDENROD"), 234, 234, 173},
193 {wxT("MEDIUM ORCHID"), 147, 112, 219},
194 {wxT("MEDIUM SEA GREEN"), 66, 111, 66},
195 {wxT("MEDIUM SLATE BLUE"), 127, 0, 255},
196 {wxT("MEDIUM SPRING GREEN"), 127, 255, 0},
197 {wxT("MEDIUM TURQUOISE"), 112, 219, 219},
198 {wxT("MEDIUM VIOLET RED"), 219, 112, 147},
199 {wxT("MIDNIGHT BLUE"), 47, 47, 79},
200 {wxT("NAVY"), 35, 35, 142},
201 {wxT("ORANGE"), 204, 50, 50},
202 {wxT("ORANGE RED"), 255, 0, 127},
203 {wxT("ORCHID"), 219, 112, 219},
204 {wxT("PALE GREEN"), 143, 188, 143},
205 {wxT("PINK"), 188, 143, 234},
206 {wxT("PLUM"), 234, 173, 234},
207 {wxT("PURPLE"), 176, 0, 255},
208 {wxT("RED"), 255, 0, 0},
209 {wxT("SALMON"), 111, 66, 66},
210 {wxT("SEA GREEN"), 35, 142, 107},
211 {wxT("SIENNA"), 142, 107, 35},
212 {wxT("SKY BLUE"), 50, 153, 204},
213 {wxT("SLATE BLUE"), 0, 127, 255},
214 {wxT("SPRING GREEN"), 0, 255, 127},
215 {wxT("STEEL BLUE"), 35, 107, 142},
216 {wxT("TAN"), 219, 147, 112},
217 {wxT("THISTLE"), 216, 191, 216},
218 {wxT("TURQUOISE"), 173, 234, 234},
219 {wxT("VIOLET"), 79, 47, 79},
220 {wxT("VIOLET RED"), 204, 50, 153},
221 {wxT("WHEAT"), 216, 216, 191},
222 {wxT("WHITE"), 255, 255, 255},
223 {wxT("YELLOW"), 255, 255, 0},
224 {wxT("YELLOW GREEN"), 153, 204, 50},
225 {wxT("MEDIUM GOLDENROD"), 234, 234, 173},
226 {wxT("MEDIUM FOREST GREEN"), 107, 142, 35},
227 {wxT("LIGHT MAGENTA"), 255, 0, 255},
228 {wxT("MEDIUM GREY"), 100, 100, 100},
233 for ( n
= 0; n
< WXSIZEOF(wxColourTable
); n
++ )
235 const wxColourDesc
& cc
= wxColourTable
[n
];
236 Append(cc
.name
, new wxColour(cc
.r
,cc
.g
,cc
.b
));
239 m_palTable
= new long[n
];
240 for ( n
= 0; n
< WXSIZEOF(wxColourTable
); n
++ )
242 const wxColourDesc
& cc
= wxColourTable
[n
];
243 m_palTable
[n
] = OS2RGB(cc
.r
,cc
.g
,cc
.b
);
250 * Changed by Ian Brown, July 1994.
252 * When running under X, the Colour Database starts off empty. The X server
253 * is queried for the colour first time after which it is entered into the
254 * database. This allows our client to use the server colour database which
255 * is hopefully gamma corrected for the display being used.
258 wxColour
*wxColourDatabase::FindColour(const wxString
& colour
)
260 // VZ: make the comparaison case insensitive and also match both grey and
262 wxString colName
= colour
;
264 wxString colName2
= colName
;
265 if ( !colName2
.Replace(_T("GRAY"), _T("GREY")) )
268 wxNode
*node
= First();
271 const wxChar
*key
= node
->GetKeyString();
272 if ( colName
== key
|| colName2
== key
)
274 return (wxColour
*)node
->Data();
287 // TODO for other implementations. This should really go into
288 // platform-specific directories.
297 wxColour
*col
= new wxColour( colour
);
301 return (wxColour
*) NULL
;
303 Append( colour
, col
);
311 Display
*display
= XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()) ;
314 Xv_Screen screen
= xv_get(xview_server
, SERVER_NTH_SCREEN
, 0);
315 Xv_opaque root_window
= xv_get(screen
, XV_ROOT
);
316 Display
*display
= (Display
*)xv_get(root_window
, XV_DISPLAY
);
319 /* MATTHEW: [4] Use wxGetMainColormap */
320 if (!XParseColor(display
, (Colormap
) wxTheApp
->GetMainColormap((WXDisplay
*) display
), colour
,&xcolour
))
323 unsigned char r
= (unsigned char)(xcolour
.red
>> 8);
324 unsigned char g
= (unsigned char)(xcolour
.green
>> 8);
325 unsigned char b
= (unsigned char)(xcolour
.blue
>> 8);
327 wxColour
*col
= new wxColour(r
, g
, b
);
334 wxString
wxColourDatabase::FindName (const wxColour
& colour
) const
338 unsigned char red
= colour
.Red ();
339 unsigned char green
= colour
.Green ();
340 unsigned char blue
= colour
.Blue ();
342 for (wxNode
* node
= First (); node
; node
= node
->Next ())
344 wxColour
*col
= (wxColour
*) node
->Data ();
346 if (col
->Red () == red
&& col
->Green () == green
&& col
->Blue () == blue
)
348 const wxChar
*found
= node
->GetKeyString();
361 void wxInitializeStockLists () {
362 wxTheBrushList
= new wxBrushList
;
363 wxThePenList
= new wxPenList
;
364 wxTheFontList
= new wxFontList
;
365 wxTheBitmapList
= new wxBitmapList
;
368 void wxInitializeStockObjects ()
374 // wxFontPool = new XFontPool;
377 // why under MSW fonts shouldn't have the standard system size?
380 static const int sizeFont = 10;
384 #if defined(__WXPM__) || defined(__WXMAC__)
385 static const int sizeFont
= 12;
386 wxNORMAL_FONT
= new wxFont (sizeFont
, wxMODERN
, wxNORMAL
, wxNORMAL
);
388 wxNORMAL_FONT
= new wxFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
389 static const int sizeFont
= wxNORMAL_FONT
->GetPointSize();
392 wxSMALL_FONT
= new wxFont (sizeFont
- 2, wxSWISS
, wxNORMAL
, wxNORMAL
);
393 wxITALIC_FONT
= new wxFont (sizeFont
, wxROMAN
, wxITALIC
, wxNORMAL
);
394 wxSWISS_FONT
= new wxFont (sizeFont
, wxSWISS
, wxNORMAL
, wxNORMAL
);
396 wxRED_PEN
= new wxPen ("RED", 1, wxSOLID
);
397 wxCYAN_PEN
= new wxPen ("CYAN", 1, wxSOLID
);
398 wxGREEN_PEN
= new wxPen ("GREEN", 1, wxSOLID
);
399 wxBLACK_PEN
= new wxPen ("BLACK", 1, wxSOLID
);
400 wxWHITE_PEN
= new wxPen ("WHITE", 1, wxSOLID
);
401 wxTRANSPARENT_PEN
= new wxPen ("BLACK", 1, wxTRANSPARENT
);
402 wxBLACK_DASHED_PEN
= new wxPen ("BLACK", 1, wxSHORT_DASH
);
403 wxGREY_PEN
= new wxPen ("GREY", 1, wxSOLID
);
404 wxMEDIUM_GREY_PEN
= new wxPen ("MEDIUM GREY", 1, wxSOLID
);
405 wxLIGHT_GREY_PEN
= new wxPen ("LIGHT GREY", 1, wxSOLID
);
407 wxBLUE_BRUSH
= new wxBrush ("BLUE", wxSOLID
);
408 wxGREEN_BRUSH
= new wxBrush ("GREEN", wxSOLID
);
409 wxWHITE_BRUSH
= new wxBrush ("WHITE", wxSOLID
);
410 wxBLACK_BRUSH
= new wxBrush ("BLACK", wxSOLID
);
411 wxTRANSPARENT_BRUSH
= new wxBrush ("BLACK", wxTRANSPARENT
);
412 wxCYAN_BRUSH
= new wxBrush ("CYAN", wxSOLID
);
413 wxRED_BRUSH
= new wxBrush ("RED", wxSOLID
);
414 wxGREY_BRUSH
= new wxBrush ("GREY", wxSOLID
);
415 wxMEDIUM_GREY_BRUSH
= new wxBrush ("MEDIUM GREY", wxSOLID
);
416 wxLIGHT_GREY_BRUSH
= new wxBrush ("LIGHT GREY", wxSOLID
);
418 wxBLACK
= new wxColour ("BLACK");
419 wxWHITE
= new wxColour ("WHITE");
420 wxRED
= new wxColour ("RED");
421 wxBLUE
= new wxColour ("BLUE");
422 wxGREEN
= new wxColour ("GREEN");
423 wxCYAN
= new wxColour ("CYAN");
424 wxLIGHT_GREY
= new wxColour ("LIGHT GREY");
426 wxSTANDARD_CURSOR
= new wxCursor (wxCURSOR_ARROW
);
427 wxHOURGLASS_CURSOR
= new wxCursor (wxCURSOR_WAIT
);
428 wxCROSS_CURSOR
= new wxCursor (wxCURSOR_CROSS
);
431 void wxDeleteStockObjects ()
433 wxDELETE(wxNORMAL_FONT
);
434 wxDELETE(wxSMALL_FONT
);
435 wxDELETE(wxITALIC_FONT
);
436 wxDELETE(wxSWISS_FONT
);
439 wxDELETE(wxCYAN_PEN
);
440 wxDELETE(wxGREEN_PEN
);
441 wxDELETE(wxBLACK_PEN
);
442 wxDELETE(wxWHITE_PEN
);
443 wxDELETE(wxTRANSPARENT_PEN
);
444 wxDELETE(wxBLACK_DASHED_PEN
);
445 wxDELETE(wxGREY_PEN
);
446 wxDELETE(wxMEDIUM_GREY_PEN
);
447 wxDELETE(wxLIGHT_GREY_PEN
);
449 wxDELETE(wxBLUE_BRUSH
);
450 wxDELETE(wxGREEN_BRUSH
);
451 wxDELETE(wxWHITE_BRUSH
);
452 wxDELETE(wxBLACK_BRUSH
);
453 wxDELETE(wxTRANSPARENT_BRUSH
);
454 wxDELETE(wxCYAN_BRUSH
);
455 wxDELETE(wxRED_BRUSH
);
456 wxDELETE(wxGREY_BRUSH
);
457 wxDELETE(wxMEDIUM_GREY_BRUSH
);
458 wxDELETE(wxLIGHT_GREY_BRUSH
);
466 wxDELETE(wxLIGHT_GREY
);
468 wxDELETE(wxSTANDARD_CURSOR
);
469 wxDELETE(wxHOURGLASS_CURSOR
);
470 wxDELETE(wxCROSS_CURSOR
);
473 void wxDeleteStockLists() {
474 wxDELETE(wxTheBrushList
);
475 wxDELETE(wxThePenList
);
476 wxDELETE(wxTheFontList
);
477 wxDELETE(wxTheBitmapList
);
480 wxBitmapList::wxBitmapList ()
484 wxBitmapList::~wxBitmapList ()
486 wxNode
*node
= First ();
489 wxBitmap
*bitmap
= (wxBitmap
*) node
->Data ();
490 wxNode
*next
= node
->Next ();
491 if (bitmap
->GetVisible())
497 // Pen and Brush lists
498 wxPenList::~wxPenList ()
500 wxNode
*node
= First ();
503 wxPen
*pen
= (wxPen
*) node
->Data ();
504 wxNode
*next
= node
->Next ();
505 if (pen
->GetVisible())
511 void wxPenList::AddPen (wxPen
* pen
)
516 void wxPenList::RemovePen (wxPen
* pen
)
521 wxPen
*wxPenList::FindOrCreatePen (const wxColour
& colour
, int width
, int style
)
523 for (wxNode
* node
= First (); node
; node
= node
->Next ())
525 wxPen
*each_pen
= (wxPen
*) node
->Data ();
527 each_pen
->GetVisible() &&
528 each_pen
->GetWidth () == width
&&
529 each_pen
->GetStyle () == style
&&
530 each_pen
->GetColour ().Red () == colour
.Red () &&
531 each_pen
->GetColour ().Green () == colour
.Green () &&
532 each_pen
->GetColour ().Blue () == colour
.Blue ())
536 wxPen
*pen
= new wxPen (colour
, width
, style
);
539 // don't save the invalid pens in the list
545 // Yes, we can return a pointer to this in a later FindOrCreatePen call,
546 // because we created it within FindOrCreatePen. Safeguards against
547 // returning a pointer to an automatic variable and hanging on to it
548 // (dangling pointer).
549 pen
->SetVisible(TRUE
);
554 wxBrushList::~wxBrushList ()
556 wxNode
*node
= First ();
559 wxBrush
*brush
= (wxBrush
*) node
->Data ();
560 wxNode
*next
= node
->Next ();
561 if (brush
->GetVisible())
567 void wxBrushList::AddBrush (wxBrush
* brush
)
572 wxBrush
*wxBrushList::FindOrCreateBrush (const wxColour
& colour
, int style
)
574 for (wxNode
* node
= First (); node
; node
= node
->Next ())
576 wxBrush
*each_brush
= (wxBrush
*) node
->Data ();
578 each_brush
->GetVisible() &&
579 each_brush
->GetStyle () == style
&&
580 each_brush
->GetColour ().Red () == colour
.Red () &&
581 each_brush
->GetColour ().Green () == colour
.Green () &&
582 each_brush
->GetColour ().Blue () == colour
.Blue ())
586 wxBrush
*brush
= new wxBrush (colour
, style
);
590 // don't put the brushes we failed to create into the list
596 brush
->SetVisible(TRUE
);
598 // Yes, we can return a pointer to this in a later FindOrCreateBrush call,
599 // because we created it within FindOrCreateBrush. Safeguards against
600 // returning a pointer to an automatic variable and hanging on to it
601 // (dangling pointer).
605 void wxBrushList::RemoveBrush (wxBrush
* brush
)
607 DeleteObject (brush
);
610 wxFontList::~wxFontList ()
612 wxNode
*node
= First ();
615 // Only delete objects that are 'visible', i.e.
616 // that have been created using FindOrCreate...,
617 // where the pointers are expected to be shared
618 // (and therefore not deleted by any one part of an app).
619 wxFont
*font
= (wxFont
*) node
->Data ();
620 wxNode
*next
= node
->Next ();
621 if (font
->GetVisible())
627 void wxFontList::AddFont (wxFont
* font
)
632 void wxFontList::RemoveFont (wxFont
* font
)
637 wxFont
*wxFontList::FindOrCreateFont(int pointSize
,
642 const wxString
& facename
,
643 wxFontEncoding encoding
)
645 wxFont
*font
= (wxFont
*)NULL
;
647 for ( node
= First(); node
; node
= node
->Next() )
649 font
= (wxFont
*)node
->Data();
650 if ( font
->GetVisible() &&
652 font
->GetPointSize () == pointSize
&&
653 font
->GetStyle () == style
&&
654 font
->GetWeight () == weight
&&
655 font
->GetUnderlined () == underline
)
657 int fontFamily
= font
->GetFamily();
659 #if defined(__WXGTK__)
660 // under GTK the default family is wxSWISS, so looking for a font
661 // with wxDEFAULT family should return a wxSWISS one instead of
662 // creating a new one
663 bool same
= (fontFamily
== family
) ||
664 (fontFamily
== wxSWISS
&& family
== wxDEFAULT
);
666 // VZ: but why elsewhere do we require an exact match? mystery...
667 bool same
= fontFamily
== family
;
670 // empty facename matches anything at all: this is bad because
671 // depending on which fonts are already created, we might get back
672 // a different font if we create it with empty facename, but it is
673 // still better than never matching anything in the cache at all
675 if ( same
&& !!facename
)
677 const wxString
& fontFace
= font
->GetFaceName();
679 // empty facename matches everything
680 same
= !fontFace
|| fontFace
== facename
;
683 if ( same
&& (encoding
!= wxFONTENCODING_DEFAULT
) )
685 // have to match the encoding too
686 same
= font
->GetEncoding() == encoding
;
698 // font not found, create the new one
699 font
= new wxFont(pointSize
, family
, style
, weight
,
700 underline
, facename
, encoding
);
702 // and mark it as being cacheable
703 font
->SetVisible(TRUE
);
709 void wxBitmapList::AddBitmap(wxBitmap
*bitmap
)
714 void wxBitmapList::RemoveBitmap(wxBitmap
*bitmap
)
716 DeleteObject(bitmap
);
719 wxSize
wxGetDisplaySize()
722 wxDisplaySize(& x
, & y
);
726 wxSize
wxGetDisplaySizeMM()
729 wxDisplaySizeMM(& x
, & y
);
733 wxResourceCache::~wxResourceCache ()
735 wxNode
*node
= First ();
737 wxObject
*item
= (wxObject
*)node
->Data();
740 node
= node
->Next ();