ListBox.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 /* GG is a GUI for SDL and OpenGL.
00003    Copyright (C) 2003 T. Zachary Laine
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public License
00007    as published by the Free Software Foundation; either version 2.1
00008    of the License, or (at your option) any later version.
00009    
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014     
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with this library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA
00019 
00020    If you do not wish to comply with the terms of the LGPL please
00021    contact the author as other terms are available for a fee.
00022     
00023    Zach Laine
00024    whatwasthataddress@hotmail.com */
00025 
00029 #ifndef _GG_ListBox_h_
00030 #define _GG_ListBox_h_
00031 
00032 #include <GG/AlignmentFlags.h>
00033 #include <GG/Control.h>
00034 #include <GG/Timer.h>
00035 
00036 #include <set>
00037 
00038 #include <boost/serialization/version.hpp>
00039 
00040 
00041 namespace GG {
00042 
00043 class Font;
00044 class Scroll;
00045 class SubTexture;
00046 class WndEvent;
00047 
00049 GG_FLAG_TYPE(ListBoxStyle);
00050 extern GG_API const ListBoxStyle LIST_NONE;           
00051 extern GG_API const ListBoxStyle LIST_VCENTER;        
00052 extern GG_API const ListBoxStyle LIST_TOP;            
00053 extern GG_API const ListBoxStyle LIST_BOTTOM;         
00054 extern GG_API const ListBoxStyle LIST_CENTER;         
00055 extern GG_API const ListBoxStyle LIST_LEFT;           
00056 extern GG_API const ListBoxStyle LIST_RIGHT;          
00057 extern GG_API const ListBoxStyle LIST_NOSORT;         
00058 extern GG_API const ListBoxStyle LIST_SORTDESCENDING; 
00059 extern GG_API const ListBoxStyle LIST_NOSEL;          
00060 extern GG_API const ListBoxStyle LIST_SINGLESEL;      
00061 extern GG_API const ListBoxStyle LIST_QUICKSEL;       
00062 extern GG_API const ListBoxStyle LIST_USERDELETE;     
00063 extern GG_API const ListBoxStyle LIST_BROWSEUPDATES;  
00064 
00065 
00093 class GG_API ListBox : public Control
00094 {
00095 public:
00110     struct GG_API Row : public Control
00111     {
00113         typedef std::string SortKeyType;
00114  
00116         Row(); 
00117         Row(int w, int h, const std::string& drag_drop_data_type, Alignment align = ALIGN_VCENTER, int margin = 2); 
00118         virtual ~Row();
00120  
00122         SortKeyType SortKey(int column) const;  
00123         size_t      size() const;               
00124         bool        empty() const;              
00125 
00126         Control*    operator[](size_t n) const; 
00127         Control*    at(size_t n) const;         
00128 
00129         Alignment   RowAlignment() const;       
00130         Alignment   ColAlignment(int n) const;  
00131         int         ColWidth(int n) const;      
00132         int         Margin() const;             
00133 
00134         Control*    CreateControl(const std::string& str, const boost::shared_ptr<Font>& font, Clr color) const; 
00135         Control*    CreateControl(const SubTexture& st) const; 
00136 
00137  
00139         virtual void Render();
00140 
00141         void        push_back(Control* c); 
00142         void        push_back(const std::string& str, const boost::shared_ptr<Font>& font, Clr color = CLR_BLACK); 
00143         void        push_back(const std::string& str, const std::string& font_filename, int pts, Clr color = CLR_BLACK); 
00144         void        push_back(const SubTexture& st); 
00145         void        clear(); 
00146         void        resize(size_t n); 
00147 
00148         void        SetCell(int n, Control* c); 
00149         Control*    RemoveCell(int n); 
00150         void        SetRowAlignment(Alignment align); 
00151         void        SetColAlignment(int n, Alignment align); 
00152         void        SetColWidth(int n, int width); 
00153         void        SetColAlignments(const std::vector<Alignment>& aligns); 
00154         void        SetColWidths(const std::vector<int>& widths); 
00155         void        SetMargin(int margin); 
00156 
00157 
00158     private:
00159         void AdjustLayout(bool adjust_for_push_back = false);
00160 
00161         std::vector<Control*>  m_cells;          
00162         Alignment              m_row_alignment;  
00163         std::vector<Alignment> m_col_alignments; 
00164         std::vector<int>       m_col_widths;     
00165         int                    m_margin;         
00166 
00167         friend class boost::serialization::access;
00168         template <class Archive>
00169         void serialize(Archive& ar, const unsigned int version);
00170     };
00171  
00173     typedef boost::signal<void ()>             ClearedSignalType;        
00174     typedef boost::signal<void (const std::set<int>&)>
00175                                                SelChangedSignalType;     
00176     typedef boost::signal<void (int, ListBox::Row*)>
00177                                                RowSignalType;            
00178     typedef boost::signal<void (int, ListBox::Row*, const Pt&)>
00179                                                RowClickSignalType;       
00180     typedef RowSignalType                      InsertedSignalType;       
00181     typedef RowSignalType                      DroppedSignalType;        
00182     typedef RowClickSignalType                 LeftClickedSignalType;    
00183     typedef RowClickSignalType                 RightClickedSignalType;   
00184     typedef RowSignalType                      DoubleClickedSignalType;  
00185     typedef RowSignalType                      ErasedSignalType;         
00186     typedef boost::signal<void (int)>          BrowsedSignalType;        
00187 
00188  
00190     typedef ClearedSignalType::slot_type       ClearedSlotType;      
00191     typedef SelChangedSignalType::slot_type    SelChangedSlotType;   
00192     typedef InsertedSignalType::slot_type      InsertedSlotType;     
00193     typedef LeftClickedSignalType::slot_type   LeftClickedSlotType;  
00194     typedef RightClickedSignalType::slot_type  RightClickedSlotType; 
00195     typedef DoubleClickedSignalType::slot_type DoubleClickedSlotType;
00196     typedef ErasedSignalType::slot_type        ErasedSlotType;       
00197     typedef DroppedSignalType::slot_type       DroppedSlotType;      
00198     typedef BrowsedSignalType::slot_type       BrowsedSlotType;      
00199 
00200  
00202 
00203     ListBox(int x, int y, int w, int h, Clr color, Clr interior = CLR_ZERO, Flags<WndFlag> flags = CLICKABLE);
00204 
00205     virtual ~ListBox(); 
00206 
00207  
00209     virtual Pt      MinUsableSize() const;
00210     virtual Pt      ClientUpperLeft() const;
00211     virtual Pt      ClientLowerRight() const;
00212 
00213     bool            Empty() const;          
00214     const Row&      GetRow(int n) const;    
00215     int             Caret() const;          
00216     const std::set<int>&
00217                     Selections() const;     
00218     bool            Selected(int n) const;  
00219     Clr             InteriorColor() const;  
00220     Clr             HiliteColor() const;    
00221 
00223     Flags<ListBoxStyle> Style() const;
00224 
00225     const Row&      ColHeaders() const;     
00226     int             FirstRowShown() const;  
00227     int             FirstColShown() const;  
00228 
00229     int             LastVisibleRow() const; 
00230     int             LastVisibleCol() const; 
00231 
00232     int             NumRows() const;        
00233     int             NumCols() const;        
00234 
00236     bool            KeepColWidths() const;
00237 
00240     int             SortCol() const;
00241 
00242     int             ColWidth(int n) const;     
00243     Alignment       ColAlignment(int n) const; 
00244     Alignment       RowAlignment(int n) const; 
00245 
00248     const std::set<std::string>& AllowedDropTypes() const;
00249 
00252     bool            AutoScrollDuringDragDrops() const;
00253 
00256     int             AutoScrollMargin() const;
00257 
00259     int             AutoScrollInterval() const;
00260 
00261     mutable ClearedSignalType       ClearedSignal;       
00262     mutable SelChangedSignalType    SelChangedSignal;    
00263     mutable InsertedSignalType      InsertedSignal;      
00264     mutable DroppedSignalType       DroppedSignal;       
00265     mutable LeftClickedSignalType   LeftClickedSignal;   
00266     mutable RightClickedSignalType  RightClickedSignal;  
00267     mutable DoubleClickedSignalType DoubleClickedSignal; 
00268     mutable ErasedSignalType        ErasedSignal;        
00269     mutable BrowsedSignalType       BrowsedSignal;       
00270 
00271  
00273     virtual void   StartingChildDragDrop(const Wnd* wnd, const GG::Pt& offset);
00274     virtual void   AcceptDrops(std::list<Wnd*>& wnds, const Pt& pt);
00275     virtual void   ChildrenDraggedAway(const std::list<Wnd*>& wnds, const Wnd* destination);
00276     virtual void   Render();
00277     virtual void   KeyPress(Key key, Flags<ModKey> mod_keys);
00278     virtual void   MouseWheel(const Pt& pt, int move, Flags<ModKey> mod_keys);
00279     virtual void   DragDropEnter(const Pt& pt, const std::map<Wnd*, Pt>& drag_drop_wnds, Flags<ModKey> mod_keys);
00280     virtual void   DragDropHere(const Pt& pt, const std::map<Wnd*, Pt>& drag_drop_wnds, Flags<ModKey> mod_keys);
00281     virtual void   DragDropLeave();
00282     virtual void   TimerFiring(int ticks, Timer* timer);
00283 
00284     virtual void   SizeMove(const Pt& ul, const Pt& lr); 
00285 
00286     virtual void   Disable(bool b = true);
00287     virtual void   SetColor(Clr c);
00288 
00289     int            Insert(Row* row, int at = -1);         
00290     Row*           Erase(int idx);                        
00291     void           Clear();                               
00292     void           SelectRow(int n);                      
00293     void           DeselectRow(int n);                    
00294     void           SelectAll();                           
00295     void           DeselectAll();                         
00296     Row&           GetRow(int n);                         
00297 
00298     void           SetSelections(const std::set<int>& s); 
00299     void           SetCaret(int idx);                     
00300     void           BringRowIntoView(int n);               
00301 
00302     void           SetInteriorColor(Clr c);               
00303     void           SetHiliteColor(Clr c);                 
00304 
00306     void           SetStyle(Flags<ListBoxStyle> s);
00307 
00308     void           SetColHeaders(Row* r);                 
00309     void           RemoveColHeaders();                    
00310 
00311     void           SetColWidth(int n, int w);             
00312     void           SetNumCols(int n);                     
00313     void           SetSortCol(int n);                     
00314 
00318     void           SetSortCmp(const boost::function<bool (const Row&, const Row&, int)>& sort_cmp);
00319 
00323     void           LockColWidths();
00324 
00325     void           UnLockColWidths();                       
00326     void           SetColAlignment(int n, Alignment align); 
00327     void           SetRowAlignment(int n, Alignment align); 
00328 
00331     void           AllowDropType(const std::string& str);
00332 
00336     void           DisallowDropType(const std::string& str);
00337 
00340     void           AutoScrollDuringDragDrops(bool auto_scroll);
00341 
00344     void           SetAutoScrollMargin(int margin);
00345 
00347     void           SetAutoScrollInterval(int interval);
00348 
00349     virtual void   DefineAttributes(WndEditor* editor);
00351 
00355     template <class RowType>
00356     struct DefaultRowCmp
00357     {
00359         bool operator()(const Row& lhs, const Row& rhs, int column) const;
00360     };
00361 
00362     static const int BORDER_THICK; 
00363  
00365 
00366     GG_ABSTRACT_EXCEPTION(Exception);
00367 
00371     GG_CONCRETE_EXCEPTION(DontAcceptDrop, GG::ListBox, Exception);
00373 
00374 protected: 
00376     ListBox(); 
00377 
00378  
00380     int             RightMargin() const;     
00381     int             BottomMargin() const;    
00382     int             CellMargin() const {return m_cell_margin;} 
00383 
00384     int             RowUnderPt(const Pt& pt) const; 
00385 
00386     int             OldSelRow() const;   
00387     int             OldRDownRow() const; 
00388     int             LClickRow() const;   
00389     int             RClickRow() const;   
00390 
00391     bool            AutoScrollingUp() const;    
00392     bool            AutoScrollingDown() const;  
00393     bool            AutoScrollingLeft() const;  
00394     bool            AutoScrollingRight() const; 
00395 
00400     int VerticalScrollPadding(int client_height_without_horizontal_scroll);
00401 
00406     int HorizontalScrollPadding(int client_width_without_vertical_scroll);
00408  
00410     virtual bool    EventFilter(Wnd* w, const WndEvent& event);
00411 
00412     int             Insert(Row* row, int at, bool dropped);  
00413     Row*            Erase(int idx, bool removing_duplicate); 
00414     void            BringCaretIntoView();           
00415     void            RecreateScrolls();              
00416     void            ResetAutoScrollVars();          
00417     void            Resort();                       
00418 
00419 
00420 private:
00421     void            ConnectSignals();
00422     void            ValidateStyle(); 
00423     void            AdjustScrolls(bool adjust_for_resize); 
00424     void            VScrolled(int tab_low, int tab_high, int low, int high); 
00425     void            HScrolled(int tab_low, int tab_high, int low, int high); 
00426     void            ClickAtRow(int row, Flags<ModKey> mod_keys); 
00427     void            NormalizeRow(Row* row); 
00428     int             FirstRowShownWhenBottomIs(int bottom_row, int client_height); 
00429     int             FirstColShownWhenRightIs(int right_col, int client_width); 
00430 
00431     Scroll*         m_vscroll;          
00432     Scroll*         m_hscroll;          
00433     int             m_caret;            
00434     std::set<int>   m_selections;       
00435     int             m_old_sel_row;      
00436     bool            m_old_sel_row_selected; 
00437     int             m_old_rdown_row;    
00438     int             m_lclick_row;       
00439     int             m_rclick_row;       
00440     int             m_last_row_browsed; 
00441     bool            m_suppress_erase_signal; 
00442 
00443     std::vector<Row*> m_rows;           
00444 
00445     int             m_first_row_shown;  
00446     int             m_first_col_shown;  
00447     std::vector<int>
00448                     m_col_widths;       
00449     std::vector<Alignment> 
00450                     m_col_alignments;   
00451     int             m_cell_margin;      
00452 
00453     Clr             m_int_color;        
00454     Clr             m_hilite_color;     
00455     Flags<ListBoxStyle>
00456                     m_style;            
00457 
00458     Row*            m_header_row;       
00459     bool            m_keep_col_widths;  
00460     bool            m_clip_cells;       
00461     int             m_sort_col;         
00462     boost::function<bool (const Row&, const Row&, int)>
00463                     m_sort_cmp;         
00464     std::set<std::string>
00465                     m_allowed_drop_types;
00466 
00467     bool            m_auto_scroll_during_drag_drops;
00468     int             m_auto_scroll_margin;
00469     bool            m_auto_scrolling_up;
00470     bool            m_auto_scrolling_down;
00471     bool            m_auto_scrolling_left;
00472     bool            m_auto_scrolling_right;
00473     Timer           m_auto_scroll_timer;
00474 
00475     friend class DropDownList; 
00476 
00477     friend class boost::serialization::access;
00478     template <class Archive>
00479     void serialize(Archive& ar, const unsigned int version);
00480 };
00481 
00482 } // namespace GG
00483 
00484 BOOST_CLASS_VERSION(GG::ListBox, 1)
00485 
00486 // template implementations
00487 template <class Archive>
00488 void GG::ListBox::Row::serialize(Archive& ar, const unsigned int version)
00489 {
00490     ar  & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Control)
00491         & BOOST_SERIALIZATION_NVP(m_cells)
00492         & BOOST_SERIALIZATION_NVP(m_row_alignment)
00493         & BOOST_SERIALIZATION_NVP(m_col_alignments)
00494         & BOOST_SERIALIZATION_NVP(m_col_widths)
00495         & BOOST_SERIALIZATION_NVP(m_margin);
00496 }
00497 
00498 template <class RowType>
00499 bool GG::ListBox::DefaultRowCmp<RowType>::operator()(const GG::ListBox::Row& lhs, const GG::ListBox::Row& rhs, int column) const
00500 {
00501     return static_cast<const RowType&>(lhs).SortKey(column) < static_cast<const RowType&>(rhs).SortKey(column);
00502 }
00503 
00504 template <class Archive>
00505 void GG::ListBox::serialize(Archive& ar, const unsigned int version)
00506 {
00507     ar  & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Control)
00508         & BOOST_SERIALIZATION_NVP(m_vscroll)
00509         & BOOST_SERIALIZATION_NVP(m_hscroll)
00510         & BOOST_SERIALIZATION_NVP(m_caret)
00511         & BOOST_SERIALIZATION_NVP(m_selections)
00512         & BOOST_SERIALIZATION_NVP(m_old_sel_row)
00513         & BOOST_SERIALIZATION_NVP(m_old_sel_row_selected)
00514         & BOOST_SERIALIZATION_NVP(m_old_rdown_row)
00515         & BOOST_SERIALIZATION_NVP(m_lclick_row)
00516         & BOOST_SERIALIZATION_NVP(m_rclick_row)
00517         & BOOST_SERIALIZATION_NVP(m_last_row_browsed)
00518         & BOOST_SERIALIZATION_NVP(m_suppress_erase_signal)
00519         & BOOST_SERIALIZATION_NVP(m_rows)
00520         & BOOST_SERIALIZATION_NVP(m_first_row_shown)
00521         & BOOST_SERIALIZATION_NVP(m_first_col_shown)
00522         & BOOST_SERIALIZATION_NVP(m_col_widths)
00523         & BOOST_SERIALIZATION_NVP(m_col_alignments)
00524         & BOOST_SERIALIZATION_NVP(m_cell_margin)
00525         & BOOST_SERIALIZATION_NVP(m_int_color)
00526         & BOOST_SERIALIZATION_NVP(m_hilite_color)
00527         & BOOST_SERIALIZATION_NVP(m_style)
00528         & BOOST_SERIALIZATION_NVP(m_header_row)
00529         & BOOST_SERIALIZATION_NVP(m_keep_col_widths)
00530         & BOOST_SERIALIZATION_NVP(m_clip_cells)
00531         & BOOST_SERIALIZATION_NVP(m_sort_col)
00532         & BOOST_SERIALIZATION_NVP(m_allowed_drop_types);
00533 
00534     if (1 <= version) {
00535         ar  & BOOST_SERIALIZATION_NVP(m_auto_scroll_during_drag_drops)
00536             & BOOST_SERIALIZATION_NVP(m_auto_scroll_margin)
00537             & BOOST_SERIALIZATION_NVP(m_auto_scrolling_up)
00538             & BOOST_SERIALIZATION_NVP(m_auto_scrolling_down)
00539             & BOOST_SERIALIZATION_NVP(m_auto_scrolling_left)
00540             & BOOST_SERIALIZATION_NVP(m_auto_scrolling_right)
00541             & BOOST_SERIALIZATION_NVP(m_auto_scroll_timer);
00542     }
00543 
00544     if (Archive::is_loading::value) {
00545         ValidateStyle();
00546         ConnectSignals();
00547     }
00548 }
00549 
00550 #endif // _GG_ListBox_h_

Generated on Wed Mar 26 14:35:41 2008 for GG by  doxygen 1.5.2