Added pagedown/pageup into fileview.
[gfxprim.git] / targets / sdl / tests / fileview.c
blob2889afaf46ba04f39b4f4a3c74936d4a71257b9b
1 /*****************************************************************************
2 * This file is part of gfxprim library. *
3 * *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
8 * *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
18 * *
19 * Copyright (C) 2009-2010 Jiri "BlueBear" Dluhos *
20 * <jiri.bluebear.dluhos@gmail.com> *
21 * *
22 * Copyright (C) 2009-2010 Cyril Hrubis <metan@ucw.cz> *
23 * *
24 *****************************************************************************/
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <SDL/SDL.h>
30 #include "GP.h"
31 #include "GP_SDL.h"
33 /* the display */
34 SDL_Surface *display = NULL;
35 GP_Context context;
37 /* precomputed color pixels in display format */
38 static GP_Pixel white_pixel, gray_pixel, dark_gray_pixel, black_pixel,
39 red_pixel, blue_pixel;
41 /* draw using proportional font? */
42 static int font_flag = 0;
44 /* font tracking */
45 static int tracking = 0;
47 /* font to be used */
48 GP_Font *font;
50 struct FileLine {
51 char *text; /* null-terminated, malloc'd string */
52 struct FileLine *next; /* next line or NULL for the last line */
53 struct FileLine *prev;
56 struct FileLine *first_line = NULL;
57 struct FileLine *last_line = NULL;
59 void redraw_screen(void)
61 SDL_LockSurface(display);
63 GP_Fill(&context, gray_pixel);
65 GP_TextStyle style = GP_DEFAULT_TEXT_STYLE;
67 switch (font_flag) {
68 case 0:
69 style.font = &GP_default_console_font;
70 break;
71 case 1:
72 style.font = &GP_default_proportional_font;
73 break;
74 case 2:
75 style.font = font;
76 break;
79 style.pixel_xmul = 1;
80 style.pixel_ymul = 1;
81 style.pixel_xspace = 0;
82 style.pixel_yspace = 0;
83 style.char_xspace = tracking;
85 /* Text alignment (we are always drawing to the right
86 * and below the starting point).
88 int align = GP_ALIGN_RIGHT|GP_VALIGN_BELOW;
90 struct FileLine *line = first_line;
91 unsigned int i;
92 for (i = 0; i < 30; i++) {
93 if (line == NULL)
94 break;
96 GP_Text(&context, &style, 16, 16*i + 16, align, line->text, black_pixel);
97 line = line->next;
100 SDL_UnlockSurface(display);
103 static void warp_up(int lines)
105 while (lines-- > 0)
106 if (first_line->prev != NULL)
107 first_line = first_line->prev;
109 redraw_screen();
110 SDL_Flip(display);
113 static void warp_down(int lines)
115 while (lines-- > 0)
116 if (first_line->next != NULL)
117 first_line = first_line->next;
119 redraw_screen();
120 SDL_Flip(display);
123 void event_loop(void)
125 SDL_Event event;
127 while (SDL_WaitEvent(&event) > 0) {
128 switch (event.type) {
130 case SDL_VIDEOEXPOSE:
131 redraw_screen();
132 SDL_Flip(display);
133 break;
135 case SDL_KEYDOWN:
136 switch (event.key.keysym.sym) {
137 case SDLK_SPACE:
138 if (font)
139 font_flag = (font_flag + 1) % 3;
140 else
141 font_flag = (font_flag + 1) % 2;
143 redraw_screen();
144 SDL_Flip(display);
145 break;
146 case SDLK_RIGHT:
147 tracking++;
148 redraw_screen();
149 SDL_Flip(display);
150 break;
151 case SDLK_LEFT:
152 tracking--;
153 redraw_screen();
154 SDL_Flip(display);
155 break;
156 case SDLK_ESCAPE:
157 return;
158 case SDLK_UP:
159 warp_up(1);
160 break;
161 case SDLK_PAGEUP:
162 warp_up(29);
163 break;
164 case SDLK_DOWN:
165 warp_down(1);
166 break;
167 case SDLK_PAGEDOWN:
168 warp_down(29);
169 break;
170 default:
171 break;
173 break;
175 case SDL_QUIT:
176 return;
181 static int read_file_head(const char *filename)
183 FILE *f = fopen(filename, "r");
184 if (f == NULL) {
185 fprintf(stderr, "Could not open file: %s\n", filename);
186 return 0;
189 char buf[512];
190 for (;;) {
191 int retval = fscanf(f, "%511[^\n]\n", buf);
192 if (retval != 1) {
193 break;
195 buf[511] = '\0';
197 struct FileLine *line = malloc(sizeof(*line));
198 line->text = strdup(buf);
199 line->next = NULL;
200 line->prev = NULL;
202 if (first_line == NULL) {
203 first_line = line;
204 last_line = line;
205 } else {
206 line->prev = last_line;
207 last_line->next = line;
208 last_line = line;
212 fclose(f);
213 return 1;
216 int main(int argc, char *argv[])
218 if (argc == 1) {
219 fprintf(stderr, "No file specified\n");
220 return 1;
223 if (!read_file_head(argv[1]))
224 return 1;
226 /* Initialize SDL */
227 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) {
228 fprintf(stderr, "Could not initialize SDL: %s\n", SDL_GetError());
229 return 1;
232 /* Create a window with a software back surface */
233 display = SDL_SetVideoMode(640, 480, 0, SDL_SWSURFACE);
234 if (display == NULL) {
235 fprintf(stderr, "Could not open display: %s\n", SDL_GetError());
236 goto fail;
239 /* Initialize a GP context from the SDL display */
240 GP_SDL_ContextFromSurface(&context, display);
242 /* Load colors suitable for the display */
243 GP_ColorNameToPixel(&context, GP_COL_WHITE, &white_pixel);
244 GP_ColorNameToPixel(&context, GP_COL_GRAY_LIGHT, &gray_pixel);
245 GP_ColorNameToPixel(&context, GP_COL_GRAY_DARK, &dark_gray_pixel);
246 GP_ColorNameToPixel(&context, GP_COL_BLACK, &black_pixel);
247 GP_ColorNameToPixel(&context, GP_COL_RED, &red_pixel);
248 GP_ColorNameToPixel(&context, GP_COL_BLUE, &blue_pixel);
250 redraw_screen();
251 SDL_Flip(display);
253 event_loop();
255 SDL_Quit();
256 return 0;
258 fail:
259 SDL_Quit();
260 return 1;