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:        J.Wielemaker@cs.nu.nl
    5    WWW:           http://www.swi-prolog.nl/projects/xpce/
    6    Copyright (c)  1985-2019, University of Amsterdam
    7                              VU University Amsterdam
    8                              CWI, Amsterdam
    9    All rights reserved.
   10
   11    Redistribution and use in source and binary forms, with or without
   12    modification, are permitted provided that the following conditions
   13    are met:
   14
   15    1. Redistributions of source code must retain the above copyright
   16       notice, this list of conditions and the following disclaimer.
   17
   18    2. Redistributions in binary form must reproduce the above copyright
   19       notice, this list of conditions and the following disclaimer in
   20       the documentation and/or other materials provided with the
   21       distribution.
   22
   23    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   24    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   25    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   26    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   27    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   28    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   29    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   30    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   31    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   32    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   33    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   34    POSSIBILITY OF SUCH DAMAGE.
   35*/
   36
   37:- module(pce_principal,
   38          [ new/2, free/1,
   39
   40            send/2, send/3, send/4, send/5, send/6, send/7,
   41            send/8,
   42
   43            get/3, get/4, get/5, get/6, get/7, get/8,
   44
   45            send_class/3,
   46            get_class/4,
   47
   48            object/1, object/2,
   49
   50            pce_class/6,
   51            pce_lazy_send_method/3,
   52            pce_lazy_get_method/3,
   53            pce_uses_template/2,
   54
   55            pce_method_implementation/2,
   56
   57            pce_open/3,                 % +Object, +Mode, -Stream
   58            in_pce_thread/1,            % :Goal
   59            set_pce_thread/0,
   60            pce_dispatch/0,
   61
   62            pce_postscript_stream/1,    % -Stream
   63
   64            op(200, fy,  @),
   65            op(250, yfx, ?),
   66            op(800, xfx, :=)
   67          ]).   68:- autoload(library(apply),[convlist/3,maplist/2]).   69:- autoload(library(lists),[member/2,last/2,reverse/2]).   70:- autoload(library(shlib),[load_foreign_library/1]).   71:- autoload(library(swi_compatibility),[pce_info/1]).   72:- autoload(library(system),[unlock_predicate/1]).   73
   74:- meta_predicate
   75    send_class(+, +, :),
   76    send(+, :),
   77    send(+, :, +),
   78    send(+, :, +, +),
   79    send(+, :, +, +, +),
   80    send(+, :, +, +, +, +),
   81    send(+, :, +, +, +, +, +),
   82
   83    get_class(+, +, :, -),
   84    get(+, :, -),
   85    get(+, :, +, -),
   86    get(+, :, +, +, -),
   87    get(+, :, +, +, +, -),
   88    get(+, :, +, +, +, +, -),
   89    get(+, :, +, +, +, +, +, -),
   90
   91    new(?, :).   92
   93                /********************************
   94                *             HOME              *
   95                ********************************/
 pce_home(-Home) is det
True when Home is the home directory of XPCE.
  101pce_home(PceHome) :-
  102    absolute_file_name(pce('.'), PceHome,
  103                       [ file_type(directory),
  104                         file_errors(fail)
  105                       ]),
  106    exists_directory(PceHome),
  107    !.
  108pce_home(PceHome) :-
  109    getenv('XPCEHOME', PceHome),
  110    exists_directory(PceHome),
  111    !.
  112pce_home(PceHome) :-
  113    (   current_prolog_flag(xpce_version, Version),
  114        atom_concat('/xpce-', Version, Suffix)
  115    ;   Suffix = '/xpce'
  116    ),
  117    absolute_file_name(swi(Suffix), PceHome,
  118                       [ file_type(directory),
  119                         file_errors(fail)
  120                       ]),
  121    exists_directory(PceHome),
  122    !.
  123pce_home(PceHome) :-
  124    current_prolog_flag(saved_program, true),
  125    !,
  126    (   current_prolog_flag(home, PceHome)
  127    ->  true
  128    ;   current_prolog_flag(executable, Exe)
  129    ->  file_directory_name(Exe, PceHome)
  130    ;   PceHome = '.'
  131    ).
  132pce_home(_) :-
  133    throw(error(pce_error(no_home), _)).
 xpce_application_dir(-Dir)
Set the directory for storing user XPCE configuration and data.
  139xpce_application_dir(Dir) :-
  140    create_config_directory(user_app_config(xpce), Dir),
  141    !.
  142xpce_application_dir(Dir) :-
  143    expand_file_name('~/.xpce', [Dir]).
 create_config_directory(+Alias, -Dir) is semidet
Try to find an existing config directory or create a writeable config directory below a directory owned by this process. If there are multiple possibilities, create the one that requires the least number of new directories.
  153create_config_directory(Alias, Dir) :-
  154    member(Access, [write, read]),
  155    absolute_file_name(Alias, Dir0,
  156                       [ file_type(directory),
  157                         access(Access),
  158                         file_errors(fail)
  159                       ]),
  160    !,
  161    Dir = Dir0.
  162create_config_directory(Alias, Dir) :-
  163    findall(Candidate,
  164            absolute_file_name(Alias, Candidate,
  165                               [ solutions(all),
  166                                 file_errors(fail)
  167                               ]),
  168            Candidates),
  169    convlist(missing, Candidates, Paths),
  170    member(_-Create, Paths),
  171    catch(maplist(make_directory, Create), _, fail),
  172    !,
  173    last(Create, Dir).
  174
  175missing(Dir, Len-Create) :-
  176    missing_(Dir, Create0),
  177    reverse(Create0, Create),
  178    length(Create, Len).
  179
  180missing_(Dir, []) :-
  181    exists_directory(Dir),
  182    access_file(Dir, write),
  183    '$my_file'(Dir),
  184    !.
  185missing_(Dir, [Dir|T]) :-
  186    file_directory_name(Dir, Parent),
  187    Parent \== Dir,
  188    missing_(Parent, T).
  189
  190
  191                /********************************
  192                *           LOAD C-PART         *
  193                ********************************/
  194
  195init_pce :-
  196    catch(load_foreign_library(foreign(pl2xpce)),
  197          error(Error, _Context),           % suppress stack trace
  198          (   print_message(error, error(Error, _)),
  199              fail
  200          )),
  201    pce_home(Home),
  202    xpce_application_dir(AppDir),
  203    pce_init(Home, AppDir),
  204    !,
  205    create_prolog_flag(xpce, true, []),
  206    thread_self(Me),
  207    assert(pce:pce_thread(Me)).
  208init_pce :-
  209    print_message(error, error(pce_error(init_failed), _)),
  210    halt(1).
  211
  212:- initialization(init_pce, now).  213
  214:- noprofile((send_implementation/3,
  215              get_implementation/4,
  216              send_class/3,
  217              get_class/4,
  218              new/2,
  219              send/2,
  220              get/3)).  221
  222
  223                /********************************
  224                *          PROLOG LAYER         *
  225                ********************************/
 free(+Ref) is det
Delete object if it exists.
  232free(Ref) :-
  233    object(Ref),
  234    !,
  235    send(Ref, free).
  236free(_).
 send(+Object, +Selector, +Arg...) is semidet
Succeeds if sending a message to Object with Selector and the given Arguments succeeds. Normally, goal_expansion/2 expands all these goals into send(Receiver, Method(Args...)).
  245send(Receiver, M:Selector, A1) :-
  246    functor(Message, Selector, 1),
  247    arg(1, Message, A1),
  248    send(Receiver, M:Message).
  249
  250send(Receiver, M:Selector, A1, A2) :-
  251    functor(Message, Selector, 2),
  252    arg(1, Message, A1),
  253    arg(2, Message, A2),
  254    send(Receiver, M:Message).
  255
  256send(Receiver, M:Selector, A1, A2, A3) :-
  257    functor(Message, Selector, 3),
  258    arg(1, Message, A1),
  259    arg(2, Message, A2),
  260    arg(3, Message, A3),
  261    send(Receiver, M:Message).
  262
  263send(Receiver, M:Selector, A1, A2, A3, A4) :-
  264    functor(Message, Selector, 4),
  265    arg(1, Message, A1),
  266    arg(2, Message, A2),
  267    arg(3, Message, A3),
  268    arg(4, Message, A4),
  269    send(Receiver, M:Message).
  270
  271send(Receiver, M:Selector, A1, A2, A3, A4, A5) :-
  272    functor(Message, Selector, 5),
  273    arg(1, Message, A1),
  274    arg(2, Message, A2),
  275    arg(3, Message, A3),
  276    arg(4, Message, A4),
  277    arg(5, Message, A5),
  278    send(Receiver, M:Message).
  279
  280send(Receiver, M:Selector, A1, A2, A3, A4, A5, A6) :-
  281    functor(Message, Selector, 6),
  282    arg(1, Message, A1),
  283    arg(2, Message, A2),
  284    arg(3, Message, A3),
  285    arg(4, Message, A4),
  286    arg(5, Message, A5),
  287    arg(6, Message, A6),
  288    send(Receiver, M:Message).
 get(+Object, :Selector, +Arg..., ?Rval) is semidet
See the comments with send/[3-12].
  295get(Receiver, M:Selector, A1, Answer) :-
  296    functor(Message, Selector, 1),
  297    arg(1, Message, A1),
  298    get(Receiver, M:Message, Answer).
  299
  300get(Receiver, M:Selector, A1, A2, Answer) :-
  301    functor(Message, Selector, 2),
  302    arg(1, Message, A1),
  303    arg(2, Message, A2),
  304    get(Receiver, M:Message, Answer).
  305
  306get(Receiver, M:Selector, A1, A2, A3, Answer) :-
  307    functor(Message, Selector, 3),
  308    arg(1, Message, A1),
  309    arg(2, Message, A2),
  310    arg(3, Message, A3),
  311    get(Receiver, M:Message, Answer).
  312
  313get(Receiver, M:Selector, A1, A2, A3, A4, Answer) :-
  314    functor(Message, Selector, 4),
  315    arg(1, Message, A1),
  316    arg(2, Message, A2),
  317    arg(3, Message, A3),
  318    arg(4, Message, A4),
  319    get(Receiver, M:Message, Answer).
  320
  321get(Receiver, M:Selector, A1, A2, A3, A4, A5, Answer) :-
  322    functor(Message, Selector, 5),
  323    arg(1, Message, A1),
  324    arg(2, Message, A2),
  325    arg(3, Message, A3),
  326    arg(4, Message, A4),
  327    arg(5, Message, A5),
  328    get(Receiver, M:Message, Answer).
  329
  330
  331                 /*******************************
  332                 *           NEW SEND           *
  333                 *******************************/
  334
  335:- multifile
  336    send_implementation/3,
  337    get_implementation/4.
 send_implementation(+Id, +Message, +Object)
Method-bodies are compiled into clauses for this predicate. Id is a unique identifier for the implementation, Message is a compound whose functor is the method name and whose arguments are the arguments to the method-call. Object is the receiving object.
  347send_implementation(true, _Args, _Obj).
  348send_implementation(fail, _Args, _Obj) :- fail.
  349send_implementation(once(Id), Args, Obj) :-
  350    send_implementation(Id, Args, Obj),
  351    !.
  352send_implementation(spy(Id), Args, Obj) :-
  353    (   current_prolog_flag(debug, true)
  354    ->  trace,
  355        send_implementation(Id, Args, Obj)
  356    ;   send_implementation(Id, Args, Obj)
  357    ).
  358send_implementation(trace(Id), Args, Obj) :-
  359    pce_info(pce_trace(enter, send_implementation(Id, Args, Obj))),
  360    (   send_implementation(Id, Args, Obj)
  361    ->  pce_info(pce_trace(exit, send_implementation(Id, Args, Obj)))
  362    ;   pce_info(pce_trace(fail, send_implementation(Id, Args, Obj)))
  363    ).
 get_implementation(+Id, +Message, +Object, -Return)
As send_implementation/3, but for get-methods.
  370get_implementation(true, _Args, _Obj, _Rval).
  371get_implementation(fail, _Args, _Obj, _Rval) :- fail.
  372get_implementation(once(Id), Args, Obj, Rval) :-
  373    get_implementation(Id, Args, Obj, Rval),
  374    !.
  375get_implementation(spy(Id), Args, Obj, Rval) :-
  376    (   current_prolog_flag(debug, true)
  377    ->  trace,
  378        get_implementation(Id, Args, Obj, Rval)
  379    ;   get_implementation(Id, Args, Obj, Rval)
  380    ).
  381get_implementation(trace(Id), Args, Obj, Rval) :-
  382    pce_info(pce_trace(enter, get_implementation(Id, Args, Obj, Rval))),
  383    (   get_implementation(Id, Args, Obj, Rval)
  384    ->  pce_info(pce_trace(exit, get_implementation(Id, Args, Obj, Rval)))
  385    ;   pce_info(pce_trace(fail, get_implementation(Id, Args, Obj, Rval))),
  386        fail
  387    ).
  388
  389%       SWI-Prolog: make this a normal user (debug-able) predicate.
  390
  391pce_ifhostproperty(prolog(swi), [
  392(:- unlock_predicate(send_implementation/3)),
  393(:- unlock_predicate(get_implementation/4)),
  394(:- '$set_predicate_attribute'(send_implementation(_,_,_),  hide_childs, false)),
  395(:- '$set_predicate_attribute'(get_implementation(_,_,_,_), hide_childs, false))
  396                   ]).
  397
  398
  399                 /*******************************
  400                 *          DECLARATIONS        *
  401                 *******************************/
  402
  403:- multifile
  404    pce_class/6,
  405    pce_lazy_send_method/3,
  406    pce_lazy_get_method/3,
  407    pce_uses_template/2.  408
  409
  410                 /*******************************
  411                 *            @PROLOG           *
  412                 *******************************/
  413
  414:- initialization
  415   (object(@prolog) -> true ; send(@host, name_reference, prolog)).