1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/colordlg.cpp
3 // Purpose: wxColourDialog class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #if wxUSE_COLOURDLG && !(defined(__SMARTPHONE__) && defined(__WXWINCE__))
29 #include "wx/colordlg.h"
30 #include "wx/modalhook.h"
33 #include "wx/msw/wrapcdlg.h"
35 #include "wx/colour.h"
36 #include "wx/gdicmn.h"
41 #include "wx/msw/private.h"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
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);
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 IMPLEMENT_DYNAMIC_CLASS(wxColourDialog
, wxDialog
)
61 // ============================================================================
63 // ============================================================================
65 // ----------------------------------------------------------------------------
66 // colour dialog hook proc
67 // ----------------------------------------------------------------------------
70 wxColourDialogHookProc(HWND hwnd
,
72 WPARAM
WXUNUSED(wParam
),
75 if ( uiMsg
== WM_INITDIALOG
)
77 CHOOSECOLOR
*pCC
= (CHOOSECOLOR
*)lParam
;
78 wxColourDialog
* const
79 dialog
= reinterpret_cast<wxColourDialog
*>(pCC
->lCustData
);
81 const wxString title
= dialog
->GetTitle();
83 ::SetWindowText(hwnd
, title
.t_str());
85 dialog
->MSWOnInitDone((WXHWND
)hwnd
);
91 // ----------------------------------------------------------------------------
93 // ----------------------------------------------------------------------------
95 void wxColourDialog::Init()
97 m_movedWindow
= false;
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
107 bool wxColourDialog::Create(wxWindow
*parent
, wxColourData
*data
)
111 m_colourData
= *data
;
116 int wxColourDialog::ShowModal()
118 WX_HOOK_MODAL_DIALOG();
120 // initialize the struct used by Windows
121 CHOOSECOLOR chooseColorStruct
;
122 memset(&chooseColorStruct
, 0, sizeof(CHOOSECOLOR
));
126 // and transfer data from m_colourData to it
127 COLORREF custColours
[16];
128 for ( i
= 0; i
< WXSIZEOF(custColours
); i
++ )
130 if ( m_colourData
.GetCustomColour(i
).IsOk() )
131 custColours
[i
] = wxColourToRGB(m_colourData
.GetCustomColour(i
));
133 custColours
[i
] = RGB(255,255,255);
136 chooseColorStruct
.lStructSize
= sizeof(CHOOSECOLOR
);
138 chooseColorStruct
.hwndOwner
= GetHwndOf(m_parent
);
139 chooseColorStruct
.rgbResult
= wxColourToRGB(m_colourData
.GetColour());
140 chooseColorStruct
.lpCustColors
= custColours
;
142 chooseColorStruct
.Flags
= CC_RGBINIT
| CC_ENABLEHOOK
;
143 chooseColorStruct
.lCustData
= (LPARAM
)this;
144 chooseColorStruct
.lpfnHook
= wxColourDialogHookProc
;
146 if ( m_colourData
.GetChooseFull() )
147 chooseColorStruct
.Flags
|= CC_FULLOPEN
;
149 // do show the modal dialog
150 if ( !::ChooseColor(&chooseColorStruct
) )
152 // 0 error means the dialog was simply cancelled, i.e. no real error
154 const DWORD err
= CommDlgExtendedError();
157 wxLogError(_("Colour selection dialog failed with error %0lx."), err
);
164 // transfer the values chosen by user back into m_colourData
165 for ( i
= 0; i
< WXSIZEOF(custColours
); i
++ )
167 wxRGBToColour(m_colourData
.m_custColours
[i
], custColours
[i
]);
170 wxRGBToColour(m_colourData
.GetColour(), chooseColorStruct
.rgbResult
);
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);
180 // ----------------------------------------------------------------------------
182 // ----------------------------------------------------------------------------
184 void wxColourDialog::SetTitle(const wxString
& title
)
189 wxString
wxColourDialog::GetTitle() const
194 // ----------------------------------------------------------------------------
196 // ----------------------------------------------------------------------------
198 void wxColourDialog::DoGetPosition(int *x
, int *y
) const
201 *x
= gs_rectDialog
.x
;
203 *y
= gs_rectDialog
.y
;
206 void wxColourDialog::DoCentre(int dir
)
210 // it's unnecessary to do anything else at this stage as we'll redo it in
211 // MSWOnInitDone() anyhow
214 void wxColourDialog::DoMoveWindow(int x
, int y
, int WXUNUSED(w
), int WXUNUSED(h
))
219 // our HWND is only set when we're called from MSWOnInitDone(), test if
221 HWND hwnd
= GetHwnd();
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
);
228 else // just remember that we were requested to move the window
230 m_movedWindow
= true;
232 // if Centre() had been called before, it shouldn't be taken into
238 void wxColourDialog::DoGetSize(int *width
, int *height
) const
241 *width
= gs_rectDialog
.width
;
243 *height
= gs_rectDialog
.height
;
246 void wxColourDialog::DoGetClientSize(int *width
, int *height
) const
249 *width
= gs_rectDialog
.width
;
251 *height
= gs_rectDialog
.height
;
254 void wxColourDialog::MSWOnInitDone(WXHWND hDlg
)
256 // set HWND so that our DoMoveWindow() works correctly
261 // now we have the real dialog size, remember it
263 ::GetWindowRect((HWND
)hDlg
, &rect
);
264 gs_rectDialog
= wxRectFromRECT(rect
);
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
);
270 else if ( m_movedWindow
) // need to just move it to the correct place
272 SetPosition(GetPosition());
275 // we shouldn't destroy hDlg, so disassociate from it
279 #endif // wxUSE_COLOURDLG && !(__SMARTPHONE__ && __WXWINCE__)