2 * Copyright (C) 2009 by Aiwota Programmer
3 * aiwotaprog@tetteke.tk
5 * This file is part of Dialektos.
7 * Dialektos is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * Dialektos is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Dialektos. If not, see <http://www.gnu.org/licenses/>.
21 #ifndef TEXT_ELEMENT_PLAIN_HXX
22 #define TEXT_ELEMENT_PLAIN_HXX
24 #include <glibmm/refptr.h>
25 #include <glibmm/ustring.h>
26 #include <gdkmm/cursor.h>
27 #include <gdkmm/rectangle.h>
28 #include <pangomm/layout.h>
29 #include <pangomm/attrlist.h>
30 #include <boost/range.hpp>
31 #include "text_element_char_size_cache.hxx"
43 /*! @brief namespace for text element */
44 namespace text_element {
49 // Rectangle(double x, double y, double width, double height):
50 // x_(x), y_(y), width_(width), height_(height) {}
52 // double get_x() const { return x_; }
53 // double get_y() const { return y_; }
54 // double get_width() const { return width_; }
55 // double get_height() const { return height_; }
56 // double get_x_right() const { return x_ + width_; }
57 // double get_y_bottom() const { return y_ + height_; }
67 /*! @brief Text element class for Plain text */
71 /*! @brief Constructor
73 * @param range is string represented by Boost.Range concepts
74 * @param bold is whether the text is bold or not
76 template <typename RangeT>
77 Plain(const RangeT& range, bool bold) :
78 str_(boost::begin(range), boost::end(range)),
79 bold_(bold), x_(0), y_(0), x_start_(0), x_end_(0) {}
83 /*! @brief For clone */
84 Plain* clone() const { return do_clone(); }
86 /*! @brief Remove right side white spaces */
89 /*! @brief return string
93 Glib::ustring get_text() const { return str_; }
95 /*! @brief return left-top position of this element.
97 * @return pair of x and y, y position is on the adjustment.
99 std::pair<gdouble, gdouble> get_xy() const { return std::make_pair(x_, y_); }
101 /*! @brief Calculate the position of characters.
103 * @param set is information sets for layout.
105 void layout(text_view::LayoutSet& set);
107 /*! @brief Draw the element.
109 * @param set is tool sets for drawing.
111 void draw(text_view::DrawingSet& set) const;
113 /*! @brief predicate whether (x, y) is on the element.
115 * @param x position on the widget.
116 * @param y position on the adjustment.
117 * @return true if (x, y) on the element, false if not.
119 bool is_xy_on_this(gdouble x, gdouble y) const;
121 /*! @brief predicate whether (x, y) is near to the element.
123 * @param x position on the widget.
124 * @param y position on the adjustment.
125 * @return true if (x, y) near to the element, false if not.
127 * Also returns true if (x, y) is on the spaces caused by character wrapping.
129 bool is_xy_near_to_this(gdouble x, gdouble y) const;
131 /*! @brief Gets a rectangle of a character under (x, y).
133 * @param x on the widget.
134 * @param y on the adjustment.
135 * @return the rectangle. y is on the adjustment.
137 Gdk::Rectangle xy_to_rectangle(gdouble x, gdouble y) const;
139 /*! @brief convert a character index from the x,y position.
141 * @param x position on the widget.
142 * @param y position on the adjustment.
144 int xy_to_index(gdouble x, gdouble y) const;
146 /*! @brief return a cursor type appropriated to the plain text element.
148 * @return the cursor type
150 virtual Gdk::CursorType get_cursor_type() const;
152 /*! @brief return an approximate character height.
154 * @param layout for calculating the character height.
155 * @return the approximate height.
157 static double get_approximate_char_height(Glib::RefPtr<const Pango::Layout> layout);
161 /*! @brief set attributes for drawing the plain text element.
165 virtual void set_attributes(Pango::AttrList& list) const;
168 virtual Plain* do_clone() const { return new Plain(*this); }
169 void get_selection_start_end_index(const Plain*, size_t, const Plain*, size_t,
170 size_t&, size_t&) const;
172 // static void initialize_char_width_cache();
176 gdouble x_; // top-left x
177 gdouble y_; // top-left y on the adjustment
181 static CharSizeCache char_size_cache;
182 static gdouble approximate_char_height;
185 /*! @brief clone the text element type. This function is for Boost.PtrContainer.
187 * @param rhs is copied object.
188 * @return new object.
190 inline Plain* new_clone(const Plain& rhs) { return rhs.clone(); }
193 } // namespace text_element
195 } // namespace dialektos