]>
git.saurik.com Git - wxWidgets.git/blob - src/common/colourdata.cpp
5e5213e32dc972188030f9f4c483384308134b8a
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/colourdata.cpp
3 // Author: Julian Smart
5 // Copyright: (c) Julian Smart
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
15 #if wxUSE_COLOURDLG || wxUSE_COLOURPICKERCTRL
17 #include "wx/colourdata.h"
18 #include "wx/tokenzr.h"
20 // ----------------------------------------------------------------------------
22 // ----------------------------------------------------------------------------
24 IMPLEMENT_DYNAMIC_CLASS(wxColourData
, wxObject
)
26 wxColourData::wxColourData()
29 m_dataColour
.Set(0,0,0);
30 // m_custColours are wxNullColours initially
33 wxColourData::wxColourData(const wxColourData
& data
)
39 wxColourData::~wxColourData()
43 void wxColourData::SetCustomColour(int i
, const wxColour
& colour
)
45 wxCHECK_RET( i
>= 0 && i
< NUM_CUSTOM
, wxT("custom colour index out of range") );
47 m_custColours
[i
] = colour
;
50 wxColour
wxColourData::GetCustomColour(int i
) const
52 wxCHECK_MSG( i
>= 0 && i
< NUM_CUSTOM
, wxColour(0,0,0),
53 wxT("custom colour index out of range") );
55 return m_custColours
[i
];
58 wxColourData
& wxColourData::operator=(const wxColourData
& data
)
60 for ( int i
= 0; i
< NUM_CUSTOM
; i
++)
61 m_custColours
[i
] = data
.m_custColours
[i
];
63 m_dataColour
= data
.m_dataColour
;
64 m_chooseFull
= data
.m_chooseFull
;
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 // separator used between different fields
74 static const char wxCOL_DATA_SEP
= ',';
76 wxString
wxColourData::ToString() const
78 wxString
str(m_chooseFull
? '1' : '0');
80 for ( int i
= 0; i
< NUM_CUSTOM
; i
++ )
82 str
+= wxCOL_DATA_SEP
;
84 const wxColour
& clr
= m_custColours
[i
];
86 str
+= clr
.GetAsString(wxC2S_HTML_SYNTAX
);
92 bool wxColourData::FromString(const wxString
& str
)
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
++)
100 token
= tokenizer
.GetNextToken();
102 m_custColours
[i
] = wxColour();
104 success
= m_custColours
[i
].Set(token
);
111 #include "wx/colordlg.h"
113 wxColour
wxGetColourFromUser(wxWindow
*parent
,
114 const wxColour
& colInit
,
115 const wxString
& caption
,
116 wxColourData
*ptrData
)
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
;
129 if ( !s_strColourData
.empty() )
131 if ( !data
.FromString(s_strColourData
) )
133 wxFAIL_MSG( "bug in wxColourData::FromString()?" );
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);
147 if ( colInit
.IsOk() )
149 ptrData
->SetColour(colInit
);
153 wxColourDialog
dialog(parent
, ptrData
);
154 if (!caption
.empty())
155 dialog
.SetTitle(caption
);
156 if ( dialog
.ShowModal() == wxID_OK
)
158 *ptrData
= dialog
.GetColourData();
159 colRet
= ptrData
->GetColour();
160 s_strColourData
= ptrData
->ToString();
162 //else: leave colRet invalid
167 #endif // wxUSE_COLOURDLG
168 #endif // wxUSE_COLOURDLG || wxUSE_COLOURPICKERCTRL