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-2011, University of Amsterdam
    7                              VU University Amsterdam
    8    All rights reserved.
    9
   10    Redistribution and use in source and binary forms, with or without
   11    modification, are permitted provided that the following conditions
   12    are met:
   13
   14    1. Redistributions of source code must retain the above copyright
   15       notice, this list of conditions and the following disclaimer.
   16
   17    2. Redistributions in binary form must reproduce the above copyright
   18       notice, this list of conditions and the following disclaimer in
   19       the documentation and/or other materials provided with the
   20       distribution.
   21
   22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   33    POSSIBILITY OF SUCH DAMAGE.
   34*/
   35
   36:- module(pce_principal,
   37          [ new/2, free/1,
   38
   39            send/2, send/3, send/4, send/5, send/6, send/7,
   40            send/8,
   41
   42            get/3, get/4, get/5, get/6, get/7, get/8,
   43
   44            send_class/3,
   45            get_class/4,
   46
   47            object/1, object/2,
   48
   49            pce_class/6,
   50            pce_lazy_send_method/3,
   51            pce_lazy_get_method/3,
   52            pce_uses_template/2,
   53
   54            pce_method_implementation/2,
   55
   56            pce_open/3,                 % +Object, +Mode, -Stream
   57            in_pce_thread/1,            % :Goal
   58            set_pce_thread/0,
   59            pce_dispatch/0,
   60
   61            pce_postscript_stream/1,    % -Stream
   62
   63            op(200, fy,  @),
   64            op(250, yfx, ?),
   65            op(800, xfx, :=)
   66          ]).   67
   68
   69:- meta_predicate
   70    send_class(+, +, :),
   71    send(+, :),
   72    send(+, :, +),
   73    send(+, :, +, +),
   74    send(+, :, +, +, +),
   75    send(+, :, +, +, +, +),
   76    send(+, :, +, +, +, +, +),
   77
   78    get_class(+, +, :, -),
   79    get(+, :, -),
   80    get(+, :, +, -),
   81    get(+, :, +, +, -),
   82    get(+, :, +, +, +, -),
   83    get(+, :, +, +, +, +, -),
   84    get(+, :, +, +, +, +, +, -),
   85
   86    new(?, :).   87
   88                /********************************
   89                *             HOME              *
   90                ********************************/
 pce_home(-Home) is det
True when Home is the home directory of XPCE.
   96pce_home(PceHome) :-
   97    absolute_file_name(pce('.'), PceHome,
   98                       [ file_type(directory),
   99                         file_errors(fail)
  100                       ]),
  101    exists_directory(PceHome),
  102    !.
  103pce_home(PceHome) :-
  104    getenv('XPCEHOME', PceHome),
  105    exists_directory(PceHome),
  106    !.
  107pce_home(PceHome) :-
  108    (   current_prolog_flag(xpce_version, Version),
  109        atom_concat('/xpce-', Version, Suffix)
  110    ;   Suffix = '/xpce'
  111    ),
  112    absolute_file_name(swi(Suffix), PceHome,
  113                       [ file_type(directory),
  114                         file_errors(fail)
  115                       ]),
  116    exists_directory(PceHome),
  117    !.
  118pce_home(PceHome) :-
  119    current_prolog_flag(saved_program, true),
  120    !,
  121    (   current_prolog_flag(home, PceHome)
  122    ->  true
  123    ;   current_prolog_flag(executable, Exe)
  124    ->  file_directory_name(Exe, PceHome)
  125    ;   PceHome = '.'
  126    ).
  127pce_home(_) :-
  128    throw(error(pce_error(no_home), _)).
  129
  130
  131                /********************************
  132                *           LOAD C-PART         *
  133                ********************************/
  134
  135init_pce :-
  136    catch(load_foreign_library(foreign(pl2xpce)),
  137          error(Error, _Context),           % suppress stack trace
  138          (   print_message(error, error(Error, _)),
  139              fail
  140          )),
  141    pce_home(Home),
  142    pce_init(Home),
  143    !,
  144    create_prolog_flag(xpce, true, []),
  145    thread_self(Me),
  146    assert(pce:pce_thread(Me)).
  147init_pce :-
  148    print_message(error, error(pce_error(init_failed), _)),
  149    halt(1).
  150
  151:- initialization(init_pce, now).  152
  153:- noprofile((send_implementation/3,
  154              get_implementation/4,
  155              send_class/3,
  156              get_class/4,
  157              new/2,
  158              send/2,
  159              get/3)).  160
  161
  162                /********************************
  163                *          PROLOG LAYER         *
  164                ********************************/
 free(+Ref) is det
Delete object if it exists.
  171free(Ref) :-
  172    object(Ref),
  173    !,
  174    send(Ref, free).
  175free(_).
 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...)).
  184send(Receiver, M:Selector, A1) :-
  185    functor(Message, Selector, 1),
  186    arg(1, Message, A1),
  187    send(Receiver, M:Message).
  188
  189send(Receiver, M:Selector, A1, A2) :-
  190    functor(Message, Selector, 2),
  191    arg(1, Message, A1),
  192    arg(2, Message, A2),
  193    send(Receiver, M:Message).
  194
  195send(Receiver, M:Selector, A1, A2, A3) :-
  196    functor(Message, Selector, 3),
  197    arg(1, Message, A1),
  198    arg(2, Message, A2),
  199    arg(3, Message, A3),
  200    send(Receiver, M:Message).
  201
  202send(Receiver, M:Selector, A1, A2, A3, A4) :-
  203    functor(Message, Selector, 4),
  204    arg(1, Message, A1),
  205    arg(2, Message, A2),
  206    arg(3, Message, A3),
  207    arg(4, Message, A4),
  208    send(Receiver, M:Message).
  209
  210send(Receiver, M:Selector, A1, A2, A3, A4, A5) :-
  211    functor(Message, Selector, 5),
  212    arg(1, Message, A1),
  213    arg(2, Message, A2),
  214    arg(3, Message, A3),
  215    arg(4, Message, A4),
  216    arg(5, Message, A5),
  217    send(Receiver, M:Message).
  218
  219send(Receiver, M:Selector, A1, A2, A3, A4, A5, A6) :-
  220    functor(Message, Selector, 6),
  221    arg(1, Message, A1),
  222    arg(2, Message, A2),
  223    arg(3, Message, A3),
  224    arg(4, Message, A4),
  225    arg(5, Message, A5),
  226    arg(6, Message, A6),
  227    send(Receiver, M:Message).
 get(+Object, :Selector, +Arg..., ?Rval) is semidet
See the comments with send/[3-12].
  234get(Receiver, M:Selector, A1, Answer) :-
  235    functor(Message, Selector, 1),
  236    arg(1, Message, A1),
  237    get(Receiver, M:Message, Answer).
  238
  239get(Receiver, M:Selector, A1, A2, Answer) :-
  240    functor(Message, Selector, 2),
  241    arg(1, Message, A1),
  242    arg(2, Message, A2),
  243    get(Receiver, M:Message, Answer).
  244
  245get(Receiver, M:Selector, A1, A2, A3, Answer) :-
  246    functor(Message, Selector, 3),
  247    arg(1, Message, A1),
  248    arg(2, Message, A2),
  249    arg(3, Message, A3),
  250    get(Receiver, M:Message, Answer).
  251
  252get(Receiver, M:Selector, A1, A2, A3, A4, Answer) :-
  253    functor(Message, Selector, 4),
  254    arg(1, Message, A1),
  255    arg(2, Message, A2),
  256    arg(3, Message, A3),
  257    arg(4, Message, A4),
  258    get(Receiver, M:Message, Answer).
  259
  260get(Receiver, M:Selector, A1, A2, A3, A4, A5, Answer) :-
  261    functor(Message, Selector, 5),
  262    arg(1, Message, A1),
  263    arg(2, Message, A2),
  264    arg(3, Message, A3),
  265    arg(4, Message, A4),
  266    arg(5, Message, A5),
  267    get(Receiver, M:Message, Answer).
  268
  269
  270                 /*******************************
  271                 *           NEW SEND           *
  272                 *******************************/
  273
  274:- multifile
  275    send_implementation/3,
  276    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.
  286send_implementation(true, _Args, _Obj).
  287send_implementation(fail, _Args, _Obj) :- fail.
  288send_implementation(once(Id), Args, Obj) :-
  289    send_implementation(Id, Args, Obj),
  290    !.
  291send_implementation(spy(Id), Args, Obj) :-
  292    (   current_prolog_flag(debug, true)
  293    ->  trace,
  294        send_implementation(Id, Args, Obj)
  295    ;   send_implementation(Id, Args, Obj)
  296    ).
  297send_implementation(trace(Id), Args, Obj) :-
  298    pce_info(pce_trace(enter, send_implementation(Id, Args, Obj))),
  299    (   send_implementation(Id, Args, Obj)
  300    ->  pce_info(pce_trace(exit, send_implementation(Id, Args, Obj)))
  301    ;   pce_info(pce_trace(fail, send_implementation(Id, Args, Obj)))
  302    ).
 get_implementation(+Id, +Message, +Object, -Return)
As send_implementation/3, but for get-methods.
  309get_implementation(true, _Args, _Obj, _Rval).
  310get_implementation(fail, _Args, _Obj, _Rval) :- fail.
  311get_implementation(once(Id), Args, Obj, Rval) :-
  312    get_implementation(Id, Args, Obj, Rval),
  313    !.
  314get_implementation(spy(Id), Args, Obj, Rval) :-
  315    (   current_prolog_flag(debug, true)
  316    ->  trace,
  317        get_implementation(Id, Args, Obj, Rval)
  318    ;   get_implementation(Id, Args, Obj, Rval)
  319    ).
  320get_implementation(trace(Id), Args, Obj, Rval) :-
  321    pce_info(pce_trace(enter, get_implementation(Id, Args, Obj, Rval))),
  322    (   get_implementation(Id, Args, Obj, Rval)
  323    ->  pce_info(pce_trace(exit, get_implementation(Id, Args, Obj, Rval)))
  324    ;   pce_info(pce_trace(fail, get_implementation(Id, Args, Obj, Rval))),
  325        fail
  326    ).
  327
  328%       SWI-Prolog: make this a normal user (debug-able) predicate.
  329
  330pce_ifhostproperty(prolog(swi), [
  331(:- unlock_predicate(send_implementation/3)),
  332(:- unlock_predicate(get_implementation/4)),
  333(:- '$set_predicate_attribute'(send_implementation(_,_,_),  hide_childs, false)),
  334(:- '$set_predicate_attribute'(get_implementation(_,_,_,_), hide_childs, false))
  335                   ]).
  336
  337
  338                 /*******************************
  339                 *          DECLARATIONS        *
  340                 *******************************/
  341
  342:- multifile
  343    pce_class/6,
  344    pce_lazy_send_method/3,
  345    pce_lazy_get_method/3,
  346    pce_uses_template/2.  347
  348
  349                 /*******************************
  350                 *            @PROLOG           *
  351                 *******************************/
  352
  353:- initialization
  354   (object(@prolog) -> true ; send(@host, name_reference, prolog)).