Re: saving from a menu bar

Am Sonntag, 15. April 2001 12:07 schrieben Sie:
Hi all,

Im new to perl-gtk and i have been editing the text widget example from
the gtk-perl tutorial on the web.
I could make a "save" button that saves the whole textfile to disk, but
that aint fun.
So i created a menu with the help of that tutorial.
Here is a snip:
{ path        => '/File/_Save',
                         accelerator => '<control>S',
                         callback    => \&save, $window,$text },

and here is the subroutine:

sub save {
        my ($widget, $window,  $text) = @_;
        my $entry_text = $text->get_chars();
        open (FILE, ">$textfile");
        print FILE "$entry_text\n";
        close FILE;

first of all, you get the values from text entry fields by $text->get_text().

Then, according to the perlgtk tutorial it would be better if you include a 
save file dialog:

Chapter 40. File Selection Dialogs Inheritance Hierarchy

   +--- Widget
         +--- Container
               +--- Bin
                     +--- Window
                           +--- FileSelectionDialog

 The file selection dialog is a quick and simple way to display a dialog box 
to prompt the user for a filename. It comes complete with Ok, Cancel, and 
Help buttons, and it's a great way to cut down on programming time. 

 To create a new file selection box use: 

 $file_dialog = new Gtk::FileSelection( $title ); 

 To set the filename, for example to bring up a specific directory, or give a 
default filename, use this function: 

 $file_dialog->set_filename( $filename ); 

 To grab the text that the user has entered or clicked on, use this function: 


 You can match a pattern to a filename in the current directory using the 
following function: 

 $file_dialog->complete( $pattern ); 

 If a match can be made, the matched filename will appear in the text entry 
field of the file selection dialog. If a partial match can be made, the list 
of files will contain those filenames which have been partially matched. 

 An example of a pattern would be *.txt or gtk*. 

 The File Selection Dialog can show some file operation buttons. You can show 
these buttons using: 

 $file_dialog->show_fileop_buttons();  These buttons can be hidden using: 


 You can also access the widgets contained within the file selection widget. 
These are: 


 Most likely you will want to use the ok_button, cancel_button, and 
help_button widgets in signaling their use. File Selection Example

  Included here is an example stolen from testgtk.c, modified to run on its 
own. As you will see, there is nothing much to creating a file selection 
widget. While in this example the Help button appears on the screen, it does 
nothing as there is not a signal attached to it. 

 File Selection Dialog Source  

      #!/usr/bin/perl -w

use Gtk;
use strict;

init Gtk;
set_locale Gtk;

my $false = 0;
my $true = 1;

my $file_dialog;

# Create a new file selection widget
$file_dialog = new Gtk::FileSelection( "File Selection" );
$file_dialog->signal_connect( "destroy", sub { Gtk->exit( 0 ); } );

# Connect the ok_button to file_ok_sel function
$file_dialog->ok_button->signal_connect( "clicked",
                                         $file_dialog );
# Connect the cancel_button to destroy the widget
$file_dialog->cancel_button->signal_connect( "clicked",
                                             sub { Gtk->exit( 0 ); } );
# Lets set the filename, as if this were a save dialog, and we are giving
# a default filename
$file_dialog->set_filename( "penguin.png" );
main Gtk;
exit( 0 );

# Get the selected filename and print it to the console
sub file_ok_sel
    my ( $widget, $file_selection ) = @_;
    my $file = $file_selection->get_filename();

    print( "$file\n" );


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]