GG

MultiEdit.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 /* GG is a GUI for SDL and OpenGL.
00003    Copyright (C) 2003-2008 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@gmail.com */
00025 
00029 #ifndef _GG_MultiEdit_h_
00030 #define _GG_MultiEdit_h_
00031 
00032 #include <GG/Edit.h>
00033 #include <GG/MultiEditFwd.h>
00034 
00035 
00036 namespace GG {
00037 
00038 class Scroll;
00039 
00060 class GG_API MultiEdit : public Edit
00061 {
00062 public: 
00064 
00065     MultiEdit(X x, Y y, X w, Y h, const std::string& str, const boost::shared_ptr<Font>& font,
00066               Clr color, Flags<MultiEditStyle> style = MULTI_LINEWRAP, Clr text_color = CLR_BLACK,
00067               Clr interior = CLR_ZERO, Flags<WndFlag> flags = INTERACTIVE);
00068 
00070     virtual ~MultiEdit();
00072  
00074     virtual Pt MinUsableSize() const;
00075     virtual Pt ClientLowerRight() const;
00076 
00078     Flags<MultiEditStyle> Style() const;
00079 
00083     std::size_t MaxLinesOfHistory() const;
00085  
00087     virtual void   Render();
00088 
00089     virtual void   SizeMove(const Pt& ul, const Pt& lr);
00090 
00091     virtual void   SelectAll();
00092     virtual void   SetText(const std::string& str);
00093 
00095     void           SetStyle(Flags<MultiEditStyle> style);
00096 
00099     void           SetMaxLinesOfHistory(std::size_t max);
00100 
00101     virtual void   DefineAttributes(WndEditor* editor);
00103 
00107     static const std::size_t ALL_LINES;
00108 
00110     static const unsigned int BORDER_THICK;
00111 
00112 protected: 
00114     MultiEdit(); 
00115 
00116  
00118 
00119     virtual bool MultiSelected() const;
00120 
00123     X RightMargin() const;
00124 
00127     Y BottomMargin() const;
00128 
00131     std::pair<std::size_t, CPSize> CharAt(const Pt& pt) const;
00132 
00136     std::pair<std::size_t, CPSize> CharAt(CPSize idx) const;
00137 
00139     Pt ScrollPosition() const;
00140 
00147     CPSize CharIndexOf(std::size_t row, CPSize char_idx,
00148                        const std::vector<Font::LineData>* line_data = 0) const;
00149 
00152     X RowStartX(std::size_t row) const;
00153 
00156     X CharXOffset(std::size_t row, CPSize idx) const;
00157 
00160     std::size_t RowAt(Y y) const;
00161 
00164     CPSize CharAt(std::size_t row, X x) const;
00165 
00167     std::size_t FirstVisibleRow() const;
00168 
00170     std::size_t LastVisibleRow() const;
00171 
00173     std::size_t FirstFullyVisibleRow() const;
00174 
00176     std::size_t LastFullyVisibleRow() const;
00177 
00180     CPSize FirstVisibleChar(std::size_t row) const;
00181 
00184     CPSize LastVisibleChar(std::size_t row) const;
00185 
00187     std::pair<std::size_t, CPSize> HighCursorPos() const;
00188 
00190     std::pair<std::size_t, CPSize> LowCursorPos() const;
00192  
00194     virtual void LButtonDown(const Pt& pt, Flags<ModKey> mod_keys);
00195     virtual void LDrag(const Pt& pt, const Pt& move, Flags<ModKey> mod_keys);
00196     virtual void MouseWheel(const Pt& pt, int move, Flags<ModKey> mod_keys);
00197     virtual void KeyPress(Key key, boost::uint32_t key_code_point, Flags<ModKey> mod_keys);
00198 
00200     void RecreateScrolls();
00201 
00209     void PreserveTextPositionOnNextSetText();
00211 
00214     static const unsigned int SCROLL_WIDTH;
00215 
00216 private:
00217     void    Init();
00218     void    ValidateStyle();
00219     void    ClearSelected();   
00220     void    AdjustView();      
00221     void    AdjustScrolls();   
00222     void    VScrolled(int upper, int lower, int range_upper, int range_lower);
00223     void    HScrolled(int upper, int lower, int range_upper, int range_lower);
00224 
00225     Flags<MultiEditStyle> m_style;
00226 
00227     std::pair<std::size_t, CPSize> m_cursor_begin; 
00228     std::pair<std::size_t, CPSize> m_cursor_end;   
00229     // if m_cursor_begin == m_cursor_end, the caret is draw at m_cursor_end
00230 
00231     Pt          m_contents_sz;          
00232 
00233     X           m_first_col_shown;      
00234     Y           m_first_row_shown;      
00235 
00236     std::size_t m_max_lines_history;
00237 
00238     Scroll*     m_vscroll;
00239     Scroll*     m_hscroll;
00240 
00241     bool        m_preserve_text_position_on_next_set_text;
00242     bool        m_ignore_adjust_scrolls;
00243 
00244     friend class boost::serialization::access;
00245     template <class Archive>
00246     void serialize(Archive& ar, const unsigned int version);
00247 };
00248 
00249 } // namespace GG
00250 
00251 // template implementations
00252 template <class Archive>
00253 void GG::MultiEdit::serialize(Archive& ar, const unsigned int version)
00254 {
00255     ar  & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Edit)
00256         & BOOST_SERIALIZATION_NVP(m_style)
00257         & BOOST_SERIALIZATION_NVP(m_cursor_begin)
00258         & BOOST_SERIALIZATION_NVP(m_cursor_end)
00259         & BOOST_SERIALIZATION_NVP(m_contents_sz)
00260         & BOOST_SERIALIZATION_NVP(m_first_col_shown)
00261         & BOOST_SERIALIZATION_NVP(m_first_row_shown)
00262         & BOOST_SERIALIZATION_NVP(m_max_lines_history)
00263         & BOOST_SERIALIZATION_NVP(m_vscroll)
00264         & BOOST_SERIALIZATION_NVP(m_hscroll);
00265 
00266     if (Archive::is_loading::value)
00267         ValidateStyle();
00268 }
00269 
00270 #endif // _GG_MultiEdit_h_