Rename GP_Context -> GP_Pixmap
[gfxprim.git] / demos / c_simple / fileview.c
blobd5c870b65f8c163bcc17164d116123b7e2aba3ab
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_Pixmap *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;
201 case GP_EV_SYS_RESIZE:
202 GP_BackendResizeAck(backend);
203 redraw_screen();
204 GP_BackendFlip(backend);
205 break;
207 break;
212 static int read_file_head(const char *filename)
214 FILE *f = fopen(filename, "r");
215 char buf[512];
217 if (f == NULL) {
218 fprintf(stderr, "Could not open file: %s\n", filename);
219 return 0;
222 for (;;) {
224 if (fgets(buf, 511, f) == NULL)
225 break;
227 struct FileLine *line = malloc(sizeof(*line));
228 line->text = strdup(buf);
229 line->next = NULL;
230 line->prev = NULL;
232 if (first_line == NULL) {
233 first_line = line;
234 last_line = line;
235 } else {
236 line->prev = last_line;
237 last_line->next = line;
238 last_line = line;
242 fclose(f);
243 return 1;
246 int main(int argc, char *argv[])
248 const char *backend_opts = "X11";
250 if (argc == 1) {
251 fprintf(stderr, "No file specified\n");
252 return 1;
255 if (argc > 2)
256 font = GP_FontFaceLoad(argv[2], 0, 16);
258 if (!read_file_head(argv[1]))
259 return 1;
261 backend = GP_BackendInit(backend_opts, "File View");
263 if (backend == NULL) {
264 fprintf(stderr, "Failed to initalize backend '%s'\n",
265 backend_opts);
266 return 1;
269 win = backend->pixmap;
271 white_pixel = GP_RGBToPixmapPixel(0xff, 0xff, 0xff, win);
272 gray_pixel = GP_RGBToPixmapPixel(0xbe, 0xbe, 0xbe, win);
273 dark_gray_pixel = GP_RGBToPixmapPixel(0x7f, 0x7f, 0x7f, win);
274 black_pixel = GP_RGBToPixmapPixel(0x00, 0x00, 0x00, win);
275 red_pixel = GP_RGBToPixmapPixel(0xff, 0x00, 0x00, win);
276 blue_pixel = GP_RGBToPixmapPixel(0x00, 0x00, 0xff, win);
278 redraw_screen();
279 GP_BackendFlip(backend);
281 event_loop();
283 return 0;