summaryrefslogtreecommitdiff
path: root/libs/gtkmm2ext/gtkmm2ext/cairocell.h
blob: 60a6f743d18d4aeab6c67dd4bf28ccbeece6e178 (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
  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 <boost/shared_ptr.hpp>

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

#include "gtkmm2ext/visibility.h"

class LIBGTKMM2EXT_API CairoCell
{
  public:
	CairoCell(int32_t id);
	virtual ~CairoCell() {}
	
	int32_t id() const { return _id; }

	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 (Cairo::RefPtr<Cairo::Context>&) {}

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

class LIBGTKMM2EXT_API CairoFontDescription {
  public:
	CairoFontDescription (const std::string& f,
			      Cairo::FontSlant s,
			      Cairo::FontWeight w,
			      double sz)
		: face (f)
		, _slant (s)
		, _weight (w)
		, _size (sz)
	{}
	CairoFontDescription (Pango::FontDescription&);

	void apply (Cairo::RefPtr<Cairo::Context> context) {
		context->select_font_face (face, _slant, _weight);
		context->set_font_size (_size);
	}

	void set_size (double sz) { _size = sz; }
	double size() const { return _size; }

       Cairo::FontSlant slant() const { return _slant; }
       void set_slant (Cairo::FontSlant sl) { _slant = sl; }

       Cairo::FontWeight weight() const { return _weight; }
       void set_weight (Cairo::FontWeight w) { _weight = w; }

  private:
	std::string face;
	Cairo::FontSlant _slant;
        Cairo::FontWeight _weight;
	double _size;
};

class LIBGTKMM2EXT_API CairoTextCell : public CairoCell
{
  public:
	CairoTextCell (int32_t id, double width_chars, boost::shared_ptr<CairoFontDescription> font = boost::shared_ptr<CairoFontDescription>());
	~CairoTextCell() {}

	virtual void set_size (Cairo::RefPtr<Cairo::Context>&);

	boost::shared_ptr<CairoFontDescription> font() const { return _font; }

	std::string get_text() const {
		return _text;
	}

	double width_chars() const { return _width_chars; }
	void render (Cairo::RefPtr<Cairo::Context>&);

  protected:
	friend class CairoEditableText;
	void set_width_chars (double wc) { _width_chars = wc; }
	void set_text (const std::string& txt);
	void set_font (boost::shared_ptr<CairoFontDescription> font) {
		_font = font;
	}

  protected:
	double _width_chars;
	std::string _text;
	boost::shared_ptr<CairoFontDescription> _font;
        double y_offset;
        double x_offset;
};

class LIBGTKMM2EXT_API CairoCharCell : public CairoTextCell
{
  public:
        CairoCharCell(int32_t id, char c);

        void set_size (Cairo::RefPtr<Cairo::Context>& context);
};

class LIBGTKMM2EXT_API CairoEditableText : public Gtk::Misc
{
public:
	CairoEditableText (boost::shared_ptr<CairoFontDescription> font  = boost::shared_ptr<CairoFontDescription>());
	~CairoEditableText ();

	void add_cell (CairoCell*);
	void clear_cells ();

	void start_editing (CairoCell*);
	void stop_editing ();

	void set_text (CairoTextCell* cell, const std::string&);
	void set_width_chars (CairoTextCell* cell, uint32_t);

	void set_draw_background (bool yn) { _draw_bg = yn; }
	
	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;
		queue_draw ();
	}

	double xpad() const { return _xpad; }
	void set_xpad (double x) { _xpad = x; queue_resize(); }
	double ypad() const { return _ypad; }
	void set_ypad (double y) { _ypad = y; queue_resize(); }
	
	double corner_radius() const { return _corner_radius; }
	void set_corner_radius (double r) { _corner_radius = r; queue_draw (); }

	boost::shared_ptr<CairoFontDescription> font() const { return _font; }
	void set_font (boost::shared_ptr<CairoFontDescription> font);
	void set_font (Pango::FontDescription& font);

	sigc::signal<bool,GdkEventScroll*,CairoCell*> scroll;
	sigc::signal<bool,GdkEventButton*,CairoCell*> button_press;
	sigc::signal<bool,GdkEventButton*,CairoCell*> button_release;

protected:
	bool on_expose_event (GdkEventExpose*);
	bool on_button_press_event (GdkEventButton*);
	bool on_button_release_event (GdkEventButton*);
	void on_size_request (GtkRequisition*);
	bool on_focus_in_event (GdkEventFocus*);
	bool on_focus_out_event (GdkEventFocus*);
	bool on_scroll_event (GdkEventScroll*);
        void on_size_allocate (Gtk::Allocation&);

private:
	typedef std::vector<CairoCell*> CellMap;

	CellMap cells;
	boost::shared_ptr<CairoFontDescription> _font;
	CairoCell* editing_cell;
	bool _draw_bg;
	double max_cell_width;
	double max_cell_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);
	void queue_draw_cell (CairoCell* target);
        void position_cells_and_get_bbox (GdkRectangle&);
        void set_cell_sizes (); 
};

#endif /* __libgtmm2ext_cairocell_h__ */