Book HomeMastering Perl/TkSearch this book

23.2. Manipulating the Cursor

Every Tk widget has a cursor shape associated with it. Most default to what's known as the left_ptr shape, shown in Figure 23-1.

Figure 23-1

Figure 23-1. The standard cursor for most widgets

The cursor shape can be changed on a widget-by-widget basis with the -cursor option:

$mw->Button(-text => 'Go ...', -cursor => cursor_name);

When the mouse is over the Button, the cursor changes to the one specified. The cursor change happens whether the Button is disabled or not. The set of available cursors is quite large. Figure 23-2 shows the cursor shapes available on most systems.

Figure 23-2

Figure 23-2. Cursor shapes available on most systems

Here's a program to look at the different cursors interactively. It's really simple, just a Listbox full of cursor shape names and a button binding that changes the application's cursor. The hardest part is figuring out where the list of cursor names is hidden.

$mw = MainWindow->new;
$mw->Button(-text => "Exit", 
            -command => sub { exit })->pack(-side => "bottom",
                                      -fill => "x");
$scroll = $mw->Scrollbar;
$lb = $mw->Listbox(-selectmode => 'single',
                   -yscrollcommand => [set => $scroll]);
$scroll->configure(-command => [yview => $lb]);

$scroll->pack(-side => 'right', -fill => 'y');
$lb->pack(-side => 'left', -fill => 'both');

## Open file that contains all available cursors
## Might have to change this if your cursorfont.h is elsewhere
## On Win32 systems look in C:\Perl\lib\site\Tk\X11\cursorfont.h
open (FH, "/usr/X11R6/include/X11/cursorfont.h") ||
  die "Couldn't open cursor file.\n";

while (<FH>) {
  push(@cursors, $1) if (/\#define XC_(\w+) /);
}

close(FH);

$lb->insert('end', sort @cursors);
$lb->bind('<Button-1>', 
     sub { $mw->configure(-cursor => $lb->get($lb->curselection)); });

MainLoop;

23.2.1. Creating Your Own Custom Cursor Shape

Besides a built-in cursor name, the -cursor option also accepts a cursor specification, which is an array reference of four elements: an X11 bitmap (XBM) filename, a mask filename, and foreground and background colors. Here's what an actual cursor specification might look like:

    my $cursor = $^O eq 'MSWin32' ? 'mouse' :
        [qw/@mouse.xbm mouse.mask brown white/];

We saw this cursor specification used in the tkneko program in Chapter 17, "Images and Animations". What it's telling us is that, unfortunately, home-brewed cursors are not supported on Win32 machines; we use a built-in cursor named mouse instead. The actual application doesn't really want a three-button mouse but one of the furry variety as shown in Figure 23-3.

Figure 23-3

Figure 23-3. The tkneko cursor

So, under Unix, Tk builds a custom cursor using the XBM files mouse.xbm and mouse.mask and colors the mouse brown and white. Note that mouse.xbm starts with an @ character, which signals to Tk that this is a custom cursor specification rather than a built-in cursor name. This is an artifact of Tcl's "everything is a string" heritage.



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.