Subversion Repositories Kolibri OS

Rev

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