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:        jan@swi.psy.uva.nl
    5    WWW:           http://www.swi.psy.uva.nl/projects/xpce/
    6    Copyright (c)  2001-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_auto_window, []).   36:- use_module(library(pce)).   37
   38/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   39This   library   defines    the     classes    auto_sized_picture    and
   40auto_sized_dialog, subclasses of picture and dialog that decide on their
   41size and scrollbars depending on the size of the contents.
   42- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
   43
   44
   45:- pce_begin_class(auto_sized_picture, window,
   46                   "Window that automatically fits the contents").
   47
   48variable(border,   int := 10,   both, "Border around contents").
   49variable(max_size, size,        both, "Maximum size").
   50
   51class_variable(max_size, size,  size(700,500), "Maximum size").
   52
   53initialise(W, L:label=[name], D:display=[display]) :->
   54    send_super(W, initialise, L, @default, D).
   55
   56'_compute_desired_size'(W) :->
   57    get(W, bounding_box, BB),
   58    get(W, border, B),
   59    send(W, scroll_to, point(BB?x-B, BB?y-B)),
   60    get(BB, width, BW),
   61    get(BB, height, BH),
   62    get(W, max_size, size(MW, MH)),
   63    WW is min(BW + 2*B, MW),
   64    WH is min(BH + 2*B, MH),
   65    (   WH < BH + 2*B               % force SB to compute!?
   66    ->  (   WW < BW + 2*B
   67        ->  send(W, scrollbars, both)
   68        ;   send(W, scrollbars, vertical)
   69        )
   70    ;   (   WW < BW + 2*B
   71        ->  send(W, scrollbars, horizontal)
   72        ;   true
   73        )
   74    ),
   75    send(W, size, size(WW, WH)).
   76
   77:- pce_end_class.
   78
   79
   80:- pce_begin_class(auto_sized_dialog, dialog,
   81                   "Dialog with scroll-bars if the contents are too big").
   82
   83variable(max_size, size,        both, "Maximum size").
   84class_variable(max_size, size,  size(700,500), "Maximum size").
   85
   86initialise(W, L:label=[name], D:display=[display]) :->
   87    send_super(W, initialise, L, @default, D).
   88
   89'_compute_desired_size'(D) :->
   90    send_super(D, '_compute_desired_size'),
   91    get(D, ideal_width, BW),
   92    get(D, ideal_height, BH),
   93    get(D, max_size, size(MW, MH)),
   94    WW is min(BW, MW),
   95    WH is min(BH, MH),
   96    (   WH < BH
   97    ->  send(D, vertical_scrollbar, @on)
   98    ;   true
   99    ),
  100    (   WW < BW
  101    ->  send(D, horizontal_scrollbar, @on)
  102    ;   true
  103    ),
  104    send(D, set, @default, @default, WW, WH).
  105
  106:- pce_end_class