span_layout.h

Go to the documentation of this file.
00001 /*
00002 **  ClanLib SDK
00003 **  Copyright (c) 1997-2011 The ClanLib Team
00004 **
00005 **  This software is provided 'as-is', without any express or implied
00006 **  warranty.  In no event will the authors be held liable for any damages
00007 **  arising from the use of this software.
00008 **
00009 **  Permission is granted to anyone to use this software for any purpose,
00010 **  including commercial applications, and to alter it and redistribute it
00011 **  freely, subject to the following restrictions:
00012 **
00013 **  1. The origin of this software must not be misrepresented; you must not
00014 **     claim that you wrote the original software. If you use this software
00015 **     in a product, an acknowledgment in the product documentation would be
00016 **     appreciated but is not required.
00017 **  2. Altered source versions must be plainly marked as such, and must not be
00018 **     misrepresented as being the original software.
00019 **  3. This notice may not be removed or altered from any source distribution.
00020 **
00021 **  Note: Some of the libraries ClanLib may link to may have additional
00022 **  requirements or restrictions.
00023 **
00024 **  File Author(s):
00025 **
00026 **    Magnus Norddahl
00027 */
00028 
00031 
00032 #pragma once
00033 
00034 #include "../../Core/System/sharedptr.h"
00035 #include "../../Core/Math/rect.h"
00036 #include "../../Core/Math/size.h"
00037 #include "color.h"
00038 
00039 class CL_Font;
00040 class CL_GraphicContext;
00041 class CL_Point;
00042 class CL_Size;
00043 class CL_SpanLayout_Impl;
00044 class CL_Image;
00045 class CL_SpanComponent;
00046 
00050 enum CL_SpanAlign
00051 {
00052         cl_left,
00053         cl_right,
00054         cl_center,
00055         cl_justify
00056 };
00057 
00061 class CL_SpanComponent
00062 {
00063 public:
00064         virtual ~CL_SpanComponent() { }
00065 
00069         virtual CL_Size get_size() const = 0;
00070 
00074         virtual void set_geometry(const CL_Rect &geometry) = 0;
00075 };
00076 
00080 template<typename T>
00081 class CL_SpanComponentBinder : public CL_SpanComponent
00082 {
00083 public:
00084 
00088         CL_SpanComponentBinder(T *component)
00089         : component(component)
00090         {
00091         }
00092 
00096         CL_Size get_size() const
00097         {
00098                 return component->get_size();
00099         }
00100 
00104         void set_geometry(const CL_Rect &geometry)
00105         {
00106                 component->set_geometry(geometry);
00107         }
00108 
00109 private:
00110         T *component;
00111 };
00112 
00116 class CL_SpanLayout
00117 {
00120 public:
00121         CL_SpanLayout();
00122         ~CL_SpanLayout();
00124 
00125         struct HitTestResult
00126         {
00127                 HitTestResult() : object_id(-1), offset(0) {}
00128 
00129                 enum Type
00130                 {
00131                         no_objects_available,
00132                         outside_top,
00133                         outside_left,
00134                         outside_right,
00135                         outside_bottom,
00136                         inside
00137                 } type;
00138 
00139                 int object_id;
00140                 int offset;
00141         };
00142 
00145 public:
00147         void clear();
00148 
00155         void add_text(const CL_String &text, const CL_Font &font, const CL_Colorf &color = CL_Colorf::white, int id = -1);
00156 
00162         void add_image(const CL_Image &image, int baseline_offset = 0, int id = -1);
00163 
00164         template<typename T>
00165 
00171         void add_component(T *component, int baseline_offset = 0, int id = -1)
00172         {
00173                 add_component_helper(new CL_SpanComponentBinder<T>(component), baseline_offset, id);
00174         }
00175 
00180         void layout(CL_GraphicContext &gc, int max_width);
00181 
00185         void set_position(const CL_Point &pos);
00186 
00190         CL_Size get_size() const;
00191 
00195         std::vector<CL_Rect> get_rect_by_id(int id) const;
00196 
00197 
00204         HitTestResult hit_test(CL_GraphicContext &gc, const CL_Point &pos);
00205 
00209         void draw_layout(CL_GraphicContext &gc);
00210 
00215         void draw_layout_ellipsis(CL_GraphicContext &gc, const CL_Rect &content_rect);
00216 
00218         void set_component_geometry();
00219 
00225         CL_Size find_preferred_size(CL_GraphicContext &gc);
00226 
00231         void set_selection_range(CL_String::size_type start, CL_String::size_type end);
00232 
00237         void set_selection_colors(const CL_Colorf &foreground, const CL_Colorf &background);
00238 
00240         void show_cursor();
00241 
00243         void hide_cursor();
00244 
00248         void set_cursor_pos(CL_String::size_type pos);
00249 
00253         void set_cursor_overwrite_mode(bool enable);
00254 
00258         void set_cursor_color(const CL_Colorf &color);
00259 
00263         CL_String get_combined_text() const;
00264 
00270         void set_align(CL_SpanAlign align);
00271 
00273         int get_first_baseline_offset();
00274 
00276         int get_last_baseline_offset();
00277 
00279 
00282 private:
00283 
00289         void add_component_helper(CL_SpanComponent *component, int baseline_offset, int id);
00290 
00291         CL_SharedPtr<CL_SpanLayout_Impl> impl;
00293 };