From 9e13c0f9a33f3f4ce08f9df937e24e686040d960 Mon Sep 17 00:00:00 2001 From: James Bursa Date: Thu, 20 Nov 2003 00:16:31 +0000 Subject: [project @ 2003-11-20 00:16:31 by bursa] Clean up and simplify themes, remove old history code. svn path=/import/netsurf/; revision=420 --- riscos/theme.c | 408 ++++++++++++++++----------------------------------------- 1 file changed, 116 insertions(+), 292 deletions(-) (limited to 'riscos/theme.c') diff --git a/riscos/theme.c b/riscos/theme.c index 34f66fa19..25cc6eb67 100644 --- a/riscos/theme.c +++ b/riscos/theme.c @@ -3,324 +3,148 @@ * Licensed under the GNU General Public License, * http://www.opensource.org/licenses/gpl-license * Copyright 2003 Phil Mellor + * Copyright 2003 James Bursa */ -#include "netsurf/riscos/theme.h" -#include "oslib/wimp.h" -#include "oslib/messagetrans.h" -#include "oslib/osspriteop.h" +/** \file + * Toolbar themes (implementation). + */ + +#include +#include #include +#include "oslib/osfile.h" +#include "oslib/osspriteop.h" +#include "oslib/wimp.h" +#include "netsurf/riscos/gui.h" +#include "netsurf/riscos/theme.h" #include "netsurf/utils/utils.h" -#include - -ro_theme* current_theme = NULL; - -void ro_theme_preload_template(ro_theme* theme, char* template_name, - int* total_winicon, int* total_indirected) -{ - int winicon, indirected; - if (wimp_load_template(0,0,0, (byte*)0xffffffff, template_name,0, &winicon, &indirected) == 0) - fprintf(stderr, "Template not found!!!!!!!!!!!\n"); - *total_winicon = *total_winicon + winicon + 32; - *total_indirected = *total_indirected + indirected + 32; - return; -} - -wimp_window* ro_theme_load_template(ro_theme* theme, char* template_name, char** indirected_ptr, char* indirected_end) -{ - int winicon, indirected; - int temp; - wimp_window* window; - char* old_indirected = *indirected_ptr; - - if (wimp_load_template(0,0,0, (byte*)0xffffffff, template_name, 0, &winicon, &indirected) == 0) - fprintf(stderr, "Template not found!!!!!!!!!!!\n"); -fprintf(stderr, "Allocating %d bytes for window / icon data:\n",winicon); -fprintf(stderr, "Require %d bytes for indirected data:\n",indirected); -fprintf(stderr, "Indirected before %d:\n",(int)indirected_ptr); - window = (wimp_window*) xcalloc(winicon + 1024, 1); - if (wimp_load_template(window, (*indirected_ptr), indirected_end, - (byte*)0xffffffff, template_name, 0, &temp, &temp) == 0) - fprintf(stderr, "Template not found!!!!!!!!!!!\n"); +static wimp_window *theme_toolbar_template = 0; +static osspriteop_area *theme_sprite_area = 0; +unsigned int theme_throbs; - *indirected_ptr = *indirected_ptr + indirected; -fprintf(stderr, "Indirected after %d:\n",(int)*indirected_ptr); -fprintf(stderr, "Difference: %d\n",(int)*indirected_ptr - (int)old_indirected); - //*winicon_ptr = old_winicon + winicon + 8 - ((int)(old_winicon + winicon) % 4); - -// *indirected_ptr = old_indirected + indirected + 8 - ((int)(old_indirected + indirected) % 4); - - return window; -} +/** + * Load a theme from a directory. + * + * The directory must contain a Templates file containing the toolbar template, + * and a Sprites file containing icons. + */ -ro_theme* ro_theme_create(char* pathname) +void ro_theme_load(char *pathname) { - char filename[1024]; - - int winicon, indirected; - char* winicon_ptr; - char* indirected_ptr; - - ro_theme* theme; - messagetrans_file_flags flags; - int size; - - FILE * fp; - int i; - - - theme = (ro_theme*) xcalloc(sizeof(ro_theme), 1); - -fprintf(stderr, "Loading templates...\n"); - sprintf(filename, "%s.Templates", pathname); -fprintf(stderr, "%s\n",filename); - - winicon = 0; - indirected = 0; - - wimp_open_template(filename); -fprintf(stderr, "Preload theme_info\n"); - ro_theme_preload_template(theme, "theme_info\0 ", &winicon, &indirected); -fprintf(stderr, "Preload toolbar\n"); - ro_theme_preload_template(theme, "toolbar\0 ", &winicon, &indirected); - -fprintf(stderr, "Allocate %d bytes of indirected data\n",indirected + 16); - theme->indirected_data = xcalloc(sizeof(char), indirected + 16); - indirected_ptr = theme->indirected_data; -fprintf(stderr, "Allocate %d bytes of window and icon data\n",winicon + 16); -// theme->window_and_icon_data = xcalloc(sizeof(char), winicon + 16); -// winicon_ptr = theme->window_and_icon_data; - -fprintf(stderr, "Load toolbar\n"); - theme->toolbar = - ro_theme_load_template(theme, "toolbar\0 ", &indirected_ptr, theme->indirected_data + indirected + 16); -fprintf(stderr, "Load theme_info\n"); - theme->theme_info = - ro_theme_load_template(theme, "theme_info\0 ", &indirected_ptr, theme->indirected_data + indirected + 16); - -fprintf(stderr, "Close template\n"); - wimp_close_template(); - -fprintf(stderr, "Loading icon names...\n"); - sprintf(filename, "%s.IconNames", pathname); -fprintf(stderr, "%s\n", filename); - messagetrans_file_info(filename, &flags, &size); - -fprintf(stderr, "Allocating %d bytes for icon names data\n", size); - theme->iconNames.data = xcalloc(size, sizeof(char)); -fprintf(stderr, "Allocating %d bytes for filename\n", strlen(filename)+2); - theme->iconNames.filename = xcalloc(strlen(filename)+2, sizeof(char)); - strcpy(theme->iconNames.filename, filename); -fprintf(stderr, "Opening messagetrans file\n"); - messagetrans_open_file(&theme->iconNames.cb, theme->iconNames.filename, - theme->iconNames.data); - - - -fprintf(stderr, "Loading icon sizes...\n"); - sprintf(filename, "%s.IconSizes", pathname); -fprintf(stderr, "%s\n", filename); - messagetrans_file_info(filename, &flags, &size); - -fprintf(stderr, "Allocating %d bytes for icon sizes data\n", size); - theme->iconSizes.data = xcalloc(size, sizeof(char)); -fprintf(stderr, "Allocating %d bytes for filename\n", strlen(filename)+2); - theme->iconSizes.filename = xcalloc(strlen(filename)+2, sizeof(char)); - strcpy(theme->iconSizes.filename, filename); -fprintf(stderr, "Opening messagetrans file\n"); - messagetrans_open_file(&theme->iconSizes.cb, theme->iconSizes.filename, - theme->iconSizes.data); - - - - sprintf(filename, "%s.Sprites", pathname); - fp = fopen(filename, "rb"); - if (fp == 0) die("Failed to open file"); - if (fseek(fp, 0, SEEK_END) != 0) die("fseek() failed"); - if ((size = (int) ftell(fp)) == -1) die("ftell() failed"); - fclose(fp); - - theme->sprites = xcalloc(size + 16, 1); - if (theme->sprites == NULL) - die("Can't claim memory for theme sprites"); - - theme->sprites->size = size + 16; - theme->sprites->sprite_count = 0; - theme->sprites->first = 16; - theme->sprites->used = 16; - osspriteop_clear_sprites(osspriteop_USER_AREA, theme->sprites); - osspriteop_load_sprite_file(osspriteop_USER_AREA, theme->sprites, filename); - - fprintf(stderr, "sprites loaded. %d counted\n", theme->sprites->sprite_count); - theme->throbs = 0; - for (i = 1; i <= theme->sprites->sprite_count; i++) - { - char name[32]; - fprintf(stderr, "returning name for %d\n", i); - osspriteop_return_name(osspriteop_USER_AREA, theme->sprites, name, 32, i); - if (strncmp(name, "throbber", 8) == 0) - { - int this_number = atoi(name+8); - if (this_number > theme->throbs) - theme->throbs = this_number; - } - } - fprintf(stderr, "%d throbbers found.\n", theme->throbs); - -fprintf(stderr, "Returning theme...\n"); - return theme; + char name[] = "toolbar"; + int context, window_size, data_size, size, i; + static char *data = 0; + char filename[strlen(pathname) + 12]; + fileswitch_object_type obj_type; + + /* free old theme data */ + free(theme_toolbar_template); + free(data); + free(theme_sprite_area); + + /* load template */ + sprintf(filename, "%s.Templates", pathname); + wimp_open_template(filename); + + /* find required buffer sizes */ + context = wimp_load_template(wimp_GET_SIZE, 0, 0, wimp_NO_FONTS, + name, 0, &window_size, &data_size); + assert(context != 0); + + theme_toolbar_template = xcalloc((unsigned int) window_size, 1); + data = xcalloc((unsigned int) data_size, 1); + + /* load */ + wimp_load_template(theme_toolbar_template, data, data + data_size, + wimp_NO_FONTS, name, 0, 0, 0); + + wimp_close_template(); + + assert(ICON_TOOLBAR_RELOAD < theme_toolbar_template->icon_count); + theme_toolbar_template->flags |= wimp_WINDOW_FURNITURE_WINDOW; + theme_toolbar_template->icons[ICON_TOOLBAR_URL].data.indirected_text.size = 256; + theme_toolbar_template->icons[ICON_TOOLBAR_STATUS].data.indirected_text.size = 256; + + /* load sprites */ + sprintf(filename, "%s.Sprites", pathname); + obj_type = osfile_read_no_path(filename, 0, 0, &size, 0); + assert(obj_type & fileswitch_IS_FILE); + + theme_sprite_area = xcalloc(size + 16, 1); + theme_sprite_area->size = size + 16; + theme_sprite_area->sprite_count = 0; + theme_sprite_area->first = 16; + theme_sprite_area->used = 16; + osspriteop_clear_sprites(osspriteop_USER_AREA, theme_sprite_area); + osspriteop_load_sprite_file(osspriteop_USER_AREA, theme_sprite_area, + filename); + + theme_toolbar_template->sprite_area = theme_sprite_area; + theme_toolbar_template->icons[ICON_TOOLBAR_THROBBER].data.indirected_sprite.area = + theme_sprite_area; + + /* find the highest sprite called throbber%i */ + theme_throbs = 0; + for (i = 1; i != theme_sprite_area->sprite_count; i++) { + char name[32]; + osspriteop_return_name(osspriteop_USER_AREA, + theme_sprite_area, name, 32, i); + if (strncmp(name, "throbber", 8) == 0) { + int n = atoi(name + 8); + if (theme_throbs < n) + theme_throbs = n; + } + } } -wimp_i ro_theme_icon(ro_theme* theme, theme_window_type type, const char* token) -{ - int used; - char buffer[32]; - messagetrans_lookup(&theme->iconNames.cb, token, buffer, 32, 0,0,0,0, &used); - if (used > 0) - return atoi(buffer); - else - return -1; -} +/** + * Create a toolbar from the current theme. + * + * The buffers url_buffer and status_buffer must be at least 256 bytes each, + * throbber_buffer at least 12 bytes; + */ -void ro_theme_set_indirected(ro_theme* theme, char* find, wimp_window* win, theme_window_type wintype, char* indirected) +wimp_w ro_theme_create_toolbar(char *url_buffer, char *status_buffer, + char *throbber_buffer) { - int context = 0, used; - char token[32]; + wimp_w w; -fprintf(stderr, "setting indirected text...\n"); -fprintf(stderr, "enumerating token '%s'\n", find); + theme_toolbar_template->icons[ICON_TOOLBAR_URL].data.indirected_text.text = url_buffer; + theme_toolbar_template->icons[ICON_TOOLBAR_STATUS].data.indirected_text.text = status_buffer; + theme_toolbar_template->icons[ICON_TOOLBAR_THROBBER].data.indirected_sprite.id = + (osspriteop_id) throbber_buffer; - while (messagetrans_enumerate_tokens(&theme->iconNames.cb, find, token, 32, context, &used, &context)) - { - fprintf(stderr, "finding theme icon to set indirected text\n"); - win->icons[ro_theme_icon(theme, wintype, token)].data.indirected_text.text = indirected; - } -fprintf(stderr, "<-- returning\n"); + w = wimp_create_window(theme_toolbar_template); + return w; } -wimp_w ro_theme_create_window(ro_theme* theme, ro_theme_window* create) -{ - wimp_window* win = NULL; - if (create == NULL) - return 0; +/** + * Return the height of the current toolbar. + */ - if (create->type == THEME_TOOLBAR) - { - int i; -fprintf(stderr, "Creating toolbar from theme (%d bytes)\n", sizeof(*theme->toolbar)); -for (i = 0; i < 152/4; i++) +int ro_theme_toolbar_height(void) { - fprintf(stderr, "%d:\x09%d\n", i,((int*)(theme->toolbar))[i]); + return abs(theme_toolbar_template->extent.y1 - theme_toolbar_template->extent.y0); } - //win = xcalloc(1024, 1); - //memcpy(win, theme->toolbar, 1024); - win = theme->toolbar; - win->flags = win->flags | wimp_WINDOW_FURNITURE_WINDOW; -// win->next = wimp_TOP; -// win->title_flags = wimp_ICON_TEXT; - win->sprite_area = theme->sprites; - ro_theme_set_indirected(theme, "TOOLBAR_URL*", win, THEME_TOOLBAR, create->data.toolbar.indirected_url); - ro_theme_set_indirected(theme, "TOOLBAR_STATUS*", win, THEME_TOOLBAR, create->data.toolbar.indirected_status); - } - else if (create->type == THEME_THEMEINFO) - { -fprintf(stderr, "Creating theme_info from theme (%d bytes)\n", sizeof(*theme->theme_info)); -// win = xcalloc(sizeof(*theme->theme_info), 1); -// memcpy(win, theme->toolbar, sizeof(*theme->theme_info)); - win = theme->theme_info; - } - if (win != NULL) - { - wimp_w w = wimp_create_window(win); - //xfree(win); - return w; - } - else - return 0; -} - -int ro_theme_toolbar_height(ro_theme* theme) -{ - return abs(theme->toolbar->extent.y1 - theme->toolbar->extent.y0); -} +/** + * Resize the URL icon in a toolbar. + */ -void ro_theme_resize(ro_theme* theme, theme_window_type wintype, wimp_w w, int width, int height) +void ro_theme_resize_toolbar(wimp_w w, int width, int height) { - int context = 0, used; - char token[32]; - char* find; - char formula[256]; - char buffer[256]; - char widths[32]; - char heights[32]; - - sprintf(widths, "%d", width); - sprintf(heights, "%d", height); - - if (wintype == THEME_TOOLBAR) - find = "TOOLBAR_*"; - else - return; - - while (messagetrans_enumerate_tokens(&theme->iconSizes.cb, find, token, 32, context, &used, &context)) - { - char* x0;// = strstr(token, "_X0"); - char* x1;// = strstr(token, "_X1"); - - x0 = strstr(token, "_X0"); - x1 = strstr(token, "_X1"); + wimp_icon_state ic = {w, ICON_TOOLBAR_URL}; + wimp_get_icon_state(&ic); - if (x0 != 0 || x1 != 0) - { - char* icon_num = token + strlen(find) - 1; - char* underscore = token + strlen(find) - 1; - wimp_i i; - int new_x, rx0, rx1; - - messagetrans_lookup(&theme->iconSizes.cb, token, formula, 255, widths, heights,0,0, &used); - - while (*underscore > 32) - { - if (*underscore == '_') - *underscore = '\0'; - underscore++; - } - - i = (wimp_i) atoi(icon_num); - - if (os_evaluate_expression(formula, buffer, 255, &new_x) == 0) - { - wimp_icon_state ic; - - ic.w = w; - ic.i = i; - wimp_get_icon_state(&ic); - - rx0 = ic.icon.extent.x0; - rx1 = ic.icon.extent.x1; - - if (x0 != 0) - { - if (new_x < rx0) - rx0 = new_x; - wimp_resize_icon(w, i, new_x, ic.icon.extent.y0, ic.icon.extent.x1, ic.icon.extent.y1); - } - else if (x1 != 0) - { - if (new_x > rx1) - rx1 = new_x; - wimp_resize_icon(w, i, ic.icon.extent.x0, ic.icon.extent.y0, new_x, ic.icon.extent.y1); - } - wimp_force_redraw(w, rx0, ic.icon.extent.y0, rx1, ic.icon.extent.y1); - } - } - } + wimp_resize_icon(w, ICON_TOOLBAR_URL, ic.icon.extent.x0, ic.icon.extent.y0, + width - 8, ic.icon.extent.y1); + wimp_force_redraw(w, ic.icon.extent.x0, ic.icon.extent.y0, + width, ic.icon.extent.y1); } - -- cgit v1.2.3