OSDN Git Service

Initial commit. Text view widget is implemented.
[fukui-no-namari/dialektos.git] / src / text_view_layoutable.cxx
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 #include "text_view_layoutable.hxx"
22
23 #include <boost/foreach.hpp>
24 #include "text_view_drawing_set.hxx"
25 #include "text_line.hxx"
26 #include "text_element_res_num_map.hxx"
27 #include "text_element_id_map.hxx"
28
29
30 namespace dialektos {
31
32
33 namespace text_view {
34
35
36 Layoutable::Layoutable():
37   Scrollable(), pango_layout_(create_pango_layout("")), line_list_(),
38       id_map_(new text_element::IDMap),
39       res_num_map_(new text_element::ResNumMap),
40       width_(-1) {
41 }
42
43 Layoutable::~Layoutable() {
44 }
45
46 void Layoutable::add_line(TextLine* line) {
47   line_list_.push_back(line);
48 }
49
50 void Layoutable::relayout() {
51   const int width = get_width();
52   LayoutSet set;
53   set.layout = pango_layout_;
54   set.y = 0;
55
56   BOOST_FOREACH(TextLine& line, line_list_) {
57     set.x = 0;
58     set.x_start = 0;
59     set.x_end = width;
60
61     line.layout(set);
62   }
63
64   width_ = width;
65   adjustment_.set_upper(set.y);
66 }
67
68 bool Layoutable::on_configure_event(GdkEventConfigure* event) {
69   if (width_ == -1)
70     relayout();
71   else if (width_ != get_width()) {
72     const gdouble value = adjustment_.get_value();
73     TextLine* first_line = 0;
74     gdouble y = 0;
75     BOOST_FOREACH(TextLine& line, line_list_) {
76       if (y + line.get_height() > value) {
77         first_line = &line;
78         break;
79       }
80       y += line.get_height();
81     }
82     gdouble delta = y - value;
83
84     relayout();
85
86     y = 0;
87     BOOST_FOREACH(TextLine& line, line_list_) {
88       if (&line == first_line) {
89         break;
90       }
91       y += line.get_height();
92     }
93     adjustment_.set_value(y - delta);
94   }
95   return Scrollable::on_configure_event(event);
96 }
97
98
99 const TextLine* Layoutable::get_text_line(const gdouble y_adj) const {
100
101   gdouble sum = 0;
102   BOOST_FOREACH(const TextLine& line, line_list_) {
103     const gdouble height = line.get_height();
104     if (sum + height > y_adj)
105       return &line;
106     sum += height;
107   }
108   return 0;
109 }
110
111 const TextLine* Layoutable::get_nearest_text_line(gdouble& y_adj) const {
112   if (line_list_.empty()) return 0;
113
114   if (y_adj < 0) {
115     y_adj = 0;
116     return &line_list_[0];
117   }
118   if (y_adj > adjustment_.get_upper()) {
119     y_adj = adjustment_.get_upper();
120     return &line_list_.back();
121   }
122
123   return get_text_line(y_adj);
124 }
125
126 const text_element::Plain* Layoutable::get_text_element(const gdouble x,
127     const gdouble y_adj) const {
128
129   const TextLine* line = get_text_line(y_adj);
130   if (!line) return 0;
131   return line->get_text_element(x, y_adj);
132 }
133
134 const text_element::Plain* Layoutable::get_nearest_text_element(
135     gdouble& x, gdouble& y_adj) const {
136
137   const TextLine* line = get_nearest_text_line(y_adj);
138   if (!line) return 0;
139   return line->get_nearest_text_element(x, y_adj);
140 }
141
142
143 } // namespace text_view
144
145 } // namespace dialektos