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