Subversion Repositories Kolibri OS

Rev

Rev 7503 | Rev 7507 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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