Subversion Repositories Kolibri OS

Rev

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