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_border;
00077 };
00078 
00079 
00080 
00081 void
00082 Button::initialize
00083 (
00084 IN const Datahash * hash,
00085 IN smart_ptr<Drawer>& drawer
00086 )
00087 {
00088         ASSERT(hash, "null");
00089         ASSERT(drawer, "null");
00090 
00091         m_drawer = drawer;
00092         m_label = getString(hash, "label");
00093         m_submit = getString(hash, "submit");
00094 
00095         font_size_t fs =
00096             m_drawer->getFontSizing(eElement_Button, m_label.c_str());
00097 
00098         m_width = fs.width;
00099         m_height = fs.height;
00100 
00101         // account for border
00102         border_size_t bs = m_drawer->getBorderSizing(eElement_Button);
00103         m_border = bs.size;
00104         m_width += 2 * m_border;
00105         m_height += 2 * m_border;
00106 }
00107 
00108 
00109 
00110 void
00111 Button::draw
00112 (
00113 IN const point_t& offset
00114 )
00115 {
00116         ASSERT(m_drawer, "null");
00117 
00118         // given offest, determine rect
00119         rect_t r(offset.x, offset.y, offset.x + m_width, offset.y + m_height);
00120 
00121         // draw background etc
00122         m_drawer->drawRectWithBorder(eElement_Button, r);
00123 
00124         // draw text
00125         point_t pos(offset.x + m_border, offset.y + m_border);
00126         m_drawer->drawString(eElement_Button, pos, m_label.c_str());
00127 }
00128 
00129 
00130 
00131 const char *
00132 Button::button
00133 (
00134 IN int button,
00135 IN int state,
00136 IN const point_t& pos
00137 )
00138 {
00139 //      DPRINTF("Button '%s' received button %d state %d",
00140 //          m_label.c_str(), button, state);
00141 
00142         return m_submit.c_str();
00143 }
00144 
00145 
00146 
00147 ////////////////////////////////////////////////////////////////////////////////
00148 //
00149 //      public API
00150 //
00151 ////////////////////////////////////////////////////////////////////////////////
00152 
00153 smart_ptr<Element>
00154 createButtonElement
00155 (
00156 IN Manager * mgr,
00157 IN const Datahash * hash
00158 )
00159 {
00160         ASSERT(mgr, "null");
00161         ASSERT(hash, "null");
00162 
00163         smart_ptr<Button> local = new Button;
00164         ASSERT(local, "out of memory");
00165 
00166         smart_ptr<Drawer> drawer = mgr->getDrawer();
00167         local->initialize(hash, drawer);
00168 
00169         return local;
00170 }
00171 
00172 
00173 
00174 };      // dialog namespace
00175