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" 
  32     #include "wx/msw/wrapcdlg.h" 
  34     #include "wx/colour.h" 
  35     #include "wx/gdicmn.h" 
  37     #include "wx/dialog.h" 
  38     #include "wx/cmndata.h" 
  42 #include "wx/msw/private.h" 
  47 // ---------------------------------------------------------------------------- 
  49 // ---------------------------------------------------------------------------- 
  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); 
  56 // ---------------------------------------------------------------------------- 
  58 // ---------------------------------------------------------------------------- 
  60 IMPLEMENT_DYNAMIC_CLASS(wxColourDialog
, wxDialog
) 
  62 // ============================================================================ 
  64 // ============================================================================ 
  66 // ---------------------------------------------------------------------------- 
  67 // colour dialog hook proc 
  68 // ---------------------------------------------------------------------------- 
  71 wxColourDialogHookProc(HWND hwnd
, 
  73                        WPARAM 
WXUNUSED(wParam
), 
  76     if ( uiMsg 
== WM_INITDIALOG 
) 
  78         CHOOSECOLOR 
*pCC 
= (CHOOSECOLOR 
*)lParam
; 
  79         wxColourDialog 
* const 
  80             dialog 
= wx_reinterpret_cast(wxColourDialog 
*, pCC
->lCustData
); 
  82         const wxString title 
= dialog
->GetTitle(); 
  84             ::SetWindowText(hwnd
, title
.wx_str()); 
  86         dialog
->MSWOnInitDone((WXHWND
)hwnd
); 
  92 // ---------------------------------------------------------------------------- 
  94 // ---------------------------------------------------------------------------- 
  96 void wxColourDialog::Init() 
  98     m_movedWindow 
= false; 
 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 
 108 bool wxColourDialog::Create(wxWindow 
*parent
, wxColourData 
*data
) 
 112         m_colourData 
= *data
; 
 117 int wxColourDialog::ShowModal() 
 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
.m_custColours
[i
].IsOk() ) 
 130             custColours
[i
] = wxColourToRGB(m_colourData
.m_custColours
[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
.m_dataColour
); 
 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(); 
 155             wxLogError(_("Colour selection dialog failed with error %0lx."), err
); 
 161     // transfer the values chosen by user back into m_colourData 
 162     for ( i 
= 0; i 
< WXSIZEOF(custColours
); i
++ ) 
 164       wxRGBToColour(m_colourData
.m_custColours
[i
], custColours
[i
]); 
 167     wxRGBToColour(m_colourData
.m_dataColour
, chooseColorStruct
.rgbResult
); 
 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); 
 177 // ---------------------------------------------------------------------------- 
 179 // ---------------------------------------------------------------------------- 
 181 void wxColourDialog::SetTitle(const wxString
& title
) 
 186 wxString 
wxColourDialog::GetTitle() const 
 191 // ---------------------------------------------------------------------------- 
 193 // ---------------------------------------------------------------------------- 
 195 void wxColourDialog::DoGetPosition(int *x
, int *y
) const 
 198         *x 
= gs_rectDialog
.x
; 
 200         *y 
= gs_rectDialog
.y
; 
 203 void wxColourDialog::DoCentre(int dir
) 
 207     // it's unnecessary to do anything else at this stage as we'll redo it in 
 208     // MSWOnInitDone() anyhow 
 211 void wxColourDialog::DoMoveWindow(int x
, int y
, int WXUNUSED(w
), int WXUNUSED(h
)) 
 216     // our HWND is only set when we're called from MSWOnInitDone(), test if 
 218     HWND hwnd 
= GetHwnd(); 
 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
); 
 225     else // just remember that we were requested to move the window 
 227         m_movedWindow 
= true; 
 229         // if Centre() had been called before, it shouldn't be taken into 
 235 void wxColourDialog::DoGetSize(int *width
, int *height
) const 
 238         *width 
= gs_rectDialog
.width
; 
 240         *height 
= gs_rectDialog
.height
; 
 243 void wxColourDialog::DoGetClientSize(int *width
, int *height
) const 
 246         *width 
= gs_rectDialog
.width
; 
 248         *height 
= gs_rectDialog
.height
; 
 251 void wxColourDialog::MSWOnInitDone(WXHWND hDlg
) 
 253     // set HWND so that our DoMoveWindow() works correctly 
 258         // now we have the real dialog size, remember it 
 260         ::GetWindowRect((HWND
)hDlg
, &rect
); 
 261         gs_rectDialog 
= wxRectFromRECT(rect
); 
 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
); 
 267     else if ( m_movedWindow 
) // need to just move it to the correct place 
 269         SetPosition(GetPosition()); 
 272     // we shouldn't destroy hDlg, so disassociate from it 
 276 #endif // wxUSE_COLOURDLG && !(__SMARTPHONE__ && __WXWINCE__)