00001 /************************************************************************ 00002 filename: CEGUITextItem.cpp 00003 created: 31/3/2005 00004 author: Tomas Lindquist Olsen (based on code by Paul D Turner) 00005 00006 purpose: Implementation of TextEntry widget base class 00007 *************************************************************************/ 00008 /************************************************************************* 00009 Crazy Eddie's GUI System (http://www.cegui.org.uk) 00010 Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk) 00011 00012 This library is free software; you can redistribute it and/or 00013 modify it under the terms of the GNU Lesser General Public 00014 License as published by the Free Software Foundation; either 00015 version 2.1 of the License, or (at your option) any later version. 00016 00017 This library is distributed in the hope that it will be useful, 00018 but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00020 Lesser General Public License for more details. 00021 00022 You should have received a copy of the GNU Lesser General Public 00023 License along with this library; if not, write to the Free Software 00024 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00025 *************************************************************************/ 00026 #include "elements/CEGUITextItem.h" 00027 #include "elements/CEGUIItemListBase.h" 00028 #include "CEGUIFont.h" 00029 00030 // Start of CEGUI namespace section 00031 namespace CEGUI 00032 { 00033 00034 /************************************************************************* 00035 Definition of Properties for this class 00036 *************************************************************************/ 00037 TextItemProperties::TextXOffset TextItem::d_textXOffsetProperty; 00038 TextItemProperties::TextColour TextItem::d_textColourProperty; 00039 TextItemProperties::TextFormatting TextItem::d_textFormattingProperty; 00040 00041 /************************************************************************* 00042 Constants 00043 *************************************************************************/ 00044 const colour TextItem::DefaultTextColour = 0xFFFFFFFF; 00045 00046 /************************************************************************* 00047 Constructor for TextItem base class. 00048 *************************************************************************/ 00049 TextItem::TextItem(const String& type, const String& name) : 00050 ItemEntry(type, name), 00051 d_textColour(DefaultTextColour), 00052 d_textFormatting(LeftAligned), 00053 d_textXOffset(0.0f) 00054 { 00055 addTextItemProperties(); 00056 } 00057 00058 00059 /************************************************************************* 00060 Destructor for TextItem base class. 00061 *************************************************************************/ 00062 TextItem::~TextItem(void) 00063 { 00064 } 00065 00066 00067 /************************************************************************* 00068 Handler called when text is changed. 00069 *************************************************************************/ 00070 void TextItem::onTextChanged(WindowEventArgs& e) 00071 { 00072 Window::onTextChanged(e); 00073 00074 // if we are attached to a ItemListBase, we inform the list of the change 00075 Window* parent = getParent(); 00076 ItemListBase* ilb = (ItemListBase*)parent; 00077 if (parent!=NULL && parent->testClassName("ItemListBase") && ilb->isItemInList(this)) 00078 { 00079 ilb->handleUpdatedItemData(); 00080 } 00081 00082 requestRedraw(); 00083 e.handled = true; 00084 } 00085 00086 00087 /************************************************************************* 00088 Perform the actual rendering for this Window. 00089 *************************************************************************/ 00090 void TextItem::populateRenderCache() 00091 { 00092 Rect absrect(0,0,d_pixelSize.d_width, d_pixelSize.d_height); 00093 00094 ColourRect colours(d_textColour); 00095 colours.modulateAlpha(getEffectiveAlpha()); 00096 00097 // 00098 // Draw label text 00099 // 00100 absrect.d_top += PixelAligned((absrect.getHeight() - getFont()->getLineSpacing()) / 2); 00101 absrect.d_left += PixelAligned(d_textXOffset); 00102 00103 float zBase = System::getSingleton().getRenderer()->getZLayer(2) - System::getSingleton().getRenderer()->getCurrentZ(); 00104 00105 d_renderCache.cacheText(getText(), getFont(), d_textFormatting, absrect, zBase, colours); 00106 } 00107 00108 00109 /************************************************************************* 00110 Get the optimal pixelsize for this TextItem 00111 *************************************************************************/ 00112 Size TextItem::getItemPixelSize() 00113 { 00114 const Font *f = getFont(); 00115 // we abs the x offset to to ensure negative offsets do not shrink the item. 00116 return Size(f->getTextExtent(getText())+(d_textXOffset<0?-d_textXOffset:d_textXOffset), f->getLineSpacing()+1); 00117 } 00118 00119 00120 /************************************************************************* 00121 Add TextItem specific properties 00122 *************************************************************************/ 00123 void TextItem::addTextItemProperties(void) 00124 { 00125 addProperty(&d_textColourProperty); 00126 addProperty(&d_textFormattingProperty); 00127 addProperty(&d_textXOffsetProperty); 00128 } 00129 00130 } // End of CEGUI namespace section