4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
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.
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.
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
23 #include "bcwindowbase.h"
44 image_sets.remove_all_objects();
49 printf("BC_Theme::dump 1 image_sets=%d contents=%d\n",
52 for(int i = 0; i < contents.total; i++)
53 printf(" %s %p\n", contents.values[i], pointers.values[i]);
56 BC_Resources* BC_Theme::get_resources()
58 return BC_WindowBase::get_resources();
62 // These create single images for storage in the image_sets table.
63 VFrame* BC_Theme::new_image(const char *title, const char *path)
65 VFrame *existing_image = title[0] ? get_image(title, 0) : 0;
66 if(existing_image) return existing_image;
68 BC_ThemeSet *result = new BC_ThemeSet(1, 0, title);
69 result->data[0] = new VFrame(get_image_data(path));
70 image_sets.append(result);
71 return result->data[0];
74 VFrame* BC_Theme::new_image(const char *path)
76 return new_image("", path);
81 // These create image sets which are stored in the image_sets table.
82 VFrame** BC_Theme::new_image_set(const char *title, int total, va_list *args)
86 printf("BC_Theme::new_image_set %d %s zero number of images\n",
91 VFrame **existing_image_set = title[0] ? get_image_set(title, 0) : 0;
92 if(existing_image_set) return existing_image_set;
94 BC_ThemeSet *result = new BC_ThemeSet(total, 1, title);
95 image_sets.append(result);
96 for(int i = 0; i < total; i++)
98 char *path = va_arg(*args, char*);
99 result->data[i] = new_image(path);
104 VFrame** BC_Theme::new_image_set_images(const char *title, int total, ...)
107 va_start(list, total);
108 BC_ThemeSet *existing_image_set = title[0] ? get_image_set_object(title) : 0;
109 if(existing_image_set)
111 image_sets.remove_object(existing_image_set);
114 BC_ThemeSet *result = new BC_ThemeSet(total, 0, title);
115 image_sets.append(result);
116 for(int i = 0; i < total; i++)
118 result->data[i] = va_arg(list, VFrame*);
124 VFrame** BC_Theme::new_image_set(const char *title, int total, ...)
127 va_start(list, total);
128 VFrame **result = new_image_set(title, total, &list);
134 VFrame** BC_Theme::new_image_set(int total, ...)
137 va_start(list, total);
138 VFrame **result = new_image_set("", total, &list);
144 BC_ThemeSet* BC_Theme::get_image_set_object(const char *title)
146 // compare title[0],title[1] for faster prefix test
147 const unsigned char *bp = (const unsigned char*)title;
148 unsigned short tval = bp[0];
149 if( tval ) tval |= (bp[1] << 8);
151 for( int i=0; i < image_sets.total; ++i ) {
152 const char *tp = image_sets[i]->title;
153 bp = (const unsigned char *) tp;
154 unsigned short val = bp[0];
155 if( val ) val |= (bp[1] << 8);
156 if( val != tval ) continue;
157 if( !strcmp(tp, title) ) return image_sets[i];
162 VFrame* BC_Theme::get_image(const char *title, int use_default)
164 BC_ThemeSet* tsp = get_image_set_object(title);
165 if( tsp ) return tsp->data[0];
168 // Return the first image it can find. This should always work.
170 printf("BC_Theme::get_image: image \"%s\" not found.\n",
173 return image_sets[0]->data[0];
176 // Give up and go to a movie.
180 VFrame** BC_Theme::get_image_set(const char *title, int use_default)
182 BC_ThemeSet* tsp = get_image_set_object(title);
183 if( tsp ) return tsp->data;
185 // Get the image set with the largest number of images.
188 printf("BC_Theme::get_image_set: image set \"%s\" not found.\n",
192 for(int i = 0; i < image_sets.total; i++)
194 if(image_sets[i]->total > max_total)
196 max_total = image_sets[i]->total;
202 return image_sets[max_number]->data;
205 // Give up and go to a movie
219 VFrame** BC_Theme::new_button(const char *overlay_path,
225 VFrame default_data(get_image_data(overlay_path));
226 BC_ThemeSet *result = new BC_ThemeSet(3, 1, title ? title : "");
227 if(title) image_sets.append(result);
229 result->data[0] = new_image(up_path);
230 result->data[1] = new_image(hi_path);
231 result->data[2] = new_image(dn_path);
232 for(int i = 0; i < 3; i++)
234 overlay(result->data[i], &default_data, -1, -1, (i == 2));
240 VFrame** BC_Theme::new_button4(const char *overlay_path,
244 const char *disabled_path,
247 VFrame default_data(get_image_data(overlay_path));
248 BC_ThemeSet *result = new BC_ThemeSet(4, 1, title ? title : "");
249 if(title) image_sets.append(result);
251 result->data[0] = new_image(up_path);
252 result->data[1] = new_image(hi_path);
253 result->data[2] = new_image(dn_path);
254 result->data[3] = new_image(disabled_path);
255 for(int i = 0; i < 4; i++)
257 overlay(result->data[i], &default_data, -1, -1, (i == 2));
263 VFrame** BC_Theme::new_button(const char *overlay_path,
269 VFrame default_data(get_image_data(overlay_path));
270 BC_ThemeSet *result = new BC_ThemeSet(3, 0, title ? title : "");
271 if(title) image_sets.append(result);
273 result->data[0] = new VFrame(*up);
274 result->data[1] = new VFrame(*hi);
275 result->data[2] = new VFrame(*dn);
276 for(int i = 0; i < 3; i++)
277 overlay(result->data[i], &default_data, -1, -1, (i == 2));
282 VFrame** BC_Theme::new_toggle(const char *overlay_path,
285 const char *checked_path,
287 const char *checkedhi_path,
290 VFrame default_data(get_image_data(overlay_path));
291 BC_ThemeSet *result = new BC_ThemeSet(5, 1, title ? title : "");
292 if(title) image_sets.append(result);
294 result->data[0] = new_image(up_path);
295 result->data[1] = new_image(hi_path);
296 result->data[2] = new_image(checked_path);
297 result->data[3] = new_image(dn_path);
298 result->data[4] = new_image(checkedhi_path);
299 for(int i = 0; i < 5; i++)
300 overlay(result->data[i], &default_data, -1, -1, (i == 3));
304 VFrame** BC_Theme::new_toggle(const char *overlay_path,
312 VFrame default_data(get_image_data(overlay_path));
313 BC_ThemeSet *result = new BC_ThemeSet(5, 0, title ? title : "");
314 if(title) image_sets.append(result);
316 result->data[0] = new VFrame(*up);
317 result->data[1] = new VFrame(*hi);
318 result->data[2] = new VFrame(*checked);
319 result->data[3] = new VFrame(*dn);
320 result->data[4] = new VFrame(*checkedhi);
321 for(int i = 0; i < 5; i++)
322 overlay(result->data[i], &default_data, -1, -1, (i == 3));
326 void BC_Theme::overlay(VFrame *dst, VFrame *src, int in_x1, int in_x2, int shift)
330 unsigned char **in_rows;
331 unsigned char **out_rows;
335 w = MIN(src->get_w(), dst->get_w());
336 h = MIN(dst->get_h(), src->get_h());
343 h = MIN(dst->get_h(), src->get_h());
345 in_rows = src->get_rows();
346 out_rows = dst->get_rows();
348 switch(src->get_color_model())
351 switch(dst->get_color_model())
354 for(int i = shift; i < h; i++)
356 unsigned char *in_row = 0;
357 unsigned char *out_row;
361 in_row = in_rows[i] + in_x1 * 4;
362 out_row = out_rows[i];
366 in_row = in_rows[i - 1] + in_x1 * 4;
367 out_row = out_rows[i] + 4;
370 for(int j = shift; j < w; j++)
372 int opacity = in_row[3];
373 int transparency = 0xff - opacity;
375 out_row[0] = (in_row[0] * opacity + out_row[0] * transparency) / 0xff;
376 out_row[1] = (in_row[1] * opacity + out_row[1] * transparency) / 0xff;
377 out_row[2] = (in_row[2] * opacity + out_row[2] * transparency) / 0xff;
378 out_row[3] = MAX(in_row[3], out_row[3]);
386 for(int i = shift; i < h; i++)
388 unsigned char *in_row;
389 unsigned char *out_row = out_rows[i];
393 in_row = in_rows[i] + in_x1 * 3;
394 out_row = out_rows[i];
398 in_row = in_rows[i - 1] + in_x1 * 3;
399 out_row = out_rows[i] + 3;
402 for(int j = shift; j < w; j++)
404 int opacity = in_row[3];
405 int transparency = 0xff - opacity;
406 out_row[0] = (in_row[0] * opacity + out_row[0] * transparency) / 0xff;
407 out_row[1] = (in_row[1] * opacity + out_row[1] * transparency) / 0xff;
408 out_row[2] = (in_row[2] * opacity + out_row[2] * transparency) / 0xff;
419 void BC_Theme::set_data(unsigned char *ptr)
421 contents_ptr = (char*)(ptr + sizeof(int));
422 int contents_size = *(int*)ptr - sizeof(int);
423 data_ptr = contents_ptr + contents_size;
425 for(int i = 0; i < contents_size; )
428 contents.append(contents_ptr + i);
429 while(contents_ptr[i] && i < contents_size)
431 if(i < contents_size)
434 pointers.append((unsigned char*)data_ptr +
435 *(unsigned int*)(contents_ptr + i));
440 pointers.append((unsigned char*)data_ptr);
446 unsigned char* BC_Theme::get_image_data(const char *title)
450 fprintf(stderr, "BC_Theme::get_image_data: no data set\n");
454 // Image is the same as the last one
455 if(last_image && !strcasecmp(last_image, title))
460 // Search for image anew.
461 for(int i = 0; i < contents.total; i++)
463 if(!strcasecmp(contents.values[i], title))
465 last_pointer = pointers.values[i];
466 last_image = contents.values[i];
468 return pointers.values[i];
472 fprintf(stderr, _("Theme::get_image: %s not found.\n"), title);
476 void BC_Theme::check_used()
478 // Can't use because some images are gotten the old fashioned way.
481 for(int i = 0; i < used.total; i++)
486 printf(_("BC_Theme::check_used: Images aren't used.\n"));
487 printf("%s ", contents.values[i]);
491 if(got_it) printf("\n");
505 BC_ThemeSet::BC_ThemeSet(int total, int is_reference, const char *title)
508 this->title = new char[strlen(title) + 1];
509 strcpy(this->title, title);
510 this->is_reference = is_reference;
511 data = new VFrame*[total];
514 BC_ThemeSet::~BC_ThemeSet()
520 for(int i = 0; i < total; i++)