1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHtmlListBox is a listbox whose items are wxHtmlCells
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_HTMLLBOX_H_
13 #define _WX_HTMLLBOX_H_
15 #include "wx/vlbox.h" // base class
16 #include "wx/html/htmlwin.h"
17 #include "wx/ctrlsub.h"
20 #include "wx/filesys.h"
21 #endif // wxUSE_FILESYSTEM
23 class WXDLLIMPEXP_HTML wxHtmlCell
;
24 class WXDLLIMPEXP_HTML wxHtmlWinParser
;
25 class WXDLLIMPEXP_HTML wxHtmlListBoxCache
;
26 class WXDLLIMPEXP_HTML wxHtmlListBoxStyle
;
28 extern WXDLLIMPEXP_DATA_HTML(const wxChar
) wxHtmlListBoxNameStr
[];
29 extern WXDLLIMPEXP_DATA_HTML(const wxChar
) wxSimpleHtmlListBoxNameStr
[];
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
35 class WXDLLIMPEXP_HTML wxHtmlListBox
: public wxVListBox
,
36 public wxHtmlWindowInterface
,
37 public wxHtmlWindowMouseHelper
39 DECLARE_ABSTRACT_CLASS(wxHtmlListBox
)
41 // constructors and such
42 // ---------------------
44 // default constructor, you must call Create() later
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
,
53 const wxString
& name
= wxHtmlListBoxNameStr
);
55 // really creates the control and sets the initial number of items in it
56 // (which may be changed later with SetItemCount())
58 // the only special style which may be specified here is wxLB_MULTIPLE
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
,
66 const wxString
& name
= wxHtmlListBoxNameStr
);
68 // destructor cleans up whatever resources we use
69 virtual ~wxHtmlListBox();
71 // override some base class virtuals
72 virtual void RefreshLine(size_t line
);
73 virtual void RefreshLines(size_t from
, size_t to
);
74 virtual void RefreshAll();
75 virtual void SetItemCount(size_t count
);
77 // provide a way to set the default encoding (non-Unicode only)
78 void SetEncoding(wxFontEncoding encoding
) { m_fontEncoding
= encoding
; }
79 wxFontEncoding
GetEncoding() { return m_fontEncoding
; }
82 // retrieve the file system used by the wxHtmlWinParser: if you use
83 // relative paths in your HTML, you should use its ChangePathTo() method
84 wxFileSystem
& GetFileSystem() { return m_filesystem
; }
85 const wxFileSystem
& GetFileSystem() const { return m_filesystem
; }
86 #endif // wxUSE_FILESYSTEM
88 virtual void OnInternalIdle();
91 // this method must be implemented in the derived class and should return
92 // the body (i.e. without <html>) of the HTML for the given item
93 virtual wxString
OnGetItem(size_t n
) const = 0;
95 // this function may be overridden to decorate HTML returned by OnGetItem()
96 virtual wxString
OnGetItemMarkup(size_t n
) const;
99 // this method allows to customize the selection appearance: it may be used
100 // to specify the colour of the text which normally has the given colour
101 // colFg when it is inside the selection
103 // by default, the original colour is not used at all and all text has the
104 // same (default for this system) colour inside selection
105 virtual wxColour
GetSelectedTextColour(const wxColour
& colFg
) const;
107 // this is the same as GetSelectedTextColour() but allows to customize the
108 // background colour -- this is even more rarely used as you can change it
109 // globally using SetSelectionBackground()
110 virtual wxColour
GetSelectedTextBgColour(const wxColour
& colBg
) const;
113 // we implement both of these functions in terms of OnGetItem(), they are
114 // not supposed to be overridden by our descendants
115 virtual void OnDrawItem(wxDC
& dc
, const wxRect
& rect
, size_t n
) const;
116 virtual wxCoord
OnMeasureItem(size_t n
) const;
118 // This method may be overriden to handle clicking on a link in
119 // the listbox. By default, clicking links is ignored.
120 virtual void OnLinkClicked(size_t n
, const wxHtmlLinkInfo
& link
);
123 void OnSize(wxSizeEvent
& event
);
124 void OnMouseMove(wxMouseEvent
& event
);
125 void OnLeftDown(wxMouseEvent
& event
);
128 // common part of all ctors
131 // ensure that the given item is cached
132 void CacheItem(size_t n
) const;
135 // wxHtmlWindowInterface methods:
136 virtual void SetHTMLWindowTitle(const wxString
& title
);
137 virtual void OnHTMLLinkClicked(const wxHtmlLinkInfo
& link
);
138 virtual wxHtmlOpeningStatus
OnHTMLOpeningURL(wxHtmlURLType type
,
140 wxString
*redirect
) const;
141 virtual wxPoint
HTMLCoordsToWindow(wxHtmlCell
*cell
,
142 const wxPoint
& pos
) const;
143 virtual wxWindow
* GetHTMLWindow();
144 virtual wxColour
GetHTMLBackgroundColour() const;
145 virtual void SetHTMLBackgroundColour(const wxColour
& clr
);
146 virtual void SetHTMLBackgroundImage(const wxBitmap
& bmpBg
);
147 virtual void SetHTMLStatusText(const wxString
& text
);
148 virtual wxCursor
GetHTMLCursor(HTMLCursor type
) const;
150 // returns index of item that contains given HTML cell
151 size_t GetItemForCell(const wxHtmlCell
*cell
) const;
153 // return physical coordinates of root wxHtmlCell of n-th item
154 wxPoint
GetRootCellCoords(size_t n
) const;
156 // Converts physical coordinates stored in @a pos into coordinates
157 // relative to the root cell of the item under mouse cursor, if any. If no
158 // cell is found under the cursor, returns false. Otherwise stores the new
159 // coordinates back into @a pos and pointer to the cell under cursor into
160 // @a cell and returns true.
161 bool PhysicalCoordsToCell(wxPoint
& pos
, wxHtmlCell
*& cell
) const;
163 // The opposite of PhysicalCoordsToCell: converts coordinates relative to
164 // given cell to physical coordinates in the window
165 wxPoint
CellCoordsToPhysical(const wxPoint
& pos
, wxHtmlCell
*cell
) const;
168 // this class caches the pre-parsed HTML to speed up display
169 wxHtmlListBoxCache
*m_cache
;
171 // HTML parser we use
172 wxHtmlWinParser
*m_htmlParser
;
174 // font encoding (used in non-Unicode mode only)
175 wxFontEncoding m_fontEncoding
;
178 // file system used by m_htmlParser
179 wxFileSystem m_filesystem
;
180 #endif // wxUSE_FILESYSTEM
182 // rendering style for the parser which allows us to customize our colours
183 wxHtmlListBoxStyle
*m_htmlRendStyle
;
186 // it calls our GetSelectedTextColour() and GetSelectedTextBgColour()
187 friend class wxHtmlListBoxStyle
;
188 friend class wxHtmlListBoxWinInterface
;
191 DECLARE_EVENT_TABLE()
192 DECLARE_NO_COPY_CLASS(wxHtmlListBox
)
196 // ----------------------------------------------------------------------------
197 // wxSimpleHtmlListBox
198 // ----------------------------------------------------------------------------
200 #define wxHLB_DEFAULT_STYLE wxBORDER_SUNKEN
201 #define wxHLB_MULTIPLE wxLB_MULTIPLE
203 class WXDLLIMPEXP_HTML wxSimpleHtmlListBox
: public wxHtmlListBox
,
204 public wxItemContainer
207 // wxListbox-compatible constructors
208 // ---------------------------------
210 wxSimpleHtmlListBox() { }
212 wxSimpleHtmlListBox(wxWindow
*parent
,
214 const wxPoint
& pos
= wxDefaultPosition
,
215 const wxSize
& size
= wxDefaultSize
,
216 int n
= 0, const wxString choices
[] = NULL
,
217 long style
= wxHLB_DEFAULT_STYLE
,
218 const wxValidator
& validator
= wxDefaultValidator
,
219 const wxString
& name
= wxSimpleHtmlListBoxNameStr
)
221 Create(parent
, id
, pos
, size
, n
, choices
, style
, validator
, name
);
224 wxSimpleHtmlListBox(wxWindow
*parent
,
228 const wxArrayString
& choices
,
229 long style
= wxHLB_DEFAULT_STYLE
,
230 const wxValidator
& validator
= wxDefaultValidator
,
231 const wxString
& name
= wxSimpleHtmlListBoxNameStr
)
233 Create(parent
, id
, pos
, size
, choices
, style
, validator
, name
);
236 bool Create(wxWindow
*parent
, wxWindowID id
,
237 const wxPoint
& pos
= wxDefaultPosition
,
238 const wxSize
& size
= wxDefaultSize
,
239 int n
= 0, const wxString choices
[] = NULL
,
240 long style
= wxHLB_DEFAULT_STYLE
,
241 const wxValidator
& validator
= wxDefaultValidator
,
242 const wxString
& name
= wxSimpleHtmlListBoxNameStr
);
243 bool Create(wxWindow
*parent
, wxWindowID id
,
246 const wxArrayString
& choices
,
247 long style
= wxHLB_DEFAULT_STYLE
,
248 const wxValidator
& validator
= wxDefaultValidator
,
249 const wxString
& name
= wxSimpleHtmlListBoxNameStr
);
251 virtual ~wxSimpleHtmlListBox();
253 // these must be overloaded otherwise the compiler will complain
254 // about wxItemContainerImmutable::[G|S]etSelection being pure virtuals...
255 void SetSelection(int n
)
256 { wxVListBox::SetSelection(n
); }
257 int GetSelection() const
258 { return wxVListBox::GetSelection(); }
260 // see ctrlsub.h for more info about this:
261 wxCONTROL_ITEMCONTAINER_CLIENTDATAOBJECT_RECAST
267 virtual unsigned int GetCount() const
268 { return m_items
.GetCount(); }
270 virtual wxString
GetString(unsigned int n
) const;
272 // override default unoptimized wxItemContainer::GetStrings() function
273 wxArrayString
GetStrings() const
276 virtual void SetString(unsigned int n
, const wxString
& s
);
278 virtual void Clear();
279 virtual void Delete(unsigned int n
);
281 // override default unoptimized wxItemContainer::Append() function
282 void Append(const wxArrayString
& strings
);
284 // since we override one Append() overload, we need to overload all others too
285 int Append(const wxString
& item
)
286 { return wxItemContainer::Append(item
); }
287 int Append(const wxString
& item
, void *clientData
)
288 { return wxItemContainer::Append(item
, clientData
); }
289 int Append(const wxString
& item
, wxClientData
*clientData
)
290 { return wxItemContainer::Append(item
, clientData
); }
295 virtual int DoAppend(const wxString
& item
);
296 virtual int DoInsert(const wxString
& item
, unsigned int pos
);
298 virtual void DoSetItemClientData(unsigned int n
, void *clientData
)
299 { m_HTMLclientData
[n
] = clientData
; }
301 virtual void *DoGetItemClientData(unsigned int n
) const
302 { return m_HTMLclientData
[n
]; }
303 virtual void DoSetItemClientObject(unsigned int n
, wxClientData
*clientData
)
304 { m_HTMLclientData
[n
] = (void *)clientData
; }
305 virtual wxClientData
*DoGetItemClientObject(unsigned int n
) const
306 { return (wxClientData
*)m_HTMLclientData
[n
]; }
308 // calls wxHtmlListBox::SetItemCount() and RefreshAll()
311 // overload these functions just to change their visibility: users of
312 // wxSimpleHtmlListBox shouldn't be allowed to call them directly!
313 virtual void SetItemCount(size_t count
)
314 { wxHtmlListBox::SetItemCount(count
); }
315 virtual void SetLineCount(size_t count
)
316 { wxHtmlListBox::SetLineCount(count
); }
318 virtual wxString
OnGetItem(size_t n
) const
319 { return m_items
[n
]; }
321 wxArrayString m_items
;
322 wxArrayPtrVoid m_HTMLclientData
;
324 // Note: For the benefit of old compilers (like gcc-2.8) this should
325 // not be named m_clientdata as that clashes with the name of an
326 // anonymous struct member in wxEvtHandler, which we derive from.
328 DECLARE_NO_COPY_CLASS(wxSimpleHtmlListBox
)
331 #endif // _WX_HTMLLBOX_H_