Subversion Repositories Kolibri OS

Rev

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