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
22 #include "bcresources.h"
24 #include "bcwindowbase.h"
43 image_sets.remove_all_objects();
48 printf("BC_Theme::dump 1 image_sets=%d contents=%d\n",
51 for(int i = 0; i < contents.total; i++)
52 printf(" %s %p\n", contents.values[i], pointers.values[i]);
55 BC_Resources* BC_Theme::get_resources()
57 return BC_WindowBase::get_resources();
60 // These create single images for storage in the image_sets table.
61 VFrame* BC_Theme::new_image(const char *title, const char *path)
63 VFrame *existing_image = title[0] ? get_image(title, 0) : 0;
64 if(existing_image) return existing_image;
66 BC_ThemeSet *result = new BC_ThemeSet(1, 0, title);
67 result->data[0] = new VFramePng(get_image_data(path));
68 image_sets.append(result);
69 return result->data[0];
72 VFrame* BC_Theme::new_image(const char *path)
74 return new_image("", path);
79 // These create image sets which are stored in the image_sets table.
80 VFrame** BC_Theme::new_image_set(const char *title, int total, va_list *args)
84 printf("BC_Theme::new_image_set %d %s zero number of images\n",
89 VFrame **existing_image_set = title[0] ? get_image_set(title, 0) : 0;
90 if(existing_image_set) return existing_image_set;
92 BC_ThemeSet *result = new BC_ThemeSet(total, 1, title);
93 image_sets.append(result);
94 for(int i = 0; i < total; i++)
96 char *path = va_arg(*args, char*);
97 result->data[i] = new_image(path);
102 VFrame** BC_Theme::new_image_set_images(const char *title, int total, ...)
105 va_start(list, total);
106 BC_ThemeSet *existing_image_set = title[0] ? get_image_set_object(title) : 0;
107 if(existing_image_set)
109 image_sets.remove_object(existing_image_set);
112 BC_ThemeSet *result = new BC_ThemeSet(total, 0, title);
113 image_sets.append(result);
114 for(int i = 0; i < total; i++)
116 result->data[i] = va_arg(list, VFrame*);
122 VFrame** BC_Theme::new_image_set(const char *title, int total, ...)
125 va_start(list, total);
126 VFrame **result = new_image_set(title, total, &list);
132 VFrame** BC_Theme::new_image_set(int total, ...)
135 va_start(list, total);
136 VFrame **result = new_image_set("", total, &list);
142 BC_ThemeSet* BC_Theme::get_image_set_object(const char *title)
144 // compare title[0],title[1] for faster prefix test
145 const unsigned char *bp = (const unsigned char*)title;
146 unsigned short tval = bp[0];
147 if( tval ) tval |= (bp[1] << 8);
149 for( int i=0; i < image_sets.total; ++i ) {
150 const char *tp = image_sets[i]->title;
151 bp = (const unsigned char *) tp;
152 unsigned short val = bp[0];
153 if( val ) val |= (bp[1] << 8);
154 if( val != tval ) continue;
155 if( !strcmp(tp, title) ) return image_sets[i];
160 VFrame* BC_Theme::get_image(const char *title, int use_default)
162 BC_ThemeSet* tsp = get_image_set_object(title);
163 if( tsp ) return tsp->data[0];
166 // Return the first image it can find. This should always work.
168 printf("BC_Theme::get_image: image \"%s\" not found.\n",
171 return image_sets[0]->data[0];
174 // Give up and go to a movie.
178 VFrame** BC_Theme::get_image_set(const char *title, int use_default)
180 BC_ThemeSet* tsp = get_image_set_object(title);
181 if( tsp ) return tsp->data;
183 // Get the image set with the largest number of images.
186 printf("BC_Theme::get_image_set: image set \"%s\" not found.\n",
190 for(int i = 0; i < image_sets.total; i++)
192 if(image_sets[i]->total > max_total)
194 max_total = image_sets[i]->total;
200 return image_sets[max_number]->data;
203 // Give up and go to a movie
217 VFrame** BC_Theme::new_button(const char *overlay_path,
223 VFramePng default_data(get_image_data(overlay_path));
224 BC_ThemeSet *result = new BC_ThemeSet(3, 1, title ? title : "");
225 if(title) image_sets.append(result);
227 result->data[0] = new_image(up_path);
228 result->data[1] = new_image(hi_path);
229 result->data[2] = new_image(dn_path);
230 for(int i = 0; i < 3; i++)
232 overlay(result->data[i], &default_data, -1, -1, (i == 2));
238 VFrame** BC_Theme::new_button4(const char *overlay_path,
242 const char *disabled_path,
245 VFramePng default_data(get_image_data(overlay_path));
246 BC_ThemeSet *result = new BC_ThemeSet(4, 1, title ? title : "");
247 if(title) image_sets.append(result);
249 result->data[0] = new_image(up_path);
250 result->data[1] = new_image(hi_path);
251 result->data[2] = new_image(dn_path);
252 result->data[3] = new_image(disabled_path);
253 for(int i = 0; i < 4; i++)
255 overlay(result->data[i], &default_data, -1, -1, (i == 2));
261 VFrame** BC_Theme::new_button(const char *overlay_path,
267 VFramePng default_data(get_image_data(overlay_path));
268 BC_ThemeSet *result = new BC_ThemeSet(3, 0, title ? title : "");
269 if(title) image_sets.append(result);
271 result->data[0] = new VFrame(*up);
272 result->data[1] = new VFrame(*hi);
273 result->data[2] = new VFrame(*dn);
274 for(int i = 0; i < 3; i++)
275 overlay(result->data[i], &default_data, -1, -1, (i == 2));
280 VFrame** BC_Theme::new_toggle(const char *overlay_path,
283 const char *checked_path,
285 const char *checkedhi_path,
288 VFramePng default_data(get_image_data(overlay_path));
289 BC_ThemeSet *result = new BC_ThemeSet(5, 1, title ? title : "");
290 if(title) image_sets.append(result);
292 result->data[0] = new_image(up_path);
293 result->data[1] = new_image(hi_path);
294 result->data[2] = new_image(checked_path);
295 result->data[3] = new_image(dn_path);
296 result->data[4] = new_image(checkedhi_path);
297 for(int i = 0; i < 5; i++)
298 overlay(result->data[i], &default_data, -1, -1, (i == 3));
302 VFrame** BC_Theme::new_toggle(const char *overlay_path,
310 VFramePng default_data(get_image_data(overlay_path));
311 BC_ThemeSet *result = new BC_ThemeSet(5, 0, title ? title : "");
312 if(title) image_sets.append(result);
314 result->data[0] = new VFrame(*up);
315 result->data[1] = new VFrame(*hi);
316 result->data[2] = new VFrame(*checked);
317 result->data[3] = new VFrame(*dn);
318 result->data[4] = new VFrame(*checkedhi);
319 for(int i = 0; i < 5; i++)
320 overlay(result->data[i], &default_data, -1, -1, (i == 3));
324 void BC_Theme::overlay(VFrame *dst, VFrame *src, int in_x1, int in_x2, int shift)
328 unsigned char **in_rows;
329 unsigned char **out_rows;
333 w = MIN(src->get_w(), dst->get_w());
334 h = MIN(dst->get_h(), src->get_h());
341 h = MIN(dst->get_h(), src->get_h());
343 in_rows = src->get_rows();
344 out_rows = dst->get_rows();
346 switch(src->get_color_model())
349 switch(dst->get_color_model())
352 for(int i = shift; i < h; i++)
354 unsigned char *in_row = 0;
355 unsigned char *out_row;
359 in_row = in_rows[i] + in_x1 * 4;
360 out_row = out_rows[i];
364 in_row = in_rows[i - 1] + in_x1 * 4;
365 out_row = out_rows[i] + 4;
368 for(int j = shift; j < w; j++)
370 int opacity = in_row[3];
371 int transparency = 0xff - opacity;
373 out_row[0] = (in_row[0] * opacity + out_row[0] * transparency) / 0xff;
374 out_row[1] = (in_row[1] * opacity + out_row[1] * transparency) / 0xff;
375 out_row[2] = (in_row[2] * opacity + out_row[2] * transparency) / 0xff;
376 out_row[3] = MAX(in_row[3], out_row[3]);
384 for(int i = shift; i < h; i++)
386 unsigned char *in_row;
387 unsigned char *out_row = out_rows[i];
391 in_row = in_rows[i] + in_x1 * 3;
392 out_row = out_rows[i];
396 in_row = in_rows[i - 1] + in_x1 * 3;
397 out_row = out_rows[i] + 3;
400 for(int j = shift; j < w; j++)
402 int opacity = in_row[3];
403 int transparency = 0xff - opacity;
404 out_row[0] = (in_row[0] * opacity + out_row[0] * transparency) / 0xff;
405 out_row[1] = (in_row[1] * opacity + out_row[1] * transparency) / 0xff;
406 out_row[2] = (in_row[2] * opacity + out_row[2] * transparency) / 0xff;
417 void BC_Theme::set_data(unsigned char *ptr)
419 contents_ptr = (char*)(ptr + sizeof(int));
420 int contents_size = *(int*)ptr - sizeof(int);
421 data_ptr = contents_ptr + contents_size;
423 for(int i = 0; i < contents_size; )
426 contents.append(contents_ptr + i);
427 while(contents_ptr[i] && i < contents_size)
429 if(i < contents_size)
432 pointers.append((unsigned char*)data_ptr +
433 *(unsigned int*)(contents_ptr + i));
438 pointers.append((unsigned char*)data_ptr);
444 unsigned char* BC_Theme::get_image_data(const char *title)
448 fprintf(stderr, "BC_Theme::get_image_data: no data set\n");
452 // Image is the same as the last one
453 if(last_image && !strcasecmp(last_image, title))
458 // Search for image anew.
459 for(int i = 0; i < contents.total; i++)
461 if(!strcasecmp(contents.values[i], title))
463 last_pointer = pointers.values[i];
464 last_image = contents.values[i];
466 return pointers.values[i];
470 fprintf(stderr, _("Theme::get_image: %s not found.\n"), title);
474 void BC_Theme::check_used()
476 // Can't use because some images are gotten the old fashioned way.
479 for(int i = 0; i < used.total; i++)
484 printf(_("BC_Theme::check_used: Images aren't used.\n"));
485 printf("%s ", contents.values[i]);
489 if(got_it) printf("\n");
503 BC_ThemeSet::BC_ThemeSet(int total, int is_reference, const char *title)
506 this->title = new char[strlen(title) + 1];
507 strcpy(this->title, title);
508 this->is_reference = is_reference;
509 data = new VFrame*[total];
512 BC_ThemeSet::~BC_ThemeSet()
518 for(int i = 0; i < total; i++)