]>
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)
130 // what else can we do?
139 bool wxRect::Inside(int cx
, int cy
) const
141 return ( (cx
>= x
) && (cy
>= y
)
142 && ((cy
- y
) < height
)
143 && ((cx
- x
) < width
)
147 wxRect
& wxRect::Intersect(const wxRect
& rect
)
156 if ( x2
> rect
.GetRight() )
157 x2
= rect
.GetRight();
158 if ( y2
> rect
.GetBottom() )
159 y2
= rect
.GetBottom();
164 if ( width
<= 0 || height
<= 0 )
173 bool wxRect::Intersects(const wxRect
& rect
) const
175 wxRect r
= Intersect(rect
);
177 // if there is no intersection, both width and height are 0
181 wxColourDatabase::wxColourDatabase (int type
) : wxList (type
)
185 wxColourDatabase::~wxColourDatabase ()
187 // Cleanup Colour allocated in Initialize()
188 wxNode
*node
= First ();
191 wxColour
*col
= (wxColour
*) node
->Data ();
192 wxNode
*next
= node
->Next ();
197 delete [] m_palTable
;
201 // Colour database stuff
202 void wxColourDatabase::Initialize ()
204 static const struct wxColourDesc
211 {wxT("AQUAMARINE"),112, 219, 147},
212 {wxT("BLACK"),0, 0, 0},
213 {wxT("BLUE"), 0, 0, 255},
214 {wxT("BLUE VIOLET"), 159, 95, 159},
215 {wxT("BROWN"), 165, 42, 42},
216 {wxT("CADET BLUE"), 95, 159, 159},
217 {wxT("CORAL"), 255, 127, 0},
218 {wxT("CORNFLOWER BLUE"), 66, 66, 111},
219 {wxT("CYAN"), 0, 255, 255},
220 {wxT("DARK GREY"), 47, 47, 47}, // ?
222 {wxT("DARK GREEN"), 47, 79, 47},
223 {wxT("DARK OLIVE GREEN"), 79, 79, 47},
224 {wxT("DARK ORCHID"), 153, 50, 204},
225 {wxT("DARK SLATE BLUE"), 107, 35, 142},
226 {wxT("DARK SLATE GREY"), 47, 79, 79},
227 {wxT("DARK TURQUOISE"), 112, 147, 219},
228 {wxT("DIM GREY"), 84, 84, 84},
229 {wxT("FIREBRICK"), 142, 35, 35},
230 {wxT("FOREST GREEN"), 35, 142, 35},
231 {wxT("GOLD"), 204, 127, 50},
232 {wxT("GOLDENROD"), 219, 219, 112},
233 {wxT("GREY"), 128, 128, 128},
234 {wxT("GREEN"), 0, 255, 0},
235 {wxT("GREEN YELLOW"), 147, 219, 112},
236 {wxT("INDIAN RED"), 79, 47, 47},
237 {wxT("KHAKI"), 159, 159, 95},
238 {wxT("LIGHT BLUE"), 191, 216, 216},
239 {wxT("LIGHT GREY"), 192, 192, 192},
240 {wxT("LIGHT STEEL BLUE"), 143, 143, 188},
241 {wxT("LIME GREEN"), 50, 204, 50},
242 {wxT("LIGHT MAGENTA"), 255, 0, 255},
243 {wxT("MAGENTA"), 255, 0, 255},
244 {wxT("MAROON"), 142, 35, 107},
245 {wxT("MEDIUM AQUAMARINE"), 50, 204, 153},
246 {wxT("MEDIUM GREY"), 100, 100, 100},
247 {wxT("MEDIUM BLUE"), 50, 50, 204},
248 {wxT("MEDIUM FOREST GREEN"), 107, 142, 35},
249 {wxT("MEDIUM GOLDENROD"), 234, 234, 173},
250 {wxT("MEDIUM ORCHID"), 147, 112, 219},
251 {wxT("MEDIUM SEA GREEN"), 66, 111, 66},
252 {wxT("MEDIUM SLATE BLUE"), 127, 0, 255},
253 {wxT("MEDIUM SPRING GREEN"), 127, 255, 0},
254 {wxT("MEDIUM TURQUOISE"), 112, 219, 219},
255 {wxT("MEDIUM VIOLET RED"), 219, 112, 147},
256 {wxT("MIDNIGHT BLUE"), 47, 47, 79},
257 {wxT("NAVY"), 35, 35, 142},
258 {wxT("ORANGE"), 204, 50, 50},
259 {wxT("ORANGE RED"), 255, 0, 127},
260 {wxT("ORCHID"), 219, 112, 219},
261 {wxT("PALE GREEN"), 143, 188, 143},
262 {wxT("PINK"), 188, 143, 234},
263 {wxT("PLUM"), 234, 173, 234},
264 {wxT("PURPLE"), 176, 0, 255},
265 {wxT("RED"), 255, 0, 0},
266 {wxT("SALMON"), 111, 66, 66},
267 {wxT("SEA GREEN"), 35, 142, 107},
268 {wxT("SIENNA"), 142, 107, 35},
269 {wxT("SKY BLUE"), 50, 153, 204},
270 {wxT("SLATE BLUE"), 0, 127, 255},
271 {wxT("SPRING GREEN"), 0, 255, 127},
272 {wxT("STEEL BLUE"), 35, 107, 142},
273 {wxT("TAN"), 219, 147, 112},
274 {wxT("THISTLE"), 216, 191, 216},
275 {wxT("TURQUOISE"), 173, 234, 234},
276 {wxT("VIOLET"), 79, 47, 79},
277 {wxT("VIOLET RED"), 204, 50, 153},
278 {wxT("WHEAT"), 216, 216, 191},
279 {wxT("WHITE"), 255, 255, 255},
280 {wxT("YELLOW"), 255, 255, 0},
281 {wxT("YELLOW GREEN"), 153, 204, 50},
282 {wxT("MEDIUM GOLDENROD"), 234, 234, 173},
283 {wxT("MEDIUM FOREST GREEN"), 107, 142, 35},
284 {wxT("LIGHT MAGENTA"), 255, 0, 255},
285 {wxT("MEDIUM GREY"), 100, 100, 100},
290 for ( n
= 0; n
< WXSIZEOF(wxColourTable
); n
++ )
292 const wxColourDesc
& cc
= wxColourTable
[n
];
293 Append(cc
.name
, new wxColour(cc
.r
,cc
.g
,cc
.b
));
296 m_palTable
= new long[n
];
297 for ( n
= 0; n
< WXSIZEOF(wxColourTable
); n
++ )
299 const wxColourDesc
& cc
= wxColourTable
[n
];
300 m_palTable
[n
] = OS2RGB(cc
.r
,cc
.g
,cc
.b
);
307 * Changed by Ian Brown, July 1994.
309 * When running under X, the Colour Database starts off empty. The X server
310 * is queried for the colour first time after which it is entered into the
311 * database. This allows our client to use the server colour database which
312 * is hopefully gamma corrected for the display being used.
315 wxColour
*wxColourDatabase::FindColour(const wxString
& colour
)
317 // VZ: make the comparaison case insensitive and also match both grey and
319 wxString colName
= colour
;
321 wxString colName2
= colName
;
322 if ( !colName2
.Replace(_T("GRAY"), _T("GREY")) )
325 wxNode
*node
= First();
328 const wxChar
*key
= node
->GetKeyString();
329 if ( colName
== key
|| colName2
== key
)
331 return (wxColour
*)node
->Data();
347 // TODO for other implementations. This should really go into
348 // platform-specific directories.
357 wxColour
*col
= new wxColour( colour
);
361 return (wxColour
*) NULL
;
363 Append( colour
, col
);
371 Display
*display
= XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()) ;
374 Xv_Screen screen
= xv_get(xview_server
, SERVER_NTH_SCREEN
, 0);
375 Xv_opaque root_window
= xv_get(screen
, XV_ROOT
);
376 Display
*display
= (Display
*)xv_get(root_window
, XV_DISPLAY
);
379 /* MATTHEW: [4] Use wxGetMainColormap */
380 if (!XParseColor(display
, (Colormap
) wxTheApp
->GetMainColormap((WXDisplay
*) display
), colour
,&xcolour
))
383 unsigned char r
= (unsigned char)(xcolour
.red
>> 8);
384 unsigned char g
= (unsigned char)(xcolour
.green
>> 8);
385 unsigned char b
= (unsigned char)(xcolour
.blue
>> 8);
387 wxColour
*col
= new wxColour(r
, g
, b
);
394 wxString
wxColourDatabase::FindName (const wxColour
& colour
) const
398 unsigned char red
= colour
.Red ();
399 unsigned char green
= colour
.Green ();
400 unsigned char blue
= colour
.Blue ();
402 for (wxNode
* node
= First (); node
; node
= node
->Next ())
404 wxColour
*col
= (wxColour
*) node
->Data ();
406 if (col
->Red () == red
&& col
->Green () == green
&& col
->Blue () == blue
)
408 const wxChar
*found
= node
->GetKeyString();
421 void wxInitializeStockLists()
423 wxTheBrushList
= new wxBrushList
;
424 wxThePenList
= new wxPenList
;
425 wxTheFontList
= new wxFontList
;
426 wxTheBitmapList
= new wxBitmapList
;
429 void wxInitializeStockObjects ()
435 // wxFontPool = new XFontPool;
438 // why under MSW fonts shouldn't have the standard system size?
441 static const int sizeFont = 10;
445 #if defined(__WXMAC__)
446 static const int sizeFont
= 12;
447 wxNORMAL_FONT
= new wxFont (sizeFont
, wxMODERN
, wxNORMAL
, wxNORMAL
);
448 #elif defined(__WXPM__)
449 static const int sizeFont
= 12;
451 wxNORMAL_FONT
= new wxFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
452 static const int sizeFont
= wxNORMAL_FONT
->GetPointSize();
455 #if defined(__WXPM__)
457 // Basic OS/2 has a fairly limited number of fonts and these are as good
458 // as I can do to get something that looks halfway "wx" normal
460 wxNORMAL_FONT
= new wxFont (sizeFont
, wxMODERN
, wxNORMAL
, wxNORMAL
); /* System VIO */
461 wxSMALL_FONT
= new wxFont (sizeFont
- 4, wxMODERN
, wxNORMAL
, wxNORMAL
); /* System VIO */
462 wxITALIC_FONT
= new wxFont (sizeFont
, wxROMAN
, wxITALIC
, wxNORMAL
);
463 wxSWISS_FONT
= new wxFont (sizeFont
, wxSWISS
, wxNORMAL
, wxNORMAL
); /* Helv */
465 wxSMALL_FONT
= new wxFont (sizeFont
- 2, wxSWISS
, wxNORMAL
, wxNORMAL
);
466 wxITALIC_FONT
= new wxFont (sizeFont
, wxROMAN
, wxITALIC
, wxNORMAL
);
467 wxSWISS_FONT
= new wxFont (sizeFont
, wxSWISS
, wxNORMAL
, wxNORMAL
);
470 wxRED_PEN
= new wxPen (wxT("RED"), 1, wxSOLID
);
471 wxCYAN_PEN
= new wxPen (wxT("CYAN"), 1, wxSOLID
);
472 wxGREEN_PEN
= new wxPen (wxT("GREEN"), 1, wxSOLID
);
473 wxBLACK_PEN
= new wxPen (wxT("BLACK"), 1, wxSOLID
);
474 wxWHITE_PEN
= new wxPen (wxT("WHITE"), 1, wxSOLID
);
475 wxTRANSPARENT_PEN
= new wxPen (wxT("BLACK"), 1, wxTRANSPARENT
);
476 wxBLACK_DASHED_PEN
= new wxPen (wxT("BLACK"), 1, wxSHORT_DASH
);
477 wxGREY_PEN
= new wxPen (wxT("GREY"), 1, wxSOLID
);
478 wxMEDIUM_GREY_PEN
= new wxPen (wxT("MEDIUM GREY"), 1, wxSOLID
);
479 wxLIGHT_GREY_PEN
= new wxPen (wxT("LIGHT GREY"), 1, wxSOLID
);
481 wxBLUE_BRUSH
= new wxBrush (wxT("BLUE"), wxSOLID
);
482 wxGREEN_BRUSH
= new wxBrush (wxT("GREEN"), wxSOLID
);
483 wxWHITE_BRUSH
= new wxBrush (wxT("WHITE"), wxSOLID
);
484 wxBLACK_BRUSH
= new wxBrush (wxT("BLACK"), wxSOLID
);
485 wxTRANSPARENT_BRUSH
= new wxBrush (wxT("BLACK"), wxTRANSPARENT
);
486 wxCYAN_BRUSH
= new wxBrush (wxT("CYAN"), wxSOLID
);
487 wxRED_BRUSH
= new wxBrush (wxT("RED"), wxSOLID
);
488 wxGREY_BRUSH
= new wxBrush (wxT("GREY"), wxSOLID
);
489 wxMEDIUM_GREY_BRUSH
= new wxBrush (wxT("MEDIUM GREY"), wxSOLID
);
490 wxLIGHT_GREY_BRUSH
= new wxBrush (wxT("LIGHT GREY"), wxSOLID
);
492 wxBLACK
= new wxColour (wxT("BLACK"));
493 wxWHITE
= new wxColour (wxT("WHITE"));
494 wxRED
= new wxColour (wxT("RED"));
495 wxBLUE
= new wxColour (wxT("BLUE"));
496 wxGREEN
= new wxColour (wxT("GREEN"));
497 wxCYAN
= new wxColour (wxT("CYAN"));
498 wxLIGHT_GREY
= new wxColour (wxT("LIGHT GREY"));
500 wxSTANDARD_CURSOR
= new wxCursor (wxCURSOR_ARROW
);
501 wxHOURGLASS_CURSOR
= new wxCursor (wxCURSOR_WAIT
);
502 wxCROSS_CURSOR
= new wxCursor (wxCURSOR_CROSS
);
505 void wxDeleteStockObjects ()
507 wxDELETE(wxNORMAL_FONT
);
508 wxDELETE(wxSMALL_FONT
);
509 wxDELETE(wxITALIC_FONT
);
510 wxDELETE(wxSWISS_FONT
);
513 wxDELETE(wxCYAN_PEN
);
514 wxDELETE(wxGREEN_PEN
);
515 wxDELETE(wxBLACK_PEN
);
516 wxDELETE(wxWHITE_PEN
);
517 wxDELETE(wxTRANSPARENT_PEN
);
518 wxDELETE(wxBLACK_DASHED_PEN
);
519 wxDELETE(wxGREY_PEN
);
520 wxDELETE(wxMEDIUM_GREY_PEN
);
521 wxDELETE(wxLIGHT_GREY_PEN
);
523 wxDELETE(wxBLUE_BRUSH
);
524 wxDELETE(wxGREEN_BRUSH
);
525 wxDELETE(wxWHITE_BRUSH
);
526 wxDELETE(wxBLACK_BRUSH
);
527 wxDELETE(wxTRANSPARENT_BRUSH
);
528 wxDELETE(wxCYAN_BRUSH
);
529 wxDELETE(wxRED_BRUSH
);
530 wxDELETE(wxGREY_BRUSH
);
531 wxDELETE(wxMEDIUM_GREY_BRUSH
);
532 wxDELETE(wxLIGHT_GREY_BRUSH
);
540 wxDELETE(wxLIGHT_GREY
);
542 wxDELETE(wxSTANDARD_CURSOR
);
543 wxDELETE(wxHOURGLASS_CURSOR
);
544 wxDELETE(wxCROSS_CURSOR
);
547 void wxDeleteStockLists()
549 wxDELETE(wxTheBrushList
);
550 wxDELETE(wxThePenList
);
551 wxDELETE(wxTheFontList
);
552 wxDELETE(wxTheBitmapList
);
555 // ============================================================================
556 // wxTheXXXList stuff (semi-obsolete)
557 // ============================================================================
559 wxBitmapList::wxBitmapList()
563 wxBitmapList::~wxBitmapList ()
565 wxNode
*node
= First ();
568 wxBitmap
*bitmap
= (wxBitmap
*) node
->Data ();
569 wxNode
*next
= node
->Next ();
570 if (bitmap
->GetVisible())
576 // Pen and Brush lists
577 wxPenList::~wxPenList ()
579 wxNode
*node
= First ();
582 wxPen
*pen
= (wxPen
*) node
->Data ();
583 wxNode
*next
= node
->Next ();
584 if (pen
->GetVisible())
590 void wxPenList::AddPen (wxPen
* pen
)
595 void wxPenList::RemovePen (wxPen
* pen
)
600 wxPen
*wxPenList::FindOrCreatePen (const wxColour
& colour
, int width
, int style
)
602 for (wxNode
* node
= First (); node
; node
= node
->Next ())
604 wxPen
*each_pen
= (wxPen
*) node
->Data ();
606 each_pen
->GetVisible() &&
607 each_pen
->GetWidth () == width
&&
608 each_pen
->GetStyle () == style
&&
609 each_pen
->GetColour ().Red () == colour
.Red () &&
610 each_pen
->GetColour ().Green () == colour
.Green () &&
611 each_pen
->GetColour ().Blue () == colour
.Blue ())
615 wxPen
*pen
= new wxPen (colour
, width
, style
);
618 // don't save the invalid pens in the list
626 // we'll delete it ourselves later
627 pen
->SetVisible(TRUE
);
632 wxBrushList::~wxBrushList ()
634 wxNode
*node
= First ();
637 wxBrush
*brush
= (wxBrush
*) node
->Data ();
638 wxNode
*next
= node
->Next ();
639 if (brush
&& brush
->GetVisible())
645 void wxBrushList::AddBrush (wxBrush
* brush
)
650 wxBrush
*wxBrushList::FindOrCreateBrush (const wxColour
& colour
, int style
)
652 for (wxNode
* node
= First (); node
; node
= node
->Next ())
654 wxBrush
*each_brush
= (wxBrush
*) node
->Data ();
656 each_brush
->GetVisible() &&
657 each_brush
->GetStyle () == style
&&
658 each_brush
->GetColour ().Red () == colour
.Red () &&
659 each_brush
->GetColour ().Green () == colour
.Green () &&
660 each_brush
->GetColour ().Blue () == colour
.Blue ())
664 wxBrush
*brush
= new wxBrush (colour
, style
);
668 // don't put the brushes we failed to create into the list
676 // we'll delete it ourselves later
677 brush
->SetVisible(TRUE
);
682 void wxBrushList::RemoveBrush (wxBrush
* brush
)
684 DeleteObject (brush
);
687 wxFontList::~wxFontList ()
689 wxNode
*node
= First ();
692 // Only delete objects that are 'visible', i.e.
693 // that have been created using FindOrCreate...,
694 // where the pointers are expected to be shared
695 // (and therefore not deleted by any one part of an app).
696 wxFont
*font
= (wxFont
*) node
->Data ();
697 wxNode
*next
= node
->Next ();
698 if (font
->GetVisible())
704 void wxFontList::AddFont (wxFont
* font
)
709 void wxFontList::RemoveFont (wxFont
* font
)
714 wxFont
*wxFontList::FindOrCreateFont(int pointSize
,
719 const wxString
& facename
,
720 wxFontEncoding encoding
)
722 wxFont
*font
= (wxFont
*)NULL
;
724 for ( node
= First(); node
; node
= node
->Next() )
726 font
= (wxFont
*)node
->Data();
727 if ( font
->GetVisible() &&
729 font
->GetPointSize () == pointSize
&&
730 font
->GetStyle () == style
&&
731 font
->GetWeight () == weight
&&
732 font
->GetUnderlined () == underline
)
734 int fontFamily
= font
->GetFamily();
736 #if defined(__WXGTK__)
737 // under GTK the default family is wxSWISS, so looking for a font
738 // with wxDEFAULT family should return a wxSWISS one instead of
739 // creating a new one
740 bool same
= (fontFamily
== family
) ||
741 (fontFamily
== wxSWISS
&& family
== wxDEFAULT
);
743 // VZ: but why elsewhere do we require an exact match? mystery...
744 bool same
= fontFamily
== family
;
747 // empty facename matches anything at all: this is bad because
748 // depending on which fonts are already created, we might get back
749 // a different font if we create it with empty facename, but it is
750 // still better than never matching anything in the cache at all
752 if ( same
&& !!facename
)
754 const wxString
& fontFace
= font
->GetFaceName();
756 // empty facename matches everything
757 same
= !fontFace
|| fontFace
== facename
;
760 if ( same
&& (encoding
!= wxFONTENCODING_DEFAULT
) )
762 // have to match the encoding too
763 same
= font
->GetEncoding() == encoding
;
775 // font not found, create the new one
776 font
= new wxFont(pointSize
, family
, style
, weight
,
777 underline
, facename
, encoding
);
781 // and mark it as being cacheable
782 font
->SetVisible(TRUE
);
788 void wxBitmapList::AddBitmap(wxBitmap
*bitmap
)
793 void wxBitmapList::RemoveBitmap(wxBitmap
*bitmap
)
795 DeleteObject(bitmap
);
798 wxSize
wxGetDisplaySize()
801 wxDisplaySize(& x
, & y
);
805 wxRect
wxGetClientDisplayRect()
807 int x
, y
, width
, height
;
808 wxClientDisplayRect(&x
, &y
, &width
, &height
); // call plat-specific version
809 return wxRect(x
, y
, width
, height
);
812 wxSize
wxGetDisplaySizeMM()
815 wxDisplaySizeMM(& x
, & y
);
819 wxResourceCache::~wxResourceCache ()
821 wxNode
*node
= First ();
823 wxObject
*item
= (wxObject
*)node
->Data();
826 node
= node
->Next ();