From a94781edc034b60c72ea148e57f6f1cdc545377d Mon Sep 17 00:00:00 2001 From: Ole Loots Date: Tue, 10 Apr 2012 23:10:49 +0000 Subject: Added function load_icon svn path=/trunk/netsurf/; revision=13847 --- atari/misc.c | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++++++---- atari/misc.h | 9 +++-- 2 files changed, 107 insertions(+), 8 deletions(-) (limited to 'atari') diff --git a/atari/misc.c b/atari/misc.c index ded8723fb..341f771a7 100755 --- a/atari/misc.c +++ b/atari/misc.c @@ -15,17 +15,23 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - -#include + +#include #include -#include +#include +#include #include +#include #include -#include - +#include + +#include "content/content.h" +#include "content/hlcache.h" #include "desktop/cookies.h" #include "desktop/mouse.h" -#include "desktop/cookies.h" +#include "desktop/cookies.h" +#include "desktop/tree.h" +#include "desktop/options.h" #include "utils/messages.h" #include "utils/utils.h" #include "utils/url.h" @@ -164,6 +170,94 @@ OBJECT *get_tree( int idx) { OBJECT *tree; RsrcGaddr( h_gem_rsrc, R_TREE, idx, &tree); return tree; +} + + + +/** + * Callback for load_icon(). Should be removed once bitmaps get loaded directly + * from disc + */ +static nserror load_icon_callback(hlcache_handle *handle, + const hlcache_event *event, void *pw) +{ + return NSERROR_OK; +} + + +/** + * utility function. Placed here so that this code doesn't have to be + * copied by each user. + * + * \param name the name of the loaded icon, if it's not a full path the icon is + * looked for in the directory specified by icons_dir + * \return the icon in form of a content or NULL on failure + */ +hlcache_handle *load_icon(const char *name, hlcache_handle_callback cb, + void * pw ) +{ + char *url = NULL; + const char *icon_url = NULL; + int len; + hlcache_handle *c; + nserror err; + nsurl *icon_nsurl; + char * icons_dir = nsoption_charp(tree_icons_path); + + /** @todo something like bitmap_from_disc is needed here */ + + if (!strncmp(name, "file://", 7)) { + icon_url = name; + } else { + char *native_path; + + if (icons_dir == NULL) + return NULL; + + /* path + separator + leafname + '\0' */ + len = strlen(icons_dir) + 1 + strlen(name) + 1; + native_path = malloc(len); + if (native_path == NULL) { + LOG(("malloc failed")); + warn_user("NoMemory", 0); + return NULL; + } + + /* Build native path */ + memcpy(native_path, icons_dir, + strlen(icons_dir) + 1); + path_add_part(native_path, len, name); + + /* Convert native path to URL */ + url = path_to_url(native_path); + + free(native_path); + icon_url = url; + } + + err = nsurl_create(icon_url, &icon_nsurl); + if (err != NSERROR_OK) { + if (url != NULL) + free(url); + return NULL; + } + + /* Fetch the icon */ + err = hlcache_handle_retrieve(icon_nsurl, 0, 0, 0, + ((cb != NULL) ? cb : load_icon_callback), pw, 0, + CONTENT_IMAGE, &c); + + nsurl_unref(icon_nsurl); + + /* If we built the URL here, free it */ + if (url != NULL) + free(url); + + if (err != NSERROR_OK) { + return NULL; + } + + return c; } void gem_set_cursor( MFORM_EX * cursor ) diff --git a/atari/misc.h b/atari/misc.h index 7064e744e..edd026ba4 100755 --- a/atari/misc.h +++ b/atari/misc.h @@ -20,6 +20,8 @@ #define NS_ATARI_MISC_H #include "cflib.h" +#include "content/content.h" +#include "content/hlcache.h" #include "desktop/textinput.h" #include "atari/gui.h" @@ -42,13 +44,16 @@ struct gui_window * find_gui_window( unsigned long, short mode ); struct gui_window * find_cmp_window( COMPONENT * c ); OBJECT *get_tree( int idx ); char *get_rsc_string( int idx ); -void gem_set_cursor( MFORM_EX * cursor ); +void gem_set_cursor( MFORM_EX * cursor ); +hlcache_handle *load_icon( const char *name, hlcache_handle_callback cb, + void * pw ); void dbg_grect( char * str, GRECT * r ); void dbg_lgrect( char * str, LGRECT * r ); void dbg_pxy( char * str, short * pxy ); void * ldg_open( char * name, short * global ); void * ldg_find( char * name, short * ldg ); +const char * file_select( const char * title, const char * name ); int ldg_close( void * ldg, short * global ); long nkc_to_input_key(short nkc, long * ucs4_out); -const char * file_select( const char * title, const char * name ); + #endif -- cgit v1.2.3