OSDN Git Service

Change idx sub dir's time stamp when saving idx.
[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/layout.h>
29 #include <pangomm/attrlist.h>
30 #include <boost/range.hpp>
31 #include "text_element_char_size_cache.hxx"
32
33
34 namespace dialektos {
35
36
37 namespace text_view {
38   class DrawingSet;
39   class LayoutSet;
40 }
41
42
43 /*! @brief namespace for text element */
44 namespace text_element {
45
46
47 //class Rectangle {
48 //public:
49 //  Rectangle(double x, double y, double width, double height):
50 //    x_(x), y_(y), width_(width), height_(height) {}
51 //
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_; }
58 //
59 //private:
60 //  double x_;
61 //  double y_;
62 //  double width_;
63 //  double height_;
64 //};
65
66
67 /*! @brief Text element class for Plain text */
68 class Plain {
69 public:
70
71   /*! @brief Constructor
72    *
73    * @param range is string represented by Boost.Range concepts
74    * @param bold is whether the text is bold or not
75    */
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) {}
80
81   virtual ~Plain() {}
82
83   /*! @brief For clone */
84   Plain* clone() const { return do_clone(); }
85
86   /*! @brief Remove right side white spaces */
87   void trim_right();
88
89   /*! @brief return string
90    *
91    * @return text
92    */
93   Glib::ustring get_text() const { return str_; }
94
95   /*! @brief return left-top position of this element.
96    *
97    * @return pair of x and y, y position is on the adjustment.
98    */
99   std::pair<gdouble, gdouble> get_xy() const { return std::make_pair(x_, y_); }
100
101   /*! @brief Calculate the position of characters.
102    *
103    * @param set is information sets for layout.
104    */
105   void layout(text_view::LayoutSet& set);
106
107   /*! @brief Draw the element.
108    *
109    * @param set is tool sets for drawing.
110    */
111   void draw(text_view::DrawingSet& set) const;
112
113   /*! @brief predicate whether (x, y) is on the element.
114    *
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.
118    */
119   bool is_xy_on_this(gdouble x, gdouble y) const;
120
121   /*! @brief predicate whether (x, y) is near to the element.
122    *
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.
126    *
127    * Also returns true if (x, y) is on the spaces caused by character wrapping.
128    */
129   bool is_xy_near_to_this(gdouble x, gdouble y) const;
130
131   /*! @brief Gets a rectangle of a character under (x, y).
132    *
133    * @param x on the widget.
134    * @param y on the adjustment.
135    * @return the rectangle. y is on the adjustment.
136    */
137   Gdk::Rectangle xy_to_rectangle(gdouble x, gdouble y) const;
138
139   /*! @brief convert a character index from the x,y position.
140
141    * @param x position on the widget.
142    * @param y position on the adjustment.
143    */
144   int xy_to_index(gdouble x, gdouble y) const;
145
146   /*! @brief return a cursor type appropriated to the plain text element.
147    *
148    * @return the cursor type
149    */
150   virtual Gdk::CursorType get_cursor_type() const;
151
152   /*! @brief return an approximate character height.
153    *
154    * @param layout for calculating the character height.
155    * @return the approximate height.
156    */
157   static double get_approximate_char_height(Glib::RefPtr<const Pango::Layout> layout);
158
159 protected:
160
161   /*! @brief set attributes for drawing the plain text element.
162    *
163    * @param list
164    */
165   virtual void set_attributes(Pango::AttrList& list) const;
166
167 private:
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;
171
172 //  static void initialize_char_width_cache();
173
174   Glib::ustring str_;
175   bool bold_;
176   gdouble x_;  // top-left x
177   gdouble y_;  // top-left y on the adjustment
178   gdouble x_start_;
179   gdouble x_end_;
180
181   static CharSizeCache char_size_cache;
182   static gdouble approximate_char_height;
183 };
184
185 /*! @brief clone the text element type. This function is for Boost.PtrContainer.
186  *
187  * @param rhs is copied object.
188  * @return new object.
189  */
190 inline Plain* new_clone(const Plain& rhs) { return rhs.clone(); }
191
192
193 } // namespace text_element
194
195 } // namespace dialektos
196
197 #endif