27 Oct 2012 00:35:57 UTC
- Distribution: Gtk2-Ex-WidgetBits
- Module version: 48
- Source (raw)
- Browse (raw)
- How to Contribute
- Issues (0)
- Testers (2330 / 50 / 0)
- KwaliteeBus factor: 1
- 42.85% Coverage
- License: open_source
- Perl: v5.8.0
- Activity24 month
- Download (136.17KB)
- MetaCPAN Explorer
- Subscribe to distribution
- This version
- Latest versionKRYDE Kevin Rydeand 1 contributors
- Kevin Ryde
Gtk2::Ex::Units -- widget sizes in various units
use Gtk2::Ex::Units; Gtk2::Ex::Units::set_default_size_with_subsizes ($dialog, [ $entry, '40 em' ], [ $textview, '20 em', '10 lines' ]); $pixels = Gtk2::Ex::Units::em($widget);
This is some functions for working with sizes of widgets etc expressed in units like em, line height, millimetres, etc.
The best feature is
set_default_size_with_subsizeswhich helps establish a sensible initial size for a dialog or toplevel window when it includes text entry widgets etc which don't have a desired size, or not when empty.
Nothing is exported by default, but the functions can be requested individually or with
:allin the usual way (see Exporter).
use Gtk2::Ex::Units qw(em ex);
$pixels = Gtk2::Ex::Units::width ($target, $str)
$pixels = Gtk2::Ex::Units::height ($target, $str)
Return a size in pixels on
$targetfor a string
10 chars # width of a average character 6 ems # width of an "M" character 1 digit # width of an average digit 0 to 9 2 ex # height of an "x" character 1 line # height of a line (baseline to baseline) 10 mm # millimetres, per screen size 2.5 inches # inches, per screen size 5 pixels # already pixels, just return 5 100 # no units, just return 100
Either singular like "inch" or plural "inches" can be given. The number part can include decimals, and the return may not be an integer.
"em", "ex", "char", "digit" and "line" follow the basic sizes functions below, for the font in
$target. For them
$targetcan be a
Gtk2::Widgetor a Pango layout
"mm" and "inch" are based on the screen size for
$target. For them
$targetcan be a
Gtk2::Gdk::Window, or anything with a
Gtk2::Gdk::Screen. In Gtk 2.0.x there's only one screen and
$targetis currently ignored in that case.
Currently "em" and "digit" are only for use as a width, and
lineonly for a height. In the future they may be supported on the opposite axis, perhaps based on what rotated text would look like. (The same pixels, or scaled if pixels aren't square?)
Gtk2::Ex::Units::set_default_size_with_subsizes ($toplevel, $subsize, ...)
$requisition = Gtk2::Ex::Units::size_request_with_subsizes ($widget, $subsize, ...)
Establish a widget size based on temporary forced sizes for some of its children. Generally the child widgets will be things like
Gtk2::Viewportwhich don't have a size while empty but where you want to allow room for likely contents.
$subsizeargument is an arrayref
[ $widget, $width, $height ]
$heightare put through the
heightfunctions above, so they can be either a count of pixels, or a string like "6 ems" or "10 lines".
-1means the widget's desired size in that axis (as usual for
undefmeans the current size request setting of that axis (ie. no change to it).
set_default_size_with_subsizesis for use on
Gtk2::Windowtoplevel or dialog widgets and applies the size to
$toplevel->set_default_size. This gives a good initial size for
$toplevel, but allows the user to expand or shrink later.
Gtk2::Ex::Units::set_default_size_with_subsizes ($dialog, [ $textview, '40 ems', '10 lines' ]);
size_request_with_subsizesis for use on any container widget and just returns a new
Gtk2::Requisitionwith the size determined.
In the following functions
$targetcan be a
$pixels = Gtk2::Ex::Units::em ($target)
$pixels = Gtk2::Ex::Units::ex ($target)
Return the width of an "M", or the height of an "x", in pixels, for
Currently an em includes inter-character spacing, so that "3 ems" makes room for "MMM", but an ex is just the inked height of that character.
$pixels = Gtk2::Ex::Units::char_width ($target)
$pixels = Gtk2::Ex::Units::digit_width ($target)
Return the average width in pixels of a character or just a digit (0-9), for
Currently these are per Pango's
get_approximate_digit_width. In a proportional font some characters or some of the digits may be wider than the average.
$pixels = Gtk2::Ex::Units::line_height ($target)
Return the height of a line, in pixels, for
$target. This the height of the glyphs in the target font, plus any Pango line spacing per (
Copyright 2007, 2008, 2009, 2010, 2011, 2012 Kevin Ryde
Gtk2-Ex-WidgetBits is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version.
Gtk2-Ex-WidgetBits is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with Gtk2-Ex-WidgetBits. If not, see http://www.gnu.org/licenses/.
Module Install Instructions
To install Gtk2::Ex::WidgetBits, copy and paste the appropriate command in to your terminal.
perl -MCPAN -e shell install Gtk2::Ex::WidgetBits
For more information on module installation, please visit the detailed CPAN module installation guide.