]>
git.saurik.com Git - wxWidgets.git/blob - 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 /////////////////////////////////////////////////////////////////////////////
14 #if wxUSE_COLOURDLG || wxUSE_COLOURPICKERCTRL
16 #include "wx/colourdata.h"
17 #include "wx/tokenzr.h"
19 // ----------------------------------------------------------------------------
21 // ----------------------------------------------------------------------------
23 IMPLEMENT_DYNAMIC_CLASS(wxColourData
, wxObject
)
25 wxColourData::wxColourData()
28 m_dataColour
.Set(0,0,0);
29 // m_custColours are wxNullColours initially
32 wxColourData::wxColourData(const wxColourData
& data
)
38 wxColourData::~wxColourData()
42 void wxColourData::SetCustomColour(int i
, const wxColour
& colour
)
44 wxCHECK_RET( i
>= 0 && i
< NUM_CUSTOM
, wxT("custom colour index out of range") );
46 m_custColours
[i
] = colour
;
49 wxColour
wxColourData::GetCustomColour(int i
) const
51 wxCHECK_MSG( i
>= 0 && i
< NUM_CUSTOM
, wxColour(0,0,0),
52 wxT("custom colour index out of range") );
54 return m_custColours
[i
];
57 wxColourData
& wxColourData::operator=(const wxColourData
& data
)
59 for ( int i
= 0; i
< NUM_CUSTOM
; i
++)
60 m_custColours
[i
] = data
.m_custColours
[i
];
62 m_dataColour
= data
.m_dataColour
;
63 m_chooseFull
= data
.m_chooseFull
;
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 // separator used between different fields
73 static const char wxCOL_DATA_SEP
= ',';
75 wxString
wxColourData::ToString() const
77 wxString
str(m_chooseFull
? '1' : '0');
79 for ( int i
= 0; i
< NUM_CUSTOM
; i
++ )
81 str
+= wxCOL_DATA_SEP
;
83 const wxColour
& clr
= m_custColours
[i
];
85 str
+= clr
.GetAsString(wxC2S_HTML_SYNTAX
);
91 bool wxColourData::FromString(const wxString
& str
)
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
++)
99 token
= tokenizer
.GetNextToken();
101 m_custColours
[i
] = wxColour();
103 success
= m_custColours
[i
].Set(token
);
110 #include "wx/colordlg.h"
112 wxColour
wxGetColourFromUser(wxWindow
*parent
,
113 const wxColour
& colInit
,
114 const wxString
& caption
,
115 wxColourData
*ptrData
)
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
;
128 if ( !s_strColourData
.empty() )
130 if ( !data
.FromString(s_strColourData
) )
132 wxFAIL_MSG( "bug in wxColourData::FromString()?" );
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);
146 if ( colInit
.IsOk() )
148 ptrData
->SetColour(colInit
);
152 wxColourDialog
dialog(parent
, ptrData
);
153 if (!caption
.empty())
154 dialog
.SetTitle(caption
);
155 if ( dialog
.ShowModal() == wxID_OK
)
157 *ptrData
= dialog
.GetColourData();
158 colRet
= ptrData
->GetColour();
159 s_strColourData
= ptrData
->ToString();
161 //else: leave colRet invalid
166 #endif // wxUSE_COLOURDLG
167 #endif // wxUSE_COLOURDLG || wxUSE_COLOURPICKERCTRL