Subversion Repositories Kolibri OS

Rev

Rev 9597 | Rev 9632 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #define MEMSIZE 1024*100
  2.  
  3. #include "../lib/gui.h"
  4. #include "../lib/random.h"
  5. #include "../lib/mem.h"
  6. #include "../lib/cursor.h"
  7. #include "../lib/list_box.h"
  8. #include "../lib/events.h"
  9.  
  10. #include "../lib/obj/libimg.h"
  11. #include "../lib/obj/box_lib.h"
  12. #include "../lib/obj/proc_lib.h"
  13.  
  14. #include "../lib/patterns/rgb.h"
  15. #include "../lib/patterns/toolbar_button.h"
  16. #include "../lib/patterns/simple_open_dialog.h"
  17.  
  18. //===================================================//
  19. //                                                   //
  20. //                       DATA                        //
  21. //                                                   //
  22. //===================================================//
  23.  
  24. #define PAD 13
  25. #define TOOLBAR_ITEM_H PAD+PAD
  26. #define TOOLBAR_W 132
  27. #define STATUSBAR_H 20
  28. #define ISIZE 18
  29.  
  30. block canvas = { TOOLBAR_W + PAD + PAD, 0, NULL, NULL };
  31.  
  32. EVENTS button;
  33. EVENTS key;
  34.  
  35. proc_info Form;
  36. dword semi_white;
  37.  
  38. char default_dir[4096] = "/sys";
  39. od_filter filter2 = { 69, "BMP\0GIF\0ICO\0CUR\0JPEG\0JPG\0PNG\0PNM\0TGA\0TIFF\0TIF\0WBMP\0XBM\0XCF\Z80\0\0" };
  40.  
  41. libimg_image icons18;
  42. libimg_image main_image;
  43.  
  44. char win_title[256] = "ImageEdit";
  45.  
  46. scroll_bar scroll_v = { 15,NULL,NULL,NULL,15,2,NULL,0,0,0xeeeeee,0xBBDDFF,0xeeeeee};
  47. scroll_bar scroll_h = { NULL,NULL,15,NULL,15,2,NULL,0,0,0xeeeeee,0xBBDDFF,0xeeeeee};
  48.  
  49. enum { SAVE_AS_PNG=1, SAVE_AS_BMP=2, SAVE_AS_RAW=4 };
  50. int saving_type=SAVE_AS_PNG;
  51.  
  52. char* libimg_bpp[] = { "8pal", "24", "32", "15", "16",
  53. "mono", "8gray", "2pal", "4pal", "8pal" };
  54.  
  55. enum {
  56.         TOOL_CROP=1,
  57.         TOOL_RESIZE=2,
  58.         TOOL_COLOR_DEPTH=4,
  59.         TOOL_FLIP_ROTATE=8
  60. };
  61. int active_tool = NULL;
  62.  
  63. //===================================================//
  64. //                                                   //
  65. //                       CODE                        //
  66. //                                                   //
  67. //===================================================//
  68.  
  69. void init_ui()
  70. {
  71.         sc.get();
  72.         semi_white = MixColors(sc.work, 0xFFFfff, skin_is_dark()*90 + 96);
  73.         icons18.load("/sys/icons16.png");
  74.         icons18.replace_color(0xffFFFfff, semi_white);
  75.         icons18.replace_color(0xffCACBD6, MixColors(sc.work, 0, 200));
  76. }
  77.  
  78. void main()
  79. {
  80.         int pressed_button_id;
  81.         load_dll(libimg, #libimg_init, 1);
  82.         load_dll(boxlib, #box_lib_init,0);
  83.         load_dll(Proc_lib, #OpenDialog_init,0);
  84.         OpenDialog_init stdcall (#o_dialog);
  85.  
  86.         init_ui();
  87.         open_image("/sys/home.png");
  88.  
  89.         SetEventMask(EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_MOUSE+EVM_MOUSE_FILTER);
  90.         loop() switch(WaitEvent())
  91.         {
  92.                 case evMouse:
  93.                         mouse.get();
  94.                         scrollbar_v_mouse stdcall(#scroll_v);
  95.                         scrollbar_h_mouse stdcall(#scroll_h);
  96.                         if (scroll_v.delta) || (scroll_h.delta) draw_canvas();
  97.                         if (EAX = mouse.vert) {
  98.                                 if (EAX<10) event_scroll_canvas(SCAN_CODE_DOWN);
  99.                                 else event_scroll_canvas(SCAN_CODE_UP);
  100.                         } if (EAX = mouse.hor) {
  101.                                 debugval("mouse.hor", mouse.hor);
  102.                                 if (EAX<10) event_scroll_canvas(SCAN_CODE_RIGHT);
  103.                                 else event_scroll_canvas(SCAN_CODE_LEFT);
  104.                         }
  105.                         break;
  106.  
  107.                 case evButton:
  108.                         pressed_button_id = GetButtonID();
  109.                         if (pressed_button_id==1) ExitProcess();
  110.                         button.press(pressed_button_id);
  111.                         break;
  112.  
  113.                 case evKey:
  114.                         GetKeys();
  115.                         if (key_scancode == SCAN_CODE_DOWN) event_scroll_canvas(SCAN_CODE_DOWN);
  116.                         if (key_scancode == SCAN_CODE_UP) event_scroll_canvas(SCAN_CODE_UP);
  117.                         if (key_scancode == SCAN_CODE_LEFT) event_scroll_canvas(SCAN_CODE_LEFT);
  118.                         if (key_scancode == SCAN_CODE_RIGHT) event_scroll_canvas(SCAN_CODE_RIGHT);
  119.                         key.press(key_scancode);
  120.                         break;
  121.  
  122.                 case evReDraw:
  123.                         draw_window();
  124.                         break;
  125.         }
  126. }
  127.  
  128. void draw_window()
  129. {
  130.         incn tx;
  131.         char save_as_type[32];
  132.         sc.get();
  133.         Form.width = screen.w/6*5;
  134.         Form.height = screen.h/6*5;
  135.         DefineAndDrawWindow(screen.w-Form.width/2, screen.h-Form.height/2, Form.width, Form.height, 0x73, NULL, #win_title, 0);
  136.         GetProcessInfo(#Form, SelfInfo);
  137.         if (Form.status_window&ROLLED_UP) return;
  138.         if (Form.width  < 560) { MoveSize(OLD,OLD,560,OLD); return; }
  139.         if (Form.height < 430) { MoveSize(OLD,OLD,OLD,430); return; }
  140.         button.init(40);
  141.         key.init(40);
  142.  
  143.         DrawBar(0, 0, canvas.x, Form.cheight, sc.work);
  144.  
  145.         canvas.w = Form.cwidth - canvas.x;
  146.         canvas.h = Form.cheight - STATUSBAR_H;
  147.         if (main_image.h > canvas.h) canvas.w -= scroll_v.size_x + 1;
  148.         if (main_image.w > canvas.w) canvas.h -= scroll_h.size_y + 1;
  149.  
  150.         DrawBar(canvas.x, 0, 1, canvas.h, sc.work_text);
  151.         if (main_image.h > canvas.h) && (main_image.w > canvas.w) {
  152.                 DrawBar(canvas.x+canvas.w, canvas.y+canvas.h, scroll_v.size_x+1, scroll_h.size_y+1, sc.work);
  153.         }
  154.  
  155.         scroll_v.all_redraw = scroll_h.all_redraw = 1;
  156.         if (skin_is_dark())
  157.         {
  158.                 scroll_v.bckg_col = scroll_h.bckg_col = sc.light;
  159.                 scroll_v.frnt_col = scroll_h.frnt_col = sc.button;
  160.                 scroll_v.line_col = scroll_h.line_col = sc.button_text;
  161.         }
  162.         scroll_v.line_col = scroll_h.line_col = sc.work_text;          
  163.  
  164.         #define GAP_S TOOLBAR_ITEM_H+8
  165.         #define GAP_B TOOLBAR_ITEM_H+23
  166.         tx.set(PAD-GAP_S);
  167.         //draw_tool_btn(10, ECTRL + SCAN_CODE_KEY_N, PAD, tx.inc(GAP_S), 02, "Create image", false);
  168.         draw_tool_btn(#event_open, ECTRL + SCAN_CODE_KEY_O, PAD, tx.inc(GAP_S), 00, "Open image", false);
  169.         //draw_tool_btn(13, ECTRL + SCAN_CODE_LEFT,  PAD, tx.inc(GAP_B), 30, false);
  170.         //draw_tool_btn(14, ECTRL + SCAN_CODE_RIGHT, PAD, tx.inc(GAP_S), 31, false);
  171.         //draw_tool_btn(15, ECTRL + SCAN_CODE_UP,    PAD, tx.inc(GAP_S), 32, false);
  172.         //draw_tool_btn(16, ECTRL + SCAN_CODE_DOWN,  PAD, tx.inc(GAP_S), 33, false);
  173.  
  174.         draw_tool_btn(#event_save, ECTRL + SCAN_CODE_KEY_S, PAD, tx.inc(GAP_B), 05, "Save file", false);
  175.         draw_tool_btn(#event_save_png, 0, PAD, tx.inc(GAP_S), -1, "PNG", saving_type & SAVE_AS_PNG);
  176.         draw_tool_btn(#event_save_bmp, 0, PAD*2+34, tx.n,     -1, "BMP", saving_type & SAVE_AS_BMP);
  177.         draw_tool_btn(#event_save_raw, 0, PAD*3+68, tx.n,     -1, "RAW", saving_type & SAVE_AS_RAW);
  178.  
  179.         draw_tool_btn(#event_activate_crop,  0, PAD, tx.inc(GAP_B), 46, "Crop", active_tool & TOOL_CROP);
  180.         draw_tool_btn(#event_activate_resize,0, PAD, tx.inc(GAP_S), 06, "Resize", active_tool & TOOL_RESIZE);
  181.         draw_tool_btn(#event_activate_depth, 0, PAD, tx.inc(GAP_S), 52, "Color depth", active_tool & TOOL_COLOR_DEPTH);
  182.         draw_tool_btn(#event_activate_flprot,0, PAD, tx.inc(GAP_S), 36, "Flip/Rotate", active_tool & TOOL_FLIP_ROTATE);
  183.         //draw_tool_btn(#event_flip_hor, ECTRL + SCAN_CODE_KEY_H, PAD,      tx.inc(GAP_S), 34, NULL, false);
  184.         //draw_tool_btn(#event_flip_ver, ECTRL + SCAN_CODE_KEY_V, PAD*2+34, tx.n,          35, NULL, false);
  185.         //draw_tool_btn(#event_rotate,   ECTRL + SCAN_CODE_KEY_R, PAD*3+68, tx.n,          36, NULL, false);
  186.  
  187.         draw_status_bar();
  188.         draw_canvas();
  189. }
  190.  
  191. void draw_status_bar()
  192. {
  193.         char img_info[24];
  194.         //draw_image_info
  195.         sprintf(#img_info, "%ix%i@%s", main_image.w, main_image.h, libimg_bpp[main_image.type-1]);
  196.         DrawBar(canvas.x, Form.cheight - STATUSBAR_H, Form.cwidth - canvas.x, STATUSBAR_H, sc.work);
  197.         WriteText(canvas.x, Form.cheight - STATUSBAR_H + 2, 0x90, sc.work_text, #img_info);
  198. }
  199.  
  200. void draw_tool_btn(dword _event, _hotkey, _x, _y, _icon_n, _text, _active)
  201. {
  202.         int w = TOOLBAR_W;
  203.         if (!_text) w = PAD + PAD + 12;
  204.         if (_icon_n==-1) w = strlen(_text) * 8 + 14;
  205.         if (_active) EDX = sc.button; else EDX = semi_white;
  206.         DrawBar(_x, _y, w, TOOLBAR_ITEM_H+1, EDX);
  207.         PutPixel(_x,_y,sc.work);
  208.         PutPixel(_x,_y+TOOLBAR_ITEM_H,sc.work);
  209.         PutPixel(_x+w-1,_y,sc.work);
  210.         PutPixel(_x+w-1,_y+TOOLBAR_ITEM_H,sc.work);
  211.         if (_event) DefineHiddenButton(_x, _y, w, TOOLBAR_ITEM_H, button.add(_event));
  212.         if (_hotkey) key.add_n(_hotkey, _event);
  213.         if (_icon_n!=-1) {
  214.                 img_draw stdcall(icons18.image, _x+7, _y+4, ISIZE, ISIZE, 0, _icon_n*ISIZE);
  215.                 _x += PAD+ISIZE+2;
  216.         } else {
  217.                 _x += 7;
  218.         }
  219.         if (_text) {
  220.                 if (_active) EDX = sc.button_text; else EDX = sc.work_text;
  221.                 WriteText(_x, _y+6, 0x90, EDX, _text);
  222.         }
  223. }
  224.  
  225. void draw_scroll_v()
  226. {
  227.         scroll_v.max_area = main_image.h;
  228.         scroll_v.cur_area = scroll_v.size_y = canvas.h;
  229.         scroll_v.start_x = canvas.x + canvas.w;
  230.         scroll_v.start_y = 0;
  231.         if (main_image.h > canvas.h) scrollbar_v_draw stdcall (#scroll_v);
  232. }
  233.  
  234. void draw_scroll_h()
  235. {
  236.         scroll_h.max_area = main_image.w;
  237.         scroll_h.cur_area = scroll_h.size_x = canvas.w;
  238.         scroll_h.start_x = canvas.x;
  239.         scroll_h.start_y = canvas.y + canvas.h;
  240.         if (main_image.w > canvas.w) scrollbar_h_draw stdcall (#scroll_h);
  241. }
  242.  
  243. void draw_canvas()
  244. {
  245.         int content_w, content_h;
  246.         content_w = math.min(main_image.w, canvas.w-1);
  247.         content_h = math.min(main_image.h, canvas.h);
  248.  
  249.         if (main_image.image) {
  250.                 img_draw stdcall(main_image.image, canvas.x+1, canvas.y,
  251.                 content_w, content_h, scroll_h.position, scroll_v.position);
  252.         }
  253.  
  254.         DrawBar(canvas.x+1+content_w, canvas.y, canvas.w - content_w - 1, content_h, 0xBFCAD2);
  255.         DrawBar(canvas.x+1, canvas.y+content_h, canvas.w - 1, canvas.h - content_h, 0xBFCAD2);
  256.  
  257.         draw_scroll_v();
  258.         draw_scroll_h();
  259. }
  260.  
  261. void update_title(char* _new_title)
  262. {
  263.         strcpy(#win_title+9, " - ");
  264.         strlcpy(#win_title+12, _new_title, sizeof(win_title));
  265.         DrawTitle(#win_title);
  266. }
  267.  
  268. void open_image(char* _path)
  269. {
  270.         main_image.load(_path);
  271.         update_title(_path);
  272.         scroll_v.position = 0;
  273.         scroll_h.position = 0;
  274. }
  275.  
  276. //===================================================//
  277. //                                                   //
  278. //                      EVENTS                       //
  279. //                                                   //
  280. //===================================================//
  281.  
  282. void event_open()
  283. {
  284.         o_dialog.type = 0; //open file
  285.         OpenDialog_start stdcall (#o_dialog);
  286.         if (o_dialog.status) {
  287.                 open_image(#openfile_path);
  288.                 draw_canvas();
  289.         }
  290. }
  291.  
  292. void event_flip_hor()
  293. {
  294.         img_flip stdcall (main_image.image, FLIP_HORIZONTAL);
  295.         draw_canvas();
  296. }
  297.  
  298. void event_flip_ver()
  299. {
  300.         img_flip stdcall (main_image.image, FLIP_VERTICAL);
  301.         draw_canvas();
  302. }
  303.  
  304. void event_rotate()
  305. {
  306.         img_rotate stdcall (main_image.image, ROTATE_90_CW);
  307.         main_image.w >< main_image.h;
  308.         draw_window();
  309. }
  310.  
  311. void event_save_png() { saving_type = SAVE_AS_PNG; draw_window(); }
  312. void event_save_bmp() { saving_type = SAVE_AS_BMP; draw_window(); }
  313. void event_save_raw() { saving_type = SAVE_AS_RAW; draw_window(); }
  314.  
  315. void event_activate_crop() { active_tool = TOOL_CROP; draw_window(); }
  316. void event_activate_resize() { active_tool = TOOL_RESIZE; draw_window(); }
  317. void event_activate_depth() { active_tool = TOOL_COLOR_DEPTH; draw_window(); }
  318. void event_activate_flprot() { active_tool = TOOL_FLIP_ROTATE; draw_window(); }
  319.  
  320. void event_save()
  321. {
  322.         o_dialog.type = 1; //save file
  323.         switch (saving_type) {
  324.                 case SAVE_AS_PNG:
  325.                                 strcpy(#filename_area, "image.png");
  326.                                 OpenDialog_start stdcall (#o_dialog);
  327.                                 if (o_dialog.status) {
  328.                                         update_title(#openfile_path);
  329.                                         img_to_rgb stdcall (main_image.image);
  330.                                         save_image(main_image.imgsrc, main_image.w, main_image.h, #openfile_path);
  331.                                 }
  332.                                 break;
  333.                 case SAVE_AS_BMP:
  334.                                 notify("Not implemented yet.");
  335.                                 break;
  336.                 case SAVE_AS_RAW:
  337.                                 notify("Not implemented yet.");
  338.                                 break;
  339.         }
  340. }
  341.  
  342. void event_scroll_canvas(int _direction)
  343. {
  344.         switch(_direction) {
  345.                 case SCAN_CODE_DOWN:
  346.                         scroll_v.position = math.min(scroll_v.position+25,
  347.                                 scroll_v.max_area - scroll_v.cur_area);
  348.                         break;
  349.                 case SCAN_CODE_UP:
  350.                         scroll_v.position = math.max(scroll_v.position-25, 0);
  351.                         break;
  352.                 case SCAN_CODE_RIGHT:
  353.                         scroll_h.position = math.min(scroll_h.position+25,
  354.                                 scroll_h.max_area - scroll_h.cur_area);
  355.                         break;
  356.                 case SCAN_CODE_LEFT:
  357.                         scroll_h.position = math.max(scroll_h.position-25, 0);
  358.         }
  359.         draw_canvas();
  360. }
  361.  
  362. stop:
  363.  
  364.