Re: Introduction and question



On Sat, 12 Jul 2008 15:15:04 +0200
Michelle Konzack <linux4michelle tamay-dogan net> wrote:

Now I have begun, to port some older "Programs" coded in  BaSH  to  Perl
and I want to change the Xdialog stuff to a real GTK-Dialog.

Since Xdialog can only show one thing at once, I have the singel options
in a "treeview", but now I want to use "notepad" with this nice tabs.

OK, my programs write the optins to singel files (one  file  per  Option
like "courier-imap") and I use "cat" to read its value.  I do  not  want
to change this...

In BaSH I used:

----[ STDIN ]-----------------------------------------------------------
 for NAME1 in $(find ${CFGDIR} -type f -maxdepth 1) ; do
   VAL=$(cat ${NAME1})
   NAME=$(basename ${NAME1})
   eval "export ${VARPREFIX}${NAME}=\${VAL}"
 done
------------------------------------------------------------------------

I'm not very good at bash, but from what I can gather, this
should get you started

#!/usr/bin/perl
use warnings;
use strict;
use File::Basename;

my $os_string = "linux";
fileparse_set_fstype($os_string);

my $dir = shift || '.';   # feed it a dir as argv[0]
                                     # defaults to '.'
my @files = ls($dir);

#print join "\n", @files,"\n";

#process your files here
foreach my $file(@files){
 print "$file\n";
 my $fullfilename = $file;
 my($base,$path,$ext)=fileparse($fullfilename,'\..*');
 print "base -> $base\n";
 print "path -> $path\n";
 print "ext -> $ext\n";
print "\n\n";
}


sub ls {
 my $path = @_ ? shift : ".";
 local *DIR;
 opendir DIR, $path or die "can't ls $path: $!";
 #remove subdirs, self, and parent dir
 return grep { $_ ne "." and $_ ne ".." and ! -d } readdir DIR;
}
__END__


Then, the "notepad" dialog...
I want to have a button "Apply"  which  write  the  new  entered  values
to the config dir whithout closing the dialog where under BaSH I used:

You have to be careful with terminology. A Dialog in Perl/Gtk2 is a specific
widget type, that does grabs and has predefined responses. You probably
could do something like this. It's not perfect, but should give you are start.
You will have to work on the Apply (to write your files out), and setting up your
pages with nice names. 

If you notice, the contents are only printed out once 
with get_vals(), maybe someone may know why? Maybe
the iter needs to be reset?


#!/usr/bin/perl
use strict;
use warnings;
use Gtk2 '-init';

package NBDialog;
use strict;
use warnings;
use Carp;
use Gtk2;
use Glib qw(TRUE FALSE);

use base 'Gtk2::Dialog';
 
sub new {
        my $class = shift;
        # create 
        my $self = Gtk2::Dialog->new;
        bless $self, $class;

        $self->set_position('center-always'); 

        # set some data if desired
        #$self->{result} = 42;

    
     $self->{notebook} = Gtk2::Notebook->new;
     $self->{notebook}->set_tab_pos ('top');
     
     # put whatever files or whatever in here
     foreach my $page('Page1','Page2','Page3'){
     
       # Create a textbuffer to contain that string
       $self->{$page}->{textbuffer} = Gtk2::TextBuffer->new();
       $self->{$page}->{textbuffer}->set_text($page);

       # Create a textview using that textbuffer
       $self->{$page}->{textview} = Gtk2::TextView->new_with_buffer($self->{$page}->{textbuffer});
       $self->{$page}->{textview}->set_left_margin (5);
       #$textview->set_editable(0); 

       $self->{$page}->{textview}->get_buffer->signal_connect (
         changed => sub { print "changed!\n"});

       # Add the textview to a scrolledwindow
       my $scrolledwindow = Gtk2::ScrolledWindow->new( undef, undef );
       $scrolledwindow->add($self->{$page}->{textview});
     
     #add to notebook
     $self->{notebook}->append_page( $scrolledwindow, $self->make_label($page) );
     
     }
     
     # the dialog's vbox is an advertised widget which you can add to
     $self->vbox->pack_start($self->{notebook},0,0,1);
     $self->vbox->show_all();

    my $button0 =  $self->add_button ("Apply" => 1);
    my $button1 = $self->add_button ("Reset" => 2);
    my $button2 = $self->add_button ("Cancel" => 3);
          
     
  $self->signal_connect (response => \&do_response );
# $self->signal_connect (response => sub { $_[0]->destroy });

return $self;
}


sub make_label {
    my ($self,$text) = @_;
    #print "@_\n";
    my $hbox   = Gtk2::HBox->new;
    my $label  = Gtk2::Label->new($text);
    my $button = Gtk2::Button->new("x");    # a pixmap would look nicer
    $button->signal_connect(
        clicked => sub {
            $self->{notebook}->remove_page( $self->{notebook}->get_current_page );
        }
    );
    $hbox->pack_start( $label,  FALSE, FALSE, 0 );
    $hbox->pack_start( $button, FALSE, FALSE, 0 );
    $label->show;
    $button->show;

    return $hbox;
}


sub do_response {
  my ($self, $resp) = @_;
  print "response $resp\n";
   
   return $resp;
}

sub get_vals{
    my $self = shift;
    
    foreach my $page('Page1','Page2','Page3'){
     print  $self->{$page}->{textbuffer}->get_text(
       $self->{$page}->{textbuffer}->get_start_iter, 
       $self->{$page}->{textbuffer}->get_end_iter,
       1),"\n";
    }
return 1;
}

1;


package main;


my $window = Gtk2::Window->new;
my $button = Gtk2::Button->new ("Click me");
$button->signal_connect (clicked => \&do_stuff);
$window->set_border_width (25);
$window->add ($button);
$window->show_all;
$window->signal_connect (destroy => sub { Gtk2->main_quit });
Gtk2->main;

sub do_stuff{
   my $dialog = NBDialog->new;

   my $response = $dialog->run;

   print 'returned ',$response,"\n";
   # do whatever here based on response

   print $dialog->get_vals(),"\n";
 
}

__END__




zentara


-- 
I'm not really a human, but I play one on earth.
http://zentara.net/CandyGram_for_Mongo.html 



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