| 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() |
| 27 | { |
| 28 | m_color.red = 0; |
| 29 | m_color.green = 0; |
| 30 | m_color.blue = 0; |
| 31 | m_color.pixel = 0; |
| 32 | m_colormap = NULL; |
| 33 | m_hasPixel = false; |
| 34 | } |
| 35 | |
| 36 | wxColourRefData(const wxColourRefData& data) |
| 37 | : wxObjectRefData() |
| 38 | { |
| 39 | m_color = data.m_color; |
| 40 | m_colormap = data.m_colormap; |
| 41 | m_hasPixel = data.m_hasPixel; |
| 42 | } |
| 43 | |
| 44 | ~wxColourRefData() |
| 45 | { |
| 46 | FreeColour(); |
| 47 | } |
| 48 | |
| 49 | void FreeColour(); |
| 50 | void AllocColour( GdkColormap* cmap ); |
| 51 | |
| 52 | GdkColor m_color; |
| 53 | GdkColormap *m_colormap; |
| 54 | bool m_hasPixel; |
| 55 | }; |
| 56 | |
| 57 | void wxColourRefData::FreeColour() |
| 58 | { |
| 59 | if (m_hasPixel) |
| 60 | { |
| 61 | gdk_colormap_free_colors(m_colormap, &m_color, 1); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | void wxColourRefData::AllocColour( GdkColormap *cmap ) |
| 66 | { |
| 67 | if (m_hasPixel && (m_colormap == cmap)) |
| 68 | return; |
| 69 | |
| 70 | FreeColour(); |
| 71 | |
| 72 | m_hasPixel = gdk_colormap_alloc_color(cmap, &m_color, FALSE, TRUE); |
| 73 | m_colormap = cmap; |
| 74 | } |
| 75 | |
| 76 | //----------------------------------------------------------------------------- |
| 77 | |
| 78 | #define M_COLDATA wx_static_cast(wxColourRefData*, m_refData) |
| 79 | |
| 80 | // GDK's values are in 0..65535 range, our are in 0..255 |
| 81 | #define SHIFT 8 |
| 82 | |
| 83 | IMPLEMENT_DYNAMIC_CLASS(wxColour,wxGDIObject) |
| 84 | |
| 85 | wxColour::~wxColour() |
| 86 | { |
| 87 | } |
| 88 | |
| 89 | bool wxColour::operator == ( const wxColour& col ) const |
| 90 | { |
| 91 | if (m_refData == col.m_refData) |
| 92 | return true; |
| 93 | |
| 94 | if (!m_refData || !col.m_refData) |
| 95 | return false; |
| 96 | |
| 97 | const GdkColor& own = M_COLDATA->m_color; |
| 98 | const GdkColor& other = wx_static_cast(wxColourRefData*, col.m_refData)->m_color; |
| 99 | return own.red == other.red && |
| 100 | own.blue == other.blue && |
| 101 | own.green == other.green; |
| 102 | } |
| 103 | |
| 104 | wxObjectRefData *wxColour::CreateRefData() const |
| 105 | { |
| 106 | return new wxColourRefData; |
| 107 | } |
| 108 | |
| 109 | wxObjectRefData *wxColour::CloneRefData(const wxObjectRefData *data) const |
| 110 | { |
| 111 | return new wxColourRefData(*(wxColourRefData *)data); |
| 112 | } |
| 113 | |
| 114 | void wxColour::InitWith( unsigned char red, unsigned char green, unsigned char blue ) |
| 115 | { |
| 116 | AllocExclusive(); |
| 117 | |
| 118 | M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT; |
| 119 | M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT; |
| 120 | M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT; |
| 121 | M_COLDATA->m_color.pixel = 0; |
| 122 | |
| 123 | M_COLDATA->m_colormap = NULL; |
| 124 | M_COLDATA->m_hasPixel = false; |
| 125 | } |
| 126 | |
| 127 | unsigned char wxColour::Red() const |
| 128 | { |
| 129 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); |
| 130 | |
| 131 | return (unsigned char)(M_COLDATA->m_color.red >> SHIFT); |
| 132 | } |
| 133 | |
| 134 | unsigned char wxColour::Green() const |
| 135 | { |
| 136 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); |
| 137 | |
| 138 | return (unsigned char)(M_COLDATA->m_color.green >> SHIFT); |
| 139 | } |
| 140 | |
| 141 | unsigned char wxColour::Blue() const |
| 142 | { |
| 143 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); |
| 144 | |
| 145 | return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT); |
| 146 | } |
| 147 | |
| 148 | void wxColour::CalcPixel( GdkColormap *cmap ) |
| 149 | { |
| 150 | if (!Ok()) return; |
| 151 | |
| 152 | M_COLDATA->AllocColour( cmap ); |
| 153 | } |
| 154 | |
| 155 | int wxColour::GetPixel() const |
| 156 | { |
| 157 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); |
| 158 | |
| 159 | return M_COLDATA->m_color.pixel; |
| 160 | } |
| 161 | |
| 162 | GdkColor *wxColour::GetColor() const |
| 163 | { |
| 164 | wxCHECK_MSG( Ok(), NULL, wxT("invalid colour") ); |
| 165 | |
| 166 | return &M_COLDATA->m_color; |
| 167 | } |
| 168 | |
| 169 | bool wxColour::FromString(const wxChar *str) |
| 170 | { |
| 171 | GdkColor colGDK; |
| 172 | if ( gdk_color_parse( wxGTK_CONV_SYS( str ), &colGDK ) ) |
| 173 | { |
| 174 | UnRef(); |
| 175 | |
| 176 | m_refData = new wxColourRefData; |
| 177 | M_COLDATA->m_color = colGDK; |
| 178 | return true; |
| 179 | } |
| 180 | |
| 181 | return wxColourBase::FromString(str); |
| 182 | } |