Subversion Repositories Kolibri OS

Rev

Rev 9633 | Rev 9635 | 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. //Sizes
  25. #define PAD 13
  26. #define TOOLBAR_ITEM_H PAD + PAD
  27. #define TOOLBAR_W 132
  28. #define STATUSBAR_H 20
  29. #define HEADERH TOOLBAR_ITEM_H + 14
  30. #define HEADER_TEXTY HEADERH - 14 / 2
  31. #define CANVASX TOOLBAR_W + PAD + PAD
  32. #define CANVASY HEADERH + 2
  33.  
  34. // Colors
  35. #define COL_WORK        0x242424
  36. #define COL_WORK_TEXT   0xBEBEBE
  37. #define COL_LIGHT       0x424242
  38. #define COL_DARK        0x1D1D1D
  39. #define COL_LINE        0x010101
  40. #define COL_BUTTON      0x181818
  41. #define COL_BUTTON_TEXT 0x18A2CC
  42.  
  43. block canvas = { CANVASX, CANVASY, NULL, NULL };
  44.  
  45. EVENTS button;
  46. EVENTS key;
  47.  
  48. proc_info Form;
  49. int pressed_button_id;
  50.  
  51. char default_dir[4096] = "/sys";
  52. od_filter filter2 = { 69, "BMP\0GIF\0ICO\0CUR\0JPEG\0JPG\0PNG\0PNM\0TGA\0TIFF\0TIF\0WBMP\0XBM\0XCF\Z80\0\0" };
  53.  
  54. libimg_image icons18;
  55. libimg_image icons18a;
  56. libimg_image pixie_skin;
  57. libimg_image main_image;
  58.  
  59. scroll_bar scroll_v = { 15,NULL,NULL,HEADERH+1,15,2,NULL,0,0,COL_DARK,COL_LIGHT,COL_LINE};
  60. scroll_bar scroll_h = { NULL,TOOLBAR_W+PAD+PAD,15,NULL,15,2,NULL,0,0,COL_DARK,COL_LIGHT,COL_LINE};
  61.  
  62. enum { SAVE_AS_PNG=1, SAVE_AS_BMP=2, SAVE_AS_RAW=4 };
  63. int saving_type=SAVE_AS_PNG;
  64.  
  65. char* libimg_bpp[] = { "???", "8pal", "24", "32", "15", "16",
  66. "mono", "8gray", "2pal", "4pal", "8gr/a" };
  67.  
  68. enum {
  69.         TOOL_CROP=1,
  70.         TOOL_RESIZE=2,
  71.         TOOL_COLOR_DEPTH=4,
  72.         TOOL_FLIP_ROTATE=8
  73. };
  74. int active_tool = NULL;
  75.  
  76. //===================================================//
  77. //                                                   //
  78. //                       CODE                        //
  79. //                                                   //
  80. //===================================================//
  81.  
  82. void init_ui()
  83. {
  84.         icons18.load("/sys/icons16.png");
  85.         icons18.replace_2colors(0xffFFFfff, COL_LIGHT, 0xffCACBD6, COL_LIGHT-0x080808);
  86.         icons18a.load("/sys/icons16.png");
  87.         icons18a.replace_2colors(0xffFFFfff, COL_BUTTON, 0xffCACBD6, 0);
  88.  
  89.         pixie_skin.load("/sys/media/pixieskn.png");
  90. }
  91.  
  92. void main()
  93. {
  94.         load_dll(libimg, #libimg_init, 1);
  95.         load_dll(boxlib, #box_lib_init,0);
  96.         load_dll(Proc_lib, #OpenDialog_init,0);
  97.         OpenDialog_init stdcall (#o_dialog);
  98.  
  99.         open_image("/sys/home.png");
  100.  
  101.         SetEventMask(EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_MOUSE+EVM_MOUSE_FILTER);
  102.         loop() switch(WaitEvent())
  103.         {
  104.                 case evMouse:
  105.                         mouse.get();
  106.                         scrollbar_v_mouse stdcall(#scroll_v);
  107.                         scrollbar_h_mouse stdcall(#scroll_h);
  108.                         if (scroll_v.delta) || (scroll_h.delta) draw_canvas();
  109.                         if (EAX = mouse.vert) {
  110.                                 if (EAX<10) event_scroll_canvas(SCAN_CODE_DOWN);
  111.                                 else event_scroll_canvas(SCAN_CODE_UP);
  112.                         } if (EAX = mouse.hor) {
  113.                                 if (EAX<10) event_scroll_canvas(SCAN_CODE_RIGHT);
  114.                                 else event_scroll_canvas(SCAN_CODE_LEFT);
  115.                         }
  116.                         break;
  117.  
  118.                 case evButton:
  119.                         pressed_button_id = GetButtonID();
  120.                         if (pressed_button_id==1) ExitProcess();
  121.                         button.press(pressed_button_id);
  122.                         break;
  123.  
  124.                 case evKey:
  125.                         GetKeys();
  126.                         if (key_scancode == SCAN_CODE_DOWN) event_scroll_canvas(SCAN_CODE_DOWN);
  127.                         if (key_scancode == SCAN_CODE_UP) event_scroll_canvas(SCAN_CODE_UP);
  128.                         if (key_scancode == SCAN_CODE_LEFT) event_scroll_canvas(SCAN_CODE_LEFT);
  129.                         if (key_scancode == SCAN_CODE_RIGHT) event_scroll_canvas(SCAN_CODE_RIGHT);
  130.                         key.press(key_scancode);
  131.                         break;
  132.  
  133.                 case evReDraw:
  134.                         draw_window();
  135.                         break;
  136.         }
  137. }
  138.  
  139. void draw_window()
  140. {
  141.         Form.width = screen.w/6*5;
  142.         Form.height = screen.h/6*5;
  143.         DefineAndDrawWindow(screen.w-Form.width/2, screen.h-Form.height/2, Form.width, Form.height, 0x42, NULL, NULL, 0);
  144.         GetProcessInfo(#Form, SelfInfo);
  145.         if (Form.status_window&ROLLED_UP) return;
  146.         if (Form.width  < 560) { MoveSize(OLD,OLD,560,OLD); return; }
  147.         if (Form.height < 430) { MoveSize(OLD,OLD,OLD,430); return; }
  148.         scroll_v.all_redraw = scroll_h.all_redraw = 1;
  149.         draw_content();
  150. }
  151.  
  152. void draw_content()
  153. {
  154.         incn tx;
  155.         button.init(40);
  156.         key.init(40);
  157.  
  158.         init_ui();
  159.         canvas.w = Form.cwidth - CANVASX - 1;
  160.         canvas.h = Form.cheight - STATUSBAR_H - CANVASY - 1;
  161.         if (main_image.h > canvas.h) canvas.w -= scroll_v.size_x + 1;
  162.         if (main_image.w > canvas.w) canvas.h -= scroll_h.size_y + 1;
  163.  
  164.         //window border and panel border
  165.         DrawRectangle(0,0,Form.width,Form.height,COL_LINE);
  166.         DrawRectangle(1,1,Form.width-2,Form.height-2,COL_LIGHT);
  167.         DrawBar(CANVASX, 0, 1, Form.cheight, COL_LINE);
  168.  
  169.         //draw title
  170.         DrawBar(2, 2, CANVASX-2, HEADERH-1, COL_DARK);
  171.         WriteText(PAD+5, HEADER_TEXTY, 0x90, COL_WORK_TEXT, "ImageEditor Pro");
  172.  
  173.         //draw header
  174.         DrawBar(CANVASX+1, 2, Form.width-CANVASX-2, HEADERH-1, COL_WORK);
  175.         DrawBar(CANVASX+1, CANVASY-1, Form.width-CANVASX-2, 1, COL_LINE);
  176.         img_draw stdcall(pixie_skin.image, Form.width-63, 7, 57, 18, 265, 0);
  177.         DefineHiddenButton(Form.width-63, 7, 28, 17, button.add(#MinimizeWindow));
  178.         DefineHiddenButton(Form.width-35, 7, 28, 17, 1);
  179.         draw_acive_panel();
  180.  
  181.         //left panel bg
  182.         DrawBar(2, 1+HEADERH, CANVASX-2, Form.cheight-2-HEADERH, COL_WORK);
  183.  
  184.         if (main_image.h > canvas.h) && (main_image.w > canvas.w) {
  185.                 DrawBar(CANVASX+canvas.w, CANVASY+canvas.h, scroll_v.size_x+1, scroll_h.size_y+1, COL_WORK);
  186.         }
  187.  
  188.         #define GAP_S TOOLBAR_ITEM_H+8
  189.         #define GAP_B TOOLBAR_ITEM_H+23
  190.         tx.set(PAD-GAP_S+HEADERH);
  191.         //draw_tool_btn(10, ECTRL + SCAN_CODE_KEY_N, PAD, tx.inc(GAP_S), 02, "Create image", false);
  192.         draw_tool_btn(#event_open, ECTRL + SCAN_CODE_KEY_O, PAD, tx.inc(GAP_S), 00, "Open image", false);
  193.         //draw_tool_btn(13, ECTRL + SCAN_CODE_LEFT,  PAD, tx.inc(GAP_B), 30, false);
  194.         //draw_tool_btn(14, ECTRL + SCAN_CODE_RIGHT, PAD, tx.inc(GAP_S), 31, false);
  195.         //draw_tool_btn(15, ECTRL + SCAN_CODE_UP,    PAD, tx.inc(GAP_S), 32, false);
  196.         //draw_tool_btn(16, ECTRL + SCAN_CODE_DOWN,  PAD, tx.inc(GAP_S), 33, false);
  197.  
  198.         draw_tool_btn(#event_save, ECTRL + SCAN_CODE_KEY_S, PAD, tx.inc(GAP_B), 05, "Save as", false);
  199.         draw_tool_btn(#event_save_png, 0, PAD, tx.inc(GAP_S), -1, "PNG", saving_type & SAVE_AS_PNG);
  200.         draw_tool_btn(#event_save_bmp, 0, PAD*2+34, tx.n,     -1, "BMP", saving_type & SAVE_AS_BMP);
  201.         draw_tool_btn(#event_save_raw, 0, PAD*3+68, tx.n,     -1, "RAW", saving_type & SAVE_AS_RAW);
  202.  
  203.         draw_tool_btn(#event_activate_crop,  0, PAD, tx.inc(GAP_B), 46, "Crop", active_tool & TOOL_CROP);
  204.         draw_tool_btn(#event_activate_resize,0, PAD, tx.inc(GAP_S), 06, "Resize", active_tool & TOOL_RESIZE);
  205.         draw_tool_btn(#event_activate_depth, 0, PAD, tx.inc(GAP_S), 52, "Color depth", active_tool & TOOL_COLOR_DEPTH);
  206.         draw_tool_btn(#event_activate_flprot,0, PAD, tx.inc(GAP_S), 36, "Flip/Rotate", active_tool & TOOL_FLIP_ROTATE);
  207.  
  208.         draw_status_bar();
  209.         draw_canvas();
  210. }
  211.  
  212. void draw_status_bar()
  213. {
  214.         char img_info[24];
  215.         sprintf(#img_info, "%i\01%i\02%s", main_image.w, main_image.h, libimg_bpp[main_image.type]);
  216.         DrawBar(CANVASX+1, Form.cheight - STATUSBAR_H - 1, Form.cwidth - CANVASX -2, STATUSBAR_H, COL_WORK);
  217.         WriteText(CANVASX+4, Form.cheight - STATUSBAR_H + 2, 0x90, COL_WORK_TEXT, #img_info);
  218.         for (ESI=0; img_info[ESI]!=0; ESI++) {
  219.                 if (img_info[ESI] == '\01') img_info[ESI]='x';
  220.                 else if (img_info[ESI] == '\02') img_info[ESI]='@';
  221.                 else img_info[ESI]=' ';
  222.         }
  223.         ECX = 0x90 << 24 + COL_BUTTON_TEXT;
  224.         $int 64
  225. }
  226.  
  227. int draw_tool_btn(dword _event, _hotkey, _x, _y, _icon_n, _text, _active)
  228. {
  229.         int w = TOOLBAR_W;
  230.         dword img_ptr = icons18.image;
  231.         if (!_text) w = PAD + PAD + 6;
  232.         if (_icon_n==-1) w = strlen(_text) * 8 + 14;
  233.         if (_active==-1) {
  234.                 $push COL_LIGHT
  235.                 EDX = COL_LINE;
  236.         } else if (_active) {
  237.                 img_ptr = icons18a.image;
  238.                 $push COL_BUTTON_TEXT
  239.                 EDX = COL_BUTTON;
  240.  
  241.         } else {
  242.                 $push COL_WORK_TEXT
  243.                 EDX = COL_LIGHT;
  244.         }
  245.         DrawBar(_x, _y, w, TOOLBAR_ITEM_H+1, EDX);
  246.         PutPixel(_x,_y,COL_WORK);
  247.         PutPixel(_x,_y+TOOLBAR_ITEM_H,COL_WORK);
  248.         PutPixel(_x+w-1,_y,COL_WORK);
  249.         PutPixel(_x+w-1,_y+TOOLBAR_ITEM_H,COL_WORK);
  250.         if (_event) DefineHiddenButton(_x, _y, w, TOOLBAR_ITEM_H, button.add(_event));
  251.         if (_hotkey) key.add_n(_hotkey, _event);
  252.         if (_icon_n!=-1) {
  253.                 #define ISIZE 18
  254.                 img_draw stdcall(img_ptr, _x+7, _y+4, ISIZE, ISIZE, 0, _icon_n*ISIZE);
  255.                 _x += PAD+ISIZE+2;
  256.         } else {
  257.                 _x += 7;
  258.         }
  259.         $pop EDX
  260.         if (_text) {
  261.                 WriteText(_x, _y+6, 0x90, EDX, _text);
  262.         }
  263.         return w;
  264. }
  265.  
  266. void draw_canvas()
  267. {
  268.         int content_w = math.min(main_image.w, canvas.w-1);
  269.         int content_h = math.min(main_image.h, canvas.h);
  270.         if (main_image.image) {
  271.                 img_draw stdcall(main_image.image, CANVASX+1, CANVASY,
  272.                 content_w, content_h, scroll_h.position, scroll_v.position);
  273.         }
  274.         DrawBar(CANVASX+1+content_w, CANVASY, canvas.w - content_w - 1, content_h, COL_DARK);
  275.         DrawBar(CANVASX+1, CANVASY+content_h, canvas.w - 1, canvas.h - content_h, COL_DARK);
  276.         //Draw scroll V
  277.         scroll_v.max_area = main_image.h;
  278.         scroll_v.cur_area = scroll_v.size_y = canvas.h + 1;
  279.         scroll_v.start_x = CANVASX + canvas.w;
  280.         if (main_image.h > canvas.h) scrollbar_v_draw stdcall (#scroll_v);
  281.         //Draw scroll H
  282.         scroll_h.max_area = main_image.w;
  283.         scroll_h.cur_area = scroll_h.size_x = canvas.w;
  284.         scroll_h.start_y = CANVASY + canvas.h;
  285.         if (main_image.w > canvas.w) scrollbar_h_draw stdcall (#scroll_h);
  286.  
  287. }
  288.  
  289. void set_file_path(char* _new_title)
  290. {
  291.         strcpy(#param, _new_title);
  292.         draw_status_bar();
  293. }
  294.  
  295. void open_image(char* _path)
  296. {
  297.         main_image.load(_path);
  298.         set_file_path(_path);
  299.         scroll_v.position = 0;
  300.         scroll_h.position = 0;
  301. }
  302.  
  303. int color_depth_id;
  304. void event_set_color_depth() {
  305.         img_convert stdcall(main_image.image, 0, pressed_button_id-color_depth_id, 0, 0);
  306.         if (!EAX) {
  307.                 notify("'ImageEdit Pro\nConvertation error' -Et");
  308.         } else {
  309.                 $push eax
  310.                 img_destroy stdcall(main_image.image);
  311.                 $pop eax
  312.                 main_image.image = EAX;
  313.                 main_image.set_vars();
  314.                 draw_acive_panel();
  315.                 draw_status_bar();
  316.                 draw_canvas();         
  317.         }
  318. }
  319.  
  320. void draw_acive_panel()
  321. {
  322.         int i, x = CANVASX + PAD;
  323.         bool a;
  324.         switch(active_tool) {
  325.                 case TOOL_CROP:
  326.                         WriteText(CANVASX+PAD, HEADER_TEXTY, 0x90, COL_WORK_TEXT, "Crop tool");
  327.                         break;
  328.                 case TOOL_RESIZE:
  329.                         WriteText(CANVASX+PAD, HEADER_TEXTY, 0x90, COL_WORK_TEXT, "New width");
  330.                         WriteText(CANVASX+PAD+150, HEADER_TEXTY, 0x90, COL_WORK_TEXT, "New height");
  331.                         draw_tool_btn(#event_rotate_left, SCAN_CODE_ENTER, CANVASX + PAD + 300, 7, -1, "Apply", false);
  332.                         break;
  333.                 case TOOL_COLOR_DEPTH:
  334.                         WriteText(CANVASX+PAD, HEADER_TEXTY, 0x90, COL_WORK_TEXT, "Color depth");
  335.                         x += 11*8 + PAD;
  336.                         color_depth_id = button.new_id;
  337.                         for (i=1; i<11; i++) {
  338.                                 if (main_image.type == i) {
  339.                                         //this is current image depth
  340.                                         a = true;
  341.                                 } else {
  342.                                         //this is image ve san set
  343.                                         a = false;
  344.                                         //probe does libimg support converting current image gepth to i-one
  345.                                         img_create stdcall(1, 1, main_image.type);
  346.                                         img_convert stdcall(EAX, 0, i, 0, 0);
  347.                                         if (EAX) {
  348.                                                 img_destroy stdcall(EAX);
  349.                                         } else {
  350.                                                 //continue;
  351.                                                 a = -1;
  352.                                         }
  353.                                 }
  354.                                 x += draw_tool_btn(#event_set_color_depth, SCAN_CODE_ENTER, x, 7, -1, libimg_bpp[i], a) + PAD;                 
  355.                         }
  356.                         break;
  357.                 case TOOL_FLIP_ROTATE:
  358.                         WriteText(CANVASX+PAD, HEADER_TEXTY, 0x90, COL_WORK_TEXT, "Flip");
  359.                         draw_tool_btn(#event_flip_hor, ECTRL + SCAN_CODE_KEY_H, CANVASX + PAD + 040, 7, 34, NULL, false);
  360.                         draw_tool_btn(#event_flip_ver, ECTRL + SCAN_CODE_KEY_V, CANVASX + PAD + 080, 7, 35, NULL, false);
  361.                         WriteText(CANVASX+PAD + 142, HEADER_TEXTY, 0x90, COL_WORK_TEXT, "Rotate");
  362.                         draw_tool_btn(#event_rotate_left,   ECTRL + SCAN_CODE_KEY_L, CANVASX + PAD + 200, 7, 37, NULL, false);
  363.                         draw_tool_btn(#event_rotate_right,   ECTRL + SCAN_CODE_KEY_R, CANVASX + PAD + 240, 7, 36, NULL, false);
  364.                         // WriteText(CANVASX+PAD + 142, HEADER_TEXTY, 0x90, COL_WORK_TEXT, "Move");
  365.                         // DrawTopPanelButton1(#EventMoveLeft,       ECTRL + SCAN_CODE_LEFT,  tx.inc(GAP_B), 30);
  366.                         // DrawTopPanelButton1(#EventMoveRight,      ECTRL + SCAN_CODE_RIGHT, tx.inc(GAP_S), 31);
  367.                         // DrawTopPanelButton1(#EventMoveUp,         ECTRL + SCAN_CODE_UP,    tx.inc(GAP_S), 32);
  368.                         // DrawTopPanelButton1(#EventMoveDown,       ECTRL + SCAN_CODE_DOWN,  tx.inc(GAP_S), 33);
  369.                         break;
  370.                 default:
  371.                         WriteText(CANVASX+PAD, HEADER_TEXTY, 0x90, COL_WORK_TEXT, "Welcome to ImageEditor Pro! Try to open some file.");
  372.         }
  373. }
  374.  
  375. //===================================================//
  376. //                                                   //
  377. //                      EVENTS                       //
  378. //                                                   //
  379. //===================================================//
  380.  
  381. void event_open()
  382. {
  383.         o_dialog.type = 0; //open file
  384.         OpenDialog_start stdcall (#o_dialog);
  385.         if (o_dialog.status) {
  386.                 open_image(#openfile_path);
  387.                 draw_canvas();
  388.         }
  389. }
  390.  
  391. void event_flip_hor()
  392. {
  393.         img_flip stdcall (main_image.image, FLIP_HORIZONTAL);
  394.         draw_canvas();
  395. }
  396.  
  397. void event_flip_ver()
  398. {
  399.         img_flip stdcall (main_image.image, FLIP_VERTICAL);
  400.         draw_canvas();
  401. }
  402.  
  403. void event_rotate_left()
  404. {
  405.         img_rotate stdcall (main_image.image, ROTATE_270_CW);
  406.         main_image.w >< main_image.h;
  407.         draw_content();
  408. }
  409.  
  410. void event_rotate_right()
  411. {
  412.         img_rotate stdcall (main_image.image, ROTATE_90_CW);
  413.         main_image.w >< main_image.h;
  414.         draw_content();
  415. }
  416.  
  417. void event_save_png() { saving_type = SAVE_AS_PNG; draw_content(); }
  418. void event_save_bmp() { saving_type = SAVE_AS_BMP; draw_content(); }
  419. void event_save_raw() { saving_type = SAVE_AS_RAW; draw_content(); }
  420.  
  421. void event_activate_crop() { active_tool = TOOL_CROP; draw_content(); }
  422. void event_activate_resize() { active_tool = TOOL_RESIZE; draw_content(); }
  423. void event_activate_depth() { active_tool = TOOL_COLOR_DEPTH; draw_content(); }
  424. void event_activate_flprot() { active_tool = TOOL_FLIP_ROTATE; draw_content(); }
  425.  
  426. void event_save()
  427. {
  428.         o_dialog.type = 1; //save file
  429.         switch (saving_type) {
  430.                 case SAVE_AS_PNG:
  431.                                 strcpy(#filename_area, "image.png");
  432.                                 OpenDialog_start stdcall (#o_dialog);
  433.                                 if (o_dialog.status) {
  434.                                         set_file_path(#openfile_path);
  435.                                         img_to_rgb stdcall (main_image.image);
  436.                                         save_image(main_image.imgsrc, main_image.w, main_image.h, #openfile_path);
  437.                                 }
  438.                                 break;
  439.                 case SAVE_AS_BMP:
  440.                                 notify("Not implemented yet.");
  441.                                 break;
  442.                 case SAVE_AS_RAW:
  443.                                 notify("Not implemented yet.");
  444.                                 break;
  445.         }
  446. }
  447.  
  448. void event_scroll_canvas(int _direction)
  449. {
  450.         switch(_direction) {
  451.                 case SCAN_CODE_DOWN:
  452.                         scroll_v.position = math.min(scroll_v.position+25,
  453.                                 scroll_v.max_area - scroll_v.cur_area);
  454.                         break;
  455.                 case SCAN_CODE_UP:
  456.                         scroll_v.position = math.max(scroll_v.position-25, 0);
  457.                         break;
  458.                 case SCAN_CODE_RIGHT:
  459.                         scroll_h.position = math.min(scroll_h.position+25,
  460.                                 scroll_h.max_area - scroll_h.cur_area);
  461.                         break;
  462.                 case SCAN_CODE_LEFT:
  463.                         scroll_h.position = math.max(scroll_h.position-25, 0);
  464.         }
  465.         draw_canvas();
  466. }
  467.  
  468. stop:
  469.  
  470.