richtextstyles.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: richtext/richtextstyles.h
  3. // Purpose: interface of wxRichTextStyleListCtrl
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @class wxRichTextStyleListCtrl
  9. This class incorporates a wxRichTextStyleListBox and a choice control that
  10. allows the user to select the category of style to view.
  11. It is demonstrated in the wxRichTextCtrl sample in @c samples/richtext.
  12. To use wxRichTextStyleListCtrl, add the control to your window hierarchy and
  13. call wxRichTextStyleListCtrl::SetStyleType with one of
  14. wxRichTextStyleListBox::wxRICHTEXT_STYLE_ALL,
  15. wxRichTextStyleListBox::wxRICHTEXT_STYLE_PARAGRAPH,
  16. wxRichTextStyleListBox::wxRICHTEXT_STYLE_CHARACTER and
  17. wxRichTextStyleListBox::wxRICHTEXT_STYLE_LIST to set the current view.
  18. Associate the control with a style sheet and rich text control with
  19. SetStyleSheet and SetRichTextCtrl, so that when a style is double-clicked,
  20. it is applied to the selection.
  21. @beginStyleTable
  22. @style{wxRICHTEXTSTYLELIST_HIDE_TYPE_SELECTOR}
  23. This style hides the category selection control.
  24. @endStyleTable
  25. @library{wxrichtext}
  26. @category{richtext}
  27. */
  28. class wxRichTextStyleListCtrl : public wxControl
  29. {
  30. public:
  31. //@{
  32. /**
  33. Constructors.
  34. */
  35. wxRichTextStyleListCtrl(wxWindow* parent,
  36. wxWindowID id = wxID_ANY,
  37. const wxPoint& pos = wxDefaultPosition,
  38. const wxSize& size = wxDefaultSize,
  39. long style = 0);
  40. wxRichTextStyleListCtrl();
  41. //@}
  42. /**
  43. Creates the windows.
  44. */
  45. bool Create(wxWindow* parent, wxWindowID id = wxID_ANY,
  46. const wxPoint& pos = wxDefaultPosition,
  47. const wxSize& size = wxDefaultSize,
  48. long style = 0);
  49. /**
  50. Returns the associated rich text control, if any.
  51. */
  52. wxRichTextCtrl* GetRichTextCtrl() const;
  53. /**
  54. Returns the wxChoice control used for selecting the style category.
  55. */
  56. wxChoice* GetStyleChoice() const;
  57. /**
  58. Returns the wxListBox control used to view the style list.
  59. */
  60. wxRichTextStyleListBox* GetStyleListBox() const;
  61. /**
  62. Returns the associated style sheet, if any.
  63. */
  64. wxRichTextStyleSheet* GetStyleSheet() const;
  65. /**
  66. Returns the type of style to show in the list box.
  67. */
  68. wxRichTextStyleListBox::wxRichTextStyleType GetStyleType() const;
  69. /**
  70. Associates the control with a wxRichTextCtrl.
  71. */
  72. void SetRichTextCtrl(wxRichTextCtrl* ctrl);
  73. /**
  74. Associates the control with a style sheet.
  75. */
  76. void SetStyleSheet(wxRichTextStyleSheet* styleSheet);
  77. /**
  78. Sets the style type to display.
  79. One of
  80. - wxRichTextStyleListBox::wxRICHTEXT_STYLE_ALL,
  81. - wxRichTextStyleListBox::wxRICHTEXT_STYLE_PARAGRAPH,
  82. - wxRichTextStyleListBox::wxRICHTEXT_STYLE_CHARACTER
  83. - wxRichTextStyleListBox::wxRICHTEXT_STYLE_LIST.
  84. */
  85. void SetStyleType(wxRichTextStyleListBox::wxRichTextStyleType styleType);
  86. /**
  87. Updates the style list box.
  88. */
  89. void UpdateStyles();
  90. };
  91. /**
  92. @class wxRichTextStyleDefinition
  93. This is a base class for paragraph and character styles.
  94. @library{wxrichtext}
  95. @category{richtext}
  96. */
  97. class wxRichTextStyleDefinition : public wxObject
  98. {
  99. public:
  100. /**
  101. Constructor.
  102. */
  103. wxRichTextStyleDefinition(const wxString& name = wxEmptyString);
  104. /**
  105. Destructor.
  106. */
  107. virtual ~wxRichTextStyleDefinition();
  108. /**
  109. Returns the style on which this style is based.
  110. */
  111. const wxString& GetBaseStyle() const;
  112. /**
  113. Returns the style's description.
  114. */
  115. const wxString& GetDescription() const;
  116. /**
  117. Returns the style name.
  118. */
  119. const wxString& GetName() const;
  120. //@{
  121. /**
  122. Returns the attributes associated with this style.
  123. */
  124. wxRichTextAttr GetStyle() const;
  125. const wxRichTextAttr GetStyle() const;
  126. //@}
  127. /**
  128. Returns the style attributes combined with the attributes of the specified base
  129. style, if any. This function works recursively.
  130. */
  131. virtual wxRichTextAttr GetStyleMergedWithBase(const wxRichTextStyleSheet* sheet) const;
  132. /**
  133. Sets the name of the style that this style is based on.
  134. */
  135. void SetBaseStyle(const wxString& name);
  136. /**
  137. Sets the style description.
  138. */
  139. void SetDescription(const wxString& descr);
  140. /**
  141. Sets the name of the style.
  142. */
  143. void SetName(const wxString& name);
  144. /**
  145. Sets the attributes for this style.
  146. */
  147. void SetStyle(const wxRichTextAttr& style);
  148. /**
  149. Returns the definition's properties.
  150. */
  151. wxRichTextProperties& GetProperties();
  152. /**
  153. Returns the definition's properties.
  154. */
  155. const wxRichTextProperties& GetProperties() const;
  156. /**
  157. Sets the definition's properties.
  158. */
  159. void SetProperties(const wxRichTextProperties& props);
  160. };
  161. /**
  162. @class wxRichTextParagraphStyleDefinition
  163. This class represents a paragraph style definition, usually added to a
  164. wxRichTextStyleSheet.
  165. @library{wxrichtext}
  166. @category{richtext}
  167. */
  168. class wxRichTextParagraphStyleDefinition : public wxRichTextStyleDefinition
  169. {
  170. public:
  171. /**
  172. Constructor.
  173. */
  174. wxRichTextParagraphStyleDefinition(const wxString& name = wxEmptyString);
  175. /**
  176. Destructor.
  177. */
  178. virtual ~wxRichTextParagraphStyleDefinition();
  179. /**
  180. Returns the style that should normally follow this style.
  181. */
  182. const wxString& GetNextStyle() const;
  183. /**
  184. Sets the style that should normally follow this style.
  185. */
  186. void SetNextStyle(const wxString& name);
  187. };
  188. /**
  189. @class wxRichTextStyleListBox
  190. This is a listbox that can display the styles in a wxRichTextStyleSheet,
  191. and apply the selection to an associated wxRichTextCtrl.
  192. See @c samples/richtext for an example of how to use it.
  193. @library{wxrichtext}
  194. @category{richtext}
  195. @see wxRichTextStyleComboCtrl, @ref overview_richtextctrl
  196. */
  197. class wxRichTextStyleListBox : public wxHtmlListBox
  198. {
  199. public:
  200. /// Which type of style definition is currently showing?
  201. enum wxRichTextStyleType
  202. {
  203. wxRICHTEXT_STYLE_ALL,
  204. wxRICHTEXT_STYLE_PARAGRAPH,
  205. wxRICHTEXT_STYLE_CHARACTER,
  206. wxRICHTEXT_STYLE_LIST,
  207. wxRICHTEXT_STYLE_BOX
  208. };
  209. /**
  210. Constructor.
  211. */
  212. wxRichTextStyleListBox(wxWindow* parent,
  213. wxWindowID id = wxID_ANY,
  214. const wxPoint& pos = wxDefaultPosition,
  215. const wxSize& size = wxDefaultSize,
  216. long style = 0);
  217. /**
  218. Destructor.
  219. */
  220. virtual ~wxRichTextStyleListBox();
  221. /**
  222. Applies the @e ith style to the associated rich text control.
  223. */
  224. void ApplyStyle(int i);
  225. /**
  226. Converts units in tenths of a millimetre to device units.
  227. */
  228. int ConvertTenthsMMToPixels(wxDC& dc, int units) const;
  229. /**
  230. Creates a suitable HTML fragment for a definition.
  231. */
  232. wxString CreateHTML(wxRichTextStyleDefinition* def) const;
  233. /**
  234. If the return value is @true, clicking on a style name in the list will
  235. immediately apply the style to the associated rich text control.
  236. */
  237. bool GetApplyOnSelection() const;
  238. /**
  239. Returns the wxRichTextCtrl associated with this listbox.
  240. */
  241. wxRichTextCtrl* GetRichTextCtrl() const;
  242. /**
  243. Gets a style for a listbox index.
  244. */
  245. wxRichTextStyleDefinition* GetStyle(size_t i) const;
  246. /**
  247. Returns the style sheet associated with this listbox.
  248. */
  249. wxRichTextStyleSheet* GetStyleSheet() const;
  250. /**
  251. Returns the type of style to show in the list box.
  252. */
  253. wxRichTextStyleListBox::wxRichTextStyleType GetStyleType() const;
  254. /**
  255. Implements left click behaviour, applying the clicked style to the
  256. wxRichTextCtrl.
  257. */
  258. void OnLeftDown(wxMouseEvent& event);
  259. /**
  260. If @a applyOnSelection is @true, clicking on a style name in the list will
  261. immediately apply the style to the associated rich text control.
  262. */
  263. void SetApplyOnSelection(bool applyOnSelection);
  264. /**
  265. Associates the listbox with a wxRichTextCtrl.
  266. */
  267. void SetRichTextCtrl(wxRichTextCtrl* ctrl);
  268. /**
  269. Associates the control with a style sheet.
  270. */
  271. void SetStyleSheet(wxRichTextStyleSheet* styleSheet);
  272. /**
  273. Sets the style type to display. One of
  274. - wxRichTextStyleListBox::wxRICHTEXT_STYLE_ALL,
  275. - wxRichTextStyleListBox::wxRICHTEXT_STYLE_PARAGRAPH,
  276. - wxRichTextStyleListBox::wxRICHTEXT_STYLE_CHARACTER
  277. - wxRichTextStyleListBox::wxRICHTEXT_STYLE_LIST.
  278. */
  279. void SetStyleType(wxRichTextStyleListBox::wxRichTextStyleType styleType);
  280. /**
  281. Updates the list from the associated style sheet.
  282. */
  283. void UpdateStyles();
  284. protected:
  285. /**
  286. Returns the HTML for this item.
  287. */
  288. virtual wxString OnGetItem(size_t n) const;
  289. };
  290. /**
  291. @class wxRichTextStyleComboCtrl
  292. This is a combo control that can display the styles in a wxRichTextStyleSheet,
  293. and apply the selection to an associated wxRichTextCtrl.
  294. See @c samples/richtext for an example of how to use it.
  295. @library{wxrichtext}
  296. @category{richtext}
  297. @see wxRichTextStyleListBox, @ref overview_richtextctrl
  298. */
  299. class wxRichTextStyleComboCtrl : public wxComboCtrl
  300. {
  301. public:
  302. /**
  303. Constructor.
  304. */
  305. wxRichTextStyleComboCtrl(wxWindow* parent,
  306. wxWindowID id = wxID_ANY,
  307. const wxPoint& pos = wxDefaultPosition,
  308. const wxSize& size = wxDefaultSize,
  309. long style = 0);
  310. /**
  311. Destructor.
  312. */
  313. virtual ~wxRichTextStyleComboCtrl();
  314. /**
  315. Returns the wxRichTextCtrl associated with this control.
  316. */
  317. wxRichTextCtrl* GetRichTextCtrl() const;
  318. /**
  319. Returns the style sheet associated with this control.
  320. */
  321. wxRichTextStyleSheet* GetStyleSheet() const;
  322. /**
  323. Associates the control with a wxRichTextCtrl.
  324. */
  325. void SetRichTextCtrl(wxRichTextCtrl* ctrl);
  326. /**
  327. Associates the control with a style sheet.
  328. */
  329. void SetStyleSheet(wxRichTextStyleSheet* styleSheet);
  330. /**
  331. Updates the combo control from the associated style sheet.
  332. */
  333. void UpdateStyles();
  334. };
  335. /**
  336. @class wxRichTextCharacterStyleDefinition
  337. This class represents a character style definition, usually added to a
  338. wxRichTextStyleSheet.
  339. @library{wxrichtext}
  340. @category{richtext}
  341. */
  342. class wxRichTextCharacterStyleDefinition : public wxRichTextStyleDefinition
  343. {
  344. public:
  345. /**
  346. Constructor.
  347. */
  348. wxRichTextCharacterStyleDefinition(const wxString& name = wxEmptyString);
  349. /**
  350. Destructor.
  351. */
  352. virtual ~wxRichTextCharacterStyleDefinition();
  353. };
  354. /**
  355. @class wxRichTextListStyleDefinition
  356. This class represents a list style definition, usually added to a
  357. wxRichTextStyleSheet.
  358. The class inherits paragraph attributes from wxRichTextStyleParagraphDefinition,
  359. and adds 10 further attribute objects, one for each level of a list.
  360. When applying a list style to a paragraph, the list style's base and
  361. appropriate level attributes are merged with the paragraph's existing attributes.
  362. You can apply a list style to one or more paragraphs using wxRichTextCtrl::SetListStyle.
  363. You can also use the functions wxRichTextCtrl::NumberList, wxRichTextCtrl::PromoteList and
  364. wxRichTextCtrl::ClearListStyle.
  365. As usual, there are wxRichTextBuffer versions of these functions
  366. so that you can apply them directly to a buffer without requiring a control.
  367. @library{wxrichtext}
  368. @category{richtext}
  369. */
  370. class wxRichTextListStyleDefinition : public wxRichTextParagraphStyleDefinition
  371. {
  372. public:
  373. /**
  374. Constructor.
  375. */
  376. wxRichTextListStyleDefinition(const wxString& name = wxEmptyString);
  377. /**
  378. Destructor.
  379. */
  380. virtual ~wxRichTextListStyleDefinition();
  381. /**
  382. This function combines the given paragraph style with the list style's base
  383. attributes and level style matching the given indent, returning the combined attributes.
  384. If @a styleSheet is specified, the base style for this definition will also be
  385. included in the result.
  386. */
  387. wxRichTextAttr CombineWithParagraphStyle(int indent,
  388. const wxRichTextAttr& paraStyle,
  389. wxRichTextStyleSheet* styleSheet = NULL);
  390. /**
  391. This function finds the level (from 0 to 9) whose indentation attribute mostly
  392. closely matches @a indent (expressed in tenths of a millimetre).
  393. */
  394. int FindLevelForIndent(int indent) const;
  395. /**
  396. This function combines the list style's base attributes and the level style
  397. matching the given indent, returning the combined attributes.
  398. If @a styleSheet is specified, the base style for this definition will also be
  399. included in the result.
  400. */
  401. wxRichTextAttr GetCombinedStyle(int indent,
  402. wxRichTextStyleSheet* styleSheet = NULL);
  403. /**
  404. This function combines the list style's base attributes and the style for the
  405. specified level, returning the combined attributes.
  406. If @a styleSheet is specified, the base style for this definition will also be
  407. included in the result.
  408. */
  409. wxRichTextAttr GetCombinedStyleForLevel(int level,
  410. wxRichTextStyleSheet* styleSheet = NULL);
  411. /**
  412. Returns the style for the given level. @a level is a number between 0 and 9.
  413. */
  414. const wxRichTextAttr* GetLevelAttributes(int level) const;
  415. /**
  416. Returns the number of levels. This is hard-wired to 10.
  417. Returns the style for the given level. @e level is a number between 0 and 9.
  418. */
  419. int GetLevelCount() const;
  420. /**
  421. Returns @true if the given level has numbered list attributes.
  422. */
  423. bool IsNumbered(int level) const;
  424. /**
  425. Sets the style for the given level. @a level is a number between 0 and 9.
  426. The first and most flexible form uses a wxTextAttr object, while the second
  427. form is for convenient setting of the most commonly-used attributes.
  428. */
  429. void SetLevelAttributes(int level, const wxRichTextAttr& attr);
  430. };
  431. /**
  432. @class wxRichTextStyleSheet
  433. A style sheet contains named paragraph and character styles that make it
  434. easy for a user to apply combinations of attributes to a wxRichTextCtrl.
  435. You can use a wxRichTextStyleListBox in your user interface to show available
  436. styles to the user, and allow application of styles to the control.
  437. @library{wxrichtext}
  438. @category{richtext}
  439. */
  440. class wxRichTextStyleSheet : public wxObject
  441. {
  442. public:
  443. /**
  444. Constructor.
  445. */
  446. wxRichTextStyleSheet();
  447. /**
  448. Destructor.
  449. */
  450. virtual ~wxRichTextStyleSheet();
  451. /**
  452. Adds a definition to the character style list.
  453. */
  454. bool AddCharacterStyle(wxRichTextCharacterStyleDefinition* def);
  455. /**
  456. Adds a definition to the list style list.
  457. */
  458. bool AddListStyle(wxRichTextListStyleDefinition* def);
  459. /**
  460. Adds a definition to the paragraph style list.
  461. */
  462. bool AddParagraphStyle(wxRichTextParagraphStyleDefinition* def);
  463. /**
  464. Adds a definition to the appropriate style list.
  465. */
  466. bool AddStyle(wxRichTextStyleDefinition* def);
  467. /**
  468. Deletes all styles.
  469. */
  470. void DeleteStyles();
  471. /**
  472. Finds a character definition by name.
  473. */
  474. wxRichTextCharacterStyleDefinition* FindCharacterStyle(const wxString& name,
  475. bool recurse = true) const;
  476. /**
  477. Finds a list definition by name.
  478. */
  479. wxRichTextListStyleDefinition* FindListStyle(const wxString& name,
  480. bool recurse = true) const;
  481. /**
  482. Finds a paragraph definition by name.
  483. */
  484. wxRichTextParagraphStyleDefinition* FindParagraphStyle(const wxString& name,
  485. bool recurse = true) const;
  486. /**
  487. Finds a style definition by name.
  488. */
  489. wxRichTextStyleDefinition* FindStyle(const wxString& name) const;
  490. /**
  491. Returns the @e nth character style.
  492. */
  493. wxRichTextCharacterStyleDefinition* GetCharacterStyle(size_t n) const;
  494. /**
  495. Returns the number of character styles.
  496. */
  497. size_t GetCharacterStyleCount() const;
  498. /**
  499. Returns the style sheet's description.
  500. */
  501. const wxString& GetDescription() const;
  502. /**
  503. Returns the @e nth list style.
  504. */
  505. wxRichTextListStyleDefinition* GetListStyle(size_t n) const;
  506. /**
  507. Returns the number of list styles.
  508. */
  509. size_t GetListStyleCount() const;
  510. /**
  511. Returns the style sheet's name.
  512. */
  513. const wxString& GetName() const;
  514. /**
  515. Returns the @e nth paragraph style.
  516. */
  517. wxRichTextParagraphStyleDefinition* GetParagraphStyle(size_t n) const;
  518. /**
  519. Returns the number of paragraph styles.
  520. */
  521. size_t GetParagraphStyleCount() const;
  522. /**
  523. Removes a character style.
  524. */
  525. bool RemoveCharacterStyle(wxRichTextStyleDefinition* def,
  526. bool deleteStyle = false);
  527. /**
  528. Removes a list style.
  529. */
  530. bool RemoveListStyle(wxRichTextStyleDefinition* def,
  531. bool deleteStyle = false);
  532. /**
  533. Removes a paragraph style.
  534. */
  535. bool RemoveParagraphStyle(wxRichTextStyleDefinition* def,
  536. bool deleteStyle = false);
  537. /**
  538. Removes a style.
  539. */
  540. bool RemoveStyle(wxRichTextStyleDefinition* def,
  541. bool deleteStyle = false);
  542. /**
  543. Sets the style sheet's description.
  544. */
  545. void SetDescription(const wxString& descr);
  546. /**
  547. Sets the style sheet's name.
  548. */
  549. void SetName(const wxString& name);
  550. /**
  551. Returns the sheet's properties.
  552. */
  553. wxRichTextProperties& GetProperties();
  554. /**
  555. Returns the sheet's properties.
  556. */
  557. const wxRichTextProperties& GetProperties() const;
  558. /**
  559. Sets the sheet's properties.
  560. */
  561. void SetProperties(const wxRichTextProperties& props);
  562. };