Subversion Repositories Kolibri OS

Rev

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