OSDN Git Service

Support yy kakiko.
[fukui-no-namari/dialektos.git] / src / text_element_plain.hxx
1 /*
2  * Copyright (C) 2009 by Aiwota Programmer
3  * aiwotaprog@tetteke.tk
4  *
5  * This file is part of Dialektos.
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
21 #ifndef TEXT_ELEMENT_PLAIN_HXX
22 #define TEXT_ELEMENT_PLAIN_HXX
23
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"
33
34
35 namespace dialektos {
36
37
38 namespace text_view {
39   class DrawingSet;
40   class LayoutSet;
41   class GetSelectedSet;
42 }
43
44
45 /*! @brief namespace for text element */
46 namespace text_element {
47
48
49 //class Rectangle {
50 //public:
51 //  Rectangle(double x, double y, double width, double height):
52 //    x_(x), y_(y), width_(width), height_(height) {}
53 //
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_; }
60 //
61 //private:
62 //  double x_;
63 //  double y_;
64 //  double width_;
65 //  double height_;
66 //};
67
68
69 /*! @brief Text element class for Plain text */
70 class Plain {
71 public:
72
73   /*! @brief Constructor
74    *
75    * @param range is string represented by Boost.Range concepts
76    * @param bold is whether the text is bold or not
77    */
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_() {}
82
83   virtual ~Plain() {}
84
85   /*! @brief For clone */
86   Plain* clone() const { return do_clone(); }
87
88   /*! @brief Remove right side white spaces */
89   void trim_right();
90
91   /*! @brief return string
92    *
93    * @return text
94    */
95   Glib::ustring get_text() const { return str_; }
96
97   /*! @brief return selected string
98    *
99    * @return selected text
100    */
101   Glib::ustring get_selected_text(const text_view::GetSelectedSet& set) const;
102
103   /*! @brief return left-top position of this element.
104    *
105    * @return pair of x and y, y position is on the adjustment.
106    */
107   std::pair<gdouble, gdouble> get_xy() const { return std::make_pair(x_, y_); }
108
109   /*! @brief Calculate the position of characters.
110    *
111    * @param set is information sets for layout.
112    */
113   void layout(text_view::LayoutSet& set);
114
115   /*! @brief Draw the element.
116    *
117    * @param set is tool sets for drawing.
118    */
119   void draw(text_view::DrawingSet& set) const;
120
121   /*! @brief predicate whether (x, y) is on the element.
122    *
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.
126    */
127   bool is_xy_on_this(gdouble x, gdouble y) const;
128
129   /*! @brief predicate whether (x, y) is near to the element.
130    *
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.
134    *
135    * Also returns true if (x, y) is on the spaces caused by character wrapping.
136    */
137   bool is_xy_near_to_this(gdouble x, gdouble y) const;
138
139   /*! @brief Gets a rectangle of a character under (x, y).
140    *
141    * @param x on the widget.
142    * @param y on the adjustment.
143    * @return the rectangle. y is on the adjustment.
144    */
145   Gdk::Rectangle xy_to_rectangle(gdouble x, gdouble y) const;
146
147   /*! @brief convert a character index from the x,y position.
148
149    * @param x position on the widget.
150    * @param y position on the adjustment.
151    */
152   int xy_to_index(gdouble x, gdouble y) const;
153
154   /*! @brief return a cursor type appropriated to the plain text element.
155    *
156    * @return the cursor type
157    */
158   virtual Gdk::CursorType get_cursor_type() const;
159
160   void itemize(const Glib::RefPtr<Pango::Context>&, const Pango::FontMetrics&);
161
162   /*! @brief return an approximate character height.
163    *
164    * @param layout for calculating the character height.
165    * @return the approximate height.
166    */
167   static double get_approximate_char_height(const Pango::FontMetrics&);
168
169 protected:
170
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;
174
175   Pango::FontMetrics get_metrics() const;
176
177 private:
178   virtual Plain* do_clone() const { return new Plain(*this); }
179
180   void do_draw(text_view::DrawingSet&, const Pango::Item&,
181       const Pango::GlyphString&, double x, double y,
182       bool in_selection) const;
183
184   void get_selection_start_end_index(const Plain*, size_t, const Plain*, size_t,
185       size_t&, size_t&) const;
186
187 //  static void initialize_char_width_cache();
188
189   Glib::ustring str_;
190   bool bold_;
191   gdouble x_;  // top-left x
192   gdouble y_;  // top-left y on the adjustment
193   gdouble x_start_;
194   gdouble x_end_;
195
196   std::vector<Pango::Item> items_;
197
198   Pango::FontMetrics metrics_;
199
200   static CharSizeCache char_size_cache;
201 };
202
203 /*! @brief clone the text element type. This function is for Boost.PtrContainer.
204  *
205  * @param rhs is copied object.
206  * @return new object.
207  */
208 inline Plain* new_clone(const Plain& rhs) { return rhs.clone(); }
209
210
211 } // namespace text_element
212
213 } // namespace dialektos
214
215 #endif