Subversion Repositories Kolibri OS

Rev

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