Subversion Repositories Kolibri OS

Rev

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

  1. /*
  2.  * Icon Editor for KolibriOS
  3.  * Authors: Leency, Nicolas
  4.  * Licence: GPL v2
  5. */
  6.  
  7. #define MEMSIZE 1024*2000
  8.  
  9. #include "../lib/gui.h"
  10. #include "../lib/random.h"
  11. #include "../lib/mem.h"
  12. #include "../lib/cursor.h"
  13. #include "../lib/list_box.h"
  14. #include "../lib/events.h"
  15.  
  16. #include "../lib/obj/libimg.h"
  17. #include "../lib/obj/box_lib.h"
  18.  
  19. #include "../lib/patterns/rgb.h"
  20. #include "../lib/patterns/libimg_load_skin.h"
  21.  
  22. #include "colors_mas.h"
  23.  
  24. #ifndef AUTOBUILD
  25. #include "lang.h--"
  26. #endif
  27.  
  28. //===================================================//
  29. //                                                   //
  30. //                       DATA                        //
  31. //                                                   //
  32. //===================================================//
  33.  
  34. #ifdef LANG_RUS
  35. char image_menu_items[] =
  36. "Š®«¨ç¥á⢮ ¨á¯®«ì§®¢ ­­ëå 梥⮢
  37. ‡ ¬¥­¨âì ¢á¥ æ¢¥â  1 ­  2";
  38. ?define T_MENU_IMAGE "ˆª®­ª "
  39. ?define T_TEST_ICON "à®¢¥à¨âì ¨ª®­ªã"
  40. ?define T_TITLE "¥¤ ªâ®à ¨ª®­®ª 0.60 Alpha"
  41. ?define T_UNIC_COLORS_COUNT "'“­¨ª «ì­ëå 梥⮢: %i.' -I"
  42. ?define T_TOO_BIG_IMAGE_FOR_PREVIEW "'IconEdit
  43. ˆ§®¡à ¦¥­¨¥ ᫨誮¬ ¡®«ì讥 ¤«ï ¯à¥¤¯à®á¬®âà !' -tE"
  44. ?define T_ERROR_CROP_TOOL "'„«ï ®¡à¥§ª¨ ¨§®¡à ¦¥­¨ï ¢­ ç «¥ ­ã¦­® ¢ë¤¥«¨âì ®¡« áâì.' -W"
  45. ?define T_ERROR_IMA_ICONEDIT "'â® ¯à®á⮠। ªâ®à ¨ª®­®ª, ¢ë¡à ­®¥
  46. ¨§®¡à ¦¥­¨¥ ᫨誮¬ ¢¥«¨ª® ¤«ï ­¥£®!' -E"
  47. #else
  48. char image_menu_items[] =
  49. "Count colors used
  50. Replace all colors equal to 1 by 2";
  51. ?define T_MENU_IMAGE "Icon"
  52. ?define T_TEST_ICON "Test Icon"
  53. ?define T_TITLE "Icon Editor 0.60 Alpha"
  54. ?define T_UNIC_COLORS_COUNT "'Image has %i unique colors.' -I"
  55. ?define T_TOO_BIG_IMAGE_FOR_PREVIEW "'IconEdit
  56. Image is too big for preview!' -tE"
  57. ?define T_ERROR_CROP_TOOL "'You need to select something before using crop tool.' -W"
  58. ?define T_ERROR_IMA_ICONEDIT "'Hey, this is just an icon editor,
  59. selected image is too big to open!' -E"
  60. #endif
  61.  
  62.  
  63.  
  64. #define TOPBAR_H    24+8
  65. #define LEFTBAR_W 16+5+5+3+3
  66. #define PALLETE_SIZE 116
  67.  
  68. #define PAL_ITEMS_X_COUNT 13
  69. #define COLSIZE 18
  70. #define RIGHT_BAR_W PAL_ITEMS_X_COUNT*COLSIZE
  71.  
  72. #define TO_CANVAS_X(xval) xval - canvas.x/zoom.value
  73. #define TO_CANVAS_Y(yval) yval - canvas.y/zoom.value
  74.  
  75. block canvas = { NULL, NULL, NULL, NULL };
  76. block wrapper = { LEFTBAR_W, TOPBAR_H, NULL, NULL };
  77. block right_bar = { NULL, 10+TOPBAR_H, RIGHT_BAR_W+10, NULL };
  78. block image_menu_btn = { NULL, 4, NULL, 22 };
  79.  
  80. dword linear_gradient[RIGHT_BAR_W];
  81. block b_color_gradient = {NULL, 40+TOPBAR_H, RIGHT_BAR_W, 25};
  82. //block b_opacity_gradient = {NULL, 75+TOPBAR_H, RIGHT_BAR_W, 15};
  83. block b_last_colors = {NULL, 75+TOPBAR_H, RIGHT_BAR_W, COLSIZE};
  84. block b_default_palette = {NULL, COLSIZE+10+75+TOPBAR_H, RIGHT_BAR_W, COLSIZE*9};
  85.  
  86. dword transparent = 0xBFCAD2;
  87. dword color1 = 0x000000;
  88. dword color2 = 0xBFCAD2;
  89. dword tool_color;
  90.  
  91. signed hoverX;
  92. signed hoverY;
  93. signed priorHoverX;
  94. signed priorHoverY;
  95. bool canvasMouseMoved = false;
  96.  
  97. EVENTS button;
  98. EVENTS key;
  99.  
  100. enum {
  101.         BTNS_PALETTE_COLOR_MAS = 100,
  102.         BTNS_LAST_USED_COLORS = 400
  103. };
  104.  
  105. proc_info Form;
  106. dword semi_white;
  107. bool bg_dark=false;
  108.  
  109. more_less_box zoom = { 11, 1, 40, "Zoom" };
  110.  
  111. dword default_palette[] = {
  112. 0x330000,0x331900,0x333300,0x193300,0x003300,0x003319,0x003333,0x001933,0x000033,0x190033,
  113. 0x330033,0x330019,0x000000,0x660000,0x663300,0x666600,0x336600,0x006600,0x006633,0x006666,
  114. 0x003366,0x000066,0x330066,0x660066,0x660033,0x202020,0x990000,0x994C00,0x999900,0x4C9900,
  115. 0x009900,0x00994C,0x009999,0x004C99,0x000099,0x4C0099,0x990099,0x99004C,0x404040,0xCC0000,
  116. 0xCC6600,0xCCCC00,0x66CC00,0x00CC00,0x00CC66,0x00CCCC,0x0066CC,0x0000CC,0x6600CC,0xCC00CC,
  117. 0xCC0066,0x606060,0xFF0000,0xFF8000,0xFFFF00,0x80FF00,0x00FF00,0x00FF80,0x00FFFF,0x0080FF,
  118. 0x0000FF,0x7F00FF,0xFF00FF,0xFF007F,0x808080,0xFF3333,0xFF9933,0xFFFF33,0x99FF33,0x33FF33,
  119. 0x33FF99,0x33FFFF,0x3399FF,0x3333FF,0x9933FF,0xFF33FF,0xFF3399,0xA0A0A0,0xFF6666,0xFFB266,
  120. 0xFFFF66,0xB2FF66,0x66FF66,0x66FFB2,0x66FFFF,0x66B2FF,0x6666FF,0xB266FF,0xFF66FF,0xFF66B2,
  121. 0xC0C0C0,0xFF9999,0xFFCC99,0xFFFF99,0xCCFF99,0x99FF99,0x99FFCC,0x99FFFF,0x99CCFF,0x9999FF,
  122. 0xCC99FF,0xFF99FF,0xFF99CC,0xE0E0E0,0xFFCCCC,0xFFE5CC,0xFFFFCC,0xE5FFCC,0xCCFFCC,0xCCFFE5,
  123. 0xCCFFFF,0xCCE5FF,0xCCCCFF,0xE5CCFF,0xFFCCFF,0xFFCCE5,0xFFFFFF 
  124. };
  125.  
  126. #define LAST_USED_MAX 13
  127. dword last_used_colors[LAST_USED_MAX] = {
  128. 0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,
  129. 0xFFFFFF,0xFFFFFF,0xFFFFFF
  130. };
  131.  
  132. CustomCursor Cursor;
  133. dword CursorBar = FROM "cursors/bar.cur";
  134. dword CursorFill = FROM "cursors/fill.cur";
  135. dword CursorLine = FROM "cursors/line.cur";
  136. dword CursorPencil = FROM "cursors/pencil.cur";
  137. dword CursorPipette = FROM "cursors/pipette.cur";
  138. dword CursorRectangle = FROM "cursors/rectangle.cur";
  139. dword CursorSelect = FROM "cursors/select.cur";
  140.  
  141. _image image;
  142.  
  143. #include "actions_history.h"
  144.  
  145. _ActionsHistory actionsHistory;
  146.  
  147. #include "tools.h"
  148. #include "canvas_resize.h"
  149.  
  150. //===================================================//
  151. //                                                   //
  152. //                       CODE                        //
  153. //                                                   //
  154. //===================================================//
  155.  
  156. libimg_image top_icons;
  157. libimg_image left_icons;
  158.  
  159. void main()
  160. {
  161.         word btn;
  162.         libimg_image open_image;
  163.         dword bg_col;
  164.  
  165.         load_dll(libio,  #libio_init,  1);
  166.         load_dll(libimg, #libimg_init, 1);
  167.         load_dll(boxlib, #box_lib_init,0);
  168.  
  169.         Libimg_LoadImage(#top_icons, "/sys/icons16.png");
  170.         Libimg_LoadImage(#left_icons, "/sys/icons16.png");
  171.  
  172.         sc.get();
  173.         bg_col = sc.work;
  174.         bg_dark = skin_is_dark();
  175.  
  176.         semi_white = MixColors(sc.work, 0xFFFfff, bg_dark*90 + 96);
  177.         Libimg_ReplaceColor(top_icons.image, top_icons.w, top_icons.h, 0xffFFFfff, semi_white);
  178.         Libimg_ReplaceColor(top_icons.image, top_icons.w, top_icons.h, 0xffCACBD6, MixColors(semi_white, 0, 220));
  179.  
  180.         Libimg_ReplaceColor(left_icons.image, left_icons.w, left_icons.h, 0xffFFFfff, sc.work);
  181.         Libimg_ReplaceColor(left_icons.image, left_icons.w, left_icons.h, 0xffCACBD6, MixColors(sc.work, 0, 200));
  182.  
  183.         //fix line and rectandle color for dark skins
  184.         if (bg_dark) Libimg_ReplaceColor(left_icons.image, left_icons.w, left_icons.h, 0xff545454, 0xffD3D3D4);
  185.  
  186.         EventSetActiveColor(1, color1);
  187.  
  188.         if (!param[0]) {
  189.                 image.create(32, 32);
  190.         }
  191.         else
  192.         {
  193.                 Libimg_LoadImage(#open_image, #param);
  194.  
  195.                 if (open_image.w*open_image.h>MAX_CELL_SIZE*MAX_CELL_SIZE) {
  196.                         notify(T_ERROR_IMA_ICONEDIT);
  197.                         ExitProcess();
  198.                 }
  199.                 else {
  200.                         image.create(open_image.h, open_image.w);
  201.                         image.set_image(open_image.imgsrc);
  202.                 }
  203.         }
  204.  
  205.         actionsHistory.init();
  206.  
  207.         initTools();
  208.         setCurrentTool(TOOL_PENCIL);
  209.        
  210.         SetEventMask(EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_MOUSE+EVM_MOUSE_FILTER);
  211.  
  212.         loop() switch(WaitEvent())
  213.         {
  214.                 case evMouse:
  215.                         if (Window_CanvasReSize.thread_exists()) break;
  216.                         mouse.get();
  217.                        
  218.                         if (mouse.lkm) tool_color = color1;
  219.                         if (mouse.pkm) tool_color = color2;
  220.                         if (mouse.mkm) break;
  221.  
  222.                         hoverX = mouse.x - canvas.x / zoom.value;
  223.                         hoverY = mouse.y - canvas.y / zoom.value;
  224.                         if (hoverX<0) hoverX = 0;
  225.                         if (hoverY<0) hoverY = 0;
  226.                         if (hoverX>image.columns-1) hoverX = image.columns-1;
  227.                         if (hoverY>image.rows-1) hoverY = image.rows-1;
  228.                         canvasMouseMoved = false;
  229.                         if (priorHoverX != hoverX) canvasMouseMoved = true;
  230.                         if (priorHoverY != hoverY) canvasMouseMoved = true;
  231.                         priorHoverX = hoverX;
  232.                         priorHoverY = hoverY;
  233.                         //DrawBar(Form.cwidth-100, 3, 80, 12, 0xFFFfff);
  234.                         //WriteText(Form.cwidth-100, 3, 0x80, 0x000000,
  235.                         //      sprintf(#param, "%i %i", hoverX, hoverY));
  236.  
  237.                         if (currentTool != TOOL_NONE)
  238.                                 tools[currentTool].onMouseEvent(mouse.x, mouse.y, mouse.lkm, mouse.pkm);
  239.  
  240.                         if (mouse.vert) {
  241.                                 if (mouse.vert==65535) zoom.inc();
  242.                                 if (mouse.vert==1) zoom.dec();
  243.                                 DrawEditArea();
  244.                         }
  245.  
  246.                         if (wrapper.hovered()) SetCursor();
  247.                         else Cursor.Restore();
  248.  
  249.                         if (mouse.down) {
  250.                                 if (b_color_gradient.hovered())
  251.                                 || (b_last_colors.hovered())
  252.                                 || (b_default_palette.hovered()) {
  253.                                         if (mouse.key&MOUSE_LEFT) EventSetActiveColor(1, GetPixelUnderMouse());
  254.                                         if (mouse.key&MOUSE_RIGHT) EventSetActiveColor(2, GetPixelUnderMouse());
  255.                                 }      
  256.                         }
  257.  
  258.                         break;
  259.  
  260.                 case evButton:
  261.                         if (Window_CanvasReSize.thread_exists()) break;
  262.                         btn = GetButtonID();
  263.  
  264.                         if (zoom.click(btn)) DrawEditArea();
  265.  
  266.                         button.press(btn);
  267.          
  268.                 case evKey:
  269.                         GetKeys();
  270.  
  271.                         if (key_modifier&KEY_LCTRL) || (key_modifier&KEY_RCTRL) key.press(ECTRL + key_scancode);
  272.  
  273.                         if (key_modifier&KEY_LSHIFT) || (key_modifier&KEY_RSHIFT) {
  274.                                 if (key_scancode == SCAN_CODE_DEL) EventCleanCanvas();
  275.                         }
  276.  
  277.                         if (currentTool != TOOL_NONE) && (tools[currentTool].onKeyEvent != 0)
  278.                                 tools[currentTool].onKeyEvent(key_scancode);
  279.  
  280.                         key.press(key_scancode);
  281.  
  282.                         if (key_scancode == SCAN_CODE_KEY_Z) actionsHistory.undoLastAction();
  283.                         if (key_scancode == SCAN_CODE_KEY_Y) actionsHistory.redoLastAction();
  284.  
  285.                         if (key_scancode == SCAN_CODE_MINUS) {zoom.dec(); DrawEditArea();}
  286.                         if (key_scancode == SCAN_CODE_PLUS)  {zoom.inc(); DrawEditArea();}
  287.  
  288.                         break;
  289.                  
  290.                 case evReDraw:
  291.                         Window_CanvasReSize.thread_exists();
  292.                         if (CheckActiveProcess(Form.ID)) EventCheckMenuItemSelected();
  293.                         DrawWindow();
  294.                         break;
  295.         }
  296. }
  297.  
  298. void DrawTopPanelButton(dword _event, _hotkey, _x, _icon_n)
  299. {
  300.         DrawWideRectangle(_x, 4, 22, 22, 3, semi_white);
  301.         PutPixel(_x,4,sc.work);
  302.         PutPixel(_x,4+21,sc.work);
  303.         PutPixel(_x+21,4,sc.work);
  304.         PutPixel(_x+21,4+21,sc.work);
  305.         DefineHiddenButton(_x, 4, 21, 21, button.add(_event));
  306.         img_draw stdcall(top_icons.image, _x+3, 7, 16, 16, 0, _icon_n*16);
  307.         if (_hotkey) key.add_n(_hotkey, _event);
  308. }
  309.  
  310.  
  311. int DrawFlatPanelButton(dword _id, _x, _y, _text)
  312. {
  313.         #define P 10
  314.         int w = strlen(_text)*6 + P + P;
  315.         DrawBar(_x, _y, w, 22, semi_white);
  316.         PutPixel(_x,_y,sc.work);
  317.         PutPixel(_x,_y+21,sc.work);
  318.         PutPixel(_x+w-1,_y,sc.work);
  319.         PutPixel(_x+w-1,_y+21,sc.work);
  320.         DefineHiddenButton(_x, _y, w, 21, _id);
  321.         WriteText(_x+P, _y+7, 0x80, sc.work_text, _text);
  322.         return w;
  323. }
  324.  
  325. void DrawLeftPanelButton(dword _event, _hotkey, _y, _icon_n)
  326. {
  327.         int x = 5;
  328.         DrawRectangle(x, _y, 22-1, 22-1, sc.work);
  329.         DefineHiddenButton(x, _y, 21, 21, button.add(_event));
  330.         img_draw stdcall(left_icons.image, x+3, _y+3, 16, 16, 0, _icon_n*16);
  331.         key.add_n(_hotkey, _event);
  332. }
  333. void DrawStatusBar()
  334. {
  335.         zoom.draw(wrapper.x, wrapper.y + wrapper.h + 6);
  336.  
  337.         sprintf(#param,"%i x %i", image.columns, image.rows);
  338.         DrawCaptButton(
  339.                 wrapper.x+wrapper.w-calc(strlen(#param)*8) -6 - 1,
  340.                 zoom.y,
  341.                 calc(strlen(#param)*8)+6,
  342.                 18,
  343.                 button.add(#EventCanvasResize),
  344.                 sc.button,
  345.                 sc.button_text,
  346.                 #param
  347.                 );
  348. }
  349.  
  350.  
  351. void DrawWindow()
  352. {
  353.         #define GAPH 27
  354.         #define GAPV 28
  355.         #define BLOCK_SPACE 10
  356.         incn tx;
  357.         incn ty;
  358.         sc.get();
  359.         DefineAndDrawWindow(115+random(100), 50+random(100), 700, 540, 0x73, NULL, T_TITLE, 0);
  360.         GetProcessInfo(#Form, SelfInfo);
  361.         if (Form.status_window>2) return;
  362.         if (Form.width  < 560) { MoveSize(OLD,OLD,560,OLD); return; }
  363.         if (Form.height < 430) { MoveSize(OLD,OLD,OLD,430); return; }
  364.         button.init(40);
  365.         key.init(40);
  366.  
  367.         right_bar.x = Form.cwidth - right_bar.w;
  368.         b_color_gradient.x = b_last_colors.x = b_default_palette.x = right_bar.x;
  369.         DrawBar(0, 0, Form.cwidth, TOPBAR_H-1, sc.work);
  370.         DrawBar(0, TOPBAR_H-1, Form.cwidth, 1, sc.work_graph);
  371.  
  372.         tx.n = 5-GAPH;
  373.         DrawTopPanelButton(#EventCreateNewIcon,  ECTRL + SCAN_CODE_KEY_N, tx.inc(GAPH), 2);
  374.         DrawTopPanelButton(#EventOpenIcon,       ECTRL + SCAN_CODE_KEY_O, tx.inc(GAPH), 0);
  375.         DrawTopPanelButton(#EventSaveIconToFile, ECTRL + SCAN_CODE_KEY_S, tx.inc(GAPH), 5);
  376.         DrawTopPanelButton(#EventMoveLeft,       ECTRL + SCAN_CODE_LEFT, tx.inc(GAPH+BLOCK_SPACE), 30);
  377.         DrawTopPanelButton(#EventMoveRight,      ECTRL + SCAN_CODE_RIGHT, tx.inc(GAPH), 31);
  378.         DrawTopPanelButton(#EventMoveUp,         ECTRL + SCAN_CODE_UP, tx.inc(GAPH), 32);
  379.         DrawTopPanelButton(#EventMoveDown,       ECTRL + SCAN_CODE_DOWN, tx.inc(GAPH), 33);
  380.         DrawTopPanelButton(#EventFlipHor,        0, tx.inc(GAPH+BLOCK_SPACE), 34);
  381.         DrawTopPanelButton(#EventFlipVer,        0, tx.inc(GAPH), 35);
  382.         DrawTopPanelButton(#EventRotateLeft,     ECTRL + SCAN_CODE_KEY_L, tx.inc(GAPH), 37);
  383.         DrawTopPanelButton(#EventRotateRight,    ECTRL + SCAN_CODE_KEY_R, tx.inc(GAPH), 36);
  384.         DrawTopPanelButton(#EventTestIcon,       ECTRL + SCAN_CODE_KEY_T, tx.inc(GAPH+BLOCK_SPACE), 12);
  385.         DrawTopPanelButton(#EventCrop,           0, tx.inc(GAPH+BLOCK_SPACE), 46);
  386.  
  387.         image_menu_btn.x = tx.n;
  388.         image_menu_btn.w = DrawFlatPanelButton(button.add(#EventShowImageMenu), image_menu_btn.x, image_menu_btn.y, T_MENU_IMAGE);
  389.         //tx.inc(image_menu_btn.w + BLOCK_SPACE);
  390.        
  391.         DrawEditArea();
  392.  
  393.         DrawBar(0, TOPBAR_H, LEFTBAR_W-1, Form.cheight - TOPBAR_H, sc.work);
  394.  
  395.         ty.n = right_bar.y - GAPV - 2;
  396.  
  397.         DrawLeftPanelButton(#EventSelectToolPencil, SCAN_CODE_KEY_P, ty.inc(GAPV), 38);
  398.         DrawLeftPanelButton(#EventSelectToolPick,   SCAN_CODE_KEY_I, ty.inc(GAPV), 39);
  399.         DrawLeftPanelButton(#EventSelectToolFill,   SCAN_CODE_KEY_F, ty.inc(GAPV), 40);
  400.         DrawLeftPanelButton(#EventSelectToolLine,   SCAN_CODE_KEY_L, ty.inc(GAPV), 41);
  401.         DrawLeftPanelButton(#EventSelectToolRect,   SCAN_CODE_KEY_R, ty.inc(GAPV), 42);
  402.         DrawLeftPanelButton(#EventSelectToolBar,    SCAN_CODE_KEY_B, ty.inc(GAPV), 43);
  403.         DrawLeftPanelButton(#EventSelectToolSelect, SCAN_CODE_KEY_S, ty.inc(GAPV), 44);
  404.         DrawLeftPanelButton(#EventSelectToolScrCopy,SCAN_CODE_KEY_Q, ty.inc(GAPV), 45);
  405.         DrawLeftPanelSelection();
  406.  
  407.         button.add_n(1, #EventExitIconEdit);
  408.  
  409.         DrawBar(wrapper.x+wrapper.w, TOPBAR_H, Form.cwidth-wrapper.x-wrapper.w,
  410.                 Form.cheight - TOPBAR_H, sc.work);
  411.         DrawActiveColor(right_bar.y);
  412.         DrawColorPallets();
  413.         DrawPreview();
  414.  
  415.         DrawBar(LEFTBAR_W-1, wrapper.y + wrapper.h, wrapper.w+1,
  416.                 Form.cheight - wrapper.y - wrapper.h, sc.work);
  417.         DrawStatusBar();
  418. }
  419.  
  420. void DrawLeftPanelSelection()
  421. {
  422.         if (previousTool!=-1) DrawRectangle3D(5, previousTool*GAPV+right_bar.y-2, 16+3+2, 16+3+2, sc.work, sc.work);
  423.         DrawRectangle3D(5, currentTool*GAPV+right_bar.y-2, 16+3+2, 16+3+2, 0x333333, 0x777777);
  424. }
  425.  
  426. void DrawEditArea()
  427. {
  428.         dword color1=0xC0C0C0;
  429.         int top_side;
  430.         int left_side;
  431.  
  432.         wrapper.w = Form.cwidth - right_bar.w - 10 - wrapper.x;
  433.         wrapper.h = Form.cheight - TOPBAR_H - 35;
  434.  
  435.         //canvas{
  436.         canvas.w = image.columns * zoom.value;
  437.         canvas.h = image.rows * zoom.value;
  438.         if (canvas.w+2 > wrapper.w) || (canvas.h+2 > wrapper.h) {
  439.                 zoom.value--;
  440.                 if (zoom.x) zoom.redraw();
  441.                 DrawEditArea();
  442.                 return;
  443.         }
  444.         canvas.x = -zoom.value*image.columns+wrapper.w/2 + wrapper.x;
  445.         canvas.y = -zoom.value*image.rows+wrapper.h/2 + wrapper.y;
  446.         DrawCanvas();
  447.         //}
  448.  
  449.         left_side = canvas.x-wrapper.x-1;
  450.         top_side = canvas.y-wrapper.y-1;
  451.  
  452.         DrawRectangle(wrapper.x-1, wrapper.y-1, wrapper.w, wrapper.h, sc.work_graph);
  453.  
  454.         if (left_side>0)
  455.         {
  456.                 DrawBar(wrapper.x, wrapper.y, wrapper.w-1, top_side, color1); //top
  457.                 DrawBar(wrapper.x, wrapper.y+wrapper.h-top_side-1, wrapper.w-1, top_side, color1); //bottom
  458.         }
  459.         if (top_side>0)
  460.         {
  461.                 //left
  462.                 DrawBar(wrapper.x, wrapper.y+top_side, left_side,
  463.                         wrapper.h-top_side-top_side, color1);
  464.                 //right
  465.                 DrawBar(wrapper.x+wrapper.w-left_side-1, wrapper.y+top_side, left_side,
  466.                         wrapper.h-top_side-top_side, color1);
  467.         }
  468.         DrawRectangle(canvas.x-1, canvas.y-1, canvas.w+1, canvas.h+1, 0x808080);
  469. }
  470.  
  471. void DrawActiveColor(dword iny)
  472. {
  473.         #define CELL 20
  474.         static dword outy;
  475.         if (iny != NULL) outy = iny;
  476.         DrawFrame(right_bar.x, outy, CELL, CELL, NULL);
  477.         DrawBar(right_bar.x+2, outy+2, CELL-4, CELL-4, color1);
  478.  
  479.         DrawFrame(right_bar.x+CELL+5, outy, CELL, CELL, NULL);
  480.         DrawBar(right_bar.x+CELL+5+2, outy+2, CELL-4, CELL-4, color2);
  481.  
  482.         //sprintf(#param, "%A", color1);
  483.         //WriteTextWithBg(right_bar.x+30, outy+3, 0xD0, sc.work_text, #param+4, sc.work);
  484.         DrawCurrentColorGradient();
  485. }
  486.  
  487. int lmax;
  488. void GenerateCurrentColorGradient()
  489. {
  490.         int i, avg, rmax;
  491.  
  492.         rgb.DwordToRgb(color1);
  493.         avg = 255 - calc(rgb.r + rgb.g + rgb.b / 3);
  494.  
  495.         lmax = b_color_gradient.w *avg/255 | 1;
  496.         rmax = b_color_gradient.w - lmax | 1;
  497.         if (lmax == 0) lmax=1;
  498.         if (rmax == 0) rmax=1;
  499.        
  500.         for (i=0; i<lmax; i++) {
  501.                 linear_gradient[i] = MixColors(color1,0xFFFfff,255*i/lmax);
  502.         }
  503.  
  504.         for (i=0 ; i<=rmax; i++) {
  505.                 linear_gradient[lmax+rmax - i] = MixColors(color1,0x000000,255*i/rmax);
  506.         }
  507. }
  508.  
  509. int DrawGradientMarker(dword marker_x, marker_color)
  510. {
  511.         if (marker_x > b_color_gradient.w - 1) marker_x = b_color_gradient.w - 1;
  512.         DrawBar(b_color_gradient.x + marker_x-2, b_color_gradient.y-3, 5, 1, marker_color);
  513.         DrawBar(b_color_gradient.x + marker_x-1, b_color_gradient.y-2, 3, 1, marker_color);
  514.         PutPixel(b_color_gradient.x + marker_x, b_color_gradient.y-1, marker_color);
  515.         return marker_x;
  516. }
  517.  
  518. int old_marker_pos;
  519. void DrawCurrentColorGradient()
  520. {
  521.         int i;
  522.         for (i=0 ; i<b_color_gradient.w; i++) {
  523.                 DrawBar(b_color_gradient.x+i, b_color_gradient.y, 1, b_color_gradient.h, linear_gradient[i]);          
  524.         }
  525.         DrawGradientMarker(old_marker_pos, sc.work);
  526.         old_marker_pos = DrawGradientMarker(lmax, 0xFFFfff * bg_dark);
  527. }
  528.  
  529. void DrawColorPallets()
  530. {
  531.         int r, c, i=0;
  532.         //Last used colors
  533.         for (r = 0; r < LAST_USED_MAX/PAL_ITEMS_X_COUNT; r++)
  534.         {
  535.                 for (c = 0; c < PAL_ITEMS_X_COUNT; c++, i++)
  536.                 {
  537.                         DrawBar(c*COLSIZE + b_last_colors.x, r*COLSIZE + b_last_colors.y,
  538.                                 COLSIZE, COLSIZE, last_used_colors[i]);
  539.                 }
  540.         }
  541.         i=0;
  542.         //Default colors
  543.         for (r = 0; r < 9; r++)
  544.         {
  545.                 for (c = 0; c < PAL_ITEMS_X_COUNT; c++, i++)
  546.                 {
  547.                         DrawBar(c*COLSIZE + b_default_palette.x, r*COLSIZE + b_default_palette.y,
  548.                                 COLSIZE, COLSIZE, default_palette[PALLETE_SIZE-i]);
  549.                 }
  550.         }
  551. }
  552.  
  553. void DrawCanvasPixel(dword _r,_c,_color)
  554. {
  555.         DrawBar(_c*zoom.value + canvas.x, _r*zoom.value + canvas.y,
  556.         zoom.value, zoom.value, _color);
  557. }
  558.  
  559. void DrawCanvas()
  560. {
  561.         int r, c;
  562.         dword color;
  563.  
  564.         if ((currentTool != TOOL_NONE) && (tools[currentTool].onCanvasDraw != 0))
  565.         {
  566.                 tools[currentTool].onCanvasDraw();
  567.         }
  568.  
  569.         for (r = 0; r < image.rows; r++)
  570.         {
  571.                 for (c = 0; c < image.columns; c++)
  572.                 {
  573.                         if (image.pixel_state.is_drawable(r,c))
  574.                                 DrawCanvasPixel(r, c, image.get_pixel(r,c));
  575.                 }
  576.         }
  577.         image.pixel_state.reset_and_set_all_drawable();
  578.  
  579.         DrawPreview();
  580. }
  581.  
  582. void DrawPreview()
  583. {
  584.         int x = right_bar.x;
  585.         int y = b_default_palette.y + b_default_palette.h + 6;
  586.         int preview_h = Form.cheight - y;
  587.  
  588.         if (image.columns > right_bar.w) return;
  589.         if (image.rows > preview_h) return;
  590.  
  591.         _PutImage(right_bar.w - image.columns / 2 + x - 3,
  592.                 preview_h - image.rows / 2 + y,
  593.                 image.columns, image.rows, image.get_image()
  594.                 );
  595. }
  596.  
  597. dword GetPixelUnderMouse()
  598. {
  599.         return GetPixelColorFromScreen(mouse.x + Form.left + 5, mouse.y + Form.top + skin_height);
  600. }
  601.  
  602. int preview_size = 128;
  603. void DrawImageWithBg(dword _x, _y, _col_to)
  604. {
  605.         _x *= preview_size;
  606.         _y *= preview_size;
  607.         DrawWideRectangle(_x,_y, preview_size, preview_size, preview_size-image.columns/2, _col_to);
  608.         _PutImage(preview_size - image.columns / 2 + _x, preview_size - image.rows / 2 + _y,
  609.                 image.columns, image.rows, image.get_image_with_replaced_color(color2, _col_to));
  610. }
  611.  
  612. void ShowWindow_TestIcon()
  613. {
  614.         if (image.rows>=preview_size) || (image.columns>=preview_size) {
  615.                 notify(T_TOO_BIG_IMAGE_FOR_PREVIEW);
  616.                 return;
  617.         }
  618.         loop() switch(WaitEvent())
  619.         {
  620.                 case evButton:
  621.                         if (GetButtonID()) ExitProcess();
  622.                         break;
  623.          
  624.                 case evKey:
  625.                         GetKeys();
  626.                         if (key_scancode == SCAN_CODE_ESC) ExitProcess();
  627.                         break;
  628.                  
  629.                 case evReDraw:
  630.                         DefineAndDrawWindow(Form.left+100, Form.top+100, preview_size*2+9,
  631.                                 preview_size*2+skin_height+4, 0x74, NULL, T_TEST_ICON, 0);
  632.                         DrawImageWithBg(0, 0, 0x000000);
  633.                         DrawImageWithBg(1, 0, 0xFFFfff);
  634.                         DrawImageWithBg(0, 1, GetPixelColorFromScreen(0, 0));
  635.                         DrawImageWithBg(1, 1, sc.work);
  636.                         break;
  637.         }
  638. }
  639.  
  640. //===================================================//
  641. //                                                   //
  642. //                      EVENTS                       //
  643. //                                                   //
  644. //===================================================//
  645.  
  646. void EventCreateNewIcon()
  647. {
  648.         EventSaveIconToFile();
  649.         Window_CanvasReSize.create();
  650. }
  651.  
  652. void EventOpenIcon()
  653. {
  654.         RunProgram("/sys/lod", sprintf(#param, "*png* %s",#program_path));
  655. }
  656.  
  657. void EventSaveIconToFile()
  658. {
  659.         int i=0;
  660.         char save_file_name[4096];
  661.         char save_path_stable[4096];
  662.         strcpy(#save_path_stable, "/tmp0/1");
  663.         do {
  664.                 i++;
  665.                 sprintf(#save_file_name, "%s/saved_icon_%i.png", #save_path_stable, i);
  666.         } while (file_exists(#save_file_name));
  667.         save_image(image.get_image(), image.columns, image.rows, #save_file_name);
  668. }
  669.  
  670. void EventCleanCanvas()
  671. {
  672.         image.create(image.rows, image.columns);
  673.         actionsHistory.saveCurrentState();
  674.         DrawCanvas();
  675. }
  676.  
  677. void EventExitIconEdit()
  678. {
  679.         EventSaveIconToFile();
  680.         ExitProcess();
  681. }
  682.  
  683. void EventSetActiveColor(int _number, _color)
  684. {
  685.         int i;
  686.         if (last_used_colors[0] == _color) return;
  687.         for (i=LAST_USED_MAX-1; i>0; i--) {
  688.                 last_used_colors[i] = last_used_colors[i-1];
  689.         }
  690.         last_used_colors[0] = _color;
  691.  
  692.         if (_number == 1) color1 = _color;
  693.         if (_number == 2) color2 = _color;
  694.  
  695.         if (b_color_gradient.hovered()) {
  696.                 lmax = mouse.x - b_color_gradient.x;
  697.         }
  698.         else {
  699.                 GenerateCurrentColorGradient();
  700.         }
  701.         DrawActiveColor(NULL);
  702.         DrawColorPallets();
  703. }
  704.  
  705. void EventTestIcon()
  706. {
  707.         CreateThread(#ShowWindow_TestIcon, #test_icon_stak+4092);
  708. }
  709.  
  710. void EventMove(dword _action)
  711. {
  712.         if (selection.state) {
  713.                 selection.buf.move(_action);
  714.                 SelectTool_onCanvasDraw();
  715.         }
  716.         else {
  717.                 image.move(_action);
  718.                 DrawCanvas();
  719.         }
  720.         actionsHistory.saveCurrentState();
  721. }
  722.  
  723. void EventCrop()
  724. {
  725.         if (selection.state) {
  726.                 EventSaveIconToFile();
  727.                 image.create(selection.buf.rows, selection.buf.columns);
  728.                 selection.move_to_point(0,0);
  729.                 selection.apply_to_image();
  730.                 selection.reset();
  731.                 actionsHistory.init();
  732.                 DrawWindow();
  733.         }
  734.         else {
  735.                 notify(T_ERROR_CROP_TOOL);
  736.         }
  737. }
  738.  
  739. void EventShowImageMenu()
  740. {
  741.         open_lmenu(Form.left+5 + image_menu_btn.x, Form.top+skin_height +
  742.                 image_menu_btn.y + image_menu_btn.h, MENU_ALIGN_TOP_LEFT, NULL, #image_menu_items);
  743. }
  744.  
  745. void EventCheckMenuItemSelected()
  746. {
  747.         switch(get_menu_click()) {
  748.                 case 1:
  749.                         EventCountColorsUsed();
  750.                         break;
  751.                 case 2:
  752.                         EventReplaceImageColors(color1, color2);
  753.                         break;
  754.         }
  755. }
  756.  
  757. void EventCountColorsUsed()
  758. {
  759.         char res_str[64];
  760.         int cur, prev;
  761.         int max = image.rows*image.columns;
  762.         int resi=0;
  763.         bool unic;
  764.         for (cur=0; cur<max; cur++) {
  765.                 unic = true;
  766.                 for (prev=0; prev<cur; prev++) {
  767.                         if (image.mas[prev] == image.mas[cur]) {unic=false; break;}
  768.                 }
  769.                 if (unic) resi++;
  770.         }
  771.         notify( sprintf(#res_str, T_UNIC_COLORS_COUNT, resi) );
  772. }
  773.  
  774. void EventReplaceImageColors(dword c1, c2)
  775. {
  776.         int max = image.rows*image.columns;
  777.         int cur;
  778.         for (cur=0; cur<max; cur++) {
  779.                 if (image.mas[cur] == color1) image.mas[cur] = color2;
  780.         }
  781. }
  782.  
  783. void EventCanvasResize()
  784. {
  785.         notify("Sorry, not implemented yet.");
  786. }
  787.  
  788. void EventMoveLeft() { EventMove(MOVE_LEFT); }
  789. void EventMoveRight() { EventMove(MOVE_RIGHT); }
  790. void EventMoveUp() { EventMove(MOVE_UP); }
  791. void EventMoveDown() { EventMove(MOVE_DOWN); }
  792. void EventFlipHor() { EventMove(FLIP_HOR); }
  793. void EventFlipVer() { EventMove(FLIP_VER); }
  794. void EventRotateLeft() { EventMove(ROTATE_LEFT); }
  795. void EventRotateRight() { EventMove(ROTATE_RIGHT); }
  796.  
  797. void EventSelectToolPencil() { setCurrentTool(TOOL_PENCIL); DrawLeftPanelSelection(); }
  798. void EventSelectToolPick() { setCurrentTool(TOOL_PIPETTE); DrawLeftPanelSelection(); }
  799. void EventSelectToolFill() { setCurrentTool(TOOL_FILL); DrawLeftPanelSelection(); }
  800. void EventSelectToolLine() { setCurrentTool(TOOL_LINE); DrawLeftPanelSelection(); }
  801. void EventSelectToolRect() { setCurrentTool(TOOL_RECT); DrawLeftPanelSelection(); }
  802. void EventSelectToolBar() { setCurrentTool(TOOL_BAR); DrawLeftPanelSelection(); }
  803. void EventSelectToolSelect() { setCurrentTool(TOOL_SELECT); DrawLeftPanelSelection(); }
  804. void EventSelectToolScrCopy() { setCurrentTool(TOOL_SCREEN_COPY); DrawLeftPanelSelection(); }
  805.  
  806. char test_icon_stak22[4096];
  807.  
  808. stop:
  809.  
  810. char test_icon_stak[4096];
  811.