initial commit
[kugel-rb.git] / apps / gui / icon.c
bloba9618bc5e8839788f9d37a177da1cce5772a92c4
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 Jonathan Gordon
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include "inttypes.h"
25 #include "config.h"
26 #include "icon.h"
27 #include "screen_access.h"
28 #include "icons.h"
29 #include "settings.h"
30 #include "bmp.h"
31 #include "filetypes.h"
32 #include "language.h"
34 #include "bitmaps/default_icons.h"
35 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
36 #include "bitmaps/remote_default_icons.h"
37 #endif
39 /* These are just the file names, the full path is snprint'ed when used */
40 #define DEFAULT_VIEWER_BMP "viewers"
41 #define DEFAULT_REMOTE_VIEWER_BMP "remote_viewers"
43 /* These can be defined in config-<target>.h, if it is not provide defaults */
44 #if !defined(MAX_ICON_HEIGHT)
45 #define MAX_ICON_HEIGHT 24
46 #endif
48 #if !defined(MAX_ICON_WIDTH)
49 #define MAX_ICON_WIDTH 24
50 #endif
52 /* We dont actually do anything with these pointers,
53 but they need to be grouped like this to save code
54 so storing them as void* is ok. (stops compile warning) */
55 static const struct bitmap inbuilt_iconset[NB_SCREENS] =
58 .width = BMPWIDTH_default_icons,
59 .height = BMPHEIGHT_default_icons,
60 .data = (unsigned char*)default_icons,
62 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
64 .width = BMPWIDTH_remote_default_icons,
65 .height = BMPHEIGHT_remote_default_icons,
66 .data = (unsigned char*)remote_default_icons,
68 #endif
71 #define IMG_BUFSIZE (MAX_ICON_HEIGHT * MAX_ICON_WIDTH * \
72 Icon_Last_Themeable *LCD_DEPTH/8)
73 static unsigned char icon_buffer[NB_SCREENS][IMG_BUFSIZE];
74 static bool custom_icons_loaded[NB_SCREENS] = {false};
75 static struct bitmap user_iconset[NB_SCREENS];
77 static unsigned char viewer_icon_buffer[NB_SCREENS][IMG_BUFSIZE];
78 static bool viewer_icons_loaded[NB_SCREENS] = {false};
79 static struct bitmap viewer_iconset[NB_SCREENS];
82 #define ICON_HEIGHT(screen) (!custom_icons_loaded[screen]? \
83 inbuilt_iconset : user_iconset)[screen].height \
84 / Icon_Last_Themeable
86 #define ICON_WIDTH(screen) (!custom_icons_loaded[screen]? \
87 inbuilt_iconset : user_iconset)[screen].width
89 /* x,y in letters, not pixles */
90 void screen_put_icon(struct screen * display,
91 int x, int y, enum themable_icons icon)
93 screen_put_icon_with_offset(display, x, y, 0, 0, icon);
96 static void screen_put_icon_with_offset_ex(struct screen * display,
97 int x, int y, int off_x, int off_y, int height,
98 enum themable_icons icon)
100 const int screen = display->screen_type;
101 const int icon_width = ICON_WIDTH(screen);
102 const int icon_height = ICON_HEIGHT(screen);
103 int xpos, ypos;
104 int width, _height;
105 display->getstringsize((unsigned char *)"M", &width, &_height);
106 if (height > 0)
107 _height = height;
108 xpos = x*icon_width + off_x;
109 ypos = y*_height + off_y;
111 if ( _height > icon_height )/* center the cursor */
112 ypos += (_height - icon_height) / 2;
113 screen_put_iconxy(display, xpos, ypos, icon);
116 void screen_put_icon_with_offset(struct screen * display,
117 int x, int y, int off_x, int off_y,
118 enum themable_icons icon)
120 screen_put_icon_with_offset_ex(display,x,y,off_x,off_y, 0, icon);
123 void screen_put_icon_with_offset_h(struct screen * display,
124 int x, int y, int off_x, int off_y, int height,
125 enum themable_icons icon)
127 screen_put_icon_with_offset_ex(display,x,y,off_x,off_y, height, icon);
130 /* x,y in pixels */
131 void screen_put_iconxy(struct screen * display,
132 int xpos, int ypos, enum themable_icons icon)
134 const void *data;
135 const int screen = display->screen_type;
136 const int width = ICON_WIDTH(screen);
137 const int height = ICON_HEIGHT(screen);
138 const int is_rtl = lang_is_rtl();
139 int stride;
140 const struct bitmap *iconset;
141 screen_bitmap_part_func *draw_func = NULL;
143 if (icon == Icon_NOICON)
145 if (is_rtl)
146 xpos = display->getwidth() - xpos - width;
147 screen_clear_area(display, xpos, ypos, width, height);
148 return;
150 else if (icon >= Icon_Last_Themeable)
152 iconset = &viewer_iconset[screen];
153 icon -= Icon_Last_Themeable;
154 if (!viewer_icons_loaded[screen] ||
155 (global_status.viewer_icon_count * height > iconset->height) ||
156 (icon * height + height > iconset->height))
158 screen_put_iconxy(display, xpos, ypos, Icon_Questionmark);
159 return;
162 else if (custom_icons_loaded[screen])
164 iconset = &user_iconset[screen];
166 else
168 iconset = &inbuilt_iconset[screen];
170 data = iconset->data;
171 stride = STRIDE(display->screen_type, iconset->width, iconset->height);
173 /* add some left padding to the icons if they are on the edge */
174 if (xpos == 0)
175 xpos++;
177 if (is_rtl)
178 xpos = display->getwidth() - xpos - width;
180 #if (LCD_DEPTH == 16) || defined(LCD_REMOTE_DEPTH) && (LCD_REMOTE_DEPTH == 16)
181 if (display->depth == 16)
182 draw_func = display->transparent_bitmap_part;
183 else
184 #endif
185 draw_func = display->bitmap_part;
187 draw_func(data, 0, height * icon, stride, xpos, ypos, width, height);
190 void screen_put_cursorxy(struct screen * display, int x, int y, bool on)
192 #ifdef HAVE_LCD_BITMAP
193 screen_put_icon(display, x, y, on?Icon_Cursor:0);
194 #else
195 screen_put_icon(display, x, y, on?CURSOR_CHAR:-1);
196 #endif
199 enum Iconset {
200 Iconset_Mainscreen,
201 Iconset_Mainscreen_viewers,
202 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
203 Iconset_Remotescreen,
204 Iconset_Remotescreen_viewers,
205 #endif
208 static void load_icons(const char* filename, enum Iconset iconset)
210 int size_read;
211 bool *loaded_ok = NULL;
212 struct bitmap *bmp = NULL;
213 int bmpformat = (FORMAT_NATIVE|FORMAT_DITHER);
215 switch (iconset)
217 case Iconset_Mainscreen:
218 loaded_ok = &custom_icons_loaded[SCREEN_MAIN];
219 bmp = &user_iconset[SCREEN_MAIN];
220 bmp->data = icon_buffer[SCREEN_MAIN];
221 break;
222 case Iconset_Mainscreen_viewers:
223 loaded_ok = &viewer_icons_loaded[SCREEN_MAIN];
224 bmp = &viewer_iconset[SCREEN_MAIN];
225 bmp->data = viewer_icon_buffer[SCREEN_MAIN];
226 break;
227 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
228 case Iconset_Remotescreen:
229 loaded_ok = &custom_icons_loaded[SCREEN_REMOTE];
230 bmp = &user_iconset[SCREEN_REMOTE];
231 bmp->data = icon_buffer[SCREEN_REMOTE];
232 bmpformat |= FORMAT_REMOTE;
233 break;
234 case Iconset_Remotescreen_viewers:
235 loaded_ok = &viewer_icons_loaded[SCREEN_REMOTE];
236 bmp = &viewer_iconset[SCREEN_REMOTE];
237 bmp->data = viewer_icon_buffer[SCREEN_REMOTE];
238 bmpformat |= FORMAT_REMOTE;
239 break;
240 #endif
243 *loaded_ok = false;
244 if (filename[0] && filename[0] != '-')
246 char path[MAX_PATH];
248 snprintf(path, sizeof(path), ICON_DIR "/%s.bmp", filename);
249 size_read = read_bmp_file(path, bmp, IMG_BUFSIZE, bmpformat, NULL);
250 if (size_read > 0)
252 *loaded_ok = true;
258 void icons_init(void)
260 load_icons(global_settings.icon_file, Iconset_Mainscreen);
262 if (global_settings.viewers_icon_file[0] &&
263 global_settings.viewers_icon_file[0] != '-')
265 load_icons(global_settings.viewers_icon_file,
266 Iconset_Mainscreen_viewers);
267 read_viewer_theme_file();
269 else
271 load_icons(DEFAULT_VIEWER_BMP, Iconset_Mainscreen_viewers);
274 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
275 load_icons(global_settings.remote_icon_file,
276 Iconset_Remotescreen);
278 if (global_settings.remote_viewers_icon_file[0] &&
279 global_settings.remote_viewers_icon_file[0] != '-')
281 load_icons(global_settings.remote_viewers_icon_file,
282 Iconset_Remotescreen_viewers);
284 else
286 load_icons(DEFAULT_REMOTE_VIEWER_BMP,
287 Iconset_Remotescreen_viewers);
289 #endif
292 int get_icon_width(enum screen_type screen_type)
294 return ICON_WIDTH(screen_type);