OSDN Git Service

Initial commit. Text view widget is implemented.
[fukui-no-namari/dialektos.git] / src / text_view_drawable.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_drawable.hxx"
22
23 #include <glibmm/refptr.h>
24 #include <gdkmm/gc.h>
25 #include <gdkmm/drawable.h>
26 #include <gtkmm/style.h>
27 #include <boost/foreach.hpp>
28 #include "text_line.hxx"
29 #include "text_element_plain.hxx"
30 #include "text_view_drawing_set.hxx"
31
32
33 namespace dialektos {
34
35
36 namespace text_view {
37
38
39 Drawable::Drawable(): PointerTrackable() {}
40
41 Drawable::~Drawable() {}
42
43 bool Drawable::on_expose_event(GdkEventExpose* event) {
44   const double value = adjustment_.get_value();
45   Glib::RefPtr<Gdk::Drawable> window = get_window();
46   Glib::RefPtr<const Gtk::Style> style = get_style();
47   Glib::RefPtr<const Gdk::GC> gc = style->get_base_gc(Gtk::STATE_NORMAL);
48   window->draw_rectangle(gc, true, 0, 0, get_width(), get_height());
49
50   DrawingSet set;
51   set.window = window;
52   set.layout = pango_layout_;
53   set.style = style;
54   set.adj_value = value;
55   set.start_element = pressed_element_;
56   set.start_index = pressed_index_;
57   set.end_element = hovered_element_;
58   set.end_index = hovered_index_;
59
60   if (pressed_element_ && hovered_element_) {
61     std::pair<gdouble, gdouble> pressed_xy = pressed_element_->get_xy();
62     std::pair<gdouble, gdouble> hovered_xy = hovered_element_->get_xy();
63
64     if ((pressed_element_ == hovered_element_
65         && pressed_index_ > hovered_index_) ||
66         (pressed_xy.second == hovered_xy.second
67             && pressed_xy.first > hovered_xy.first) ||
68             (pressed_xy.second > hovered_xy.second)) {
69       std::swap(set.start_element, set.end_element);
70       std::swap(set.start_index, set.end_index);
71     }
72   }
73
74   const int area_bottom = get_height() + value;
75
76   BOOST_FOREACH(TextLine& line, line_list_) {
77     if (line.get_y() > area_bottom) break;
78     if (line.get_btm_y() >= value) line.draw(set);
79   }
80   return true;
81 }
82
83
84 } // namespace text_view
85
86
87 } // namespace dialektos