Subversion Repositories Kolibri OS

Rev

Rev 7533 | Rev 7624 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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