Rev 8429 | Rev 8438 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
8436 | maxcodehac | 1 | // getrusage |
2 | #include "resource.h" |
||
3 | |||
5496 | leency | 4 | #include |
4680 | right-hear | 5 | #include "fitz.h" |
6 | #include "mupdf.h" |
||
7 | #include "pdfapp.h" |
||
4696 | right-hear | 8 | #include "icons/allbtns.h" |
8436 | maxcodehac | 9 | #include "kolibri.h" |
4680 | right-hear | 10 | |
7533 | leency | 11 | void run_app() |
12 | { |
||
13 | return; |
||
14 | } |
||
4680 | right-hear | 15 | |
5496 | leency | 16 | /*==== DATA ====*/ |
17 | |||
18 | static char Title[1024] = "uPDF"; |
||
19 | static pdfapp_t gapp; |
||
20 | char debugstr[256]; |
||
21 | char do_not_blit=0; |
||
22 | |||
23 | #define TOOLBAR_HEIGHT 34 |
||
24 | struct process_table_entry Form; |
||
25 | |||
26 | #define DOCUMENT_BORDER 0x979797 |
||
27 | #define DOCUMENT_BG 0xABABAB |
||
28 | |||
5508 | leency | 29 | #define SCROLL_H 25 |
5496 | leency | 30 | |
5508 | leency | 31 | short show_area_w = 65; |
32 | short show_area_x; |
||
33 | |||
34 | char key_mode_enter_page_number; |
||
35 | int new_page_number; |
||
36 | |||
37 | static short window_center, draw_h, draw_w; |
||
38 | |||
5496 | leency | 39 | const char *help[] = { |
40 | "Keys:", |
||
41 | " ", |
||
42 | "PageUp - go to previous page", |
||
43 | "PageDown - go to next page", |
||
44 | "Home - go to first page", |
||
45 | "End - go to last page", |
||
46 | "Down arrow - scroll current page down", |
||
47 | "Up arrow - scroll current page up", |
||
48 | "+/- - zoom in/out", |
||
49 | "[ or l - rotate page 90 deg to the left", |
||
50 | "] or r - rotate page 90 deg to the right", |
||
5508 | leency | 51 | "g - grayscale on/off", |
5496 | leency | 52 | " ", |
53 | "Press Escape to hide help", |
||
54 | |||
55 | }; |
||
56 | |||
57 | /*==== CODE ====*/ |
||
8436 | maxcodehac | 58 | // Prototypes // |
59 | void RunApp(char app[], char param[]); |
||
60 | void winblit(pdfapp_t *app); |
||
61 | void DrawPagination(void); |
||
62 | void HandleNewPageNumber(unsigned char key); |
||
63 | void ApplyNewPageNumber(void); |
||
64 | void DrawWindow(void); |
||
5496 | leency | 65 | |
66 | |||
67 | // not implemented yet |
||
68 | void wincursor(pdfapp_t *app, int curs) { } |
||
69 | void winhelp(pdfapp_t *app) { } |
||
70 | void winresize(pdfapp_t *app, int w, int h) { } |
||
71 | void windocopy(pdfapp_t *app) { } |
||
72 | void winopenuri(pdfapp_t *app, char *buf) { } |
||
73 | void winrepaintsearch(pdfapp_t *app) { } |
||
74 | |||
75 | |||
4680 | right-hear | 76 | void winwarn(pdfapp_t *app, char *msg) |
77 | { |
||
78 | fprintf(stderr, "mupdf: %s\n", msg); |
||
79 | } |
||
80 | |||
5496 | leency | 81 | |
4680 | right-hear | 82 | void winerror(pdfapp_t *app, fz_error error) |
83 | { |
||
84 | fz_catch(error, "aborting"); |
||
85 | exit(1); |
||
86 | } |
||
87 | |||
5496 | leency | 88 | |
4680 | right-hear | 89 | char *winpassword(pdfapp_t *app, char *filename) |
90 | { |
||
91 | char *r = ""; |
||
92 | return r; |
||
7533 | leency | 93 | random(); |
4680 | right-hear | 94 | } |
95 | |||
96 | |||
97 | void wintitle(pdfapp_t *app, char *s) |
||
98 | { |
||
5496 | leency | 99 | char* param = *(char**)0x1C; |
100 | sprintf(Title,"%s - uPDF", strrchr(param, '/') + 1 ); |
||
4680 | right-hear | 101 | } |
102 | |||
103 | |||
104 | void winreloadfile(pdfapp_t *app) |
||
105 | { |
||
5496 | leency | 106 | //pdfapp_close(app); |
107 | //pdfapp_open(app, filename, 0, 1); |
||
4680 | right-hear | 108 | } |
109 | |||
110 | void winclose(pdfapp_t *app) |
||
111 | { |
||
112 | pdfapp_close(&gapp); |
||
8436 | maxcodehac | 113 | exit(0); |
4680 | right-hear | 114 | } |
115 | |||
8436 | maxcodehac | 116 | void RunOpenApp(char name[]) |
7533 | leency | 117 | { |
8436 | maxcodehac | 118 | char cmd[250] = "*pdf* "; |
119 | strcat(cmd, name); |
||
120 | RunApp("/sys/lod", cmd); |
||
7533 | leency | 121 | } |
5496 | leency | 122 | |
7533 | leency | 123 | |
5496 | leency | 124 | void winrepaint(pdfapp_t *app) |
4680 | right-hear | 125 | { |
5496 | leency | 126 | winblit(&gapp); |
4680 | right-hear | 127 | } |
128 | |||
129 | |||
5496 | leency | 130 | void winblit(pdfapp_t *app) |
4680 | right-hear | 131 | { |
132 | |||
5496 | leency | 133 | if (do_not_blit) return; |
4680 | right-hear | 134 | |
5508 | leency | 135 | if (key_mode_enter_page_number==1) HandleNewPageNumber(0); else DrawPagination(); |
136 | |||
5496 | leency | 137 | if (Form.client_width > gapp.image->w) window_center = (Form.client_width - gapp.image->w) / 2; else window_center = 0; |
4680 | right-hear | 138 | |
5821 | leency | 139 | gapp.panx = 0; |
7475 | leency | 140 | if (gapp.image->n == 4) { |
7624 | leency | 141 | kos_blit(window_center + Form.client_left, |
5496 | leency | 142 | Form.client_top + TOOLBAR_HEIGHT, |
143 | Form.client_width, |
||
144 | Form.client_height - TOOLBAR_HEIGHT, |
||
145 | gapp.panx, |
||
146 | gapp.pany, |
||
147 | gapp.image->w, |
||
148 | gapp.image->h, |
||
149 | gapp.image->w * gapp.image->n, |
||
150 | gapp.image->samples |
||
151 | ); |
||
7475 | leency | 152 | } |
4680 | right-hear | 153 | else if (gapp.image->n == 2) |
154 | { |
||
155 | int i = gapp.image->w*gapp.image->h; |
||
156 | unsigned char *color = malloc(i*4); |
||
157 | if (color != NULL) |
||
158 | { |
||
159 | unsigned char *s = gapp.image->samples; |
||
160 | unsigned char *d = color; |
||
161 | for (; i > 0 ; i--) |
||
162 | { |
||
163 | d[2] = d[1] = d[0] = *s++; |
||
164 | d[3] = *s++; |
||
165 | d += 4; |
||
166 | } |
||
7624 | leency | 167 | kos_blit(window_center + Form.client_left, |
5496 | leency | 168 | Form.client_top + TOOLBAR_HEIGHT, |
169 | Form.client_width, |
||
170 | Form.client_height - TOOLBAR_HEIGHT, |
||
171 | gapp.panx, |
||
172 | gapp.pany, |
||
173 | gapp.image->w, |
||
174 | gapp.image->h, |
||
175 | gapp.image->w * 4, |
||
176 | color |
||
177 | ); |
||
4680 | right-hear | 178 | free(color); |
179 | } |
||
5508 | leency | 180 | } |
181 | } |
||
182 | |||
183 | |||
184 | void DrawPageSides(void) |
||
7624 | leency | 185 | { |
186 | if (gapp.image->h < Form.client_height - TOOLBAR_HEIGHT) { |
||
187 | draw_h = gapp.image->h - gapp.pany; |
||
188 | } else { |
||
189 | draw_h = Form.client_height - TOOLBAR_HEIGHT; |
||
190 | } |
||
191 | |||
192 | if (gapp.image->w < Form.client_width) { |
||
193 | window_center = (Form.client_width - gapp.image->w) / 2; |
||
194 | draw_w = gapp.image->w + 2; |
||
7475 | leency | 195 | kol_paint_bar(0, TOOLBAR_HEIGHT, window_center-1, Form.client_height - TOOLBAR_HEIGHT, DOCUMENT_BG); |
196 | kol_paint_bar(window_center-1, TOOLBAR_HEIGHT, 1, draw_h, DOCUMENT_BORDER); |
||
197 | kol_paint_bar(window_center + gapp.image->w, TOOLBAR_HEIGHT, 1, draw_h, DOCUMENT_BORDER); |
||
198 | kol_paint_bar(window_center + gapp.image->w+1, TOOLBAR_HEIGHT, Form.client_width - window_center - gapp.image->w - 1, Form.client_height - TOOLBAR_HEIGHT, DOCUMENT_BG); |
||
7624 | leency | 199 | } else { |
5508 | leency | 200 | window_center = 1; |
201 | draw_w = Form.client_width; |
||
202 | } |
||
7624 | leency | 203 | |
7475 | leency | 204 | kol_paint_bar(window_center - 1, gapp.image->h - gapp.pany + TOOLBAR_HEIGHT, draw_w, 1, DOCUMENT_BORDER); |
7624 | leency | 205 | kol_paint_bar(window_center - 1, gapp.image->h - gapp.pany + TOOLBAR_HEIGHT + 1, |
206 | draw_w, Form.client_height - gapp.image->h - TOOLBAR_HEIGHT + gapp.pany - 1, DOCUMENT_BG); |
||
4680 | right-hear | 207 | } |
208 | |||
209 | |||
5508 | leency | 210 | void GetNewPageNumber(void) |
211 | { |
||
212 | new_page_number = gapp.pageno; |
||
213 | key_mode_enter_page_number = 1; |
||
214 | HandleNewPageNumber(0); |
||
215 | } |
||
5496 | leency | 216 | |
5508 | leency | 217 | void HandleNewPageNumber(unsigned char key) |
5496 | leency | 218 | { |
5508 | leency | 219 | char label_new_page[8]; |
220 | |||
221 | if ((key >= '0') && (key <= '9')) |
||
5496 | leency | 222 | { |
5508 | leency | 223 | new_page_number = new_page_number * 10 + key - '0'; |
5496 | leency | 224 | } |
5508 | leency | 225 | if (key == ASCII_KEY_BS) |
226 | { |
||
227 | new_page_number /= 10; |
||
228 | } |
||
229 | if (key == ASCII_KEY_ENTER) |
||
230 | { |
||
231 | ApplyNewPageNumber(); |
||
232 | return; |
||
233 | } |
||
234 | if (key==ASCII_KEY_ESC) |
||
235 | { |
||
236 | key_mode_enter_page_number = 0; |
||
237 | DrawWindow(); |
||
238 | return; |
||
239 | } |
||
240 | |||
241 | itoa(new_page_number, label_new_page, 10); |
||
242 | strcat(label_new_page, "_"); |
||
7475 | leency | 243 | kol_paint_bar(show_area_x, 6, show_area_w, 22, 0xFDF88E); |
5508 | leency | 244 | __menuet__write_text(show_area_x + show_area_w/2 - strlen(label_new_page)*6/2, 14, 0x000000, label_new_page, strlen(label_new_page)); |
245 | |||
246 | if (new_page_number > gapp.pagecount) ApplyNewPageNumber(); |
||
5496 | leency | 247 | } |
248 | |||
5508 | leency | 249 | void ApplyNewPageNumber(void) |
250 | { |
||
251 | key_mode_enter_page_number = 0; |
||
252 | gapp.pageno = new_page_number -1; |
||
253 | pdfapp_onkey(&gapp, ']'); |
||
254 | } |
||
5496 | leency | 255 | |
256 | void DrawPagination(void) |
||
257 | { |
||
258 | char pages_display[12]; |
||
7475 | leency | 259 | kol_paint_bar(show_area_x, 6, show_area_w, 22, 0xF4F4F4); |
5496 | leency | 260 | sprintf (pages_display, "%d/%d", gapp.pageno, gapp.pagecount); |
261 | __menuet__write_text(show_area_x + show_area_w/2 - strlen(pages_display)*6/2, 14, 0x000000, pages_display, strlen(pages_display)); |
||
262 | } |
||
263 | |||
7624 | leency | 264 | void DrawToolbarButton(int x, char image_id) |
265 | { |
||
266 | __menuet__make_button(x, 5, 26-1, 24-1, 10 + image_id + BT_HIDE, 0); |
||
267 | __menuet__putimage(x, 5, 26, 24, image_id * 24 * 26 * 3 + toolbar_image); |
||
268 | } |
||
5496 | leency | 269 | |
270 | void DrawWindow(void) |
||
271 | { |
||
7475 | leency | 272 | kol_paint_bar(0, 0, Form.client_width, TOOLBAR_HEIGHT - 1, 0xe1e1e1); // bar on the top (buttons holder) |
273 | kol_paint_bar(0, TOOLBAR_HEIGHT - 1, Form.client_width, 1, 0x7F7F7F); |
||
5508 | leency | 274 | DrawToolbarButton(8,0); //open_folder |
275 | DrawToolbarButton(42,1); //magnify - |
||
276 | DrawToolbarButton(67,2); //magnify + |
||
277 | DrawToolbarButton(101,6); //rotate left |
||
278 | DrawToolbarButton(126,7); //rotate right |
||
279 | DrawToolbarButton(Form.client_width - 160,3); //show help |
||
280 | show_area_x = Form.client_width - show_area_w - 34; |
||
281 | DrawToolbarButton(show_area_x - 26,4); //prev page |
||
282 | DrawToolbarButton(show_area_x + show_area_w,5); //nex page |
||
283 | __menuet__make_button(show_area_x-1, 5, show_area_w+1, 23, 20 + BT_HIDE, 0xA4A4A4); |
||
7475 | leency | 284 | kol_paint_bar(show_area_x, 5, show_area_w, 1, 0xA4A4A4); |
285 | kol_paint_bar(show_area_x, 28, show_area_w, 1, 0xA4A4A4); |
||
5496 | leency | 286 | winblit(&gapp); |
287 | DrawPageSides(); |
||
288 | } |
||
289 | |||
290 | |||
291 | /* Actions */ |
||
292 | |||
293 | void PageScrollDown(void) |
||
294 | { |
||
5497 | leency | 295 | //pdfapp_onkey(&gapp, 'k'); //move down |
5496 | leency | 296 | if (gapp.image->h - gapp.pany - SCROLL_H < Form.client_height - TOOLBAR_HEIGHT) |
297 | { |
||
298 | pdfapp_onkey(&gapp, '.'); |
||
299 | } |
||
300 | else { |
||
301 | gapp.pany += SCROLL_H; |
||
302 | winblit(&gapp); |
||
303 | } |
||
304 | } |
||
305 | |||
306 | |||
307 | void PageScrollUp(void) |
||
308 | { |
||
5497 | leency | 309 | //pdfapp_onkey(&gapp, 'j'); //move up |
5496 | leency | 310 | if (gapp.pany >= SCROLL_H) { |
311 | gapp.pany -= SCROLL_H; |
||
312 | winblit(&gapp); |
||
313 | } |
||
314 | else { |
||
5497 | leency | 315 | //not very nice way of using do_not_blit, but it simple |
5496 | leency | 316 | if (gapp.pageno == 1) return; |
317 | do_not_blit = 1; |
||
318 | pdfapp_onkey(&gapp, ','); |
||
319 | do_not_blit = 0; |
||
320 | gapp.pany = gapp.image->h - SCROLL_H - Form.client_height + TOOLBAR_HEIGHT; |
||
321 | if (gapp.pany < 0) gapp.pany = 0; |
||
322 | //sprintf (debugstr, "gapp.pany: %d \n", gapp.pany); |
||
7475 | leency | 323 | //kol_board_puts(debugstr); |
5496 | leency | 324 | winblit(&gapp); |
325 | } |
||
326 | } |
||
327 | |||
7475 | leency | 328 | void RunApp(char app[], char param[]) |
329 | { |
||
330 | kol_struct70 r; |
||
331 | r.p00 = 7; |
||
332 | r.p04 = 0; |
||
333 | r.p08 = param; |
||
334 | r.p12 = 0; |
||
335 | r.p16 = 0; |
||
336 | r.p20 = 0; |
||
337 | r.p21 = app; |
||
338 | kol_file_70(&r); |
||
339 | } |
||
5496 | leency | 340 | |
7475 | leency | 341 | |
5496 | leency | 342 | void PageZoomIn(void) |
343 | { |
||
344 | pdfapp_onkey(&gapp, '+'); |
||
345 | DrawPageSides(); |
||
346 | } |
||
347 | |||
348 | |||
349 | void PageZoomOut(void) |
||
350 | { |
||
351 | pdfapp_onkey(&gapp, '-'); |
||
352 | DrawPageSides(); |
||
353 | } |
||
354 | |||
5497 | leency | 355 | void PageRotateLeft(void) |
356 | { |
||
357 | pdfapp_onkey(&gapp, 'L'); |
||
358 | DrawPageSides(); |
||
359 | } |
||
5496 | leency | 360 | |
5497 | leency | 361 | void PageRotateRight(void) |
362 | { |
||
363 | pdfapp_onkey(&gapp, 'R'); |
||
364 | DrawPageSides(); |
||
365 | } |
||
366 | |||
8436 | maxcodehac | 367 | int main (int argc, char* argv[]) |
7624 | leency | 368 | { |
369 | char ii, mouse_wheels_state; |
||
370 | |||
8436 | maxcodehac | 371 | if (argc == 1) { |
7624 | leency | 372 | kol_board_puts("uPDF: no param set, showing OpenDialog"); |
8436 | maxcodehac | 373 | RunOpenApp(argv[0]); |
374 | exit(0); |
||
7624 | leency | 375 | } |
376 | |||
8436 | maxcodehac | 377 | kol_board_puts(argv[1]); |
7624 | leency | 378 | kol_board_puts("\n"); |
379 | |||
380 | char buf[128]; |
||
381 | int resolution = 72; |
||
382 | int pageno = 1; |
||
383 | fz_accelerate(); |
||
384 | kol_board_puts("PDF init\n"); |
||
385 | pdfapp_init(&gapp); |
||
386 | gapp.scrw = 600; |
||
387 | gapp.scrh = 400; |
||
388 | gapp.resolution = resolution; |
||
389 | gapp.pageno = pageno; |
||
390 | kol_board_puts("PDF Open\n"); |
||
8436 | maxcodehac | 391 | pdfapp_open(&gapp, argv[1], 0, 0); |
7624 | leency | 392 | kol_board_puts("PDF Opened\n"); |
393 | wintitle(&gapp, 0); |
||
394 | |||
395 | kol_board_puts("Inital paint\n"); |
||
396 | |||
397 | int butt, key, screen_max_x, screen_max_y; |
||
398 | __menuet__get_screen_max(&screen_max_x, &screen_max_y); |
||
399 | __menuet__set_bitfield_for_wanted_events(EVENT_REDRAW+EVENT_KEY+EVENT_BUTTON+EVENT_MOUSE_CHANGE); |
||
400 | |||
8436 | maxcodehac | 401 | for(;;) |
7624 | leency | 402 | { |
8436 | maxcodehac | 403 | switch(__menuet__wait_for_event()) |
404 | { |
||
405 | case evReDraw: |
||
406 | // gapp.shrinkwrap = 2; |
||
407 | __menuet__window_redraw(1); |
||
408 | __menuet__define_window(screen_max_x / 2 - 350-50+kos_random(50), |
||
409 | screen_max_y / 2 - 300-50+kos_random(50), |
||
410 | 700, 600, 0x73000000, 0x800000FF, Title); |
||
411 | __menuet__window_redraw(2); |
||
412 | __menuet__get_process_table(&Form, PID_WHOAMI); |
||
413 | if (Form.window_state > 2) continue; //fix rolled up |
||
414 | Form.client_width++; //fix for Menuet kernel bug |
||
415 | Form.client_height++; //fix for Menuet kernel bug |
||
416 | DrawWindow(); |
||
417 | break; |
||
7624 | leency | 418 | |
8436 | maxcodehac | 419 | case evKey: |
420 | key = __menuet__getkey(); |
||
421 | if (key_mode_enter_page_number) |
||
422 | { |
||
423 | HandleNewPageNumber(key); |
||
424 | break; |
||
425 | } |
||
426 | if (key==ASCII_KEY_ESC) DrawWindow(); //close help |
||
427 | if (key==ASCII_KEY_PGDN) pdfapp_onkey(&gapp, ']'); |
||
428 | if (key==ASCII_KEY_PGUP) pdfapp_onkey(&gapp, '['); |
||
429 | if (key==ASCII_KEY_HOME) pdfapp_onkey(&gapp, 'g'); |
||
430 | if (key==ASCII_KEY_END ) pdfapp_onkey(&gapp, 'G'); |
||
431 | if (key=='g' ) pdfapp_onkey(&gapp, 'c'); |
||
432 | if ((key=='[' ) || (key=='l')) PageRotateLeft(); |
||
433 | if ((key==']' ) || (key=='r')) PageRotateRight(); |
||
434 | if (key==ASCII_KEY_DOWN ) PageScrollDown(); |
||
435 | if (key==ASCII_KEY_UP ) PageScrollUp(); |
||
436 | if (key=='-') PageZoomOut(); |
||
437 | if ((key=='=') || (key=='+')) PageZoomIn(); |
||
7624 | leency | 438 | break; |
439 | |||
8436 | maxcodehac | 440 | case evButton: |
441 | butt = __menuet__get_button_id(); |
||
442 | if(butt==1) exit(0); |
||
443 | if(butt==10) RunOpenApp(argv[0]); |
||
444 | if(butt==11) PageZoomOut(); //magnify - |
||
445 | if(butt==12) PageZoomIn(); //magnify + |
||
446 | if(butt==13) //show help |
||
447 | { |
||
448 | kol_paint_bar(0, TOOLBAR_HEIGHT, Form.client_width, Form.client_height - TOOLBAR_HEIGHT, 0xF2F2F2); |
||
449 | __menuet__write_text(20, TOOLBAR_HEIGHT + 20 , 0x90000000, "uPDF for KolibriOS v1.2", 0); |
||
450 | __menuet__write_text(21, TOOLBAR_HEIGHT + 20 , 0x90000000, "uPDF for KolibriOS v1.2", 0); |
||
451 | for (ii=0; help[ii]!=0; ii++) { |
||
452 | __menuet__write_text(20, TOOLBAR_HEIGHT + 60 + ii * 15, 0x80000000, help[ii], 0); |
||
453 | } |
||
7624 | leency | 454 | } |
8436 | maxcodehac | 455 | if(butt==14) pdfapp_onkey(&gapp, '['); //previous page |
456 | if(butt==15) pdfapp_onkey(&gapp, ']'); //next page |
||
457 | if(butt==16) PageRotateLeft(); |
||
458 | if(butt==17) PageRotateRight(); |
||
459 | if(butt==20) GetNewPageNumber(); |
||
460 | break; |
||
7624 | leency | 461 | |
8436 | maxcodehac | 462 | case evMouse: |
463 | if (mouse_wheels_state = kos_get_mouse_wheels()) |
||
464 | { |
||
465 | if (mouse_wheels_state==1) { PageScrollDown(); PageScrollDown(); } |
||
466 | if (mouse_wheels_state==-1) { PageScrollUp(); PageScrollUp(); } |
||
467 | } |
||
468 | //sprintf (debugstr, "mouse_wheels_state: %d \n", mouse_wheels_state); |
||
469 | //kol_board_puts(debugstr); |
||
470 | //pdfapp_onmouse(&gapp, int x, int y, int btn, int modifiers, int state) |
||
471 | break; |
||
472 | } |
||
7624 | leency | 473 | } |
8436 | maxcodehac | 474 | }>>=>>> |