]> git.saurik.com Git - wxWidgets.git/blob - src/common/colourcmn.cpp
allow dynamically changing most of text control styles in wxGTK
[wxWidgets.git] / src / common / colourcmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/colourcmn.cpp
3 // Purpose: wxColourBase implementation
4 // Author: Francesco Montorsi
5 // Modified by:
6 // Created: 20/4/2006
7 // RCS-ID: $Id$
8 // Copyright: (c) Francesco Montorsi
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12
13 // For compilers that support precompilation, includes "wx.h".
14 #include "wx/wxprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #include "wx/colour.h"
21
22 #ifndef WX_PRECOMP
23 #include "wx/log.h"
24 #include "wx/utils.h"
25 #include "wx/gdicmn.h"
26 #endif
27
28 #if wxUSE_VARIANT
29 IMPLEMENT_VARIANT_OBJECT_EXPORTED(wxColour,WXDLLEXPORT)
30 #endif
31
32 // ============================================================================
33 // wxString <-> wxColour conversions
34 // ============================================================================
35
36 bool wxColourBase::FromString(const wxChar *str)
37 {
38 if ( str == NULL || str[0] == wxT('\0'))
39 return false; // invalid or empty string
40
41 if ( wxStrncmp(str, wxT("RGB"), 3) == 0 ||
42 wxStrncmp(str, wxT("rgb"), 3) == 0 )
43 {
44 // CSS-like RGB specification
45 // according to http://www.w3.org/TR/REC-CSS2/syndata.html#color-units
46 // values outside 0-255 range are allowed but should be clipped
47 int red, green, blue;
48 if (wxSscanf(&str[3], wxT("(%d, %d, %d)"), &red, &green, &blue) != 3)
49 return false;
50
51 Set((unsigned char)wxClip(red,0,255),
52 (unsigned char)wxClip(green,0,255),
53 (unsigned char)wxClip(blue,0,255));
54 }
55 else if ( str[0] == wxT('#') && wxStrlen(str) == 7 )
56 {
57 // hexadecimal prefixed with # (HTML syntax)
58 unsigned long tmp;
59 if (wxSscanf(&str[1], wxT("%lx"), &tmp) != 1)
60 return false;
61
62 Set((unsigned char)(tmp >> 16),
63 (unsigned char)(tmp >> 8),
64 (unsigned char)tmp);
65 }
66 else if (wxTheColourDatabase) // a colour name ?
67 {
68 // we can't do
69 // *this = wxTheColourDatabase->Find(str)
70 // because this place can be called from constructor
71 // and 'this' could not be available yet
72 wxColour clr = wxTheColourDatabase->Find(str);
73 if (clr.Ok())
74 Set((unsigned char)clr.Red(),
75 (unsigned char)clr.Green(),
76 (unsigned char)clr.Blue());
77 }
78
79 if (Ok())
80 return true;
81
82 wxLogDebug(wxT("wxColour::Set - couldn't set to colour string '%s'"), str);
83 return false;
84 }
85
86 wxString wxColourBase::GetAsString(long flags) const
87 {
88 wxString colName;
89
90 if (flags & wxC2S_NAME)
91 colName = wxTheColourDatabase->FindName((const wxColour &)(*this)).MakeLower();
92
93 if ( colName.empty() && (flags & wxC2S_CSS_SYNTAX) )
94 {
95 // no name for this colour; return it in CSS syntax
96 colName.Printf(wxT("rgb(%d, %d, %d)"),
97 Red(), Green(), Blue());
98 }
99 else if ( colName.empty() && (flags & wxC2S_HTML_SYNTAX) )
100 {
101 // no name for this colour; return it in HTML syntax
102 colName.Printf(wxT("#%02X%02X%02X"),
103 Red(), Green(), Blue());
104 }
105
106 // this function always returns a non-empty string
107 wxASSERT_MSG(!colName.empty(),
108 wxT("Invalid wxColour -> wxString conversion flags"));
109
110 return colName;
111 }
112
113 #if WXWIN_COMPATIBILITY_2_6
114
115 // static
116 wxColour wxColourBase::CreateByName(const wxString& name)
117 {
118 return wxColour(name);
119 }
120
121 void wxColourBase::InitFromName(const wxString& col)
122 {
123 Set(col);
124 }
125
126 #endif // WXWIN_COMPATIBILITY_2_6