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