Subversion Repositories Kolibri OS

Rev

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

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