1 /* view_area.c - functions for managing standard view area
2 *
3 * Copyright (C) 2003, 2007 Patrice St-Gelais
4 * patrstg@users.sourceforge.net
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21 // Modified 2005-01-09 for removing double buffering (now auto with GTK2) PSTG
22
23 #include "../utils/x_alloc.h"
24 #include "view_area.h"
25 #include <stdlib.h>
26 #include <stdio.h>
27
resize_area(view_struct * vs,gint new_width,gint new_height)28 void resize_area (view_struct *vs, gint new_width, gint new_height) {
29 if (vs->if_rgb)
30 vs->buf8 = (unsigned char *) x_realloc(vs->buf8, 3*new_width * new_height * sizeof(unsigned char), "unsigned char (vs->buf8 in view_area.c - realloc)");
31 else
32 vs->buf8 = (unsigned char *) x_realloc(vs->buf8, new_width * new_height * sizeof(unsigned char), "unsigned char (vs->buf8 in view_area.c - realloc)");
33 gtk_widget_set_size_request (vs->area, new_width, new_height);
34 if (vs->viewport) {
35 gtk_widget_set_size_request (vs->viewport, new_width, new_height);
36 }
37 vs->width = new_width;
38 vs->height = new_height;
39 }
40
draw_area(view_struct * vs)41 void draw_area (view_struct *vs) {
42 if (vs->calc_buf)
43 (*vs->calc_buf) (vs->external_data, vs->buf8, vs->width, vs->height);
44
45 // This calls an expose_event for vs->area:
46 gtk_widget_queue_draw_area ( vs->area, 0, 0, vs->width, vs->height);
47 }
48
draw_area_callb(GtkWidget * wdg,gpointer view_struct_ptr)49 void draw_area_callb (GtkWidget *wdg, gpointer view_struct_ptr) {
50 // Convenience function
51 if (view_struct_ptr)
52 draw_area((view_struct *) view_struct_ptr);
53 }
54
area_configure_event(GtkWidget * widget,GdkEventConfigure * event,gpointer data)55 static gint area_configure_event (GtkWidget *widget, GdkEventConfigure *event, gpointer data)
56 {
57 view_struct *vs;
58 vs = (view_struct *) data;
59 // We initialize the drawable:
60 // printf("Area_configure_event\n");
61 gdk_draw_rectangle(vs->area->window,
62 vs->area->style->black_gc,TRUE, 0, 0,
63 vs->area->allocation.width,
64 vs->area->allocation.height);
65 draw_area (vs);
66
67 return TRUE;
68 }
69
area_expose_event(GtkWidget * widget,GdkEventExpose * event,gpointer data)70 gint area_expose_event (GtkWidget *widget, GdkEventExpose *event, gpointer data)
71 {
72 view_struct *vs;
73 vs = (view_struct *) data;
74 if (event->count > 0) {
75 return(TRUE);
76 }
77 if (GTK_IS_DRAWING_AREA(vs->area)) {
78 if (vs->if_rgb) {
79 gdk_draw_rgb_image(vs->area->window,
80 vs->area->style->fg_gc[GTK_WIDGET_STATE (vs->area)],
81 0,0,vs->width,
82 vs->height,
83 GDK_RGB_DITHER_NONE,
84 vs->buf8,
85 3*vs->width);
86 }
87 else {
88 gdk_draw_gray_image(vs->area->window,
89 vs->area->style->black_gc,
90 0, 0, vs->width,
91 vs->height,
92 GDK_RGB_DITHER_NONE,
93 vs->buf8,
94 vs->width);
95 }
96 }
97 return TRUE;
98 }
99
view_struct_new_with_rgb(gint width,gint height,void (* calc_buf)(gpointer,unsigned char *,gint,gint),gpointer external_data,gboolean if_rgb)100 view_struct *view_struct_new_with_rgb (gint width, gint height, void (*calc_buf) (gpointer, unsigned char*, gint, gint), gpointer external_data, gboolean if_rgb) {
101 // Builds a preview structure, for a 8 bits image
102 view_struct *vs;
103 vs = (view_struct *) x_malloc(sizeof(view_struct), "view_struct");
104 // printf("View_struct_new: %d\n",vs);
105 vs->width = width;
106 vs->height = height;
107 vs->calc_buf = calc_buf;
108 vs->external_data = external_data;
109 vs->if_rgb = if_rgb;
110
111 if (if_rgb)
112 vs->buf8 = (unsigned char *) x_calloc(3*width*height,sizeof(unsigned char), "unsigned char (vs->buf8 in view_area.c)");
113 else
114 vs->buf8 = (unsigned char *) x_calloc(width*height,sizeof(unsigned char), "unsigned char (vs->buf8 in view_area.c)");
115
116 vs->area = gtk_drawing_area_new();
117 gtk_widget_set_size_request( GTK_WIDGET(vs->area), vs->width, vs->height);
118 gtk_signal_connect(GTK_OBJECT(vs->area), "configure_event",
119 (GtkSignalFunc) area_configure_event, (gpointer) vs);
120 gtk_signal_connect(GTK_OBJECT(vs->area),"expose_event",
121 (GtkSignalFunc) area_expose_event, (gpointer) vs);
122 gtk_widget_show(vs->area);
123
124 vs->viewport = NULL;
125 return vs;
126 }
127
view_struct_new(gint width,gint height,void (* calc_buf)(gpointer,unsigned char *,gint,gint),gpointer external_data)128 view_struct *view_struct_new (gint width, gint height, void (*calc_buf) (gpointer, unsigned char*, gint, gint), gpointer external_data) {
129
130 return view_struct_new_with_rgb (width, height, calc_buf, external_data, FALSE);
131 }
132
view_struct_free(view_struct * vs)133 void view_struct_free (view_struct *vs) {
134 if (!vs)
135 return;
136 if (vs->buf8)
137 x_free(vs->buf8);
138 x_free(vs);
139 }
140
141