/*

    Minimum Profit 5.x
    A Programmer's Text Editor

    Clipboard routines.

    Copyright (C) 1991-2007 Angel Ortega <angel@triptico.com>

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

    http://www.triptico.com

*/

/* editor actions */

mp.actions['unmark']	= sub (d) { mp.unmark(d); };
mp.actions['mark']	= sub (d) { mp.mark(d); };
mp.actions['copy_mark']	= sub (d) {
					mp.busy(1);
					mp.copy(d);
					mp.unmark(d);
					mp.drv.clip_to_sys();
					mp.busy(0);
				};

mp.actions['paste_mark'] = sub (d) {
					mp.busy(1);
					mp.store_undo(d);
					mp.drv.sys_to_clip();
					mp.paste(d);
					mp.busy(0);
				};

mp.actions['cut_mark']	= sub (d) {
					mp.busy(1);
					mp.store_undo(d);
					mp.cut(d);
					mp.drv.clip_to_sys();
					mp.busy(0);
				};

mp.actions['mouse_drag_mark'] = sub (d) {

	/* no selection yet? move to initial click and mark */
	if (d.txt.mark == NULL)
		mp.mark(d);

	/* move to drag position */
	mp.move_to_coords_xy(d, mp.mouse_to_x, mp.mouse_to_y);

	/* and mark */
	mp.mark(d);
};

/* default key bindings */

mp.keycodes['f8']		= "unmark";
mp.keycodes['f9']		= "mark";
mp.keycodes['ctrl-c']		= "copy_mark";
mp.keycodes['ctrl-v']		= "paste_mark";
mp.keycodes['ctrl-x']		= "cut_mark";
mp.keycodes['mouse-drag']	= "mouse_drag_mark";

/* action descriptions */
mp.actdesc['unmark']		= LL("Unmark block");
mp.actdesc['mark']		= LL("Mark beginning/end of block");
mp.actdesc['copy_mark']		= LL("Copy block");
mp.actdesc['paste_mark']	= LL("Paste block");
mp.actdesc['cut_mark']		= LL("Cut block");
mp.actdesc['mouse_drag_mark']	= LL("Mark using mouse dragging");

/* code */

sub mp.unmark(doc)
/* unmarks the block */
{
	/* just destroy the mark */
	doc.txt.mark = NULL;
}


sub mp.mark(doc)
/* marks the start or end of the block */
{
	local txt = doc.txt;

	if (txt.mark == NULL) {
		/* no mark; create one */
		txt.mark = {};
		txt.mark.ax = txt.mark.bx = txt.mark.ex = txt.x;
		txt.mark.ay = txt.mark.by = txt.mark.ey = txt.y;
	}
	else {
		/* mark exists; extend current one */
		if (txt.y < txt.mark.ay ||
			(txt.y == txt.mark.ay && txt.x < txt.mark.ax)) {
			/* move the beginning of the block */
			txt.mark.bx = txt.x;
			txt.mark.by = txt.y;
			txt.mark.ex = txt.mark.ax;
			txt.mark.ey = txt.mark.ay;
		}
		else {
			/* move the end of the block */
			txt.mark.ex = txt.x;
			txt.mark.ey = txt.y;
			txt.mark.bx = txt.mark.ax;
			txt.mark.by = txt.mark.ay;
		}
	}
}


sub mp.get_active_area(doc)
/* returns the active area: the selection or the full document */
{
	local m;

	if ((m = doc.txt.mark) == NULL)
		return doc.txt.lines;
	else
		return mp.get_range(doc, m.bx, m.by, m.ex, m.ey);
}


sub mp.copy(doc)
/* copies the mark to the clipboard */
{
	if (doc.txt.mark)
		mp.clipboard = mp.get_active_area(doc);
}


sub mp.delete_mark(doc)
/* deletes current selection */
{
	local txt = doc.txt;

	/* no mark? done */
	if (txt.mark == NULL)
		return;

	/* deletes the range */
	mp.delete_range(doc,
		txt.mark.bx, txt.mark.by, txt.mark.ex, txt.mark.ey);

	mp.unmark(doc);
}


sub mp.cut(doc)
/* cut (copy + delete) selected mark */
{
	mp.copy(doc);
	mp.delete_mark(doc);
}


sub mp.paste(doc)
/* pastes from the clipboard into a text */
{
	local t = mp.config.auto_indent;
	mp.config.auto_indent = 0;

	mp.insert(doc, mp.clipboard);

	mp.config.auto_indent = t;
}
