Subversion Repositories Kolibri OS

Rev

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

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