textmeasure.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/private/textmeasure.h
  3. // Purpose: declaration of wxTextMeasure class
  4. // Author: Manuel Martin
  5. // Created: 2012-10-05
  6. // Copyright: (c) 1997-2012 wxWidgets team
  7. // Licence: wxWindows licence
  8. ///////////////////////////////////////////////////////////////////////////////
  9. #ifndef _WX_PRIVATE_TEXTMEASURE_H_
  10. #define _WX_PRIVATE_TEXTMEASURE_H_
  11. class WXDLLIMPEXP_FWD_CORE wxDC;
  12. class WXDLLIMPEXP_FWD_CORE wxFont;
  13. class WXDLLIMPEXP_FWD_CORE wxWindow;
  14. // ----------------------------------------------------------------------------
  15. // wxTextMeasure: class used to measure text extent.
  16. // ----------------------------------------------------------------------------
  17. class wxTextMeasureBase
  18. {
  19. public:
  20. // The first ctor argument must be non-NULL, i.e. each object of this class
  21. // is associated with either a valid wxDC or a valid wxWindow. The font can
  22. // be NULL to use the current DC/window font or can be specified explicitly.
  23. wxTextMeasureBase(const wxDC *dc, const wxFont *theFont);
  24. wxTextMeasureBase(const wxWindow *win, const wxFont *theFont);
  25. // Even though this class is not supposed to be used polymorphically, give
  26. // it a virtual dtor to avoid compiler warnings.
  27. virtual ~wxTextMeasureBase() { }
  28. // Return the extent of a single line string.
  29. void GetTextExtent(const wxString& string,
  30. wxCoord *width,
  31. wxCoord *height,
  32. wxCoord *descent = NULL,
  33. wxCoord *externalLeading = NULL);
  34. // The same for a multiline (with '\n') string.
  35. void GetMultiLineTextExtent(const wxString& text,
  36. wxCoord *width,
  37. wxCoord *height,
  38. wxCoord *heightOneLine = NULL);
  39. // Find the dimensions of the largest string.
  40. wxSize GetLargestStringExtent(size_t n, const wxString* strings);
  41. wxSize GetLargestStringExtent(const wxArrayString& strings)
  42. {
  43. return GetLargestStringExtent(strings.size(), &strings[0]);
  44. }
  45. // Fill the array with the widths for each "0..N" substrings for N from 1
  46. // to text.length().
  47. //
  48. // The scaleX argument is the horizontal scale used by wxDC and is only
  49. // used in the generic implementation.
  50. bool GetPartialTextExtents(const wxString& text,
  51. wxArrayInt& widths,
  52. double scaleX);
  53. // These functions are called by our public methods before and after each
  54. // call to DoGetTextExtent(). Derived classes may override them to prepare
  55. // for -- possibly several -- subsequent calls to DoGetTextExtent().
  56. //
  57. // As these calls must be always paired, they're never called directly but
  58. // only by our friend MeasuringGuard class.
  59. //
  60. // NB: They're public only to allow VC6 to compile this code, there doesn't
  61. // seem to be any way to give MeasuringGuard access to them (FIXME-VC6)
  62. virtual void BeginMeasuring() { }
  63. virtual void EndMeasuring() { }
  64. // This is another method which is only used by MeasuringGuard.
  65. bool IsUsingDCImpl() const { return m_useDCImpl; }
  66. protected:
  67. // RAII wrapper for the two methods above.
  68. class MeasuringGuard
  69. {
  70. public:
  71. MeasuringGuard(wxTextMeasureBase& tm) : m_tm(tm)
  72. {
  73. // BeginMeasuring() should only be called if we have a native DC,
  74. // so don't call it if we delegate to a DC of unknown type.
  75. if ( !m_tm.IsUsingDCImpl() )
  76. m_tm.BeginMeasuring();
  77. }
  78. ~MeasuringGuard()
  79. {
  80. if ( !m_tm.IsUsingDCImpl() )
  81. m_tm.EndMeasuring();
  82. }
  83. private:
  84. wxTextMeasureBase& m_tm;
  85. };
  86. // The main function of this class, to be implemented in platform-specific
  87. // way used by all our public methods.
  88. //
  89. // The width and height pointers here are never NULL and the input string
  90. // is not empty.
  91. virtual void DoGetTextExtent(const wxString& string,
  92. wxCoord *width,
  93. wxCoord *height,
  94. wxCoord *descent = NULL,
  95. wxCoord *externalLeading = NULL) = 0;
  96. // The real implementation of GetPartialTextExtents().
  97. //
  98. // On input, widths array contains text.length() zero elements and the text
  99. // is guaranteed to be non-empty.
  100. virtual bool DoGetPartialTextExtents(const wxString& text,
  101. wxArrayInt& widths,
  102. double scaleX) = 0;
  103. // Call either DoGetTextExtent() or wxDC::GetTextExtent() depending on the
  104. // value of m_useDCImpl.
  105. //
  106. // This must be always used instead of calling DoGetTextExtent() directly!
  107. void CallGetTextExtent(const wxString& string,
  108. wxCoord *width,
  109. wxCoord *height,
  110. wxCoord *descent = NULL,
  111. wxCoord *externalLeading = NULL);
  112. // Return a valid font: if one was given to us in the ctor, use this one,
  113. // otherwise use the current font of the associated wxDC or wxWindow.
  114. wxFont GetFont() const;
  115. // Exactly one of m_dc and m_win is non-NULL for any given object of this
  116. // class.
  117. const wxDC* const m_dc;
  118. const wxWindow* const m_win;
  119. // If this is true, simply forward to wxDC::GetTextExtent() from our
  120. // CallGetTextExtent() instead of calling our own DoGetTextExtent().
  121. //
  122. // We need this because our DoGetTextExtent() typically only works with
  123. // native DCs, i.e. those having an HDC under Windows or using Pango under
  124. // GTK+. However wxTextMeasure object can be constructed for any wxDC, not
  125. // necessarily a native one and in this case we must call back into the DC
  126. // implementation of text measuring itself.
  127. bool m_useDCImpl;
  128. // This one can be NULL or not.
  129. const wxFont* const m_font;
  130. wxDECLARE_NO_COPY_CLASS(wxTextMeasureBase);
  131. };
  132. // Include the platform dependent class declaration, if any.
  133. #if defined(__WXGTK20__)
  134. #include "wx/gtk/private/textmeasure.h"
  135. #elif defined(__WXMSW__)
  136. #include "wx/msw/private/textmeasure.h"
  137. #else // no platform-specific implementation of wxTextMeasure yet
  138. #include "wx/generic/private/textmeasure.h"
  139. #define wxUSE_GENERIC_TEXTMEASURE 1
  140. #endif
  141. #ifndef wxUSE_GENERIC_TEXTMEASURE
  142. #define wxUSE_GENERIC_TEXTMEASURE 0
  143. #endif
  144. #endif // _WX_PRIVATE_TEXTMEASURE_H_