View source with raw comments or as raw
    1/*  Part of XPCE --- The SWI-Prolog GUI toolkit
    2
    3    Author:        Jan Wielemaker and Anjo Anjewierden
    4    E-mail:        jan@swi.psy.uva.nl
    5    WWW:           http://www.swi.psy.uva.nl/projects/xpce/
    6    Copyright (c)  1985-2002, University of Amsterdam
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   34
   35:- module(editor_buttons, []).   36:- use_module(pce_boot(pce_principal)).   37:- use_module(pce_boot(pce_realise),
   38              [ pce_register_class/1,
   39                pce_begin_class_definition/4
   40              ]).   41
   42make_editor_recogniser(G) :-
   43    new(Editor, @event?receiver),
   44    new(G, handler_group(new(select_editor_text_gesture),
   45                         click_gesture(middle, '', single,
   46                                       and(message(Editor, paste, primary),
   47                                           message(Editor, mark_undo))))).
   48
   49/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   50This module defines @editor_recogniser, a recogniser called from
   51
   52Parts of the specs by Uwe Lesta.
   53- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
   54
   55:- pce_begin_class(select_editor_text_gesture, gesture,
   56                   "Select text in an editor").
   57
   58variable(selecting,     bool := @off,   get, "Are we making a selection").
   59variable(down_position, point*,         get, "Position of down-event").
   60variable(origin,        int*,           get, "Index of down event").
   61variable(unit,          {character,word,line}, get, "Selection unit").
   62variable(editor,        editor*,        get, "Client object").
   63
   64initialise(G) :->
   65    send_super(G, initialise),
   66    send(G, slot, unit, character),
   67    send(G, drag_scroll, self).
   68
   69
   70initiate(G, Ev:event) :->
   71    "Set caret and prepare for selectiong"::
   72    send(G, slot, down_position, Ev?position),
   73    get(Ev, receiver, Editor),
   74    send(G, slot, editor, Editor),
   75    get(Editor, image, Image),
   76    get(Image, index, Ev, Index),
   77    send(Editor, caret, Index),
   78    get(Ev, multiclick, Multi),
   79    selection_unit(Multi, Unit),
   80    send(G, slot, unit, Unit),
   81    (   Multi == single
   82    ->  send(G, slot, origin, Index),
   83        send(G, selecting, @off)
   84    ;   send(G, selecting, @on)
   85    ).
   86
   87selection_unit(single, character).
   88selection_unit(double, word).
   89selection_unit(triple, line).
   90
   91
   92selecting(G, Val:bool) :->
   93    "Start/stop selecting"::
   94    send(G, slot, selecting, Val),
   95    get(G, editor, Editor),
   96    (   Val == @on
   97    ->  get(G, origin, Origin), Origin \== @nil,
   98        send(Editor, selection_unit, G?unit),
   99        send(Editor, selection_origin, Origin)
  100    ;   send(Editor, mark_status, inactive)
  101    ).
  102
  103
  104drag(G, Ev:event) :->
  105    "Extend the selection if selecting"::
  106    (   (   get(G, selecting, @on)
  107        ->  true
  108        ;   get(G, down_position, DownPos),
  109            get(Ev, position, EvPos),
  110            get(DownPos, distance, EvPos, D),
  111            D > 25
  112        ->  send(G, selecting, @on)
  113        )
  114    ->  get(Ev, receiver, Editor),
  115        get(Editor, image, Image),
  116        (   get(Image, index, Ev, Index)
  117        ->  send(Editor, selection_extend, Index)
  118        ;   true
  119        )
  120    ;   true
  121    ).
  122
  123terminate(G, _Ev:event) :->
  124    "If we are selecting, copy the selection"::
  125    get(G, editor, Editor),
  126    send(G, slot, editor, @nil),
  127    (   get(G, selecting, @on),
  128        get(Editor, class_variable_value, auto_copy, @on)
  129    ->  send(Editor, copy)
  130    ;   true
  131    ).
  132
  133:- pce_end_class.
  134
  135:- free(@editor_recogniser).  136:- initialization
  137    make_editor_recogniser(@editor_recogniser).