fix wxColourDialog::Centre() and SetSize() (modified patch 1831628)
[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
31 #ifndef WX_PRECOMP
32 #include "wx/msw/wrapcdlg.h"
33 #include <stdio.h>
34 #include "wx/colour.h"
35 #include "wx/gdicmn.h"
36 #include "wx/utils.h"
37 #include "wx/dialog.h"
38 #include "wx/cmndata.h"
39 #include "wx/math.h"
40 #endif
41
42 #include "wx/msw/private.h"
43
44 #include <stdlib.h>
45 #include <string.h>
46
47 // ----------------------------------------------------------------------------
48 // globals
49 // ----------------------------------------------------------------------------
50
51 // standard colors dialog size for the Windows systems
52 // this is ok if color dialog created with standart color
53 // and "Define Custom Colors" extension not shown
54 static wxRect gs_rectDialog(0, 0, 222, 324);
55
56 // ----------------------------------------------------------------------------
57 // wxWin macros
58 // ----------------------------------------------------------------------------
59
60 IMPLEMENT_DYNAMIC_CLASS(wxColourDialog, wxDialog)
61
62 // ============================================================================
63 // implementation
64 // ============================================================================
65
66 // ----------------------------------------------------------------------------
67 // colour dialog hook proc
68 // ----------------------------------------------------------------------------
69
70 UINT_PTR CALLBACK
71 wxColourDialogHookProc(HWND hwnd,
72 UINT uiMsg,
73 WPARAM WXUNUSED(wParam),
74 LPARAM lParam)
75 {
76 if ( uiMsg == WM_INITDIALOG )
77 {
78 CHOOSECOLOR *pCC = (CHOOSECOLOR *)lParam;
79 wxColourDialog * const
80 dialog = wx_reinterpret_cast(wxColourDialog *, pCC->lCustData);
81
82 const wxString title = dialog->GetTitle();
83 if ( !title.empty() )
84 ::SetWindowText(hwnd, title.wx_str());
85
86 dialog->MSWOnInitDone((WXHWND)hwnd);
87 }
88
89 return 0;
90 }
91
92 // ----------------------------------------------------------------------------
93 // wxColourDialog
94 // ----------------------------------------------------------------------------
95
96 void wxColourDialog::Init()
97 {
98 m_movedWindow = false;
99 m_centreDir = 0;
100
101 // reset to zero, otherwise the wx routines won't size the window the
102 // second time the dialog is shown, because they would believe it already
103 // has the requested size/position
104 gs_rectDialog.x =
105 gs_rectDialog.y = 0;
106 }
107
108 bool wxColourDialog::Create(wxWindow *parent, wxColourData *data)
109 {
110 m_parent = parent;
111 if (data)
112 m_colourData = *data;
113
114 return true;
115 }
116
117 int wxColourDialog::ShowModal()
118 {
119 CHOOSECOLOR chooseColorStruct;
120 COLORREF custColours[16];
121 memset(&chooseColorStruct, 0, sizeof(CHOOSECOLOR));
122
123 int i;
124 for (i = 0; i < 16; i++)
125 {
126 if (m_colourData.m_custColours[i].Ok())
127 custColours[i] = wxColourToRGB(m_colourData.m_custColours[i]);
128 else
129 custColours[i] = RGB(255,255,255);
130 }
131
132 chooseColorStruct.lStructSize = sizeof(CHOOSECOLOR);
133 if ( m_parent )
134 chooseColorStruct.hwndOwner = GetHwndOf(m_parent);
135 chooseColorStruct.rgbResult = wxColourToRGB(m_colourData.m_dataColour);
136 chooseColorStruct.lpCustColors = custColours;
137
138 chooseColorStruct.Flags = CC_RGBINIT | CC_ENABLEHOOK;
139 chooseColorStruct.lCustData = (LPARAM)this;
140 chooseColorStruct.lpfnHook = wxColourDialogHookProc;
141
142 if (m_colourData.GetChooseFull())
143 chooseColorStruct.Flags |= CC_FULLOPEN;
144
145 // Do the modal dialog
146 bool success = ::ChooseColor(&(chooseColorStruct)) != 0;
147
148 // Try to highlight the correct window (the parent)
149 if (GetParent())
150 {
151 HWND hWndParent = (HWND) GetParent()->GetHWND();
152 if (hWndParent)
153 ::BringWindowToTop(hWndParent);
154 }
155
156
157 // Restore values
158 for (i = 0; i < 16; i++)
159 {
160 wxRGBToColour(m_colourData.m_custColours[i], custColours[i]);
161 }
162
163 wxRGBToColour(m_colourData.m_dataColour, chooseColorStruct.rgbResult);
164
165 return success ? wxID_OK : wxID_CANCEL;
166 }
167
168 // ----------------------------------------------------------------------------
169 // title
170 // ----------------------------------------------------------------------------
171
172 void wxColourDialog::SetTitle(const wxString& title)
173 {
174 m_title = title;
175 }
176
177 wxString wxColourDialog::GetTitle() const
178 {
179 return m_title;
180 }
181
182 // ----------------------------------------------------------------------------
183 // position/size
184 // ----------------------------------------------------------------------------
185
186 void wxColourDialog::DoGetPosition(int *x, int *y) const
187 {
188 if ( x )
189 *x = gs_rectDialog.x;
190 if ( y )
191 *y = gs_rectDialog.y;
192 }
193
194 void wxColourDialog::DoCentre(int dir)
195 {
196 m_centreDir = dir;
197
198 // it's unnecessary to do anything else at this stage as we'll redo it in
199 // MSWOnInitDone() anyhow
200 }
201
202 void wxColourDialog::DoMoveWindow(int x, int y, int WXUNUSED(w), int WXUNUSED(h))
203 {
204 gs_rectDialog.x = x;
205 gs_rectDialog.y = y;
206
207 // our HWND is only set when we're called from MSWOnInitDone(), test if
208 // this is the case
209 HWND hwnd = GetHwnd();
210 if ( hwnd )
211 {
212 // size of the dialog can't be changed because the controls are not
213 // laid out correctly then
214 ::SetWindowPos(hwnd, HWND_TOP, x, y, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
215 }
216 else // just remember that we were requested to move the window
217 {
218 m_movedWindow = true;
219
220 // if Centre() had been called before, it shouldn't be taken into
221 // account now
222 m_centreDir = 0;
223 }
224 }
225
226 void wxColourDialog::DoGetSize(int *width, int *height) const
227 {
228 if ( width )
229 *width = gs_rectDialog.width;
230 if ( height )
231 *height = gs_rectDialog.height;
232 }
233
234 void wxColourDialog::DoGetClientSize(int *width, int *height) const
235 {
236 if ( width )
237 *width = gs_rectDialog.width;
238 if ( height )
239 *height = gs_rectDialog.height;
240 }
241
242 void wxColourDialog::MSWOnInitDone(WXHWND hDlg)
243 {
244 // set HWND so that our DoMoveWindow() works correctly
245 SetHWND(hDlg);
246
247 if ( m_centreDir )
248 {
249 // now we have the real dialog size, remember it
250 RECT rect;
251 ::GetWindowRect((HWND)hDlg, &rect);
252 gs_rectDialog = wxRectFromRECT(rect);
253
254 // and position the window correctly: notice that we must use the base
255 // class version as our own doesn't do anything except setting flags
256 wxDialog::DoCentre(m_centreDir);
257 }
258 else if ( m_movedWindow ) // need to just move it to the correct place
259 {
260 SetPosition(GetPosition());
261 }
262
263 // we shouldn't destroy hDlg, so disassociate from it
264 SetHWND(NULL);
265 }
266
267 #endif // wxUSE_COLOURDLG && !(__SMARTPHONE__ && __WXWINCE__)