File: viewer.c

package info (click to toggle)
picview 0.8-4
  • links: PTS
  • area: main
  • in suites: woody
  • size: 604 kB
  • ctags: 98
  • sloc: ansic: 1,496; sh: 175; makefile: 75
file content (273 lines) | stat: -rw-r--r-- 6,501 bytes parent folder | download
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include "viewer.h"
#include <stdio.h>

/* closes window w and destroys any static reference.
 * i.e. the same window used in fullscreen mode */
void close_window(GtkWidget *w, gpointer *data)
{
	/* get the image if this is viewed in a toplevel window */
	GdkImlibImage *im = NULL;
	im = (GdkImlibImage *) gtk_object_get_data(GTK_OBJECT(w), "image");
	if (im)
    	gdk_imlib_destroy_image(im);

	gtk_widget_destroy(w);
	if (data)
		*data = NULL;
}

/* handles mouse click events for fullscreen and windowed images.*/
void click_handler(GtkWidget *w, GdkEventButton *event, gpointer *data)
{
	switch (event->button)
	{
		case 2:
			close_window(w, data);
			break;
		case 1:
			change_image(w, 1);
			break;
		case 3:
			change_image(w, -1);
			break;
	}
}

/* handles key press events for fullscreen and windowed images.*/
int key_handler(GtkWidget *w, GdkEventKey *event, gpointer *data)
{
	switch (event->keyval)
	{
		case GDK_q:
		case GDK_Q:
		case GDK_Escape:
			close_window(w, data);
			return TRUE;
		case GDK_n:
		case GDK_N:
		case GDK_space:
			change_image(w, 1);
			return TRUE;
		case GDK_p:
		case GDK_P:
			change_image(w, -1);
			return TRUE;
		default:
			break;
	}
	return FALSE;
}

void image_resize_cb(GtkWidget *widget, GdkEventConfigure *event)
{
	gint w, h;
	static gint pw = -1, ph = -1;
	GtkWidget *a;
	GdkImlibImage *im;
	GdkPixmap *pmap;

	w = event->width;
	h = event->height;

	if ((pw == w) && (ph == h))
	  return;

	pw = w;
	ph = h;

	im = (GdkImlibImage*) gtk_object_get_data(GTK_OBJECT(widget), "image");
	a = (GtkWidget*) gtk_object_get_data(GTK_OBJECT(widget), "view");
	if (!a || !im)
		return;

	gdk_imlib_render(im, w, h);
	pmap = gdk_imlib_move_image(im);
	gdk_window_set_back_pixmap(a->window, pmap, FALSE);
	gdk_window_clear(a->window);
	gdk_imlib_free_pixmap(pmap);
}


/* moves to the next or previous image in the list,
 * which is data attached to win */
void change_image(GtkWidget *win, int change)
{
	GList *list, *new;

	list = (GList*) gtk_object_get_user_data(GTK_OBJECT(win));

	if (list)
	{
		if (change > 0)
				new = list->next;
		else
				new = list->prev;

		if (new)
		{
			gtk_object_set_user_data(GTK_OBJECT(win), (gpointer) new);
			view_image_fullscreen((gchar*)new->data, NULL);
		}
	}
}

/* list contains a list of files to view */
void view_images(GList *list)
{
	GtkWidget *win = NULL;
	GtkWidget *draw_area;
	int screen_x, screen_y;

	if (list)
	{
		screen_x=gdk_screen_width();
		screen_y=gdk_screen_height();

		draw_area = gtk_drawing_area_new();
		gtk_drawing_area_size (GTK_DRAWING_AREA (draw_area), screen_x, screen_y);
		gtk_widget_show(draw_area);

		win = gtk_window_new(GTK_WINDOW_POPUP);
		gtk_window_set_modal(GTK_WINDOW(win), TRUE);

		gtk_signal_connect( GTK_OBJECT (win), "delete_event",
					GTK_SIGNAL_FUNC (close_window), &win);
		gtk_widget_set_events(win, GDK_BUTTON_PRESS_MASK);
		gtk_signal_connect( GTK_OBJECT (win), "key_press_event",
					GTK_SIGNAL_FUNC (key_handler), &win);
		gtk_signal_connect(GTK_OBJECT(win), "button_press_event",
					GTK_SIGNAL_FUNC (click_handler), &win);
		gtk_container_set_border_width( GTK_CONTAINER (win), 0 );

		gtk_widget_set_usize(win, screen_x, screen_y);
		gtk_widget_show( win );
		gtk_container_add(GTK_CONTAINER(win), draw_area);

		gtk_object_set_user_data(GTK_OBJECT(win), (gpointer) list);
	
		while (gtk_events_pending())
			gtk_main_iteration();

		if (win != NULL)
			view_image_fullscreen(list->data, draw_area);
	}
}

/* views a single file in the draw_area */
GtkWidget *view_image_fullscreen(gchar *file, GtkWidget *da)
{
	static GtkWidget *draw_area;
	GdkImlibImage *im;	
	GdkPixmap *pmap;
	int width, height, screen_x, screen_y, win_x, win_y;
	double wh_ratio = 1;

	if (da != NULL)
		draw_area = da;

	im = gdk_imlib_load_image(file);

	if (!im)
		return NULL;

	width = im->rgb_width;
	height = im->rgb_height;
	screen_x=gdk_screen_width();
	screen_y=gdk_screen_height();

	/* deal with images larger than the screen */
	wh_ratio = (double) width / (double)height;
	if (width > screen_x)
	{
		width = screen_x;
		height = width / wh_ratio;
	}
	if (height > screen_y)
	{
		height = screen_y;
		width = height * wh_ratio;
	}

	win_x=(screen_x - width) / 2;
	win_y=(screen_y - height) / 2;

	gdk_imlib_render(im, width, height);
	pmap = gdk_imlib_move_image(im);
	gdk_window_set_background(draw_area->window, &draw_area->style->black);
	gdk_window_clear(draw_area->window);
	gdk_draw_pixmap(draw_area->window, draw_area->style->fg_gc[draw_area->state], pmap, 0, 0, win_x, win_y, width, height);
	gdk_flush();

	gdk_imlib_free_pixmap(pmap);
	gdk_imlib_destroy_image(im);
}

/* views a file and creates a new window */
void view_image_window (gchar *file)
{
	GdkImlibImage *im;
	GtkWidget *win, *draw_area, *sc_win;
	GdkPixmap *pmap;
	int width, height, screen_x, screen_y;
	double wh_ratio = 1;

	im = gdk_imlib_load_image(file);

	if (!im)
		return;

	width = im->rgb_width;
	height = im->rgb_height;
	screen_x=gdk_screen_width();
	screen_y=gdk_screen_height();

	/* deal with images larger than the screen */
	wh_ratio = (double) width / (double)height;
	if (width > screen_x)
	{
		width = screen_x;
		height = width / wh_ratio;
	}
	if (height > screen_y)
	{
		height = screen_y;
		width = height * wh_ratio;
	}

	gdk_imlib_render(im, width, height);
	pmap = gdk_imlib_move_image(im);

	draw_area = gtk_drawing_area_new();
	gtk_widget_show(draw_area);

	win = gtk_window_new(GTK_WINDOW_TOPLEVEL);

	gtk_signal_connect( GTK_OBJECT (win), "delete_event",
                    GTK_SIGNAL_FUNC (close_window), NULL );
	gtk_widget_set_events(win, GDK_BUTTON_PRESS_MASK);
	gtk_signal_connect( GTK_OBJECT (win), "key_press_event",
                    GTK_SIGNAL_FUNC (key_handler), NULL);
	gtk_signal_connect(GTK_OBJECT(win), "button_press_event",
					GTK_SIGNAL_FUNC (click_handler), NULL);
	gtk_signal_connect(GTK_OBJECT(win), "configure_event",
					GTK_SIGNAL_FUNC (image_resize_cb), NULL);

	gtk_object_set_data(GTK_OBJECT(win), "image", (gpointer)im);
	gtk_object_set_data(GTK_OBJECT(win), "view", (gpointer)draw_area);

	gtk_container_set_border_width( GTK_CONTAINER (win), 0 );
	
	gtk_window_set_default_size(GTK_WINDOW(win), width, height);
	gtk_window_set_title(GTK_WINDOW(win), file);

	gtk_widget_show( win );
	gtk_container_add(GTK_CONTAINER(win), draw_area);

	while (gtk_events_pending())
		gtk_main_iteration();

	gdk_window_set_back_pixmap(draw_area->window, pmap, FALSE);
	gdk_window_clear(draw_area->window);
	
	gdk_imlib_free_pixmap(pmap);
}