textwrapper.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/textwrapper.h
  3. // Purpose: declaration of wxTextWrapper class
  4. // Author: Vadim Zeitlin
  5. // Created: 2009-05-31 (extracted from dlgcmn.cpp via wx/private/stattext.h)
  6. // Copyright: (c) 1999, 2009 Vadim Zeitlin <vadim@wxwidgets.org>
  7. // Licence: wxWindows licence
  8. ///////////////////////////////////////////////////////////////////////////////
  9. #ifndef _WX_TEXTWRAPPER_H_
  10. #define _WX_TEXTWRAPPER_H_
  11. #include "wx/window.h"
  12. // ----------------------------------------------------------------------------
  13. // wxTextWrapper
  14. // ----------------------------------------------------------------------------
  15. // this class is used to wrap the text on word boundary: wrapping is done by
  16. // calling OnStartLine() and OnOutputLine() functions
  17. class WXDLLIMPEXP_CORE wxTextWrapper
  18. {
  19. public:
  20. wxTextWrapper() { m_eol = false; }
  21. // win is used for getting the font, text is the text to wrap, width is the
  22. // max line width or -1 to disable wrapping
  23. void Wrap(wxWindow *win, const wxString& text, int widthMax);
  24. // we don't need it, but just to avoid compiler warnings
  25. virtual ~wxTextWrapper() { }
  26. protected:
  27. // line may be empty
  28. virtual void OnOutputLine(const wxString& line) = 0;
  29. // called at the start of every new line (except the very first one)
  30. virtual void OnNewLine() { }
  31. private:
  32. // call OnOutputLine() and set m_eol to true
  33. void DoOutputLine(const wxString& line)
  34. {
  35. OnOutputLine(line);
  36. m_eol = true;
  37. }
  38. // this function is a destructive inspector: when it returns true it also
  39. // resets the flag to false so calling it again wouldn't return true any
  40. // more
  41. bool IsStartOfNewLine()
  42. {
  43. if ( !m_eol )
  44. return false;
  45. m_eol = false;
  46. return true;
  47. }
  48. bool m_eol;
  49. wxDECLARE_NO_COPY_CLASS(wxTextWrapper);
  50. };
  51. #if wxUSE_STATTEXT
  52. #include "wx/sizer.h"
  53. #include "wx/stattext.h"
  54. // A class creating a sizer with one static text per line of text. Creation of
  55. // the controls used for each line can be customized by overriding
  56. // OnCreateLine() function.
  57. //
  58. // This class is currently private to wxWidgets and used only by wxDialog
  59. // itself. We may make it public later if there is sufficient interest.
  60. class wxTextSizerWrapper : public wxTextWrapper
  61. {
  62. public:
  63. wxTextSizerWrapper(wxWindow *win)
  64. {
  65. m_win = win;
  66. m_hLine = 0;
  67. }
  68. wxSizer *CreateSizer(const wxString& text, int widthMax)
  69. {
  70. m_sizer = new wxBoxSizer(wxVERTICAL);
  71. Wrap(m_win, text, widthMax);
  72. return m_sizer;
  73. }
  74. wxWindow *GetParent() const { return m_win; }
  75. protected:
  76. virtual wxWindow *OnCreateLine(const wxString& line)
  77. {
  78. return new wxStaticText(m_win, wxID_ANY,
  79. wxControl::EscapeMnemonics(line));
  80. }
  81. virtual void OnOutputLine(const wxString& line)
  82. {
  83. if ( !line.empty() )
  84. {
  85. m_sizer->Add(OnCreateLine(line));
  86. }
  87. else // empty line, no need to create a control for it
  88. {
  89. if ( !m_hLine )
  90. m_hLine = m_win->GetCharHeight();
  91. m_sizer->Add(5, m_hLine);
  92. }
  93. }
  94. private:
  95. wxWindow *m_win;
  96. wxSizer *m_sizer;
  97. int m_hLine;
  98. };
  99. #endif // wxUSE_STATTEXT
  100. #endif // _WX_TEXTWRAPPER_H_