General Concepts

Changing Color of a Widget to indicate Error

The following is an example callback that does real time verification on a text entry box, and changes the background to red if it doesn't validate.

   1 sub on_date_entry_changed {
   2     my $entry = shift;
   3 
   4     if($entry->get_text() =~ /^\d\d\d\d:\d\d:\d\d \d\d:\d\d:\d\d$/) {
   5         my $style = $glade->get_widget('date_entry')->get_default_style();
   6         $glade->get_widget('date_entry')->modify_base('GTK_STATE_NORMAL',
   7                                                       $style->base('GTK_STATE_NORMAL'));
   8     } else {
   9         $glade->get_widget('date_entry')->modify_base('GTK_STATE_NORMAL',
  10                                                       Gtk2::Gdk::Color->new(65535, 0, 0));
  11     }
  12 }

The important fact is on 5-6 where you can always get back the default color values by pulling the default style for the widget, pulling the base color out of the style, and doing a modify_base back to it.

Gtk2::Entry

Because Gtk2::Entry objects are Gtk2::Widgets, you can change the background color of the text entry with:

   1     $entry->modify_base('GTK_STATE_NORMAL', Gtk2::Gdk::Color->new (65535, 0, 0));

Gtk2::TreeView

When using Glade2, the furthest down you can specify tree widgets is to the TreeView level. This means that the TreeView widget doesn't have columns, or a model. If you want to make your TreeView helpful, you first need to add a function that initializes columns on the TreeView, i.e.:

   1     my $column = Gtk2::TreeViewColumn->new_with_attributes ("Tag Name", 
   2                                                             Gtk2::CellRendererText->new, 
   3                                                             "text", 0);
   4     my $column2 = Gtk2::TreeViewColumn->new_with_attributes ("Group", 
   5                                                              Gtk2::CellRendererText->new,
   6                                                              "text", 1);
   7     my $column3 = Gtk2::TreeViewColumn->new_with_attributes ("Value", 
   8                                                              Gtk2::CellRendererText->new,
   9                                                              "text", 2);
  10     $glade->get_widget('exif_treeview')->append_column($column);
  11     $glade->get_widget('exif_treeview')->append_column($column2);
  12     $glade->get_widget('exif_treeview')->append_column($column3);

It is important to note that some examples on the web show text => 0 which is a red herring, as => is only used in this example to get autoquoting of the text key word.

After you have columns (i.e. your view is ready), you can create a model with something akin to:

   1     my @tags = Image::ExifTool::GetWritableTags();
   2     my $exifTool = new Image::ExifTool;
   3     my $liststore = Gtk2::ListStore->new('Glib::String','Glib::String','Glib::String');
   4     my $taginfo;
   5     for my $tag (sort @tags) {
   6         if(exists $img->{tags}->{$tag}) {
   7             my $iter = $liststore->append;
   8             my $value = $img->{tags}->{$tag} || "";
   9             $liststore->set($iter, 
  10                             0 => $img->{exiftool}->GetDescription($tag),
  11                             1 => ($img->{exiftool}->GetGroup($tag) || ""),
  12                             2 => $value);
  13         }
  14     }
  15 
  16     $glade->get_widget('exif_treeview')->set_model($liststore);

There are 2 important things to remember here:

  1. $liststore->append returns an iterator (i.e. a cursor), so needs to be done inside the loop

  2. you can set as many columns in the row as you like with set

SeansHacks: PerlGtk2 (last edited 2006-11-01 07:51:53 by SeanDague)