]> git.saurik.com Git - wxWidgets.git/blob - src/msw/colordlg.cpp
Fix potential buffer overflow in wxSTC DefaultFont() function.
[wxWidgets.git] / src / msw / colordlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/colordlg.cpp
3 // Purpose: wxColourDialog class
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_COLOURDLG && !(defined(__SMARTPHONE__) && defined(__WXWINCE__))
28
29 #include "wx/colordlg.h"
30 #include "wx/modalhook.h"
31
32 #ifndef WX_PRECOMP
33 #include "wx/msw/wrapcdlg.h"
34 #include <stdio.h>
35 #include "wx/colour.h"
36 #include "wx/gdicmn.h"
37 #include "wx/utils.h"
38 #include "wx/math.h"
39 #endif
40
41 #include "wx/msw/private.h"
42
43 #include <stdlib.h>
44 #include <string.h>
45
46 // ----------------------------------------------------------------------------
47 // globals
48 // ----------------------------------------------------------------------------
49
50 // standard colors dialog size for the Windows systems
51 // this is ok if color dialog created with standart color
52 // and "Define Custom Colors" extension not shown
53 static wxRect gs_rectDialog(0, 0, 222, 324);
54
55 // ----------------------------------------------------------------------------
56 // wxWin macros
57 // ----------------------------------------------------------------------------
58
59 IMPLEMENT_DYNAMIC_CLASS(wxColourDialog, wxDialog)
60
61 // ============================================================================
62 // implementation
63 // ============================================================================
64
65 // ----------------------------------------------------------------------------
66 // colour dialog hook proc
67 // ----------------------------------------------------------------------------
68
69 UINT_PTR CALLBACK
70 wxColourDialogHookProc(HWND hwnd,
71 UINT uiMsg,
72 WPARAM WXUNUSED(wParam),
73 LPARAM lParam)
74 {
75 if ( uiMsg == WM_INITDIALOG )
76 {
77 CHOOSECOLOR *pCC = (CHOOSECOLOR *)lParam;
78 wxColourDialog * const
79 dialog = reinterpret_cast<wxColourDialog *>(pCC->lCustData);
80
81 const wxString title = dialog->GetTitle();
82 if ( !title.empty() )
83 ::SetWindowText(hwnd, title.t_str());
84
85 dialog->MSWOnInitDone((WXHWND)hwnd);
86 }
87
88 return 0;
89 }
90
91 // ----------------------------------------------------------------------------
92 // wxColourDialog
93 // ----------------------------------------------------------------------------
94
95 void wxColourDialog::Init()
96 {
97 m_movedWindow = false;
98 m_centreDir = 0;
99
100 // reset to zero, otherwise the wx routines won't size the window the
101 // second time the dialog is shown, because they would believe it already
102 // has the requested size/position
103 gs_rectDialog.x =
104 gs_rectDialog.y = 0;
105 }
106
107 bool wxColourDialog::Create(wxWindow *parent, wxColourData *data)
108 {
109 m_parent = parent;
110 if (data)
111 m_colourData = *data;
112
113 return true;
114 }
115
116 int wxColourDialog::ShowModal()
117 {
118 WX_HOOK_MODAL_DIALOG();
119
120 // initialize the struct used by Windows
121 CHOOSECOLOR chooseColorStruct;
122 memset(&chooseColorStruct, 0, sizeof(CHOOSECOLOR));
123
124 size_t i;
125
126 // and transfer data from m_colourData to it
127 COLORREF custColours[16];
128 for ( i = 0; i < WXSIZEOF(custColours); i++ )
129 {
130 if ( m_colourData.GetCustomColour(i).IsOk() )
131 custColours[i] = wxColourToRGB(m_colourData.GetCustomColour(i));
132 else
133 custColours[i] = RGB(255,255,255);
134 }
135
136 chooseColorStruct.lStructSize = sizeof(CHOOSECOLOR);
137 if ( m_parent )
138 chooseColorStruct.hwndOwner = GetHwndOf(m_parent);
139 chooseColorStruct.rgbResult = wxColourToRGB(m_colourData.GetColour());
140 chooseColorStruct.lpCustColors = custColours;
141
142 chooseColorStruct.Flags = CC_RGBINIT | CC_ENABLEHOOK;
143 chooseColorStruct.lCustData = (LPARAM)this;
144 chooseColorStruct.lpfnHook = wxColourDialogHookProc;
145
146 if ( m_colourData.GetChooseFull() )
147 chooseColorStruct.Flags |= CC_FULLOPEN;
148
149 // do show the modal dialog
150 if ( !::ChooseColor(&chooseColorStruct) )
151 {
152 // 0 error means the dialog was simply cancelled, i.e. no real error
153 // occurred
154 const DWORD err = CommDlgExtendedError();
155 if ( err )
156 {
157 wxLogError(_("Colour selection dialog failed with error %0lx."), err);
158 }
159
160 return wxID_CANCEL;
161 }
162
163
164 // transfer the values chosen by user back into m_colourData
165 for ( i = 0; i < WXSIZEOF(custColours); i++ )
166 {
167 wxRGBToColour(m_colourData.m_custColours[i], custColours[i]);
168 }
169
170 wxRGBToColour(m_colourData.GetColour(), chooseColorStruct.rgbResult);
171
172 // this doesn't seem to work (contrary to what MSDN implies) on current
173 // Windows versions: CC_FULLOPEN is never set on return if it wasn't
174 // initially set and vice versa
175 //m_colourData.SetChooseFull((chooseColorStruct.Flags & CC_FULLOPEN) != 0);
176
177 return wxID_OK;
178 }
179
180 // ----------------------------------------------------------------------------
181 // title
182 // ----------------------------------------------------------------------------
183
184 void wxColourDialog::SetTitle(const wxString& title)
185 {
186 m_title = title;
187 }
188
189 wxString wxColourDialog::GetTitle() const
190 {
191 return m_title;
192 }
193
194 // ----------------------------------------------------------------------------
195 // position/size
196 // ----------------------------------------------------------------------------
197
198 void wxColourDialog::DoGetPosition(int *x, int *y) const
199 {
200 if ( x )
201 *x = gs_rectDialog.x;
202 if ( y )
203 *y = gs_rectDialog.y;
204 }
205
206 void wxColourDialog::DoCentre(int dir)
207 {
208 m_centreDir = dir;
209
210 // it's unnecessary to do anything else at this stage as we'll redo it in
211 // MSWOnInitDone() anyhow
212 }
213
214 void wxColourDialog::DoMoveWindow(int x, int y, int WXUNUSED(w), int WXUNUSED(h))
215 {
216 gs_rectDialog.x = x;
217 gs_rectDialog.y = y;
218
219 // our HWND is only set when we're called from MSWOnInitDone(), test if
220 // this is the case
221 HWND hwnd = GetHwnd();
222 if ( hwnd )
223 {
224 // size of the dialog can't be changed because the controls are not
225 // laid out correctly then
226 ::SetWindowPos(hwnd, HWND_TOP, x, y, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
227 }
228 else // just remember that we were requested to move the window
229 {
230 m_movedWindow = true;
231
232 // if Centre() had been called before, it shouldn't be taken into
233 // account now
234 m_centreDir = 0;
235 }
236 }
237
238 void wxColourDialog::DoGetSize(int *width, int *height) const
239 {
240 if ( width )
241 *width = gs_rectDialog.width;
242 if ( height )
243 *height = gs_rectDialog.height;
244 }
245
246 void wxColourDialog::DoGetClientSize(int *width, int *height) const
247 {
248 if ( width )
249 *width = gs_rectDialog.width;
250 if ( height )
251 *height = gs_rectDialog.height;
252 }
253
254 void wxColourDialog::MSWOnInitDone(WXHWND hDlg)
255 {
256 // set HWND so that our DoMoveWindow() works correctly
257 SetHWND(hDlg);
258
259 if ( m_centreDir )
260 {
261 // now we have the real dialog size, remember it
262 RECT rect;
263 ::GetWindowRect((HWND)hDlg, &rect);
264 gs_rectDialog = wxRectFromRECT(rect);
265
266 // and position the window correctly: notice that we must use the base
267 // class version as our own doesn't do anything except setting flags
268 wxDialog::DoCentre(m_centreDir);
269 }
270 else if ( m_movedWindow ) // need to just move it to the correct place
271 {
272 SetPosition(GetPosition());
273 }
274
275 // we shouldn't destroy hDlg, so disassociate from it
276 SetHWND(NULL);
277 }
278
279 #endif // wxUSE_COLOURDLG && !(__SMARTPHONE__ && __WXWINCE__)