1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/colordlg.cpp
3 // Purpose: wxColourDialog class
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
26 #if wxUSE_COLOURDLG && !(defined(__SMARTPHONE__) && defined(__WXWINCE__))
28 #include "wx/colordlg.h"
29 #include "wx/modalhook.h"
32 #include "wx/msw/wrapcdlg.h"
34 #include "wx/colour.h"
35 #include "wx/gdicmn.h"
40 #include "wx/msw/private.h"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
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
52 static wxRect
gs_rectDialog(0, 0, 222, 324);
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 IMPLEMENT_DYNAMIC_CLASS(wxColourDialog
, wxDialog
)
60 // ============================================================================
62 // ============================================================================
64 // ----------------------------------------------------------------------------
65 // colour dialog hook proc
66 // ----------------------------------------------------------------------------
69 wxColourDialogHookProc(HWND hwnd
,
71 WPARAM
WXUNUSED(wParam
),
74 if ( uiMsg
== WM_INITDIALOG
)
76 CHOOSECOLOR
*pCC
= (CHOOSECOLOR
*)lParam
;
77 wxColourDialog
* const
78 dialog
= reinterpret_cast<wxColourDialog
*>(pCC
->lCustData
);
80 const wxString title
= dialog
->GetTitle();
82 ::SetWindowText(hwnd
, title
.t_str());
84 dialog
->MSWOnInitDone((WXHWND
)hwnd
);
90 // ----------------------------------------------------------------------------
92 // ----------------------------------------------------------------------------
94 void wxColourDialog::Init()
96 m_movedWindow
= false;
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
106 bool wxColourDialog::Create(wxWindow
*parent
, wxColourData
*data
)
110 m_colourData
= *data
;
115 int wxColourDialog::ShowModal()
117 WX_HOOK_MODAL_DIALOG();
119 // initialize the struct used by Windows
120 CHOOSECOLOR chooseColorStruct
;
121 memset(&chooseColorStruct
, 0, sizeof(CHOOSECOLOR
));
125 // and transfer data from m_colourData to it
126 COLORREF custColours
[16];
127 for ( i
= 0; i
< WXSIZEOF(custColours
); i
++ )
129 if ( m_colourData
.GetCustomColour(i
).IsOk() )
130 custColours
[i
] = wxColourToRGB(m_colourData
.GetCustomColour(i
));
132 custColours
[i
] = RGB(255,255,255);
135 chooseColorStruct
.lStructSize
= sizeof(CHOOSECOLOR
);
137 chooseColorStruct
.hwndOwner
= GetHwndOf(m_parent
);
138 chooseColorStruct
.rgbResult
= wxColourToRGB(m_colourData
.GetColour());
139 chooseColorStruct
.lpCustColors
= custColours
;
141 chooseColorStruct
.Flags
= CC_RGBINIT
| CC_ENABLEHOOK
;
142 chooseColorStruct
.lCustData
= (LPARAM
)this;
143 chooseColorStruct
.lpfnHook
= wxColourDialogHookProc
;
145 if ( m_colourData
.GetChooseFull() )
146 chooseColorStruct
.Flags
|= CC_FULLOPEN
;
148 // do show the modal dialog
149 if ( !::ChooseColor(&chooseColorStruct
) )
151 // 0 error means the dialog was simply cancelled, i.e. no real error
153 const DWORD err
= CommDlgExtendedError();
156 wxLogError(_("Colour selection dialog failed with error %0lx."), err
);
163 // transfer the values chosen by user back into m_colourData
164 for ( i
= 0; i
< WXSIZEOF(custColours
); i
++ )
166 wxRGBToColour(m_colourData
.m_custColours
[i
], custColours
[i
]);
169 wxRGBToColour(m_colourData
.GetColour(), chooseColorStruct
.rgbResult
);
171 // this doesn't seem to work (contrary to what MSDN implies) on current
172 // Windows versions: CC_FULLOPEN is never set on return if it wasn't
173 // initially set and vice versa
174 //m_colourData.SetChooseFull((chooseColorStruct.Flags & CC_FULLOPEN) != 0);
179 // ----------------------------------------------------------------------------
181 // ----------------------------------------------------------------------------
183 void wxColourDialog::SetTitle(const wxString
& title
)
188 wxString
wxColourDialog::GetTitle() const
193 // ----------------------------------------------------------------------------
195 // ----------------------------------------------------------------------------
197 void wxColourDialog::DoGetPosition(int *x
, int *y
) const
200 *x
= gs_rectDialog
.x
;
202 *y
= gs_rectDialog
.y
;
205 void wxColourDialog::DoCentre(int dir
)
209 // it's unnecessary to do anything else at this stage as we'll redo it in
210 // MSWOnInitDone() anyhow
213 void wxColourDialog::DoMoveWindow(int x
, int y
, int WXUNUSED(w
), int WXUNUSED(h
))
218 // our HWND is only set when we're called from MSWOnInitDone(), test if
220 HWND hwnd
= GetHwnd();
223 // size of the dialog can't be changed because the controls are not
224 // laid out correctly then
225 ::SetWindowPos(hwnd
, HWND_TOP
, x
, y
, 0, 0, SWP_NOZORDER
| SWP_NOSIZE
);
227 else // just remember that we were requested to move the window
229 m_movedWindow
= true;
231 // if Centre() had been called before, it shouldn't be taken into
237 void wxColourDialog::DoGetSize(int *width
, int *height
) const
240 *width
= gs_rectDialog
.width
;
242 *height
= gs_rectDialog
.height
;
245 void wxColourDialog::DoGetClientSize(int *width
, int *height
) const
248 *width
= gs_rectDialog
.width
;
250 *height
= gs_rectDialog
.height
;
253 void wxColourDialog::MSWOnInitDone(WXHWND hDlg
)
255 // set HWND so that our DoMoveWindow() works correctly
260 // now we have the real dialog size, remember it
262 ::GetWindowRect((HWND
)hDlg
, &rect
);
263 gs_rectDialog
= wxRectFromRECT(rect
);
265 // and position the window correctly: notice that we must use the base
266 // class version as our own doesn't do anything except setting flags
267 wxDialog::DoCentre(m_centreDir
);
269 else if ( m_movedWindow
) // need to just move it to the correct place
271 SetPosition(GetPosition());
274 // we shouldn't destroy hDlg, so disassociate from it
278 #endif // wxUSE_COLOURDLG && !(__SMARTPHONE__ && __WXWINCE__)