summaryrefslogtreecommitdiff
path: root/ieditor_gesture.pl
blob: fe5d68c42b118e2f91e074445439da6c40685470 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 *  This file contains minor patches to the select_editor_text_gesture
 *  class included with XPCE to bring the behavior closer to UI
 *  standards.
 *
 */

:- use_module(library(pce)).

% Override the default @editor_recogniser. This affects the default
% editor class too!

ieditor_gesture_override :-
	free(@editor_recogniser),
	new(@editor_recogniser, handler_group(new(ieditor_gesture))).

:- pce_begin_class(ieditor_gesture, select_editor_text_gesture).

% Get the index of the clicked character -- but if the user clicked
% close to the next character, get that character's index instead.
%
% (If the mouse is dragged above the editor, though, the pointer's x
% position should not be accounted for whatsoever.)

index(_, Ev, Index) :<-
	"Calculate index of clicked character."::
	get(Ev, receiver, Editor),
	get(Editor, image, Image),
	get(Image, index, Ev, Index0),
	get(Ev, y, Y_ev),
	(   Y_ev > 1,
	    get(Image, character_position, Index0, Point1),
	    get(Image, character_position, Index0 + 1, Point2),
	    get(Ev, x, X_ev),
	    get(Image, x, X_image),
	    get(Point1, x, X1),
	    get(Point2, x, X2),
	    D1 is X_ev - X_image - X1,
	    D2 is X2 - (X_ev - X_image),
	    D1 > abs(D2),
	    Index is Index0 + 1
	;   Index = Index0
	).

initiate(G, Ev:event) :->
	"Set caret and prepare for selectiong"::
	send(G, slot, down_position, Ev?position),
	get(Ev, receiver, Editor),
	send(G, slot, editor, Editor),
	get(G, index, Ev, Index),		% Use sane index.
	send(Editor, caret, Index),
	get(Ev, multiclick, Multi),
	selection_unit(Multi, Unit),
	send(G, slot, unit, Unit),
	(   Multi == single
	->  send(G, slot, origin, Index),
	    send(G, selecting, @off)
	;   send(G, selecting, @on)
	).

selection_unit(single, character).
selection_unit(double, word).
selection_unit(triple, line).

drag(G, Ev:event) :->
	"Extend the selection if selecting"::
	get(Ev, receiver, Editor),
	(   (   get(G, selecting, @on)
	    ->  true
	    ;   get(G, down_position, DownPos),
		get(Ev, position, EvPos),
		get(DownPos, distance, EvPos, D),
		D > 5				% Default is 25.
	    ->  send(G, selecting, @on)
	    )
	->  (   get(G, index, Ev, Index)	% Use sane index.
	    ->  (   get(G, unit, character)	% Fix mark after char selection.
		->  get(G, selection_save_mark, Editor, M),
		    send(Editor, selection_extend, Index),
		    send(Editor, mark, M)
		;   send(Editor, selection_extend, Index)
		)
	    ;   true
	    )
	;   true
	).

selection_save_mark(G, Editor, M) :<-
	(   (   get(Editor, mark_status, active)
	    ;   get(G, selecting, @on)
	    )
	->  get(Editor, mark, M)		% Already selecting.
	;   get(Editor, caret, C),
	    send(Editor, selection_origin, C),	% Start new selection.
	    M = C
	).

:- pce_end_class.

/*  Based on code included with XPCE --- The SWI-Prolog GUI toolkit

    Original file: xpce/prolog/boot/pce_editor.pl

    Author:        Jan Wielemaker and Anjo Anjewierden
    E-mail:        jan@swi.psy.uva.nl
    WWW:           http://www.swi.psy.uva.nl/projects/xpce/
    Copyright (c)  1985-2002, University of Amsterdam
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:

    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in
       the documentation and/or other materials provided with the
       distribution.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
*/