forked from iPodLinux-Community/Floydzilla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
textview.c
383 lines (333 loc) · 8.97 KB
/
textview.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
* Copyright (C) 2004 David Carne, matz-josh
* Copyright (C) 2005 Courtney Cavin
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "pz.h"
static GR_GC_ID tv_gc;
static GR_WINDOW_ID tv_wid;
static GR_WINDOW_INFO tv_winfo;
static char *g_title;
static int last_y_top;
static unsigned int totalLines = 0;
static int lines_per_screen = 7;
static unsigned int currentLine = 0;
static char ** lineData;
void destroy_textview_window(char *msg)
{
pz_close_window(tv_wid);
GrDestroyGC(tv_gc);
free(g_title);
if(msg!=NULL)
pz_error(msg);
}
static void printPage(int startLine, int x, int y, int w, int h)
{
int i;
GR_SIZE width, height, base;
GrGetGCTextSize(tv_gc, "M", -1, GR_TFASCII, &width, &height, &base);
for (i = startLine;i < startLine + lines_per_screen && i < totalLines;i++)
{
GrText(tv_wid, tv_gc, 3, (i - startLine + 1) * height + 4,
lineData[i], -1, GR_TFASCII);
}
}
static void buildLineData(char *starttextptr)
{
char * curtextptr;
char ** localAr;
int allocedlines;
currentLine = 0;
curtextptr = starttextptr;
if((localAr = (char **)malloc(sizeof(char *) * 20))==NULL) {
destroy_textview_window("malloc failed");
return;
}
allocedlines = 20;
/* While we haven't reached the end of the file */
while (*curtextptr != '\0')
{
char * sol;
/* See if we need to allocate more lines to the line storage */
if (allocedlines < currentLine + 1)
{
if((localAr = (char**)realloc(localAr, sizeof(char *) *
(allocedlines + 20)))==NULL) {
destroy_textview_window("realloc failed");
return;
}
allocedlines += 20;
}
sol = curtextptr;
while (1) {
GR_SIZE width, height, base;
if((*curtextptr == '\r') || (*curtextptr == '\n'))
break;
else if(*curtextptr == '\0')
break;
GrGetGCTextSize(tv_gc, sol, curtextptr - sol + 1,
GR_TFASCII, &width, &height, &base);
if(width > tv_winfo.width - 16) {
char *optr;
/* backtrack to last whitespace or hyphen */
for(optr=curtextptr; optr>sol; optr--) {
if(isspace(*optr) || *optr=='\t' ||
*optr=='-') {
curtextptr=optr;
curtextptr++;
break;
}
}
break;
}
curtextptr++;
}
if((localAr[currentLine] = malloc((curtextptr - sol + 1) *
sizeof(char)))==NULL) {
destroy_textview_window(_("malloc failed"));
return;
}
snprintf(localAr[currentLine], curtextptr - sol + 1, "%s", sol);
while ((sol = strchr(localAr[currentLine], '\t')))
*sol = ' ';
if(*curtextptr == '\r')
curtextptr++;
if(*curtextptr == '\n')
curtextptr++;
currentLine++;
}
totalLines = currentLine;
currentLine = 0;
lineData = localAr;
}
static void draw_scrollbar(const int height, const int y_top)
{
int y_bottom = y_top + height;
/* draw the containing box */
GrSetGCForeground(tv_gc, appearance_get_color( CS_SCRLBDR ));
GrRect(tv_wid, tv_gc, tv_winfo.width - 8, 1, 8, tv_winfo.height - 1);
/* erase the scrollbar */
GrSetGCForeground(tv_gc, appearance_get_color( CS_SCRLCTNR ));
GrFillRect(tv_wid, tv_gc, tv_winfo.width - (8 - 1), 1 + 1,
(8 - 2), y_top - (1 + 1));
GrFillRect(tv_wid, tv_gc, tv_winfo.width - (8 - 1), y_bottom, (8 - 2),
(tv_winfo.height - 1) - y_bottom);
/* draw the bar */
GrSetGCForeground(tv_gc, appearance_get_color( CS_SCRLKNOB ));
GrFillRect(tv_wid, tv_gc, tv_winfo.width - (8 - 1), y_top, (8 - 2), height);
GrSetGCForeground(tv_gc, appearance_get_color(CS_FG));
}
static void drawtext(void)
{
int per = (totalLines - lines_per_screen) <= lines_per_screen ?
lines_per_screen * 100 / totalLines :
lines_per_screen * 100 / (totalLines - lines_per_screen);
int height = (tv_winfo.height - 2) * (per < 3 ? 3 : per) / 100;
int y_top;
GrSetGCForeground(tv_gc, appearance_get_color(CS_BG));
GrFillRect(tv_wid, tv_gc, 0, 0, tv_winfo.width - 8, tv_winfo.height);
GrSetGCForeground(tv_gc, appearance_get_color(CS_FG));
/* Draw the text */
printPage(currentLine, 0,0,0,0);
if (totalLines > lines_per_screen) {
y_top = ((((tv_winfo.height - 3) - height)*100) * currentLine /
(totalLines - lines_per_screen)) / 100 + 2;
if (y_top != last_y_top)
draw_scrollbar(height, y_top);
last_y_top = y_top;
}
}
static void textview_do_draw()
{
pz_draw_header(g_title);
last_y_top = -1;
drawtext();
}
static int textview_do_keystroke(GR_EVENT * event)
{
int i;
int ret = 1;
switch (event->type) {
case GR_EVENT_TYPE_KEY_DOWN:
switch (event->keystroke.ch) {
case 'm':
case 'q':
destroy_textview_window(NULL);
for (i = 0; i < totalLines; i++)
free(lineData[i]);
free (lineData);
break;
case 'r':
if (currentLine + lines_per_screen < totalLines) {
currentLine++;
drawtext();
}
break;
case 'l':
if (currentLine > 0) {
currentLine--;
drawtext();
}
break;
case 'f':
/* forward key pressed: go down one screen */
if (currentLine + lines_per_screen < totalLines) {
currentLine = currentLine + lines_per_screen;
if (currentLine + lines_per_screen > totalLines)
/* if we went down beyond the end of
* the text we go to the end of the
* text */
currentLine = totalLines - lines_per_screen;
drawtext();
}
break;
case 'w':
/* rewind key pressed: go up one screen
* if it's already the first line, nothing is done */
if (currentLine > 0) {
if (currentLine < lines_per_screen)
/* if there is not a full screen above
* the current one go up to the very
* first line */
currentLine = 0;
else /* go up one screen */
currentLine = currentLine -
lines_per_screen;
drawtext();
}
break;
default:
ret = 0;
}
break;
}
return ret;
}
int is_text_type(char * extension)
{
return strcmp(extension, ".txt") == 0;
}
int is_ascii_file(char *filename)
{
FILE *fp;
unsigned char buf[20], *ptr;
long file_len;
struct stat ftype;
stat(filename, &ftype);
if(S_ISBLK(ftype.st_mode)||S_ISCHR(ftype.st_mode))
return 0;
if((fp=fopen(filename, "r"))==NULL) {
perror(filename);
return 0;
}
fseek(fp, 0, SEEK_END);
file_len = ftell(fp);
rewind(fp);
fread(buf, file_len<20?file_len:20, 1, fp);
fclose(fp);
for(ptr=buf;ptr-buf<(file_len<20?file_len:20);ptr++)
if(*ptr<7||*ptr>127)
return 0;
return 1;
}
void create_textview_window()
{
GR_SIZE width, height, base;
tv_gc = pz_get_gc(1);
GrSetGCUseBackground(tv_gc, GR_FALSE);
GrSetGCForeground(tv_gc, appearance_get_color(CS_FG));
tv_wid = pz_new_window(0, HEADER_TOPLINE + 1, screen_info.cols,
screen_info.rows - (HEADER_TOPLINE + 1),
textview_do_draw, textview_do_keystroke);
GrSetWindowBackgroundColor(tv_wid, appearance_get_color(CS_BG));
GrSelectEvents(tv_wid, GR_EVENT_MASK_EXPOSURE| GR_EVENT_MASK_KEY_UP|
GR_EVENT_MASK_KEY_DOWN);
GrGetGCTextSize(tv_gc, "M", -1, GR_TFASCII, &width, &height, &base);
lines_per_screen = (int)(screen_info.rows - (HEADER_TOPLINE + 1)) /
(height + 1);
GrMapWindow(tv_wid);
GrGetWindowInfo(tv_wid, &tv_winfo);
}
void new_textview_window(char *filename)
{
char *buf;
char tmp[512];
FILE *fp;
size_t file_len;
g_title = strdup(filename);
create_textview_window();
if ((fp = fopen(filename,"r"))==NULL) {
destroy_textview_window(_("Error opening file"));
return;
}
fseek(fp, 0, SEEK_END);
file_len = ftell(fp);
rewind(fp);
if(file_len==0) {
buf = '\0';
while(fgets(tmp, 512, fp)!=NULL) {
if((buf = realloc(buf, ((buf=='\0'?0:strlen(buf)) +
512) * sizeof(char)))==NULL) {
destroy_textview_window(_("realloc failed"));
fclose(fp);
return;
}
if(file_len==0) {
strncpy(buf, tmp, 512);
file_len=1;
} else
strncat(buf, tmp, 512);
}
}
else {
if((buf = calloc(file_len+1, 1))==NULL) {
destroy_textview_window(_("calloc failed"));
fclose(fp);
return;
}
if(fread(buf, 1, file_len, fp)!=file_len)
pz_error(_("unknown read error, continuing"));
}
if(buf=='\0') {
destroy_textview_window(NULL);
new_message_window(_("Empty File"));
fclose(fp);
return;
}
fclose(fp);
buildLineData(buf);
free(buf);
}
void new_stringview_window(char *buf, char *title)
{
char *exbuf;
g_title = strdup(title);
create_textview_window();
if((exbuf = malloc(strlen(buf)+1))==NULL) {
destroy_textview_window(_("malloc failed"));
return;
}
snprintf(exbuf, strlen(buf)+1, "%s0", buf);
buildLineData(exbuf);
free(exbuf);
}