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)  1999-2011, 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(pce_nedit, []).   36:- use_module(library(pce)).   37:- use_module(library(pce_meta)).   38
   39:- multifile
   40    prolog_edit:locate/3,           % +Partial, -FullSpec, -Location
   41    prolog_edit:locate/2,
   42    prolog_edit:select_location/3.   43
   44
   45                 /*******************************
   46                 *      FINDING LOCATIONS       *
   47                 *******************************/
   48
   49prolog_edit:(locate(ClassName, class(ClassName), Location) :-
   50        locate(class(ClassName), Location)).
   51
   52prolog_edit:locate(class(ClassName), Location) :-       % class(Name)
   53    atom(ClassName),
   54    get(@pce, convert, ClassName, class, Class),
   55    \+ get(Class, creator, built_in),
   56    source(Class, Location).
   57prolog_edit:locate(SourceLoc, [file(File)|Extra]) :-
   58    object(SourceLoc),
   59    send(SourceLoc, instance_of, source_location),
   60    get(SourceLoc, file_name, File),
   61    (   get(SourceLoc, line_no, Line),
   62        Line \== @nil
   63    ->  Extra = [line(Line)]
   64    ;   Extra = []
   65    ).
   66prolog_edit:locate(Object, Location) :-                 % @reference
   67    source(Object, Location).
   68prolog_edit:locate(Object, Location) :-                 % @reference
   69    object(Object),
   70    get(Object, class, Class),
   71    source(Class, Location).
   72prolog_edit:locate(send(Receiver, Selector), Location) :-
   73    receiver_class(Receiver, Class),
   74    (   implements(Class, send(Selector), Method),
   75        source(Method, Location)
   76    ;   method_source(Class, send(Selector), Location)
   77    ).
   78prolog_edit:locate(get(Receiver, Selector), Location) :-
   79    receiver_class(Receiver, Class),
   80    (   implements(Class, get(Selector), Method),
   81        source(Method, Location)
   82    ;   method_source(Class, get(Selector), Location)
   83    ).
   84prolog_edit:(locate(->(Receiver, Selector), Location) :- !,
   85        locate(send(Receiver, Selector), Location)).
   86prolog_edit:(locate(<-(Receiver, Selector), Location) :- !,
   87        locate(get(Receiver, Selector), Location)).
   88
   89source(Object, [file(Path)|T]) :-
   90    object(Object),
   91    send(Object, has_get_method, source),
   92    get(Object, source, Loc),
   93    Loc \== @nil,
   94    get(Loc, file_name, FileName),
   95    absolute_file_name(FileName, Path),
   96    get(Loc, line_no, Line),
   97    (   integer(Line)
   98    ->  T = [line(Line)]
   99    ;   T = []
  100    ).
  101
  102receiver_class(Object, Class) :-
  103    object(Object),
  104    !,
  105    get(Object, class_name, Class).
  106receiver_class(Class, Class).
  107
  108method_source(ClassName, send(Selector), [file(File),line(Line)]) :-
  109    var(ClassName),
  110    pce_principal:pce_lazy_send_method(Selector, ClassName, Binder),
  111    arg(4, Binder, source_location(File, Line)),
  112    \+ get(@classes, member, ClassName, _).
  113method_source(ClassName, get(Selector), [file(File),line(Line)]) :-
  114    var(ClassName),
  115    pce_principal:pce_lazy_get_method(Selector, ClassName, Binder),
  116    arg(4, Binder, source_location(File, Line)),
  117    \+ get(@classes, member, ClassName, _).
  118
  119
  120                 /*******************************
  121                 *             SELECT           *
  122                 *******************************/
  123
  124%       Use GUI-based selection if the request comes from the GUI!
  125
  126prolog_edit:select_location(Pairs, _Spec, Location) :-
  127    Pairs \= [_],                                   % direct match
  128    object(@event),
  129    send(@event, instance_of, event),              % GUI initiated
  130    !,
  131    (   Pairs == []
  132    ->  send(@event?receiver, report, error, 'No match'),
  133        Location = []                               % Cancel
  134    ;   get(@event?receiver, frame, Frame),
  135        new(D, dialog('Select object to edit')),
  136        send(D, append, label(title, 'Click object to edit')),
  137        length(Pairs, Len),
  138        LBH is min(Len, 10),
  139        send(D, append, new(LB, list_browser(@default, 30, LBH))),
  140        fill_browser(Pairs, 1, LB),
  141        send(LB, select_message, message(D, return, LB?selection?object)),
  142        send(D, append,
  143             new(C, button(cancel, message(D, destroy)))),
  144        send(C, alignment, right),
  145        send(D, resize_message, message(D, layout, @arg2)),
  146        send(D, modal, transient),
  147        send(D, transient_for, Frame),
  148        (   get(D, confirm_centered, Frame?area?center, Rval)
  149        ->  send(D, destroy),
  150            Location = Rval
  151        ;   Location = []
  152        )
  153    ).
  154
  155fill_browser([], _, _).
  156fill_browser([Location-Spec|T], N, LB) :-
  157    message_to_string(edit(target(Location-Spec, N)), Label),
  158    send(LB, append,
  159         dict_item(Label, object := prolog(Location))),
  160    NN is N + 1,
  161    fill_browser(T, NN, LB)