00001 /* 00002 * label.cpp 00003 * 00004 * Copyright (C) 2008 Thomas A. Vaughan 00005 * All rights reserved. 00006 * 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions are met: 00010 * * Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * * Redistributions in binary form must reproduce the above copyright 00013 * notice, this list of conditions and the following disclaimer in the 00014 * documentation and/or other materials provided with the distribution. 00015 * * Neither the name of the <organization> nor the 00016 * names of its contributors may be used to endorse or promote products 00017 * derived from this software without specific prior written permission. 00018 * 00019 * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY 00020 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00022 * DISCLAIMED. IN NO EVENT SHALL THOMAS A. VAUGHAN BE LIABLE FOR ANY 00023 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00026 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 * 00030 * 00031 * Implementation of a text-only dialog element (static label). 00032 */ 00033 00034 // includes -------------------------------------------------------------------- 00035 #include "element.h" // always include our own header first! 00036 00037 #include "dialog.h" 00038 00039 #include "common/wave_ex.h" 00040 #include "datahash/datahash_util.h" 00041 00042 00043 namespace dialog { 00044 00045 00046 //////////////////////////////////////////////////////////////////////////////// 00047 // 00048 // Label -- class that implements the Element interface for text elements 00049 // 00050 //////////////////////////////////////////////////////////////////////////////// 00051 00052 class Label : public Element { 00053 public: 00054 Label(void) throw(); 00055 ~Label(void) throw() { } 00056 00057 // public class methods ------------------------------------------------ 00058 void initialize(IN const Datahash * hash, 00059 IN smart_ptr<Drawer>& drawer); 00060 00061 // dialog::Element class interface methods ----------------------------- 00062 int getWidth(void) { return m_width; } 00063 int getHeight(void) { return m_height; } 00064 void draw(IN const point_t& offset); 00065 void cursor(IN const point_t& pos) { } 00066 const char * button(IN int button, IN int state, 00067 IN const point_t& pos) { return NULL; } 00068 00069 private: 00070 // private helper methods ---------------------------------------------- 00071 00072 // private member data ------------------------------------------------- 00073 std::string m_value; 00074 int m_width; 00075 int m_height; 00076 int m_rise; 00077 smart_ptr<Drawer> m_drawer; 00078 }; 00079 00080 00081 00082 Label::Label(void) throw() 00083 { 00084 } 00085 00086 00087 00088 void 00089 Label::initialize 00090 ( 00091 IN const Datahash * hash, 00092 IN smart_ptr<Drawer>& drawer 00093 ) 00094 { 00095 ASSERT(hash, "null"); 00096 ASSERT(drawer, "null"); 00097 00098 m_drawer = drawer; 00099 m_value = getString(hash, "value"); 00100 00101 const char * testString = m_value.c_str(); 00102 if (!*testString) { 00103 // empty label? Use "Mfg" (height, drop) 00104 testString = "Mfg"; 00105 } 00106 font_size_t fs = m_drawer->getFontSizing(eElement_Label, testString); 00107 00108 m_width = fs.lead + fs.trail + 1; 00109 m_height = fs.drop + fs.rise + 1; 00110 m_rise = fs.rise; 00111 00112 // DPRINTF("LABEL '%s'", m_value.c_str()); 00113 // DPRINTF(" width:%d lead:%d trail:%d", m_width, fs.lead, fs.trail); 00114 // DPRINTF(" height: %d rise:%d drop:%d", m_height, fs.rise, fs.drop); 00115 } 00116 00117 00118 00119 void 00120 Label::draw 00121 ( 00122 IN const point_t& offset 00123 ) 00124 { 00125 ASSERT(m_drawer, "null"); 00126 00127 // account for font drop 00128 point_t off2 = offset; 00129 off2.y += m_rise; 00130 00131 m_drawer->drawString(eElement_Label, off2, m_value.c_str()); 00132 } 00133 00134 00135 00136 //////////////////////////////////////////////////////////////////////////////// 00137 // 00138 // public API 00139 // 00140 //////////////////////////////////////////////////////////////////////////////// 00141 00142 smart_ptr<Element> 00143 createLabelElement 00144 ( 00145 IN Manager * mgr, 00146 IN const Datahash * hash 00147 ) 00148 { 00149 ASSERT(mgr, "null"); 00150 ASSERT(hash, "null"); 00151 00152 smart_ptr<Label> local = new Label; 00153 ASSERT(local, "out of memory"); 00154 00155 smart_ptr<Drawer> drawer = mgr->getDrawer(); 00156 local->initialize(hash, drawer); 00157 00158 return local; 00159 } 00160 00161 00162 00163 }; // dialog namespace 00164