]> git.saurik.com Git - wxWidgets.git/blob - include/wx/htmllbox.h
Don't reuse the same event object for multiple events in wxGTK.
[wxWidgets.git] / include / wx / htmllbox.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/htmllbox.h
3 // Purpose: wxHtmlListBox is a listbox whose items are wxHtmlCells
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 31.05.03
7 // RCS-ID: $Id$
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_HTMLLBOX_H_
13 #define _WX_HTMLLBOX_H_
14
15 #include "wx/vlbox.h" // base class
16 #include "wx/html/htmlwin.h"
17 #include "wx/ctrlsub.h"
18
19 #if wxUSE_FILESYSTEM
20 #include "wx/filesys.h"
21 #endif // wxUSE_FILESYSTEM
22
23 class WXDLLIMPEXP_FWD_HTML wxHtmlCell;
24 class WXDLLIMPEXP_FWD_HTML wxHtmlWinParser;
25 class WXDLLIMPEXP_FWD_HTML wxHtmlListBoxCache;
26 class WXDLLIMPEXP_FWD_HTML wxHtmlListBoxStyle;
27
28 extern WXDLLIMPEXP_DATA_HTML(const char) wxHtmlListBoxNameStr[];
29 extern WXDLLIMPEXP_DATA_HTML(const char) wxSimpleHtmlListBoxNameStr[];
30
31 // ----------------------------------------------------------------------------
32 // wxHtmlListBox
33 // ----------------------------------------------------------------------------
34
35 class WXDLLIMPEXP_HTML wxHtmlListBox : public wxVListBox,
36 public wxHtmlWindowInterface,
37 public wxHtmlWindowMouseHelper
38 {
39 DECLARE_ABSTRACT_CLASS(wxHtmlListBox)
40 public:
41 // constructors and such
42 // ---------------------
43
44 // default constructor, you must call Create() later
45 wxHtmlListBox();
46
47 // normal constructor which calls Create() internally
48 wxHtmlListBox(wxWindow *parent,
49 wxWindowID id = wxID_ANY,
50 const wxPoint& pos = wxDefaultPosition,
51 const wxSize& size = wxDefaultSize,
52 long style = 0,
53 const wxString& name = wxHtmlListBoxNameStr);
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 // the only special style which may be specified here is wxLB_MULTIPLE
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 long style = 0,
66 const wxString& name = wxHtmlListBoxNameStr);
67
68 // destructor cleans up whatever resources we use
69 virtual ~wxHtmlListBox();
70
71 // override some base class virtuals
72 virtual void RefreshRow(size_t line);
73 virtual void RefreshRows(size_t from, size_t to);
74 virtual void RefreshAll();
75 virtual void SetItemCount(size_t count);
76
77 #if wxUSE_FILESYSTEM
78 // retrieve the file system used by the wxHtmlWinParser: if you use
79 // relative paths in your HTML, you should use its ChangePathTo() method
80 wxFileSystem& GetFileSystem() { return m_filesystem; }
81 const wxFileSystem& GetFileSystem() const { return m_filesystem; }
82 #endif // wxUSE_FILESYSTEM
83
84 virtual void OnInternalIdle();
85
86 protected:
87 // this method must be implemented in the derived class and should return
88 // the body (i.e. without <html>) of the HTML for the given item
89 virtual wxString OnGetItem(size_t n) const = 0;
90
91 // this function may be overridden to decorate HTML returned by OnGetItem()
92 virtual wxString OnGetItemMarkup(size_t n) const;
93
94
95 // this method allows to customize the selection appearance: it may be used
96 // to specify the colour of the text which normally has the given colour
97 // colFg when it is inside the selection
98 //
99 // by default, the original colour is not used at all and all text has the
100 // same (default for this system) colour inside selection
101 virtual wxColour GetSelectedTextColour(const wxColour& colFg) const;
102
103 // this is the same as GetSelectedTextColour() but allows to customize the
104 // background colour -- this is even more rarely used as you can change it
105 // globally using SetSelectionBackground()
106 virtual wxColour GetSelectedTextBgColour(const wxColour& colBg) const;
107
108
109 // we implement both of these functions in terms of OnGetItem(), they are
110 // not supposed to be overridden by our descendants
111 virtual void OnDrawItem(wxDC& dc, const wxRect& rect, size_t n) const;
112 virtual wxCoord OnMeasureItem(size_t n) const;
113
114 // override this one to draw custom background for selected items correctly
115 virtual void OnDrawBackground(wxDC& dc, const wxRect& rect, size_t n) const;
116
117 // this method may be overridden to handle clicking on a link in the
118 // listbox (by default, clicks on links are simply ignored)
119 virtual void OnLinkClicked(size_t n, const wxHtmlLinkInfo& link);
120
121 // event handlers
122 void OnSize(wxSizeEvent& event);
123 void OnMouseMove(wxMouseEvent& event);
124 void OnLeftDown(wxMouseEvent& event);
125
126
127 // common part of all ctors
128 void Init();
129
130 // ensure that the given item is cached
131 void CacheItem(size_t n) const;
132
133 private:
134 // wxHtmlWindowInterface methods:
135 virtual void SetHTMLWindowTitle(const wxString& title);
136 virtual void OnHTMLLinkClicked(const wxHtmlLinkInfo& link);
137 virtual wxHtmlOpeningStatus OnHTMLOpeningURL(wxHtmlURLType type,
138 const wxString& url,
139 wxString *redirect) const;
140 virtual wxPoint HTMLCoordsToWindow(wxHtmlCell *cell,
141 const wxPoint& pos) const;
142 virtual wxWindow* GetHTMLWindow();
143 virtual wxColour GetHTMLBackgroundColour() const;
144 virtual void SetHTMLBackgroundColour(const wxColour& clr);
145 virtual void SetHTMLBackgroundImage(const wxBitmap& bmpBg);
146 virtual void SetHTMLStatusText(const wxString& text);
147 virtual wxCursor GetHTMLCursor(HTMLCursor type) const;
148
149 // returns index of item that contains given HTML cell
150 size_t GetItemForCell(const wxHtmlCell *cell) const;
151
152 // return physical coordinates of root wxHtmlCell of n-th item
153 wxPoint GetRootCellCoords(size_t n) const;
154
155 // Converts physical coordinates stored in @a pos into coordinates
156 // relative to the root cell of the item under mouse cursor, if any. If no
157 // cell is found under the cursor, returns false. Otherwise stores the new
158 // coordinates back into @a pos and pointer to the cell under cursor into
159 // @a cell and returns true.
160 bool PhysicalCoordsToCell(wxPoint& pos, wxHtmlCell*& cell) const;
161
162 // The opposite of PhysicalCoordsToCell: converts coordinates relative to
163 // given cell to physical coordinates in the window
164 wxPoint CellCoordsToPhysical(const wxPoint& pos, wxHtmlCell *cell) const;
165
166 private:
167 // this class caches the pre-parsed HTML to speed up display
168 wxHtmlListBoxCache *m_cache;
169
170 // HTML parser we use
171 wxHtmlWinParser *m_htmlParser;
172
173 #if wxUSE_FILESYSTEM
174 // file system used by m_htmlParser
175 wxFileSystem m_filesystem;
176 #endif // wxUSE_FILESYSTEM
177
178 // rendering style for the parser which allows us to customize our colours
179 wxHtmlListBoxStyle *m_htmlRendStyle;
180
181
182 // it calls our GetSelectedTextColour() and GetSelectedTextBgColour()
183 friend class wxHtmlListBoxStyle;
184 friend class wxHtmlListBoxWinInterface;
185
186
187 DECLARE_EVENT_TABLE()
188 wxDECLARE_NO_COPY_CLASS(wxHtmlListBox);
189 };
190
191
192 // ----------------------------------------------------------------------------
193 // wxSimpleHtmlListBox
194 // ----------------------------------------------------------------------------
195
196 #define wxHLB_DEFAULT_STYLE wxBORDER_SUNKEN
197 #define wxHLB_MULTIPLE wxLB_MULTIPLE
198
199 class WXDLLIMPEXP_HTML wxSimpleHtmlListBox : public wxHtmlListBox,
200 public wxItemContainer
201 {
202 public:
203 // wxListbox-compatible constructors
204 // ---------------------------------
205
206 wxSimpleHtmlListBox() { }
207
208 wxSimpleHtmlListBox(wxWindow *parent,
209 wxWindowID id,
210 const wxPoint& pos = wxDefaultPosition,
211 const wxSize& size = wxDefaultSize,
212 int n = 0, const wxString choices[] = NULL,
213 long style = wxHLB_DEFAULT_STYLE,
214 const wxValidator& validator = wxDefaultValidator,
215 const wxString& name = wxSimpleHtmlListBoxNameStr)
216 {
217 Create(parent, id, pos, size, n, choices, style, validator, name);
218 }
219
220 wxSimpleHtmlListBox(wxWindow *parent,
221 wxWindowID id,
222 const wxPoint& pos,
223 const wxSize& size,
224 const wxArrayString& choices,
225 long style = wxHLB_DEFAULT_STYLE,
226 const wxValidator& validator = wxDefaultValidator,
227 const wxString& name = wxSimpleHtmlListBoxNameStr)
228 {
229 Create(parent, id, pos, size, choices, style, validator, name);
230 }
231
232 bool Create(wxWindow *parent, wxWindowID id,
233 const wxPoint& pos = wxDefaultPosition,
234 const wxSize& size = wxDefaultSize,
235 int n = 0, const wxString choices[] = NULL,
236 long style = wxHLB_DEFAULT_STYLE,
237 const wxValidator& validator = wxDefaultValidator,
238 const wxString& name = wxSimpleHtmlListBoxNameStr);
239 bool Create(wxWindow *parent, wxWindowID id,
240 const wxPoint& pos,
241 const wxSize& size,
242 const wxArrayString& choices,
243 long style = wxHLB_DEFAULT_STYLE,
244 const wxValidator& validator = wxDefaultValidator,
245 const wxString& name = wxSimpleHtmlListBoxNameStr);
246
247 virtual ~wxSimpleHtmlListBox();
248
249 // these must be overloaded otherwise the compiler will complain
250 // about wxItemContainerImmutable::[G|S]etSelection being pure virtuals...
251 void SetSelection(int n)
252 { wxVListBox::SetSelection(n); }
253 int GetSelection() const
254 { return wxVListBox::GetSelection(); }
255
256 // see ctrlsub.h for more info about this:
257 wxCONTROL_ITEMCONTAINER_CLIENTDATAOBJECT_RECAST
258
259
260 // accessing strings
261 // -----------------
262
263 virtual unsigned int GetCount() const
264 { return m_items.GetCount(); }
265
266 virtual wxString GetString(unsigned int n) const;
267
268 // override default unoptimized wxItemContainer::GetStrings() function
269 wxArrayString GetStrings() const
270 { return m_items; }
271
272 virtual void SetString(unsigned int n, const wxString& s);
273
274 // resolve ambiguity between wxItemContainer and wxVListBox versions
275 void Clear();
276
277 protected:
278 virtual int DoInsertItems(const wxArrayStringsAdapter & items,
279 unsigned int pos,
280 void **clientData, wxClientDataType type);
281
282 virtual void DoSetItemClientData(unsigned int n, void *clientData)
283 { m_HTMLclientData[n] = clientData; }
284
285 virtual void *DoGetItemClientData(unsigned int n) const
286 { return m_HTMLclientData[n]; }
287
288 // wxItemContainer methods
289 virtual void DoClear();
290 virtual void DoDeleteOneItem(unsigned int n);
291
292 // calls wxHtmlListBox::SetItemCount() and RefreshAll()
293 void UpdateCount();
294
295 // override these functions just to change their visibility: users of
296 // wxSimpleHtmlListBox shouldn't be allowed to call them directly!
297 virtual void SetItemCount(size_t count)
298 { wxHtmlListBox::SetItemCount(count); }
299 virtual void SetRowCount(size_t count)
300 { wxHtmlListBox::SetRowCount(count); }
301
302 virtual wxString OnGetItem(size_t n) const
303 { return m_items[n]; }
304
305 virtual void InitEvent(wxCommandEvent& event, int n)
306 {
307 // we're not a virtual control and we can include the string
308 // of the item which was clicked:
309 event.SetString(m_items[n]);
310 wxVListBox::InitEvent(event, n);
311 }
312
313 wxArrayString m_items;
314 wxArrayPtrVoid m_HTMLclientData;
315
316 // Note: For the benefit of old compilers (like gcc-2.8) this should
317 // not be named m_clientdata as that clashes with the name of an
318 // anonymous struct member in wxEvtHandler, which we derive from.
319
320 wxDECLARE_NO_COPY_CLASS(wxSimpleHtmlListBox);
321 };
322
323 #endif // _WX_HTMLLBOX_H_
324