button.cpp

Go to the documentation of this file.
00001 /*
00002  * button.cpp
00003  *
00004  * Copyright (C) 2008-2009  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 button dialog element.
00032  */
00033 
00034 // includes --------------------------------------------------------------------
00035 #include "element.h"            // always include our own header first
00036 
00037 #include "dialog.h"
00038 
00039 #include "datahash/datahash_util.h"
00040 
00041 
00042 namespace dialog {
00043 
00044 
00045 ////////////////////////////////////////////////////////////////////////////////
00046 //
00047 //      Button -- class that implements Element for button elements
00048 //
00049 ////////////////////////////////////////////////////////////////////////////////
00050 
00051 class Button : public Element {
00052 public:
00053         ~Button(void) throw() { }
00054 
00055         // public class methods ------------------------------------------------
00056         void initialize(IN const Datahash * hash,
00057                                 IN smart_ptr<Drawer>& drawer);
00058 
00059         // dialog::Element class interface methods -----------------------------
00060         int getWidth(void) { return m_width; }
00061         int getHeight(void) { return m_height; }
00062         void draw(IN const point_t& offset);
00063         void cursor(IN const point_t& pos) { }
00064         const char * button(IN int button, IN int state,
00065                         IN const point_t& pos);
00066 
00067 private:
00068         // private helper methods ----------------------------------------------
00069 
00070         // private member data -------------------------------------------------
00071         smart_ptr<Drawer>       m_drawer;
00072         std::string             m_label;
00073         std::string             m_submit;
00074         int                     m_width;
00075         int                     m_height;
00076         int                     m_rise;
00077         int                     m_border;
00078 };
00079 
00080 
00081 
00082 void
00083 Button::initialize
00084 (
00085 IN const Datahash * hash,
00086 IN smart_ptr<Drawer>& drawer
00087 )
00088 {
00089         ASSERT(hash, "null");
00090         ASSERT(drawer, "null");
00091 
00092         m_drawer = drawer;
00093         m_label = getString(hash, "label");
00094         m_submit = getString(hash, "submit");
00095 
00096         font_size_t fs =
00097             m_drawer->getFontSizing(eElement_Button, m_label.c_str());
00098 
00099         m_width = fs.lead + fs.trail + 1;
00100         m_height = fs.drop + fs.rise + 1;
00101         m_rise = fs.rise;
00102 
00103 //      DPRINTF("BUTTON '%s'", m_label.c_str());
00104 //      DPRINTF("  width:%d  lead:%d  trail:%d", m_width, fs.lead, fs.trail);
00105 //      DPRINTF("  height:%d  rise:%d  drop:%d", m_height, fs.rise, fs.drop);
00106 
00107         // account for border
00108         border_size_t bs = m_drawer->getBorderSizing(eElement_Button);
00109         m_border = bs.size;
00110         m_width += 2 * m_border;
00111         m_height += 2 * m_border;
00112 }
00113 
00114 
00115 
00116 void
00117 Button::draw
00118 (
00119 IN const point_t& offset
00120 )
00121 {
00122         ASSERT(m_drawer, "null");
00123 
00124         // given offest, determine rect
00125         rect_t r(offset.x, offset.y, offset.x + m_width, offset.y + m_height);
00126 
00127         // draw background etc
00128         m_drawer->drawRectWithBorder(eElement_Button, r);
00129 
00130         // draw text
00131         point_t pos(offset.x + m_border, offset.y + m_border + m_rise);
00132         m_drawer->drawString(eElement_Button, pos, m_label.c_str());
00133 }
00134 
00135 
00136 
00137 const char *
00138 Button::button
00139 (
00140 IN int button,
00141 IN int state,
00142 IN const point_t& pos
00143 )
00144 {
00145 //      DPRINTF("Button '%s' received button %d state %d",
00146 //          m_label.c_str(), button, state);
00147 
00148         return m_submit.c_str();
00149 }
00150 
00151 
00152 
00153 ////////////////////////////////////////////////////////////////////////////////
00154 //
00155 //      public API
00156 //
00157 ////////////////////////////////////////////////////////////////////////////////
00158 
00159 smart_ptr<Element>
00160 createButtonElement
00161 (
00162 IN Manager * mgr,
00163 IN const Datahash * hash
00164 )
00165 {
00166         ASSERT(mgr, "null");
00167         ASSERT(hash, "null");
00168 
00169         smart_ptr<Button> local = new Button;
00170         ASSERT(local, "out of memory");
00171 
00172         smart_ptr<Drawer> drawer = mgr->getDrawer();
00173         local->initialize(hash, drawer);
00174 
00175         return local;
00176 }
00177 
00178 
00179 
00180 };      // dialog namespace
00181