OSDN Git Service

Initial commit. Text view widget is implemented.
[fukui-no-namari/dialektos.git] / src / text_line.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_line.hxx"
22
23 #include <boost/foreach.hpp>
24 #include "text_view_drawing_set.hxx"
25 #include "text_element_plain.hxx"
26
27
28 namespace dialektos {
29
30
31 TextLine::TextLine(int res_num, int l_mgn) :
32   res_num_(res_num), element_list_(), height_(12), y_(0), width_(0),
33   left_margin_(l_mgn), right_margin_(1) {
34 }
35
36 TextLine::TextLine(const TextLine& rhs) :
37   res_num_(rhs.res_num_), element_list_(rhs.element_list_),
38   height_(rhs.height_), y_(rhs.y_), width_(rhs.width_),
39   left_margin_(rhs.left_margin_), right_margin_(rhs.right_margin_) {
40 }
41
42 TextLine::~TextLine(){}
43
44 void TextLine::add_element(text_element::Plain* element) {
45   element_list_.push_back(element);
46 }
47
48 void TextLine::trim_right() {
49   if (element_list_.empty()) return;
50
51   element_list_.back().trim_right();
52 }
53
54 bool TextLine::empty() const {
55   return element_list_.empty();
56 }
57
58 void TextLine::layout(text_view::LayoutSet& set) {
59   width_ = set.x_end - set.x_start;
60   y_ = set.y;
61
62   set.x_start += left_margin_;
63   set.x_end -= right_margin_;
64   set.x = set.x_start;
65
66   BOOST_FOREACH(text_element::Plain& elem, element_list_) {
67     elem.layout(set);
68   }
69   set.y += text_element::Plain::get_approximate_char_height(set.layout);
70
71   height_ = set.y - y_;
72 }
73
74 void TextLine::draw(text_view::DrawingSet& set) const {
75   BOOST_FOREACH(const text_element::Plain& elem, element_list_) {
76     elem.draw(set);
77   }
78 }
79
80 const text_element::Plain* TextLine::get_text_element(
81     double x, double y_adj) const {
82
83   BOOST_FOREACH(const text_element::Plain& element, element_list_) {
84     if (element.is_xy_on_this(x, y_adj)) return &element;
85   }
86   return 0;
87 }
88
89 const text_element::Plain* TextLine::get_nearest_text_element(
90     double& x, double& y_adj) const {
91
92   if (empty()) return 0;
93
94   x = std::min(double(width_-right_margin_),
95       std::max(x, double(left_margin_)));
96   y_adj = std::min(y_ + height_, std::max(y_adj, y_));
97
98   BOOST_FOREACH(const text_element::Plain& element, element_list_) {
99     if (element.is_xy_near_to_this(x, y_adj)) return &element;
100   }
101
102   // the last element is the nearest element if not found.
103   return &element_list_.back();
104 }
105
106
107 } // namespace dialektos