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 #include "text_line.hxx"
23 #include <boost/foreach.hpp>
24 #include "text_view_drawing_set.hxx"
25 #include "text_element_plain.hxx"
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) {
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_) {
42 TextLine::~TextLine(){}
44 void TextLine::add_element(text_element::Plain* element) {
45 element_list_.push_back(element);
48 void TextLine::trim_right() {
49 if (element_list_.empty()) return;
51 element_list_.back().trim_right();
54 bool TextLine::empty() const {
55 return element_list_.empty();
58 void TextLine::layout(text_view::LayoutSet& set) {
59 width_ = set.x_end - set.x_start;
62 set.x_start += left_margin_;
63 set.x_end -= right_margin_;
66 BOOST_FOREACH(text_element::Plain& elem, element_list_) {
69 set.y += text_element::Plain::get_approximate_char_height(set.layout);
74 void TextLine::draw(text_view::DrawingSet& set) const {
75 BOOST_FOREACH(const text_element::Plain& elem, element_list_) {
80 const text_element::Plain* TextLine::get_text_element(
81 double x, double y_adj) const {
83 BOOST_FOREACH(const text_element::Plain& element, element_list_) {
84 if (element.is_xy_on_this(x, y_adj)) return &element;
89 const text_element::Plain* TextLine::get_nearest_text_element(
90 double& x, double& y_adj) const {
92 if (empty()) return 0;
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_));
98 BOOST_FOREACH(const text_element::Plain& element, element_list_) {
99 if (element.is_xy_near_to_this(x, y_adj)) return &element;
102 // the last element is the nearest element if not found.
103 return &element_list_.back();
107 } // namespace dialektos