Back to index

nux  3.0.0
StaticTextBox.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #include "Nux.h"
00024 #include "Layout.h"
00025 #include "HLayout.h"
00026 #include "VLayout.h"
00027 #include "Validator.h"
00028 #include "StaticTextBox.h"
00029 
00030 namespace nux
00031 {
00032   NUX_IMPLEMENT_OBJECT_TYPE(StaticTextBox);
00033 
00034   StaticTextBox::StaticTextBox(const char *Caption, NUX_FILE_LINE_DECL)
00035     :   View(NUX_FILE_LINE_PARAM)
00036     ,   m_TextAlignment(eAlignTextLeft)
00037     ,   m_bMinimumWidthMatchText(true)
00038     ,   m_bDrawBackground(false)
00039     ,   m_WriteAlpha(true)
00040   {
00041     m_BackgroundColor = Color(0xFF343434);
00042     m_TextColor = Color(1.0f, 1.0f, 1.0f, 1.0f);
00043     // First, set the default minimum size.
00044     SetMinimumSize(DEFAULT_WIDGET_WIDTH, PRACTICAL_WIDGET_HEIGHT);
00045 
00046     // The width size of the text is computed in SetText and set as the minimum for the widget.
00047     // If the text is null or empty, then the default minimum widtth set above remains.
00048     SetText(Caption);
00049 
00050     SetGeometry(Geometry(0, 0, 3 * DEFAULT_WIDGET_WIDTH, DEFAULT_WIDGET_HEIGHT));
00051 
00052     // This widget does not use a layout!
00053     m_Background = 0;
00054   }
00055 
00056   StaticTextBox::~StaticTextBox()
00057   {
00058     delete m_Background;
00059   }
00060 
00061   void StaticTextBox::Draw(GraphicsEngine &graphics_engine, bool force_draw)
00062   {
00063     Geometry base = GetGeometry();
00064     {
00065       graphics_engine.PushClippingRectangle(base);
00066 
00067       if (m_bDrawBackground)
00068       {
00069         GetPainter().PushDrawLayer(graphics_engine, base, m_Background);
00070         GetPainter().PaintTextLineStatic(graphics_engine, GetFont(), GetGeometry(), m_Text.GetTCharPtr(), m_TextColor, m_WriteAlpha, m_TextAlignment);
00071         GetPainter().PopBackground();
00072       }
00073       else
00074       {
00075         //GetPainter().PaintBackground(graphics_engine, base);
00076         GetPainter().PaintTextLineStatic(graphics_engine, GetFont(), GetGeometry(), m_Text.GetTCharPtr(), m_TextColor, m_WriteAlpha, m_TextAlignment);
00077       }
00078 
00079       graphics_engine.PopClippingRectangle();
00080     }
00081   }
00082 
00083   void StaticTextBox::DrawContent(GraphicsEngine &graphics_engine, bool force_draw)
00084   {
00085 
00086   }
00087 
00088   void StaticTextBox::PostDraw(GraphicsEngine &graphics_engine, bool force_draw)
00089   {
00090 
00091   }
00092 
00093   void StaticTextBox::SetText(const char &Caption)
00094   {
00095     NString s(Caption);
00096     SetText(s);
00097   }
00098 
00099   void StaticTextBox::SetText(const char *Caption)
00100   {
00101     NString s(Caption);
00102     SetText(s);
00103   }
00104 
00105   void StaticTextBox::SetText(const tstring &Caption)
00106   {
00107     NString s(Caption);
00108     SetText(s);
00109   }
00110 
00111   void StaticTextBox::SetText(const NString &Caption)
00112   {
00113     m_Text = Caption;
00114 
00115     if (GetMinWidthMatchText())
00116       AdjustMinWidthToMatchText();
00117 
00118     QueueDraw();
00119   }
00120 
00121   void StaticTextBox::SetMinWidthMatchText(bool b)
00122   {
00123     m_bMinimumWidthMatchText = b;
00124 
00125     if (m_bMinimumWidthMatchText)
00126       AdjustMinWidthToMatchText();
00127   }
00128 
00129   bool StaticTextBox::GetMinWidthMatchText() const
00130   {
00131     return m_bMinimumWidthMatchText;
00132   }
00133 
00134   void StaticTextBox::AdjustMinWidthToMatchText()
00135   {
00136     if (m_Text.Size() == 0)
00137       return;
00138 
00139     SetMinimumWidth(/*4 + */GetFont()->GetStringWidth(m_Text.GetTStringRef()));
00140   }
00141 
00142   void StaticTextBox::SetFont(ObjectPtr<FontTexture> Font)
00143   {
00144     View::SetFont(Font);
00145 
00146     if (GetMinWidthMatchText())
00147       AdjustMinWidthToMatchText();
00148   }
00149 
00150   void StaticTextBox::SetBackground(AbstractPaintLayer *layer)
00151   {
00152     delete m_Background;
00153     m_Background = layer->Clone();
00154   }
00155 
00156   bool StaticTextBox::AcceptKeyNavFocus()
00157   {
00158     return false;
00159   }
00160 }