htmllbox.h 12 KB

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