Subversion Repositories Kolibri OS

Rev

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