| 1 | package Tk::XFontSelect; | ||
| 2 | $VERSION=0.43; | ||
| 3 | # This is a kludge to make the CPAN version software | ||
| 4 | # and non-GNU make users happy. The real version | ||
| 5 | # is taken from the RCS record. | ||
| 6 | my $RCSRevKey = '$Revision: 0.44 $'; | ||
| 7 | $RCSRevKey =~ /Revision: (.*?) /; | ||
| 8 | $VERSION=$1; | ||
| 9 | use vars qw( $VERSION ); | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | Tk::XFontSelect.pm--X11 Font Selection Dialog. | ||
| 14 | |||
| 15 | =head1 SYNOPSIS | ||
| 16 | |||
| 17 | use Tk; | ||
| 18 | use Tk::XFontSelect; | ||
| 19 | |||
| 20 | my $fontdialog = $mw -> XFontSelect; | ||
| 21 | my $font = $fontdialog -> Show; | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | The Tk::XFontSelect widget displays a dialog box that lists the | ||
| 26 | descriptors of all of the fonts that are present on the system. | ||
| 27 | Selecting a font descriptor and pressing the "Accept" button returns | ||
| 28 | to the main application a string containing that font descriptor. | ||
| 29 | |||
| 30 | The list of font descriptors is generated by the xlsfonts command, | ||
| 31 | a standard utility of the XFree86 Rev. 3.3.2 distribution. | ||
| 32 | |||
| 33 | =head1 SEE ALSO | ||
| 34 | |||
| 35 | xlsfonts(1) man page. | ||
| 36 | |||
| 37 | =head1 VERSION INFO | ||
| 38 | |||
| 39 | First Release Version | ||
| 40 | |||
| 41 | $Revision: 0.44 $ | ||
| 42 | |||
| 43 | =cut | ||
| 44 | |||
| 45 | use Tk qw(Ev); | ||
| 46 | use Carp; | ||
| 47 | use Tk::widgets qw( LabEntry Button Frame Listbox Scrollbar ); | ||
| 48 | use base qw(Tk::Toplevel); | ||
| 49 | |||
| 50 | Construct Tk::Widget 'XFontSelect'; | ||
| 51 | |||
| 52 | my $defaultfont="*-helvetica-medium-r-*-*-12-*"; | ||
| 53 | |||
| 54 | |||
| 55 | sub Cancel { | ||
| 56 | shift -> withdraw; | ||
| 57 | return undef; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub Accept { | ||
| 61 | my ($w) = shift; | ||
| 62 | $w->configure( -font => $w -> Subwidget('fontlist')-> | ||
| 63 | get($w -> Subwidget('fontlist') -> curselection ) ); | ||
| 64 | $w->withdraw; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub Populate { | ||
| 68 | my($w, $args) = @_; | ||
| 69 | require Tk::Listbox; | ||
| 70 | require Tk::Button; | ||
| 71 | require Tk::Dialog; | ||
| 72 | require Tk::DialogBox; | ||
| 73 | require Tk::Toplevel; | ||
| 74 | |||
| 75 | $w -> SUPER::Populate( $args ); | ||
| 76 | my $l = $w -> Component( 'ScrlListbox' => 'fontlist', | ||
| 77 | -height => 15, | ||
| 78 | -width => 50, | ||
| 79 | -scrollbars => 'se', | ||
| 80 | -font => $defaultfont ) -> pack; | ||
| 81 | $l -> Subwidget('yscrollbar') -> configure(-width=>10); | ||
| 82 | $l -> Subwidget('xscrollbar') -> configure(-width=>10); | ||
| 83 | my $f1 = $w -> Component( Frame => 'buttonframe', | ||
| 84 | -container => 0, -relief => 'groove', | ||
| 85 | -borderwidth => 3 ); | ||
| 86 | my $ab = $f1 -> Component ( Button => 'acceptbutton', | ||
| 87 | -text => 'Accept', -font => $defaultfont, | ||
| 88 | -default => 'active', | ||
| 89 | -command => ['Accept', $w]) | ||
| 90 | -> pack( -side => 'left', -padx => 10, -pady => 5 ); | ||
| 91 | my $cb = $f1 -> Component ( Button => 'cancelbutton', | ||
| 92 | -text => 'Cancel', -font => $defaultfont, | ||
| 93 | -default => 'normal', | ||
| 94 | -command => ['Cancel', $w]) | ||
| 95 | -> pack( -side => 'left', -padx => 20, -pady => 2 ); | ||
| 96 | $f1 -> pack( -side => 'bottom', -expand => '1', -fill => 'x' ); | ||
| 97 | |||
| 98 | $w -> ConfigSpecs ( -font => ['PASSIVE', undef, undef, ''] ); | ||
| 99 | |||
| 100 | return $w; | ||
| 101 | } | ||
| 102 | |||
| 103 | sub Show { | ||
| 104 | my $w = shift; | ||
| 105 | $w -> grab; | ||
| 106 | $w -> watchcursor; | ||
| 107 | my @systemfonts; | ||
| 108 | { | ||
| 109 | open FONTLIST, 'xlsfonts|' or | ||
| 110 | die "Could not get system fonts using xlsfonts.\n"; | ||
| 111 | while ( <FONTLIST> ) { | ||
| 112 | @systemfonts = map {split /^/m; } <FONTLIST>; | ||
| 113 | } | ||
| 114 | close FONTLIST; | ||
| 115 | my $l = $w -> Subwidget('fontlist'); | ||
| 116 | foreach $f ( @systemfonts ) { | ||
| 117 | chomp $f; | ||
| 118 | $l -> insert( 'end', $f ); | ||
| 119 | } | ||
| 120 | } | ||
| 121 | $w -> defaultcursor; | ||
| 122 | $w -> waitVariable(\$w -> {Configure}{-font} ); | ||
| 123 | return $w -> cget(-font); | ||
| 124 | } | ||
| 125 | |||
| 126 | sub watchcursor { | ||
| 127 | my $w = shift; | ||
| 128 | $w -> Busy( -recurse => '1' ); | ||
| 129 | } | ||
| 130 | |||
| 131 | sub defaultcursor { | ||
| 132 | my $w = shift; | ||
| 133 | $w -> Unbusy( -recurse => '1' ); | ||
| 134 | } | ||
| 135 | |||
| 136 | |||
| 137 | 1; | ||
| 138 | __END__; |