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