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