1998-05-20 10:12:05 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: msw/checklst.cpp
|
|
|
|
// Purpose: implementation of wxCheckListBox class
|
|
|
|
// Author: Vadim Zeitlin
|
1998-08-27 19:32:09 -04:00
|
|
|
// Modified by:
|
1998-05-20 10:12:05 -04:00
|
|
|
// Created: 16.11.97
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
|
|
|
|
// Licence: wxWindows license
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
1999-02-28 10:35:51 -05:00
|
|
|
// ============================================================================
|
|
|
|
// declarations
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// headers
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
1998-05-20 10:12:05 -04:00
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation "checklst.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// For compilers that support precompilation, includes "wx.h".
|
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
1998-09-25 09:28:52 -04:00
|
|
|
#if wxUSE_OWNER_DRAWN
|
1998-06-22 18:43:48 -04:00
|
|
|
|
1999-01-07 03:43:47 -05:00
|
|
|
#include "wx/object.h"
|
|
|
|
#include "wx/colour.h"
|
|
|
|
#include "wx/font.h"
|
|
|
|
#include "wx/bitmap.h"
|
|
|
|
#include "wx/window.h"
|
|
|
|
#include "wx/listbox.h"
|
1998-05-20 10:12:05 -04:00
|
|
|
#include "wx/ownerdrw.h"
|
1999-01-07 03:43:47 -05:00
|
|
|
#include "wx/settings.h"
|
|
|
|
#include "wx/dcmemory.h"
|
1998-05-20 10:12:05 -04:00
|
|
|
#include "wx/msw/checklst.h"
|
1999-05-06 04:38:01 -04:00
|
|
|
#include "wx/log.h"
|
1998-05-20 10:12:05 -04:00
|
|
|
|
1999-01-09 15:18:06 -05:00
|
|
|
#include <windows.h>
|
1999-02-28 10:35:51 -05:00
|
|
|
#include <windowsx.h>
|
|
|
|
|
1999-08-03 20:42:51 -04:00
|
|
|
#if defined(__GNUWIN32__) && !defined(wxUSE_NORLANDER_HEADERS)
|
1999-02-28 18:39:59 -05:00
|
|
|
#include "wx/msw/gnuwin32/extra.h"
|
|
|
|
#endif
|
|
|
|
|
1999-02-28 10:35:51 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// private functions
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// get item (converted to right type)
|
|
|
|
#define GetItem(n) ((wxCheckListBoxItem *)(GetItem(n)))
|
1999-01-09 15:18:06 -05:00
|
|
|
|
1998-05-20 10:12:05 -04:00
|
|
|
// ============================================================================
|
|
|
|
// implementation
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
#if !USE_SHARED_LIBRARY
|
|
|
|
IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// declaration and implementation of wxCheckListBoxItem class
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class wxCheckListBoxItem : public wxOwnerDrawn
|
|
|
|
{
|
1999-02-28 10:35:51 -05:00
|
|
|
friend class wxCheckListBox;
|
1998-05-20 10:12:05 -04:00
|
|
|
public:
|
|
|
|
// ctor
|
1998-08-18 11:36:12 -04:00
|
|
|
wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex);
|
1998-05-20 10:12:05 -04:00
|
|
|
|
|
|
|
// drawing functions
|
|
|
|
virtual bool OnDrawItem(wxDC& dc, const wxRect& rc, wxODAction act, wxODStatus stat);
|
|
|
|
|
|
|
|
// simple accessors
|
|
|
|
bool IsChecked() const { return m_bChecked; }
|
1999-02-28 10:35:51 -05:00
|
|
|
void Check(bool bCheck);
|
|
|
|
void Toggle() { Check(!IsChecked()); }
|
1998-05-20 10:12:05 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
bool m_bChecked;
|
|
|
|
wxCheckListBox *m_pParent;
|
1998-08-18 11:36:12 -04:00
|
|
|
size_t m_nIndex;
|
1998-05-20 10:12:05 -04:00
|
|
|
};
|
|
|
|
|
1998-08-18 11:36:12 -04:00
|
|
|
wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex)
|
1998-05-20 10:12:05 -04:00
|
|
|
: wxOwnerDrawn("", TRUE) // checkable
|
|
|
|
{
|
|
|
|
m_bChecked = FALSE;
|
|
|
|
m_pParent = pParent;
|
|
|
|
m_nIndex = nIndex;
|
|
|
|
|
|
|
|
// we don't initialize m_nCheckHeight/Width vars because it's
|
|
|
|
// done in OnMeasure while they are used only in OnDraw and we
|
|
|
|
// know that there will always be OnMeasure before OnDraw
|
|
|
|
|
|
|
|
// fix appearance
|
|
|
|
SetMarginWidth(GetDefaultMarginWidth());
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* JACS - I've got the owner-draw stuff partially working with WIN16,
|
|
|
|
* with a really horrible-looking cross for wxCheckListBox instead of a
|
|
|
|
* check - could use a bitmap check-mark instead, defined in wx.rc.
|
|
|
|
* Also there's a refresh problem whereby it doesn't always draw the
|
|
|
|
* check until you click to the right of it, which is OK for WIN32.
|
|
|
|
*/
|
|
|
|
|
1998-08-27 19:32:09 -04:00
|
|
|
bool wxCheckListBoxItem::OnDrawItem(wxDC& dc, const wxRect& rc,
|
1998-05-20 10:12:05 -04:00
|
|
|
wxODAction act, wxODStatus stat)
|
|
|
|
{
|
|
|
|
if ( IsChecked() )
|
|
|
|
stat = (wxOwnerDrawn::wxODStatus)(stat | wxOwnerDrawn::wxODChecked);
|
|
|
|
|
|
|
|
if ( wxOwnerDrawn::OnDrawItem(dc, rc, act, stat) ) {
|
|
|
|
// ## using native API for performance and precision
|
1998-08-18 11:36:12 -04:00
|
|
|
size_t nCheckWidth = GetDefaultMarginWidth(),
|
1998-05-20 10:12:05 -04:00
|
|
|
nCheckHeight = m_pParent->GetItemHeight();
|
1998-08-27 19:32:09 -04:00
|
|
|
|
|
|
|
int x = rc.GetX(),
|
1998-05-20 10:12:05 -04:00
|
|
|
y = rc.GetY();
|
|
|
|
|
|
|
|
HDC hdc = (HDC)dc.GetHDC();
|
|
|
|
|
|
|
|
// create pens
|
|
|
|
HPEN hpenBack = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_WINDOW)),
|
|
|
|
hpenGray = CreatePen(PS_SOLID, 0, RGB(128, 128, 128)),
|
1998-08-27 19:32:09 -04:00
|
|
|
hpenPrev = (HPEN)SelectObject(hdc, hpenBack);
|
1998-05-20 10:12:05 -04:00
|
|
|
|
|
|
|
// we erase the 1-pixel border
|
|
|
|
Rectangle(hdc, x, y, x + nCheckWidth, y + nCheckHeight);
|
|
|
|
|
|
|
|
// shift check mark 1 pixel to the right (it looks better like this)
|
|
|
|
x++;
|
|
|
|
|
|
|
|
if ( IsChecked() ) {
|
|
|
|
// first create a monochrome bitmap in a memory DC
|
|
|
|
HDC hdcMem = CreateCompatibleDC(hdc);
|
|
|
|
HBITMAP hbmpCheck = CreateBitmap(nCheckWidth, nCheckHeight, 1, 1, 0);
|
1998-08-27 19:32:09 -04:00
|
|
|
HBITMAP hbmpOld = (HBITMAP)SelectObject(hdcMem, hbmpCheck);
|
1998-05-20 10:12:05 -04:00
|
|
|
|
|
|
|
// then draw a check mark into it
|
|
|
|
RECT rect = { 0, 0, nCheckWidth, nCheckHeight };
|
|
|
|
|
|
|
|
#ifdef __WIN32__
|
1999-01-07 03:43:47 -05:00
|
|
|
#ifndef __SC__
|
1998-05-20 10:12:05 -04:00
|
|
|
DrawFrameControl(hdcMem, &rect, DFC_MENU, DFCS_MENUCHECK);
|
1999-01-07 03:43:47 -05:00
|
|
|
#endif
|
1998-05-20 10:12:05 -04:00
|
|
|
#else
|
|
|
|
// In WIN16, draw a cross
|
|
|
|
HPEN blackPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
|
1999-06-14 03:12:16 -04:00
|
|
|
HPEN whiteBrush = (HPEN)GetStockObject(WHITE_BRUSH);
|
|
|
|
HPEN hPenOld = (HPEN)::SelectObject(hdcMem, blackPen);
|
|
|
|
HPEN hBrushOld = (HPEN)::SelectObject(hdcMem, whiteBrush);
|
1998-05-20 10:12:05 -04:00
|
|
|
::SetROP2(hdcMem, R2_COPYPEN);
|
|
|
|
Rectangle(hdcMem, 0, 0, nCheckWidth, nCheckHeight);
|
|
|
|
MoveTo(hdcMem, 0, 0);
|
|
|
|
LineTo(hdcMem, nCheckWidth, nCheckHeight);
|
|
|
|
MoveTo(hdcMem, nCheckWidth, 0);
|
|
|
|
LineTo(hdcMem, 0, nCheckHeight);
|
|
|
|
::SelectObject(hdcMem, hPenOld);
|
|
|
|
::SelectObject(hdcMem, hBrushOld);
|
|
|
|
::DeleteObject(blackPen);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// finally copy it to screen DC and clean up
|
1998-08-27 19:32:09 -04:00
|
|
|
BitBlt(hdc, x, y, nCheckWidth - 1, nCheckHeight,
|
1998-05-20 10:12:05 -04:00
|
|
|
hdcMem, 0, 0, SRCCOPY);
|
|
|
|
|
|
|
|
SelectObject(hdcMem, hbmpOld);
|
|
|
|
DeleteObject(hbmpCheck);
|
|
|
|
DeleteDC(hdcMem);
|
|
|
|
}
|
|
|
|
|
|
|
|
// now we draw the smaller rectangle
|
|
|
|
y++;
|
|
|
|
nCheckWidth -= 2;
|
|
|
|
nCheckHeight -= 2;
|
|
|
|
|
|
|
|
// draw hollow gray rectangle
|
|
|
|
(void)SelectObject(hdc, hpenGray);
|
1998-08-27 19:32:09 -04:00
|
|
|
HBRUSH hbrPrev = (HBRUSH)SelectObject(hdc, GetStockObject(NULL_BRUSH));
|
1998-05-20 10:12:05 -04:00
|
|
|
Rectangle(hdc, x, y, x + nCheckWidth, y + nCheckHeight);
|
|
|
|
|
|
|
|
// clean up
|
|
|
|
(void)SelectObject(hdc, hpenPrev);
|
|
|
|
(void)SelectObject(hdc, hbrPrev);
|
|
|
|
|
|
|
|
DeleteObject(hpenBack);
|
|
|
|
DeleteObject(hpenGray);
|
|
|
|
|
|
|
|
/*
|
|
|
|
dc.DrawRectangle(x, y, nCheckWidth, nCheckHeight);
|
|
|
|
|
|
|
|
if ( IsChecked() ) {
|
|
|
|
dc.DrawLine(x, y, x + nCheckWidth, y + nCheckHeight);
|
|
|
|
dc.DrawLine(x, y + nCheckHeight, x + nCheckWidth, y);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// change the state of the item and redraw it
|
1999-02-28 10:35:51 -05:00
|
|
|
void wxCheckListBoxItem::Check(bool check)
|
1998-08-27 19:32:09 -04:00
|
|
|
{
|
1999-02-28 10:35:51 -05:00
|
|
|
m_bChecked = check;
|
1998-05-20 10:12:05 -04:00
|
|
|
|
1999-02-28 10:35:51 -05:00
|
|
|
// index may be chanegd because new items were added/deleted
|
|
|
|
if ( m_pParent->GetItemIndex(this) != (int)m_nIndex )
|
|
|
|
{
|
|
|
|
// update it
|
|
|
|
int index = m_pParent->GetItemIndex(this);
|
|
|
|
|
1999-06-14 10:39:11 -04:00
|
|
|
wxASSERT_MSG( index != wxNOT_FOUND, _T("what does this item do here?") );
|
1999-02-28 10:35:51 -05:00
|
|
|
|
|
|
|
m_nIndex = (size_t)index;
|
|
|
|
}
|
1998-05-20 10:12:05 -04:00
|
|
|
|
1999-03-24 18:38:24 -05:00
|
|
|
HWND hwndListbox = (HWND)m_pParent->GetHWND();
|
|
|
|
|
|
|
|
#ifdef __WIN32__
|
|
|
|
RECT rcUpdate;
|
1999-05-28 13:27:55 -04:00
|
|
|
|
1999-03-24 18:38:24 -05:00
|
|
|
if ( ::SendMessage(hwndListbox, LB_GETITEMRECT,
|
|
|
|
m_nIndex, (LPARAM)&rcUpdate) == LB_ERR )
|
|
|
|
{
|
1999-06-14 10:39:11 -04:00
|
|
|
wxLogDebug(_T("LB_GETITEMRECT failed"));
|
1999-03-24 18:38:24 -05:00
|
|
|
}
|
|
|
|
#else // Win16
|
|
|
|
// FIXME this doesn't work if the listbox is scrolled!
|
|
|
|
size_t nHeight = m_pParent->GetItemHeight();
|
|
|
|
size_t y = m_nIndex * nHeight;
|
|
|
|
RECT rcUpdate = { 0, y, GetDefaultMarginWidth(), y + nHeight};
|
|
|
|
#endif // Win32/16
|
|
|
|
|
|
|
|
InvalidateRect(hwndListbox, &rcUpdate, FALSE);
|
1999-05-28 13:27:55 -04:00
|
|
|
|
1999-02-28 10:35:51 -05:00
|
|
|
wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, m_pParent->GetId());
|
|
|
|
event.SetInt(m_nIndex);
|
|
|
|
event.SetEventObject(m_pParent);
|
|
|
|
m_pParent->ProcessCommand(event);
|
1998-05-20 10:12:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// implementation of wxCheckListBox class
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// define event table
|
|
|
|
// ------------------
|
|
|
|
BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
|
|
|
|
EVT_CHAR(wxCheckListBox::OnChar)
|
|
|
|
EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick)
|
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
|
|
|
// control creation
|
|
|
|
// ----------------
|
|
|
|
|
|
|
|
// def ctor: use Create() to really create the control
|
|
|
|
wxCheckListBox::wxCheckListBox() : wxListBox()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// ctor which creates the associated control
|
1998-07-04 11:17:59 -04:00
|
|
|
wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
|
1998-05-20 10:12:05 -04:00
|
|
|
const wxPoint& pos, const wxSize& size,
|
1998-07-04 11:17:59 -04:00
|
|
|
int nStrings, const wxString choices[],
|
|
|
|
long style, const wxValidator& val,
|
1999-02-28 10:35:51 -05:00
|
|
|
const wxString& name)
|
1998-05-20 10:12:05 -04:00
|
|
|
: wxListBox()
|
|
|
|
{
|
1999-02-28 10:35:51 -05:00
|
|
|
Create(parent, id, pos, size, nStrings, choices,
|
|
|
|
style | wxLB_OWNERDRAW, val, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void wxCheckListBox::Delete(int N)
|
|
|
|
{
|
|
|
|
wxCHECK_RET( N >= 0 && N < m_noItems,
|
1999-06-14 10:39:11 -04:00
|
|
|
_T("invalid index in wxListBox::Delete") );
|
1999-02-28 10:35:51 -05:00
|
|
|
|
|
|
|
wxListBox::Delete(N);
|
|
|
|
|
|
|
|
// free memory
|
|
|
|
delete m_aItems[N];
|
|
|
|
|
|
|
|
m_aItems.Remove(N);
|
|
|
|
}
|
|
|
|
|
|
|
|
void wxCheckListBox::InsertItems(int nItems, const wxString items[], int pos)
|
|
|
|
{
|
|
|
|
wxCHECK_RET( pos >= 0 && pos <= m_noItems,
|
1999-06-14 10:39:11 -04:00
|
|
|
_T("invalid index in wxCheckListBox::InsertItems") );
|
1999-02-28 10:35:51 -05:00
|
|
|
|
|
|
|
wxListBox::InsertItems(nItems, items, pos);
|
|
|
|
|
|
|
|
int i;
|
|
|
|
for ( i = 0; i < nItems; i++ ) {
|
|
|
|
wxOwnerDrawn *pNewItem = CreateItem((size_t)(pos + i));
|
|
|
|
pNewItem->SetName(items[i]);
|
|
|
|
m_aItems.Insert(pNewItem, (size_t)(pos + i));
|
|
|
|
ListBox_SetItemData((HWND)GetHWND(), i + pos, pNewItem);
|
|
|
|
}
|
1998-05-20 10:12:05 -04:00
|
|
|
}
|
|
|
|
|
1999-05-28 13:27:55 -04:00
|
|
|
|
|
|
|
bool wxCheckListBox::SetFont( const wxFont &font )
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
for (i=0; i < m_aItems.GetCount(); i++)
|
|
|
|
m_aItems[i]->SetFont(font);
|
|
|
|
wxListBox::SetFont(font);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
1998-05-20 10:12:05 -04:00
|
|
|
// create/retrieve item
|
|
|
|
// --------------------
|
|
|
|
|
|
|
|
// create a check list box item
|
1998-08-18 11:36:12 -04:00
|
|
|
wxOwnerDrawn *wxCheckListBox::CreateItem(size_t nIndex)
|
1998-05-20 10:12:05 -04:00
|
|
|
{
|
|
|
|
wxCheckListBoxItem *pItem = new wxCheckListBoxItem(this, nIndex);
|
|
|
|
return pItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return item size
|
|
|
|
// ----------------
|
|
|
|
bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
|
|
|
|
{
|
|
|
|
if ( wxListBox::MSWOnMeasure(item) ) {
|
|
|
|
MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
|
|
|
|
|
|
|
|
// save item height
|
|
|
|
m_nItemHeight = pStruct->itemHeight;
|
|
|
|
|
|
|
|
// add place for the check mark
|
|
|
|
pStruct->itemWidth += wxOwnerDrawn::GetDefaultMarginWidth();
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check items
|
|
|
|
// -----------
|
|
|
|
|
1998-08-18 11:36:12 -04:00
|
|
|
bool wxCheckListBox::IsChecked(size_t uiIndex) const
|
1998-05-20 10:12:05 -04:00
|
|
|
{
|
|
|
|
return GetItem(uiIndex)->IsChecked();
|
|
|
|
}
|
|
|
|
|
1998-08-18 11:36:12 -04:00
|
|
|
void wxCheckListBox::Check(size_t uiIndex, bool bCheck)
|
1998-05-20 10:12:05 -04:00
|
|
|
{
|
|
|
|
GetItem(uiIndex)->Check(bCheck);
|
|
|
|
}
|
|
|
|
|
|
|
|
// process events
|
|
|
|
// --------------
|
|
|
|
|
|
|
|
void wxCheckListBox::OnChar(wxKeyEvent& event)
|
|
|
|
{
|
|
|
|
if ( event.KeyCode() == WXK_SPACE )
|
|
|
|
GetItem(GetSelection())->Toggle();
|
|
|
|
else
|
1999-05-13 17:21:04 -04:00
|
|
|
event.Skip();
|
1998-05-20 10:12:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void wxCheckListBox::OnLeftClick(wxMouseEvent& event)
|
|
|
|
{
|
|
|
|
// clicking on the item selects it, clicking on the checkmark toggles
|
1998-05-27 19:30:50 -04:00
|
|
|
if ( event.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() ) {
|
1999-03-24 18:38:24 -05:00
|
|
|
#ifdef __WIN32__
|
|
|
|
size_t nItem = (size_t)::SendMessage
|
|
|
|
(
|
|
|
|
(HWND)GetHWND(),
|
|
|
|
LB_ITEMFROMPOINT,
|
|
|
|
0,
|
|
|
|
MAKELPARAM(event.GetX(), event.GetY())
|
|
|
|
);
|
|
|
|
#else // Win16
|
|
|
|
// FIXME this doesn't work when the listbox is scrolled!
|
|
|
|
size_t nItem = ((size_t)event.GetY()) / m_nItemHeight;
|
|
|
|
#endif // Win32/16
|
|
|
|
|
1998-08-18 11:36:12 -04:00
|
|
|
if ( nItem < (size_t)m_noItems )
|
1998-05-20 10:12:05 -04:00
|
|
|
GetItem(nItem)->Toggle();
|
|
|
|
//else: it's not an error, just click outside of client zone
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// implement default behaviour: clicking on the item selects it
|
|
|
|
event.Skip();
|
|
|
|
}
|
|
|
|
}
|
1998-06-22 18:43:48 -04:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|