wxruler.h

00001 /*************************************************************************************
00002  * MechSys - A C++ library to simulate (Continuum) Mechanical Systems                *
00003  * Copyright (C) 2005 Dorival de Moraes Pedroso <dorival.pedroso at gmail.com>       *
00004  * Copyright (C) 2005 Raul Dario Durand Farfan  <raul.durand at gmail.com>           *
00005  *                                                                                   *
00006  * This file is part of MechSys.                                                     *
00007  *                                                                                   *
00008  * MechSys is free software; you can redistribute it and/or modify it under the      *
00009  * terms of the GNU General Public License as published by the Free Software         *
00010  * Foundation; either version 2 of the License, or (at your option) any later        *
00011  * version.                                                                          *
00012  *                                                                                   *
00013  * MechSys is distributed in the hope that it will be useful, but WITHOUT ANY        *
00014  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A   *
00015  * PARTICULAR PURPOSE. See the GNU General Public License for more details.          *
00016  *                                                                                   *
00017  * You should have received a copy of the GNU General Public License along with      *
00018  * MechSys; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, *
00019  * Fifth Floor, Boston, MA 02110-1301, USA                                           *
00020  *************************************************************************************/
00021 
00022 #ifndef MECHSYS_WXRULER_H
00023 #define MECHSYS_WXRULER_H
00024 
00025 #ifdef HAVE_CONFIG_H
00026   #include "config.h"
00027 #else
00028   #ifndef REAL
00029     #define REAL double
00030   #endif
00031 #endif
00032 
00033 #include "util/array.h"
00034 
00035 #include "gui/wxtransform.h"
00036 
00037 class WxRuler
00038 {
00039 public:
00040     // RulerPosition
00041     enum RulerPosition
00042     { // {{{
00043         POS_BOTTOM ,
00044         POS_LEFT   ,
00045         POS_TOP    ,
00046         POS_RIGHT
00047     }; // enum RulerPosition // }}}
00048 
00049     // Constructor & Destructor
00050      WxRuler ();
00051     ~WxRuler ();
00052 
00053     // Config Methods
00054     WxRuler & Active       (bool          Val) { _active=Val; return (*this); }
00055     WxRuler & Position     (RulerPosition Pos);
00056     WxRuler & Thickness    (int           Wid);
00057     WxRuler & nMajorTicks  (int           Num);
00058     WxRuler & MajorTickLen (int           Len);
00059 
00060     // Methods
00061     void UpdateSize (WxTransformData const & TD);
00062     void Draw       (wxDC & DC);
00063     bool IsActive   () const { return _active;    }
00064     int  Thick      () const { return _thickness; }
00065 
00066 private:
00067     bool           _active;
00068     RulerPosition  _position;
00069     int            _thickness; // should be "width", however we use this to avoid confusion with vertical and horizontal "width's"
00070     int            _n_major_ticks;
00071     int            _major_tick_len;
00072     Array<wxPoint> _major_start_points;
00073     Array<wxPoint> _major_end_points;
00074     wxPoint        _base_line_start;
00075     wxPoint        _base_line_end;
00076 }; // class WxScale
00077 
00078 
00080 
00081 
00082 inline WxRuler::WxRuler() // {{{
00083     : _active         (true)      ,
00084       _position       (POS_BOTTOM),
00085       _thickness      (15)        ,
00086       _major_tick_len (10)        
00087 {
00088     nMajorTicks(10);
00089 } // }}}
00090 
00091 inline WxRuler::~WxRuler() // {{{
00092 {
00093 } // }}}
00094 
00095 inline WxRuler & WxRuler::Position(WxRuler::RulerPosition Pos) // {{{
00096 {
00097     _position = Pos;
00098     return (*this);
00099 } // }}}
00100 
00101 inline WxRuler & WxRuler::Thickness(int Wid) // {{{
00102 {
00103     _thickness = Wid;
00104     return (*this);
00105 } // }}}
00106 
00107 inline WxRuler & WxRuler::nMajorTicks(int Num) // {{{
00108 {
00109     _n_major_ticks = Num;
00110     _major_start_points.resize(_n_major_ticks+1);
00111     _major_end_points.  resize(_n_major_ticks+1);
00112     return (*this);
00113 } // }}}
00114 
00115 inline WxRuler & WxRuler::MajorTickLen(int Len) // {{{
00116 {
00117     _major_tick_len = Len;
00118     return (*this);
00119 } // }}}
00120 
00121 inline void WxRuler::UpdateSize(WxTransformData const & TD) // {{{
00122 {
00123     // Base line
00124     switch (_position)
00125     {
00126     case POS_BOTTOM:
00127         {
00128             // Base line
00129             _base_line_start.x = TD.CanvasRect.x;
00130             _base_line_start.y = TD.CanvasRect.y + TD.CanvasRect.height;
00131               _base_line_end.x = _base_line_start.x + TD.CanvasRect.width;
00132               _base_line_end.y = _base_line_start.y;
00133 
00134             // Major ticks
00135             int major_step = TD.CanvasRect.width/_n_major_ticks;
00136             _major_start_points[0].x = _base_line_start.x;
00137             _major_start_points[0].y = _base_line_start.y;
00138               _major_end_points[0].x = _major_start_points[0].x;
00139               _major_end_points[0].y = _major_start_points[0].y + _major_tick_len;
00140             for (int i=1; i<_n_major_ticks+1; ++i)
00141             {
00142                 _major_start_points[i].x = _major_start_points[i-1].x + major_step;
00143                 _major_start_points[i].y = _base_line_start.y;
00144                   _major_end_points[i].x = _major_start_points[i].x;
00145                   _major_end_points[i].y = _major_start_points[i].y + _major_tick_len;
00146             }
00147 
00148             break;
00149         }
00150     case POS_LEFT:
00151         {
00152             // Base line
00153             _base_line_start.x = TD.CanvasRect.x;
00154             _base_line_start.y = TD.CanvasRect.y;
00155               _base_line_end.x = _base_line_start.x;
00156               _base_line_end.y = _base_line_start.y + TD.CanvasRect.height;
00157 
00158             // Major ticks
00159             int major_step = TD.CanvasRect.height/_n_major_ticks;
00160             _major_start_points[0].x = _base_line_start.x - _major_tick_len;
00161             _major_start_points[0].y = _base_line_start.y;
00162               _major_end_points[0].x = _base_line_start.x;
00163               _major_end_points[0].y = _major_start_points[0].y;
00164             for (int i=1; i<_n_major_ticks+1; ++i)
00165             {
00166                 _major_start_points[i].x = _base_line_start.x - _major_tick_len;
00167                 _major_start_points[i].y = _major_start_points[i-1].y + major_step;
00168                   _major_end_points[i].x = _base_line_start.x;
00169                   _major_end_points[i].y = _major_start_points[i].y;
00170             }
00171 
00172             break;
00173         }
00174     case POS_TOP:
00175         {
00176             // Base line
00177             _base_line_start.x = TD.CanvasRect.x;
00178             _base_line_start.y = TD.CanvasRect.y;
00179               _base_line_end.x = _base_line_start.x + TD.CanvasRect.width;
00180               _base_line_end.y = _base_line_start.y;
00181 
00182             // Major ticks
00183             int major_step = TD.CanvasRect.width/_n_major_ticks;
00184             _major_start_points[0].x = _base_line_start.x;
00185             _major_start_points[0].y = _base_line_start.y;
00186               _major_end_points[0].x = _major_start_points[0].x;
00187               _major_end_points[0].y = _major_start_points[0].y - _major_tick_len;
00188             for (int i=1; i<_n_major_ticks+1; ++i)
00189             {
00190                 _major_start_points[i].x = _major_start_points[i-1].x + major_step;
00191                 _major_start_points[i].y = _base_line_start.y;
00192                   _major_end_points[i].x = _major_start_points[i].x;
00193                   _major_end_points[i].y = _major_start_points[i].y - _major_tick_len;
00194             }
00195 
00196             break;
00197         }
00198     case POS_RIGHT:
00199         {
00200             // Base line
00201             _base_line_start.x = TD.CanvasRect.x + TD.CanvasRect.width;
00202             _base_line_start.y = TD.CanvasRect.y;
00203               _base_line_end.x = _base_line_start.x;
00204               _base_line_end.y = _base_line_start.y + TD.CanvasRect.height;
00205 
00206             // Major ticks
00207             int major_step = TD.CanvasRect.height/_n_major_ticks;
00208             _major_start_points[0].x = _base_line_start.x;
00209             _major_start_points[0].y = _base_line_start.y;
00210               _major_end_points[0].x = _major_start_points[0].x + _major_tick_len;
00211               _major_end_points[0].y = _major_start_points[0].y;
00212             for (int i=1; i<_n_major_ticks+1; ++i)
00213             {
00214                 _major_start_points[i].x = _base_line_start.x;
00215                 _major_start_points[i].y = _major_start_points[i-1].y + major_step;
00216                   _major_end_points[i].x = _major_start_points[i].x + _major_tick_len;
00217                   _major_end_points[i].y = _major_start_points[i].y;
00218             }
00219 
00220             break;
00221         }
00222     }
00223 } // }}}
00224 
00225 inline void WxRuler::Draw(wxDC & DC) // {{{
00226 {
00227     if (_active)
00228     {
00229         // Major ticks
00230         DC.SetPen(wxPen(_T("black")));
00231         for (int i=0; i<_n_major_ticks+1; ++i)
00232             DC.DrawLine(_major_start_points[i], _major_end_points[i]);
00233 
00234         // Baseline
00235         DC.DrawLine(_base_line_start, _base_line_end);
00236     }
00237 } // }}}
00238 
00239 #endif // MECHSYS_WXRULER_H
00240 
00241 // vim:fdm=marker

Generated on Wed Jan 24 15:56:26 2007 for MechSys by  doxygen 1.4.7