예제 #1
0
void process_key()
{
	Dword mouse_btn, ckeys, shift, ctrl;
	int mouse_x, mouse_y, dx = 0, dy = 0;

	// key pressed, read it 
	Byte keyCode;
	ckeys = kos_GetSpecialKeyState();
	shift = ckeys & 0x3;
	ctrl = ckeys & 0x0c;
	//if (ctrl)
	//	rtlDebugOutString("control pressed!");
	dx = 0, dy = 0;
	sel_moved = 0;
	sel_end_move = 0;
	kos_GetKey(keyCode);

	__asm
	{
		mov ah, keyCode
	}
	edit_box_key((dword)&cell_box);
	edit_box_key((dword)&file_box);


	switch (keyCode)
	{
		case 178:			// стрелки
			//dx = 0;
			dy = -1;
			break;
		case 176:
			dx = -1;
			//dy = 0;
			break;
		case 179:
			dx = 1;
			//dy = 0;
			break;
		case 177:
			//dx = 0;
			dy = 1;
			break;
		/*
		case 183:
			if (sel_y < row_count-(ny - scroll_y))	// page down
				dy = ny - scroll_y;
			else
				dy = row_count-(ny - scroll_y) - sel_y;
			dx = 0;
			redraw = 1;
			break;
		case 184:
			if (sel_y > ny - scroll_y)		// page up
				dy= - (ny - scroll_y);
			else
				dy = - (ny - scroll_y) + sel_y;
			dx = 0;
			redraw = 1;
			break;
		*/
		case 180: //home
			dx = -sel_x + 1;
			dy = 0;
			draw_grid(); //draw_window(); 
			break;
		case 181: //end
			dx = col_count - (nx - scroll_x) - 1 - sel_x;
			dy = 0;
			draw_grid(); //draw_window();
			break;
		case 27:		// escape
			cancel_edit();
			break;
		case 182:			// delete
			{
				int i,j,n0,n1,k0,k1;
				n0 = min(sel_x, sel_end_x);
				n1 = max(sel_x, sel_end_x);
				k0 = min(sel_y, sel_end_y);
				k1 = max(sel_y, sel_end_y);

				for (i = n0; i <= n1; i++)
					for (j = k0; j <= k1; j++)
					{
						if (cells[i][j])
						{
							freemem(cells[i][j]);
							cells[i][j] = NULL;
						}
					}
				calculate_values();
				draw_grid();
				break;
			}
		case 0x0D:			// enter
			if (is_edit)
			{
				stop_edit();
				draw_window();
			}
			break;
		case 22:	// contol-v
			{
				if (ctrl)
				{
					//rtlDebugOutString("control-v!");
					int i, j, x0, y0;
					x0 = min(sel_x, sel_end_x);
					y0 = min(sel_y, sel_end_y);
					int delta_x = x0 - buf_old_x;
					int delta_y = y0 - buf_old_y;

					for (i = 0; i < buf_col; i++)
						for (j = 0; j < buf_row; j++)
						{
							if (i + x0 >= col_count || j + y0 >= row_count)
								continue;
							if (cells[i + x0][j + y0])
								freemem(cells[i + x0][j + y0]);
							if (buffer[i][j])
							{
								cf_x0 = buf_old_x; cf_y0 = buf_old_y;
								cf_x1 = buf_old_x + buf_col;
								cf_y1 = buf_old_y + buf_row;
								cells[i + x0][j + y0] = change_formula(buffer[i][j], delta_x, delta_y);
								//cells[i + x0][j + y0] = (char*)allocmem(strlen(buffer[i][j]));
								//strcpy(cells[i + x0][j + y0], buffer[i][j]);
							}
							else
								cells[i + x0][j + y0] = NULL;
						}

					calculate_values();
					draw_window();
					break;
				}
			}
			case 24:	// control-x
			case 03:	// control-c
			{
				if (ctrl)
				{
					//rtlDebugOutString("control-c!");
					int i, j, x0, y0;

					freeBuffer();

					buf_col = abs(sel_end_x - sel_x) + 1;
					buf_row = abs(sel_end_y - sel_y) + 1;
					x0 = min(sel_x, sel_end_x);
					y0 = min(sel_y, sel_end_y);
					buf_old_x = x0;
					buf_old_y = y0;

					//sprintf(debuf, "%U %U %U %U", buf_col, buf_row, x0, y0);
					//rtlDebugOutString(debuf);
				
					buffer = (char***)allocmem(buf_col * sizeof(char**));
					for (i = 0; i < buf_col; i++)
					{
						buffer[i] = (char**)allocmem(buf_row * sizeof(char*));
						for (j = 0; j < buf_row; j++)
						{
							if (cells[i + x0][j + y0])
							{
								if (keyCode == 03)	// ctrl-c
								{
									buffer[i][j] = (char*)allocmem(strlen(cells[i + x0][j + y0]));
									strcpy(buffer[i][j], cells[i + x0][j + y0]);
								}
								else
								{
									buffer[i][j] = cells[i + x0][j + y0];
									cells[i + x0][j + y0] = NULL;
								}
							}
							else
								buffer[i][j] = NULL;
						}
					}
					if (keyCode == 24) 
						calculate_values();
					draw_window();
					break;
				}
			}
		case 06:		// control-f
			{
				display_formulas = !display_formulas;
				draw_grid(); //draw_window();
				break;
			}
		default:
			
			if (!is_edit && !(file_box.flags & ed_focus))
			{
				start_edit(sel_x, sel_y);
				if (keyCode == 8)
				{
					cell_box.pos = strlen(edit_text);
				}
				else
				{
					__asm
					{
						mov ah, keyCode
					}
					edit_box_key((dword)&cell_box);
				}
			}
			if (is_edit)
				edit_box_draw((dword)&cell_box);
			break;
	}
	if (dx != 0)
	{
		if (shift)
		{
			sel_end_x += dx;
			if (sel_end_x <= 1)
				sel_end_x = 1;
			else if (sel_end_x >= col_count)
				sel_end_x = col_count - 1;
		//	sprintf(debuf,"sel end x change. sel end %U %U",sel_end_x,sel_end_y);
		//	rtlDebugOutString(debuf);
			sel_moved = sel_end_move = 1;
			//stop_edit();
			//draw_grid();
		}
		else
		{
		}
	}
	if (dy != 0)
	{
		if (shift)
		{
			sel_end_y += dy;
			if (sel_end_y <= 1)
				sel_end_y = 1;
			else if (sel_end_y >= row_count)
				sel_end_y = row_count - 1;
		//	sprintf(debuf,"sel end y change. sel end %U %U",sel_end_x,sel_end_y);
		//	rtlDebugOutString(debuf);
			sel_moved = sel_end_move = 1;
			//stop_edit();
			//draw_grid();
		}
	}
	/*
	if (sel_end_x < sel_x)
	{
		Dword tmp = sel_end_x; sel_end_x = sel_x; sel_x = tmp;
	}
	if (sel_end_y < sel_y)
	{
		Dword tmp = sel_end_y; sel_end_y = sel_y; sel_y = tmp;
	}
	*/
	if ((dx || dy))
	{
		if (!shift)
		{
			if ((sel_end_x + dx) >= (col_count-1)) {dx=0;} //заглушка
			else if ((sel_end_y + dy) >= (row_count-1)) {dy=0;}
			else {
			move_sel(sel_x + dx, sel_y + dy);
			}
		}
		else
		{
			sel_moved = 0;
			stop_edit();
			draw_grid();
		}
	}
}
예제 #2
0
void fill_cells(int sel_x, int sel_y, int sel_end_x, int sel_end_y, int old_end_x, int old_end_y)
{
    // итак, (sel_x, sel_y) :: (old_end_x, old_end_y) - источник
    // результат хранится либо в строке sel_x .. sel_end_x, либо в столбце sel_y .. sel_end_y

    int i, start, end, step, gdir = -1;
    int pdir = -1;
    char *source;

    cf_x0 = cf_y0 = 0;
    cf_x1 = col_count;
    cf_y1 = row_count;

    if (sel_end_x == -1)
        sel_end_x = sel_x;
    if (sel_end_y == -1)
        sel_end_y = sel_y;

    // если направления выделений перпендикулярны, то просто в цикле повторяем то же, что для 1 ячейки:

    if (old_end_x == sel_end_x && sel_y == old_end_y)
    {
        gdir = 0;
    }
    else if (old_end_y == sel_end_y && sel_x == old_end_x)
    {
        gdir = 1;
    }

    //sprintf(debuf, "f**k in ass %U %U %U %U %U %U dir %U",sel_x,sel_y,sel_end_x,sel_end_y,old_end_x,old_end_y,gdir);
    //rtlDebugOutString(debuf);
    if (gdir != -1)
    {
        int gstep = gdir ? sign(old_end_y - sel_y) : sign(old_end_x - sel_x);
        if (gstep == 0)
        {
            /*	if (gdir)
            	{
            		//old_end_y += 1;
            	}
            	else
            	{
            		//old_end_x += 1;
            	}
            */
            gstep = 1;
        }

        for (; gdir ? (sel_y != old_end_y + gstep) : (sel_x != old_end_x + gstep);
                gdir ? (sel_y += gstep) : (sel_x += gstep))
        {
            //sprintf(debuf, "cycle %U %U %U %U %U %U dir %U",sel_x,sel_y,sel_end_x,sel_end_y,old_end_x,old_end_y,gdir);
            //rtlDebugOutString(debuf);
            int dir;
            source = cells[sel_x][sel_y];
            if (gdir == 0)
            {
                start = sel_y;
                end = sel_end_y;
                step = (sel_y < sel_end_y ? 1 : -1);
                dir = 1;
            }
            else
            {
                start = sel_x;
                end = sel_end_x;
                step = (sel_x < sel_end_x ? 1 : -1);
                dir = 0;
            }

            //sprintf(debuf, "cyc %U %U %U %U",start,end,step,dir);
            //rtlDebugOutString(debuf);
            for (i = start + step; i != end + step; i += step)
            {
                //char **p = &cells[dir ? sel_x : i][dir ? i : sel_end_y];
                //sprintf(debuf, "to %U %U dir %U copying '%S'",dir ? sel_x : i,dir ? i : sel_y,dir,source);
                //rtlDebugOutString(debuf);
                if (cells[dir ? sel_x : i][dir ? i : sel_y])
                {
                    freemem(cells[dir ? sel_x : i][dir ? i : sel_y]);
                }
                if (source)
                {
                    cells[dir ? sel_x : i][dir ? i : sel_y] = change_formula(source, dir ? 0 : (i - start), dir ? (i - start) : 0);
                    //cells[dir ? sel_x : i][dir ? i : sel_y] = (char *)allocmem(strlen(source) + 1);
                    //strcpy(cells[dir ? sel_x : i][dir ? i : sel_y], source);
                }
                else
                    cells[dir ? sel_x : i][dir ? i : sel_y] = NULL;
            }
        }
    }

    // а вот если параллельны...
    /*

    if (sel_x == sel_end_x && sel_x == old_end_x)
    {
    	pdir = 0;
    }
    if (sel_y == sel_end_y && sel_y == old_end_y)
    {
    	pdir = 1;
    }
    if (pdir != -1)
    {
    	// арифметическая прогрессия - если числа. и тупо размножитьт последнее, если нет

    	sprintf(debuf, "maybe arith dir %U", pdir);
    	rtlDebugOutString(debuf);

    	int is_arith = 1;
    	int gstep = pdir ? sign(old_end_y - sel_y) : sign(old_end_x - sel_x);
    	if (gstep == 0)
    		gstep = 1;

    	for (int i = pdir ? sel_y : sel_x; i != pdir ? (old_end_y + gstep) : (old_end_x + gstep); i++)
    	{
    		convert_error = 0;
    		sprintf(debuf,"cell %U %U", !pdir ? sel_x : i, !pdir ? i : sel_y);
    		rtlDebugOutString(debuf);
    		if (cells[!pdir ? sel_x : i][!pdir ? i : sel_y])
    		{
    			double d = atof(cells[!pdir ? sel_x : i][!pdir ? i : sel_y]);
    			if (convert_error)
    			{
    				rtlDebugOutString("failed arith");
    				is_arith = 0;
    				break;
    			}
    		}
    		else
    		{
    			is_arith = 0;
    			rtlDebugOutString("failed arith in null");
    			break;
    		}
    	}

    	double arith_first, arith_step;
    	if (is_arith)
    	{
    		rtlDebugOutString("really arith");
    		arith_first = atof(cells[sel_x][sel_y]);
    		arith_step = atof(cells[pdir ? sel_x : old_end_x][pdir ? sel_y : old_end_y]) - arith_first;
    		arith_first += arith_step * pdir ? abs(sel_end_x - old_end_x) : abs(sel_end_y - old_end_y);
    	}
    	else
    		rtlDebugOutString("none arith");

    	// собственно заполнение
    	for (i = pdir ? old_end_y : old_end_x; i != pdir ? (sel_end_y + gstep) : (sel_end_x + gstep); i++)
    	{
    		if (cells[pdir ? sel_x : i][pdir ? i : sel_y])
    			freemem(cells[pdir ? sel_x : i][pdir ? i : sel_y]);
    		if (is_arith)
    		{
    			cells[pdir ? sel_x : i][pdir ? i : sel_y] = ftoa(arith_first);
    			arith_first += arith_step;
    		}
    		else
    		{
    			if (cells[sel_x][sel_y])
    			{
    				cells[pdir ? sel_x : i][pdir ? i : sel_y] = (char*)allocmem(strlen(cells[sel_x][sel_y]) + 1);
    				strcpy(cells[pdir ? sel_x : i][pdir ? i : sel_y], cells[sel_x][sel_y]);
    			}
    		}
    	}
    }
    */

    calculate_values();
}