Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

# text_painter.cpp

    00001 // text_painter.cpp
    00002
    00003 // Copyright (C) 2004 Steven Weiß (steven11@gmx.de)
    00004 //
    00005 // Permission to copy, use, sell and distribute this software is granted
    00006 // provided this copyright notice appears in all copies.
    00007 // Permission to modify the code and to distribute modified code is granted
    00008 // provided this copyright notice appears in all copies, and a notice
    00009 // that the code was modified is included with the copyright notice.
    00010 //
    00011 // This software is provided "as is" without express or implied warranty,
    00012 // and with no claim as to its suitability for any purpose.
    00013
    00014
    00015 #include <background_wnd/painters/text_painter.hpp>
    00016
    00017
    00018 namespace win32 { namespace gui {
    00019
    00027 text_painter::font_info::font_info(LOGFONT lf, unsigned min_font_height, unsigned max_font_height, COLORREF cr)
    00028      : lf_(lf), min_font_height_(min(min_font_height, max_font_height)), max_font_height_(max(min_font_height, max_font_height)), cr_(cr)
    00029 {
    00030      lf_.lfHeight = 0;
    00031 }
    00032
    00033 // ========================================================================================================= //
    00034
    00041 text_painter::text_painter(param0 str, param1 font_info)
    00042      : str_(str), info_(font_info), draw_text_options_(NULL)
    00043 {}
    00044
    00045 text_painter* text_painter::clone() const
    00046 {
    00047      return new text_painter(*this);
    00048 }
    00049
    00050 void text_painter::draw(HDC hDC, int cx, int cy)
    00051 {
    00052      if (cx <= 0 || cy <= 0)
    00053           return;
    00054      RECT rc = { 0, 0, cx, cy };
    00055
    00056      // create font
    00057      info_.lf_.lfHeight = info_.max_font_height_;
    00058      HFONT hFont = CreateFontIndirect(&info_.lf_);
    00059      HGDIOBJ hOldFont = SelectObject(hDC, hFont);
    00060
    00061      // Iterate through font-sizes and find a size where the text fits
    00062      for (; info_.lf_.lfHeight >= (long) info_.min_font_height_; --info_.lf_.lfHeight) {
    00063           if (info_.lf_.lfHeight != info_.max_font_height_) {
    00064                SelectObject(hDC, hOldFont);
    00065                DeleteObject(hFont);
    00066
    00067                hFont = CreateFontIndirect(&info_.lf_);
    00068                SelectObject(hDC, hFont);
    00069           }
    00070
    00071           // because DrawText() modifies the rect we must have a temporary copy
    00072           RECT rc_temp = rc;
    00073           int height = DrawText(hDC, str_.c_str(), (int) str_.length(), &rc_temp, DT_CALCRECT | draw_text_options_);
    00074           if (height <= rc.bottom - rc.top)
    00075                break;
    00076      }
    00077      info_.lf_.lfHeight = max(info_.lf_.lfHeight, info_.min_font_height_);
    00078
    00079      // Draw text and clean up
    00080      int oldBkMode = SetBkMode(hDC, TRANSPARENT);
    00081      COLORREF crOld = SetTextColor(hDC, info_.cr_);
    00082      DrawText(hDC, str_.c_str(), (int) str_.length(), &rc, draw_text_options_);
    00083      SetTextColor(hDC, crOld);
    00084      SetBkMode(hDC, oldBkMode);
    00085
    00086      SelectObject(hDC, hOldFont);
    00087      DeleteObject(hFont);
    00088 }
    00089
    00091 void text_painter::text(const std::string& str)
    00092 {
    00093      str_ = str;
    00094 }
    00095
    00097 std::string text_painter::text() const
    00098 {
    00099      return str_;
    00100 }
    00101
    00103 void text_painter::info(const font_info& info)
    00104 {
    00105      unsigned last_height = info_.lf_.lfHeight;        // save last height
    00106      info_ = info;
    00107      info_.lf_.lfHeight = last_height;
    00108 }
    00109
    00111 text_painter::font_info text_painter::info() const
    00112 {
    00113      return info_;
    00114 }
    00115
    00122 void text_painter::draw_text_options(unsigned flags)
    00123 {
    00124      draw_text_options_ = flags;
    00125 }
    00126
    00129 unsigned text_painter::draw_text_options() const
    00130 {
    00131      return draw_text_options_;
    00132 }
    00133
    00136 unsigned text_painter::last_used_text_height() const
    00137 {
    00138      return info_.lf_.lfHeight;
    00139 }
    00140
    00141
    00142 } }  // namespace win32::gui