View source with formatted 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@vu.nl
    5    WWW:           http://www.swi-prolog.org/packages/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/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   38Module PCE.  This module defines the core   of  XPCE.  It is designed in
   39such a way that it  may  be   compiled  using  the SWI-Prolog qcompile/1
   40compiler, which makes XPCE an autoloadable module of SWI-Prolog.
   41
   42Various things are Prolog-implementation specific in this module and
   43therefore each Prolog system will require a different version of this
   44module.
   45
   46This module only defines some  paths,  some   things  to  make  the .qlf
   47compiler work on it and  finally  it   just  loads  the XPCE modules and
   48reexports the content of these files.
   49- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
   50
   51:- module(pce,
   52          [ new/2, free/1,              % pce_principal predicates
   53
   54            send/2, send/3, send/4, send/5, send/6, send/7,
   55            send/8,
   56
   57            get/3, get/4, get/5, get/6, get/7, get/8,
   58
   59            send_class/3,
   60            get_class/4,
   61            object/1, object/2,
   62
   63            pce_global/2,               % pce_global
   64            pce_autoload/2,             % pce_autoload
   65            pce_autoload_all/0,
   66
   67            pce_term_expansion/2,
   68            pce_compiling/1,            % -Class
   69            pce_compiling/2,            % -Class, -Path
   70            pce_begin_recording/1,
   71            pce_end_recording/0,
   72
   73            pce_register_class/1,
   74            pce_extended_class/1,
   75            pce_begin_class_definition/4,
   76            pce_prolog_class/1,
   77            pce_prolog_class/2,
   78
   79            pce_catch_error/2,          % pce_error
   80            pce_open/3,
   81            in_pce_thread/1,            % :Goal
   82            in_pce_thread_sync/1,       % :Goal
   83            set_pce_thread/0,
   84            pce_thread/1,               % -Thread
   85            pce_dispatch/0,
   86
   87            op(200, fy,  @),
   88            op(250, yfx, ?),
   89            op(800, xfx, :=)
   90          ]).   91
   92:- multifile
   93    on_load/0.   94
   95:- set_prolog_flag(generate_debug_info, false).   96
   97:- meta_predicate
   98    in_pce_thread_sync(0).   99
  100                /********************************
  101                *      LOAD COMMON PLATFORM     *
  102                ********************************/
  103
  104:- multifile user:file_search_path/2.  105
  106user:file_search_path(pce_boot, pce(prolog/boot)).
  107
  108:- load_files([ pce_boot(pce_expand),
  109                pce_boot(pce_pl),
  110                pce_boot(pce_principal),
  111                pce_boot(pce_error),
  112                pce_boot(pce_global),
  113                pce_boot(pce_expansion),
  114                pce_boot(pce_realise),
  115                pce_boot(pce_goal_expansion),
  116                pce_boot(pce_autoload),
  117                pce_boot(pce_editor),
  118                pce_boot(pce_keybinding),
  119                pce_boot(pce_portray)
  120              ],
  121              [ qcompile(part),         % compile boot files as part of pce.qlf
  122                silent(true)
  123              ]).  124:- use_module(pce_dispatch).  125
  126%!  pce_thread(-Thread) is det.
  127%
  128%   True if Thread is the Prolog thread that runs the graphics
  129%   message loop.
  130%
  131%   @see pce_dispatch/1.
  132
  133:- current_prolog_flag(threads, HasThreads),
  134   create_prolog_flag(xpce_threaded, HasThreads, [keep(true)]).  135
  136:- dynamic
  137    pce_thread/1.  138
  139start_dispatch :-
  140    (   current_prolog_flag(xpce_threaded, true)
  141    ->  pce_dispatch([])
  142    ;   true
  143    ).
  144
  145%!  in_pce_thread_sync(:Goal) is semidet.
  146%
  147%   Same as in_pce_thread/1, but wait  for   Goal  to  be completed.
  148%   Success depends on the success of executing Goal. If Goal throws
  149%   an exception, this exception is re-thrown by in_pce_thread/1.
  150%
  151%   Possible bindings of Goal are returned,   but  be aware that the
  152%   term has been _copied_. If in_pce_thread_sync/1 is called in the
  153%   thread running pce, it behaves as once/1.
  154
  155in_pce_thread_sync(Goal) :-
  156    thread_self(Me),
  157    pce_thread(Me),
  158    !,
  159    Goal,
  160    !.
  161in_pce_thread_sync(Goal) :-
  162    term_variables(Goal, Vars),
  163    pce_principal:in_pce_thread_sync2(Goal-Vars, Vars).
  164
  165:- initialization
  166    start_dispatch.  167
  168set_version :-
  169    current_prolog_flag(version_data, swi(Major, Minor, Patch, _)),
  170    format(string(PlId),
  171           'SWI-Prolog version ~w.~w.~w', [Major, Minor, Patch]),
  172    send(@prolog, system, PlId).
  173
  174:- initialization set_version.  175
  176get_pce_version :-
  177    (   current_prolog_flag(xpce_version, _)
  178    ->  true
  179    ;   get(@pce, version, name, Version),
  180        create_prolog_flag(xpce_version, Version, [])
  181    ).
  182
  183:- initialization get_pce_version.  184
  185run_on_load :-
  186    forall(on_load, true).
  187
  188:- initialization run_on_load.  189
  190
  191                 /*******************************
  192                 *           CONSOLE            *
  193                 *******************************/
  194
  195%:- send(@pce, console_label, 'XPCE/SWI-Prolog').
  196
  197
  198                /********************************
  199                *       PROLOG LIBRARIES        *
  200                ********************************/
  201
  202:- multifile
  203    user:file_search_path/2.  204
  205user:file_search_path(demo,    pce('prolog/demo')).
  206user:file_search_path(contrib, pce('prolog/contrib')).
  207user:file_search_path(image,   pce(bitmaps)).
  208
  209
  210                 /*******************************
  211                 *            HOOKS             *
  212                 *******************************/
  213
  214:- use_module(library(swi_hooks)).  215
  216                 /*******************************
  217                 *         EDIT HOOKS           *
  218                 *******************************/
  219
  220%       make sure SWI-Prolog edit/0 loads the XPCE edit hooks.
  221
  222:- multifile
  223    prolog_edit:load/0,
  224    prolog:locate_clauses/2.  225
  226prolog_edit:load :-
  227    ensure_loaded(library(swi_edit)).
  228
  229                 /*******************************
  230                 *          LIST HOOKS          *
  231                 *******************************/
  232
  233%!  prolog:locate_clauses(Term, Refs)
  234%
  235%   Locate a list of clause-references from a method-specification
  236%   like Class->Method.
  237%
  238%   see library(listing).
  239
  240prolog:locate_clauses(Term, Refs) :-
  241    (   Term = ->(_,_)
  242    ;   Term = <-(_,_)
  243    ),
  244    !,
  245    findall(R, method_clause(Term, R), Refs).
  246
  247match_id(->(Class, Method), Id) :-
  248    atomic(Class), atomic(Method),
  249    !,
  250    atomic_list_concat([Class, (->), Method], Id).
  251match_id(->(_Class, _Method), _Id).
  252match_id(<-(Class, Method), Id) :-
  253    atomic(Class), atomic(Method),
  254    !,
  255    atomic_list_concat([Class, (<-), Method], Id).
  256match_id(<-(_Class, _Method), _Id).
  257
  258method_clause(->(Class, Send), Ref) :-
  259    match_id((Class->Send), Id),
  260    clause(pce_principal:send_implementation(Id, _M, _O), _B, Ref),
  261    atom(Id),
  262    atomic_list_concat([Class,Send], '->', Id).
  263method_clause(<-(Class, Get), Ref) :-
  264    match_id(<-(Class, Get), Id),
  265    clause(pce_principal:get_implementation(Id, _M, _O, _R), _B, Ref),
  266    atom(Id),
  267    atomic_list_concat([Class,Get], '->', Id)