wxiconbox.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_WXICONBOX_H
00023 #define MECHSYS_WXICONBOX_H
00024 
00025 #include <wx/vlbox.h>
00026 #include <wx/icon.h>
00027 #include <wx/imaglist.h>
00028 
00029 class WxIconBox : public wxVListBox
00030 {
00031 public:
00032     WxIconBox(wxWindow * parent, wxWindowID id=wxID_ANY, const wxPoint & pos=wxDefaultPosition, int Width=-1, int nLines=1);
00033     /*
00034       The derived class must implement this function to actually draw the item with the given index on the provided DC.
00035       dc:   The device context to use for drawing
00036       rect: The bounding rectangle for the item being drawn (DC clipping region is set to this rectangle before calling this function)
00037       n:    The index of the item to be drawn
00038     */
00039     void    OnDrawItem    (wxDC & dc, const wxRect & rect, size_t n) const;
00040     wxCoord OnMeasureItem (size_t n) const; // The derived class must implement this method to return the height of the specified item (in pixels).
00041     // Methods
00042     void Add(wxIcon const & anIcon);
00043     void Add(wxIcon const & anIcon, wxString const & Text);
00044 private:
00045     // Data
00046     int                   _width;
00047     int                   _n_lines;
00048     mutable wxImageList   _icons;
00049     mutable wxArrayString _text;
00050     // Methods
00051     void _draw_button(wxDC & dc, wxRect const & BtnRect) const;
00052 }; // class WxIconBox
00053 
00054 
00056 
00057 inline WxIconBox::WxIconBox(wxWindow * parent, wxWindowID id, const wxPoint & pos, int Width, int nLines) // {{{
00058     : wxVListBox (parent, id, pos, wxDefaultSize, wxSUNKEN_BORDER),
00059       _width     (Width),
00060       _n_lines   (nLines)
00061 {
00062 } // }}}
00063 
00064 inline void WxIconBox::OnDrawItem (wxDC & dc, const wxRect & rect, size_t n) const // {{{
00065 {
00066     int x=rect.GetX();
00067     int y=rect.GetY();
00068     int w,h;
00069     _icons.GetSize(/*index (not used yet)*/0, w, h);
00070     _icons.Draw(n,dc,/*x*/x,/*y*/y);
00071     if (_text.size()>0)
00072     {
00073         //wxFont font(12,wxFONTFAMILY_SWISS, wxNORMAL, wxBOLD);
00074         wxFont font(12,wxFONTFAMILY_SWISS, wxNORMAL, wxNORMAL);
00075         dc.SetFont(font);
00076         dc.SetBackgroundMode(wxTRANSPARENT);
00077         dc.SetTextForeground(*wxBLACK);
00078         dc.SetTextBackground(*wxWHITE);
00079         wxCoord wtxt,htxt;
00080         GetTextExtent(_text[n], &wtxt, &htxt);
00081         dc.DrawText(_text[n],wxPoint(x+w,y+(h-htxt)/2));
00082     }
00083     /*
00084     wxRect btn_rect(_width-h,0,h,h);
00085     _draw_button(dc,btn_rect);
00086     */
00087 } // }}}
00088 
00089 inline wxCoord WxIconBox::OnMeasureItem (size_t n) const // {{{
00090 {
00091     int w,h;
00092     _icons.GetSize(/*index (not used yet)*/0, w, h);
00093     return h;
00094 } // }}}
00095 
00096 inline void WxIconBox::Add(wxIcon const & anIcon) // {{{
00097 {
00098     _icons.Add(anIcon); 
00099     SetItemCount(GetItemCount()+1);
00100     if (_width==-1) _width=anIcon.GetWidth();
00101     SetClientSize(_width,anIcon.GetHeight()*_n_lines);
00102 } // }}}
00103 
00104 inline void WxIconBox::Add(wxIcon const & anIcon, wxString const & Text) // {{{
00105 {
00106     _icons.Add(anIcon); 
00107     _text.Add(Text);
00108     SetItemCount(GetItemCount()+1);
00109     if (_width==-1) _width=wxDefaultSize.GetWidth();
00110     SetClientSize(_width,anIcon.GetHeight()*_n_lines);
00111 } // }}}
00112 
00113 inline void WxIconBox::_draw_button(wxDC & dc, wxRect const & BtnRect) const // {{{
00114 {
00115     // Draw button
00116     wxColour bg_clr = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
00117     wxColour fg_clr = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT);
00118     wxColour sh_clr = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW);
00119     wxColour hi_clr = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNHIGHLIGHT);
00120     dc.SetBrush(wxBrush(bg_clr, wxSOLID));
00121     dc.SetPen(wxPen(bg_clr, 1, wxSOLID));
00122     dc.DrawRectangle(BtnRect);
00123     dc.SetPen(wxPen(fg_clr, 1, wxSOLID));
00124     dc.DrawLine(BtnRect.GetLeft() , BtnRect.GetBottom(), BtnRect.GetRight(), BtnRect.GetBottom());
00125     dc.DrawLine(BtnRect.GetRight(), BtnRect.GetBottom(), BtnRect.GetRight(), BtnRect.GetTop()-1);
00126     dc.SetPen(wxPen(sh_clr, 1, wxSOLID));
00127     dc.DrawLine(BtnRect.GetLeft()+1 , BtnRect.GetBottom()-1, BtnRect.GetRight()-1, BtnRect.GetBottom()-1);
00128     dc.DrawLine(BtnRect.GetRight()-1, BtnRect.GetBottom()-1, BtnRect.GetRight()-1, BtnRect.GetTop());
00129     dc.SetPen(wxPen(hi_clr, 1, wxSOLID));
00130     dc.DrawLine(BtnRect.GetRight()-2, BtnRect.GetTop()+1, BtnRect.GetLeft()+1, BtnRect.GetTop()+1);
00131     dc.DrawLine(BtnRect.GetLeft()+1 , BtnRect.GetTop()+1, BtnRect.GetLeft()+1, BtnRect.GetBottom()-1);
00132     // Draw little triangle
00133     int W = BtnRect.GetWidth();
00134     int H = BtnRect.GetHeight();
00135     int w = H/3;
00136     int h = H/6;
00137     wxPoint point[3];
00138     point[0] = wxPoint(BtnRect.GetX()+(W-w)/2,BtnRect.GetY()+(H-h)/2);
00139     point[1] = wxPoint(point[0].x+w,point[0].y);
00140     point[2] = wxPoint(point[0].x+w/2,point[0].y+h);
00141     dc.SetBrush(wxBrush(fg_clr, wxSOLID));
00142     dc.SetPen(wxPen(fg_clr, 1, wxSOLID));
00143     dc.DrawPolygon(3, point);
00144 } // }}}
00145 
00146 #endif // MECHSYS_WXICONBOX_H
00147 
00148 // vim:fdm=marker

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