demos: Fix and cleanup GP_BackendInit() + docs.
[gfxprim/pasky.git] / demos / c_simple / fileview.c
blob2d06dae025489337f1e09606cbf9449ac0d69d2f
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-2013 Cyril Hrubis <metan@ucw.cz> *
23 * *
24 *****************************************************************************/
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
30 #include <GP.h>
32 static GP_Context *win;
33 static GP_Backend *backend;
35 static GP_Pixel white_pixel, gray_pixel, dark_gray_pixel, black_pixel,
36 red_pixel, blue_pixel;
38 static int font_flag = 0;
39 static int tracking = 0;
41 static int mul = 1;
42 static int space = 0;
44 static GP_FontFace *font;
46 struct FileLine {
47 char *text;
48 struct FileLine *next;
49 struct FileLine *prev;
52 struct FileLine *first_line = NULL;
53 struct FileLine *last_line = NULL;
55 void redraw_screen(void)
57 GP_Fill(win, gray_pixel);
59 GP_TextStyle style = GP_DEFAULT_TEXT_STYLE;
61 switch (font_flag) {
62 case 0:
63 style.font = &GP_DefaultConsoleFont;
64 break;
65 case 1:
66 style.font = &GP_DefaultProportionalFont;
67 break;
68 case 2:
69 style.font = GP_FontTinyMono;
70 break;
71 case 3:
72 style.font = GP_FontTiny;
73 break;
74 case 4:
75 style.font = GP_FontC64;
76 break;
77 case 5:
78 style.font = font;
79 break;
82 style.pixel_xmul = mul;
83 style.pixel_ymul = mul;
84 style.pixel_xspace = space;
85 style.pixel_yspace = space;
86 style.char_xspace = tracking;
88 /* Text alignment (we are always drawing to the right
89 * and below the starting point).
91 int align = GP_ALIGN_RIGHT|GP_VALIGN_BELOW;
93 struct FileLine *line = first_line;
94 unsigned int i;
95 for (i = 0; i < win->h/GP_TextHeight(&style); i++) {
96 if (line == NULL)
97 break;
98 GP_Text(win, &style, 16, 16 + (1.0 * GP_TextHeight(&style))*i,
99 align, black_pixel, gray_pixel, line->text);
100 line = line->next;
104 static void warp_up(int lines)
106 while (lines-- > 0)
107 if (first_line->prev != NULL)
108 first_line = first_line->prev;
110 redraw_screen();
111 GP_BackendFlip(backend);
114 static void warp_down(int lines)
116 while (lines-- > 0)
117 if (first_line->next != NULL)
118 first_line = first_line->next;
120 redraw_screen();
121 GP_BackendFlip(backend);
124 void event_loop(void)
126 GP_Event ev;
128 for (;;) {
129 GP_BackendWaitEvent(backend, &ev);
131 switch (ev.type) {
132 case GP_EV_KEY:
133 if (ev.code != GP_EV_KEY_DOWN)
134 continue;
136 switch (ev.val.key.key) {
137 case GP_KEY_SPACE:
138 if (font)
139 font_flag = (font_flag + 1) % 6;
140 else
141 font_flag = (font_flag + 1) % 5;
143 redraw_screen();
144 GP_BackendFlip(backend);
145 break;
146 case GP_KEY_RIGHT:
147 tracking++;
148 redraw_screen();
149 GP_BackendFlip(backend);
150 break;
151 case GP_KEY_LEFT:
152 tracking--;
153 redraw_screen();
154 GP_BackendFlip(backend);
155 break;
156 case GP_KEY_UP:
157 warp_up(1);
158 break;
159 case GP_KEY_DOWN:
160 warp_down(1);
161 break;
162 case GP_KEY_DOT:
163 space++;
164 redraw_screen();
165 GP_BackendFlip(backend);
166 break;
167 case GP_KEY_COMMA:
168 space--;
169 redraw_screen();
170 GP_BackendFlip(backend);
171 break;
172 case GP_KEY_RIGHT_BRACE:
173 mul++;
174 redraw_screen();
175 GP_BackendFlip(backend);
176 break;
177 case GP_KEY_LEFT_BRACE:
178 if (mul > 0)
179 mul--;
180 redraw_screen();
181 GP_BackendFlip(backend);
182 break;
183 case GP_KEY_PAGE_UP:
184 warp_up(30);
185 break;
186 case GP_KEY_PAGE_DOWN:
187 warp_down(30);
188 break;
189 case GP_KEY_ESC:
190 GP_BackendExit(backend);
191 exit(0);
192 break;
194 break;
195 case GP_EV_SYS:
196 switch(ev.code) {
197 case GP_EV_SYS_QUIT:
198 GP_BackendExit(backend);
199 exit(0);
200 break;
202 break;
207 static int read_file_head(const char *filename)
209 FILE *f = fopen(filename, "r");
210 char buf[512];
212 if (f == NULL) {
213 fprintf(stderr, "Could not open file: %s\n", filename);
214 return 0;
217 for (;;) {
219 if (fgets(buf, 511, f) == NULL)
220 break;
222 struct FileLine *line = malloc(sizeof(*line));
223 line->text = strdup(buf);
224 line->next = NULL;
225 line->prev = NULL;
227 if (first_line == NULL) {
228 first_line = line;
229 last_line = line;
230 } else {
231 line->prev = last_line;
232 last_line->next = line;
233 last_line = line;
237 fclose(f);
238 return 1;
241 int main(int argc, char *argv[])
243 const char *backend_opts = "X11";
245 if (argc == 1) {
246 fprintf(stderr, "No file specified\n");
247 return 1;
250 if (argc > 2)
251 font = GP_FontFaceLoad(argv[2], 0, 16);
253 if (!read_file_head(argv[1]))
254 return 1;
256 backend = GP_BackendInit(backend_opts, "File View");
258 if (backend == NULL) {
259 fprintf(stderr, "Failed to initalize backend '%s'\n",
260 backend_opts);
261 return 1;
264 win = backend->context;
266 white_pixel = GP_ColorToContextPixel(GP_COL_WHITE, win);
267 gray_pixel = GP_ColorToContextPixel(GP_COL_GRAY_LIGHT, win);
268 dark_gray_pixel = GP_ColorToContextPixel(GP_COL_GRAY_DARK, win);
269 black_pixel = GP_ColorToContextPixel(GP_COL_BLACK, win);
270 red_pixel = GP_ColorToContextPixel(GP_COL_RED, win);
271 blue_pixel = GP_ColorToContextPixel(GP_COL_BLUE, win);
273 redraw_screen();
274 GP_BackendFlip(backend);
276 event_loop();
278 return 0;