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/context.h>
29 #include <pangomm/item.h>
30 #include <pangomm/fontmetrics.h>
31 #include <boost/range.hpp>
32 #include "text_element_char_size_cache.hxx"
45 /*! @brief namespace for text element */
46 namespace text_element {
51 // Rectangle(double x, double y, double width, double height):
52 // x_(x), y_(y), width_(width), height_(height) {}
54 // double get_x() const { return x_; }
55 // double get_y() const { return y_; }
56 // double get_width() const { return width_; }
57 // double get_height() const { return height_; }
58 // double get_x_right() const { return x_ + width_; }
59 // double get_y_bottom() const { return y_ + height_; }
69 /*! @brief Text element class for Plain text */
73 /*! @brief Constructor
75 * @param range is string represented by Boost.Range concepts
76 * @param bold is whether the text is bold or not
78 template <typename RangeT>
79 Plain(const RangeT& range, bool bold) :
80 str_(boost::begin(range), boost::end(range)),
81 bold_(bold), x_(0), y_(0), x_start_(0), x_end_(0), items_(), metrics_() {}
85 /*! @brief For clone */
86 Plain* clone() const { return do_clone(); }
88 /*! @brief Remove right side white spaces */
91 /*! @brief return string
95 Glib::ustring get_text() const { return str_; }
97 /*! @brief return selected string
99 * @return selected text
101 Glib::ustring get_selected_text(const text_view::GetSelectedSet& set) const;
103 /*! @brief return left-top position of this element.
105 * @return pair of x and y, y position is on the adjustment.
107 std::pair<gdouble, gdouble> get_xy() const { return std::make_pair(x_, y_); }
109 /*! @brief Calculate the position of characters.
111 * @param set is information sets for layout.
113 void layout(text_view::LayoutSet& set);
115 /*! @brief Draw the element.
117 * @param set is tool sets for drawing.
119 void draw(text_view::DrawingSet& set) const;
121 /*! @brief predicate whether (x, y) is on the element.
123 * @param x position on the widget.
124 * @param y position on the adjustment.
125 * @return true if (x, y) on the element, false if not.
127 bool is_xy_on_this(gdouble x, gdouble y) const;
129 /*! @brief predicate whether (x, y) is near to the element.
131 * @param x position on the widget.
132 * @param y position on the adjustment.
133 * @return true if (x, y) near to the element, false if not.
135 * Also returns true if (x, y) is on the spaces caused by character wrapping.
137 bool is_xy_near_to_this(gdouble x, gdouble y) const;
139 /*! @brief Gets a rectangle of a character under (x, y).
141 * @param x on the widget.
142 * @param y on the adjustment.
143 * @return the rectangle. y is on the adjustment.
145 Gdk::Rectangle xy_to_rectangle(gdouble x, gdouble y) const;
147 /*! @brief convert a character index from the x,y position.
149 * @param x position on the widget.
150 * @param y position on the adjustment.
152 int xy_to_index(gdouble x, gdouble y) const;
154 /*! @brief return a cursor type appropriated to the plain text element.
156 * @return the cursor type
158 virtual Gdk::CursorType get_cursor_type() const;
160 void itemize(const Glib::RefPtr<Pango::Context>&, const Pango::FontMetrics&);
162 /*! @brief return an approximate character height.
164 * @param layout for calculating the character height.
165 * @return the approximate height.
167 static double get_approximate_char_height(const Pango::FontMetrics&);
171 virtual void do_draw_glyphs(text_view::DrawingSet&, const Pango::Item&,
172 const Pango::GlyphString&, double x, double y,
173 bool in_selection) const;
175 Pango::FontMetrics get_metrics() const;
178 virtual Plain* do_clone() const { return new Plain(*this); }
180 void do_draw(text_view::DrawingSet&, const Pango::Item&,
181 const Pango::GlyphString&, double x, double y,
182 bool in_selection) const;
184 void get_selection_start_end_index(const Plain*, size_t, const Plain*, size_t,
185 size_t&, size_t&) const;
187 // static void initialize_char_width_cache();
191 gdouble x_; // top-left x
192 gdouble y_; // top-left y on the adjustment
196 std::vector<Pango::Item> items_;
198 Pango::FontMetrics metrics_;
200 static CharSizeCache char_size_cache;
203 /*! @brief clone the text element type. This function is for Boost.PtrContainer.
205 * @param rhs is copied object.
206 * @return new object.
208 inline Plain* new_clone(const Plain& rhs) { return rhs.clone(); }
211 } // namespace text_element
213 } // namespace dialektos