Subversion Repositories Kolibri OS

Rev

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

  1. //если выделить область ячеек и сдвинуть курсор ввода с помощью клавиш, "следы" остануться
  2. //нельзя перемещаться по буквам в редактируемой строке
  3.  
  4. #include "func.h"
  5. #include "parser.h"
  6. #include "calc.h"
  7. #include "use_library.h"
  8.  
  9. #ifdef AUTOBUILD
  10. extern char params[1024];
  11. #endif
  12. char params[1024];
  13.  
  14. #define TABLE_VERSION "0.99.3"
  15.  
  16. // strings
  17. const char *sFileSign = "KolibriTable File\n";
  18. const char sFilename[] = "Filename:";
  19. const char sSave[] = "Save";
  20. const char sLoad[] = "Load";
  21. const char sNew[] = "New";
  22.  
  23. const char er_file_not_found[] = "'Cannot open file' -E";
  24. const char er_format[] = "'Error: bad format' -E";
  25. const char msg_save[] = "'File saved' -O";
  26. const char msg_load[] = "'File loaded' -O";
  27. const char msg_save_error[] = "'Error saving file' -E";
  28. const char msg_new[] = "'Memory cleared' -I";
  29.  
  30. // initial window sizes
  31. #define WND_W 718
  32. #define WND_H 514
  33. // new window size and coordinates
  34. int cWidth;
  35. int cHeight;
  36. kosSysColors sc;
  37. // bottom panel
  38. #define MENU_PANEL_HEIGHT 40
  39.  
  40. // interface colors
  41. #define GRID_COLOR 0xa0a0a0
  42. #define TEXT_COLOR 0x000000
  43. #define CELL_COLOR 0xffffff
  44. #define CELL_COLOR_ACTIVE 0xe0e0ff
  45. #define HEADER_CELL_COLOR 0xE9E7E3
  46. #define HEADER_CELL_COLOR_ACTIVE 0xC4C5BA //0xBBBBFF
  47.  
  48. // button IDs
  49. #define SAVE_BUTTON 100
  50. #define LOAD_BUTTON 101
  51. #define NEW_BUTTON  102
  52. #define SELECT_ALL_BUTTON 103
  53.  
  54. #define COL_BUTTON 0x100
  55. #define ROW_BUTTON (COL_BUTTON + 0x100)
  56. #define COL_HEAD_BUTTON (ROW_BUTTON + 0x100)
  57. #define ROW_HEAD_BUTTON (COL_HEAD_BUTTON + 0x100)
  58. #define CELL_BUTTON (ROW_HEAD_BUTTON + 0x100)
  59.  
  60. // editbox data
  61. char edit_text[256];
  62. edit_box cell_box = {0,9*8-6,WND_H - 16-32,0xffffff,0x94AECE,0,0x808080,0x10000000,255,(dword)&edit_text,0,0};
  63.  
  64. // scrolls
  65. #define SCROLL_SIZE 16
  66. scroll_bar scroll_v = { SCROLL_SIZE,200,398, NULL, SCROLL_SIZE,0,115,15,0,0xeeeeee,0xD2CED0,0x555555,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1};
  67. scroll_bar scroll_h = { 200,NULL,SCROLL_SIZE, NULL, SCROLL_SIZE,0,115,15,0,0xeeeeee,0xD2CED0,0x555555,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1};
  68.  
  69. // ячейки - их параметры и текст
  70. DWORD col_count = 100, row_count = 100;
  71. DWORD *cell_w, *cell_h;
  72. char ***cells;
  73.  
  74. struct GRID
  75. {
  76.         int x,y,w,h;
  77.         int firstx, firsty; // cell x:y in the top left corner
  78. } grid = {
  79.         0,0,NULL,NULL,
  80.         1,1
  81. };
  82.  
  83. char ***values; // значения формул, если есть
  84.  
  85. bool display_formulas = false;  // отображать ли формулы вместо значений
  86.  
  87. // координаты отображаемых столбцов и строк
  88. DWORD *cell_x, *cell_y;
  89.  
  90. // буфер обмена
  91. char ***buffer = NULL;
  92. DWORD buf_col, buf_row;
  93. DWORD buf_old_x, buf_old_y;
  94.  
  95. // это выделенная ячейка
  96. DWORD sel_x = 1, sel_y = 1;
  97. DWORD prev_x = 0, prev_y = 0;   // предыдущая выделенная
  98. int was_single_selection = 0;
  99.  
  100. // конец выделения если выделено несколько ячеек
  101. DWORD sel_end_x = sel_x, sel_end_y = sel_y;
  102.  
  103. // флаг
  104. bool sel_moved = 0;
  105. bool sel_end_move = 0;
  106. // сколько ячеек помещается в окне по х и у
  107. DWORD nx = 0, ny = 0;
  108.  
  109. // флаг реадктирования ячейки
  110. #define is_edit (cell_box.flags & ed_focus)
  111.  
  112. // редактирование имени файла
  113. bool fn_edit = 0;
  114. char fname[256];
  115. edit_box file_box = {160,9*8+12,WND_H - 16-32,0xffffff,0x94AECE,0,0x808080,0x10000000,255,(dword)&fname,0,0};
  116.  
  117. // изменение размеров
  118. #define SIZE_X 1 // состояние
  119. #define SIZE_Y 2
  120. #define SIZE_SELECT 3
  121. #define SIZE_DRAG 4
  122. int size_mouse_x, size_mouse_y, size_id, size_state = 0;
  123.  
  124. // растаскивание ячейки при ее тащении за правый нижний угол, с заполнением ячеек
  125. int drag_x, drag_y;
  126. int old_end_x, old_end_y;
  127.  
  128. void draw_grid();
  129. void EventGridSelectAll();
  130.  
  131. void DrawSelectedFrame(int x, int y, int w, int h, DWORD col)
  132. {
  133.         kos_DrawBar(x,y,w,2,col);          // up
  134.         kos_DrawBar(x,y,2,h,col);          // left
  135.         kos_DrawBar(x,y+h-2,w-2-3,2,col);  // bottom
  136.         kos_DrawBar(x+w-2,y, 2,h-2-3,col); // right
  137.         kos_DrawBar(x+w-4,y+h-4,4,4,col);
  138. }
  139.  
  140. void DrawScrolls()
  141. {
  142.         // HOR
  143.         scroll_h.x = 0;
  144.         scroll_h.y = grid.y + grid.h;
  145.         scroll_h.w = grid.w + SCROLL_SIZE + 1;
  146.         scroll_h.all_redraw = true;
  147.         scroll_h.max_area = col_count - 2;
  148.         scroll_h.cur_area = nx-grid.firstx-1;
  149.         scroll_h.position = grid.firstx-1;
  150.         scrollbar_h_draw((DWORD)&scroll_h);
  151.  
  152.         // VER
  153.         scroll_v.x = grid.x + grid.w;
  154.         scroll_v.y = 0;
  155.         scroll_v.h = grid.h + 1;
  156.         scroll_v.all_redraw = true;
  157.         scroll_v.max_area = row_count - 2;
  158.         scroll_v.cur_area = ny-grid.firsty-1;
  159.         scroll_v.position = grid.firsty-1;
  160.         scrollbar_v_draw((DWORD)&scroll_v);
  161. }
  162.  
  163.  
  164. void start_edit(int x, int y)
  165. {
  166.         int ch = 0;
  167.         if (x < grid.firstx || x > nx - 1)
  168.         {
  169.                 grid.firstx = x;
  170.                 ch = 1;
  171.         }
  172.         if (y < grid.firsty || y > ny - 1)
  173.         {
  174.                 grid.firsty = y;
  175.                 ch = 1;
  176.         }
  177.         if (ch)
  178.         {
  179.                 sel_moved = 1;
  180.                 draw_grid();
  181.         }
  182.  
  183.         file_box.flags &= ~ed_focus;
  184.  
  185.         cell_box.flags = ed_focus;
  186.         cell_box.left = cell_x[x] + 1;
  187.         cell_box.top = cell_y[y];
  188.         cell_box.width = cell_w[x] - 2;
  189.         memset((Byte*)edit_text, 0, sizeof(edit_text));
  190.         if (cells[x][y])
  191.         {
  192.                 strcpy(edit_text, cells[x][y]);
  193.         }
  194.         cell_box.offset = cell_box.shift = cell_box.shift_old = 0;
  195.         cell_box.pos = cell_box.size = strlen(edit_text);
  196.  
  197.         edit_box_draw((DWORD)&cell_box);
  198.         edit_box_draw((DWORD)&file_box);
  199. }
  200.  
  201. void stop_edit()
  202. {
  203.         if (is_edit)
  204.         {
  205.                 cell_box.flags &= ~ed_focus;
  206.                 if (cells[sel_x][sel_y])
  207.                         freemem(cells[sel_x][sel_y]);
  208.                 if (strlen(edit_text) > 0)
  209.                 {
  210.                         cells[sel_x][sel_y] = (char*)allocmem(strlen(edit_text)+1);
  211.                         strcpy(cells[sel_x][sel_y], edit_text);
  212.                 }
  213.                 else
  214.                         cells[sel_x][sel_y] = NULL;
  215.                 //memset((Byte*)edit_text,0, 256);
  216.                 calculate_values();
  217.         }
  218. }
  219.  
  220. void cancel_edit()
  221. {
  222.         if (!is_edit)
  223.                 return;
  224.         cell_box.flags &= ~ed_focus;
  225.         memset((Byte*)edit_text,0, 256);
  226.         draw_grid();
  227. }
  228.  
  229. void check_sel()
  230. {
  231.         DWORD sx0=grid.firstx, sy0=grid.firsty;
  232.  
  233.         if (sel_x >= nx - 1  /*&& sel_x < col_count - nx + grid.firstx + 1*/)
  234.                 //if (sel_x == nx)
  235.                         grid.firstx++;
  236.                 //else
  237.                 //      grid.firstx = sel_x;
  238.         if (sel_y >= ny - 1 /*&& sel_y < row_count - ny + grid.firsty */)
  239.                 //if (sel_y == ny)
  240.                         grid.firsty++;
  241.                 //else
  242.                 //      grid.firsty = sel_y;
  243.  
  244.         if (sel_x < grid.firstx)
  245.                 grid.firstx = sel_x;
  246.         if (sel_y < grid.firsty)
  247.                 grid.firsty = sel_y;
  248.  
  249.         if (sx0 != grid.firstx || sy0 != grid.firsty)
  250.                 sel_moved = 0;                  // надо перерисовать все
  251.  
  252. }
  253.  
  254. void move_selection(DWORD new_x, DWORD new_y)
  255. {
  256.         sel_moved = 1;
  257.         stop_edit();
  258.         prev_x = sel_x;
  259.         prev_y = sel_y;
  260.         sel_x = new_x;
  261.         if (sel_x < 1)
  262.                 sel_x = 1;
  263.         if (sel_x > col_count - 1)
  264.                 sel_x = col_count - 1;
  265.         sel_end_x = sel_x;
  266.         sel_y = new_y;
  267.         if (sel_y < 1)
  268.                 sel_y = 1;
  269.         if (sel_y > row_count - 1)
  270.                 sel_y = row_count - 1;
  271.         sel_end_y = sel_y;
  272.         check_sel();
  273.         draw_grid();
  274. }
  275.  
  276. // x - между low и high ? - необязательно low<high
  277. bool is_between(Dword x, Dword low, Dword high)
  278. {
  279.         return ((low<high)?(x >= low && x <= high):(x >= high && x <= low));
  280. }
  281.  
  282. void clear_cell_slow(int px, int py)
  283. {
  284.         int i;
  285.         int x0 = cell_w[0];
  286.         for (i = grid.firstx; i < px; i++)
  287.         {
  288.                 x0 += cell_w[i];
  289.         }
  290.         int x1 = x0;
  291.         x1 += cell_w[px];
  292.         int y0 = cell_h[0];
  293.         for (i = grid.firsty; i < py; i++)
  294.         {
  295.                 y0 += cell_h[i];
  296.         }
  297.         int y1 = y0;
  298.         y1 += cell_h[py];
  299.         kos_DrawBar(x0 + 1, y0 + 1, x1 - x0 - 1, y1 - y0 - 1, 0xffffff);
  300. }
  301.  
  302.  
  303.  
  304. // рисование ячеек
  305. #define is_x_changed(v) ((v) == sel_x || (v) == prev_x)
  306. #define is_y_changed(v) ((v) == sel_y || (v) == prev_y)
  307.  
  308. void DrawCell(int x, int y, Dword w, Dword h, Dword id, Dword bg_color, char* text, bool header)
  309. {
  310.         bool small = false;
  311.         if (x>grid.x+grid.w || w>grid.w || w<=0) return;
  312.         if (x+w > grid.x + grid.w) {
  313.                 w = grid.x + grid.w - x;
  314.                 small = true;
  315.         }
  316.         if (y+h > grid.y + grid.h) {
  317.                 h = grid.y + grid.h - y;
  318.                 small = true;
  319.         }
  320.         kos_DrawBar(x, y, w, h, bg_color);
  321.         if (!small) {
  322.                 if (id) kos_DefineButton(x+5, y, w-10, h-1, id+BT_NODRAW,0);
  323.                 if (header) kos_WriteTextToWindow( x + w/2 -strlen(text)*4, h/2-7+y, 0x90,TEXT_COLOR,text,0); //WriteTextCenter
  324.                 else kos_DrawCutTextSmall(x+3, h/2-7+y, w-7, TEXT_COLOR, text);
  325.         }
  326. }
  327.  
  328. void draw_grid()
  329. {
  330.         int i,j;
  331.         long x0 = 0, y0 = 0, x = 0, y = 0;
  332.         DWORD bg_color;
  333.         kos_DrawBar(0,0,cell_w[0],cell_h[0],HEADER_CELL_COLOR); // left top cell
  334.         kos_DefineButton(0,0,cell_w[0]-4,cell_h[0]-4, SELECT_ALL_BUTTON + BT_NODRAW, 0);
  335.  
  336.         //kos_DebugValue("sel_moved", sel_moved);
  337.  
  338.         nx=ny=0;
  339.  
  340.         // очистить область около выделенной ячейки
  341.         if (sel_moved)
  342.         {
  343.                 clear_cell_slow(sel_x, sel_y);
  344.                 clear_cell_slow(prev_x, prev_y);
  345.         }
  346.         else
  347.         {
  348.                 // clean all cells
  349.                 //kos_DrawBar(cell_w[0]+1, cell_h[0]+1, grid.w - SCROLL_SIZE-cell_w[0]-1, he - SCROLL_SIZE-cell_h[0]-1, 0xffffff);
  350.         }
  351.  
  352.         // column headers + vertical lines
  353.         cell_x[0] = 0;
  354.         x = cell_w[0];
  355.         nx = 1;
  356.         for (i = 1; i < col_count && x-x0 < grid.w; i++)
  357.         {
  358.                 cell_x[i] = -1;
  359.                 if (i >= grid.firstx)
  360.                 {
  361.                         {                              
  362.                                 //if (!sel_moved || (is_x_changed(i))) {
  363.                                         if (is_between(i,sel_x,sel_end_x)) bg_color = HEADER_CELL_COLOR_ACTIVE; else bg_color = HEADER_CELL_COLOR;
  364.                                         kos_DrawBar(x-x0, 0, 1, grid.h, GRID_COLOR);
  365.                                         DrawCell(x-x0+1, 0, cell_w[i]-1, cell_h[0], i+COL_HEAD_BUTTON, bg_color, cells[i][0], true);
  366.                                 //}
  367.                                 cell_x[i] = x - x0;
  368.                         }
  369.                 }
  370.                 else
  371.                 {
  372.                         x0 += cell_w[i];
  373.                 }
  374.                 x += cell_w[i];
  375.                 nx++;
  376.         }
  377.  
  378.         // row headers + horizontal lines
  379.         y = cell_h[0];
  380.         ny = 1;
  381.         cell_y[0] = 0;
  382.         for (i = 1; i < row_count && y-y0 < grid.h; i++)
  383.         {
  384.                 cell_y[i] = -1;
  385.                 if (i >= grid.firsty)
  386.                 {
  387.                         {
  388.                                 //if (!sel_moved || (is_y_changed(i))) {
  389.                                         if (is_between(i,sel_y,sel_end_y)) bg_color = HEADER_CELL_COLOR_ACTIVE; else bg_color = HEADER_CELL_COLOR;
  390.                                         kos_DrawBar(0, y-y0, grid.w, 1, GRID_COLOR);
  391.                                         DrawCell(0, y-y0+1, cell_w[0], cell_h[i]-1, i+ROW_HEAD_BUTTON, bg_color, cells[0][i], true);
  392.                                 //}
  393.                                 cell_y[i] = y - y0;
  394.                         }
  395.                 }
  396.                 else
  397.                 {
  398.                         y0 += cell_h[i];
  399.                 }
  400.                 y += cell_h[i];
  401.                 ny++;
  402.         }
  403.        
  404.         // cells itself
  405.         y = cell_h[0];
  406.         for (i = grid.firsty; i < ny; i++)
  407.         {
  408.                 x = cell_w[0];
  409.                 for (j = grid.firstx; j < nx; j++)
  410.                 {
  411.                         if (i && j)     //no need to draw headers one more
  412.                         {
  413.                                 bool draw_frame_selection = false;
  414.                                 bool error = false;
  415.                                 bg_color = CELL_COLOR;
  416.  
  417.                                 if (is_between(j,sel_x,sel_end_x) && is_between(i, sel_y, sel_end_y)    // (j,i) - selected
  418.                                 && ((!sel_moved) || (is_x_changed(j) && is_y_changed(i))))                      // and we must draw it
  419.                                 {
  420.                                         if (i == sel_y && j == sel_x)
  421.                                         {
  422.                                                 draw_frame_selection = true;
  423.                                                 drag_x = x + cell_w[j] - 4;
  424.                                                 drag_y = y + cell_h[i] - 4;
  425.                                         }
  426.                                         else {
  427.                                                 bg_color = CELL_COLOR_ACTIVE; // selected but not main
  428.                                         }
  429.                                 }
  430.  
  431.                                 char *text;
  432.                                 if (values[j][i] && values[j][i][0] == '#')
  433.                                 {
  434.                                         text = cells[j][i];
  435.                                         error = true;
  436.                                 }
  437.                                 else {
  438.                                         text = (values[j][i] && !display_formulas ? values[j][i] : cells[j][i]);
  439.                                 }
  440.  
  441.                                 DrawCell(x+1, y+1, cell_w[j]-1, cell_h[i]-1, 0, bg_color, text, false);
  442.                                 if (draw_frame_selection && j<nx-1 && i<ny-1) {
  443.                                         DrawSelectedFrame(x+1,y, cell_w[j]-1, cell_h[i]+1, TEXT_COLOR);
  444.                                 }
  445.                                 else if (error) kos_DrawRegion(x+1, y+1, cell_w[j]-1, cell_h[i]-1, 0xff0000, 0);
  446.                         }
  447.                         x += cell_w[j];
  448.                 }
  449.                 y += cell_h[i];
  450.         }
  451.         DrawScrolls();
  452. }
  453.  
  454. // очень быстрое рисование сетки, в процессе изменения размеров ячеек
  455. void draw_size_grid()
  456. {
  457.         //rtlDebugOutString("draw size grid");
  458.  
  459.         if (size_state == SIZE_X)
  460.         {
  461.                 int x, x0, i;
  462.  
  463.                 x = cell_w[0];
  464.                 x0 = 0;
  465.                 for (i = 1; i < col_count && x - x0 + cell_w[i] < grid.w - 10; i++)
  466.                 {
  467.                         if (i >= grid.firstx)
  468.                         {
  469.                                 if (i >= size_id)
  470.                                         kos_DrawLine(x - x0, 0, x - x0, grid.h, 0, 1);
  471.                         }
  472.                         else
  473.                                 x0 += cell_w[i];
  474.                         x += cell_w[i];
  475.                 }
  476.                 kos_DrawLine(x - x0, 0, x - x0, grid.h, 0, 1);
  477.         }
  478.         else
  479.         {
  480.                 int y, y0, i;
  481.  
  482.                 y = cell_h[0];
  483.                 y0 = 0;
  484.                 for (i = 1; i < col_count && y - y0 + cell_h[i] < grid.h - 10; i++)
  485.                 {
  486.                         if (i >= grid.firsty)
  487.                         {
  488.                                 if (i >= size_id)
  489.                                         kos_DrawLine(0, y - y0, grid.w, y - y0, 0, 1);
  490.                         }
  491.                         else
  492.                                 y0 += cell_h[i];
  493.                         y += cell_h[i];
  494.                 }
  495.                 kos_DrawLine(0, y - y0, grid.w, y - y0, 0, 1);
  496.         }
  497.  
  498. }
  499.  
  500.  
  501. // быстрое рисование выделенной области при выделении мышью
  502. #define DCOLOR 0
  503. //0xff0000
  504. #define DINVERT 1
  505. void draw_drag()
  506. {
  507.         // inverted lines
  508.         int k0 = min(sel_x, sel_end_x);
  509.         int k1 = max(sel_x, sel_end_x);
  510.         int n0 = min(sel_y, sel_end_y);
  511.         int n1 = max(sel_y, sel_end_y);
  512.  
  513.         DWORD x0 = cell_x[k0] - 1;
  514.         DWORD x1 = cell_x[k1] + cell_w[k1] + 1;
  515.         DWORD y0 = cell_y[n0] - 1;     
  516.         DWORD y1 = cell_y[n1] + cell_h[n1] + 1;
  517.         if (x0 > grid.w - 1) x0 = grid.w - 1;
  518.         if (x1 > grid.w - 1) x1 = grid.w - 1;
  519.         if (y0 > grid.h - 1) y0 = grid.h - 1;
  520.         if (y1 > grid.h - 1) y1 = grid.h - 1;
  521.  
  522.         //sprintf(debuf,"drag %U %U %U %U",k0,k1,n0,n1);
  523.         //rtlDebugOutString(debuf);
  524.  
  525.         kos_DrawLine(x0, y0, x0, y1, DCOLOR, DINVERT);
  526.         kos_DrawLine(x0, y0, x1, y0, DCOLOR, DINVERT);
  527.         kos_DrawLine(x1, y0, x1, y1, DCOLOR, DINVERT);
  528.         kos_DrawLine(x0, y1, x1, y1, DCOLOR, DINVERT);
  529. }
  530.  
  531. void draw_window()
  532. {
  533.         kos_WindowRedrawStatus(1);
  534.         kos_DefineAndDrawWindow(110,40,WND_W,WND_H,0x73,0x40FFFFFF,0,0,(Dword)"Table v" TABLE_VERSION);
  535.         kos_WindowRedrawStatus(2);
  536.  
  537.         kos_GetSystemColors(&sc);
  538.  
  539.         sProcessInfo info;
  540.         kos_ProcessInfo(&info, 0xFFFFFFFF);
  541.         cWidth = info.processInfo.width - 9;
  542.         cHeight = info.processInfo.height - kos_GetSkinHeight() - 4;
  543.  
  544.         grid.x = 0;
  545.         grid.y = 0;
  546.         grid.w = cWidth - SCROLL_SIZE - 1;
  547.         grid.h = cHeight - MENU_PANEL_HEIGHT - SCROLL_SIZE;
  548.  
  549.         if (info.processInfo.status_window&0x04) return; //draw nothing if window is rolled-up
  550.  
  551.         if (cWidth  < 430) { kos_ChangeWindow( -1, -1, 450, -1 ); return; }
  552.         if (cHeight < 250) { kos_ChangeWindow( -1, -1, -1, 300 ); return; }
  553.  
  554.         sel_moved = 0;
  555.         if (is_edit) stop_edit();
  556.  
  557.         int panel_y = cHeight - MENU_PANEL_HEIGHT + 1;
  558.         kos_DrawBar(0, panel_y, cWidth, MENU_PANEL_HEIGHT-1, sc.work);
  559.         kos_WriteTextToWindow(3 + 1, panel_y + 14, 0x90, sc.work_text, (char*)sFilename, 0);   
  560.         file_box.top = panel_y + 10;
  561.         #define BTX 230
  562.         #define BTW 70
  563.         //save
  564.         kos_DefineButton(BTX + 25, file_box.top, BTW, 21, SAVE_BUTTON, sc.work);
  565.         kos_WriteTextToWindow(BTX + 25 + (BTW - strlen(sSave) * 8) / 2, panel_y + 14, 0x90, sc.work_text, (char*)sSave, 0);
  566.         //load
  567.         kos_DefineButton(BTX + 25+BTW+5, file_box.top, BTW, 21, LOAD_BUTTON, sc.work);
  568.         kos_WriteTextToWindow(BTX + 25+BTW+5 + (BTW - strlen(sLoad) * 8) / 2, panel_y + 14, 0x90, sc.work_text, (char*)sLoad, 0);
  569.         // // new (clean)
  570.         // kos_DefineButton(90 + 160 + 70, panel_y + 9, 60, 20, NEW_BUTTON, sc.work);
  571.         // kos_WriteTextToWindow(92 + 160 + 10 + 70, panel_y + 16, 0, sc.work_text, (char*)sNew, strlen(sNew));
  572.  
  573.         if (sel_end_move) sel_moved = 0;
  574.         draw_grid();
  575.         sel_moved = 0;
  576.  
  577.         if (is_edit) edit_box_draw((DWORD)&cell_box);
  578.         edit_box_draw((DWORD)&file_box);
  579. }
  580.  
  581. void process_mouse()
  582. {
  583.         Dword mouse_btn, ckeys, shift, ctrl;
  584.  
  585.         int vert, hor;
  586.         kos_GetScrollInfo(vert, hor);  
  587.         if (vert != 0)
  588.         {
  589.                 stop_edit();
  590.                 grid.firsty += vert;
  591.                 if (grid.firsty<1) grid.firsty=1;
  592.                 if (grid.firsty>row_count-25) grid.firsty=row_count-25;
  593.                 draw_grid();
  594.                 return;
  595.         }
  596.  
  597.         if (!size_state) //do not handle scrollbars when user selects cells
  598.         {
  599.                 if (!scroll_h.delta2) scrollbar_v_mouse((DWORD)&scroll_v);
  600.                 if (scroll_v.position != grid.firsty-1)
  601.                 {
  602.                         stop_edit();
  603.                         grid.firsty = scroll_v.position + 1;
  604.                         draw_grid();
  605.                 }
  606.  
  607.                 if (!scroll_v.delta2) scrollbar_h_mouse((DWORD)&scroll_h);
  608.                 if (scroll_h.position != grid.firstx-1)
  609.                 {
  610.                         stop_edit();
  611.                         grid.firstx = scroll_h.position + 1;
  612.                         draw_grid();
  613.                 }
  614.         }
  615.         if (scroll_v.delta2 || scroll_h.delta2) return;
  616.  
  617.         if (is_edit) edit_box_mouse((dword)&cell_box);
  618.         edit_box_mouse((dword)&file_box);
  619.  
  620.         int mouse_x, mouse_y, i;
  621.         kos_GetMouseState(mouse_btn, mouse_x, mouse_y);
  622.         mouse_x -= 5;
  623.         mouse_y -= kos_GetSkinHeight();
  624.  
  625.         if (is_edit && mouse_x>=cell_box.left && mouse_x<=cell_box.left+cell_box.width
  626.                 && mouse_y>=cell_box.top && mouse_y<=cell_box.top+22) return;
  627.  
  628.         mouse_btn &= 0x0001;
  629.  
  630.         if (mouse_btn)
  631.         {
  632.                 if (mouse_y < 0) return; // do nothing if mouse over header
  633.                 if (mouse_y > grid.y + grid.h) return;
  634.         }
  635.  
  636.         ckeys = kos_GetSpecialKeyState();
  637.         shift = ckeys & 0x3;
  638.  
  639.         if (!size_state && !mouse_btn)
  640.                 return;
  641.         if (mouse_btn && !size_state)           // LMB down                            
  642.         {
  643.                 //rtlDebugOutString("lmb down and not resize");
  644.  
  645.                 if (mouse_x >= drag_x && mouse_x <= drag_x + 4 && mouse_y >= drag_y && mouse_y <= drag_y + 4)
  646.                 {
  647.                         size_state = SIZE_DRAG;
  648.                         old_end_x = sel_end_x;
  649.                         old_end_y = sel_end_y;
  650.                 }
  651.                 else if (mouse_y <= cell_h[0])
  652.                 {
  653.                         //rtlDebugOutString("can resize col_count");
  654.                         int kx = -1, i;
  655.                         for (i = 0; i < col_count - 1; i++)
  656.                         if (mouse_x >= cell_x[i] + cell_w[i] - 5 &&
  657.                                 mouse_x <= cell_x[i + 1] + 5)
  658.                         {
  659.                                 kx = i; break;
  660.                         }
  661.                         if (kx != -1)
  662.                         {
  663.                                 //sprintf(debuf,"size x %U",k);
  664.                                 //rtlDebugOutString(debuf);
  665.                                 size_id = kx;
  666.                                 size_state = SIZE_X;
  667.                         }
  668.                 }
  669.                 else if (mouse_x <= cell_w[0])
  670.                 {
  671.                         int ky = -1;
  672.                         for (i = 0; i < row_count - 1; i++)
  673.                         if (mouse_y >= cell_y[i] + cell_h[i] - 5 &&
  674.                                 mouse_y <= cell_y[i + 1] + 5)
  675.                         {
  676.                                 ky = i; break;
  677.                         }
  678.                         if (ky != -1)
  679.                         {
  680.                                 size_id = ky;
  681.                                 size_state = SIZE_Y;
  682.                         }
  683.                 }
  684.                 else   // click on cell
  685.                 if (mouse_x <= cell_x[nx - 1] &&  mouse_y <= cell_y[ny - 1])
  686.                 {
  687.                         was_single_selection = sel_x == sel_end_x && sel_y == sel_end_y;
  688.                         int kx = -1, i;
  689.                         for (i = 0; i < col_count - 1; i++)
  690.                         if (mouse_x >= cell_x[i] &&
  691.                                 mouse_x <= cell_x[i] + cell_w[i])
  692.                         {
  693.                                 kx = i; break;
  694.                         }
  695.                         int ky = -1;
  696.                         for (i = 0; i < row_count - 1; i++)
  697.                         if (mouse_y >= cell_y[i] &&
  698.                                 mouse_y <= cell_y[i] + cell_h[i])
  699.                         {
  700.                                 ky = i; break;
  701.                         }
  702.                         if (kx != -1 && ky != -1)
  703.                         {
  704.                                 if (!shift)
  705.                                 {
  706.                                         move_selection(kx, ky);
  707.                                         //return;
  708.                                 }
  709.                                 else
  710.                                 {
  711.                                         sel_end_x = kx;
  712.                                         sel_end_y = ky;
  713.                                 }
  714.                                 size_state = SIZE_SELECT;
  715.                         }
  716.                 }
  717.                 if (size_state)
  718.                 {
  719.                         size_mouse_x = mouse_x;
  720.                         size_mouse_y = mouse_y;
  721.                 }
  722.                 return;
  723.         }
  724.         else if (!mouse_btn && size_state)
  725.         {
  726.                 sel_moved = 0;          // for a good redraw
  727.                 //rtlDebugOutString("resize end");
  728.  
  729.                 if (size_state == SIZE_DRAG)
  730.                 {
  731.                         fill_cells(sel_x, sel_y, sel_end_x, sel_end_y, old_end_x, old_end_y);
  732.                 }
  733.  
  734.                 //sel_moved = (size_state == SIZE_SELECT && sel_x == sel_end_x && sel_y == sel_end_y && was_single_selection);
  735.                 size_state = 0;
  736.                 draw_grid();            // все сдвинулось - надо обновиться
  737.                 return;
  738.         }
  739.         if (size_state == SIZE_X && mouse_x != size_mouse_x)
  740.         {
  741.                 draw_size_grid();
  742.                 cell_w[size_id] += mouse_x - size_mouse_x;
  743.                 if (cell_w[size_id] < 15)
  744.                         cell_w[size_id] = 15;
  745.                 else if (cell_w[size_id] > grid.w / 2)
  746.                         cell_w[size_id] = grid.w / 2;
  747.                 draw_size_grid();
  748.         }
  749.         if (size_state == SIZE_Y && mouse_y != size_mouse_y)
  750.         {
  751.                 draw_size_grid();
  752.                 cell_h[size_id] += mouse_y - size_mouse_y;
  753.                 if (cell_h[size_id] < 15)
  754.                         cell_h[size_id] = 15;
  755.                 else if (cell_h[size_id] > grid.h / 2)
  756.                         cell_h[size_id] = grid.h / 2;
  757.                 draw_size_grid();
  758.         }
  759.         if ((size_state == SIZE_SELECT || size_state == SIZE_DRAG) && (mouse_x != size_mouse_x || mouse_y != size_mouse_y))
  760.         {
  761.                 draw_drag();
  762.                 int kx = -1, i;
  763.                 for (i = 0; i < col_count - 1; i++)
  764.                         if (mouse_x >= cell_x[i] &&
  765.                                 mouse_x <= cell_x[i + 1])
  766.                         {
  767.                                 //sprintf(debuf, "yyy %U",cell_x[i+1]);
  768.                                 //rtlDebugOutString(debuf);
  769.                                 kx = i; break;
  770.                         }
  771.                 int ky = -1;
  772.                 for (i = 0; i < row_count - 1; i++)
  773.                         if (mouse_y >= cell_y[i] &&
  774.                                 mouse_y <= cell_y[i + 1])
  775.                         {
  776.                                 ky = i; break;
  777.                         }
  778.                 if (kx != -1) sel_end_x = kx;
  779.                 if (ky != -1) sel_end_y = ky;
  780.                 if (size_state == SIZE_DRAG)
  781.                 {
  782.                         if (abs(sel_end_x - sel_x) > 0)
  783.                         {
  784.                                 sel_end_y = old_end_y;
  785.                         }
  786.                         else if (abs(sel_end_y - sel_y) > 0)
  787.                         {
  788.                                 sel_end_x = old_end_x;
  789.                         }
  790.                 }
  791.                 draw_drag();
  792.         }        
  793.         size_mouse_x = mouse_x;
  794.         size_mouse_y = mouse_y;
  795. }
  796.  
  797.  
  798. void shift_selection(int dx, int dy, Dword shift)
  799. {
  800.         if (dx != 0)
  801.         {
  802.                 if (shift)
  803.                 {
  804.                         sel_end_x += dx;
  805.                         if (sel_end_x <= 1)
  806.                                 sel_end_x = 1;
  807.                         else if (sel_end_x >= col_count)
  808.                                 sel_end_x = col_count - 1;
  809.                 //      sprintf(debuf,"sel end x change. sel end %U %U",sel_end_x,sel_end_y);
  810.                 //      rtlDebugOutString(debuf);
  811.                         sel_moved = sel_end_move = 1;
  812.                         //stop_edit();
  813.                         //draw_grid();
  814.                 }
  815.                 else
  816.                 {
  817.                 }
  818.         }
  819.         if (dy != 0)
  820.         {
  821.                 if (shift)
  822.                 {
  823.                         sel_end_y += dy;
  824.                         if (sel_end_y <= 1)
  825.                                 sel_end_y = 1;
  826.                         else if (sel_end_y >= row_count)
  827.                                 sel_end_y = row_count - 1;
  828.                 //      sprintf(debuf,"sel end y change. sel end %U %U",sel_end_x,sel_end_y);
  829.                 //      rtlDebugOutString(debuf);
  830.                         sel_moved = sel_end_move = 1;
  831.                         //stop_edit();
  832.                         //draw_grid();
  833.                 }
  834.         }
  835.         /*
  836.         if (sel_end_x < sel_x)
  837.         {
  838.                 Dword tmp = sel_end_x; sel_end_x = sel_x; sel_x = tmp;
  839.         }
  840.         if (sel_end_y < sel_y)
  841.         {
  842.                 Dword tmp = sel_end_y; sel_end_y = sel_y; sel_y = tmp;
  843.         }
  844.         */
  845.         if ((dx || dy))
  846.         {
  847.                 if (!shift)
  848.                 {
  849.                         if ((sel_end_x + dx) >= (col_count-1)) {dx=0;} //stub
  850.                         else if ((sel_end_y + dy) >= (row_count-1)) {dy=0;}
  851.                         else {
  852.                         move_selection(sel_x + dx, sel_y + dy);
  853.                         }
  854.                 }
  855.                 else
  856.                 {
  857.                         sel_moved = 0;
  858.                         stop_edit();
  859.                         draw_grid();
  860.                 }
  861.         }
  862. }
  863.  
  864.  
  865. void process_key()
  866. {
  867.         Dword ckeys, shift, ctrl;
  868.         dword key_editbox;
  869.         Byte key_ascii, key_scancode;
  870.  
  871.         // key pressed, read it
  872.         ckeys = kos_GetSpecialKeyState();
  873.         shift = ckeys & 0x3;
  874.         ctrl = ckeys & 0x0c;
  875.         sel_moved = 0;
  876.         sel_end_move = 0;
  877.        
  878.         kos_GetKeys(key_editbox, key_ascii, key_scancode);
  879.  
  880.         if (cell_box.flags & ed_focus) {
  881.                 if (SCAN_CODE_ENTER == key_scancode) {
  882.                         stop_edit();
  883.                         draw_grid();
  884.                 }
  885.                 else if (SCAN_CODE_ESC == key_scancode) {
  886.                         cancel_edit();
  887.                 }
  888.                 else {
  889.                         __asm
  890.                         {
  891.                                 mov eax, key_editbox
  892.                         }
  893.                         edit_box_key((dword)&cell_box);                
  894.                 }
  895.         }
  896.         else if (file_box.flags & ed_focus) {
  897.                 __asm
  898.                 {
  899.                         mov eax, key_editbox
  900.                 }
  901.                 edit_box_key((dword)&file_box);
  902.                 return;
  903.         }
  904.         else if (ctrl) {
  905.                 switch (key_scancode)
  906.                 {
  907.                         case SCAN_CODE_KEY_A:
  908.                                 EventGridSelectAll();
  909.                                 break;
  910.                         case SCAN_CODE_KEY_V:
  911.                                 {
  912.                                         int i, j, x0, y0;
  913.                                         x0 = min(sel_x, sel_end_x);
  914.                                         y0 = min(sel_y, sel_end_y);
  915.                                         int delta_x = x0 - buf_old_x;
  916.                                         int delta_y = y0 - buf_old_y;
  917.  
  918.                                         for (i = 0; i < buf_col; i++)
  919.                                                 for (j = 0; j < buf_row; j++)
  920.                                                 {
  921.                                                         if (i + x0 >= col_count || j + y0 >= row_count)
  922.                                                                 continue;
  923.                                                         if (cells[i + x0][j + y0])
  924.                                                                 freemem(cells[i + x0][j + y0]);
  925.                                                         if (buffer[i][j])
  926.                                                         {
  927.                                                                 cf_x0 = buf_old_x; cf_y0 = buf_old_y;
  928.                                                                 cf_x1 = buf_old_x + buf_col;
  929.                                                                 cf_y1 = buf_old_y + buf_row;
  930.                                                                 cells[i + x0][j + y0] = change_formula(buffer[i][j], delta_x, delta_y);
  931.                                                                 //cells[i + x0][j + y0] = (char*)allocmem(strlen(buffer[i][j]));
  932.                                                                 //strcpy(cells[i + x0][j + y0], buffer[i][j]);
  933.                                                         }
  934.                                                         else
  935.                                                                 cells[i + x0][j + y0] = NULL;
  936.                                                 }
  937.  
  938.                                         calculate_values();
  939.                                         draw_grid();
  940.                                         break;
  941.                                 }
  942.                                 case SCAN_CODE_KEY_X:
  943.                                 case SCAN_CODE_KEY_C:
  944.                                 {
  945.                                         int i, j, x0, y0;
  946.  
  947.                                         freeBuffer();
  948.  
  949.                                         buf_col = abs(sel_end_x - sel_x) + 1;
  950.                                         buf_row = abs(sel_end_y - sel_y) + 1;
  951.                                         x0 = min(sel_x, sel_end_x);
  952.                                         y0 = min(sel_y, sel_end_y);
  953.                                         buf_old_x = x0;
  954.                                         buf_old_y = y0;
  955.  
  956.                                         //sprintf(debuf, "%U %U %U %U", buf_col, buf_row, x0, y0);
  957.                                         //rtlDebugOutString(debuf);
  958.                                
  959.                                         buffer = (char***)allocmem(buf_col * sizeof(char**));
  960.                                         for (i = 0; i < buf_col; i++)
  961.                                         {
  962.                                                 buffer[i] = (char**)allocmem(buf_row * sizeof(char*));
  963.                                                 for (j = 0; j < buf_row; j++)
  964.                                                 {
  965.                                                         if (cells[i + x0][j + y0])
  966.                                                         {
  967.                                                                 if (SCAN_CODE_KEY_C == key_scancode)
  968.                                                                 {
  969.                                                                         buffer[i][j] = (char*)allocmem(strlen(cells[i + x0][j + y0]));
  970.                                                                         strcpy(buffer[i][j], cells[i + x0][j + y0]);
  971.                                                                 }
  972.                                                                 else
  973.                                                                 {
  974.                                                                         buffer[i][j] = cells[i + x0][j + y0];
  975.                                                                         cells[i + x0][j + y0] = NULL;
  976.                                                                 }
  977.                                                         }
  978.                                                         else
  979.                                                                 buffer[i][j] = NULL;
  980.                                                 }
  981.                                         }
  982.                                         if (key_ascii == 24)     ///////WTF????
  983.                                                 calculate_values();
  984.                                         draw_grid();
  985.                                         break;
  986.                                 }
  987.                         case SCAN_CODE_KEY_F:
  988.                                 display_formulas = !display_formulas;
  989.                                 draw_grid();
  990.                                 break;
  991.                 }
  992.         }
  993.         else switch (key_scancode)
  994.         {
  995.                 case SCAN_CODE_UP:
  996.                         shift_selection(0, -1, shift);
  997.                         break;
  998.                 case SCAN_CODE_LEFT:
  999.                         shift_selection(-1, 0, shift);
  1000.                         break;
  1001.                 case SCAN_CODE_RIGHT:
  1002.                         shift_selection(1, 0, shift);
  1003.                         break;
  1004.                 case SCAN_CODE_DOWN:
  1005.                         shift_selection(0, 1, shift);
  1006.                         break;
  1007.                 case SCAN_CODE_PGDN:
  1008.                         shift_selection(0, ny-grid.firsty-1, shift);
  1009.                         break;
  1010.                 case SCAN_CODE_PGUP:
  1011.                         shift_selection(0, -(ny-grid.firsty), shift);
  1012.                         break;
  1013.                 case SCAN_CODE_HOME:
  1014.                         shift_selection(-sel_x + 1, 0, shift);
  1015.                         break;
  1016.                 case SCAN_CODE_END:
  1017.                         shift_selection(col_count - (nx - grid.firstx) - 1 - sel_x, 0, shift);
  1018.                         break;
  1019.                 case SCAN_CODE_DEL:
  1020.                         {
  1021.                                 int n0 = min(sel_x, sel_end_x);
  1022.                                 int n1 = max(sel_x, sel_end_x);
  1023.                                 int k0 = min(sel_y, sel_end_y);
  1024.                                 int k1 = max(sel_y, sel_end_y);
  1025.  
  1026.                                 for (int i = n0; i <= n1; i++)
  1027.                                         for (int j = k0; j <= k1; j++)
  1028.                                         {
  1029.                                                 if (cells[i][j])
  1030.                                                 {
  1031.                                                         freemem(cells[i][j]);
  1032.                                                         cells[i][j] = NULL;
  1033.                                                 }
  1034.                                         }
  1035.                                 calculate_values();
  1036.                                 draw_grid();
  1037.                                 break;
  1038.                         }
  1039.                         break;
  1040.                 case SCAN_CODE_F2:
  1041.                         start_edit(sel_x, sel_y);
  1042.                         break;
  1043.                 case SCAN_CODE_F5:
  1044.                         draw_grid();
  1045.                         break;
  1046.                 default:
  1047.                         start_edit(sel_x, sel_y);
  1048.                         __asm
  1049.                         {
  1050.                                 mov eax, key_editbox
  1051.                         }
  1052.                         edit_box_key((dword)&cell_box);
  1053.                         break;
  1054.         }
  1055. }
  1056.  
  1057. void EventLoadFile()
  1058. {
  1059.         stop_edit();
  1060.         int r = LoadFile(fname);
  1061.         char *result;
  1062.         if (r > 0) {
  1063.                 calculate_values();
  1064.                 sel_moved = 0;
  1065.                 draw_grid();
  1066.                 result = (char*)msg_load;
  1067.         }
  1068.         else if (r == -1) result = (char*)er_file_not_found;
  1069.         else if (r == -2) result = (char*)er_format;
  1070.         kos_AppRun("/sys/@notify", result);
  1071. }
  1072.  
  1073. void EventGridSelectAll()
  1074. {
  1075.         sel_y = 1;
  1076.         sel_x = 1;
  1077.         sel_end_x = col_count - 1;
  1078.         sel_end_y = row_count - 1;
  1079.         stop_edit();
  1080.         draw_grid();
  1081. }
  1082.  
  1083. void process_button()
  1084. {
  1085.         Dword button;
  1086.         if (!kos_GetButtonID(button)) return;
  1087.         switch (button)
  1088.         {
  1089.         case 1:
  1090.                 kos_ExitApp();
  1091.  
  1092.         case NEW_BUTTON:        // clear the table
  1093.                 reinit();
  1094.                 draw_grid();
  1095.                 break;
  1096.  
  1097.         case SAVE_BUTTON:
  1098.                 stop_edit();
  1099.                 if (SaveFile(fname)) {
  1100.                         kos_AppRun("/sys/@notify", (char*)msg_save);
  1101.                 }
  1102.                 else {
  1103.                         kos_AppRun("/sys/@notify", (char*)msg_save_error);
  1104.                 }
  1105.                 break;
  1106.  
  1107.         case LOAD_BUTTON:
  1108.                 EventLoadFile();
  1109.                 break;
  1110.  
  1111.         case SELECT_ALL_BUTTON:
  1112.                 EventGridSelectAll();
  1113.                 break;
  1114.         }
  1115.         if (button >= COL_HEAD_BUTTON    &&    button < ROW_HEAD_BUTTON)
  1116.         {
  1117.                 sel_end_x = sel_x = button - COL_HEAD_BUTTON;
  1118.                 sel_y = 1;
  1119.                 sel_end_y = row_count - 1;
  1120.                 stop_edit();
  1121.                 draw_grid();
  1122.                 return;
  1123.         }
  1124.         else if (button >= ROW_HEAD_BUTTON    &&    button < CELL_BUTTON)
  1125.         {
  1126.                 sel_end_y = sel_y = button - ROW_HEAD_BUTTON;
  1127.                 sel_x = 1;
  1128.                 sel_end_x = col_count - 1;
  1129.                 stop_edit();
  1130.                 draw_grid();
  1131.                 return;
  1132.         }
  1133. }
  1134.  
  1135. void kos_Main()
  1136. {
  1137.         kos_InitHeap();
  1138.         load_edit_box();
  1139.         init();
  1140.         if (params[0]) {
  1141.                 strcpy(fname, params);
  1142.                 file_box.size = file_box.pos = strlen(fname);
  1143.                 EventLoadFile();
  1144.         }
  1145.         kos_SetMaskForEvents(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER);
  1146.         for (;;)
  1147.         {
  1148.                 switch (kos_WaitForEvent())
  1149.                 {
  1150.                 case EM_MOUSE_EVENT:
  1151.                         process_mouse();
  1152.                         break;
  1153.  
  1154.                 case EM_KEY_PRESS:
  1155.                         process_key();
  1156.                         break;
  1157.  
  1158.                 case EM_BUTTON_CLICK:
  1159.                         process_button();
  1160.                         break;
  1161.                
  1162.                 case EM_WINDOW_REDRAW:
  1163.                         draw_window();
  1164.                         break;
  1165.                 }
  1166.         }
  1167. }
  1168.  
  1169.