Re: Motif -> GTK



Andreas Tille <tille@physik.uni-halle.de> writes:

|    replace  XtFoo()  by   GtkFoo()  (GdkFoo())
|    replace  XmBar()  by   GtkBar()

You need a search+replace? This is what I wrote some days ago for my webpages:

#! /usr/bin/perl
##
## A simple Serach and Replace Tool
##
## Copyright (C) 1998  Sascha Ziemann
##
## Copying: GPL
##

use Gtk;

#
# Callbacks
#
sub destroy_window {
    my($widget, $windowref, $w2) = @_;
    $$windowref = undef;
    $w2 = undef if defined $w2;
    0;
}

sub file_selection_ok {
    my($widget, $fs) = @_;
    if (-d $fs->get_filename) {
        $directory_entry->set_text ($fs->get_filename);
        destroy $fs;
    } else {
        Gtk->print ("Select a directory\n");
    }
}

sub select_dir {
    Gtk->print("+++ select dir\n");
    if (not defined $fs_window) {
        $fs_window = new Gtk::FileSelection "Select Directory";
        $fs_window->position(-mouse);
        $fs_window->signal_connect("destroy", \&destroy_window, \$fs_window);
        $fs_window->signal_connect("delete_event", \&destroy_window, \$fs_window);
        $fs_window->ok_button->signal_connect("clicked", \&file_selection_ok, $fs_window);
        $fs_window->cancel_button->signal_connect("clicked", sub { destroy $fs_window });

    }
    if (!visible $fs_window) {
        show $fs_window;
    } else {
        destroy $fs_window;
    }
}

@files = ();

sub filter_files {
    $dirname = $directory_entry->get_text;
    $filter = $filter_entry->get_text;
    $filter =~ s/\./\\./g; # FIXME: this might be not enough
    $filter =~ s/\*/.*/g;
    opendir (DIR, $dirname);
    @files = grep { /^$filter$/ && -f "$dirname/$_" } readdir(DIR);
    closedir (DIR);
    $text->freeze;
    $text->realize;
    $text->backward_delete($text->get_point);
    foreach $file (@files) {
        $text->insert(undef, $text->style->black, undef, $file ."\n");
    }
    $text->thaw;
}

sub run_search {
    $search_regx = $search_entry->get_text;
    $text->freeze;
    $text->realize;
    $text->backward_delete($text->get_point);
    $dirname = $directory_entry->get_text;
    foreach $file (@files) {
        open (FILE, "$dirname/$file");
        $lineno = 0;
        while (<FILE>) {
            if (/$search_regx/) {
                $text->insert(undef, $text->style->black, undef,
                              "$file:$lineno: ");
                @line_parts = split (/($search_regx)/);
                $delim = 0;
                foreach $part (@line_parts) {
                    if ($delim) {
                        $text->insert(undef, $text->style->white,
                                      $text->style->black,
                                      $part);
                    } else {
                        $text->insert(undef, $text->style->black, undef,
                                      $part);
                    }
                    $delim = !$delim;
                }
            }
            $lineno++;
        }
        close (FILE);
    }
    $text->thaw;
}

sub run_replace {
    $search_regx = $search_entry->get_text;
    $replace_expr = $replace_entry->get_text;
    $dirname = $directory_entry->get_text;
    foreach $file (@files) {
        open (FILE, "$dirname/$file");
        @lines = <FILE>;
        close (FILE);
        open (FILE, "> $dirname/$file");
        foreach (@lines) {
            s/$search_regx/$replace_expr/g;
            print FILE;
        }
        close (FILE);
    }
    $text->freeze;
    $text->realize;
    $text->backward_delete($text->get_point);
    $text->thaw;
}

#
# Inits
#
init Gtk;

#
# Window, main vbox and table
#
$window = new Gtk::Widget "GtkWindow",
  GtkWindow::type => -toplevel,
  GtkWindow::title => "Search & Replace",
  GtkWindow::allow_grow => 1,
  GtkWindow::allow_shrink => 1,
  GtkContainer::border_width => 10,
  GtkObject::signal::destroy => \&Gtk::main_quit,
  GtkObject::signal::delete_event => \&Gtk::false;

$vbox = new_child $window "GtkVBox",
  GtkBox::spacing => 5,
  GtkWidget::visible => 1;

$table = new Gtk::Table (4, 3, 0);
$table->set_row_spacings(5);
$table->set_col_spacings(5);
$table->border_width(0);
$vbox->pack_start($table, 0, 0, 0);
$table->show;

#
# Directory row
#
$directory_label = new Gtk::Label "Directory: ";
$directory_label->set_alignment(0, 0.5);
$table->attach($directory_label, 0, 1, 0, 1,
               {expand=>0, fill=>0},
               {expand=>0, fill=>0},
               0, 0);
$directory_label->show;
$directory_entry = new Gtk::Entry;
$directory_entry->set_text (".");
$table->attach($directory_entry, 1, 2, 0, 1,
               {expand=>1, fill=>1},
               {expand=>1, fill=>1},
               0, 0);
$directory_entry->show;
$select_button = new Gtk::Widget "GtkButton",
  GtkButton::label => "Select",
  GtkObject::signal::clicked => \&select_dir;
$table->attach($select_button, 2, 3, 0, 1,
               {expand=>0, fill=>0},
               {expand=>0, fill=>0},
               0, 0);
$select_button->show;

#
# Filter row
#
$filter_label = new Gtk::Label "Filter: ";
$filter_label->set_alignment(0, 0.5);
$table->attach($filter_label, 0, 1, 1, 2,
               {expand=>0, fill=>0},
               {expand=>0, fill=>0},
               0, 0);
$filter_label->show;
$filter_entry = new Gtk::Entry;
$filter_entry->set_text ("*.html");
$table->attach($filter_entry, 1, 2, 1, 2,
               {expand=>1, fill=>1},
               {expand=>1, fill=>1},
               0, 0);
$filter_entry->show;
$filter_button = new Gtk::Widget "GtkButton",
  GtkButton::label => "Filter",
  GtkObject::signal::clicked => \&filter_files;
$table->attach($filter_button, 2, 3, 1, 2,
               {expand=>0, fill=>0},
               {expand=>0, fill=>0},
               0, 0);
$filter_button->show;

#
# Search row
#
$search_label = new Gtk::Label "Search: ";
$search_label->set_alignment(0, 0.5);
$table->attach($search_label, 0, 1, 2, 3,
               {expand=>0, fill=>0},
               {expand=>0, fill=>0},
               0, 0);
$search_label->show;
$search_entry = new Gtk::Entry;
$search_entry->set_text ("http://www");
$table->attach($search_entry, 1, 2, 2, 3,
               {expand=>1, fill=>1},
               {expand=>1, fill=>1},
               0, 0);
show $search_entry;
$search_button = new Gtk::Widget "GtkButton",
  GtkButton::label => "Search",
  GtkObject::signal::clicked => \&run_search;
$table->attach($search_button, 2, 3, 2, 3,
               {expand=>0, fill=>0},
               {expand=>0, fill=>0},
               0, 0);
show $search_button;

#
# Replace row
#
$replace_label = new Gtk::Label "Replace: ";
$replace_label->set_alignment(0, 0.5);
$table->attach($replace_label, 0, 1, 3, 4,
               {expand=>0, fill=>0},
               {expand=>0, fill=>0},
               0, 0);
show $replace_label;
$replace_entry = new Gtk::Entry;
$replace_entry->set_text ("ftp://ftp");
$table->attach($replace_entry, 1, 2, 3, 4,
               {expand=>1, fill=>1},
               {expand=>1, fill=>1},
               0, 0);
show $replace_entry;
$replace_button = new Gtk::Widget "GtkButton",
  GtkButton::label => "Replace",
  GtkObject::signal::clicked => \&run_replace;
$table->attach($replace_button, 2, 3, 3, 4,
               {expand=>0, fill=>0},
               {expand=>0, fill=>0},
               0, 0);
show $replace_button;

#
# Text box
#
$text_table = new Gtk::Table(2,2,0);
$text_table->set_row_spacing(0,2);
$text_table->set_col_spacing(0,2);
$vbox->pack_start($text_table,1,1,0);
$text_table->show;

$text = new Gtk::Text(undef,undef);
$text_table->attach_defaults($text, 0,1,0,1);
show $text;

$hscrollbar = new Gtk::HScrollbar($text->hadj);
$text_table->attach($hscrollbar, 0, 1,1,2,[-expand,-fill],[-fill],0,0);
$hscrollbar->show;

$vscrollbar = new Gtk::VScrollbar($text->vadj);
$text_table->attach($vscrollbar, 1, 2,0,1,[-fill],[-expand,-fill],0,0);
$vscrollbar->show;

$text->freeze;
$text->realize;
$text->insert(undef, $text->style->black, undef,
              "\n      Be carefully! This is ALPHA stuff!\n" .
              "\n      Okay Baby - let's hit me!\n" .
              "\n   You have to filter befor you can search\n");
$text->thaw;

#
# let's go
#
show $window;

main Gtk;

-- 
http://www.ping.de/sites/aibon/



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