]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/gtk/colour.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | // For compilers that support precompilation, includes "wx.h". | |
11 | #include "wx/wxprec.h" | |
12 | ||
13 | #include "wx/colour.h" | |
14 | ||
15 | #include "wx/gtk/private.h" | |
16 | ||
17 | #include <gdk/gdk.h> | |
18 | ||
19 | //----------------------------------------------------------------------------- | |
20 | // wxColour | |
21 | //----------------------------------------------------------------------------- | |
22 | ||
23 | class wxColourRefData: public wxObjectRefData | |
24 | { | |
25 | public: | |
26 | wxColourRefData(guint16 red, guint16 green, guint16 blue) | |
27 | { | |
28 | m_color.red = | |
29 | m_red = red; | |
30 | m_color.green = | |
31 | m_green = green; | |
32 | m_color.blue = | |
33 | m_blue = blue; | |
34 | m_color.pixel = 0; | |
35 | m_colormap = NULL; | |
36 | } | |
37 | ||
38 | ~wxColourRefData() | |
39 | { | |
40 | FreeColour(); | |
41 | } | |
42 | ||
43 | void FreeColour(); | |
44 | void AllocColour( GdkColormap* cmap ); | |
45 | ||
46 | GdkColor m_color; | |
47 | GdkColormap *m_colormap; | |
48 | // gdk_colormap_alloc_color may change the RGB values in m_color, so we need separate copies | |
49 | guint16 m_red; | |
50 | guint16 m_green; | |
51 | guint16 m_blue; | |
52 | ||
53 | DECLARE_NO_COPY_CLASS(wxColourRefData) | |
54 | }; | |
55 | ||
56 | void wxColourRefData::FreeColour() | |
57 | { | |
58 | if (m_colormap) | |
59 | { | |
60 | gdk_colormap_free_colors(m_colormap, &m_color, 1); | |
61 | m_colormap = NULL; | |
62 | m_color.pixel = 0; | |
63 | } | |
64 | } | |
65 | ||
66 | void wxColourRefData::AllocColour( GdkColormap *cmap ) | |
67 | { | |
68 | if (m_colormap != cmap) | |
69 | { | |
70 | FreeColour(); | |
71 | ||
72 | m_color.red = m_red; | |
73 | m_color.green = m_green; | |
74 | m_color.blue = m_blue; | |
75 | if (gdk_colormap_alloc_color(cmap, &m_color, FALSE, TRUE)) | |
76 | { | |
77 | m_colormap = cmap; | |
78 | } | |
79 | } | |
80 | } | |
81 | ||
82 | //----------------------------------------------------------------------------- | |
83 | ||
84 | #define M_COLDATA wx_static_cast(wxColourRefData*, m_refData) | |
85 | ||
86 | // GDK's values are in 0..65535 range, ours are in 0..255 | |
87 | #define SHIFT 8 | |
88 | ||
89 | IMPLEMENT_DYNAMIC_CLASS(wxColour,wxGDIObject) | |
90 | ||
91 | wxColour::wxColour(const GdkColor& gdkColor) | |
92 | { | |
93 | m_refData = new wxColourRefData(gdkColor.red, gdkColor.green, gdkColor.blue); | |
94 | } | |
95 | ||
96 | wxColour::~wxColour() | |
97 | { | |
98 | } | |
99 | ||
100 | bool wxColour::operator == ( const wxColour& col ) const | |
101 | { | |
102 | if (m_refData == col.m_refData) | |
103 | return true; | |
104 | ||
105 | if (!m_refData || !col.m_refData) | |
106 | return false; | |
107 | ||
108 | wxColourRefData* refData = M_COLDATA; | |
109 | wxColourRefData* that_refData = wx_static_cast(wxColourRefData*, col.m_refData); | |
110 | return refData->m_red == that_refData->m_red && | |
111 | refData->m_green == that_refData->m_green && | |
112 | refData->m_blue == that_refData->m_blue; | |
113 | } | |
114 | ||
115 | void wxColour::InitWith( unsigned char red, unsigned char green, unsigned char blue ) | |
116 | { | |
117 | UnRef(); | |
118 | ||
119 | m_refData = new wxColourRefData( | |
120 | (guint16(red) << SHIFT) + red, | |
121 | (guint16(green) << SHIFT) + green, | |
122 | (guint16(blue) << SHIFT) + blue); | |
123 | } | |
124 | ||
125 | unsigned char wxColour::Red() const | |
126 | { | |
127 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); | |
128 | ||
129 | return wxByte(M_COLDATA->m_red >> SHIFT); | |
130 | } | |
131 | ||
132 | unsigned char wxColour::Green() const | |
133 | { | |
134 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); | |
135 | ||
136 | return wxByte(M_COLDATA->m_green >> SHIFT); | |
137 | } | |
138 | ||
139 | unsigned char wxColour::Blue() const | |
140 | { | |
141 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); | |
142 | ||
143 | return wxByte(M_COLDATA->m_blue >> SHIFT); | |
144 | } | |
145 | ||
146 | void wxColour::CalcPixel( GdkColormap *cmap ) | |
147 | { | |
148 | if (!Ok()) return; | |
149 | ||
150 | M_COLDATA->AllocColour( cmap ); | |
151 | } | |
152 | ||
153 | int wxColour::GetPixel() const | |
154 | { | |
155 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); | |
156 | ||
157 | return M_COLDATA->m_color.pixel; | |
158 | } | |
159 | ||
160 | #ifdef __WXGTK24__ | |
161 | const GdkColor *wxColour::GetColor() const | |
162 | #else | |
163 | GdkColor *wxColour::GetColor() const | |
164 | #endif | |
165 | { | |
166 | wxCHECK_MSG( Ok(), NULL, wxT("invalid colour") ); | |
167 | ||
168 | return &M_COLDATA->m_color; | |
169 | } | |
170 | ||
171 | bool wxColour::FromString(const wxChar *str) | |
172 | { | |
173 | GdkColor colGDK; | |
174 | if ( gdk_color_parse( wxGTK_CONV_SYS( str ), &colGDK ) ) | |
175 | { | |
176 | *this = wxColour(colGDK); | |
177 | return true; | |
178 | } | |
179 | ||
180 | return wxColourBase::FromString(str); | |
181 | } |