[perl-Gtk3] Add a few Gtk3::TreeStore overrides
- From: Torsten SchÃnfeld <tsch src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [perl-Gtk3] Add a few Gtk3::TreeStore overrides
- Date: Fri, 14 Oct 2011 18:35:37 +0000 (UTC)
commit 6737f96163764c7ef9dd36921b9bf26f18fdef86
Author: Torsten SchÃnfeld <kaffeetisch gmx de>
Date: Fri Oct 14 20:11:42 2011 +0200
Add a few Gtk3::TreeStore overrides
lib/Gtk3.pm | 92 ++++++++++++++++++++++++++++++++++++---------------------
t/overrides.t | 31 ++++++++++++++++---
2 files changed, 84 insertions(+), 39 deletions(-)
---
diff --git a/lib/Gtk3.pm b/lib/Gtk3.pm
index cb71606..2475e24 100644
--- a/lib/Gtk3.pm
+++ b/lib/Gtk3.pm
@@ -17,6 +17,7 @@ my @_GTK_FLATTEN_ARRAY_REF_RETURN_FOR = qw/
Gtk3::Window::list_toplevels
/;
my @_GTK_HANDLE_SENTINEL_BOOLEAN_FOR = qw/
+ Gtk3::TreeModel::get_iter
Gtk3::TreeModel::get_iter_first
Gtk3::TreeSelection::get_selected
/;
@@ -98,46 +99,16 @@ sub Gtk3::Button::new {
}
sub Gtk3::ListStore::new {
- my ($class, @types) = @_;
- local $@;
- my $real_types = (@types == 1 && eval { @{$types[0]} })
- ? $types[0]
- : \ types;
- return Glib::Object::Introspection->invoke (
- $_GTK_BASENAME, 'ListStore', 'new',
- $class, $real_types);
+ return _common_tree_model_new ('ListStore', @_);
}
-# Reroute 'get' to Gtk3::ListStore instead of Glib::Object.
+# Reroute 'get' to Gtk3::TreeModel instead of Glib::Object.
sub Gtk3::ListStore::get {
return Gtk3::TreeModel::get (@_);
}
sub Gtk3::ListStore::set {
- my ($model, $iter, @columns_and_values) = @_;
- my (@columns, @values);
- local $@;
- if (@columns_and_values == 2 && eval { @{$columns_and_values[0]} }) {
- @columns = @{$columns_and_values[0]};
- @values = @{$columns_and_values[1]};
- } elsif (@columns_and_values % 2 == 0) {
- my %cols_to_vals = @columns_and_values;
- @columns = keys %cols_to_vals;
- @values = values %cols_to_vals;
- } else {
- croak ('Usage: Gtk3::ListStore::set ($store, \ columns, \ values)',
- ' -or-: Gtk3::ListStore::set ($store, $column1 => $value1, ...)');
- }
- my @wrapped_values = ();
- foreach my $i (0..$#columns) {
- my $column_type = $model->get_column_type ($columns[$i]);
- push @wrapped_values,
- Glib::Object::Introspection::GValueWrapper->new (
- $column_type, $values[$i]);
- }
- Glib::Object::Introspection->invoke (
- $_GTK_BASENAME, 'ListStore', 'set',
- $model, $iter, \ columns, \ wrapped_values);
+ return _common_tree_model_set ('ListStore', @_);
}
sub Gtk3::MessageDialog::new {
@@ -174,6 +145,19 @@ sub Gtk3::TreePath::new {
$_GTK_BASENAME, 'TreePath', $method, @_);
}
+sub Gtk3::TreeStore::new {
+ return _common_tree_model_new ('TreeStore', @_);
+}
+
+# Reroute 'get' to Gtk3::TreeModel instead of Glib::Object.
+sub Gtk3::TreeStore::get {
+ return Gtk3::TreeModel::get (@_);
+}
+
+sub Gtk3::TreeStore::set {
+ return _common_tree_model_set ('TreeStore', @_);
+}
+
sub Gtk3::TreeView::new {
my ($class, @args) = @_;
my $method = (@args == 1) ? 'new_with_model' : 'new';
@@ -199,11 +183,51 @@ sub Gtk3::Window::new {
$_GTK_BASENAME, 'Window', 'new', $class, $type);
}
+# - Helpers ----------------------------------------------------------------- #
+
+sub _common_tree_model_new {
+ my ($package, $class, @types) = @_;
+ local $@;
+ my $real_types = (@types == 1 && eval { @{$types[0]} })
+ ? $types[0]
+ : \ types;
+ return Glib::Object::Introspection->invoke (
+ $_GTK_BASENAME, $package, 'new',
+ $class, $real_types);
+}
+
+sub _common_tree_model_set {
+ my ($package, $model, $iter, @columns_and_values) = @_;
+ my (@columns, @values);
+ local $@;
+ if (@columns_and_values == 2 && eval { @{$columns_and_values[0]} }) {
+ @columns = @{$columns_and_values[0]};
+ @values = @{$columns_and_values[1]};
+ } elsif (@columns_and_values % 2 == 0) {
+ my %cols_to_vals = @columns_and_values;
+ @columns = keys %cols_to_vals;
+ @values = values %cols_to_vals;
+ } else {
+ croak ('Usage: Gtk3::${package}::set ($store, \ columns, \ values)',
+ ' -or-: Gtk3::${package}::set ($store, $column1 => $value1, ...)');
+ }
+ my @wrapped_values = ();
+ foreach my $i (0..$#columns) {
+ my $column_type = $model->get_column_type ($columns[$i]);
+ push @wrapped_values,
+ Glib::Object::Introspection::GValueWrapper->new (
+ $column_type, $values[$i]);
+ }
+ Glib::Object::Introspection->invoke (
+ $_GTK_BASENAME, $package, 'set',
+ $model, $iter, \ columns, \ wrapped_values);
+}
+
1;
__END__
-# - Docs --------------------------------------------------------------- #
+# - Docs -------------------------------------------------------------------- #
=head1 NAME
diff --git a/t/overrides.t b/t/overrides.t
index 202266a..386c7df 100644
--- a/t/overrides.t
+++ b/t/overrides.t
@@ -5,7 +5,7 @@ BEGIN { require './t/inc/setup.pl' };
use strict;
use warnings;
-plan tests => 14;
+plan tests => 21;
# Gtk3::CellLayout::get_cells
{
@@ -18,7 +18,7 @@ plan tests => 14;
is_deeply([$cell->get_cells], [$one, $two]);
}
-# Gtk3::ListStore->new, set and get
+# Gtk3::ListStore::new, set and get
{
my $model = Gtk3::ListStore->new ([qw/Glib::String Glib::Int/]);
my $iter = $model->append;
@@ -36,22 +36,25 @@ plan tests => 14;
like ($@, qr/Usage/);
}
-# Gtk3::TreeModel->get_iter_first
+# Gtk3::TreeModel::get_iter and get_iter_first
{
my $model = Gtk3::ListStore->new ('Glib::String');
+ my $path = Gtk3::TreePath->new_from_string ('0');
+ is ($model->get_iter ($path), undef);
is ($model->get_iter_first, undef);
my $iter = $model->append;
+ isa_ok ($model->get_iter ($path), 'Gtk3::TreeIter');
isa_ok ($model->get_iter_first, 'Gtk3::TreeIter');
}
-# Gtk3::TreePath->get_indices
+# Gtk3::TreePath::get_indices
{
# my $path = Gtk3::TreePath->new_from_indices ([1, 2, 3]); # FIXME
my $path = Gtk3::TreePath->new_from_string ('1:2:3');
is_deeply ([$path->get_indices], [1, 2, 3]);
}
-# Gtk3::TreeSelection->get_selected
+# Gtk3::TreeSelection::get_selected
{
my $model = Gtk3::ListStore->new ('Glib::String');
my $view = Gtk3::TreeView->new ($model);
@@ -63,6 +66,24 @@ plan tests => 14;
isa_ok ($sel_iter, 'Gtk3::TreeIter');
}
+# Gtk3::TreeStore::new, set and get
+{
+ my $model = Gtk3::TreeStore->new ([qw/Glib::String Glib::Int/]);
+ my $iter = $model->append (undef);
+ $model->set ($iter, [0, 1], ['Foo', 23]);
+ is_deeply ([$model->get ($iter, 0,1)], ['Foo', 23]);
+ is (scalar $model->get ($iter, 0,1), 23);
+
+ $iter = $model->append (undef);
+ $model->set ($iter, 0 => 'Bar', 1 => 42);
+ is_deeply ([$model->get ($iter, 0,1)], ['Bar', 42]);
+ is (scalar $model->get ($iter, 0,1), 42);
+
+ local $@;
+ eval { $model->set ($iter, 0) };
+ like ($@, qr/Usage/);
+}
+
# Gtk3::Window::new and list_toplevels
{
my $window1 = Gtk3::Window->new ('toplevel');
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]