1 /*****************************************************************************
2 * This file is part of gfxprim library. *
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. *
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. *
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 *
19 * Copyright (C) 2009-2010 Jiri "BlueBear" Dluhos *
20 * <jiri.bluebear.dluhos@gmail.com> *
22 * Copyright (C) 2009-2013 Cyril Hrubis <metan@ucw.cz> *
24 *****************************************************************************/
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;
44 static GP_FontFace
*font
;
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
;
63 style
.font
= &GP_DefaultConsoleFont
;
66 style
.font
= &GP_DefaultProportionalFont
;
69 style
.font
= GP_FontTinyMono
;
72 style
.font
= GP_FontTiny
;
75 style
.font
= GP_FontC64
;
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
;
95 for (i
= 0; i
< win
->h
/GP_TextHeight(&style
); i
++) {
98 GP_Text(win
, &style
, 16, 16 + (1.0 * GP_TextHeight(&style
))*i
,
99 align
, black_pixel
, gray_pixel
, line
->text
);
104 static void warp_up(int lines
)
107 if (first_line
->prev
!= NULL
)
108 first_line
= first_line
->prev
;
111 GP_BackendFlip(backend
);
114 static void warp_down(int lines
)
117 if (first_line
->next
!= NULL
)
118 first_line
= first_line
->next
;
121 GP_BackendFlip(backend
);
124 void event_loop(void)
129 GP_BackendWaitEvent(backend
, &ev
);
133 if (ev
.code
!= GP_EV_KEY_DOWN
)
136 switch (ev
.val
.key
.key
) {
139 font_flag
= (font_flag
+ 1) % 6;
141 font_flag
= (font_flag
+ 1) % 5;
144 GP_BackendFlip(backend
);
149 GP_BackendFlip(backend
);
154 GP_BackendFlip(backend
);
165 GP_BackendFlip(backend
);
170 GP_BackendFlip(backend
);
172 case GP_KEY_RIGHT_BRACE
:
175 GP_BackendFlip(backend
);
177 case GP_KEY_LEFT_BRACE
:
181 GP_BackendFlip(backend
);
186 case GP_KEY_PAGE_DOWN
:
190 GP_BackendExit(backend
);
198 GP_BackendExit(backend
);
207 static int read_file_head(const char *filename
)
209 FILE *f
= fopen(filename
, "r");
213 fprintf(stderr
, "Could not open file: %s\n", filename
);
219 if (fgets(buf
, 511, f
) == NULL
)
222 struct FileLine
*line
= malloc(sizeof(*line
));
223 line
->text
= strdup(buf
);
227 if (first_line
== NULL
) {
231 line
->prev
= last_line
;
232 last_line
->next
= line
;
241 int main(int argc
, char *argv
[])
243 const char *backend_opts
= "X11";
246 fprintf(stderr
, "No file specified\n");
251 font
= GP_FontFaceLoad(argv
[2], 0, 16);
253 if (!read_file_head(argv
[1]))
256 backend
= GP_BackendInit(backend_opts
, "File View");
258 if (backend
== NULL
) {
259 fprintf(stderr
, "Failed to initalize backend '%s'\n",
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
);
274 GP_BackendFlip(backend
);