#! /usr/bin/perl -w

use strict;
use Gtk2 '-init';
use Glib qw/TRUE FALSE/; 

my $tt = Gtk2::Tooltips->new();

#standard window creation, placement, and signal connecting
my $window = Gtk2::Window->new('toplevel');
$window->signal_connect('delete_event' => sub { Gtk2->main_quit; });
$window->set_border_width(5);
$window->set_position('center_always');

#this vbox will return the bulk of the gui
my $vbox = &ret_vbox();

#add and show the vbox
$window->add($vbox);
$window->show();

#our main event-loop
Gtk2->main();

sub ret_vbox {

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

	#====================================
	#The handle box helps in creating a detachable toolbar 
	my $hb = Gtk2::HandleBox->new;
	#create a toolbar, and do some initial settings
	my $toolbar = Gtk2::Toolbar->new;
	$toolbar->set_icon_size ('large-toolbar');
	$toolbar->set_show_arrow (FALSE);
	
	#________
	#Create a menu tool button that contains a simple menu.
		my $t_mbtn_edit = Gtk2::MenuToolButton->new_from_stock('gtk-edit');
		$t_mbtn_edit->set_menu (&ret_menu);
					
	$toolbar->insert($t_mbtn_edit,-1);
	
	#________
	#Insert a separator tool item	
	$toolbar->insert(Gtk2::SeparatorToolItem->new ,-1 );
	
	#________
	#Create three radio tool buttons, radio btn "both"	
		my $t_rbtn_both = Gtk2::RadioToolButton->new (undef);
		$t_rbtn_both->set_label ('Both');
		$t_rbtn_both->set_active(TRUE);
		$t_rbtn_both->set_icon_widget (Gtk2::Image->new_from_file('./pix/tux.png'));
		$t_rbtn_both->signal_connect('toggled', \&radio_toggled,$toolbar);
		
	$toolbar->insert($t_rbtn_both,-1); 
	
	#________
	#radio btn "text"	
		my $t_rbtn_text = Gtk2::RadioToolButton->new_from_widget($t_rbtn_both);
		$t_rbtn_text->set_label ('Text');
		$t_rbtn_text->set_icon_widget (Gtk2::Image->new_from_file('./pix/tux.png'));
		$t_rbtn_text->signal_connect('toggled', \&radio_toggled,$toolbar);
						
	$toolbar->insert($t_rbtn_text,-1);
	
	#________
	#radio btn "icon"
		my $t_rbtn_icon = Gtk2::RadioToolButton->new_from_widget($t_rbtn_both);
		$t_rbtn_icon->set_label ('Icons');
		$t_rbtn_icon->set_icon_widget (Gtk2::Image->new_from_file('./pix/tux.png'));
		$t_rbtn_icon->signal_connect('toggled', \&radio_toggled,$toolbar);
		
	$toolbar->insert($t_rbtn_icon,-1);
	
	#________
	#Insert a separator tool item	
	$toolbar->insert(Gtk2::SeparatorToolItem->new ,-1 );
	
	#________
	#Insert a toggle tool button to toggle the "set_show_arrow" property
		my $t_tbtn_arrow = Gtk2::ToggleToolButton->new_from_stock('gtk-go-down');
		$t_tbtn_arrow->set_label ('Show Arrow');
		$t_tbtn_arrow->signal_connect('toggled' => sub {
		
			($t_tbtn_arrow->get_active)&&($toolbar->set_show_arrow (TRUE));
			($t_tbtn_arrow->get_active)||($toolbar->set_show_arrow (FALSE));		
		});
				
	$toolbar->insert($t_tbtn_arrow,-1);
	
	#________
	#Insert a separator tool item
	$toolbar->insert(Gtk2::SeparatorToolItem->new ,-1 );
	
	#________
	#Insert a toggle tool button to toggle the "set_tooltips" property
		my $t_tbtn_tt = Gtk2::ToggleToolButton->new_from_stock('gtk-add');
		$t_tbtn_tt->set_label ('Show Tooltips');
		$t_tbtn_tt->set_active(TRUE);
		$t_tbtn_tt->set_tooltip ($tt, "Toggle Tooltips on / off", "");
		$t_tbtn_tt->signal_connect('toggled' => sub {
		
			($t_tbtn_tt->get_active)&&($toolbar->set_tooltips (TRUE));
			($t_tbtn_tt->get_active)||($toolbar->set_tooltips (FALSE));		
		});
				
	$toolbar->insert($t_tbtn_tt,-1);
	
	#________
	#Insert a separator tool item
	$toolbar->insert(Gtk2::SeparatorToolItem->new ,-1 );
	#________
	#Insert a separator tool item that will be used as an empty space
		my $space = Gtk2::SeparatorToolItem->new;
		$space->set_draw(FALSE);
		$space->set_expand(TRUE);
		
	$toolbar->insert($space,-1 );
	
	#________
	#Insert a tool item containing an entry widget
		my $tool_item = Gtk2::ToolItem->new;
			my $ent = Gtk2::Entry->new;
		$tool_item->add($ent);
	
	$toolbar->insert($tool_item,-1);
	
	#________
	#Insert a standard tool button 	
		my $t_btn_find = Gtk2::ToolButton->new_from_stock ('gtk-find');
		$t_btn_find->set_tooltip ($tt, "Find yourself", "");
		
	$toolbar->insert($t_btn_find,-1);
	$hb->add($toolbar);
	#====================================
	
$vbox->pack_start($hb,FALSE,FALSE,0);

	$vbox->show_all();
	
return $vbox;
}

sub radio_toggled {

	#this sub discovers which radio button is active and 
	#sets the toolbar's style accordingly
	my($widget,$toolbar) = @_;
	if ($widget->get_active){
	
		my $lbl = $widget->get_label;
		
		($lbl =~ m/Text/i)&&($toolbar->set_style('text'));
		($lbl =~ m/Icon/i)&&($toolbar->set_style('icons'));
		($lbl =~ m/Both/i)&&($toolbar->set_style('both'));
	
	}
}

sub ret_menu {

my $menu_edit = Gtk2::Menu->new();
	
$menu_edit->append(Gtk2::MenuItem->new('_Cut'));
$menu_edit->append(Gtk2::MenuItem->new('C_opy'));
$menu_edit->append(Gtk2::MenuItem->new('_Paste'));
	
$menu_edit->show_all();	
return $menu_edit;
	
}
