richtextformatdlg.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/richtext/richtextformatdlg.h
  3. // Purpose: Formatting dialog for wxRichTextCtrl
  4. // Author: Julian Smart
  5. // Modified by:
  6. // Created: 2006-10-01
  7. // Copyright: (c) Julian Smart
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_RICHTEXTFORMATDLG_H_
  11. #define _WX_RICHTEXTFORMATDLG_H_
  12. /*!
  13. * Includes
  14. */
  15. #include "wx/defs.h"
  16. #if wxUSE_RICHTEXT
  17. #include "wx/propdlg.h"
  18. #include "wx/bookctrl.h"
  19. #include "wx/withimages.h"
  20. #if wxUSE_HTML
  21. #include "wx/htmllbox.h"
  22. #endif
  23. #include "wx/richtext/richtextbuffer.h"
  24. #include "wx/richtext/richtextstyles.h"
  25. #include "wx/richtext/richtextuicustomization.h"
  26. class WXDLLIMPEXP_FWD_RICHTEXT wxRichTextFormattingDialog;
  27. class WXDLLIMPEXP_FWD_CORE wxComboBox;
  28. class WXDLLIMPEXP_FWD_CORE wxCheckBox;
  29. /*!
  30. * Flags determining the pages and buttons to be created in the dialog
  31. */
  32. #define wxRICHTEXT_FORMAT_STYLE_EDITOR 0x0001
  33. #define wxRICHTEXT_FORMAT_FONT 0x0002
  34. #define wxRICHTEXT_FORMAT_TABS 0x0004
  35. #define wxRICHTEXT_FORMAT_BULLETS 0x0008
  36. #define wxRICHTEXT_FORMAT_INDENTS_SPACING 0x0010
  37. #define wxRICHTEXT_FORMAT_LIST_STYLE 0x0020
  38. #define wxRICHTEXT_FORMAT_MARGINS 0x0040
  39. #define wxRICHTEXT_FORMAT_SIZE 0x0080
  40. #define wxRICHTEXT_FORMAT_BORDERS 0x0100
  41. #define wxRICHTEXT_FORMAT_BACKGROUND 0x0200
  42. #define wxRICHTEXT_FORMAT_HELP_BUTTON 0x1000
  43. /*!
  44. * Indices for bullet styles in list control
  45. */
  46. enum {
  47. wxRICHTEXT_BULLETINDEX_NONE = 0,
  48. wxRICHTEXT_BULLETINDEX_ARABIC,
  49. wxRICHTEXT_BULLETINDEX_UPPER_CASE,
  50. wxRICHTEXT_BULLETINDEX_LOWER_CASE,
  51. wxRICHTEXT_BULLETINDEX_UPPER_CASE_ROMAN,
  52. wxRICHTEXT_BULLETINDEX_LOWER_CASE_ROMAN,
  53. wxRICHTEXT_BULLETINDEX_OUTLINE,
  54. wxRICHTEXT_BULLETINDEX_SYMBOL,
  55. wxRICHTEXT_BULLETINDEX_BITMAP,
  56. wxRICHTEXT_BULLETINDEX_STANDARD
  57. };
  58. /*!
  59. * Shorthand for common combinations of pages
  60. */
  61. #define wxRICHTEXT_FORMAT_PARAGRAPH (wxRICHTEXT_FORMAT_INDENTS_SPACING | wxRICHTEXT_FORMAT_BULLETS | wxRICHTEXT_FORMAT_TABS | wxRICHTEXT_FORMAT_FONT)
  62. #define wxRICHTEXT_FORMAT_CHARACTER (wxRICHTEXT_FORMAT_FONT)
  63. #define wxRICHTEXT_FORMAT_STYLE (wxRICHTEXT_FORMAT_PARAGRAPH | wxRICHTEXT_FORMAT_STYLE_EDITOR)
  64. /*!
  65. * Factory for formatting dialog
  66. */
  67. class WXDLLIMPEXP_RICHTEXT wxRichTextFormattingDialogFactory: public wxObject
  68. {
  69. public:
  70. wxRichTextFormattingDialogFactory() {}
  71. virtual ~wxRichTextFormattingDialogFactory() {}
  72. // Overridables
  73. /// Create all pages, under the dialog's book control, also calling AddPage
  74. virtual bool CreatePages(long pages, wxRichTextFormattingDialog* dialog);
  75. /// Create a page, given a page identifier
  76. virtual wxPanel* CreatePage(int page, wxString& title, wxRichTextFormattingDialog* dialog);
  77. /// Enumerate all available page identifiers
  78. virtual int GetPageId(int i) const;
  79. /// Get the number of available page identifiers
  80. virtual int GetPageIdCount() const;
  81. /// Get the image index for the given page identifier
  82. virtual int GetPageImage(int WXUNUSED(id)) const { return -1; }
  83. /// Invoke help for the dialog
  84. virtual bool ShowHelp(int page, wxRichTextFormattingDialog* dialog);
  85. /// Set the sheet style, called at the start of wxRichTextFormattingDialog::Create
  86. virtual bool SetSheetStyle(wxRichTextFormattingDialog* dialog);
  87. /// Create the main dialog buttons
  88. virtual bool CreateButtons(wxRichTextFormattingDialog* dialog);
  89. };
  90. /*!
  91. * Formatting dialog for a wxRichTextCtrl
  92. */
  93. class WXDLLIMPEXP_RICHTEXT wxRichTextFormattingDialog: public wxPropertySheetDialog,
  94. public wxWithImages
  95. {
  96. DECLARE_CLASS(wxRichTextFormattingDialog)
  97. DECLARE_HELP_PROVISION()
  98. public:
  99. enum { Option_AllowPixelFontSize = 0x0001 };
  100. wxRichTextFormattingDialog() { Init(); }
  101. wxRichTextFormattingDialog(long flags, wxWindow* parent, const wxString& title = wxGetTranslation(wxT("Formatting")), wxWindowID id = wxID_ANY,
  102. const wxPoint& pos = wxDefaultPosition, const wxSize& sz = wxDefaultSize,
  103. long style = wxDEFAULT_DIALOG_STYLE)
  104. {
  105. Init();
  106. Create(flags, parent, title, id, pos, sz, style);
  107. }
  108. ~wxRichTextFormattingDialog();
  109. void Init();
  110. bool Create(long flags, wxWindow* parent, const wxString& title = wxGetTranslation(wxT("Formatting")), wxWindowID id = wxID_ANY,
  111. const wxPoint& pos = wxDefaultPosition, const wxSize& sz = wxDefaultSize,
  112. long style = wxDEFAULT_DIALOG_STYLE);
  113. /// Get attributes from the given range
  114. virtual bool GetStyle(wxRichTextCtrl* ctrl, const wxRichTextRange& range);
  115. /// Set the attributes and optionally update the display
  116. virtual bool SetStyle(const wxRichTextAttr& style, bool update = true);
  117. /// Set the style definition and optionally update the display
  118. virtual bool SetStyleDefinition(const wxRichTextStyleDefinition& styleDef, wxRichTextStyleSheet* sheet, bool update = true);
  119. /// Get the style definition, if any
  120. virtual wxRichTextStyleDefinition* GetStyleDefinition() const { return m_styleDefinition; }
  121. /// Get the style sheet, if any
  122. virtual wxRichTextStyleSheet* GetStyleSheet() const { return m_styleSheet; }
  123. /// Update the display
  124. virtual bool UpdateDisplay();
  125. /// Apply attributes to the given range
  126. virtual bool ApplyStyle(wxRichTextCtrl* ctrl, const wxRichTextRange& range, int flags = wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE);
  127. /// Apply attributes to the object being edited, if any
  128. virtual bool ApplyStyle(wxRichTextCtrl* ctrl, int flags = wxRICHTEXT_SETSTYLE_WITH_UNDO);
  129. /// Gets and sets the attributes
  130. const wxRichTextAttr& GetAttributes() const { return m_attributes; }
  131. wxRichTextAttr& GetAttributes() { return m_attributes; }
  132. void SetAttributes(const wxRichTextAttr& attr) { m_attributes = attr; }
  133. /// Sets the dialog options, determining what the interface presents to the user.
  134. /// Currently the only option is Option_AllowPixelFontSize.
  135. void SetOptions(int options) { m_options = options; }
  136. /// Gets the dialog options, determining what the interface presents to the user.
  137. /// Currently the only option is Option_AllowPixelFontSize.
  138. int GetOptions() const { return m_options; }
  139. /// Returns @true if the given option is present.
  140. bool HasOption(int option) const { return (m_options & option) != 0; }
  141. /// If editing the attributes for a particular object, such as an image,
  142. /// set the object so the code can initialize attributes such as size correctly.
  143. wxRichTextObject* GetObject() const { return m_object; }
  144. void SetObject(wxRichTextObject* obj) { m_object = obj; }
  145. /// Transfers the data and from to the window
  146. virtual bool TransferDataToWindow();
  147. virtual bool TransferDataFromWindow();
  148. /// Apply the styles when a different tab is selected, so the previews are
  149. /// up to date
  150. void OnTabChanged(wxBookCtrlEvent& event);
  151. /// Respond to help command
  152. void OnHelp(wxCommandEvent& event);
  153. void OnUpdateHelp(wxUpdateUIEvent& event);
  154. /// Get/set formatting factory object
  155. static void SetFormattingDialogFactory(wxRichTextFormattingDialogFactory* factory);
  156. static wxRichTextFormattingDialogFactory* GetFormattingDialogFactory() { return ms_FormattingDialogFactory; }
  157. /// Helper for pages to get the top-level dialog
  158. static wxRichTextFormattingDialog* GetDialog(wxWindow* win);
  159. /// Helper for pages to get the attributes
  160. static wxRichTextAttr* GetDialogAttributes(wxWindow* win);
  161. /// Helper for pages to get the reset attributes
  162. static wxRichTextAttr* GetDialogResetAttributes(wxWindow* win);
  163. /// Helper for pages to get the style
  164. static wxRichTextStyleDefinition* GetDialogStyleDefinition(wxWindow* win);
  165. /// Should we show tooltips?
  166. static bool ShowToolTips() { return sm_showToolTips; }
  167. /// Determines whether tooltips will be shown
  168. static void SetShowToolTips(bool show) { sm_showToolTips = show; }
  169. /// Set the dimension into the value and units controls. Optionally pass units to
  170. /// specify the ordering of units in the combobox.
  171. static void SetDimensionValue(wxTextAttrDimension& dim, wxTextCtrl* valueCtrl, wxComboBox* unitsCtrl, wxCheckBox* checkBox, wxArrayInt* units = NULL);
  172. /// Get the dimension from the value and units controls Optionally pass units to
  173. /// specify the ordering of units in the combobox.
  174. static void GetDimensionValue(wxTextAttrDimension& dim, wxTextCtrl* valueCtrl, wxComboBox* unitsCtrl, wxCheckBox* checkBox, wxArrayInt* units = NULL);
  175. /// Convert from a string to a dimension integer.
  176. static bool ConvertFromString(const wxString& str, int& ret, int unit);
  177. /// Map book control page index to our page id
  178. void AddPageId(int id) { m_pageIds.Add(id); }
  179. /// Find a page by class
  180. wxWindow* FindPage(wxClassInfo* info) const;
  181. protected:
  182. wxRichTextAttr m_attributes;
  183. wxRichTextStyleDefinition* m_styleDefinition;
  184. wxRichTextStyleSheet* m_styleSheet;
  185. wxRichTextObject* m_object;
  186. wxArrayInt m_pageIds; // mapping of book control indexes to page ids
  187. int m_options; // UI options
  188. static wxRichTextFormattingDialogFactory* ms_FormattingDialogFactory;
  189. static bool sm_showToolTips;
  190. DECLARE_EVENT_TABLE()
  191. };
  192. //-----------------------------------------------------------------------------
  193. // helper class - wxRichTextFontPreviewCtrl
  194. //-----------------------------------------------------------------------------
  195. class WXDLLIMPEXP_RICHTEXT wxRichTextFontPreviewCtrl : public wxWindow
  196. {
  197. public:
  198. wxRichTextFontPreviewCtrl(wxWindow *parent, wxWindowID id = wxID_ANY, const wxPoint& pos = wxDefaultPosition, const wxSize& sz = wxDefaultSize, long style = 0);
  199. void SetTextEffects(int effects) { m_textEffects = effects; }
  200. int GetTextEffects() const { return m_textEffects; }
  201. private:
  202. int m_textEffects;
  203. void OnPaint(wxPaintEvent& event);
  204. DECLARE_EVENT_TABLE()
  205. };
  206. /*
  207. * A control for displaying a small preview of a colour or bitmap
  208. */
  209. class WXDLLIMPEXP_RICHTEXT wxRichTextColourSwatchCtrl: public wxControl
  210. {
  211. DECLARE_CLASS(wxRichTextColourSwatchCtrl)
  212. public:
  213. wxRichTextColourSwatchCtrl(wxWindow* parent, wxWindowID id, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = 0);
  214. ~wxRichTextColourSwatchCtrl();
  215. void OnMouseEvent(wxMouseEvent& event);
  216. void SetColour(const wxColour& colour) { m_colour = colour; SetBackgroundColour(m_colour); }
  217. wxColour& GetColour() { return m_colour; }
  218. virtual wxSize DoGetBestSize() const { return GetSize(); }
  219. protected:
  220. wxColour m_colour;
  221. DECLARE_EVENT_TABLE()
  222. };
  223. /*!
  224. * wxRichTextFontListBox class declaration
  225. * A listbox to display fonts.
  226. */
  227. class WXDLLIMPEXP_RICHTEXT wxRichTextFontListBox: public wxHtmlListBox
  228. {
  229. DECLARE_CLASS(wxRichTextFontListBox)
  230. DECLARE_EVENT_TABLE()
  231. public:
  232. wxRichTextFontListBox()
  233. {
  234. Init();
  235. }
  236. wxRichTextFontListBox(wxWindow* parent, wxWindowID id = wxID_ANY, const wxPoint& pos = wxDefaultPosition,
  237. const wxSize& size = wxDefaultSize, long style = 0);
  238. virtual ~wxRichTextFontListBox();
  239. void Init()
  240. {
  241. }
  242. bool Create(wxWindow* parent, wxWindowID id = wxID_ANY, const wxPoint& pos = wxDefaultPosition,
  243. const wxSize& size = wxDefaultSize, long style = 0);
  244. /// Creates a suitable HTML fragment for a font
  245. wxString CreateHTML(const wxString& facename) const;
  246. /// Get font name for index
  247. wxString GetFaceName(size_t i) const ;
  248. /// Set selection for string, returning the index.
  249. int SetFaceNameSelection(const wxString& name);
  250. /// Updates the font list
  251. void UpdateFonts();
  252. /// Does this face name exist?
  253. bool HasFaceName(const wxString& faceName) const { return m_faceNames.Index(faceName) != wxNOT_FOUND; }
  254. /// Returns the array of face names
  255. const wxArrayString& GetFaceNames() const { return m_faceNames; }
  256. protected:
  257. /// Returns the HTML for this item
  258. virtual wxString OnGetItem(size_t n) const;
  259. private:
  260. wxArrayString m_faceNames;
  261. };
  262. #endif
  263. // wxUSE_RICHTEXT
  264. #endif
  265. // _WX_RICHTEXTFORMATDLG_H_