1 /*
2  * Copyright 2006 Richard Wilson <info@tinct.net>
3  *
4  * This file is part of NetSurf, http://www.netsurf-browser.org/
5  *
6  * NetSurf 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; version 2 of the License.
9  *
10  * NetSurf is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include <stdbool.h>
20 #include <swis.h>
21 #include <oslib/osspriteop.h>
22 #include <oslib/wimp.h>
23 
24 #include "utils/nsoption.h"
25 #include "utils/log.h"
26 
27 #include "riscos/gui.h"
28 #include "riscos/configure/configure.h"
29 #include "riscos/dialog.h"
30 #include "riscos/menus.h"
31 #include "riscos/tinct.h"
32 #include "riscos/wimp.h"
33 #include "riscos/wimp_event.h"
34 
35 
36 #define IMAGE_FOREGROUND_FIELD 3
37 #define IMAGE_FOREGROUND_MENU 4
38 #define IMAGE_BACKGROUND_FIELD 6
39 #define IMAGE_BACKGROUND_MENU 7
40 #define IMAGE_CURRENT_DISPLAY 8
41 #define IMAGE_DISABLE_ANIMATION 11
42 #define IMAGE_DEFAULT_BUTTON 12
43 #define IMAGE_CANCEL_BUTTON 13
44 #define IMAGE_OK_BUTTON 14
45 
46 static bool ro_gui_options_image_click(wimp_pointer *pointer);
47 static bool ro_gui_options_image_ok(wimp_w w);
48 static void ro_gui_options_image_redraw(wimp_draw *redraw);
49 static bool ro_gui_options_image_update(wimp_w w, wimp_i i, wimp_menu *m,
50 		wimp_selection *s, menu_action a);
51 static void ro_gui_options_image_read(wimp_w w, unsigned int *bg,
52 		unsigned int *fg);
53 static void ro_gui_options_update_shading(wimp_w w);
54 
55 static osspriteop_area *example_images;
56 int example_users = 0;
57 unsigned int tinct_options[] = {tinct_USE_OS_SPRITE_OP, 0, tinct_DITHER,
58 		tinct_ERROR_DIFFUSE};
59 
ro_gui_options_image_initialise(wimp_w w)60 bool ro_gui_options_image_initialise(wimp_w w)
61 {
62 	int i;
63 
64 	/* load the sprite file */
65 	if (example_users == 0) {
66 		char pathname[256];
67 		snprintf(pathname, 256, "%s.Resources.Image", NETSURF_DIR);
68 		pathname[255] = '\0';
69 		example_images = ro_gui_load_sprite_file(pathname);
70 		if (!example_images)
71 			return false;
72 	}
73 	example_users++;
74 
75 	/* set the current values */
76 	for (i = 0; (i < 4); i++) {
77 		if ((unsigned int)nsoption_int(plot_fg_quality) == tinct_options[i])
78 			ro_gui_set_icon_string(w, IMAGE_FOREGROUND_FIELD,
79 					image_quality_menu->entries[i].
80 						data.indirected_text.text, true);
81 		if ((unsigned int)nsoption_int(plot_bg_quality) == tinct_options[i])
82 			ro_gui_set_icon_string(w, IMAGE_BACKGROUND_FIELD,
83 					image_quality_menu->entries[i].
84 						data.indirected_text.text, true);
85 	}
86 	ro_gui_set_icon_selected_state(w, IMAGE_DISABLE_ANIMATION,
87 				       !nsoption_bool(animate_images));
88 	ro_gui_options_update_shading(w);
89 
90 	/* register icons */
91 	ro_gui_wimp_event_register_menu_gright(w, IMAGE_FOREGROUND_FIELD,
92 			IMAGE_FOREGROUND_MENU, image_quality_menu);
93 	ro_gui_wimp_event_register_menu_gright(w, IMAGE_BACKGROUND_FIELD,
94 			IMAGE_BACKGROUND_MENU, image_quality_menu);
95 	ro_gui_wimp_event_register_checkbox(w, IMAGE_DISABLE_ANIMATION);
96 	ro_gui_wimp_event_register_redraw_window(w,
97 			ro_gui_options_image_redraw);
98 	ro_gui_wimp_event_register_mouse_click(w,
99 			ro_gui_options_image_click);
100 	ro_gui_wimp_event_register_menu_selection(w,
101 			ro_gui_options_image_update);
102 	ro_gui_wimp_event_register_cancel(w, IMAGE_CANCEL_BUTTON);
103 	ro_gui_wimp_event_register_ok(w, IMAGE_OK_BUTTON,
104 			ro_gui_options_image_ok);
105 	ro_gui_wimp_event_set_help_prefix(w, "HelpImageConfig");
106 	ro_gui_wimp_event_memorise(w);
107 
108 	return true;
109 }
110 
ro_gui_options_image_finalise(wimp_w w)111 void ro_gui_options_image_finalise(wimp_w w)
112 {
113 	example_users--;
114 	if (example_users == 0) {
115 	  	free(example_images);
116 	  	example_images = NULL;
117 	}
118 	ro_gui_wimp_event_finalise(w);
119 }
120 
ro_gui_options_image_update(wimp_w w,wimp_i i,wimp_menu * m,wimp_selection * s,menu_action a)121 bool ro_gui_options_image_update(wimp_w w, wimp_i i, wimp_menu *m,
122 		wimp_selection *s, menu_action a)
123 {
124 	ro_gui_redraw_icon(w, IMAGE_CURRENT_DISPLAY);
125 
126 	return true;
127 }
128 
ro_gui_options_image_redraw(wimp_draw * redraw)129 void ro_gui_options_image_redraw(wimp_draw *redraw)
130 {
131 	osbool more;
132 	os_error *error;
133 	wimp_icon_state icon_state;
134 	osspriteop_header *bg = NULL, *fg = NULL;
135 	unsigned int bg_tinct = 0, fg_tinct = 0;
136 
137 	/* get the icon location */
138 	icon_state.w = redraw->w;
139 	icon_state.i = IMAGE_CURRENT_DISPLAY;
140 	error = xwimp_get_icon_state(&icon_state);
141 	if (error) {
142 		NSLOG(netsurf, INFO,
143 		      "xwimp_get_icon_state: 0x%x: %s",
144 		      error->errnum, error->errmess);
145 		ro_warn_user("MenuError", error->errmess);
146 		return;
147 	}
148 
149 	/* find the sprites */
150 	if (example_images) {
151 		ro_gui_options_image_read(redraw->w, &bg_tinct, &fg_tinct);
152 		fg_tinct |= 0xeeeeee00;
153 		xosspriteop_select_sprite(osspriteop_USER_AREA,
154 				example_images, (osspriteop_id)"img_bg", &bg);
155 		xosspriteop_select_sprite(osspriteop_USER_AREA,
156 				example_images, (osspriteop_id)"img_fg", &fg);
157 	}
158 
159 	/* perform the redraw */
160 	more = wimp_redraw_window(redraw);
161 	while (more) {
162 		int origin_x, origin_y;
163 		origin_x = redraw->box.x0 - redraw->xscroll +
164 				icon_state.icon.extent.x0 + 2;
165 		origin_y = redraw->box.y1 - redraw->yscroll +
166 				icon_state.icon.extent.y0 + 2;
167 		if (bg)
168 			_swix(Tinct_Plot, _INR(2,4) | _IN(7),
169 					bg, origin_x, origin_y, bg_tinct);
170 		if (fg)
171 			_swix(Tinct_PlotAlpha, _INR(2,4) | _IN(7),
172 					fg, origin_x, origin_y, fg_tinct);
173 		more = wimp_get_rectangle(redraw);
174 	}
175 }
176 
ro_gui_options_image_read(wimp_w w,unsigned int * bg,unsigned int * fg)177 void ro_gui_options_image_read(wimp_w w, unsigned int *bg, unsigned int *fg)
178 {
179 	const char *text;
180 	int i;
181 
182 	text = ro_gui_get_icon_string(w, IMAGE_FOREGROUND_FIELD);
183 	for (i = 0; i < 4; i++)
184 		if (!strcmp(text, image_quality_menu->entries[i].
185 				data.indirected_text.text))
186 			*fg = tinct_options[i];
187 
188 	text = ro_gui_get_icon_string(w, IMAGE_BACKGROUND_FIELD);
189 	for (i = 0; i < 4; i++)
190 		if (!strcmp(text, image_quality_menu->entries[i].
191 				data.indirected_text.text))
192 			*bg = tinct_options[i];
193 }
194 
ro_gui_options_image_click(wimp_pointer * pointer)195 bool ro_gui_options_image_click(wimp_pointer *pointer)
196 {
197 	unsigned int old_fg, old_bg, bg, fg;
198 
199 	ro_gui_options_image_read(pointer->w, &old_bg, &old_fg);
200 	switch (pointer->i) {
201 		case IMAGE_DEFAULT_BUTTON:
202 			ro_gui_set_icon_string(pointer->w,
203 					IMAGE_FOREGROUND_FIELD,
204 					image_quality_menu->entries[3].
205 						data.indirected_text.text, true);
206   			ro_gui_set_icon_string(pointer->w,
207 					IMAGE_BACKGROUND_FIELD,
208 					image_quality_menu->entries[2].
209 						data.indirected_text.text, true);
210 			ro_gui_set_icon_selected_state(pointer->w,
211 					IMAGE_DISABLE_ANIMATION, false);
212 		case IMAGE_DISABLE_ANIMATION:
213 			ro_gui_options_update_shading(pointer->w);
214 			break;
215 		case IMAGE_CANCEL_BUTTON:
216 			ro_gui_wimp_event_restore(pointer->w);
217 			break;
218 		default:
219 			return false;
220 	}
221 
222 	ro_gui_options_image_read(pointer->w, &bg, &fg);
223 	if ((bg != old_bg) || (fg != old_fg))
224 		ro_gui_options_image_update(pointer->w, pointer->i,
225 				NULL, NULL, NO_ACTION);
226 
227 	return false;
228 }
229 
ro_gui_options_update_shading(wimp_w w)230 void ro_gui_options_update_shading(wimp_w w)
231 {
232 	bool shaded;
233 
234 	shaded = ro_gui_get_icon_selected_state(w, IMAGE_DISABLE_ANIMATION);
235 }
236 
ro_gui_options_image_ok(wimp_w w)237 bool ro_gui_options_image_ok(wimp_w w)
238 {
239 	ro_gui_options_image_read(w,
240 				  (unsigned int *)&nsoption_int(plot_bg_quality),
241 				  (unsigned int *)&nsoption_int(plot_fg_quality));
242 
243 	nsoption_set_bool(animate_images,
244 			  !ro_gui_get_icon_selected_state(w,
245 					IMAGE_DISABLE_ANIMATION));
246 	ro_gui_save_options();
247 
248 	return true;
249 }
250