Author: mordante
Date: Tue Sep  2 20:05:13 2008
New Revision: 29209

URL: http://svn.gna.org/viewcvs/wesnoth?rev=29209&view=rev
Log:
Implemented the maximum length for a text.

The maximum length got implemented in font::ttext instead of the
gui2::ttext_ class since that simplified the length checks to one
location. Also add return values to indicate what happened upon
insertion. Added the maximum length to the user name in the mp selection
dialog.

Modified:
    trunk/src/gui/dialogs/mp_method_selection.cpp
    trunk/src/gui/widgets/text.cpp
    trunk/src/gui/widgets/text.hpp
    trunk/src/text.cpp
    trunk/src/text.hpp

Modified: trunk/src/gui/dialogs/mp_method_selection.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/dialogs/mp_method_selection.cpp?rev=29209&r1=29208&r2=29209&view=diff
==============================================================================
--- trunk/src/gui/dialogs/mp_method_selection.cpp (original)
+++ trunk/src/gui/dialogs/mp_method_selection.cpp Tue Sep  2 20:05:13 2008
@@ -23,6 +23,7 @@
 #include "gui/widgets/settings.hpp"
 #include "gui/widgets/text_box.hpp"
 #include "log.hpp"
+#include "multiplayer.hpp"
 #include "video.hpp"
 #include "wml_exception.hpp"
 
@@ -43,7 +44,8 @@
  * 
  * @start_table = container
  *     user_name (text_box)            This text contains the name the user on 
- *                                     the MP server.
+ *                                     the MP server. This widget will get a
+ *                                     fixed maximum length by the engine.
  *     method_list (listbox)           The list with possible game methods.
  * @end_table
  */
@@ -59,6 +61,7 @@
        VALIDATE(user_widget, missing_widget("user_name"));
 
        user_widget->set_value(user_name_);
+       user_widget->set_maximum_length(mp::max_login_size);
        window.keyboard_capture(user_widget);
 
        tlistbox* list = 
dynamic_cast<tlistbox*>(window.find_widget("method_list", false));

Modified: trunk/src/gui/widgets/text.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/text.cpp?rev=29209&r1=29208&r2=29209&view=diff
==============================================================================
--- trunk/src/gui/widgets/text.cpp (original)
+++ trunk/src/gui/widgets/text.cpp Tue Sep  2 20:05:13 2008
@@ -204,6 +204,24 @@
        }
 }
 
+void ttext_::set_maximum_length(const size_t maximum_length)
+{
+       const bool need_update = text_.get_length() > maximum_length;
+
+       text_.set_maximum_length(maximum_length); 
+
+       if(need_update) {
+               if(selection_start_ > maximum_length) {
+                       selection_start_ = maximum_length;
+                       selection_length_ = 0;
+               } else if(selection_start_ + selection_length_ > 
maximum_length) {
+                       selection_length_ = maximum_length - selection_start_;
+               }
+               update_canvas();
+               set_dirty(); 
+       }
+}
+
 void ttext_::set_value(const std::string& text)
 { 
        if(text != text_.text()) { 
@@ -248,12 +266,13 @@
 {
        delete_selection();
 
-       text_.insert_unicode(selection_start_, unicode);
-
-       // Update status
-       set_cursor(selection_start_ + 1, false);
-       update_canvas();
-       set_dirty();
+       if(text_.insert_unicode(selection_start_, unicode)) {
+
+               // Update status
+               set_cursor(selection_start_ + 1, false);
+               update_canvas();
+               set_dirty();
+       }
 }
 
 void ttext_::copy_selection(const bool mouse)

Modified: trunk/src/gui/widgets/text.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/text.hpp?rev=29209&r1=29208&r2=29209&view=diff
==============================================================================
--- trunk/src/gui/widgets/text.hpp (original)
+++ trunk/src/gui/widgets/text.hpp Tue Sep  2 20:05:13 2008
@@ -36,8 +36,7 @@
                state_(ENABLED),
                text_(),
                selection_start_(0),
-               selection_length_(0),
-               max_length_(std::string::npos)
+               selection_length_(0)
        {
        }
 
@@ -69,6 +68,10 @@
 
        /** Inherited from tcontrol. */
        unsigned get_state() const { return state_; }
+
+       /***** ***** ***** ***** expose some functions ***** ***** ***** *****/
+
+       void set_maximum_length(const size_t maximum_length);
 
        /***** ***** ***** setters / getters for members ***** ****** *****/
 
@@ -213,13 +216,6 @@
         */
        int selection_length_;
 
-       /** 
-        * Maximum length of the text. 
-        *
-        * @todo, respect the maximum (which isn't done at the moment).
-        */
-       size_t max_length_;
-
        /****** handling of special keys first the pure virtuals *****/
 
        /**

Modified: trunk/src/text.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/text.cpp?rev=29209&r1=29208&r2=29209&view=diff
==============================================================================
--- trunk/src/text.cpp (original)
+++ trunk/src/text.cpp Tue Sep  2 20:05:13 2008
@@ -74,6 +74,7 @@
        maximum_width_(-1),
        maximum_height_(-1),
        ellipse_mode_(PANGO_ELLIPSIZE_END),
+       maximum_length_(std::string::npos),
        calculation_dirty_(true),
        length_(0),
        surface_dirty_(true),
@@ -135,24 +136,31 @@
                return 0;
        }
 
-       wide_string wtext = utils::string_to_wstring(text);
-       insert_unicode(offset, wtext);
-       return wtext.size();
-}
-
-void ttext::insert_unicode(const unsigned offset, const wchar_t unicode)
-{
-       insert_unicode(offset, wide_string(1, unicode));
-}
-
-void ttext::insert_unicode(const unsigned offset, const wide_string& unicode)
+       return insert_unicode(offset, utils::string_to_wstring(text));
+}
+
+bool ttext::insert_unicode(const unsigned offset, const wchar_t unicode)
+{
+       return (insert_unicode(offset, wide_string(1, unicode)) == 1);
+}
+
+unsigned ttext::insert_unicode(const unsigned offset, const wide_string& 
unicode)
 {
        assert(offset <= length_);
 
+       if(length_ == maximum_length_) {
+               return 0;
+       }
+
+       const unsigned len = length_ + unicode.size() > maximum_length_
+               ? maximum_length_ - length_  : unicode.size();
+
        wide_string tmp = utils::string_to_wstring(text_);
-       tmp.insert(tmp.begin() + offset, unicode.begin(), unicode.end());
+       tmp.insert(tmp.begin() + offset, unicode.begin(), unicode.begin() + 
len);
 
        set_text(utils::wstring_to_string(tmp), false);
+
+       return len;
 }
 
 gui2::tpoint ttext::get_cursor_position(
@@ -318,6 +326,21 @@
        return *this;
 }
 
+ttext& ttext::set_maximum_length(const size_t maximum_length)
+{
+       if(maximum_length != maximum_length_) {
+               maximum_length_ = maximum_length;
+               if(length_ > maximum_length_) {
+
+                       wide_string tmp = utils::string_to_wstring(text_);
+                       tmp.resize(maximum_length_);
+                       set_text(utils::wstring_to_string(tmp), false);
+               }
+       }
+
+       return *this;
+}
+
 namespace {
 
 /** Small helper class to make sure the font object is destroyed properly. */

Modified: trunk/src/text.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/text.hpp?rev=29209&r1=29208&r2=29209&view=diff
==============================================================================
--- trunk/src/text.hpp (original)
+++ trunk/src/text.hpp Tue Sep  2 20:05:13 2008
@@ -98,16 +98,19 @@
         * @param offset              The position to insert the char.
         * @param unicode             The character to insert.
         *
-        */
-       void insert_unicode(const unsigned offset, const wchar_t unicode);
+        * @returns                   True upon success, false otherwise.
+        */
+       bool insert_unicode(const unsigned offset, const wchar_t unicode);
 
        /**
         * Inserts unicode text.
         *
         * @param offset              The position to insert the text.
         * @param unicode             Vector with characters to insert.
-        */
-       void insert_unicode(
+        *
+        * @returns                   The number of characters inserted.
+        */
+       unsigned insert_unicode(
                const unsigned offset, const std::vector<wchar_t>& unicode);
 
        /***** ***** ***** ***** Font flags ***** ***** ***** *****/
@@ -174,6 +177,8 @@
 
        ttext& set_ellipse_mode(const PangoEllipsizeMode ellipse_mode);
 
+       ttext& set_maximum_length(const size_t maximum_length);
+
 private:
 
        /***** ***** ***** *****  Pango variables ***** ***** ***** *****/
@@ -209,6 +214,9 @@
        /** The way too long text is shown depends on this mode. */
        PangoEllipsizeMode ellipse_mode_;
 
+       /** The maximum length of the text. */
+       size_t maximum_length_;
+
        /** 
         * The text has two dirty states:
         * - The setting of the state and the size calculations.


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

Reply via email to