Author: mordante
Date: Wed Aug 27 00:20:47 2008
New Revision: 29006

URL: http://svn.gna.org/viewcvs/wesnoth?rev=29006&view=rev
Log:
Cleanup.

Removed some direct access to member via a non const reference.
Renemed sel_len to selection_length and sel_start to selection_start.

Modified:
    trunk/src/gui/widgets/text.cpp
    trunk/src/gui/widgets/text.hpp
    trunk/src/gui/widgets/text_box.cpp

Modified: trunk/src/gui/widgets/text.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/text.cpp?rev=29006&r1=29005&r2=29006&view=diff
==============================================================================
--- trunk/src/gui/widgets/text.cpp (original)
+++ trunk/src/gui/widgets/text.cpp Wed Aug 27 00:20:47 2008
@@ -66,8 +66,8 @@
 { 
        DBG_G_E << "Text: left mouse button double click.\n";
 
-       sel_start_ = 0;
-       sel_len_ = text_.size();
+       selection_start_ = 0;
+       selection_length_ = text_.size();
 
 }
 
@@ -211,8 +211,8 @@
                calculate_char_offset(); 
 
                // default to put the cursor at the end of the buffer.
-               sel_start_ = text_.size();
-               sel_len_ = 0;
+               selection_start_ = text_.size();
+               selection_length_ = 0;
                set_canvas_text();
                set_dirty(); 
        } 
@@ -222,10 +222,10 @@
 {
        if(select) {
 
-               if(sel_start_ == offset) {
-                       sel_len_ = 0;
+               if(selection_start_ == offset) {
+                       selection_length_ = 0;
                } else {
-                       sel_len_ = - (sel_start_ - offset);
+                       selection_length_ = - (selection_start_ - offset);
                }
 
 #ifdef __unix__
@@ -237,8 +237,8 @@
 
        } else {
                assert(offset <= text_.size());
-               sel_start_ = offset;
-               sel_len_ = 0;
+               selection_start_ = offset;
+               selection_length_ = 0;
 
                set_canvas_text();
                set_dirty();
@@ -247,17 +247,17 @@
 
 void ttext_::copy_selection(const bool mouse)
 {
-       int len = sel_len();
-       unsigned start = sel_start();
-
-       if(len < 0) {
-               len = - len;
-               start -= len;
+       int length = selection_length_;
+       unsigned start = selection_start_;
+
+       if(length < 0) {
+               length = - length;
+               start -= length;
        }
 
        const wide_string& wtext = utils::string_to_wstring(text_);
        const std::string& text = utils::wstring_to_string(
-               wide_string(wtext.begin() + start, wtext.begin() + start +len));
+               wide_string(wtext.begin() + start, wtext.begin() + start + 
length));
 
        copy_to_clipboard(text, mouse);
 }
@@ -271,15 +271,31 @@
 
        delete_selection();
 
-       text_.insert(sel_start_, text);
-
-       sel_start_ += utils::string_to_wstring(text).size();
+       text_.insert(selection_start_, text);
+
+       selection_start_ += utils::string_to_wstring(text).size();
 
        calculate_char_offset(); 
        set_canvas_text();
        set_dirty(); 
 }
 
+void  ttext_::set_selection_start(const size_t selection_start)
+{
+       if(selection_start != selection_start_) {
+               selection_start_ = selection_start;
+               set_dirty();
+       }
+}
+
+void ttext_::set_selection_length(const unsigned selection_length) 
+{ 
+       if(selection_length != selection_length_) {
+               selection_length_ = selection_length;
+               set_dirty();
+       }
+}
+
 void ttext_::set_state(const tstate state)
 {
        if(state != state_) {
@@ -294,8 +310,9 @@
        DBG_G_E << "Text: key press: left arrow.\n";
 
        handled = true;
-       if(sel_start_) {
-               set_cursor(sel_start_ - 1 + sel_len_, modifier & KMOD_SHIFT);
+       if(selection_start_) {
+               set_cursor(
+                       selection_start_ - 1 + selection_length_, modifier & 
KMOD_SHIFT);
        }
 }
 
@@ -305,8 +322,9 @@
        DBG_G_E << "Text: key press: right arrow.\n";
 
        handled = true;
-       if(sel_start_ < text_.size()) {
-               set_cursor(sel_start_ + 1 + sel_len_, modifier & KMOD_SHIFT);
+       if(selection_start_ < text_.size()) {
+               set_cursor(
+                       selection_start_ + 1 + selection_length_, modifier & 
KMOD_SHIFT);
        }
 }
 
@@ -339,7 +357,7 @@
        DBG_G_E << "Text: key press: backspace.\n";
 
        handled = true;
-       if(sel_start_){
+       if(selection_start_){
                delete_char(true);
        }
 }
@@ -349,9 +367,9 @@
        DBG_G_E << "Text: key press: delete.\n";
 
        handled = true;
-       if(sel_len_ != 0) {
+       if(selection_length_ != 0) {
                delete_selection();
-       } else if (sel_start_ < text_.size()) {
+       } else if (selection_start_ < text_.size()) {
                delete_char(false);
        }
 }

Modified: trunk/src/gui/widgets/text.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/text.hpp?rev=29006&r1=29005&r2=29006&view=diff
==============================================================================
--- trunk/src/gui/widgets/text.hpp (original)
+++ trunk/src/gui/widgets/text.hpp Wed Aug 27 00:20:47 2008
@@ -34,8 +34,8 @@
                tcontrol(COUNT),
                state_(ENABLED),
                text_(),
-               sel_start_(0),
-               sel_len_(0),
+               selection_start_(0),
+               selection_length_(0),
                max_length_(std::string::npos)
        {
        }
@@ -114,7 +114,7 @@
        void goto_start_of_data(const bool select = false) { set_cursor(0, 
select); }
 
        /** Selects all text. */
-       void select_all() { sel_start_ = 0; goto_end_of_data(true); }
+       void select_all() { selection_start_ = 0; goto_end_of_data(true); }
 
        /**
         * Moves the cursor at the wanted position.
@@ -155,22 +155,11 @@
 
        /***** ***** ***** setters / getters for members ***** ****** *****/
        
-       size_t get_sel_start() const { return sel_start_; }
-       void  set_sel_start(const size_t sel_start) 
-               { sel_start_ = sel_start; set_dirty(); }
-
-       size_t get_sel_len() const { return sel_len_; }
-       void set_sel_len(const unsigned sel_len) { sel_len_ = sel_len; 
set_dirty(); }
-
-protected:
-
-       /***** ***** ***** setters / getters for members ***** ****** *****/
-
-       std::string& text() { return text_; }
-
-       size_t& sel_start() { return sel_start_; }
-
-       int& sel_len() { return sel_len_; }
+       size_t get_selection_start() const { return selection_start_; }
+       void  set_selection_start(const size_t selection_start);
+
+       size_t get_selection_length() const { return selection_length_; }
+       void set_selection_length(const unsigned selection_length);
 
 private:
        /** Note the order of the states must be the same as defined in 
settings.hpp. */
@@ -193,16 +182,16 @@
        virtual void calculate_char_offset() = 0;
 
        /** Start of the selected text. */
-       size_t sel_start_;
+       size_t selection_start_;
 
        /** 
         * Length of the selected text.
         *
-        *  positive sel_len_ means selection to the right.
-        *  negative sel_len_ means selection to the left.
-        *  sel_len_ == 0 means no selection.
-        */
-       int sel_len_;
+        * * positive selection_len_ means selection to the right.
+        * * negative selection_len_ means selection to the left.
+        * * selection_len_ == 0 means no selection.
+        */
+       int selection_length_;
 
        /** 
         * Maximum length of the text. 

Modified: trunk/src/gui/widgets/text_box.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/text_box.cpp?rev=29006&r1=29005&r2=29006&view=diff
==============================================================================
--- trunk/src/gui/widgets/text_box.cpp (original)
+++ trunk/src/gui/widgets/text_box.cpp Wed Aug 27 00:20:47 2008
@@ -123,19 +123,20 @@
 {
        foreach(tcanvas& tmp, canvas()) {
 
-               // NOTE when sel_start() == - sel_len() then the offset 
calculation will
-               // access character_offset_[-1] so add special cases to use 0 
instead.
-               // The same can happen if sel_start() == 0.
+               // NOTE when get_selection_start() == - get_selection_length() 
then the
+               // offset calculation will access character_offset_[-1] so add 
special
+               // cases to use 0 instead.  The same can happen if
+               // get_selection_start() == 0.
 
                // Set the general variables.
-               tmp.set_variable("text", variant(text()));
+               tmp.set_variable("text", variant(get_value()));
                tmp.set_variable("text_x_offset", variant(text_x_offset_));
                tmp.set_variable("text_y_offset", variant(text_y_offset_));
 
                // Set the cursor info.
-               const unsigned start = sel_start();
-               const int len = sel_len();
-               if(text().empty() || start + len == 0) {
+               const unsigned start = get_selection_start();
+               const int len = get_selection_length();
+               if(get_value().empty() || start + len == 0) {
                        tmp.set_variable("cursor_offset", variant(0));
                } else {
                        tmp.set_variable("cursor_offset", 
@@ -208,22 +209,29 @@
        const unsigned width = surf->w;
 
        // Insert the char in the buffer, we need to assume it's a wide string.
-       wide_string tmp = utils::string_to_wstring(text());
-       tmp.insert(tmp.begin() + sel_start(), unicode);
-       text() = utils::wstring_to_string(tmp);
+       wide_string tmp = utils::string_to_wstring(get_value());
+       tmp.insert(tmp.begin() + get_selection_start(), unicode);
 
        // Update the widths.
-       character_offset_.insert(character_offset_.begin() + sel_start(), 
width);
-       if(sel_start() != 0) {
-               character_offset_[sel_start()] += character_offset_[sel_start() 
- 1]; 
-       }
-
-       ++sel_start();
-       for(size_t i = sel_start(); i < character_offset_.size(); ++i) {
+       character_offset_.insert(
+               character_offset_.begin() + get_selection_start(), width);
+
+       if(get_selection_start() != 0) {
+               character_offset_[get_selection_start()] 
+                       += character_offset_[get_selection_start() - 1]; 
+       }
+
+       const unsigned start = get_selection_start() + 1;
+       // set_value also resets the selection start, maybe add a real insert
+       // member to avoid that.
+       set_value(utils::wstring_to_string(tmp));
+       for(size_t i = start; 
+                       i < character_offset_.size(); ++i) {
+
                character_offset_[i] += width;
        }
 
-       set_cursor(sel_start(), false);
+       set_cursor(start, false);
        set_canvas_text();
        set_dirty();
 }
@@ -231,32 +239,31 @@
 void ttext_box::delete_char(const bool before_cursor)
 {
        if(before_cursor) {
-               --sel_start();
-               set_cursor(sel_start(), false);
-       }
-
-       sel_len() = 1;
+               set_cursor(get_selection_start() - 1, false);
+       }
+
+       set_selection_length(1);
 
        delete_selection();
 }
 
 void ttext_box::delete_selection()
 {
-       if(sel_len() == 0) {
+       if(get_selection_length() == 0) {
                return;
        }
 
        // If we have a negative range change it to a positive range.
        // This makes the rest of the algoritms easier.
-       int len = sel_len();
-       unsigned  start = sel_start();
+       int len = get_selection_length();
+       unsigned  start = get_selection_start();
        if(len < 0) {
                len = - len;
                start -= len;
        }
 
        // Update the text, we need to assume it's a wide string.
-       wide_string tmp = utils::string_to_wstring(text());
+       wide_string tmp = utils::string_to_wstring(get_value());
        tmp.erase(tmp.begin() + start, tmp.begin() + start + len);
        const std::string& text = utils::wstring_to_string(tmp);
        set_value(text);
@@ -276,7 +283,9 @@
                return;
        }
 
-       int offset = get_character_offset_at(mouse.x - 
static_cast<int>(text_x_offset_));
+       int offset = 
+               get_character_offset_at(mouse.x - 
static_cast<int>(text_x_offset_));
+
        if(offset < 0) {
                return;
        }
@@ -298,7 +307,7 @@
 
                ++result;
        }
-       return text().size();
+       return get_value().size();
 }
 
 void ttext_box::update_offsets()
@@ -306,7 +315,9 @@
        assert(config());
 
        boost::intrusive_ptr<const ttext_box_definition::tresolution> conf =
-               boost::dynamic_pointer_cast<const 
ttext_box_definition::tresolution>(config());
+               boost::dynamic_pointer_cast
+               <const ttext_box_definition::tresolution>(config());
+
        assert(conf);
 
        text_height_ = font::get_max_height(conf->text_font_size);
@@ -337,7 +348,7 @@
        std::string rendered_text;
        const unsigned font_size = config()->text_font_size;
 
-       foreach(const wchar_t& unicode, utils::string_to_wstring(text())) {
+       foreach(const wchar_t& unicode, utils::string_to_wstring(get_value())) {
                rendered_text.insert(rendered_text.end(), unicode);
                surface surf = render_text(rendered_text, font_size);
                assert(surf);
@@ -349,7 +360,7 @@
 void ttext_box::handle_key_up_arrow(SDLMod /*modifier*/, bool& handled)
 {
        if (history_.get_enabled()) {
-               std::string s = history_.up(text());
+               std::string s = history_.up(get_value());
                if (!s.empty()) {
                        set_value(s);
                }
@@ -361,7 +372,7 @@
 void ttext_box::handle_key_down_arrow(SDLMod /*modifier*/, bool& handled)
 {
        if (history_.get_enabled()) {
-               set_value(history_.down(text()));
+               set_value(history_.down(get_value()));
                handled = true;
        }
 }


_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits

Reply via email to