Subversion Repositories Kolibri OS

Rev

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