]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: wx/vlbox.h | |
3 | // Purpose: wxVListBox is a virtual listbox with lines of variable height | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 31.05.03 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org> | |
9 | // Licence: wxWindows licence | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifndef _WX_VLBOX_H_ | |
13 | #define _WX_VLBOX_H_ | |
14 | ||
15 | #include "wx/vscroll.h" // base class | |
16 | ||
17 | #define wxVListBoxNameStr _T("wxVListBox") | |
18 | ||
19 | // ---------------------------------------------------------------------------- | |
20 | // wxVListBox | |
21 | // ---------------------------------------------------------------------------- | |
22 | ||
23 | /* | |
24 | This class has two main differences from a regular listbox: it can have an | |
25 | arbitrarily huge number of items because it doesn't store them itself but | |
26 | uses OnDrawItem() callback to draw them and its items can have variable | |
27 | height as determined by OnMeasureItem(). | |
28 | ||
29 | It emits the same events as wxListBox and the same event macros may be used | |
30 | with it. | |
31 | */ | |
32 | class WXDLLEXPORT wxVListBox : public wxVScrolledWindow | |
33 | { | |
34 | public: | |
35 | // constructors and such | |
36 | // --------------------- | |
37 | ||
38 | // default constructor, you must call Create() later | |
39 | wxVListBox() { Init(); } | |
40 | ||
41 | // normal constructor which calls Create() internally | |
42 | wxVListBox(wxWindow *parent, | |
43 | wxWindowID id = wxID_ANY, | |
44 | const wxPoint& pos = wxDefaultPosition, | |
45 | const wxSize& size = wxDefaultSize, | |
46 | size_t countItems = 0, | |
47 | long style = 0, | |
48 | const wxString& name = wxVListBoxNameStr) | |
49 | { | |
50 | Init(); | |
51 | ||
52 | (void)Create(parent, id, pos, size, countItems, style, name); | |
53 | } | |
54 | ||
55 | // really creates the control and sets the initial number of items in it | |
56 | // (which may be changed later with SetItemCount()) | |
57 | // | |
58 | // there are no special styles defined for wxVListBox | |
59 | // | |
60 | // returns true on success or false if the control couldn't be created | |
61 | bool Create(wxWindow *parent, | |
62 | wxWindowID id = wxID_ANY, | |
63 | const wxPoint& pos = wxDefaultPosition, | |
64 | const wxSize& size = wxDefaultSize, | |
65 | size_t countItems = 0, | |
66 | long style = 0, | |
67 | const wxString& name = wxVListBoxNameStr); | |
68 | ||
69 | ||
70 | // operations | |
71 | // ---------- | |
72 | ||
73 | // set the number of items to be shown in the control | |
74 | // | |
75 | // this is just a synonym for wxVScrolledWindow::SetLineCount() | |
76 | void SetItemCount(size_t count) { SetLineCount(count); } | |
77 | ||
78 | // delete all items from the control | |
79 | void Clear() { SetItemCount(0); } | |
80 | ||
81 | // set the selection to the specified item, if it is -1 the selection is | |
82 | // unset | |
83 | void SetSelection(int selection) { DoSetSelection(selection, false); } | |
84 | ||
85 | // set the margins: horizontal margin is the distance between the window | |
86 | // border and the item contents while vertical margin is half of the | |
87 | // distance between items | |
88 | // | |
89 | // by default both margins are 0 | |
90 | void SetMargins(const wxPoint& pt); | |
91 | void SetMargins(wxCoord x, wxCoord y) { SetMargins(wxPoint(x, y)); } | |
92 | ||
93 | ||
94 | // accessors | |
95 | // --------- | |
96 | ||
97 | // get the number of items in the control | |
98 | size_t GetItemCount() const { return GetLineCount(); } | |
99 | ||
100 | // get the currently selected item or -1 if there is no selection | |
101 | int GetSelection() const { return m_selection; } | |
102 | ||
103 | // is this item selected? | |
104 | bool IsSelected(size_t line) const { return (int)line == m_selection; } | |
105 | ||
106 | ||
107 | protected: | |
108 | // the derived class must implement this function to actually draw the item | |
109 | // with the given index on the provided DC | |
110 | virtual void OnDrawItem(wxDC& dc, const wxRect& rect, size_t n) const = 0; | |
111 | ||
112 | // the derived class must implement this method to return the height of the | |
113 | // specified item | |
114 | virtual wxCoord OnMeasureItem(size_t n) const = 0; | |
115 | ||
116 | // this method may be used to draw separators between the lines; note that | |
117 | // the rectangle may be modified, typically to deflate it a bit before | |
118 | // passing to OnDrawItem() | |
119 | // | |
120 | // the base class version doesn't do anything | |
121 | virtual void OnDrawSeparator(wxDC& dc, wxRect& rect, size_t n) const; | |
122 | ||
123 | ||
124 | // we implement OnGetLineHeight() in terms of OnMeasureItem() because this | |
125 | // allows us to add borders to the items easily | |
126 | // | |
127 | // this function is not upposed to be overridden by the derived classes | |
128 | virtual wxCoord OnGetLineHeight(size_t line) const; | |
129 | ||
130 | ||
131 | // event handlers | |
132 | void OnPaint(wxPaintEvent& event); | |
133 | void OnKeyDown(wxKeyEvent& event); | |
134 | void OnLeftDown(wxMouseEvent& event); | |
135 | void OnLeftDClick(wxMouseEvent& event); | |
136 | ||
137 | ||
138 | // common part of all ctors | |
139 | void Init(); | |
140 | ||
141 | // SetSelection() with additional parameter telling it whether to send a | |
142 | // notification event or not | |
143 | void DoSetSelection(int selection, bool sendEvent = true); | |
144 | ||
145 | private: | |
146 | // the current selection or -1 | |
147 | int m_selection; | |
148 | ||
149 | // margins | |
150 | wxPoint m_ptMargins; | |
151 | ||
152 | ||
153 | DECLARE_EVENT_TABLE() | |
154 | }; | |
155 | ||
156 | #endif // _WX_VLBOX_H_ | |
157 |