]> git.saurik.com Git - wxWidgets.git/blob - src/common/colourdata.cpp
Misc validity fixes to samples/xrc/rc/*.xrc.
[wxWidgets.git] / src / common / colourdata.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/colourdata.cpp
3 // Author: Julian Smart
4 // Copyright: (c) Julian Smart
5 // Licence: wxWindows licence
6 /////////////////////////////////////////////////////////////////////////////
7
8 #include "wx/wxprec.h"
9
10 #ifdef __BORLANDC__
11 #pragma hdrstop
12 #endif
13
14 #if wxUSE_COLOURDLG || wxUSE_COLOURPICKERCTRL
15
16 #include "wx/colourdata.h"
17 #include "wx/tokenzr.h"
18
19 // ----------------------------------------------------------------------------
20 // wxColourData
21 // ----------------------------------------------------------------------------
22
23 IMPLEMENT_DYNAMIC_CLASS(wxColourData, wxObject)
24
25 wxColourData::wxColourData()
26 {
27 m_chooseFull = false;
28 m_dataColour.Set(0,0,0);
29 // m_custColours are wxNullColours initially
30 }
31
32 wxColourData::wxColourData(const wxColourData& data)
33 : wxObject()
34 {
35 (*this) = data;
36 }
37
38 wxColourData::~wxColourData()
39 {
40 }
41
42 void wxColourData::SetCustomColour(int i, const wxColour& colour)
43 {
44 wxCHECK_RET( i >= 0 && i < NUM_CUSTOM, wxT("custom colour index out of range") );
45
46 m_custColours[i] = colour;
47 }
48
49 wxColour wxColourData::GetCustomColour(int i) const
50 {
51 wxCHECK_MSG( i >= 0 && i < NUM_CUSTOM, wxColour(0,0,0),
52 wxT("custom colour index out of range") );
53
54 return m_custColours[i];
55 }
56
57 wxColourData& wxColourData::operator=(const wxColourData& data)
58 {
59 for ( int i = 0; i < NUM_CUSTOM; i++)
60 m_custColours[i] = data.m_custColours[i];
61
62 m_dataColour = data.m_dataColour;
63 m_chooseFull = data.m_chooseFull;
64
65 return *this;
66 }
67
68 // ----------------------------------------------------------------------------
69 // [de]serialization
70 // ----------------------------------------------------------------------------
71
72 // separator used between different fields
73 static const char wxCOL_DATA_SEP = ',';
74
75 wxString wxColourData::ToString() const
76 {
77 wxString str(m_chooseFull ? '1' : '0');
78
79 for ( int i = 0; i < NUM_CUSTOM; i++ )
80 {
81 str += wxCOL_DATA_SEP;
82
83 const wxColour& clr = m_custColours[i];
84 if ( clr.IsOk() )
85 str += clr.GetAsString(wxC2S_HTML_SYNTAX);
86 }
87
88 return str;
89 }
90
91 bool wxColourData::FromString(const wxString& str)
92 {
93 wxStringTokenizer tokenizer(str, wxCOL_DATA_SEP);
94 wxString token = tokenizer.GetNextToken();
95 m_chooseFull = token == '1';
96 bool success = m_chooseFull || token == '0';
97 for (int i = 0; success && i < NUM_CUSTOM; i++)
98 {
99 token = tokenizer.GetNextToken();
100 if (token.empty())
101 m_custColours[i] = wxColour();
102 else
103 success = m_custColours[i].Set(token);
104 }
105 return success;
106 }
107
108 #if wxUSE_COLOURDLG
109
110 #include "wx/colordlg.h"
111
112 wxColour wxGetColourFromUser(wxWindow *parent,
113 const wxColour& colInit,
114 const wxString& caption,
115 wxColourData *ptrData)
116 {
117 // contains serialized representation of wxColourData used the last time
118 // the dialog was shown: we want to reuse it the next time in order to show
119 // the same custom colours to the user (and we can't just have static
120 // wxColourData itself because it's a GUI object and so should be destroyed
121 // before GUI shutdown and doing it during static cleanup is too late)
122 static wxString s_strColourData;
123
124 wxColourData data;
125 if ( !ptrData )
126 {
127 ptrData = &data;
128 if ( !s_strColourData.empty() )
129 {
130 if ( !data.FromString(s_strColourData) )
131 {
132 wxFAIL_MSG( "bug in wxColourData::FromString()?" );
133 }
134
135 #ifdef __WXMSW__
136 // we don't get back the "choose full" flag value from the native
137 // dialog and so we can't preserve it between runs, so we decide to
138 // always use it as it seems better than not using it (user can
139 // just ignore the extra controls in the dialog but having to click
140 // a button each time to show them would be very annoying
141 data.SetChooseFull(true);
142 #endif // __WXMSW__
143 }
144 }
145
146 if ( colInit.IsOk() )
147 {
148 ptrData->SetColour(colInit);
149 }
150
151 wxColour colRet;
152 wxColourDialog dialog(parent, ptrData);
153 if (!caption.empty())
154 dialog.SetTitle(caption);
155 if ( dialog.ShowModal() == wxID_OK )
156 {
157 *ptrData = dialog.GetColourData();
158 colRet = ptrData->GetColour();
159 s_strColourData = ptrData->ToString();
160 }
161 //else: leave colRet invalid
162
163 return colRet;
164 }
165
166 #endif // wxUSE_COLOURDLG
167 #endif // wxUSE_COLOURDLG || wxUSE_COLOURPICKERCTRL