summaryrefslogtreecommitdiff
path: root/gtk/gtk_plotters.c
blob: 86ba56fa9e2618806394a333d6defe18c6c21cf8 (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
/*

 * This file is part of NetSurf, http://netsurf.sourceforge.net/

 * Licensed under the GNU General Public License,

 *                http://www.opensource.org/licenses/gpl-license

 * Copyright 2004 James Bursa <bursa@users.sourceforge.net>

 */

/** \file

 * Target independent plotting (GDK / GTK+  implementation).

 */

#include <gdk/gdk.h>

#include <gtk/gtk.h>

#include "netsurf/desktop/plotters.h"

#include "netsurf/render/font.h"

#include "netsurf/utils/log.h"



extern GtkWidget *current_widget;
extern GdkDrawable *current_drawable;
extern GdkGC *current_gc;


static bool nsgtk_plot_clg(colour c);
static bool nsgtk_plot_rectangle(int x0, int y0, int width, int height,
		colour c, bool dotted);
static bool nsgtk_plot_line(int x0, int y0, int x1, int y1, int width,
		colour c, bool dotted, bool dashed);
static bool nsgtk_plot_polygon(int *p, unsigned int n, colour fill);
static bool nsgtk_plot_fill(int x0, int y0, int x1, int y1, colour c);
static bool nsgtk_plot_clip(int clip_x0, int clip_y0,
		int clip_x1, int clip_y1);
static bool nsgtk_plot_text(int x, int y, struct font_data *font,
		const char *text, size_t length, colour bg, colour c);
static bool nsgtk_plot_disc(int x, int y, int radius, colour colour);
static bool nsgtk_plot_bitmap(int x, int y, int width, int height,
		struct bitmap *bitmap, colour bg);
static bool nsgtk_plot_bitmap_tile(int x, int y, int width, int height,
		struct bitmap *bitmap, colour bg,
		bool repeat_x, bool repeat_y);
static void nsgtk_set_colour(colour c);


struct plotter_table plot;

const struct plotter_table nsgtk_plotters = {
	nsgtk_plot_clg,
	nsgtk_plot_rectangle,
	nsgtk_plot_line,
	nsgtk_plot_polygon,
	nsgtk_plot_fill,
	nsgtk_plot_clip,
	nsgtk_plot_text,
	nsgtk_plot_disc,
	nsgtk_plot_bitmap,
	nsgtk_plot_bitmap_tile
};


bool nsgtk_plot_clg(colour c)
{
	return true;
}


bool nsgtk_plot_rectangle(int x0, int y0, int width, int height,
		colour c, bool dotted)
{
	nsgtk_set_colour(c);
	gdk_draw_rectangle(current_drawable, current_gc,
			FALSE, x0, y0, width, height);
	return true;
}


bool nsgtk_plot_line(int x0, int y0, int x1, int y1, int width,
		colour c, bool dotted, bool dashed)
{
	nsgtk_set_colour(c);
	gdk_draw_line(current_drawable, current_gc,
			x0, y0, x1, y1);
	return true;
}


bool nsgtk_plot_polygon(int *p, unsigned int n, colour fill)
{
	unsigned int i;
	GdkPoint q[n];
	for (i = 0; i != n; i++) {
		q[i].x = p[i * 2];
		q[i].y = p[i * 2 + 1];
	}
	nsgtk_set_colour(fill);
	gdk_draw_polygon(current_drawable, current_gc,
			TRUE, q, n);
	return true;
}


bool nsgtk_plot_fill(int x0, int y0, int x1, int y1, colour c)
{
	nsgtk_set_colour(c);
	gdk_draw_rectangle(current_drawable, current_gc,
			TRUE, x0, y0, x1 - x0, y1 - y0);
	return true;
}


bool nsgtk_plot_clip(int clip_x0, int clip_y0,
		int clip_x1, int clip_y1)
{
	GdkRectangle clip = { clip_x0, clip_y0,
		clip_x1 - clip_x0 + 1, clip_y1 - clip_y0 + 1 };
	gdk_gc_set_clip_rectangle(current_gc, &clip);
	return true;
}


bool nsgtk_plot_text(int x, int y, struct font_data *font,
		const char *text, size_t length, colour bg, colour c)
{
	PangoContext *context;
	PangoLayout *layout;
	PangoLayoutLine *line;
	GdkColor colour = { 0,
			((c & 0xff) << 8) | (c & 0xff),
			(c & 0xff00) | (c & 0xff00 >> 8),
			((c & 0xff0000) >> 8) | (c & 0xff0000 >> 16) };

	context = gtk_widget_get_pango_context(current_widget);
	layout = pango_layout_new(context);
	pango_layout_set_font_description(layout,
			(const PangoFontDescription *) font->id);
	pango_layout_set_text(layout, text, length);

	line = pango_layout_get_line(layout, 0);

	gdk_draw_layout_line_with_colors(current_drawable, current_gc,
			x, y, line, &colour, 0);

	g_object_unref(layout);

	return true;
}


bool nsgtk_plot_disc(int x, int y, int radius, colour colour)
{
	return true;
}


bool nsgtk_plot_bitmap(int x, int y, int width, int height,
		struct bitmap *bitmap, colour bg)
{
	GdkPixbuf *pixbuf = (GdkPixbuf *) bitmap;

	if (gdk_pixbuf_get_width(pixbuf) == width &&
			gdk_pixbuf_get_height(pixbuf) == height) {
		gdk_draw_pixbuf(current_drawable, current_gc,
				pixbuf,
				0, 0,
				x, y,
				width, height,
				GDK_RGB_DITHER_NORMAL, 0, 0);

	} else {
		GdkPixbuf *scaled;
		scaled = gdk_pixbuf_scale_simple(pixbuf,
				width, height,
				GDK_INTERP_BILINEAR);
		if (!scaled)
			return false;

		gdk_draw_pixbuf(current_drawable, current_gc,
				scaled,
				0, 0,
				x, y,
				width, height,
				GDK_RGB_DITHER_NORMAL, 0, 0);

		g_object_unref(scaled);
	}

	return true;
}


bool nsgtk_plot_bitmap_tile(int x, int y, int width, int height,
		struct bitmap *bitmap, colour bg,
		bool repeat_x, bool repeat_y)
{
	return true;
}


void nsgtk_set_colour(colour c)
{
	int r, g, b;
	GdkColor colour;

	r = c & 0xff;
	g = (c & 0xff00) >> 8;
	b = (c & 0xff0000) >> 16;

	colour.red = r | (r << 8);
	colour.green = g | (g << 8);
	colour.blue = b | (b << 8);
	colour.pixel = (r << 16) | (g << 8) | b;

	gdk_color_alloc(gtk_widget_get_colormap(current_widget),
			&colour);
	gdk_gc_set_foreground(current_gc, &colour);
}