Gnome2-GConf

 view release on metacpan or  search on metacpan

examples/basic-gconf-app.pl  view on Meta::CPAN

#!/usr/bin/perl

# This program demonstrates how to use GConf.  The key thing is that
# the main window and the prefs dialog have NO KNOWLEDGE of one
# another as far as configuration values are concerned; they don't
# even have to be in the same process. That is, the GConfClient acts
# as the data "model" for configuration information; the main
# application is a "view" of the model; and the prefs dialog is a
# "controller."
#
# You can tell if your application has done this correctly by
# using "gconftool" instead of your preferences dialog to set
# preferences. For example:
# 
# gconftool --type=string --set /apps/basic-gconf-app/foo "My string"
# 
# If that doesn't work every bit as well as setting the value
# via the prefs dialog, then you aren't doing things right. ;-)
#
#
# If you really want to be mean to your app, make it survive
# this:
# 
# gconftool --break-key /apps/basic-gconf-app/foo
# 
# Remember, the GConf database is just like an external file or
# the network - it may have bogus values in it. GConf admin
# tools will let people put in whatever they can think of.

examples/basic-gconf-app.pl  view on Meta::CPAN


	$config = create_configurable_widget ($client, "/apps/basic-gconf-app/baz");
	$vbox->pack_start($config, TRUE, TRUE, 0);

	$config = create_configurable_widget ($client, "/apps/basic-gconf-app/blah");
	$vbox->pack_start($config, TRUE, TRUE, 0);

	$w->signal_connect(destroy => sub { Gtk2->main_quit });
	$w->{client} = $client;

	my $prefs = Gtk2::Button->new("Prefs");
	$vbox->pack_end($prefs, FALSE, FALSE, 0);
	$prefs->signal_connect(clicked => sub {
			my $button = shift;
			my $main_window = shift;

			my $prefs_dialog = $main_window->{prefs};
			if (not $prefs_dialog) {
				my $client = $main_window->{client};
				$prefs_dialog = create_prefs_dialog ($main_window, $client);
				$main_window->{prefs} = $prefs_dialog;
				
				$prefs_dialog->signal_connect(
						destroy => \&prefs_dialog_destroyed,
						$main_window);

				$prefs_dialog->show_all;
			} else {	
				# show existing dialog
				$prefs_dialog->present;
			}
		}, $w);
		
	return $w;
}

# Create a GtkLabel inside a frame, that we can "configure"
# (the label displays the value of the config key).
sub create_configurable_widget
{

examples/basic-gconf-app.pl  view on Meta::CPAN

			my $client = $_[0]->{client};
			my $notify_id = $_[0]->{notify_id};

			$client->notify_remove($notify_id) if $notify_id;
		});
	
	return $frame;
}

#
# Preferences dialog code. NOTE that the prefs dialog knows NOTHING
# about the existence of the main window; it is purely a way to fool
# with the GConf database. It never does something like change
# the main window directly; it ONLY changes GConf keys via
# GConfClient. This is _important_, because people may configure
# your app without using your preferences dialog.
#
# This is an instant-apply prefs dialog. For a complicated
# apply/revert/cancel dialog as in GNOME 1, see the
# complex-gconf-app.c example. But don't actually copy that example
# in GNOME 2, thanks. ;-) complex-gconf-app.c does show how
# to use GConfChangeSet.
#

sub prefs_dialog_destroyed
{
	my $dialog = shift;
	my $main_window = shift;

	$main_window->{prefs} = undef;
}

sub config_entry_commit
{
	my $entry = shift;

	my $client = $entry->{client};
	my $key    = $entry->{key};
	
	my $text = $entry->get_chars(0, -1);

examples/basic-gconf-app.pl  view on Meta::CPAN

		$client->unset($key);
	}
	
	# since we connect the "focus_out_event" to this callback,
	# this return is needed. (ebassi)
	return FALSE;
}

sub create_config_entry
{
	my $prefs_dialog = shift;
	my $client       = shift;
	my $config_key   = shift;
	my $has_focus    = shift || FALSE;

	my $hbox  = Gtk2::HBox->new(FALSE, 6);
	my $label = Gtk2::Label->new("$config_key =");
	my $entry = Gtk2::Entry->new;

	$hbox->pack_start($label, FALSE, FALSE, 0);
	$hbox->pack_end($entry, FALSE, FALSE, 0);

examples/basic-gconf-app.pl  view on Meta::CPAN

	# Technically, we should update this sensitivity if the key
	# gets a change notify, but that's probably overkill.
	$entry->set_sensitive($client->key_is_writable($config_key));

	$entry->grab_focus if $has_focus;
	
	return $hbox;
}


sub create_prefs_dialog
{
	my $parent = shift;
	my $client = shift;

	my $dialog = Gtk2::Dialog->new("basic-gconf-app Preferences",
				       $parent,
				       [ qw/destroy-with-parent/ ],
				       'gtk-close', 'accept');
				       
	# destroy dialog on button press

examples/complex-gconf-app.pl  view on Meta::CPAN

	$config = create_configurable_widget ($client, BAZ_KEY);
	$vbox->pack_start($config, TRUE, TRUE, 0);

	$config = create_configurable_widget ($client, BLAH_KEY);
	$vbox->pack_start($config, TRUE, TRUE, 0);

	$w->signal_connect(delete_event => sub { $_[0]->destroy });
	$w->signal_connect(destroy => sub { Gtk2->main_quit });
	$w->{client} = $client;

	my $prefs = Gtk2::Button->new("Prefs");
	$vbox->pack_end($prefs, FALSE, FALSE, 0);
	$prefs->signal_connect(clicked => sub {
			my $button = shift;
			my $main_window = shift;

			my $prefs_dialog = $main_window->{prefs};
			if (not $prefs_dialog)
			{
				my $client = $main_window->{client};
				$prefs_dialog = create_prefs_dialog ($main_window, $client);
				$main_window->{prefs} = $prefs_dialog;
				
				$prefs_dialog->signal_connect(
						destroy => \&prefs_dialog_destroyed,
						$main_window);

				$prefs_dialog->show_all;
			}
			else
			{	
				# show existing dialog
				$prefs_dialog->present;
			}
		}, $w);
		
	return $w;
}

# Create a GtkLabel inside a frame, that we can "configure"
# (the label displays the value of the config key).
sub create_configurable_widget
{

examples/complex-gconf-app.pl  view on Meta::CPAN

			# notifications is destroyed
			my $client = $_[0]->{client};
			my $notify_id = $_[0]->{notify_id};

			$client->notify_remove($notify_id) if $notify_id;
		});
	
	return $frame;
}

# Preferences dialog code. NOTE that the prefs dialog knows NOTHING
# about the existence of the main window; it is purely a way to fool
# with the GConf database. It never does something like change
# the main window directly; it ONLY changes GConf keys via
# GConfClient. This is _important_, because people may configure
# your app without using your preferences dialog.
#
# This is an explicit-apply prefs dialog that uses GConfChangeSets. This kind
# of dialog is disencouraged inside the GNOME Human Interface Guidelines,
# since it's anti-intuitive; nevertheless, sometimes it is the only
# acceptable solution (e.g.: when preferences might take more than a short
# period of time to apply). (ebassi)

sub prefs_dialog_destroyed
{
	my $dialog = shift;
	my $main_window = shift;

	$main_window->{prefs} = undef;
}

sub on_prefs_response
{
	use Data::Dumper;
	my $dialog = shift;
	my $response = shift;
	my $client = shift;

	my $changeset = $dialog->{changeset};
	
	# see the state of the change set before committing it
	#print Dumper($changeset);

examples/complex-gconf-app.pl  view on Meta::CPAN

		$changeset->{$key} = { type => 'string', value => undef };
	}
	
	# since we also connect the "focus_out_event" to this callback, this return
	# is needed. (ebassi)
	FALSE;
}

sub create_config_entry
{
	my $prefs_dialog = shift;
	my $changeset	 = shift;
	my $config_key   = shift;
	my $has_focus    = shift || FALSE;

	my $hbox  = Gtk2::HBox->new(FALSE, 5);
	my $label = Gtk2::Label->new($config_key);
	my $entry = Gtk2::Entry->new;

	$hbox->pack_start($label, FALSE, FALSE, 0);
	$hbox->pack_end($entry, FALSE, FALSE, 0);

examples/complex-gconf-app.pl  view on Meta::CPAN

    # Technically, we should update this sensitivity if the key gets
    # a change notify, but that's probably overkill.
	$entry->set_sensitive($client->key_is_writable($config_key));

	$entry->grab_focus if $has_focus;
	
	return $hbox;
}


sub create_prefs_dialog
{
	use Data::Dumper;
	
	my $parent = shift;
	my $client = shift;

	my $dialog = Gtk2::Dialog->new("basic-gconf-app Preferences",
								   $parent,
								   [ qw/destroy-with-parent/ ],
								   'gtk-cancel', 'cancel',
								   'gtk-apply', 'apply',
								   'gtk-ok', 'ok');
	
	# commit on button press
	$dialog->signal_connect(response => \&on_prefs_response, $client);
	$dialog->set_default_response('ok');
	
	# resizing doesn't grow the entries anyhow
	$dialog->set_resizable(FALSE);

	my $vbox = Gtk2::VBox->new(FALSE, 5);
	$vbox->set_border_width(5);

	$dialog->vbox->pack_start($vbox, FALSE, FALSE, 0);
	



( run in 2.577 seconds using v1.01-cache-2.11-cpan-0bb4e1dffa6 )