summaryrefslogtreecommitdiff
path: root/libs/gtkmm2ext/gtkmm2ext/cairocell.h
blob: dde4e687f780b61b259366696800cc94d77aa294 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
    Copyright (C) 2011 Paul Davis

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#ifndef __libgtmm2ext_cairocell_h__
#define __libgtmm2ext_cairocell_h__

#include <map>

#include <stdint.h>
#include <cairomm/cairomm.h>
#include <gtkmm.h>

class CairoCell 
{
  public:
    CairoCell();
    virtual ~CairoCell() {}

    virtual void render (Cairo::RefPtr<Cairo::Context>&) = 0;

    double x() const { return bbox.x; }
    double y() const { return bbox.y; }
    double width() const { return bbox.width; }
    double height() const { return bbox.height; }

    void set_position (double x, double y) {
            bbox.x = x;
            bbox.y = y;
    }

    bool intersects (GdkRectangle& r) const {
            return gdk_rectangle_intersect (&r, &bbox, 0);
    }

    bool covers (double x, double y) const {
            return bbox.x <= x && bbox.x + bbox.width > x &&
                    bbox.y <= y && bbox.y + bbox.height > y;
    }

    double xpad() const { return _xpad; }
    void   set_xpad (double x) { _xpad = x; }

    void set_visible (bool yn) { _visible = yn; }
    bool visible() const { return _visible; }
    virtual void set_size (Glib::RefPtr<Pango::Context>&, const Pango::FontDescription&) {}

  protected:
    GdkRectangle bbox;
    bool _visible;
    uint32_t _xpad;
};

class CairoBarCell : public CairoCell 
{
  public:
    CairoBarCell() {};
    
    void render (Cairo::RefPtr<Cairo::Context>& context) {
            context->move_to (bbox.x, bbox.y); 
            context->set_line_width (bbox.width);
            context->rel_line_to (0, bbox.height);
            context->stroke ();
    }

    void set_size (Glib::RefPtr<Pango::Context>& context, const Pango::FontDescription& font) {
        Pango::FontMetrics metrics = context->get_metrics (font);
        bbox.width = 2;
        bbox.height = (metrics.get_ascent() + metrics.get_descent()) / PANGO_SCALE;
    }

  private:
};

class CairoColonCell : public CairoCell 
{
  public:
    CairoColonCell() {};
    
    void render (Cairo::RefPtr<Cairo::Context>& context) {
            /* two very small circles, at 1/3 and 2/3 of the height */
            context->arc (bbox.x, bbox.y + (bbox.height/3.0), 1.5, 0.0, M_PI*2.0);
            context->fill ();
            context->arc (bbox.x, bbox.y + (2.0 * bbox.height/3.0), 1.5, 0.0, M_PI*2.0);
            context->fill ();
    }

    void set_size (Glib::RefPtr<Pango::Context>& context, const Pango::FontDescription& font) {
        Pango::FontMetrics metrics = context->get_metrics (font);
        bbox.width = 3.0;
        bbox.height = (metrics.get_ascent() + metrics.get_descent()) / PANGO_SCALE;
    }

  private:
};

class CairoTextCell : public CairoCell
{
  public:
    CairoTextCell (uint32_t width_chars);

    void set_size (Glib::RefPtr<Pango::Context>&, const Pango::FontDescription&);

    void   set_text (const std::string& txt) {
            layout->set_text (txt); 
    }
    std::string get_text() const {
            return layout->get_text ();
    }
    uint32_t width_chars() const { return _width_chars; }

    void render (Cairo::RefPtr<Cairo::Context>&);

  protected:
    uint32_t _width_chars;
    Glib::RefPtr<Pango::Layout> layout;
};

class CairoEditableText : public Gtk::Misc
{
  public:
    CairoEditableText ();
    
    void add_cell (uint32_t id, CairoCell*);
    CairoCell* get_cell (uint32_t id);

    void set_text (uint32_t id, const std::string& text);
    
    void set_colors (double cr, double cg, double cb, double ca) {
            r = cr;
            g = cg;
            b = cb;
            a = ca;
            queue_draw ();
    }

    void set_edit_colors (double cr, double cg, double cb, double ca) {
            edit_r = cr;
            edit_g = cg;
            edit_b = cb;
            edit_a = ca;
            queue_draw ();
    }

    void set_bg (double r, double g, double b, double a) {
            bg_r = r;
            bg_g = g;
            bg_b = b;
            bg_a = a;
    }

    bool on_expose_event (GdkEventExpose*);
    bool on_key_press_event (GdkEventKey*);
    bool on_key_release_event (GdkEventKey*);
    bool on_button_press_event (GdkEventButton*);
    bool on_button_release_event (GdkEventButton*);
    void on_size_request (GtkRequisition*);
    void on_size_allocate (Gtk::Allocation&);
    bool on_focus_in_event (GdkEventFocus*);
    bool on_focus_out_event (GdkEventFocus*);

    void set_font (const std::string& str) {
            font = Pango::FontDescription (str);
    }

  private:
    typedef std::map<uint32_t,CairoCell*> CellMap;

    CellMap cells;
    Pango::FontDescription font;
    uint32_t editing_id;
    uint32_t editing_pos;
    double width;
    double max_cell_height;
    double height;
    double corner_radius;
    double xpad;
    double ypad;
    double r;
    double g;
    double b;
    double a;
    double edit_r;
    double edit_g;
    double edit_b;
    double edit_a;
    double bg_r;
    double bg_g;
    double bg_b;
    double bg_a;


    CairoCell* find_cell (uint32_t x, uint32_t y, uint32_t& cell_id);
    void edit_next_cell ();
    void queue_draw_cell (CairoCell* target);
    void set_text (CairoTextCell* cell, const std::string&);
};

#endif /* __libgtmm2ext_cairocell_h__ */