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/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   36NOTE: Should also include colour attribute
   37- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
   38
   39:- module(pce_style_item, []).   40:- use_module(library(pce)).   41:- require([ default/3
   42           , forall/2
   43           ]).   44
   45:- pce_autoload(font_item,  library(pce_font_item)).   46:- pce_autoload(image_item, library(pce_image_item)).   47:- pce_autoload(tick_box,   library(pce_tick_box)).   48
   49style_attribute(highlight).
   50style_attribute(underline).
   51style_attribute(bold).
   52style_attribute(grey).
   53
   54:- pce_begin_class(style_item, figure, "Item to define/edit a style object").
   55
   56variable(message,       code*,  both, "Executed message").
   57variable(default,       style,  get,  "->restore value").
   58variable(selection,     style,  none, "Current style").
   59
   60initialise(SI, Label:name, Default:[style], Message:[message]) :->
   61    "Create from label, default and message"::
   62    send(SI, send_super, initialise),
   63    send(SI, name, Label),
   64    default(Message, @nil, Msg),
   65    default(Default, new(style), Def),
   66    send(SI, message, Msg),
   67    send(SI, append_dialog_item,
   68         tick_box(has_font, @off,
   69                  message(?(SI, member, font), active, @arg1))),
   70    send(SI, append_dialog_item, font_item(font), right),
   71    send(SI, append_dialog_item,
   72         tick_box(has_icon, @off,
   73                  message(?(SI, member, icon), active, @arg1))),
   74    send(SI, append_dialog_item, image_item(icon), right),
   75    send(SI, append_dialog_item, new(AI, menu(attributes, toggle)), below),
   76    forall(style_attribute(Att),
   77           send(AI, append, Att)),
   78    send(SI, layout_dialog, size(0,5)),
   79    send(SI, default, Def).
   80
   81get_attributes(Style, Attrs) :-
   82    new(Attrs, chain),
   83    forall(style_attribute(Att),
   84           (   get(Style, Att, @on)
   85           ->  send(Attrs, append, Att)
   86           ;   true
   87           )).
   88
   89set_attributes(Style, Attrs) :-
   90    forall(style_attribute(Att),
   91           (   send(Attrs, member, Att)
   92           ->  send(Style, Att, @on)
   93           ;   send(Style, Att, @off)
   94           )).
   95
   96
   97font(SI, Font:[font]) :->
   98    get(SI, member, has_font, Box),
   99    get(SI, member, font, FontItem),
  100    (   Font == @default
  101    ->  send(Box, selection, @off),
  102        send(FontItem, active, @off)
  103    ;   send(Box, selection, has_font),
  104        send(FontItem, active, @on),
  105        send(FontItem, selection, Font)
  106    ).
  107font(SI, Font:[font]) :<-
  108    get(SI, member, has_font, Box),
  109    (   get(Box, selected, has_font, @off)
  110    ->  Font = @default
  111    ;   get(SI, member, font, FontItem),
  112        get(FontItem, selection, Font)
  113    ).
  114
  115icon(SI, Icon:image*) :->
  116    get(SI, member, has_icon, Box),
  117    get(SI, member, icon, ImageItem),
  118    (   Icon == @nil
  119    ->  send(Box, selection, @off),
  120        send(ImageItem, active, @off)
  121    ;   send(Box, selection, has_icon),
  122        send(ImageItem, active, @on),
  123        send(ImageItem, selection, Icon)
  124    ).
  125icon(SI, Icon:image*) :<-
  126    get(SI, member, has_icon, Box),
  127    (   get(Box, selected, has_icon, @off)
  128    ->  Icon = @nil
  129    ;   get(SI, member, icon, ImageItem),
  130        get(ImageItem, selection, Icon)
  131    ).
  132
  133
  134selection(SI, Style:style) :->
  135    "Set the currently edited style object"::
  136    send(SI, slot, selection, Style),
  137    send(SI, font, Style?font),
  138    send(SI, icon, Style?icon),
  139    get(SI, member, attributes, AI),
  140    get_attributes(Style, Attrs),
  141    send(AI, selection, Attrs).
  142
  143
  144selection(SI, Style:style) :<-
  145    "Update -selection and return it"::
  146    get(SI, slot, selection, Style),
  147    send(Style, font, SI?font),
  148    send(Style, icon, SI?icon),
  149    get(SI, member, attributes, AI),
  150    get(AI, selection, Attrs),
  151    set_attributes(Style, Attrs).
  152
  153
  154default(SI, Style:style) :->
  155    "Set the <-restore value"::
  156    send(SI, slot, default, Style),
  157    send(SI, restore).
  158
  159
  160restore(SI) :->
  161    "Restore the selection from <-default"::
  162    send(SI, selection, SI?default).
  163
  164
  165apply(SI, _Always:[bool]) :->
  166    "Execute <-message with <-selection"::
  167    get(SI, message, Message),
  168    (   Message \== @nil
  169    ->  get(SI, selection, Style),
  170        send(Message, forward, Style)
  171    ;   true
  172    ).
  173
  174
  175:- pce_end_class