Re: Get a list of object *names* from GtkBuilder
- From: Daniel Kasak <d j kasak dk gmail com>
- To: Emmanuele Bassi <ebassi gmail com>
- Cc: gtk-app-devel-list list <gtk-app-devel-list gnome org>
- Subject: Re: Get a list of object *names* from GtkBuilder
- Date: Fri, 27 Nov 2015 16:15:40 +1100
OK so based on Emmanuele's suggestion ( thanks, by the way ), I've
done it like this, which is dodgy, but still pretty simple, and could
be of use for others who might bump into this thread with similar
requirements.
First, in the abstract 'window' class, I've got a global variable to
store the names of GtkPaned widgets:
---
my @all_gtkPaned_names;
---
Then just after I've generated a window from a gtkBuilder file, I go:
@all_GtkPaned_names = ();
if ( $window->{options}->{builder_path} ) {
my $xml_parser = XML::Parser->new( Style => 'Subs', Pkg => 'window' );
my $whatever = $xml_parser->parsefile(
$window->{options}->{builder_path} );
}
foreach my $paned_name ( @all_GtkPaned_names ) {
my $state_name = $class . ':' . $paned_name . ':position';
my $state = $globals->{config_manager}->simpleGet( $state_name );
my $widget = $window->{builder}->get_object( $paned_name );
if ( $state ) {
$widget->set_position( $state );
}
$widget->signal_connect(
notify => sub { $window->save_gtkPaned_state( @_,
$state_name ) }
);
}
---
Then I have a *static* *function* ( ie not a method ) in the same
class, which gets called by XML::Parser because of the options passed
to its constructor ( ie the style and pkg ):
---
sub object {
my ( $expat, $object, $class, $gtk_object_class, $gtk_object_name,
$gtk_object_name ) = @_;
if ( $gtk_object_class eq 'GtkPaned' ) {
push @all_GtkPaned_names, $gtk_object_name;
}
}
---
In here I'm obviously just appending to the global list of widgets.
I've also got:
---
sub save_gtkPaned_state {
my ( $self, $widget, $some_boolean_thing, $state_name ) = @_;
$self->{globals}->{config_manager}->simpleSet( $state_name,
$widget->get_position );
}
---
That's pretty much it ( I think ). Hopefully it's useful to someone.
I still think it would be handy to be able to enumerate over the list
of widget names without having to use an external parser, but I
concede it's kinda a corner case, and not too difficult to work
around.
Dan
On Wed, Nov 18, 2015 at 12:14 PM, Daniel Kasak <d j kasak dk gmail com> wrote:
I anticipated this question :)
Here's what I'm trying to do ...
I've got an abstract 'window' class that handles loading windows from
a builder file, and attaching some convenience functions and stuff. I
want to save the state of various things in a SQLite database. For
example, GtkPaned positions are causing me issues on different
resolutions and DPIs. So I'd like to be able to do:
foreach my $item ( $window->{builder}->get_objects() ) {
if ( ref $item eq "Gtk3::Paned" ) {
my $name = $item->get_name;
my $state_name = $class . ':' . $name . ':position';
my $state = $globals->{config_manager}->simpleGet( $state_name
); # simpleGet fetches from a SQLite key/value pair config db
$item->set_position( $state );
}
}
... on startup. I'd of course have signals on the 'notify' signal of
each Gtk3::Paned to save the state each time it's altered ( with some
work-arounds for initial state ). This code would similarly have to
know the name of each item, so it could write it to the SQLite DB.
I've done this all manually for a couple of widgets ( ie by having
special code per widget ), but being able to do it generically as
above would be a far better solution. Thoughts?
Dan
On Tue, Nov 17, 2015 at 4:57 AM, Emmanuele Bassi <ebassi gmail com> wrote:
Hi;
On 16 November 2015 at 02:26, Daniel Kasak <d j kasak dk gmail com> wrote:
Greetings all.
I'd like to get a list of object names from a GtkBuilder object ( I'm
using Perl ). I know about
https://developer.gnome.org/gtk3/stable/GtkBuilder.html#gtk-builder-get-objects
- which returns a list of objects, but I really need the names. Is it
possible?
The question would be "why do you think you need the names from the
XML, considering you wrote the XML definitions in the first place" —
i.e. since you're the author, you're supposed to already know those
names, not figure them out programmatically.
In practice, you could load up the XML yourself and figure those names
out using the XML::Parser or XML::LibXML modules in Perl; but the
question betrays some confusion as to what GtkBuilder UI definition
files are, and how they are used, so it's probably better for you to
answer the question: what is it that you're trying to achieve?
Ciao,
Emmanuele.
--
https://www.bassi.io
[ ] ebassi [ gmail com]
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]