richtextstyles.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/richtext/richtextstyles.h
  3. // Purpose: Style management for wxRichTextCtrl
  4. // Author: Julian Smart
  5. // Modified by:
  6. // Created: 2005-09-30
  7. // Copyright: (c) Julian Smart
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_RICHTEXTSTYLES_H_
  11. #define _WX_RICHTEXTSTYLES_H_
  12. /*!
  13. * Includes
  14. */
  15. #include "wx/defs.h"
  16. #if wxUSE_RICHTEXT
  17. #include "wx/richtext/richtextbuffer.h"
  18. #if wxUSE_HTML
  19. #include "wx/htmllbox.h"
  20. #endif
  21. #if wxUSE_COMBOCTRL
  22. #include "wx/combo.h"
  23. #endif
  24. #include "wx/choice.h"
  25. /*!
  26. * Forward declarations
  27. */
  28. class WXDLLIMPEXP_FWD_RICHTEXT wxRichTextCtrl;
  29. class WXDLLIMPEXP_FWD_RICHTEXT wxRichTextBuffer;
  30. /*!
  31. * wxRichTextStyleDefinition class declaration
  32. * A base class for paragraph and character styles.
  33. */
  34. class WXDLLIMPEXP_RICHTEXT wxRichTextStyleDefinition: public wxObject
  35. {
  36. DECLARE_CLASS(wxRichTextStyleDefinition)
  37. public:
  38. /// Copy constructors
  39. wxRichTextStyleDefinition(const wxRichTextStyleDefinition& def)
  40. : wxObject()
  41. {
  42. Init();
  43. Copy(def);
  44. }
  45. /// Default constructor
  46. wxRichTextStyleDefinition(const wxString& name = wxEmptyString) { Init(); m_name = name; }
  47. /// Destructor
  48. virtual ~wxRichTextStyleDefinition() {}
  49. /// Initialises members
  50. void Init() {}
  51. /// Copies from def
  52. void Copy(const wxRichTextStyleDefinition& def);
  53. /// Equality test
  54. bool Eq(const wxRichTextStyleDefinition& def) const;
  55. /// Assignment operator
  56. void operator =(const wxRichTextStyleDefinition& def) { Copy(def); }
  57. /// Equality operator
  58. bool operator ==(const wxRichTextStyleDefinition& def) const { return Eq(def); }
  59. /// Override to clone the object
  60. virtual wxRichTextStyleDefinition* Clone() const = 0;
  61. /// Sets and gets the name of the style
  62. void SetName(const wxString& name) { m_name = name; }
  63. const wxString& GetName() const { return m_name; }
  64. /// Sets and gets the style description
  65. void SetDescription(const wxString& descr) { m_description = descr; }
  66. const wxString& GetDescription() const { return m_description; }
  67. /// Sets and gets the name of the style that this style is based on
  68. void SetBaseStyle(const wxString& name) { m_baseStyle = name; }
  69. const wxString& GetBaseStyle() const { return m_baseStyle; }
  70. /// Sets and gets the style
  71. void SetStyle(const wxRichTextAttr& style) { m_style = style; }
  72. const wxRichTextAttr& GetStyle() const { return m_style; }
  73. wxRichTextAttr& GetStyle() { return m_style; }
  74. /// Gets the style combined with the base style
  75. virtual wxRichTextAttr GetStyleMergedWithBase(const wxRichTextStyleSheet* sheet) const;
  76. /**
  77. Returns the definition's properties.
  78. */
  79. wxRichTextProperties& GetProperties() { return m_properties; }
  80. /**
  81. Returns the definition's properties.
  82. */
  83. const wxRichTextProperties& GetProperties() const { return m_properties; }
  84. /**
  85. Sets the definition's properties.
  86. */
  87. void SetProperties(const wxRichTextProperties& props) { m_properties = props; }
  88. protected:
  89. wxString m_name;
  90. wxString m_baseStyle;
  91. wxString m_description;
  92. wxRichTextAttr m_style;
  93. wxRichTextProperties m_properties;
  94. };
  95. /*!
  96. * wxRichTextCharacterStyleDefinition class declaration
  97. */
  98. class WXDLLIMPEXP_RICHTEXT wxRichTextCharacterStyleDefinition: public wxRichTextStyleDefinition
  99. {
  100. DECLARE_DYNAMIC_CLASS(wxRichTextCharacterStyleDefinition)
  101. public:
  102. /// Copy constructor
  103. wxRichTextCharacterStyleDefinition(const wxRichTextCharacterStyleDefinition& def): wxRichTextStyleDefinition(def) {}
  104. /// Default constructor
  105. wxRichTextCharacterStyleDefinition(const wxString& name = wxEmptyString):
  106. wxRichTextStyleDefinition(name) {}
  107. /// Destructor
  108. virtual ~wxRichTextCharacterStyleDefinition() {}
  109. /// Clones the object
  110. virtual wxRichTextStyleDefinition* Clone() const { return new wxRichTextCharacterStyleDefinition(*this); }
  111. protected:
  112. };
  113. /*!
  114. * wxRichTextParagraphStyleDefinition class declaration
  115. */
  116. class WXDLLIMPEXP_RICHTEXT wxRichTextParagraphStyleDefinition: public wxRichTextStyleDefinition
  117. {
  118. DECLARE_DYNAMIC_CLASS(wxRichTextParagraphStyleDefinition)
  119. public:
  120. /// Copy constructor
  121. wxRichTextParagraphStyleDefinition(const wxRichTextParagraphStyleDefinition& def): wxRichTextStyleDefinition(def) { m_nextStyle = def.m_nextStyle; }
  122. /// Default constructor
  123. wxRichTextParagraphStyleDefinition(const wxString& name = wxEmptyString):
  124. wxRichTextStyleDefinition(name) {}
  125. // Destructor
  126. virtual ~wxRichTextParagraphStyleDefinition() {}
  127. /// Sets and gets the next style
  128. void SetNextStyle(const wxString& name) { m_nextStyle = name; }
  129. const wxString& GetNextStyle() const { return m_nextStyle; }
  130. /// Copies from def
  131. void Copy(const wxRichTextParagraphStyleDefinition& def);
  132. /// Assignment operator
  133. void operator =(const wxRichTextParagraphStyleDefinition& def) { Copy(def); }
  134. /// Equality operator
  135. bool operator ==(const wxRichTextParagraphStyleDefinition& def) const;
  136. /// Clones the object
  137. virtual wxRichTextStyleDefinition* Clone() const { return new wxRichTextParagraphStyleDefinition(*this); }
  138. protected:
  139. /// The next style to use when adding a paragraph after this style.
  140. wxString m_nextStyle;
  141. };
  142. /*!
  143. * wxRichTextListStyleDefinition class declaration
  144. */
  145. class WXDLLIMPEXP_RICHTEXT wxRichTextListStyleDefinition: public wxRichTextParagraphStyleDefinition
  146. {
  147. DECLARE_DYNAMIC_CLASS(wxRichTextListStyleDefinition)
  148. public:
  149. /// Copy constructor
  150. wxRichTextListStyleDefinition(const wxRichTextListStyleDefinition& def): wxRichTextParagraphStyleDefinition(def) { Init(); Copy(def); }
  151. /// Default constructor
  152. wxRichTextListStyleDefinition(const wxString& name = wxEmptyString):
  153. wxRichTextParagraphStyleDefinition(name) { Init(); }
  154. /// Destructor
  155. virtual ~wxRichTextListStyleDefinition() {}
  156. /// Copies from def
  157. void Copy(const wxRichTextListStyleDefinition& def);
  158. /// Assignment operator
  159. void operator =(const wxRichTextListStyleDefinition& def) { Copy(def); }
  160. /// Equality operator
  161. bool operator ==(const wxRichTextListStyleDefinition& def) const;
  162. /// Clones the object
  163. virtual wxRichTextStyleDefinition* Clone() const { return new wxRichTextListStyleDefinition(*this); }
  164. /// Sets/gets the attributes for the given level
  165. void SetLevelAttributes(int i, const wxRichTextAttr& attr);
  166. wxRichTextAttr* GetLevelAttributes(int i);
  167. const wxRichTextAttr* GetLevelAttributes(int i) const;
  168. /// Convenience function for setting the major attributes for a list level specification
  169. void SetAttributes(int i, int leftIndent, int leftSubIndent, int bulletStyle, const wxString& bulletSymbol = wxEmptyString);
  170. /// Finds the level corresponding to the given indentation
  171. int FindLevelForIndent(int indent) const;
  172. /// Combine the base and list style with a paragraph style, using the given indent (from which
  173. /// an appropriate level is found)
  174. wxRichTextAttr CombineWithParagraphStyle(int indent, const wxRichTextAttr& paraStyle, wxRichTextStyleSheet* styleSheet = NULL);
  175. /// Combine the base and list style, using the given indent (from which
  176. /// an appropriate level is found)
  177. wxRichTextAttr GetCombinedStyle(int indent, wxRichTextStyleSheet* styleSheet = NULL);
  178. /// Combine the base and list style, using the given level from which
  179. /// an appropriate level is found)
  180. wxRichTextAttr GetCombinedStyleForLevel(int level, wxRichTextStyleSheet* styleSheet = NULL);
  181. /// Gets the number of available levels
  182. int GetLevelCount() const { return 10; }
  183. /// Is this a numbered list?
  184. bool IsNumbered(int i) const;
  185. protected:
  186. /// The styles for each level (up to 10)
  187. wxRichTextAttr m_levelStyles[10];
  188. };
  189. /*!
  190. * wxRichTextBoxStyleDefinition class declaration, for box attributes in objects such as wxRichTextBox.
  191. */
  192. class WXDLLIMPEXP_RICHTEXT wxRichTextBoxStyleDefinition: public wxRichTextStyleDefinition
  193. {
  194. DECLARE_DYNAMIC_CLASS(wxRichTextBoxStyleDefinition)
  195. public:
  196. /// Copy constructor
  197. wxRichTextBoxStyleDefinition(const wxRichTextBoxStyleDefinition& def): wxRichTextStyleDefinition(def) { Copy(def); }
  198. /// Default constructor
  199. wxRichTextBoxStyleDefinition(const wxString& name = wxEmptyString):
  200. wxRichTextStyleDefinition(name) {}
  201. // Destructor
  202. virtual ~wxRichTextBoxStyleDefinition() {}
  203. /// Copies from def
  204. void Copy(const wxRichTextBoxStyleDefinition& def);
  205. /// Assignment operator
  206. void operator =(const wxRichTextBoxStyleDefinition& def) { Copy(def); }
  207. /// Equality operator
  208. bool operator ==(const wxRichTextBoxStyleDefinition& def) const;
  209. /// Clones the object
  210. virtual wxRichTextStyleDefinition* Clone() const { return new wxRichTextBoxStyleDefinition(*this); }
  211. protected:
  212. };
  213. /*!
  214. * The style sheet
  215. */
  216. class WXDLLIMPEXP_RICHTEXT wxRichTextStyleSheet: public wxObject
  217. {
  218. DECLARE_CLASS( wxRichTextStyleSheet )
  219. public:
  220. /// Constructors
  221. wxRichTextStyleSheet(const wxRichTextStyleSheet& sheet)
  222. : wxObject()
  223. {
  224. Init();
  225. Copy(sheet);
  226. }
  227. wxRichTextStyleSheet() { Init(); }
  228. virtual ~wxRichTextStyleSheet();
  229. /// Initialisation
  230. void Init();
  231. /// Copy
  232. void Copy(const wxRichTextStyleSheet& sheet);
  233. /// Assignment
  234. void operator=(const wxRichTextStyleSheet& sheet) { Copy(sheet); }
  235. /// Equality
  236. bool operator==(const wxRichTextStyleSheet& sheet) const;
  237. /// Add a definition to the character style list
  238. bool AddCharacterStyle(wxRichTextCharacterStyleDefinition* def);
  239. /// Add a definition to the paragraph style list
  240. bool AddParagraphStyle(wxRichTextParagraphStyleDefinition* def);
  241. /// Add a definition to the list style list
  242. bool AddListStyle(wxRichTextListStyleDefinition* def);
  243. /// Add a definition to the box style list
  244. bool AddBoxStyle(wxRichTextBoxStyleDefinition* def);
  245. /// Add a definition to the appropriate style list
  246. bool AddStyle(wxRichTextStyleDefinition* def);
  247. /// Remove a character style
  248. bool RemoveCharacterStyle(wxRichTextStyleDefinition* def, bool deleteStyle = false) { return RemoveStyle(m_characterStyleDefinitions, def, deleteStyle); }
  249. /// Remove a paragraph style
  250. bool RemoveParagraphStyle(wxRichTextStyleDefinition* def, bool deleteStyle = false) { return RemoveStyle(m_paragraphStyleDefinitions, def, deleteStyle); }
  251. /// Remove a list style
  252. bool RemoveListStyle(wxRichTextStyleDefinition* def, bool deleteStyle = false) { return RemoveStyle(m_listStyleDefinitions, def, deleteStyle); }
  253. /// Remove a box style
  254. bool RemoveBoxStyle(wxRichTextStyleDefinition* def, bool deleteStyle = false) { return RemoveStyle(m_boxStyleDefinitions, def, deleteStyle); }
  255. /// Remove a style
  256. bool RemoveStyle(wxRichTextStyleDefinition* def, bool deleteStyle = false);
  257. /// Find a character definition by name
  258. wxRichTextCharacterStyleDefinition* FindCharacterStyle(const wxString& name, bool recurse = true) const { return (wxRichTextCharacterStyleDefinition*) FindStyle(m_characterStyleDefinitions, name, recurse); }
  259. /// Find a paragraph definition by name
  260. wxRichTextParagraphStyleDefinition* FindParagraphStyle(const wxString& name, bool recurse = true) const { return (wxRichTextParagraphStyleDefinition*) FindStyle(m_paragraphStyleDefinitions, name, recurse); }
  261. /// Find a list definition by name
  262. wxRichTextListStyleDefinition* FindListStyle(const wxString& name, bool recurse = true) const { return (wxRichTextListStyleDefinition*) FindStyle(m_listStyleDefinitions, name, recurse); }
  263. /// Find a box definition by name
  264. wxRichTextBoxStyleDefinition* FindBoxStyle(const wxString& name, bool recurse = true) const { return (wxRichTextBoxStyleDefinition*) FindStyle(m_boxStyleDefinitions, name, recurse); }
  265. /// Find any definition by name
  266. wxRichTextStyleDefinition* FindStyle(const wxString& name, bool recurse = true) const;
  267. /// Return the number of character styles
  268. size_t GetCharacterStyleCount() const { return m_characterStyleDefinitions.GetCount(); }
  269. /// Return the number of paragraph styles
  270. size_t GetParagraphStyleCount() const { return m_paragraphStyleDefinitions.GetCount(); }
  271. /// Return the number of list styles
  272. size_t GetListStyleCount() const { return m_listStyleDefinitions.GetCount(); }
  273. /// Return the number of box styles
  274. size_t GetBoxStyleCount() const { return m_boxStyleDefinitions.GetCount(); }
  275. /// Return the nth character style
  276. wxRichTextCharacterStyleDefinition* GetCharacterStyle(size_t n) const { return (wxRichTextCharacterStyleDefinition*) m_characterStyleDefinitions.Item(n)->GetData(); }
  277. /// Return the nth paragraph style
  278. wxRichTextParagraphStyleDefinition* GetParagraphStyle(size_t n) const { return (wxRichTextParagraphStyleDefinition*) m_paragraphStyleDefinitions.Item(n)->GetData(); }
  279. /// Return the nth list style
  280. wxRichTextListStyleDefinition* GetListStyle(size_t n) const { return (wxRichTextListStyleDefinition*) m_listStyleDefinitions.Item(n)->GetData(); }
  281. /// Return the nth box style
  282. wxRichTextBoxStyleDefinition* GetBoxStyle(size_t n) const { return (wxRichTextBoxStyleDefinition*) m_boxStyleDefinitions.Item(n)->GetData(); }
  283. /// Delete all styles
  284. void DeleteStyles();
  285. /// Insert into list of style sheets
  286. bool InsertSheet(wxRichTextStyleSheet* before);
  287. /// Append to list of style sheets
  288. bool AppendSheet(wxRichTextStyleSheet* after);
  289. /// Unlink from the list of style sheets
  290. void Unlink();
  291. /// Get/set next sheet
  292. wxRichTextStyleSheet* GetNextSheet() const { return m_nextSheet; }
  293. void SetNextSheet(wxRichTextStyleSheet* sheet) { m_nextSheet = sheet; }
  294. /// Get/set previous sheet
  295. wxRichTextStyleSheet* GetPreviousSheet() const { return m_previousSheet; }
  296. void SetPreviousSheet(wxRichTextStyleSheet* sheet) { m_previousSheet = sheet; }
  297. /// Sets and gets the name of the style sheet
  298. void SetName(const wxString& name) { m_name = name; }
  299. const wxString& GetName() const { return m_name; }
  300. /// Sets and gets the style description
  301. void SetDescription(const wxString& descr) { m_description = descr; }
  302. const wxString& GetDescription() const { return m_description; }
  303. /**
  304. Returns the sheet's properties.
  305. */
  306. wxRichTextProperties& GetProperties() { return m_properties; }
  307. /**
  308. Returns the sheet's properties.
  309. */
  310. const wxRichTextProperties& GetProperties() const { return m_properties; }
  311. /**
  312. Sets the sheet's properties.
  313. */
  314. void SetProperties(const wxRichTextProperties& props) { m_properties = props; }
  315. /// Implementation
  316. /// Add a definition to one of the style lists
  317. bool AddStyle(wxList& list, wxRichTextStyleDefinition* def);
  318. /// Remove a style
  319. bool RemoveStyle(wxList& list, wxRichTextStyleDefinition* def, bool deleteStyle);
  320. /// Find a definition by name
  321. wxRichTextStyleDefinition* FindStyle(const wxList& list, const wxString& name, bool recurse = true) const;
  322. protected:
  323. wxString m_description;
  324. wxString m_name;
  325. wxList m_characterStyleDefinitions;
  326. wxList m_paragraphStyleDefinitions;
  327. wxList m_listStyleDefinitions;
  328. wxList m_boxStyleDefinitions;
  329. wxRichTextStyleSheet* m_previousSheet;
  330. wxRichTextStyleSheet* m_nextSheet;
  331. wxRichTextProperties m_properties;
  332. };
  333. #if wxUSE_HTML
  334. /*!
  335. * wxRichTextStyleListBox class declaration
  336. * A listbox to display styles.
  337. */
  338. class WXDLLIMPEXP_RICHTEXT wxRichTextStyleListBox: public wxHtmlListBox
  339. {
  340. DECLARE_CLASS(wxRichTextStyleListBox)
  341. DECLARE_EVENT_TABLE()
  342. public:
  343. /// Which type of style definition is currently showing?
  344. enum wxRichTextStyleType
  345. {
  346. wxRICHTEXT_STYLE_ALL,
  347. wxRICHTEXT_STYLE_PARAGRAPH,
  348. wxRICHTEXT_STYLE_CHARACTER,
  349. wxRICHTEXT_STYLE_LIST,
  350. wxRICHTEXT_STYLE_BOX
  351. };
  352. wxRichTextStyleListBox()
  353. {
  354. Init();
  355. }
  356. wxRichTextStyleListBox(wxWindow* parent, wxWindowID id = wxID_ANY, const wxPoint& pos = wxDefaultPosition,
  357. const wxSize& size = wxDefaultSize, long style = 0);
  358. virtual ~wxRichTextStyleListBox();
  359. void Init()
  360. {
  361. m_styleSheet = NULL;
  362. m_richTextCtrl = NULL;
  363. m_applyOnSelection = false;
  364. m_styleType = wxRICHTEXT_STYLE_PARAGRAPH;
  365. m_autoSetSelection = true;
  366. }
  367. bool Create(wxWindow* parent, wxWindowID id = wxID_ANY, const wxPoint& pos = wxDefaultPosition,
  368. const wxSize& size = wxDefaultSize, long style = 0);
  369. /// Creates a suitable HTML fragment for a definition
  370. wxString CreateHTML(wxRichTextStyleDefinition* def) const;
  371. /// Associates the control with a style sheet
  372. void SetStyleSheet(wxRichTextStyleSheet* styleSheet) { m_styleSheet = styleSheet; }
  373. wxRichTextStyleSheet* GetStyleSheet() const { return m_styleSheet; }
  374. /// Associates the control with a wxRichTextCtrl
  375. void SetRichTextCtrl(wxRichTextCtrl* ctrl) { m_richTextCtrl = ctrl; }
  376. wxRichTextCtrl* GetRichTextCtrl() const { return m_richTextCtrl; }
  377. /// Get style for index
  378. wxRichTextStyleDefinition* GetStyle(size_t i) const ;
  379. /// Get index for style name
  380. int GetIndexForStyle(const wxString& name) const ;
  381. /// Set selection for string, returning the index.
  382. int SetStyleSelection(const wxString& name);
  383. /// Updates the list
  384. void UpdateStyles();
  385. /// Apply the style
  386. void ApplyStyle(int i);
  387. /// Left click
  388. void OnLeftDown(wxMouseEvent& event);
  389. /// Left double-click
  390. void OnLeftDoubleClick(wxMouseEvent& event);
  391. /// Auto-select from style under caret in idle time
  392. void OnIdle(wxIdleEvent& event);
  393. /// Convert units in tends of a millimetre to device units
  394. int ConvertTenthsMMToPixels(wxDC& dc, int units) const;
  395. /// Can we set the selection based on the editor caret position?
  396. /// Need to override this if being used in a combobox popup
  397. virtual bool CanAutoSetSelection() { return m_autoSetSelection; }
  398. virtual void SetAutoSetSelection(bool autoSet) { m_autoSetSelection = autoSet; }
  399. /// Set whether the style should be applied as soon as the item is selected (the default)
  400. void SetApplyOnSelection(bool applyOnSel) { m_applyOnSelection = applyOnSel; }
  401. bool GetApplyOnSelection() const { return m_applyOnSelection; }
  402. /// Set the style type to display
  403. void SetStyleType(wxRichTextStyleType styleType) { m_styleType = styleType; UpdateStyles(); }
  404. wxRichTextStyleType GetStyleType() const { return m_styleType; }
  405. /// Helper for listbox and combo control
  406. static wxString GetStyleToShowInIdleTime(wxRichTextCtrl* ctrl, wxRichTextStyleType styleType);
  407. protected:
  408. /// Returns the HTML for this item
  409. virtual wxString OnGetItem(size_t n) const;
  410. private:
  411. wxRichTextStyleSheet* m_styleSheet;
  412. wxRichTextCtrl* m_richTextCtrl;
  413. bool m_applyOnSelection; // if true, applies style on selection
  414. wxRichTextStyleType m_styleType; // style type to display
  415. bool m_autoSetSelection;
  416. wxArrayString m_styleNames;
  417. };
  418. /*!
  419. * wxRichTextStyleListCtrl class declaration
  420. * This is a container for the list control plus a combobox to switch between
  421. * style types.
  422. */
  423. #define wxRICHTEXTSTYLELIST_HIDE_TYPE_SELECTOR 0x1000
  424. class WXDLLIMPEXP_RICHTEXT wxRichTextStyleListCtrl: public wxControl
  425. {
  426. DECLARE_CLASS(wxRichTextStyleListCtrl)
  427. DECLARE_EVENT_TABLE()
  428. public:
  429. /// Constructors
  430. wxRichTextStyleListCtrl()
  431. {
  432. Init();
  433. }
  434. wxRichTextStyleListCtrl(wxWindow* parent, wxWindowID id = wxID_ANY, const wxPoint& pos = wxDefaultPosition,
  435. const wxSize& size = wxDefaultSize, long style = 0);
  436. /// Constructors
  437. virtual ~wxRichTextStyleListCtrl();
  438. /// Member initialisation
  439. void Init()
  440. {
  441. m_styleListBox = NULL;
  442. m_styleChoice = NULL;
  443. m_dontUpdate = false;
  444. }
  445. /// Creates the windows
  446. bool Create(wxWindow* parent, wxWindowID id = wxID_ANY, const wxPoint& pos = wxDefaultPosition,
  447. const wxSize& size = wxDefaultSize, long style = 0);
  448. /// Updates the style list box
  449. void UpdateStyles();
  450. /// Associates the control with a style sheet
  451. void SetStyleSheet(wxRichTextStyleSheet* styleSheet);
  452. wxRichTextStyleSheet* GetStyleSheet() const;
  453. /// Associates the control with a wxRichTextCtrl
  454. void SetRichTextCtrl(wxRichTextCtrl* ctrl);
  455. wxRichTextCtrl* GetRichTextCtrl() const;
  456. /// Set/get the style type to display
  457. void SetStyleType(wxRichTextStyleListBox::wxRichTextStyleType styleType);
  458. wxRichTextStyleListBox::wxRichTextStyleType GetStyleType() const;
  459. /// Get the choice index for style type
  460. int StyleTypeToIndex(wxRichTextStyleListBox::wxRichTextStyleType styleType);
  461. /// Get the style type for choice index
  462. wxRichTextStyleListBox::wxRichTextStyleType StyleIndexToType(int i);
  463. /// Get the listbox
  464. wxRichTextStyleListBox* GetStyleListBox() const { return m_styleListBox; }
  465. /// Get the choice
  466. wxChoice* GetStyleChoice() const { return m_styleChoice; }
  467. /// React to style type choice
  468. void OnChooseType(wxCommandEvent& event);
  469. /// Lay out the controls
  470. void OnSize(wxSizeEvent& event);
  471. private:
  472. wxRichTextStyleListBox* m_styleListBox;
  473. wxChoice* m_styleChoice;
  474. bool m_dontUpdate;
  475. };
  476. #if wxUSE_COMBOCTRL
  477. /*!
  478. * Style drop-down for a wxComboCtrl
  479. */
  480. class wxRichTextStyleComboPopup : public wxRichTextStyleListBox, public wxComboPopup
  481. {
  482. public:
  483. virtual void Init()
  484. {
  485. m_itemHere = -1; // hot item in list
  486. m_value = -1;
  487. }
  488. virtual bool Create( wxWindow* parent );
  489. virtual wxWindow *GetControl() { return this; }
  490. virtual void SetStringValue( const wxString& s );
  491. virtual wxString GetStringValue() const;
  492. /// Can we set the selection based on the editor caret position?
  493. // virtual bool CanAutoSetSelection() { return ((m_combo == NULL) || !m_combo->IsPopupShown()); }
  494. virtual bool CanAutoSetSelection() { return false; }
  495. //
  496. // Popup event handlers
  497. //
  498. // Mouse hot-tracking
  499. void OnMouseMove(wxMouseEvent& event);
  500. // On mouse left, set the value and close the popup
  501. void OnMouseClick(wxMouseEvent& WXUNUSED(event));
  502. protected:
  503. int m_itemHere; // hot item in popup
  504. int m_value;
  505. private:
  506. DECLARE_EVENT_TABLE()
  507. };
  508. /*!
  509. * wxRichTextStyleComboCtrl
  510. * A combo for applying styles.
  511. */
  512. class WXDLLIMPEXP_RICHTEXT wxRichTextStyleComboCtrl: public wxComboCtrl
  513. {
  514. DECLARE_CLASS(wxRichTextStyleComboCtrl)
  515. DECLARE_EVENT_TABLE()
  516. public:
  517. wxRichTextStyleComboCtrl()
  518. {
  519. Init();
  520. }
  521. wxRichTextStyleComboCtrl(wxWindow* parent, wxWindowID id = wxID_ANY, const wxPoint& pos = wxDefaultPosition,
  522. const wxSize& size = wxDefaultSize, long style = wxCB_READONLY)
  523. {
  524. Init();
  525. Create(parent, id, pos, size, style);
  526. }
  527. virtual ~wxRichTextStyleComboCtrl() {}
  528. void Init()
  529. {
  530. m_stylePopup = NULL;
  531. }
  532. bool Create(wxWindow* parent, wxWindowID id = wxID_ANY, const wxPoint& pos = wxDefaultPosition,
  533. const wxSize& size = wxDefaultSize, long style = 0);
  534. /// Updates the list
  535. void UpdateStyles() { m_stylePopup->UpdateStyles(); }
  536. /// Associates the control with a style sheet
  537. void SetStyleSheet(wxRichTextStyleSheet* styleSheet) { m_stylePopup->SetStyleSheet(styleSheet); }
  538. wxRichTextStyleSheet* GetStyleSheet() const { return m_stylePopup->GetStyleSheet(); }
  539. /// Associates the control with a wxRichTextCtrl
  540. void SetRichTextCtrl(wxRichTextCtrl* ctrl) { m_stylePopup->SetRichTextCtrl(ctrl); }
  541. wxRichTextCtrl* GetRichTextCtrl() const { return m_stylePopup->GetRichTextCtrl(); }
  542. /// Gets the style popup
  543. wxRichTextStyleComboPopup* GetStylePopup() const { return m_stylePopup; }
  544. /// Auto-select from style under caret in idle time
  545. void OnIdle(wxIdleEvent& event);
  546. protected:
  547. wxRichTextStyleComboPopup* m_stylePopup;
  548. };
  549. #endif
  550. // wxUSE_COMBOCTRL
  551. #endif
  552. // wxUSE_HTML
  553. #endif
  554. // wxUSE_RICHTEXT
  555. #endif
  556. // _WX_RICHTEXTSTYLES_H_