]>
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 wxRect
& wxRect::Inflate(wxCoord dx
, wxCoord dy
)
123 // check that we didn't make the rectangle invalid by accident (you almost
124 // never want to have negative coords and never want negative size)
125 wxASSERT_MSG( x
>= 0 && y
>= 0 && width
>= 0 && height
>= 0,
126 _T("wxRect::Inflate() resulted in an invalid rectangle!") );
131 bool wxRect::Inside(int cx
, int cy
) const
133 return ( (cx
>= x
) && (cy
>= y
)
134 && ((cy
- y
) < height
)
135 && ((cx
- x
) < width
)
139 wxRect
& wxRect::Intersect(const wxRect
& rect
)
148 if ( x2
> rect
.GetRight() )
149 x2
= rect
.GetRight();
150 if ( y2
> rect
.GetBottom() )
151 y2
= rect
.GetBottom();
156 if ( width
<= 0 || height
<= 0 )
165 bool wxRect::Intersects(const wxRect
& rect
) const
167 wxRect r
= Intersect(rect
);
169 // if there is no intersection, both width and height are 0
173 wxColourDatabase::wxColourDatabase (int type
) : wxList (type
)
177 wxColourDatabase::~wxColourDatabase ()
179 // Cleanup Colour allocated in Initialize()
180 wxNode
*node
= First ();
183 wxColour
*col
= (wxColour
*) node
->Data ();
184 wxNode
*next
= node
->Next ();
189 delete [] m_palTable
;
193 // Colour database stuff
194 void wxColourDatabase::Initialize ()
196 static const struct wxColourDesc
203 {wxT("AQUAMARINE"),112, 219, 147},
204 {wxT("BLACK"),0, 0, 0},
205 {wxT("BLUE"), 0, 0, 255},
206 {wxT("BLUE VIOLET"), 159, 95, 159},
207 {wxT("BROWN"), 165, 42, 42},
208 {wxT("CADET BLUE"), 95, 159, 159},
209 {wxT("CORAL"), 255, 127, 0},
210 {wxT("CORNFLOWER BLUE"), 66, 66, 111},
211 {wxT("CYAN"), 0, 255, 255},
212 {wxT("DARK GREY"), 47, 47, 47}, // ?
214 {wxT("DARK GREEN"), 47, 79, 47},
215 {wxT("DARK OLIVE GREEN"), 79, 79, 47},
216 {wxT("DARK ORCHID"), 153, 50, 204},
217 {wxT("DARK SLATE BLUE"), 107, 35, 142},
218 {wxT("DARK SLATE GREY"), 47, 79, 79},
219 {wxT("DARK TURQUOISE"), 112, 147, 219},
220 {wxT("DIM GREY"), 84, 84, 84},
221 {wxT("FIREBRICK"), 142, 35, 35},
222 {wxT("FOREST GREEN"), 35, 142, 35},
223 {wxT("GOLD"), 204, 127, 50},
224 {wxT("GOLDENROD"), 219, 219, 112},
225 {wxT("GREY"), 128, 128, 128},
226 {wxT("GREEN"), 0, 255, 0},
227 {wxT("GREEN YELLOW"), 147, 219, 112},
228 {wxT("INDIAN RED"), 79, 47, 47},
229 {wxT("KHAKI"), 159, 159, 95},
230 {wxT("LIGHT BLUE"), 191, 216, 216},
231 {wxT("LIGHT GREY"), 192, 192, 192},
232 {wxT("LIGHT STEEL BLUE"), 143, 143, 188},
233 {wxT("LIME GREEN"), 50, 204, 50},
234 {wxT("LIGHT MAGENTA"), 255, 0, 255},
235 {wxT("MAGENTA"), 255, 0, 255},
236 {wxT("MAROON"), 142, 35, 107},
237 {wxT("MEDIUM AQUAMARINE"), 50, 204, 153},
238 {wxT("MEDIUM GREY"), 100, 100, 100},
239 {wxT("MEDIUM BLUE"), 50, 50, 204},
240 {wxT("MEDIUM FOREST GREEN"), 107, 142, 35},
241 {wxT("MEDIUM GOLDENROD"), 234, 234, 173},
242 {wxT("MEDIUM ORCHID"), 147, 112, 219},
243 {wxT("MEDIUM SEA GREEN"), 66, 111, 66},
244 {wxT("MEDIUM SLATE BLUE"), 127, 0, 255},
245 {wxT("MEDIUM SPRING GREEN"), 127, 255, 0},
246 {wxT("MEDIUM TURQUOISE"), 112, 219, 219},
247 {wxT("MEDIUM VIOLET RED"), 219, 112, 147},
248 {wxT("MIDNIGHT BLUE"), 47, 47, 79},
249 {wxT("NAVY"), 35, 35, 142},
250 {wxT("ORANGE"), 204, 50, 50},
251 {wxT("ORANGE RED"), 255, 0, 127},
252 {wxT("ORCHID"), 219, 112, 219},
253 {wxT("PALE GREEN"), 143, 188, 143},
254 {wxT("PINK"), 188, 143, 234},
255 {wxT("PLUM"), 234, 173, 234},
256 {wxT("PURPLE"), 176, 0, 255},
257 {wxT("RED"), 255, 0, 0},
258 {wxT("SALMON"), 111, 66, 66},
259 {wxT("SEA GREEN"), 35, 142, 107},
260 {wxT("SIENNA"), 142, 107, 35},
261 {wxT("SKY BLUE"), 50, 153, 204},
262 {wxT("SLATE BLUE"), 0, 127, 255},
263 {wxT("SPRING GREEN"), 0, 255, 127},
264 {wxT("STEEL BLUE"), 35, 107, 142},
265 {wxT("TAN"), 219, 147, 112},
266 {wxT("THISTLE"), 216, 191, 216},
267 {wxT("TURQUOISE"), 173, 234, 234},
268 {wxT("VIOLET"), 79, 47, 79},
269 {wxT("VIOLET RED"), 204, 50, 153},
270 {wxT("WHEAT"), 216, 216, 191},
271 {wxT("WHITE"), 255, 255, 255},
272 {wxT("YELLOW"), 255, 255, 0},
273 {wxT("YELLOW GREEN"), 153, 204, 50},
274 {wxT("MEDIUM GOLDENROD"), 234, 234, 173},
275 {wxT("MEDIUM FOREST GREEN"), 107, 142, 35},
276 {wxT("LIGHT MAGENTA"), 255, 0, 255},
277 {wxT("MEDIUM GREY"), 100, 100, 100},
282 for ( n
= 0; n
< WXSIZEOF(wxColourTable
); n
++ )
284 const wxColourDesc
& cc
= wxColourTable
[n
];
285 Append(cc
.name
, new wxColour(cc
.r
,cc
.g
,cc
.b
));
288 m_palTable
= new long[n
];
289 for ( n
= 0; n
< WXSIZEOF(wxColourTable
); n
++ )
291 const wxColourDesc
& cc
= wxColourTable
[n
];
292 m_palTable
[n
] = OS2RGB(cc
.r
,cc
.g
,cc
.b
);
299 * Changed by Ian Brown, July 1994.
301 * When running under X, the Colour Database starts off empty. The X server
302 * is queried for the colour first time after which it is entered into the
303 * database. This allows our client to use the server colour database which
304 * is hopefully gamma corrected for the display being used.
307 wxColour
*wxColourDatabase::FindColour(const wxString
& colour
)
309 // VZ: make the comparaison case insensitive and also match both grey and
311 wxString colName
= colour
;
313 wxString colName2
= colName
;
314 if ( !colName2
.Replace(_T("GRAY"), _T("GREY")) )
317 wxNode
*node
= First();
320 const wxChar
*key
= node
->GetKeyString();
321 if ( colName
== key
|| colName2
== key
)
323 return (wxColour
*)node
->Data();
339 // TODO for other implementations. This should really go into
340 // platform-specific directories.
349 wxColour
*col
= new wxColour( colour
);
353 return (wxColour
*) NULL
;
355 Append( colour
, col
);
363 Display
*display
= XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()) ;
366 Xv_Screen screen
= xv_get(xview_server
, SERVER_NTH_SCREEN
, 0);
367 Xv_opaque root_window
= xv_get(screen
, XV_ROOT
);
368 Display
*display
= (Display
*)xv_get(root_window
, XV_DISPLAY
);
371 /* MATTHEW: [4] Use wxGetMainColormap */
372 if (!XParseColor(display
, (Colormap
) wxTheApp
->GetMainColormap((WXDisplay
*) display
), colour
,&xcolour
))
375 unsigned char r
= (unsigned char)(xcolour
.red
>> 8);
376 unsigned char g
= (unsigned char)(xcolour
.green
>> 8);
377 unsigned char b
= (unsigned char)(xcolour
.blue
>> 8);
379 wxColour
*col
= new wxColour(r
, g
, b
);
386 wxString
wxColourDatabase::FindName (const wxColour
& colour
) const
390 unsigned char red
= colour
.Red ();
391 unsigned char green
= colour
.Green ();
392 unsigned char blue
= colour
.Blue ();
394 for (wxNode
* node
= First (); node
; node
= node
->Next ())
396 wxColour
*col
= (wxColour
*) node
->Data ();
398 if (col
->Red () == red
&& col
->Green () == green
&& col
->Blue () == blue
)
400 const wxChar
*found
= node
->GetKeyString();
413 void wxInitializeStockLists () {
414 wxTheBrushList
= new wxBrushList
;
415 wxThePenList
= new wxPenList
;
416 wxTheFontList
= new wxFontList
;
417 wxTheBitmapList
= new wxBitmapList
;
420 void wxInitializeStockObjects ()
426 // wxFontPool = new XFontPool;
429 // why under MSW fonts shouldn't have the standard system size?
432 static const int sizeFont = 10;
436 #if defined(__WXPM__) || defined(__WXMAC__)
437 static const int sizeFont
= 12;
438 wxNORMAL_FONT
= new wxFont (sizeFont
, wxMODERN
, wxNORMAL
, wxNORMAL
);
440 wxNORMAL_FONT
= new wxFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
441 static const int sizeFont
= wxNORMAL_FONT
->GetPointSize();
444 wxSMALL_FONT
= new wxFont (sizeFont
- 2, wxSWISS
, wxNORMAL
, wxNORMAL
);
445 wxITALIC_FONT
= new wxFont (sizeFont
, wxROMAN
, wxITALIC
, wxNORMAL
);
446 wxSWISS_FONT
= new wxFont (sizeFont
, wxSWISS
, wxNORMAL
, wxNORMAL
);
448 wxRED_PEN
= new wxPen ("RED", 1, wxSOLID
);
449 wxCYAN_PEN
= new wxPen ("CYAN", 1, wxSOLID
);
450 wxGREEN_PEN
= new wxPen ("GREEN", 1, wxSOLID
);
451 wxBLACK_PEN
= new wxPen ("BLACK", 1, wxSOLID
);
452 wxWHITE_PEN
= new wxPen ("WHITE", 1, wxSOLID
);
453 wxTRANSPARENT_PEN
= new wxPen ("BLACK", 1, wxTRANSPARENT
);
454 wxBLACK_DASHED_PEN
= new wxPen ("BLACK", 1, wxSHORT_DASH
);
455 wxGREY_PEN
= new wxPen ("GREY", 1, wxSOLID
);
456 wxMEDIUM_GREY_PEN
= new wxPen ("MEDIUM GREY", 1, wxSOLID
);
457 wxLIGHT_GREY_PEN
= new wxPen ("LIGHT GREY", 1, wxSOLID
);
459 wxBLUE_BRUSH
= new wxBrush ("BLUE", wxSOLID
);
460 wxGREEN_BRUSH
= new wxBrush ("GREEN", wxSOLID
);
461 wxWHITE_BRUSH
= new wxBrush ("WHITE", wxSOLID
);
462 wxBLACK_BRUSH
= new wxBrush ("BLACK", wxSOLID
);
463 wxTRANSPARENT_BRUSH
= new wxBrush ("BLACK", wxTRANSPARENT
);
464 wxCYAN_BRUSH
= new wxBrush ("CYAN", wxSOLID
);
465 wxRED_BRUSH
= new wxBrush ("RED", wxSOLID
);
466 wxGREY_BRUSH
= new wxBrush ("GREY", wxSOLID
);
467 wxMEDIUM_GREY_BRUSH
= new wxBrush ("MEDIUM GREY", wxSOLID
);
468 wxLIGHT_GREY_BRUSH
= new wxBrush ("LIGHT GREY", wxSOLID
);
470 wxBLACK
= new wxColour ("BLACK");
471 wxWHITE
= new wxColour ("WHITE");
472 wxRED
= new wxColour ("RED");
473 wxBLUE
= new wxColour ("BLUE");
474 wxGREEN
= new wxColour ("GREEN");
475 wxCYAN
= new wxColour ("CYAN");
476 wxLIGHT_GREY
= new wxColour ("LIGHT GREY");
478 wxSTANDARD_CURSOR
= new wxCursor (wxCURSOR_ARROW
);
479 wxHOURGLASS_CURSOR
= new wxCursor (wxCURSOR_WAIT
);
480 wxCROSS_CURSOR
= new wxCursor (wxCURSOR_CROSS
);
483 void wxDeleteStockObjects ()
485 wxDELETE(wxNORMAL_FONT
);
486 wxDELETE(wxSMALL_FONT
);
487 wxDELETE(wxITALIC_FONT
);
488 wxDELETE(wxSWISS_FONT
);
491 wxDELETE(wxCYAN_PEN
);
492 wxDELETE(wxGREEN_PEN
);
493 wxDELETE(wxBLACK_PEN
);
494 wxDELETE(wxWHITE_PEN
);
495 wxDELETE(wxTRANSPARENT_PEN
);
496 wxDELETE(wxBLACK_DASHED_PEN
);
497 wxDELETE(wxGREY_PEN
);
498 wxDELETE(wxMEDIUM_GREY_PEN
);
499 wxDELETE(wxLIGHT_GREY_PEN
);
501 wxDELETE(wxBLUE_BRUSH
);
502 wxDELETE(wxGREEN_BRUSH
);
503 wxDELETE(wxWHITE_BRUSH
);
504 wxDELETE(wxBLACK_BRUSH
);
505 wxDELETE(wxTRANSPARENT_BRUSH
);
506 wxDELETE(wxCYAN_BRUSH
);
507 wxDELETE(wxRED_BRUSH
);
508 wxDELETE(wxGREY_BRUSH
);
509 wxDELETE(wxMEDIUM_GREY_BRUSH
);
510 wxDELETE(wxLIGHT_GREY_BRUSH
);
518 wxDELETE(wxLIGHT_GREY
);
520 wxDELETE(wxSTANDARD_CURSOR
);
521 wxDELETE(wxHOURGLASS_CURSOR
);
522 wxDELETE(wxCROSS_CURSOR
);
525 void wxDeleteStockLists() {
526 wxDELETE(wxTheBrushList
);
527 wxDELETE(wxThePenList
);
528 wxDELETE(wxTheFontList
);
529 wxDELETE(wxTheBitmapList
);
532 wxBitmapList::wxBitmapList ()
536 wxBitmapList::~wxBitmapList ()
538 wxNode
*node
= First ();
541 wxBitmap
*bitmap
= (wxBitmap
*) node
->Data ();
542 wxNode
*next
= node
->Next ();
543 if (bitmap
->GetVisible())
549 // Pen and Brush lists
550 wxPenList::~wxPenList ()
552 wxNode
*node
= First ();
555 wxPen
*pen
= (wxPen
*) node
->Data ();
556 wxNode
*next
= node
->Next ();
557 if (pen
->GetVisible())
563 void wxPenList::AddPen (wxPen
* pen
)
568 void wxPenList::RemovePen (wxPen
* pen
)
573 wxPen
*wxPenList::FindOrCreatePen (const wxColour
& colour
, int width
, int style
)
575 for (wxNode
* node
= First (); node
; node
= node
->Next ())
577 wxPen
*each_pen
= (wxPen
*) node
->Data ();
579 each_pen
->GetVisible() &&
580 each_pen
->GetWidth () == width
&&
581 each_pen
->GetStyle () == style
&&
582 each_pen
->GetColour ().Red () == colour
.Red () &&
583 each_pen
->GetColour ().Green () == colour
.Green () &&
584 each_pen
->GetColour ().Blue () == colour
.Blue ())
588 wxPen
*pen
= new wxPen (colour
, width
, style
);
591 // don't save the invalid pens in the list
597 // Yes, we can return a pointer to this in a later FindOrCreatePen call,
598 // because we created it within FindOrCreatePen. Safeguards against
599 // returning a pointer to an automatic variable and hanging on to it
600 // (dangling pointer).
601 pen
->SetVisible(TRUE
);
606 wxBrushList::~wxBrushList ()
608 wxNode
*node
= First ();
611 wxBrush
*brush
= (wxBrush
*) node
->Data ();
612 wxNode
*next
= node
->Next ();
613 if (brush
->GetVisible())
619 void wxBrushList::AddBrush (wxBrush
* brush
)
624 wxBrush
*wxBrushList::FindOrCreateBrush (const wxColour
& colour
, int style
)
626 for (wxNode
* node
= First (); node
; node
= node
->Next ())
628 wxBrush
*each_brush
= (wxBrush
*) node
->Data ();
630 each_brush
->GetVisible() &&
631 each_brush
->GetStyle () == style
&&
632 each_brush
->GetColour ().Red () == colour
.Red () &&
633 each_brush
->GetColour ().Green () == colour
.Green () &&
634 each_brush
->GetColour ().Blue () == colour
.Blue ())
638 wxBrush
*brush
= new wxBrush (colour
, style
);
642 // don't put the brushes we failed to create into the list
648 brush
->SetVisible(TRUE
);
650 // Yes, we can return a pointer to this in a later FindOrCreateBrush call,
651 // because we created it within FindOrCreateBrush. Safeguards against
652 // returning a pointer to an automatic variable and hanging on to it
653 // (dangling pointer).
657 void wxBrushList::RemoveBrush (wxBrush
* brush
)
659 DeleteObject (brush
);
662 wxFontList::~wxFontList ()
664 wxNode
*node
= First ();
667 // Only delete objects that are 'visible', i.e.
668 // that have been created using FindOrCreate...,
669 // where the pointers are expected to be shared
670 // (and therefore not deleted by any one part of an app).
671 wxFont
*font
= (wxFont
*) node
->Data ();
672 wxNode
*next
= node
->Next ();
673 if (font
->GetVisible())
679 void wxFontList::AddFont (wxFont
* font
)
684 void wxFontList::RemoveFont (wxFont
* font
)
689 wxFont
*wxFontList::FindOrCreateFont(int pointSize
,
694 const wxString
& facename
,
695 wxFontEncoding encoding
)
697 wxFont
*font
= (wxFont
*)NULL
;
699 for ( node
= First(); node
; node
= node
->Next() )
701 font
= (wxFont
*)node
->Data();
702 if ( font
->GetVisible() &&
704 font
->GetPointSize () == pointSize
&&
705 font
->GetStyle () == style
&&
706 font
->GetWeight () == weight
&&
707 font
->GetUnderlined () == underline
)
709 int fontFamily
= font
->GetFamily();
711 #if defined(__WXGTK__)
712 // under GTK the default family is wxSWISS, so looking for a font
713 // with wxDEFAULT family should return a wxSWISS one instead of
714 // creating a new one
715 bool same
= (fontFamily
== family
) ||
716 (fontFamily
== wxSWISS
&& family
== wxDEFAULT
);
718 // VZ: but why elsewhere do we require an exact match? mystery...
719 bool same
= fontFamily
== family
;
722 // empty facename matches anything at all: this is bad because
723 // depending on which fonts are already created, we might get back
724 // a different font if we create it with empty facename, but it is
725 // still better than never matching anything in the cache at all
727 if ( same
&& !!facename
)
729 const wxString
& fontFace
= font
->GetFaceName();
731 // empty facename matches everything
732 same
= !fontFace
|| fontFace
== facename
;
735 if ( same
&& (encoding
!= wxFONTENCODING_DEFAULT
) )
737 // have to match the encoding too
738 same
= font
->GetEncoding() == encoding
;
750 // font not found, create the new one
751 font
= new wxFont(pointSize
, family
, style
, weight
,
752 underline
, facename
, encoding
);
754 // and mark it as being cacheable
755 font
->SetVisible(TRUE
);
761 void wxBitmapList::AddBitmap(wxBitmap
*bitmap
)
766 void wxBitmapList::RemoveBitmap(wxBitmap
*bitmap
)
768 DeleteObject(bitmap
);
771 wxSize
wxGetDisplaySize()
774 wxDisplaySize(& x
, & y
);
778 wxRect
wxGetClientDisplayRect()
780 int x
, y
, width
, height
;
781 wxClientDisplayRect(&x
, &y
, &width
, &height
); // call plat-specific version
782 return wxRect(x
, y
, width
, height
);
785 wxSize
wxGetDisplaySizeMM()
788 wxDisplaySizeMM(& x
, & y
);
792 wxResourceCache::~wxResourceCache ()
794 wxNode
*node
= First ();
796 wxObject
*item
= (wxObject
*)node
->Data();
799 node
= node
->Next ();