textwrapper.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/textwrapper.h
  3. // Purpose: documentation of wxTextWrapper interface
  4. // Author: Vadim Zeitlin
  5. // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
  6. // Licence: wxWindows licence
  7. /////////////////////////////////////////////////////////////////////////////
  8. /**
  9. @class wxTextWrapper
  10. Helps wrap lines of text to given width.
  11. This is a generic purpose class which can be used to wrap lines of text to
  12. the specified width. It doesn't do anything by itself but simply calls its
  13. virtual OnOutputLine() and OnNewLine() methods for each wrapped line of
  14. text, you need to implement them in your derived class to actually do
  15. something useful.
  16. Here is an example function using this class which inserts hard line breaks
  17. into a string of text at the positions where it would be wrapped:
  18. @code
  19. wxString WrapText(wxWindow *win, const wxString& text, int widthMax)
  20. {
  21. class HardBreakWrapper : public wxTextWrapper
  22. {
  23. public:
  24. HardBreakWrapper(wxWindow *win, const wxString& text, int widthMax)
  25. {
  26. Wrap(win, text, widthMax);
  27. }
  28. wxString const& GetWrapped() const { return m_wrapped; }
  29. protected:
  30. virtual void OnOutputLine(const wxString& line)
  31. {
  32. m_wrapped += line;
  33. }
  34. virtual void OnNewLine()
  35. {
  36. m_wrapped += '\n';
  37. }
  38. private:
  39. wxString m_wrapped;
  40. };
  41. HardBreakWrapper wrapper(win, text, widthMax);
  42. return wrapper.GetWrapped();
  43. }
  44. @endcode
  45. @nolibrary
  46. @category{gdi}
  47. */
  48. class wxTextWrapper
  49. {
  50. public:
  51. /**
  52. Trivial default constructor.
  53. */
  54. wxTextWrapper();
  55. /**
  56. Wrap the given text.
  57. This method will call OnOutputLine() for every line of wrapped text and
  58. OnNewLine() before the beginning of every new line after the first one
  59. (so it might be never called at all if the width of entire @a text is
  60. less than @a widthMax).
  61. @param win
  62. A non-@NULL window used for measuring the text extents.
  63. @param text
  64. The text to wrap.
  65. @param widthMax
  66. Maximal width of each line of text or @c -1 to disable wrapping.
  67. */
  68. void Wrap(wxWindow *win, const wxString& text, int widthMax);
  69. protected:
  70. /**
  71. Called by Wrap() for each wrapped line of text.
  72. This method will always be called at least once by Wrap(). Notice that
  73. @a line may be empty if the text passed to Wrap() was empty itself.
  74. */
  75. virtual void OnOutputLine(const wxString& line) = 0;
  76. /**
  77. Called at the start of each subsequent line of text by Wrap().
  78. This method may not be called at all if the entire text passed to
  79. Wrap() fits into the specified width.
  80. */
  81. virtual void OnNewLine();
  82. };