mail[Wesnoth-commits] r28405 - in /trunk/src: gui/widgets/control.cpp gui/widgets/control.hpp text.cpp text.hpp


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by koraq on August 09, 2008 - 11:58:
Author: mordante
Date: Sat Aug  9 11:11:26 2008
New Revision: 28405

URL: http://svn.gna.org/viewcvs/wesnoth?rev=28405&view=rev
Log:
Switch the control class to use pango for text size determination.
NOTE: This change makes wesnoth feel a bit sluggy, but there is still room for
optimization, which will follow later.

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

Modified: trunk/src/gui/widgets/control.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/control.cpp?rev=28405&r1=28404&r2=28405&view=diff
==============================================================================
--- trunk/src/gui/widgets/control.cpp (original)
+++ trunk/src/gui/widgets/control.cpp Sat Aug  9 11:11:26 2008
@@ -14,7 +14,6 @@
 
 #include "gui/widgets/control.hpp"
 
-#include "font.hpp"
 #include "foreach.hpp"
 #include "gui/widgets/helper.hpp"
 #include "gui/widgets/window.hpp"
@@ -22,6 +21,7 @@
 #include "marked-up_text.hpp"
 #include "util.hpp"
 #include "sdl_utils.hpp"
+#include "../../text.hpp"
 
 #define DBG_G LOG_STREAM_INDENT(debug, gui)
 #define LOG_G LOG_STREAM_INDENT(info, gui)
@@ -113,11 +113,7 @@
                return min_size;
        }
 
-       if(!multiline_label_) {
-               return get_single_line_best_size(min_size);
-       } else {
-               return get_multi_line_best_size(min_size);
-       }
+       return get_best_text_size(min_size, multiline_label_);
 }
 
 tpoint tcontrol::get_best_size() const
@@ -130,11 +126,7 @@
                return default_size;
        }
 
-       if(!multiline_label_) {
-               return get_single_line_best_size(default_size);
-       } else {
-               return get_multi_line_best_size(default_size);
-       }
+       return get_best_text_size(default_size, multiline_label_);
 }
 
 tpoint tcontrol::get_maximum_size() const
@@ -250,78 +242,41 @@
        gui2::restore_background(restorer_, dst, get_rect());
 }
 
-tpoint tcontrol::get_single_line_best_size(const tpoint& config_size) const
+tpoint tcontrol::get_best_text_size(const tpoint& minimum_size, const bool 
word_wrap) const
 {
        assert(!label_.empty());
 
-       // Get the best size depending on the label.
-       SDL_Rect rect = font::line_size(label_, config_->text_font_size, 
config_->text_font_style);
-       const tpoint text_size(rect.w + config_->text_extra_width, rect.h + 
config_->text_extra_height);
-
-       // Get the best size if default has a 0 value always use the size of 
the text.
-       tpoint size(0, 0);
-       if(config_size == size) { // config_size == 0,0
-               size = text_size;
-       } else if(!config_size.x) {
-               size = tpoint(text_size.x, maximum(config_size.y, 
text_size.y));
-       } else if(!config_size.y) {
-               size = tpoint(maximum(config_size.x, text_size.x), 
text_size.y);
-       } else {
-               size.x = maximum(config_size.x, text_size.x);
-               size.y = maximum(config_size.y, text_size.y);
-       }
-
-       // Honour the maximum.
-       const tpoint maximum_size(config_->max_width, config_->max_height);
-       if(maximum_size.x && size.x > maximum_size.x) {
-               size.x = maximum_size.x;
-       }
-       if(maximum_size.y && size.y > maximum_size.y) {
-               size.y = maximum_size.y;
-       }
+       const tpoint border(config_->text_extra_width, 
config_->text_extra_height);
+       tpoint size = minimum_size - border;
+
+       font::ttext text;
+       text.set_text(label_, false);
+       text.set_font_size(config_->text_font_size);
+       text.set_word_wrap(word_wrap);
+
+       // Try with the minimum wanted size.
+       text.set_maximum_width(size.x);
+       text.set_maximum_height(size.y);
+
+       // If doesn't fit try the maximum.
+       if(text.is_truncated()) {
+               const tpoint maximum_size(config_->max_width, 
config_->max_height);
+               text.set_maximum_width(maximum_size.x ? maximum_size.x - 
border.x : -1);
+               text.set_maximum_height(maximum_size.y ? maximum_size.y - 
border.y : -1);
+       }
+
+       size = text.get_size() + border;
+
+       if(size.x < minimum_size.x) {
+               size.x = minimum_size.x;
+       }
+
+       if(size.y < minimum_size.y) {
+               size.y = minimum_size.y;
+       }
+
        return size;
 }
 
-tpoint tcontrol::get_multi_line_best_size(const tpoint& config_size) const
-{
-       assert(!label_.empty());
-
-       // In multiline mode we only expect a fixed width and no
-       // fixed height so we ignore the height ;-)
-       const tpoint maximum_size(config_->max_width, config_->max_height);
-       if(config_size.y || maximum_size.y) {
-               WRN_G << "Control: Multiline items don't respect the wanted 
height.\n";
-       }
-       unsigned width = 0;
-       if(!config_size.x && !maximum_size.x) {
-               /** @todo FIMXE implement */
-/*             const twindow* window = get_window();
-               if(window) {
-                       const SDL_Rect rect = window->get_client_rect();
-                       LOG_G << "Control: Multiline items want a width, 
falling back to window size.\n";
-                       width = rect.w;
-               } else {
-*/                     ERR_G << "Control: Multiline items want a width, no 
window setting hardcoded.\n";
-                       width = 100;
-//             }
-       } else {
-
-               if(!config_size.x) {
-                       width = maximum_size.x;
-               } else if(!maximum_size.x) {
-                       width = config_size.x;
-               } else {
-                       width = minimum(config_size.x, maximum_size.x);
-               }
-       }
-
-       static const SDL_Color col = {0, 0, 0, 0};
-       const std::string& wrapped_message = font::word_wrap_text(label_, 
config_->text_font_size, width);
-       surface surf = font::get_rendered_text(wrapped_message, 
config_->text_font_size, col);
-       assert(surf);
-
-       return tpoint(surf->w + config_->text_extra_width, surf->h + 
config_->text_extra_height);
-}
-
 } // namespace gui2
 

Modified: trunk/src/gui/widgets/control.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/control.hpp?rev=28405&r1=28404&r2=28405&view=diff
==============================================================================
--- trunk/src/gui/widgets/control.hpp (original)
+++ trunk/src/gui/widgets/control.hpp Sat Aug  9 11:11:26 2008
@@ -300,21 +300,16 @@
         */
        virtual const std::string& get_control_type() const = 0;
 
-       /** 
-        * Gets the best size for a single line label. 
-        *
-        * @param config_size         The wanted size.
+       /**
+        * Gets the best size for a text.
+        *
+        * @param minimum_size        The minimum size of the text.
+        * @param word_wrap           Are we allowed to wrap the lines.
+        *
         * @returns                   The best size.
         */
-       tpoint get_single_line_best_size(const tpoint& config_size) const;
-
-       /** 
-        * Gets the best size for a multiline line label. 
-        *
-        * @param config_size         The wanted size.
-        * @returns                   The best size.
-        */
-       tpoint get_multi_line_best_size(const tpoint& config_size) const;
+       tpoint get_best_text_size(const tpoint& minimum_size, 
+               const bool word_wrap) const;
 };
 
 } // namespace gui2

Modified: trunk/src/text.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/text.cpp?rev=28405&r1=28404&r2=28405&view=diff
==============================================================================
--- trunk/src/text.cpp (original)
+++ trunk/src/text.cpp Sat Aug  9 11:11:26 2008
@@ -86,6 +86,13 @@
        return gui2::tpoint(rect_.width, rect_.height);
 }
 
+bool ttext::is_truncated()
+{
+       recalculate();
+
+       return (pango_layout_is_ellipsized(layout_) == TRUE);
+}      
+
 ttext& ttext::set_text(const t_string& text, const bool markedup) 
 {
        if(markedup != markedup_text_ || text != text_) {

Modified: trunk/src/text.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/text.hpp?rev=28405&r1=28404&r2=28405&view=diff
==============================================================================
--- trunk/src/text.hpp (original)
+++ trunk/src/text.hpp Sat Aug  9 11:11:26 2008
@@ -60,6 +60,9 @@
 
        /** Returns the size needed for the text. */
        gui2::tpoint get_size() ;
+
+       /** Has the text been truncated? */
+       bool is_truncated();
 
        /***** ***** ***** ***** Setters ***** ***** ***** *****/
 




Related Messages


Powered by MHonArc, Updated Sat Aug 09 20:01:16 2008