package Gtk2::Ex::Email::AddressVBox;

use warnings;
use strict;
use Gtk2;

=head1 NAME

Gtk2::Ex::Email::AddressVBox - Creates a VBox for handling email addresses similar to Claws-Mail.

=head1 VERSION

Version 0.0.1


our $VERSION = '0.0.1';


    use Gtk2::Ex::Email::AddressVBox;
    use Gtk2;
    use Data::Dumper;
    #init it
    my $avb=Gtk2::Ex::Email::AddressVBox->new();
    #get the VBox and add it
    my $vbox=Gtk2::VBox->new;
    my $vbox2=$avb->vbox;
    $vbox->pack_start($vbox2, 1, 1, 1);
    #adds a button that calls getAddresses
    my $button=Gtk2::Button->new;
    my $buttonLabel=Gtk2::Label->new('get addresses');
    $vbox->pack_start($button, 1, 1, 1);
	    								my %addresses=$avb->getAddresses;
		    							print Dumper(\%addresses);
    #add the VBox to the window
    my $window=Gtk2::Window->new;
    #run it

=head1 METHODS

=head2 new

This initiates the object.

    my $avb=Gtk2::Ex::Email::AddressVBox->new();


sub new{
	my $self={error=>undef,
	bless $self;
	return $self;

=head2 addHB

This adds a new HBox to the VBox. The HBox contains
the stuff for a single address.

Two option arguements are taken the first is the type to add
and the second is the address.

    #adds a new blank one

    #adds a new blank To one

    #adds a new blank CC one

    #adds a new blank BCC one

    #adds a new blank To one set to foo@bar
    $avd->addHB('to', 'foo@bar');


sub addHB{
	my $self=$_[0];
	my $type=$_[1];
	my $address=$_[2];

	my %hbox;

	#this is the ID of it this specific one... zero is the top and they increase going down

	#once this is set to true, it means that a new one has been added and should not be done again

	#sets the id

	#creates the first HB
	#creates the combo box to display to, cc, and bcc
	$hbox{combobox}->append_text('To: ');
	$hbox{combobox}->append_text('CC: ');
	$hbox{combobox}->append_text('BCC: ');
	$hbox{hbox}->pack_start($hbox{combobox}, 0, 1, 0);

	#sets the type
	my $typeSet=undef;
	if (defined($type)) {
		if ($type eq 'to') {
		if ($type eq 'cc') {
		if ($type eq 'bcc') {
	if (!$typeSet) {

	#adds the entry box
	$hbox{hbox}->pack_start($hbox{entry}, 1, 1, 1);

	#sets the address if needed
	if (defined($address)) {

	$hbox{entry}->signal_connect (changed => sub {
									  if ($_[1]{self}{gui}{hboxes}{ $_[1]{id} }{new} eq '0'){
										  $_[1]{self}{gui}{hboxes}{ $_[1]{id} }{new}=1;

	#adds the delete button to the hbox
	$hbox{hbox}->pack_start($hbox{del}, 0, 1, 1);
								   my @HBIDs=keys(%{ $self->{gui}{hboxes} });
								   if (defined($HBIDs[1])) {
									   $self->{gui}{hboxes}{ $_[1]{id} }{hbox}->destroy;
									   delete($self->{gui}{hboxes}{ $_[1]{id} });
								   }else {
									   $self->{gui}{hboxes}{ $_[1]{id} }{entry}->set_text('');

	#saves the hbox info
	$self->{gui}{hboxes}{ $hbox{id} }=\%hbox;

	$self->{gui}{vbox}->pack_start($hbox{hbox}, 0, 1, 1);

	return $hbox{id};

=head2 getAddresses

This gets the the addresses users have entered. Any that match
'' or /^ *$/ will be ignored.

    my %addresses=$avb->getAddresses;
        print 'To: '.join(' ', @{$addresses{to}});
        print 'CC: '.join(' ', @{$addresses{cc}});
        print 'BCC: '.join(' ', @{$addresses{bcc}});


sub getAddresses{
	my $self=$_[0];

	#this will be returned
	my %addresses;

	#these will be added later
	my @to;
	my @cc;
	my @bcc;

	my @hboxes=keys(%{$self->{gui}{hboxes}});

	#processes each one
	my $int=0;
	while (defined($hboxes[$int])) {
		my $address=$self->{gui}{hboxes}{ $hboxes[$int] }{entry}->get_text;

		my $typeInt=$self->{gui}{hboxes}{ $hboxes[$int] }{combobox}->get_active;
		#figures out the type
		my $type;
		if ($typeInt eq '0') {
		if ($typeInt eq '1') {
		if ($typeInt eq '2') {

		#controls if it will be added or not
		my $add=1;

		#if it is blank do not add it
		if ($address eq '') {

		#if it is all spaces do not add it
		if ($address=~/^ *$/) {

		#add it
		if ($add) {
			if ($type eq 'to') {
				push(@to, $address);
			if ($type eq 'cc') {
				push(@cc, $address);
			if ($type eq 'bcc') {
				push(@bcc, $address);


	#adds them to the hash
	if (defined($to[0])) {
	if (defined($cc[0])) {
	if (defined($bcc[0])) {

	return %addresses;

=head2 vbox

This creates the VBox that contains the widgets.

=head3 args hash

=head4 bcc

This is a array of BCC addresses.

=head4 cc

This is a array of CC addresses.

=head4 to

This is a array of To addresses.

    my $vbox=$avb->vbox({


sub vbox{
	my $self=$_[0];
	my %args;
		%args= %{$_[1]};

	#this is what will be returned

	#adds any To if needed
	my $int=0;
	if (defined($args{to})) {
		if (defined($args{to}[0])) {
			while (defined($args{to}[$int])) {
				$self->addHB('to', $args{to}[$int]);


	#adds any CC if needed
	if (defined($args{cc})) {
		if (defined($args{cc}[0])) {
			while (defined($args{cc}[$int])) {
				$self->addHB('cc', $args{cc}[$int]);


	#adds any BCC if needed
	if (defined($args{bcc})) {
		if (defined($args{bcc}[0])) {
			while (defined($args{bcc}[$int])) {
				$self->addHB('bcc', $args{bcc}[$int]);


	#adds the final HBox

	return $self->{gui}{vbox};

=head1 AUTHOR

Zane C. Bowers, C<< <vvelox at> >>

=head1 BUGS

Please report any bugs or feature requests to C<bug-gtk2-ex-email-addressvbox at>, or through
the web interface at L<>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.

=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Gtk2::Ex::Email::AddressVBox

You can also look for information at:

=over 4

=item * RT: CPAN's request tracker


=item * AnnoCPAN: Annotated CPAN documentation


=item * CPAN Ratings


=item * Search CPAN





Copyright 2009 Zane C. Bowers, all rights reserved.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.


1; # End of Gtk2::Ex::Email::AddressVBox