Subversion Repositories Kolibri OS

Rev

Rev 7897 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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