| 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: msw/checklst.cpp |
| 3 | // Purpose: implementation of wxCheckListBox class |
| 4 | // Author: Vadim Zeitlin |
| 5 | // Modified by: |
| 6 | // Created: 16.11.97 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> |
| 9 | // Licence: wxWindows license |
| 10 | /////////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // ============================================================================ |
| 13 | // headers & declarations |
| 14 | // ============================================================================ |
| 15 | |
| 16 | #ifdef __GNUG__ |
| 17 | #pragma implementation "checklst.h" |
| 18 | #endif |
| 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 USE_OWNER_DRAWN |
| 28 | |
| 29 | #include <windows.h> |
| 30 | |
| 31 | #include "wx/ownerdrw.h" |
| 32 | #include "wx/msw/checklst.h" |
| 33 | |
| 34 | // ============================================================================ |
| 35 | // implementation |
| 36 | // ============================================================================ |
| 37 | |
| 38 | #if !USE_SHARED_LIBRARY |
| 39 | IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox) |
| 40 | #endif |
| 41 | |
| 42 | // ---------------------------------------------------------------------------- |
| 43 | // declaration and implementation of wxCheckListBoxItem class |
| 44 | // ---------------------------------------------------------------------------- |
| 45 | |
| 46 | class wxCheckListBoxItem : public wxOwnerDrawn |
| 47 | { |
| 48 | public: |
| 49 | // ctor |
| 50 | wxCheckListBoxItem(wxCheckListBox *pParent, uint nIndex); |
| 51 | |
| 52 | // drawing functions |
| 53 | virtual bool OnDrawItem(wxDC& dc, const wxRect& rc, wxODAction act, wxODStatus stat); |
| 54 | |
| 55 | // simple accessors |
| 56 | bool IsChecked() const { return m_bChecked; } |
| 57 | void Check(bool bCheck) { m_bChecked = bCheck; } |
| 58 | void Toggle(); |
| 59 | |
| 60 | private: |
| 61 | bool m_bChecked; |
| 62 | wxCheckListBox *m_pParent; |
| 63 | uint m_nIndex; |
| 64 | }; |
| 65 | |
| 66 | wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox *pParent, uint nIndex) |
| 67 | : wxOwnerDrawn("", TRUE) // checkable |
| 68 | { |
| 69 | m_bChecked = FALSE; |
| 70 | m_pParent = pParent; |
| 71 | m_nIndex = nIndex; |
| 72 | |
| 73 | // we don't initialize m_nCheckHeight/Width vars because it's |
| 74 | // done in OnMeasure while they are used only in OnDraw and we |
| 75 | // know that there will always be OnMeasure before OnDraw |
| 76 | |
| 77 | // fix appearance |
| 78 | SetFont(wxSystemSettings::GetSystemFont(wxSYS_ANSI_VAR_FONT)); |
| 79 | SetMarginWidth(GetDefaultMarginWidth()); |
| 80 | } |
| 81 | |
| 82 | /* |
| 83 | * JACS - I've got the owner-draw stuff partially working with WIN16, |
| 84 | * with a really horrible-looking cross for wxCheckListBox instead of a |
| 85 | * check - could use a bitmap check-mark instead, defined in wx.rc. |
| 86 | * Also there's a refresh problem whereby it doesn't always draw the |
| 87 | * check until you click to the right of it, which is OK for WIN32. |
| 88 | */ |
| 89 | |
| 90 | bool wxCheckListBoxItem::OnDrawItem(wxDC& dc, const wxRect& rc, |
| 91 | wxODAction act, wxODStatus stat) |
| 92 | { |
| 93 | if ( IsChecked() ) |
| 94 | stat = (wxOwnerDrawn::wxODStatus)(stat | wxOwnerDrawn::wxODChecked); |
| 95 | |
| 96 | if ( wxOwnerDrawn::OnDrawItem(dc, rc, act, stat) ) { |
| 97 | // ## using native API for performance and precision |
| 98 | uint nCheckWidth = GetDefaultMarginWidth(), |
| 99 | nCheckHeight = m_pParent->GetItemHeight(); |
| 100 | |
| 101 | int x = rc.GetX(), |
| 102 | y = rc.GetY(); |
| 103 | |
| 104 | HDC hdc = (HDC)dc.GetHDC(); |
| 105 | |
| 106 | // create pens |
| 107 | HPEN hpenBack = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_WINDOW)), |
| 108 | hpenGray = CreatePen(PS_SOLID, 0, RGB(128, 128, 128)), |
| 109 | hpenPrev = SelectObject(hdc, hpenBack); |
| 110 | |
| 111 | // we erase the 1-pixel border |
| 112 | Rectangle(hdc, x, y, x + nCheckWidth, y + nCheckHeight); |
| 113 | |
| 114 | // shift check mark 1 pixel to the right (it looks better like this) |
| 115 | x++; |
| 116 | |
| 117 | if ( IsChecked() ) { |
| 118 | // first create a monochrome bitmap in a memory DC |
| 119 | HDC hdcMem = CreateCompatibleDC(hdc); |
| 120 | HBITMAP hbmpCheck = CreateBitmap(nCheckWidth, nCheckHeight, 1, 1, 0); |
| 121 | HBITMAP hbmpOld = SelectObject(hdcMem, hbmpCheck); |
| 122 | |
| 123 | // then draw a check mark into it |
| 124 | RECT rect = { 0, 0, nCheckWidth, nCheckHeight }; |
| 125 | |
| 126 | #ifdef __WIN32__ |
| 127 | DrawFrameControl(hdcMem, &rect, DFC_MENU, DFCS_MENUCHECK); |
| 128 | #else |
| 129 | // In WIN16, draw a cross |
| 130 | HPEN blackPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0)); |
| 131 | HPEN whiteBrush = GetStockObject(WHITE_BRUSH); |
| 132 | HPEN hPenOld = ::SelectObject(hdcMem, blackPen); |
| 133 | HPEN hBrushOld = ::SelectObject(hdcMem, whiteBrush); |
| 134 | ::SetROP2(hdcMem, R2_COPYPEN); |
| 135 | Rectangle(hdcMem, 0, 0, nCheckWidth, nCheckHeight); |
| 136 | MoveTo(hdcMem, 0, 0); |
| 137 | LineTo(hdcMem, nCheckWidth, nCheckHeight); |
| 138 | MoveTo(hdcMem, nCheckWidth, 0); |
| 139 | LineTo(hdcMem, 0, nCheckHeight); |
| 140 | ::SelectObject(hdcMem, hPenOld); |
| 141 | ::SelectObject(hdcMem, hBrushOld); |
| 142 | ::DeleteObject(blackPen); |
| 143 | #endif |
| 144 | |
| 145 | // finally copy it to screen DC and clean up |
| 146 | BitBlt(hdc, x, y, nCheckWidth - 1, nCheckHeight, |
| 147 | hdcMem, 0, 0, SRCCOPY); |
| 148 | |
| 149 | SelectObject(hdcMem, hbmpOld); |
| 150 | DeleteObject(hbmpCheck); |
| 151 | DeleteDC(hdcMem); |
| 152 | } |
| 153 | |
| 154 | // now we draw the smaller rectangle |
| 155 | y++; |
| 156 | nCheckWidth -= 2; |
| 157 | nCheckHeight -= 2; |
| 158 | |
| 159 | // draw hollow gray rectangle |
| 160 | (void)SelectObject(hdc, hpenGray); |
| 161 | HBRUSH hbrPrev = SelectObject(hdc, GetStockObject(NULL_BRUSH)); |
| 162 | Rectangle(hdc, x, y, x + nCheckWidth, y + nCheckHeight); |
| 163 | |
| 164 | // clean up |
| 165 | (void)SelectObject(hdc, hpenPrev); |
| 166 | (void)SelectObject(hdc, hbrPrev); |
| 167 | |
| 168 | DeleteObject(hpenBack); |
| 169 | DeleteObject(hpenGray); |
| 170 | |
| 171 | /* |
| 172 | dc.DrawRectangle(x, y, nCheckWidth, nCheckHeight); |
| 173 | |
| 174 | if ( IsChecked() ) { |
| 175 | dc.DrawLine(x, y, x + nCheckWidth, y + nCheckHeight); |
| 176 | dc.DrawLine(x, y + nCheckHeight, x + nCheckWidth, y); |
| 177 | } |
| 178 | */ |
| 179 | |
| 180 | return TRUE; |
| 181 | } |
| 182 | |
| 183 | return FALSE; |
| 184 | } |
| 185 | |
| 186 | // change the state of the item and redraw it |
| 187 | void wxCheckListBoxItem::Toggle() |
| 188 | { |
| 189 | m_bChecked = !m_bChecked; |
| 190 | |
| 191 | uint nHeight = m_pParent->GetItemHeight(); |
| 192 | uint y = m_nIndex * nHeight; |
| 193 | RECT rcUpdate = { 0, y, GetDefaultMarginWidth(), y + nHeight}; |
| 194 | InvalidateRect((HWND)m_pParent->GetHWND(), &rcUpdate, FALSE); |
| 195 | |
| 196 | wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, m_pParent->GetId()); |
| 197 | event.SetInt(m_nIndex); |
| 198 | event.SetEventObject(m_pParent); |
| 199 | m_pParent->ProcessCommand(event); |
| 200 | } |
| 201 | |
| 202 | // ---------------------------------------------------------------------------- |
| 203 | // implementation of wxCheckListBox class |
| 204 | // ---------------------------------------------------------------------------- |
| 205 | |
| 206 | // define event table |
| 207 | // ------------------ |
| 208 | BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox) |
| 209 | EVT_CHAR(wxCheckListBox::OnChar) |
| 210 | EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick) |
| 211 | END_EVENT_TABLE() |
| 212 | |
| 213 | // control creation |
| 214 | // ---------------- |
| 215 | |
| 216 | // def ctor: use Create() to really create the control |
| 217 | wxCheckListBox::wxCheckListBox() : wxListBox() |
| 218 | { |
| 219 | } |
| 220 | |
| 221 | // ctor which creates the associated control |
| 222 | wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id, |
| 223 | const wxPoint& pos, const wxSize& size, |
| 224 | int nStrings, const wxString choices[], |
| 225 | long style, const wxValidator& val, |
| 226 | const wxString& name) // , const wxFont& font) |
| 227 | // don't use ctor with arguments! we must call Create() |
| 228 | // ourselves from here. |
| 229 | : wxListBox() |
| 230 | // , m_font(font) |
| 231 | { |
| 232 | Create(parent, id, pos, size, nStrings, choices, style|wxLB_OWNERDRAW, val, name); |
| 233 | } |
| 234 | |
| 235 | // create/retrieve item |
| 236 | // -------------------- |
| 237 | |
| 238 | // create a check list box item |
| 239 | wxOwnerDrawn *wxCheckListBox::CreateItem(uint nIndex) |
| 240 | { |
| 241 | wxCheckListBoxItem *pItem = new wxCheckListBoxItem(this, nIndex); |
| 242 | if ( m_windowFont.Ok() ) |
| 243 | pItem->SetFont(m_windowFont); |
| 244 | |
| 245 | return pItem; |
| 246 | } |
| 247 | |
| 248 | // get item (converted to right type) |
| 249 | #define GetItem(n) ((wxCheckListBoxItem *)(GetItem(n))) |
| 250 | |
| 251 | // return item size |
| 252 | // ---------------- |
| 253 | bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item) |
| 254 | { |
| 255 | if ( wxListBox::MSWOnMeasure(item) ) { |
| 256 | MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item; |
| 257 | |
| 258 | // save item height |
| 259 | m_nItemHeight = pStruct->itemHeight; |
| 260 | |
| 261 | // add place for the check mark |
| 262 | pStruct->itemWidth += wxOwnerDrawn::GetDefaultMarginWidth(); |
| 263 | |
| 264 | return TRUE; |
| 265 | } |
| 266 | |
| 267 | return FALSE; |
| 268 | } |
| 269 | |
| 270 | // check items |
| 271 | // ----------- |
| 272 | |
| 273 | bool wxCheckListBox::IsChecked(uint uiIndex) const |
| 274 | { |
| 275 | return GetItem(uiIndex)->IsChecked(); |
| 276 | } |
| 277 | |
| 278 | void wxCheckListBox::Check(uint uiIndex, bool bCheck) |
| 279 | { |
| 280 | GetItem(uiIndex)->Check(bCheck); |
| 281 | } |
| 282 | |
| 283 | // process events |
| 284 | // -------------- |
| 285 | |
| 286 | void wxCheckListBox::OnChar(wxKeyEvent& event) |
| 287 | { |
| 288 | if ( event.KeyCode() == WXK_SPACE ) |
| 289 | GetItem(GetSelection())->Toggle(); |
| 290 | else |
| 291 | wxListBox::OnChar(event); |
| 292 | } |
| 293 | |
| 294 | void wxCheckListBox::OnLeftClick(wxMouseEvent& event) |
| 295 | { |
| 296 | // clicking on the item selects it, clicking on the checkmark toggles |
| 297 | if ( event.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() ) { |
| 298 | // # better use LB_ITEMFROMPOINT perhaps? |
| 299 | uint nItem = ((uint)event.GetY()) / m_nItemHeight; |
| 300 | if ( nItem < (uint)m_noItems ) |
| 301 | GetItem(nItem)->Toggle(); |
| 302 | //else: it's not an error, just click outside of client zone |
| 303 | } |
| 304 | else { |
| 305 | // implement default behaviour: clicking on the item selects it |
| 306 | event.Skip(); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | #endif |
| 311 | |