Subversion Repositories Kolibri OS

Rev

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

  1.  
  2. #include "func.h"
  3. #include "parser.h"
  4. #include "calc.h"
  5. #include "use_library.h"
  6. ///#include "use_library.h"
  7. //const char header[] = "Table";
  8.  
  9. #define TABLE_VERSION "0.95"
  10.  
  11. // ñòðîêè, êîòîðûå âûâîäèò ïðîãðàììà
  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. ";
  19. const char er_format[] = "Error: bad format. ";
  20. const char msg_save[] = "File saved. ";
  21. const char msg_load[] = "File loaded. ";
  22. const char msg_new[] = "Memory cleared. ";
  23.  
  24. // ñâîé PID
  25. Dword myPID = -1;
  26.  
  27. // íà÷àëüíûå ðàçìåðû
  28. #define WND_W 550
  29. #define WND_H 400
  30. // íîâûå ðàçìåðû è êîîðäèíàòû
  31. int wi = WND_W, he = WND_H;
  32. int win_x, win_y;
  33.  
  34. // öâåòà ýëåìåíòîâ èíòåðôåéñà
  35. #define GRID_COLOR 0xa0a0a0
  36. #define TEXT_COLOR 0x000000
  37. #define CELL_COLOR 0xffffff
  38. #define SEL_CELL_COLOR 0xe0e0ff
  39. #define FIXED_CELL_COLOR 0xe0e0ff
  40. #define SEL_FIXED_CELL_COLOR 0x758FC1
  41. #define TEXT_SEL_FIXED_COLOR 0xffffff
  42.  
  43. #define SCROLL_BAR_WIDTH 16
  44. #define SCROLL_BAR_HEIGHT 16
  45.  
  46. // ID êíîïîê
  47. #define FILENAME_BUTTON 0x10
  48. #define SAVE_BUTTON 0x11
  49. #define LOAD_BUTTON 0x12
  50. #define NEW_BUTTON 0x13
  51. #define DRAG_BUTTON 0x20
  52.  
  53. #define SCROLL_LEFT_BUTTON 0x21
  54. #define SCROLL_RIGHT_BUTTON 0x22
  55. #define SCROLL_UP_BUTTON 0x23
  56. #define SCROLL_DOWN_BUTTON 0x24
  57. #define SCROLL_WIDTH 0x25
  58. #define SCROLL_HEIGHT 0x26
  59.  
  60. #define COL_BUTTON 0x100
  61. #define ROW_BUTTON (COL_BUTTON + 0x100)
  62. #define COL_HEAD_BUTTON (ROW_BUTTON + 0x100)
  63. #define ROW_HEAD_BUTTON (COL_HEAD_BUTTON + 0x100)
  64. #define CELL_BUTTON (ROW_HEAD_BUTTON + 0x100)
  65.  
  66.  
  67. // íèæíÿÿ ïàíåëü ñ êíîïêàìè è ïîëåì ââîäà
  68. #define MENU_PANEL_HEIGHT 40
  69. Dword panel_y = 0;
  70.  
  71. // äëÿ ïîëÿ ââîäà
  72. char edit_text[256] = "";
  73. edit_box cell_box = {0,9*8-5,WND_H - 16-32,0xffffff,0x6a9480,0,0x808080,0,255,(dword)&edit_text,0};
  74.  
  75. // ÿ÷åéêè - èõ ïàðàìåòðû è òåêñò
  76. DWORD def_col_width = 80, def_row_height = 16;
  77. DWORD col_count = 200, row_count = 100;
  78. DWORD *col_width, *row_height;
  79. char ***cells;
  80. char ***values; // çíà÷åíèÿ ôîðìóë, åñëè åñòü
  81.  
  82. bool display_formulas = 0;      // îòîáðàæàòü ëè ôîðìóëû âìåñòî çíà÷åíèé
  83.  
  84. // êîîðäèíàòû îòîáðàæàåìûõ ñòîëáöîâ è ñòðîê
  85. DWORD *col_left, *row_top;
  86.  
  87. // áóôåð îáìåíà
  88. char ***buffer = NULL;
  89. DWORD buf_col, buf_row;
  90. DWORD buf_old_x, buf_old_y;
  91.  
  92. // ýòî êîîðäèíàòû ÿ÷åéêè, îòîáðàæàåìîé â Ë óãëå
  93. DWORD scroll_x = 1, scroll_y = 1;
  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. //bool is_edit = 0;
  110. #define ed_focus 2
  111. #define is_edit (cell_box.flags & ed_focus)
  112.  
  113. // ðåäàêòèðîâàíèå èìåíè ôàéëà
  114. bool fn_edit = 0;
  115. char fname[256];
  116. edit_box file_box = {0,9*8-5,WND_H - 16-32,0xffffff,0x6a9480,0,0x808080,0,255,(dword)&fname,0};
  117.  
  118. // èçìåíåíèå ðàçìåðîâ
  119. #define SIZE_X 1 // ñîñòîÿíèå
  120. #define SIZE_Y 2
  121. #define SIZE_SELECT 3
  122. #define SIZE_DRAG 4
  123. int size_mouse_x, size_mouse_y, size_id, size_state = 0;
  124.  
  125. int window_is_dragged = 0; // ñåé÷àñ ïåðåòàñêèâàåòñÿ îêíî
  126.  
  127. // ðàñòàñêèâàíèå ÿ÷åéêè ïðè åå òàùåíèè çà ïðàâûé íèæíèé óãîë, ñ çàïîëíåíèåì ÿ÷ååê
  128. int drag_x, drag_y;
  129. int old_end_x, old_end_y;
  130.  
  131. void draw_window();
  132.  
  133. //edit_box ebox = {250,14,35,0xffffff,0x6f9480,0,0xAABBCC,0,248,0,2,20,20};
  134.  
  135. void kos_DrawRegion(Word x, Word y,Word width, Word height, Dword color1, Word invert)
  136. {
  137.         kos_DrawLine(x,y,x+width-2,y,color1,invert);
  138.         kos_DrawLine(x,y+1,x,y+height-1,color1,invert);
  139.         kos_DrawLine(x+width-1,y,x+width-1,y+height-2,color1,invert);
  140.         kos_DrawLine(x+1,y+height-1,x+width-1,y+height-1,color1,invert);
  141. }
  142.  
  143. void start_edit(int x, int y)
  144. {
  145.  
  146.         int ch = 0;
  147.         if (x < scroll_x || x > nx - 1)
  148.         {
  149.                 scroll_x = x;
  150.                 ch = 1;
  151.         }
  152.         if (y < scroll_y || y > ny - 1)
  153.         {
  154.                 scroll_y = y;
  155.                 ch = 1;
  156.         }
  157.         if (ch)
  158.         {
  159.                 sel_moved = 1;
  160.                 draw_window();
  161.         }
  162.  
  163.         file_box.flags &= ~ed_focus;
  164.  
  165.         cell_box.flags |= ed_focus;
  166.         cell_box.left = col_left[x] + 1;
  167.         cell_box.top = row_top[y] + 1;
  168.         cell_box.width = col_width[x] - 2;
  169.         //cell_box.height= row_height[y];
  170.         memset((Byte*)edit_text, 0, sizeof(edit_text));
  171.         if (cells[x][y])
  172.         {
  173.                 strcpy(edit_text, cells[x][y]);
  174.                 edit_text[strlen(cells[x][y]) - 1] = '\0';
  175.         }
  176.         cell_box.pos = cell_box.offset = 0;
  177.  
  178.         draw_window();
  179. }
  180.  
  181. void stop_edit()
  182. {
  183.         if (is_edit)
  184.         {
  185.                 cell_box.flags &= ~ed_focus;
  186.                 if (cells[sel_x][sel_y])
  187.                         freemem(cells[sel_x][sel_y]);
  188.                 if (strlen(edit_text) > 0)
  189.                 {
  190.                         cells[sel_x][sel_y] = (char*)allocmem(strlen(edit_text)+1);
  191.                         strcpy(cells[sel_x][sel_y], edit_text);
  192.                 }
  193.                 else
  194.                         cells[sel_x][sel_y] = NULL;
  195.                 //memset((Byte*)edit_text,0, 256);
  196.                 calculate_values();
  197.         }
  198. }
  199.  
  200. void cancel_edit()
  201. {
  202.         if (!is_edit)
  203.                 return;
  204.         cell_box.flags &= ~ed_focus;
  205.         memset((Byte*)edit_text,0, 256);
  206.         draw_window();
  207. }
  208.  
  209. void check_sel()
  210. {
  211.         DWORD sx0=scroll_x, sy0=scroll_y;
  212.  
  213.         if (sel_x >= nx - 1  /*&& sel_x < col_count - nx + scroll_x + 1*/)
  214.                 //if (sel_x == nx)
  215.                         scroll_x++;
  216.                 //else
  217.                 //      scroll_x = sel_x;
  218.         if (sel_y >= ny - 1 /*&& sel_y < row_count - ny + scroll_y */)
  219.                 //if (sel_y == ny)
  220.                         scroll_y++;
  221.                 //else
  222.                 //      scroll_y = sel_y;
  223.  
  224.         if (sel_x < scroll_x)
  225.                 scroll_x = sel_x;
  226.         if (sel_y < scroll_y)
  227.                 scroll_y = sel_y;
  228.  
  229.         if (sx0 != scroll_x || sy0 != scroll_y)
  230.                 sel_moved = 0;                  // íàäî ïåðåðèñîâàòü âñå
  231.  
  232. }
  233.  
  234. // ñäâèíóòü âûäåëåíèå
  235. void move_sel(DWORD new_x, DWORD new_y)
  236. {
  237.         sel_moved = 1;
  238.         stop_edit();
  239.         prev_x = sel_x;
  240.         prev_y = sel_y;
  241.         sel_x = new_x;
  242.         if (sel_x < 1)
  243.                 sel_x = 1;
  244.         if (sel_x > col_count - 1)
  245.                 sel_x = col_count - 1;
  246.         sel_end_x = sel_x;
  247.         sel_y = new_y;
  248.         if (sel_y < 1)
  249.                 sel_y = 1;
  250.         if (sel_y > row_count - 1)
  251.                 sel_y = row_count - 1;
  252.         sel_end_y = sel_y;
  253.         check_sel();
  254.         draw_window();
  255. }
  256.  
  257. void draw_custom_button(int x0, int y0, int sx, int sy, int blue_border)
  258. {
  259.         int x1 = x0 + sx;
  260.         int y1 = y0 + sy;
  261.  
  262.         if (blue_border) kos_DrawRegion(x0-1, y0-1, sx+3, sy+3, 0x94aece, 0);
  263.  
  264.         // ñåðûé ïðÿìîóãîëüíèê
  265.  
  266.         kos_DrawBar(x0 + 1, y0 + 1, sx - 1, sy - 1, 0xe4dfe1);
  267.        
  268.         // äâå áåëûå ëèíèè: ñâåðõó è ñëåâà
  269.  
  270.         kos_DrawLine(x0, y0, x1, y0, 0xffffff, 0);
  271.         kos_DrawLine(x0, y0, x0, y1, 0xffffff, 0);
  272.  
  273.         // äâå ñåðûå ëèíèè: ñíèçó è ñïðàâà
  274.         kos_DrawLine(x0, y1, x1, y1, 0xc7c7c7, 0);
  275.         kos_DrawLine(x1, y0, x1, y1, 0xc7c7c7, 0);
  276. }
  277.  
  278. // x - ìåæäó low è high ? - íåîáÿçàòåëüíî low<high
  279. bool is_between(Dword x, Dword low, Dword high)
  280. {
  281.         return ((low<high)?(x >= low && x <= high):(x >= high && x <= low));
  282. }
  283.  
  284. void clear_cell_slow(int px, int py)
  285. {
  286.         int i;
  287.         int x0 = col_width[0];
  288.         for (i = scroll_x; i < px; i++)
  289.         {
  290.                 x0 += col_width[i];
  291.         }
  292.         int x1 = x0;
  293.         x1 += col_width[px];
  294.         int y0 = row_height[0];
  295.         for (i = scroll_y; i < py; i++)
  296.         {
  297.                 y0 += row_height[i];
  298.         }
  299.         int y1 = y0;
  300.         y1 += row_height[py];
  301.         kos_DrawBar(x0 + 1, y0 + 1, x1 - x0 - 1, y1 - y0 - 1, 0xffffff);
  302. }
  303.  
  304. //debug
  305. const int debugcolor[10]={0xff0000,0x00ff00,0x0000ff,0xffff00,0x00ffff,0xff00ff,0x800000,0x008000,0x000080,0x800080};
  306. int debugc=0;
  307.  
  308. // ðèñîâàíèå ÿ÷ååê
  309. #define is_x_changed(v) ((v) == sel_x || (v) == prev_x)
  310. #define is_y_changed(v) ((v) == sel_y || (v) == prev_y)
  311.  
  312. void draw_grid()
  313. {
  314.         int i,j;
  315.         long x0 = 0, y0 = 0, x = 0, y = 0, dx, popravka;
  316.         DWORD text_color;
  317.         //int lx, ly;
  318.  
  319. //      sprintf(debuf, "%U,%U", scroll_x, scroll_y);
  320. //      rtlDebugOutString(debuf);
  321.  
  322.         nx=ny=0;
  323.  
  324.         // î÷èñòèòü îáëàñòü îêîëî âûäåëåííîé ÿ÷åéêè
  325.         if (sel_moved)
  326.         {
  327.                 clear_cell_slow(sel_x, sel_y);
  328.                 clear_cell_slow(prev_x, prev_y);
  329.         }
  330.         else
  331.         {
  332.                 // î÷èñòèòü âñþ îáëàñòü ÿ÷ååê
  333.                 //kos_DrawBar(col_width[0]+1, row_height[0]+1, wi - SCROLL_BAR_WIDTH-col_width[0]-1, he - SCROLL_BAR_HEIGHT-row_height[0]-1, 0xffffff);
  334.         }
  335.  
  336.         col_left[0] = 0;
  337.         // ÿ÷åéêè - çàãîëîâêè ñòîëáöîâ + âåðòèêàëüíûå ëèíèè
  338.         x = col_width[0];
  339.         nx = 1;
  340.         for (i = 1; i < col_count; i++)
  341.         {
  342.                 col_left[i] = -1;
  343.                 if (i >= scroll_x)
  344.                 {
  345.                         {                              
  346.                                 if (!sel_moved || is_x_changed(i))
  347.                                         kos_DrawLine(x-x0, 0, x-x0, row_height[0], GRID_COLOR, 0);
  348.                         // è çàãîëîâîê ÿ÷åéêè ïî õ
  349.                                 text_color = TEXT_COLOR;
  350.                                 dx = (col_width[i]-6)/2;
  351.                                 int dy = (row_height[0] - 8) / 2 + 1;
  352.                                 int cur_width = col_width[i] - 1;
  353.                                 if (cur_width + x - x0 > wi - SCROLL_BAR_WIDTH)
  354.                                         cur_width = wi - SCROLL_BAR_WIDTH - x + x0;
  355.                                 if (!sel_moved || (is_x_changed(i)))
  356.                                         if (is_between(i,sel_x,sel_end_x))     
  357.                                         {
  358.                                                 kos_DrawBar(x - x0 + 1,0,cur_width,row_height[0],SEL_FIXED_CELL_COLOR); //0x0000CC
  359.                                                 text_color = TEXT_SEL_FIXED_COLOR;
  360.                                         }
  361.                                         else
  362.                                         {
  363.                                                 kos_DrawBar(x - x0 + 1,0,cur_width,row_height[0],FIXED_CELL_COLOR);
  364.                                                 text_color = TEXT_COLOR;
  365.                                         }
  366.                                 if (!sel_moved || (is_x_changed(i))) kos_WriteTextToWindow(x-x0+2+dx,dy,0,text_color,cells[i][0],strlen(cells[i][0]));
  367.  
  368.                                 // åñòü êíîïêà ñòîáëöà è åùå êíîïêà èçìåíåíèÿ øèðèíû
  369.                                 if (x - x0 + col_width[i] <= wi - col_width[0])
  370.                                         kos_DefineButton(x-x0+5,0,cur_width - 10,row_height[0]-1,0x60000000+COL_HEAD_BUTTON+i,0);
  371.                                 //kos_DefineButton(x-x0+col_width[i]-10,0,15,row_height[0]-1,0x60000000+COL_SIZE_BUTTON+i,0);
  372.                                 col_left[i] = x - x0;
  373.                         }
  374.                         if (x - x0 > wi - col_width[0])
  375.                         {
  376.                                 x += col_width[i];
  377.                                 nx++;
  378.                                 break;
  379.                         }
  380.                 }
  381.                 else
  382.                 {
  383.                         x0 += col_width[i];
  384.                 }
  385.                 x += col_width[i];
  386.                 nx++;
  387.         }
  388.  
  389.         //kos_DefineButton(0,0,0,0,0x80000000+COL_HEAD_BUTTON+i,0);
  390.  
  391.         for (j = i + 1; j < col_count; j++)
  392.                 col_left[j] = wi;
  393.         //if (!sel_moved || (is_x_changed(nx))) kos_DrawLine(x - x0, 0, x - x0, he, GRID_COLOR, 0);
  394.  
  395.         // ÿ÷åéêè - çàãîëîâêè ñòðîê + ãîðèçîíò. ëèíèè
  396.         y = row_height[0];
  397.         ny = 1;
  398.         row_top[0] = 0;
  399.         for (i = 1; i < row_count && y - y0 < he - 10; i++)
  400.         {
  401.                 row_top[i] = -1;
  402.                 if (i >= scroll_y)
  403.                 {
  404.                         {
  405.                                 if (!sel_moved || (is_y_changed(i)))
  406.                                         kos_DrawLine(0, y - y0, wi - SCROLL_BAR_WIDTH, y - y0, GRID_COLOR, 0);
  407.                                 // è çàãîëîâîê ÿ÷åéêè ïî y
  408.                                 text_color = TEXT_COLOR;
  409.                                 dx = (col_width[0]-6 * strlen(cells[0][i]))/2;  // optimize this, change strlen
  410.                                 int dy = (row_height[i] - 8) / 2 + 1;
  411.                                 if (!sel_moved || (is_y_changed(i)))
  412.                                         if (is_between(i,sel_y,sel_end_y))
  413.                                         {
  414.                                                 kos_DrawBar(0,y-y0+1,col_width[0],row_height[i] - 1,SEL_FIXED_CELL_COLOR);
  415.                                                 text_color = TEXT_SEL_FIXED_COLOR;
  416.                                         }
  417.                                         else
  418.                                         {
  419.                                                 kos_DrawBar(0,y-y0+1,col_width[0],row_height[i] - 1,FIXED_CELL_COLOR);
  420.                                                 text_color = TEXT_COLOR;
  421.                                         }
  422.  
  423.                                 if (!sel_moved || (is_y_changed(i)))
  424.                                         kos_WriteTextToWindow(2+dx,y-y0+dy,0,text_color,cells[0][i],strlen(cells[0][i]));
  425.  
  426.                                 kos_DefineButton(0,y-y0+5,col_width[0]-1,row_height[i]-6,0x60000000+ROW_HEAD_BUTTON+i,0);
  427.                                 //kos_DefineButton(0,y-y0+row_height[i]-5,col_width[0]-1,10,0x60000000+ROW_SIZE_BUTTON+i,0);
  428.                                 row_top[i] = y - y0;
  429.                         }
  430.                 }
  431.                 else
  432.                 {
  433.                         y0 += row_height[i];
  434.                 }
  435.                 y += row_height[i];
  436.                 ny++;
  437.         }
  438.        
  439.         kos_DefineButton(0,0,0,0,0x80000000+ROW_HEAD_BUTTON+ny-1,0);
  440.  
  441.         for (j = i + 1; j < row_count; j++)
  442.                 row_top[j] = he;
  443.         if (!sel_moved || (is_y_changed(ny)))
  444.                 kos_DrawLine(0, y - y0, wi - SCROLL_BAR_WIDTH, y - y0, GRID_COLOR, 0);
  445.  
  446.         if (!sel_moved || (is_x_changed(0) && is_y_changed(0)))
  447.                 kos_DrawBar(0,0,col_width[0],row_height[0],FIXED_CELL_COLOR);
  448.         // Ë ÿ÷åéêà
  449.  
  450.         //sprintf(debuf, "%U, %U; %U, %U", x0, y0, nx, ny);
  451.         //rtlDebugOutString(debuf);
  452.  
  453. //      popravka = (y - y0 < he - 10);
  454.         //sprintf(debuf, "%U, %U", scroll_y, ny);
  455.         //rtlDebugOutString(debuf);
  456.  
  457.        
  458.         // ñàìè ÿ÷åéêè
  459.  
  460.         y = row_height[0];
  461.         for (i = scroll_y; i < ny; i++)
  462.         {
  463.                 x = col_width[0];
  464.                 if (!sel_moved)
  465.                         kos_DrawBar(col_width[0]+1, y+1, wi - SCROLL_BAR_WIDTH-col_width[0]-1, row_height[i]-1, 0xffffff);
  466.                 for (j = scroll_x; j < nx-1; j++)
  467.                 {
  468.                         if (!sel_moved || is_x_changed(j) || is_y_changed(i))
  469.                                 kos_DrawLine(col_left[j], row_top[i], col_left[j], row_height[i], GRID_COLOR, 0);
  470.  
  471.                         // çàãîëîâêè óæå íàðèñîâàíû - ïðîïóñêàåì èõ
  472.                         if (i && j)    
  473.                         {
  474.                                 //kos_DrawBar(x+1, y+1, col_width[i]-1, row_height[i]-1, 0xffffff);
  475.  
  476.                                 //rtlDebugOutString(cap);
  477.                                 //if (j >= sel_x && j <= sel_end_x && i >= sel_y && i <= sel_end_y)
  478.                                 if (is_between(j,sel_x,sel_end_x) && is_between(i, sel_y, sel_end_y)    // (j,i) - âûäåëåíà
  479.                                         && ((!sel_moved) || (is_x_changed(j) && is_y_changed(i))))                      // è åå íóæíî íàðèñîâàòü
  480.                                 {
  481.                                         if (i == sel_y && j == sel_x)           // ðàìêà
  482.                                         {
  483.                                                 kos_DrawBar(x,y,col_width[j],2,TEXT_COLOR);     // up
  484.                                                 kos_DrawBar(x,y,2,row_height[i],TEXT_COLOR);    // left
  485.                                                 kos_DrawBar(x,y+row_height[i]-2,col_width[j]-2-3,2,TEXT_COLOR);                         // bottom
  486.                                                 kos_DrawBar(x+col_width[j]-2,y, 2,row_height[i]-2-3,TEXT_COLOR);                                // right
  487.  
  488.                                                 kos_DrawBar(x+col_width[j]-4,y+row_height[i]-4,4,4,TEXT_COLOR);
  489.                                                 //kos_DefineButton(x+col_width[j]-2,y+row_height[i]-2,4,4,0x60000000+DRAG_BUTTON,0x000000);
  490.                                                 drag_x = x + col_width[j] - 4;
  491.                                                 drag_y = y + row_height[i] - 4;
  492.                                         }
  493.                                         else
  494.                                                 kos_DrawBar(x + 1,y + 1,col_width[j] - 2,row_height[i] - 2,SEL_CELL_COLOR);     //      âûäåëåíà íî íå îñíîâíàÿ(ñåðàÿ)
  495.  
  496.                                 }
  497.                                 //kos_DefineButton(x,y,col_width[j]-1,row_height[i]-1,0x60000000+CELL_BUTTON+((i << 8) + j),0);
  498.  
  499.                                 char *text;
  500.                                 if (values[j][i] && values[j][i][0] == '#')
  501.                                 {
  502.                                         text = cells[j][i];
  503.                                         kos_DrawRegion(x+1, y+1, col_width[j]-1, row_height[i]-1, 0xff0000, 0);
  504.                                 }
  505.                                 else
  506.                                         text = (values[j][i] && !display_formulas ? values[j][i] : cells[j][i]);
  507.  
  508.                                 int dy = (row_height[i] - 8) / 2 + 1;
  509.  
  510.                                 if (text)
  511.                                         if (strlen(text) < col_width[j]/6)
  512.                                                 kos_WriteTextToWindow(x+2,y+dy,0,text_color,text,strlen(text));
  513.                                         else
  514.                                                 kos_WriteTextToWindow(x+2,y+dy,0,text_color,text,col_width[j]/6);
  515.  
  516.                         }
  517.                         if (!sel_moved || is_x_changed(j) || is_y_changed(i))  
  518.                                 kos_DrawLine(col_left[j]+col_width[j], row_top[i], col_left[j]+col_width[j], row_height[i], GRID_COLOR, 0);
  519.                         x += col_width[j];
  520.                 }
  521.                 y += row_height[i];
  522.         }
  523.  
  524.         // Scrolls:
  525.         // horizontal
  526.  
  527.         //if (!sel_moved) kos_DrawBar(0, he - SCROLL_BAR_HEIGHT, wi - SCROLL_BAR_WIDTH, SCROLL_BAR_HEIGHT, FIXED_CELL_COLOR);
  528.         //if (!sel_moved) kos_DrawBar(scroll_x * wi / col_count, he - SCROLL_BAR_HEIGHT, wi / col_count, SCROLL_BAR_HEIGHT, SEL_FIXED_CELL_COLOR);
  529.         if (!sel_moved)
  530.         {
  531.                 // ãîðèçîíòàëü
  532.                 kos_DrawBar(17, he - SCROLL_BAR_HEIGHT, wi - SCROLL_BAR_WIDTH - 32, SCROLL_BAR_HEIGHT, 0xced0d0);
  533.                 // ñèíèå ëèíèè
  534.                 kos_DrawRegion(0, he - SCROLL_BAR_HEIGHT, wi - SCROLL_BAR_WIDTH, SCROLL_BAR_HEIGHT+1, 0x94aece, 0);
  535.                 // ëåâàÿ êíîïêà
  536.                 draw_custom_button(1, he - SCROLL_BAR_HEIGHT + 1, 14, 14, 1);
  537.                 kos_WriteTextToWindow(6, he - SCROLL_BAR_HEIGHT + 5, 0, 0, "\x1B", 1);
  538.                 // ïðàâàÿ
  539.                 draw_custom_button(wi - SCROLL_BAR_WIDTH * 2 + 1, he - SCROLL_BAR_HEIGHT + 1, 14, 14, 1);
  540.                 kos_WriteTextToWindow(wi - SCROLL_BAR_WIDTH * 2 + 6, he - SCROLL_BAR_HEIGHT + 5, 0, 0, "\x1A", 1);
  541.                 // ïîëçóíîê
  542.                 int tmp_w = (nx - scroll_x) * (wi - SCROLL_BAR_WIDTH - 2 * 14 - 14) / (col_count + 1);
  543.                 if (tmp_w < 16)
  544.                         tmp_w = 16;
  545.                 draw_custom_button(17 + (scroll_x - 1) * (wi - SCROLL_BAR_WIDTH - 2 * 14 - 14) / (col_count + 1), he - SCROLL_BAR_HEIGHT + 1,
  546.                         tmp_w, 14, 1);
  547.  
  548. #define sw(x,y) y,x
  549. // íå ïèíàéòå ìåíÿ çà ýòî, áûëî ëåíü ïåðåñòàâëÿòü ðóêàìè...
  550.  
  551.                 // âåðòèêàëü
  552.                 kos_DrawBar(sw(17, wi - SCROLL_BAR_WIDTH), sw(he - SCROLL_BAR_HEIGHT - 33, SCROLL_BAR_WIDTH), 0xced0d0);
  553.                 // ñèíèå ëèíèè
  554.                 kos_DrawRegion(sw(0, wi - SCROLL_BAR_WIDTH), sw(he - SCROLL_BAR_HEIGHT, SCROLL_BAR_WIDTH+1), 0x94aece, 0); // up
  555.  
  556.                 // âåðõíÿÿ êíîïêà
  557.                 draw_custom_button(sw(1, wi - SCROLL_BAR_WIDTH + 1), 14, 14, 1);
  558.                 kos_WriteTextToWindow(sw(5, wi - SCROLL_BAR_WIDTH + 6), 0, 0, "\x18", 1);
  559.                 // íèæíÿÿ
  560.                 draw_custom_button(sw(he - SCROLL_BAR_HEIGHT * 2 + 1, wi - SCROLL_BAR_WIDTH + 1), 14, 14, 1);
  561.                 //draw_custom_button(sw(he - SCROLL_BAR_HEIGHT * 2 + 1, wi - SCROLL_BAR_WIDTH + 1), 14, 14, 1);
  562.                 kos_WriteTextToWindow(sw(he - SCROLL_BAR_HEIGHT * 2 + 5, wi - SCROLL_BAR_WIDTH + 6), 0, 0, "\x19", 1);
  563.                 // ïîëçóíîê
  564.                 int tmp_h = (ny - scroll_y) * (he - SCROLL_BAR_HEIGHT - 2 * 14 - 14) / (row_count + 1);
  565.                 if (tmp_h < 16)
  566.                         tmp_h = 16;
  567.                 draw_custom_button(sw(17 + (scroll_y - 1) * (he - SCROLL_BAR_HEIGHT - 2 * 14 - 14) / (row_count + 1), wi - SCROLL_BAR_WIDTH + 1),
  568.                         sw(tmp_h, 14), 1);
  569.         }
  570. #define NO_DRAW 0x60000000
  571.         kos_DefineButton(1, he - SCROLL_BAR_HEIGHT + 1, 14, 14, NO_DRAW + SCROLL_LEFT_BUTTON,0);
  572.         kos_DefineButton(wi - SCROLL_BAR_WIDTH * 2 + 2, he - SCROLL_BAR_HEIGHT + 1, 14, 14, NO_DRAW + SCROLL_RIGHT_BUTTON,0);
  573.         kos_DefineButton(17, he - SCROLL_BAR_HEIGHT + 1,  (wi - SCROLL_BAR_WIDTH - 2 * 14), 14, NO_DRAW + SCROLL_WIDTH,0);
  574.  
  575.         kos_DefineButton(sw(1, wi - SCROLL_BAR_WIDTH + 1), 14, 14, NO_DRAW + SCROLL_UP_BUTTON,0);
  576.         kos_DefineButton(sw(he - SCROLL_BAR_HEIGHT * 2 + 2, wi - SCROLL_BAR_WIDTH + 1), 14, 14, NO_DRAW + SCROLL_DOWN_BUTTON,0);
  577.         kos_DefineButton(sw(17, wi - SCROLL_BAR_WIDTH + 1),  sw((he - SCROLL_BAR_HEIGHT - 2 * 14), 14), NO_DRAW + SCROLL_HEIGHT,0);
  578.  
  579. }
  580.  
  581. // î÷åíü áûñòðîå ðèñîâàíèå ñåòêè, â ïðîöåññå èçìåíåíèÿ ðàçìåðîâ ÿ÷ååê
  582. void draw_size_grid()
  583. {
  584.         //rtlDebugOutString("draw size grid");
  585.  
  586.         kos_WindowRedrawStatus(1);
  587.  
  588.         if (size_state == SIZE_X)
  589.         {
  590.                 int x, x0, i;
  591.  
  592.                 x = col_width[0];
  593.                 x0 = 0;
  594.                 for (i = 1; i < col_count && x - x0 + col_width[i] < wi - 10; i++)
  595.                 {
  596.                         if (i >= scroll_x)
  597.                         {
  598.                                 if (i >= size_id)
  599.                                         kos_DrawLine(x - x0, 0, x - x0, he, 0, 1);
  600.                         }
  601.                         else
  602.                                 x0 += col_width[i];
  603.                         x += col_width[i];
  604.                 }
  605.                 kos_DrawLine(x - x0, 0, x - x0, he, 0, 1);
  606.         }
  607.         else
  608.         {
  609.                 int y, y0, i;
  610.  
  611.                 y = row_height[0];
  612.                 y0 = 0;
  613.                 for (i = 1; i < col_count && y - y0 + row_height[i] < he - 10; i++)
  614.                 {
  615.                         if (i >= scroll_y)
  616.                         {
  617.                                 if (i >= size_id)
  618.                                         kos_DrawLine(0, y - y0, wi, y - y0, 0, 1);
  619.                         }
  620.                         else
  621.                                 y0 += row_height[i];
  622.                         y += row_height[i];
  623.                 }
  624.                 kos_DrawLine(0, y - y0, wi, y - y0, 0, 1);
  625.         }
  626.  
  627.  
  628.         kos_WindowRedrawStatus(2);
  629. }
  630.  
  631.  
  632. // áûñòðîå ðèñîâàíèå âûäåëåííîé îáëàñòè ïðè âûäåëåíèè ìûøüþ
  633. #define DCOLOR 0
  634. //0xff0000
  635. #define DINVERT 1
  636. void draw_drag()
  637. {
  638.         kos_WindowRedrawStatus(1);
  639.  
  640.         // ñîáñòâåííî, 4 èíâåðñíûå ëèíèè
  641.  
  642.         int k0 = min(sel_x, sel_end_x);
  643.         int k1 = max(sel_x, sel_end_x);
  644.         int n0 = min(sel_y, sel_end_y);
  645.         int n1 = max(sel_y, sel_end_y);
  646.  
  647.         DWORD x0 = col_left[k0] - 1;
  648.         DWORD x1 = col_left[k1] + col_width[k1] + 1;
  649.         DWORD y0 = row_top[n0] - 1;    
  650.         DWORD y1 = row_top[n1] + row_height[n1] + 1;
  651.         if (x0 > wi - 1) x0 = wi - 1;
  652.         if (x1 > wi - 1) x1 = wi - 1;
  653.         if (y0 > he - 1) y0 = he - 1;
  654.         if (y1 > he - 1) y1 = he - 1;
  655.  
  656.         //sprintf(debuf,"drag %U %U %U %U",k0,k1,n0,n1);
  657.         //rtlDebugOutString(debuf);
  658.  
  659.         kos_DrawLine(x0, y0, x0, y1, DCOLOR, DINVERT);
  660.         kos_DrawLine(x0, y0, x1, y0, DCOLOR, DINVERT);
  661.         kos_DrawLine(x1, y0, x1, y1, DCOLOR, DINVERT);
  662.         kos_DrawLine(x0, y1, x1, y1, DCOLOR, DINVERT);
  663.  
  664.         kos_WindowRedrawStatus(2);
  665. }
  666.  
  667. void draw_window()
  668. {
  669.         int i;
  670.         double xx0=0.0, yy0=0.0;
  671.         sProcessInfo info;
  672.         void *p;
  673.  
  674.         if (sel_end_move)
  675.                 sel_moved = 0;
  676.  
  677.         memset((Byte*)&info, 0, 1024);
  678.  
  679.         kos_ProcessInfo(&info, 0xFFFFFFFF);
  680.  
  681.         p = info.rawData + 42;                  // magic
  682.         wi = *(Dword *)(p);
  683.         he = *(Dword *)((Byte *)p + 4);
  684.         win_x = *(Dword *)((Byte *)p - 8);
  685.         win_y = *(Dword *)((Byte *)p - 4);
  686.  
  687.         myPID = *(Dword*)((Byte *)p - 12);
  688.  
  689.         if (wi == 0)
  690.                 wi = WND_W;
  691.         if (he == 0)
  692.                 he = WND_H;
  693.  
  694.         he -= kos_GetSkinHeight() + MENU_PANEL_HEIGHT; // äîñòóïíàÿ âûñîòà îêíà
  695.         wi -= 6 + 4;
  696.  
  697.         // start redraw
  698.         kos_WindowRedrawStatus(1);
  699.  
  700.         kos_DefineAndDrawWindow(10,40,WND_W,WND_H,0x33,0x40FFFFFF,0,0,(Dword)"Table v" TABLE_VERSION);
  701.  
  702.         if (he + MENU_PANEL_HEIGHT <= 8)
  703.         {
  704.                 kos_WindowRedrawStatus(2);
  705.                 return;
  706.         }
  707.  
  708.         if (!sel_moved)
  709.         {
  710.                 kos_DrawBar(wi-15,he - kos_GetSkinHeight() +7,16,16,0xe4dfe1);
  711.                 kos_DrawBar(0,he - kos_GetSkinHeight() + 23,wi + 1,MENU_PANEL_HEIGHT-4,0xe4dfe1);
  712.         }
  713.  
  714. //      edit_box_draw((dword)&ebox);
  715.         int y = he + kos_GetSkinHeight() - 10;
  716.  
  717.         if (!sel_moved)
  718.         {
  719.                 kos_WriteTextToWindow(3 + 1, y + 3, 0x80 , 0x000000, (char*)sFilename, strlen(sFilename));     
  720.         }
  721.  
  722.         //DWORD fn_line_color = fn_edit ? 0x000000 : 0xc0c0c0;
  723.         //kos_DrawRegion(61, y - 2, 102, 18, fn_line_color, 0);
  724.  
  725.         // äàëüøå editbox width = 100
  726.  
  727.         // border around edit box
  728.         file_box.left = 64;
  729.         file_box.top = y - 1;
  730.         file_box.width = 98;
  731.                 //editbox_h = 18;
  732.         //kos_DefineButton(62, y + 3, 100, 16, 0x60000000+FILENAME_BUTTON, 0xd0d0d0);
  733.  
  734.  
  735.         // ñîõðàíèòü
  736.         kos_DefineButton(20 + 160, y - 5, 60, 20, SAVE_BUTTON, 0xd0d0d0);
  737.         kos_WriteTextToWindow(22 + 160 + (60 - strlen(sSave) * 6) / 2, y + 2, 0, 0x000000, (char*)sSave, strlen(sSave));
  738.  
  739.         // çàãðóçèòü
  740.         kos_DefineButton(90 + 160, y - 5, 60, 20, LOAD_BUTTON, 0xd0d0d0);
  741.         kos_WriteTextToWindow(92 + 160 + (60 - strlen(sLoad) * 6) / 2, y + 2, 0, 0x000000, (char*)sLoad, strlen(sLoad));
  742.  
  743.         // ñîçäàòü. òîëüêî ýòó êíîïó âîòêíóòü íåêóäà î_Î
  744.         /*
  745.         kos_DefineButton(90 + 160 + 70, y - 5, 60, 20, NEW_BUTTON, 0xd0d0d0);
  746.         kos_WriteTextToWindow(92 + 160 + 10 + 70, y + 2, 0, 0x000000, (char*)sNew, strlen(sNew));
  747.         */
  748.         panel_y = y;
  749.  
  750.         draw_grid();
  751.         //kos_DefineButton(0,0,WND_W,WND_H,0x60000002,0);
  752.         //if (is_edit) KEdit();
  753.  
  754.         if ((void*)edit_box_draw != NULL)
  755.         {
  756.                 if (is_edit)
  757.                         edit_box_draw((DWORD)&cell_box);
  758.                 edit_box_draw((DWORD)&file_box);
  759.         }      
  760.  
  761.         // end redraw
  762.         kos_WindowRedrawStatus(2);
  763.         sel_moved = 0;
  764. }
  765.  
  766.  
  767. void process_mouse()
  768. {
  769.         Dword mouse_btn, ckeys, shift, ctrl;
  770.         int mouse_x, mouse_y, i, p, dx = 0, dy = 0;
  771.         int redraw = 0;
  772.        
  773.         Dword mySlot = kos_GetSlotByPID(myPID);
  774.         if (kos_GetActiveSlot() != mySlot)
  775.                 return;
  776.  
  777.         edit_box_mouse((dword)&cell_box);
  778.         edit_box_mouse((dword)&file_box);
  779.  
  780.         int vert, hor;
  781.         kos_GetScrollInfo(vert, hor);
  782.  
  783.         //sprintf(debuf, "scroll %U %U", vert, hor);
  784.         //rtlDebugOutString(debuf);
  785.  
  786.         if (vert != 0) //òðóú ïåðåðèñîâêà!
  787.         {
  788.                 move_sel(sel_x, sel_y + vert);
  789.                 //move_sel(sel_x + hor, sel_y);
  790.                 return;
  791.         }
  792.        
  793.         kos_GetMouseState(mouse_btn, mouse_x, mouse_y);
  794.         mouse_x -= 5;
  795.         mouse_y -= kos_GetSkinHeight();
  796.  
  797.         mouse_btn &= 0x0001;
  798.  
  799.         ckeys = kos_GetSpecialKeyState();
  800.         shift = ckeys & 0x3;
  801.  
  802.         if (mouse_y < 0 && mouse_btn)   // ò.ê. ìûøêà íà çàãîëîâêå îêíà
  803.         {
  804.                 window_is_dragged = 1;
  805.                 return;
  806.         }
  807.         if (window_is_dragged)
  808.         {
  809.                 if (mouse_btn)
  810.                         return;
  811.                 else
  812.                         window_is_dragged = 0;
  813.         }
  814.  
  815.         if (!size_state && !mouse_btn)
  816.                 return;
  817.         if (mouse_btn && !size_state)           // LMB down                            
  818.         {
  819.                 //rtlDebugOutString("lmb down and not resize");
  820.  
  821.                 if (mouse_x >= drag_x && mouse_x <= drag_x + 4 && mouse_y >= drag_y && mouse_y <= drag_y + 4)
  822.                 {
  823.                         size_state = SIZE_DRAG;
  824.                         old_end_x = sel_end_x;
  825.                         old_end_y = sel_end_y;
  826.                 }
  827.                 else if (mouse_y <= row_height[0])
  828.                 {
  829.                         //rtlDebugOutString("can resize cols");
  830.                         int kx = -1, i;
  831.                         for (i = 0; i < col_count - 1; i++)
  832.                         if (mouse_x >= col_left[i] + col_width[i] - 5 &&
  833.                                 mouse_x <= col_left[i + 1] + 5)
  834.                         {
  835.                                 kx = i; break;
  836.                         }
  837.                         if (kx != -1)
  838.                         {
  839.                                 //sprintf(debuf,"size x %U",k);
  840.                                 //rtlDebugOutString(debuf);
  841.                                 size_id = kx;
  842.                                 size_state = SIZE_X;
  843.                         }
  844.                 }
  845.                 else if (mouse_x <= col_width[0])
  846.                 {
  847.                         int ky = -1;
  848.                         for (i = 0; i < row_count - 1; i++)
  849.                         if (mouse_y >= row_top[i] + row_height[i] - 5 &&
  850.                                 mouse_y <= row_top[i + 1] + 5)
  851.                         {
  852.                                 ky = i; break;
  853.                         }
  854.                         if (ky != -1)
  855.                         {
  856.                                 size_id = ky;
  857.                                 size_state = SIZE_Y;
  858.                         }
  859.                 }
  860.                 else            // êëèêíóòà ÿ÷åéêà
  861.                 if (mouse_x <= col_left[nx - 1] &&  mouse_y <= row_top[ny - 1])
  862.                 {
  863.                         was_single_selection = sel_x == sel_end_x && sel_y == sel_end_y;
  864.                         int kx = -1, i;
  865.                         for (i = 0; i < col_count - 1; i++)
  866.                         if (mouse_x >= col_left[i] &&
  867.                                 mouse_x <= col_left[i] + col_width[i])
  868.                         {
  869.                                 kx = i; break;
  870.                         }
  871.                         int ky = -1;
  872.                         for (i = 0; i < row_count - 1; i++)
  873.                         if (mouse_y >= row_top[i] &&
  874.                                 mouse_y <= row_top[i] + row_height[i])
  875.                         {
  876.                                 ky = i; break;
  877.                         }
  878.                         if (kx != -1 && ky != -1)
  879.                         {
  880.                                 if (!shift)
  881.                                 {
  882.                                         move_sel(kx, ky);
  883.                                         //return;
  884.                                 }
  885.                                 else
  886.                                 {
  887.                                         sel_end_x = kx;
  888.                                         sel_end_y = ky;
  889.                                 }
  890.                                 size_state = SIZE_SELECT;
  891.                         }
  892.                 }
  893.                 if (size_state)
  894.                 {
  895.                         size_mouse_x = mouse_x;
  896.                         size_mouse_y = mouse_y;
  897.                 }
  898.                 return;
  899.         }
  900.         else if (!mouse_btn && size_state)
  901.         {
  902.                 sel_moved = 0;          // ÷òîáû áûëà òðó ïåðåðèñîâêà
  903.                 //rtlDebugOutString("resize end");
  904.  
  905.                 if (size_state == SIZE_DRAG)
  906.                 {
  907.                         fill_cells(sel_x, sel_y, sel_end_x, sel_end_y, old_end_x, old_end_y);
  908.                 }
  909.  
  910.                 //sel_moved = (size_state == SIZE_SELECT && sel_x == sel_end_x && sel_y == sel_end_y && was_single_selection);
  911.                 size_state = 0;
  912.                 draw_window();          // âñå ñäâèíóëîñü - íàäî îáíîâèòüñÿ
  913.                 return;
  914.         }
  915.         if (size_state == SIZE_X && mouse_x != size_mouse_x)
  916.         {
  917.                 draw_size_grid();
  918.                 col_width[size_id] += mouse_x - size_mouse_x;
  919.                 if (col_width[size_id] < 15)
  920.                         col_width[size_id] = 15;
  921.                 else if (col_width[size_id] > wi / 2)
  922.                         col_width[size_id] = wi / 2;
  923.                 draw_size_grid();
  924.         }
  925.         if (size_state == SIZE_Y && mouse_y != size_mouse_y)
  926.         {
  927.                 draw_size_grid();
  928.                 row_height[size_id] += mouse_y - size_mouse_y;
  929.                 if (row_height[size_id] < 15)
  930.                         row_height[size_id] = 15;
  931.                 else if (row_height[size_id] > he / 2)
  932.                         row_height[size_id] = he / 2;
  933.                 draw_size_grid();
  934.         }
  935.         if ((size_state == SIZE_SELECT || size_state == SIZE_DRAG) && (mouse_x != size_mouse_x || mouse_y != size_mouse_y))
  936.         {
  937.                 draw_drag();
  938.                 int kx = -1, i;
  939.                 for (i = 0; i < col_count - 1; i++)
  940.                         if (mouse_x >= col_left[i] &&
  941.                                 mouse_x <= col_left[i + 1])
  942.                         {
  943.                                 sprintf(debuf, "yyy %U",col_left[i+1]);
  944.                                 rtlDebugOutString(debuf);
  945.                                 kx = i; break;
  946.                         }
  947.                 int ky = -1;
  948.                 for (i = 0; i < row_count - 1; i++)
  949.                         if (mouse_y >= row_top[i] &&
  950.                                 mouse_y <= row_top[i + 1])
  951.                         {
  952.                                 ky = i; break;
  953.                         }
  954.                 if (kx != -1) sel_end_x = kx;
  955.                 if (kx != -1) sel_end_y = ky;
  956.                 if (size_state == SIZE_DRAG)
  957.                 {
  958.                         if (abs(sel_end_x - sel_x) > 0)
  959.                         {
  960.                                 sel_end_y = old_end_y;
  961.                         }
  962.                         else if (abs(sel_end_y - sel_y) > 0)
  963.                         {
  964.                                 sel_end_x = old_end_x;
  965.                         }
  966.                 }
  967.                 draw_drag();
  968.         }
  969.         size_mouse_x = mouse_x;
  970.         size_mouse_y = mouse_y;
  971. }
  972.  
  973. void process_key()
  974. {
  975.         Dword mouse_btn, ckeys, shift, ctrl;
  976.         int mouse_x, mouse_y, i, p, dx = 0, dy = 0;
  977.  
  978.         // key pressed, read it
  979.         Byte keyCode;
  980.         ckeys = kos_GetSpecialKeyState();
  981.         shift = ckeys & 0x3;
  982.         ctrl = ckeys & 0x0c;
  983.         //if (ctrl)
  984.         //      rtlDebugOutString("control pressed!");
  985.         dx = 0, dy = 0;
  986.         sel_moved = 0;
  987.         sel_end_move = 0;
  988.         kos_GetKey(keyCode);
  989.  
  990.         __asm
  991.         {
  992.                 mov ah, keyCode
  993.         }
  994.         edit_box_key((dword)&cell_box);
  995.         edit_box_key((dword)&file_box);
  996.  
  997.  
  998.         switch (keyCode)
  999.         {
  1000.                 case 178:                       // ñòðåëêè
  1001.                         //dx = 0;
  1002.                         dy = -1;
  1003.                         break;
  1004.                 case 176:
  1005.                         dx = -1;
  1006.                         //dy = 0;
  1007.                         break;
  1008.                 case 179:
  1009.                         dx = 1;
  1010.                         //dy = 0;
  1011.                         break;
  1012.                 case 177:
  1013.                         //dx = 0;
  1014.                         dy = 1;
  1015.                         break;
  1016.                 /*
  1017.                 case 183:
  1018.                         if (sel_y < row_count-(ny - scroll_y))  // page down
  1019.                                 dy = ny - scroll_y;
  1020.                         else
  1021.                                 dy = row_count-(ny - scroll_y) - sel_y;
  1022.                         dx = 0;
  1023.                         redraw = 1;
  1024.                         break;
  1025.                 case 184:
  1026.                         if (sel_y > ny - scroll_y)              // page up
  1027.                                 dy= - (ny - scroll_y);
  1028.                         else
  1029.                                 dy = - (ny - scroll_y) + sel_y;
  1030.                         dx = 0;
  1031.                         redraw = 1;
  1032.                         break;
  1033.                 */
  1034.                 case 180: //home
  1035.                         dx = -sel_x + 1;
  1036.                         dy = 0;
  1037.                         draw_grid(); //draw_window();
  1038.                         break;
  1039.                 case 181: //end
  1040.                         dx = col_count - (nx - scroll_x) - 1 - sel_x;
  1041.                         dy = 0;
  1042.                         draw_grid(); //draw_window();
  1043.                         break;
  1044.                 case 27:                // escape
  1045.                         cancel_edit();
  1046.                         break;
  1047.                 case 182:                       // delete
  1048.                         {
  1049.                                 int i,j,n0,n1,k0,k1;
  1050.                                 n0 = min(sel_x, sel_end_x);
  1051.                                 n1 = max(sel_x, sel_end_x);
  1052.                                 k0 = min(sel_y, sel_end_y);
  1053.                                 k1 = max(sel_y, sel_end_y);
  1054.  
  1055.                                 for (i = n0; i <= n1; i++)
  1056.                                         for (j = k0; j <= k1; j++)
  1057.                                         {
  1058.                                                 if (cells[i][j])
  1059.                                                 {
  1060.                                                         freemem(cells[i][j]);
  1061.                                                         cells[i][j] = NULL;
  1062.                                                 }
  1063.                                         }
  1064.                                 calculate_values();
  1065.                                 draw_grid();
  1066.                                 break;
  1067.                         }
  1068.                 case 0x0D:                      // enter
  1069.                         if (is_edit)
  1070.                         {
  1071.                                 stop_edit();
  1072.                                 draw_window();
  1073.                         }
  1074.                         break;
  1075.                 //case 0x08:                    // backspace
  1076.                         /*if (is_edit || fn_edit)
  1077.                         {
  1078.                                 if (strlen(edit_text) != 0)
  1079.                                         edit_text[strlen(edit_text) - 1] = '\0';
  1080.                                 KEdit();
  1081.                         }
  1082.                         else if (cells[sel_x][sel_y])
  1083.                         {
  1084.                                 start_edit(sel_x, sel_y);
  1085.                         }
  1086.                         */
  1087.                 //      break;
  1088.                 case 22:        // contol-v
  1089.                         {
  1090.                                 if (ctrl)
  1091.                                 {
  1092.                                         //rtlDebugOutString("control-v!");
  1093.                                         int i, j, x0, y0;
  1094.                                         x0 = min(sel_x, sel_end_x);
  1095.                                         y0 = min(sel_y, sel_end_y);
  1096.                                         int delta_x = x0 - buf_old_x;
  1097.                                         int delta_y = y0 - buf_old_y;
  1098.  
  1099.                                         for (i = 0; i < buf_col; i++)
  1100.                                                 for (j = 0; j < buf_row; j++)
  1101.                                                 {
  1102.                                                         if (i + x0 >= col_count || j + y0 >= row_count)
  1103.                                                                 continue;
  1104.                                                         if (cells[i + x0][j + y0])
  1105.                                                                 freemem(cells[i + x0][j + y0]);
  1106.                                                         if (buffer[i][j])
  1107.                                                         {
  1108.                                                                 cf_x0 = buf_old_x; cf_y0 = buf_old_y;
  1109.                                                                 cf_x1 = buf_old_x + buf_col;
  1110.                                                                 cf_y1 = buf_old_y + buf_row;
  1111.                                                                 cells[i + x0][j + y0] = change_formula(buffer[i][j], delta_x, delta_y);
  1112.                                                                 //cells[i + x0][j + y0] = (char*)allocmem(strlen(buffer[i][j]));
  1113.                                                                 //strcpy(cells[i + x0][j + y0], buffer[i][j]);
  1114.                                                         }
  1115.                                                         else
  1116.                                                                 cells[i + x0][j + y0] = NULL;
  1117.                                                 }
  1118.  
  1119.                                         calculate_values();
  1120.                                         draw_window();
  1121.                                         break;
  1122.                                 }
  1123.                         }
  1124.                         case 24:        // control-x
  1125.                         case 03:        // control-c
  1126.                         {
  1127.                                 if (ctrl)
  1128.                                 {
  1129.                                         //rtlDebugOutString("control-c!");
  1130.                                         int i, j, x0, y0;
  1131.  
  1132.                                         freeBuffer();
  1133.  
  1134.                                         buf_col = abs(sel_end_x - sel_x) + 1;
  1135.                                         buf_row = abs(sel_end_y - sel_y) + 1;
  1136.                                         x0 = min(sel_x, sel_end_x);
  1137.                                         y0 = min(sel_y, sel_end_y);
  1138.                                         buf_old_x = x0;
  1139.                                         buf_old_y = y0;
  1140.  
  1141.                                         //sprintf(debuf, "%U %U %U %U", buf_col, buf_row, x0, y0);
  1142.                                         //rtlDebugOutString(debuf);
  1143.                                
  1144.                                         buffer = (char***)allocmem(buf_col * sizeof(char**));
  1145.                                         for (i = 0; i < buf_col; i++)
  1146.                                         {
  1147.                                                 buffer[i] = (char**)allocmem(buf_row * sizeof(char*));
  1148.                                                 for (j = 0; j < buf_row; j++)
  1149.                                                 {
  1150.                                                         if (cells[i + x0][j + y0])
  1151.                                                         {
  1152.                                                                 if (keyCode == 03)      // ctrl-c
  1153.                                                                 {
  1154.                                                                         buffer[i][j] = (char*)allocmem(strlen(cells[i + x0][j + y0]));
  1155.                                                                         strcpy(buffer[i][j], cells[i + x0][j + y0]);
  1156.                                                                 }
  1157.                                                                 else
  1158.                                                                 {
  1159.                                                                         buffer[i][j] = cells[i + x0][j + y0];
  1160.                                                                         cells[i + x0][j + y0] = NULL;
  1161.                                                                 }
  1162.                                                         }
  1163.                                                         else
  1164.                                                                 buffer[i][j] = NULL;
  1165.                                                 }
  1166.                                         }
  1167.                                         if (keyCode == 24)
  1168.                                                 calculate_values();
  1169.                                         draw_window();
  1170.                                         break;
  1171.                                 }
  1172.                         }
  1173.                 case 06:                // control-f
  1174.                         {
  1175.                                 display_formulas = !display_formulas;
  1176.                                 draw_grid(); //draw_window();
  1177.                                 break;
  1178.                         }
  1179.                 default:
  1180.                        
  1181.                         if (!is_edit && !(file_box.flags & ed_focus))
  1182.                         {
  1183.                                 start_edit(sel_x, sel_y);
  1184.                                 if (keyCode == 8)
  1185.                                 {
  1186.                                         cell_box.pos = strlen(edit_text);
  1187.                                 }
  1188.                                 else
  1189.                                 {
  1190.                                         __asm
  1191.                                         {
  1192.                                                 mov ah, keyCode
  1193.                                         }
  1194.                                         edit_box_key((dword)&cell_box);
  1195.                                 }
  1196.                         }
  1197.                         if (is_edit)
  1198.                                 edit_box_draw((dword)&cell_box);
  1199.                         /*
  1200.                         if (strlen(edit_text)<256)
  1201.                         {
  1202.                                 edit_text[strlen(edit_text)]=keyCode;
  1203.                                 edit_text[strlen(edit_text) + 1]='\0';
  1204.                                 KEdit();
  1205.                         }
  1206.                         */
  1207.                         break;
  1208.         }
  1209.         if (dx != 0)
  1210.         {
  1211.                 if (shift)
  1212.                 {
  1213.                         sel_end_x += dx;
  1214.                         if (sel_end_x <= 1)
  1215.                                 sel_end_x = 1;
  1216.                         else if (sel_end_x >= col_count)
  1217.                                 sel_end_x = col_count - 1;
  1218.                 //      sprintf(debuf,"sel end x change. sel end %U %U",sel_end_x,sel_end_y);
  1219.                 //      rtlDebugOutString(debuf);
  1220.                         sel_moved = sel_end_move = 1;
  1221.                         //stop_edit();
  1222.                         //draw_grid();
  1223.                 }
  1224.                 else
  1225.                 {
  1226.                 }
  1227.         }
  1228.         if (dy != 0)
  1229.         {
  1230.                 if (shift)
  1231.                 {
  1232.                         sel_end_y += dy;
  1233.                         if (sel_end_y <= 1)
  1234.                                 sel_end_y = 1;
  1235.                         else if (sel_end_y >= row_count)
  1236.                                 sel_end_y = row_count - 1;
  1237.                 //      sprintf(debuf,"sel end y change. sel end %U %U",sel_end_x,sel_end_y);
  1238.                 //      rtlDebugOutString(debuf);
  1239.                         sel_moved = sel_end_move = 1;
  1240.                         //stop_edit();
  1241.                         //draw_grid();
  1242.                 }
  1243.         }
  1244.         /*
  1245.         if (sel_end_x < sel_x)
  1246.         {
  1247.                 Dword tmp = sel_end_x; sel_end_x = sel_x; sel_x = tmp;
  1248.         }
  1249.         if (sel_end_y < sel_y)
  1250.         {
  1251.                 Dword tmp = sel_end_y; sel_end_y = sel_y; sel_y = tmp;
  1252.         }
  1253.         */
  1254.         if ((dx || dy))
  1255.         {
  1256.                 if (!shift)
  1257.                 {
  1258.                         move_sel(sel_x + dx, sel_y + dy);
  1259.                 }
  1260.                 else
  1261.                 {
  1262.                         sel_moved = 0;
  1263.                         stop_edit();
  1264.                         draw_grid();
  1265.                 }
  1266.         }
  1267. }
  1268.  
  1269.  
  1270. void process_button()
  1271. {
  1272.         Dword mouse_btn, ckeys, shift, ctrl;
  1273.         int mouse_x, mouse_y, i, p, dx = 0, dy = 0;
  1274.         int redraw = 0;
  1275.  
  1276.         Dword button;
  1277.         kos_GetButtonID(button);
  1278.  
  1279.         /*
  1280.         sprintf(debuf, "button %U", button);
  1281.         rtlDebugOutString(debuf);
  1282.         //*/
  1283.  
  1284.         switch (button)
  1285.         {
  1286.         case 1:
  1287.                 kos_ExitApp();
  1288.  
  1289.         case SCROLL_LEFT_BUTTON:
  1290.                 //rtlDebugOutString("scroll left btn");
  1291.                 stop_edit();
  1292.                 scroll_x--;
  1293.                 if (scroll_x <= 0)
  1294.                         scroll_x = 1;
  1295.                 sel_moved = 0;
  1296.                 /*if (sel_x > nx - 1)
  1297.                 {
  1298.                         nx - 1;
  1299.                         sel_end_x = sel_x;
  1300.                 }*/
  1301.                 draw_window();
  1302.                 return;
  1303.  
  1304.         case SCROLL_RIGHT_BUTTON:
  1305.                 //rtlDebugOutString("scroll right btn");
  1306.                 stop_edit();
  1307.                 scroll_x++;
  1308.                 if (scroll_x >= col_count - 1)
  1309.                         scroll_x = col_count - 1;
  1310.                 sel_moved = 0;/*
  1311.                 if (sel_x < scroll_x)
  1312.                 {
  1313.                         sel_x = scroll_x;
  1314.                         sel_end_x = sel_x;
  1315.                 }*/
  1316.                 draw_window();
  1317.                 return;
  1318.  
  1319.         case SCROLL_WIDTH:
  1320.                 {
  1321.                         //rtlDebugOutString("scroll width btn");
  1322.                         stop_edit();
  1323.                         kos_GetMouseState(mouse_btn, mouse_x, mouse_y);
  1324.                         mouse_x -= 5;
  1325.                         mouse_y -= kos_GetSkinHeight();
  1326.  
  1327.                         // âñåãî: wi - SCROLL_BAR_WIDTH - 2 * 14
  1328.  
  1329.                         int tmp_w = (nx - scroll_x) * (wi - SCROLL_BAR_WIDTH - 3 * 14) / (col_count + 1);
  1330.                         if (tmp_w < 16)
  1331.                                 tmp_w = 16;
  1332.                         scroll_x = (mouse_x - 14 - tmp_w / 2) * (col_count + 1) / (wi - SCROLL_BAR_WIDTH - 3 * 14) + 1;
  1333.                         if (scroll_x <= 0)
  1334.                                 scroll_x = 1;
  1335.                         else if (scroll_x >= col_count - 1)
  1336.                                 scroll_x = col_count - 1;
  1337.                         sel_moved = 0;
  1338.                         draw_window();
  1339.                         return;
  1340.                 }
  1341.  
  1342.         case SCROLL_UP_BUTTON:
  1343.                 stop_edit();
  1344.                 scroll_y--;
  1345.                 if (scroll_y <= 0)
  1346.                         scroll_y = 1;
  1347.                 sel_moved = 0;
  1348.                 //draw_window();
  1349.                 draw_grid();
  1350.                 /*
  1351.                 if (sel_y > ny - 1)
  1352.                 {
  1353.                         sel_y = ny - 1;
  1354.                         sel_end_y = sel_y;
  1355.                 }*/
  1356.                 return;
  1357.  
  1358.         case SCROLL_DOWN_BUTTON:
  1359.                 stop_edit();
  1360.                 scroll_y++;
  1361.                 if (scroll_y >= row_count - 1)
  1362.                         scroll_y = row_count - 1;
  1363.                 sel_moved = 0;/*
  1364.                 if (sel_y < scroll_y)
  1365.                 {
  1366.                         sel_y = scroll_y;
  1367.                         sel_end_y = sel_y;
  1368.                 }*/
  1369.                 draw_grid();
  1370.                 return;
  1371.  
  1372.         case SCROLL_HEIGHT:
  1373.                 {
  1374.                         stop_edit();
  1375.                         kos_GetMouseState(mouse_btn, mouse_x, mouse_y);
  1376.                         mouse_x -= 5;
  1377.                         mouse_y -= kos_GetSkinHeight();
  1378.                         int tmp_h = (ny - scroll_y) * (he - SCROLL_BAR_HEIGHT - 2 * 14) / row_count;
  1379.                         if (tmp_h < 16)
  1380.                                 tmp_h = 16;
  1381.                         scroll_y = (mouse_y - 2 * 14) * (row_count + 1) / (he - SCROLL_BAR_HEIGHT - 3 * 14) + 1;
  1382.                         if (scroll_y <= 0)
  1383.                                 scroll_y = 1;
  1384.                         else if (scroll_y >= row_count - 1)
  1385.                                 scroll_y = row_count - 1;
  1386.                         sel_moved = 0;
  1387.                         draw_grid();
  1388.                         return;
  1389.                 }
  1390.  
  1391.         case NEW_BUTTON:        // clear the table
  1392.                 reinit();
  1393.                 draw_window();
  1394.                 break;
  1395.  
  1396.         case FILENAME_BUTTON:
  1397.                 sel_moved = 1;
  1398.                 stop_edit();
  1399.                 fn_edit = 1;
  1400.                 strcpy(edit_text, fname);
  1401.                 draw_window();
  1402.                 break;
  1403.  
  1404.         case SAVE_BUTTON:
  1405.                 stop_edit();
  1406.                 kos_DrawBar(320, panel_y, wi - 320 - 10, 10, 0xe4dfe1);
  1407.                 if (SaveFile(fname))
  1408.                         kos_WriteTextToWindow(320, panel_y, 0, 0x000000, (char*)msg_save, strlen(msg_save));
  1409.                 break;
  1410.  
  1411.         case LOAD_BUTTON:
  1412.                 stop_edit();
  1413.                 int r = LoadFile(fname);
  1414.                 kos_DrawBar(320, panel_y, wi - 320 - 10, 10, 0xe4dfe1);
  1415.                 if (r > 0)
  1416.                 {
  1417.                         calculate_values();
  1418.                         sel_moved = 0;
  1419.                         draw_window();
  1420.                         kos_WriteTextToWindow(320, panel_y,0,0x000000,(char*)msg_load, strlen(msg_load));
  1421.                 }
  1422.                 else if (r == -1)
  1423.                         kos_WriteTextToWindow(320, panel_y,0,0x000000,(char*)er_file_not_found,strlen(er_file_not_found));
  1424.                 else if (r == -2)
  1425.                         kos_WriteTextToWindow(320, panel_y,0,0x000000,(char*)er_format,strlen(er_format));
  1426.                 break;
  1427.         }
  1428.         if (button >= COL_HEAD_BUTTON && button < ROW_HEAD_BUTTON)
  1429.         {
  1430.                 sel_end_x = sel_x = button - COL_HEAD_BUTTON;
  1431.                 sel_y = 1;
  1432.                 sel_end_y = row_count - 1;
  1433.                 stop_edit();
  1434.                 draw_window();
  1435.                 return;
  1436.         }
  1437.         else if (button >= ROW_HEAD_BUTTON && button < CELL_BUTTON)
  1438.         {
  1439.                 sel_end_y = sel_y = button - ROW_HEAD_BUTTON;
  1440.                 sel_x = 1;
  1441.                 sel_end_x = col_count - 1;
  1442.                 stop_edit();
  1443.                 draw_window();
  1444.                 return;
  1445.         }
  1446.  
  1447. }
  1448.  
  1449. void kos_Main()
  1450. {
  1451.         kos_InitHeap();
  1452.         load_edit_box();
  1453.  
  1454.         init();
  1455.         draw_window();
  1456.         for (;;)
  1457.         {
  1458.                 switch (kos_WaitForEvent(10))   // äà, ïëîõî. ïîòîì íóæíî áóäåò ïðîñòî ëîâèòü ñîáûòèÿ ìûøè.
  1459.                 {
  1460.                 case 0:
  1461.                         process_mouse();
  1462.                         break;
  1463.                 case 1:
  1464.                         draw_window();
  1465.                         break;
  1466.                 case 2:
  1467.                         process_key();
  1468.                         break;
  1469.                 case 3:
  1470.                         process_button();
  1471.                         break;
  1472.                 //case 6:
  1473.                 //      draw_window();
  1474.                 //      break;
  1475.                 }
  1476.         }
  1477. }
  1478.  
  1479.