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