App-Chart

 view release on metacpan or  search on metacpan

lib/App/Chart/Gtk2/OpenDialog.pm  view on Meta::CPAN

# uncomment this to run the ### lines
#use Smart::Comments;

use Glib::Object::Subclass
  'Gtk2::Dialog',
  signals => { map => \&_do_map };

# use App::Chart::Gtk2::Ex::ToplevelSingleton hide_on_delete => 1;
# use base 'App::Chart::Gtk2::Ex::ToplevelSingleton';
# sub popup {
#   my ($class) = @_;
#   my $self = $class->instance;
#   $self->present;
#   return $self;
# }

use constant { RESPONSE_OPEN  => 0,
               RESPONSE_NEW   => 1 };

sub INIT_INSTANCE {
  my ($self) = @_;

  $self->set_title (__('Chart: Open'));
  $self->add_buttons ('gtk-open'   => RESPONSE_OPEN,
                      'gtk-new'    => RESPONSE_NEW,
                      'gtk-cancel' => 'cancel',
                      'gtk-help'   => 'help');
  $self->signal_connect (response => \&_do_response);

  my $vbox = $self->vbox;

  my $scrolled = Gtk2::ScrolledWindow->new;
  $scrolled->set(hscrollbar_policy => 'automatic');
  $vbox->pack_start ($scrolled, 1,1,0);

  require App::Chart::Gtk2::OpenModel;
  my $model = $self->{'model'} = App::Chart::Gtk2::OpenModel->instance;
  #   require App::Chart::Gtk2::SymlistTreeModel;
  #   my $model = $self->{'model'} = App::Chart::Gtk2::SymlistTreeModel->instance;

  my $treeview = $self->{'treeview'} = Gtk2::TreeView->new_with_model ($model);
  $treeview->set (reorderable       => 1,
                  headers_visible   => 0,
                  fixed_height_mode => 1,
                  search_column     => $model->COL_ITEM_SYMBOL);
  $treeview->signal_connect (row_activated => \&_do_row_activated);
  $scrolled->add ($treeview);

  foreach my $key ('all', 'favourites') {
    if (my $path = $model->path_for_key ($key)) {
      $treeview->expand_row($path, 0);
    }
  }

  my $selection = $treeview->get_selection();
  $selection->set_mode ('single');

  my $renderer = Gtk2::CellRendererText->new;
  $renderer->set (xalign => 0, # left justify
                  ypad => 0);
  $renderer->set_fixed_height_from_font (1);

  my $column = Gtk2::TreeViewColumn->new;
  $column->pack_start ($renderer, 1);
  $column->set_cell_data_func ($renderer, \&_cell_data_func);
  $column->set (sizing => 'fixed');
  $treeview->append_column ($column);

  my $notfound = $self->{'notfound'}
    = Gtk2::Label->new (__('Not in database, click "New" to download.
Be sure capitalization is right for download.
(Or click/return again to really open.)'));
  $notfound->set_justify ('center');
  $vbox->pack_start ($notfound, 0,0,0);

  my $hbox = Gtk2::HBox->new;
  $vbox->pack_start ($hbox, 0,0,0);

  $hbox->pack_start (Gtk2::Label->new (__('Symbol')), 0,0,0);

  my $entry = $self->{'entry'} = Gtk2::Entry->new;
  $hbox->pack_start ($entry, 1, 1, 0.5 * Gtk2::Ex::Units::em($entry));
  $entry->signal_connect (activate => \&_do_entry_open);
  $entry->signal_connect (changed  => \&_do_entry_changed);

  my $button = Gtk2::Button->new_with_label (__('Enter'));
  $hbox->pack_start ($button, 0,0,0);
  $button->signal_connect (clicked => \&_do_entry_open);

  $vbox->show_all;
  $notfound->hide;
  $entry->grab_focus;

  # with a sensible size for the TreeView
  Gtk2::Ex::Units::set_default_size_with_subsizes
      ($self, [$scrolled, '40 ems', '20 lines']);
}

# select etc when newly mapped
sub _do_map {
  my ($self) = @_;

  $self->{'notfound'}->hide;
  my $entry = $self->{'entry'};
  $entry->grab_focus;
  Gtk2::Ex::EntryBits::select_region_noclip ($entry, 0, -1);

  return shift->signal_chain_from_overridden(@_);
}

sub _do_response {
  my ($self, $response) = @_;

  if ($response eq RESPONSE_OPEN) {
    _do_entry_open ($self);

  } elsif ($response eq RESPONSE_NEW) {
    my $symbol = $self->entry_str;
    if ($symbol) { # should be insensitive when empty anyway
      App::Chart::Database->add_symbol ($symbol);
      $self->_do_open ($symbol);



( run in 0.526 second using v1.01-cache-2.11-cpan-5735350b133 )