Subversion Repositories Kolibri OS

Rev

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