Colors as list in Combo-box?? - Linux

This is a discussion on Colors as list in Combo-box?? - Linux ; How it is possible to create a combo-box consists of different colors as list (instead of text list) in GTK+2.0? Please someone help. And thanking you....

+ Reply to Thread
Results 1 to 3 of 3

Thread: Colors as list in Combo-box??

  1. Colors as list in Combo-box??

    How it is possible to create a combo-box consists of different colors
    as list (instead of text list) in GTK+2.0?
    Please someone help. And thanking you.


  2. Re: Colors as list in Combo-box??

    On 16 Dec 2006 21:41:20 -0800, "saketram" wrote:

    >How it is possible to create a combo-box consists of different colors
    >as list (instead of text list) in GTK+2.0?
    >Please someone help. And thanking you.

    Try this:

    #! /usr/bin/perl
    use warnings;
    use strict;
    use Gtk2 '-init';
    use Glib qw/TRUE FALSE/;
    use MIME::Base64;
    use Data:umper;

    #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);

    my $list_store = Gtk2::ListStore->new ('Gtk2::Gdk::Pixbuf',
    'Glib::String');

    my ($pixbuf_b, undef,undef) = get_bunny_buf('b');
    my ($pixbuf_r, undef,undef) = get_bunny_buf('r');
    my ($pixbuf_g, undef,undef) = get_bunny_buf('g');
    my @pbufs = ($pixbuf_b, $pixbuf_r, $pixbuf_g);


    #fill it with arbitry data
    foreach (0..49) {
    push (@pbufs ,shift(@pbufs));
    my $iter = $list_store->append;
    # Usage: $liststore->set ($iter, column1, value1, column2,
    value2, ...)
    #$list_store->set ($iter, 0 =>$pbufs[0] , 1 => "ZZZ x
    $_");
    $list_store->set ($iter, 0 =>$pbufs[0] );
    $list_store->set ($iter, 1 => "ZZZ x $_");
    }


    # my $combo_box = Gtk2::ComboBox->new_text; #will cause slew of
    errors with pixbufs
    # $combo_box->set_model($list_store);

    my $combo_box = Gtk2::ComboBox->new($list_store);

    my $renderer = Gtk2::CellRendererPixbuf->new;
    $combo_box->pack_start ($renderer, FALSE);
    $combo_box->add_attribute ($renderer, pixbuf => 0);

    $renderer = Gtk2::CellRendererText->new;
    $combo_box->pack_start ($renderer, TRUE);
    $combo_box->add_attribute ($renderer, text => 1);


    $combo_box->set_wrap_width (5);
    $combo_box->signal_connect('changed' => sub {
    my ($combo_box) = @_;
    my $iter = $combo_box->get_active_iter;
    print 'iter-> ',$combo_box->get_active_iter,"\n";
    print 'index-> ',$combo_box->get_active,"\n";

    #only works easily when text is first in cell
    # print 'text-> ', $combo_box->get_active_text,"\n";

    print "\n";
    });

    $combo_box->set_active(0);

    $vbox->pack_start($combo_box,TRUE,TRUE,0);
    $vbox->show_all();
    return $vbox;
    }

    ################################################## ############
    sub get_bunny_buf{

    my $type = shift;

    my $bunny_b = decode_base64(
    'iVBORw0KGgoAAAANSUhEUgAAAB4AAAAjCAYAAACD1LrRAAAAB mJLR0QA/wD/AP+gvaeTAAAACXBI
    WXMAAAsSAAALEgHS3X78AAAAmElEQVRYw+1WQQ7AIAizxP9/mV121Gm0BZfYxNukKxSwlIPg75HG
    MrGALkykfHjPBKmf+tbIAad/0Ngp3CHGIrnvEoco7xFDTf6lGMIeH6YaBNVYrTEUKYfYUGDW0b OI
    t2qrTjVYNXa2f9SDAqt97I1AlMFST9pOIbjEYRuKsSS4fUZydc qSQOTzVubq/w+QmmVWtmIvFx08
    tNghLUXwK/sAAAAASUVORK5CYII=');

    my $bunny_r = decode_base64(
    'iVBORw0KGgoAAAANSUhEUgAAAB4AAAAjCAYAAACD1LrRAAAAB mJLR0QA/wD/AP+gvaeTAAAACXBI
    WXMAAAsSAAALEgHS3X78AAAAsElEQVRYw+2WOxKAIAxEzQ5j6z k9kee0pdETiCHsAuOYFsjLPyzL
    LHKu+3Wu+6XWBaUDpXMoPPe8Azv03rtgKqwxEOwQhsFbPiwCrz UKquIJgd+8ZsAfPfbAJeCWfHt0
    IPqwNeSmLKiS0aasaCq4NbfSUHs6wkbk1wVuGRQlOGqgWz6MNV jSNNupl/zgbhsKjGEgXRKRqh6y
    JN4ihRE9PNf3tpckprKaDqB6zK6Db8kNXCVtbbOb7CYAAAAASU VORK5CYII=');

    my $bunny_g = decode_base64(
    'iVBORw0KGgoAAAANSUhEUgAAAB4AAAAjCAYAAACD1LrRAAAAB mJLR0QA/wD/AP+gvaeTAAAACXBI
    WXMAAAsSAAALEgHS3X78AAAAsUlEQVRYw+2W3Q2AIAyE7YURjI O4/wwOYthBJxBLuQNi7CvQr/9l
    WWaR9dyv9dwvtS4oHSidQ+G55x3YoffeBVNhjYFghzAMztthEX itUVAVTwj85jUD/uixBy4Bt+Tb
    owPRh60hN2VBlYw2ZUVTwa25lYba0xE2Ir8ucMugKMFRA83bYa zBkqbZTr3kB3fbUGAMA+mSiFT1
    kCXxFimM6OG5vre9JDGV1XQA1WN2HXxLbnT4as1MFhrXAAAAAE lFTkSuQmCC');

    my $bunny;

    if($type eq 'b'){ $bunny = $bunny_b }
    elsif($type eq 'r'){ $bunny = $bunny_r }
    elsif($type eq 'g'){ $bunny = $bunny_g }

    # this properly renders it
    my $pixbuf = do {
    my $loader = Gtk2::Gdk::PixbufLoader->new();
    $loader->write( $bunny );
    $loader->close();
    $loader->get_pixbuf();
    };

    my ($x, $y) = ($pixbuf->get_width, $pixbuf->get_height);

    return ($pixbuf,$x,$y);

    }
    ################################################## ###################
    __END__

    --
    I'm not really a human, but I play one on earth.
    http://zentara.net/japh.html

  3. Re: Colors as list in Combo-box??

    On 16 Dec 2006 21:41:20 -0800, "saketram" wrote:

    >How it is possible to create a combo-box consists of different colors
    >as list (instead of text list) in GTK+2.0?
    >Please someone help. And thanking you.


    Heres a slightly better version, where you can get assorted colors.
    It should give you the idea.

    #! /usr/bin/perl
    use warnings;
    use strict;
    use Gtk2 '-init';
    use Glib qw/TRUE FALSE/;
    use MIME::Base64;
    use Imager;

    my %bunnies; #global hash to hold
    build_bunnies();

    #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);

    my $list_store = Gtk2::ListStore->new ('Glib::String',
    'Gtk2::Gdk::Pixbuf');


    #fill it with arbitry data
    foreach (0..11) {

    my $iter = $list_store->append;
    # Usage: $liststore->set ($iter, column1, value1, column2,
    value2, ...)
    #$list_store->set ($iter, 0 =>$pbufs[0] , 1 => "ZZZ x
    $_");
    $list_store->set ($iter, 0 => "ZZZ x $_");
    $list_store->set ($iter, 1 => $bunnies{$_} );
    }

    # my $combo_box = Gtk2::ComboBox->new_text; #will cause slew of
    errors with pixbufs
    # $combo_box->set_model($list_store);

    my $combo_box = Gtk2::ComboBox->new($list_store);

    my $renderer = Gtk2::CellRendererText->new;
    $renderer->set (xpad => 0, xalign => 0);
    # $renderer->set_fixed_size (150, 40);

    $combo_box->pack_start ($renderer, TRUE);
    $combo_box->add_attribute ($renderer, text => 0);

    my $renderer1 = Gtk2::CellRendererPixbuf->new;


    $renderer1->set (xpad => 0, xalign => 0);
    # $renderer->set_fixed_size (35, 40);

    $combo_box->pack_start ($renderer1, FALSE);
    $combo_box->add_attribute ($renderer1, pixbuf => 1);


    $combo_box->set_wrap_width (3);
    $combo_box->signal_connect('changed' => sub {
    my ($combo_box) = @_;
    my $iter = $combo_box->get_active_iter;
    print 'iter-> ',$combo_box->get_active_iter,"\n";
    print 'index-> ',$combo_box->get_active,"\n";
    print 'text-> ', $combo_box->get_active_text."\n";
    print "\n";
    });

    $combo_box->set_active(0);

    $vbox->pack_start($combo_box,TRUE,TRUE,0);
    $vbox->show_all();
    return $vbox;
    }

    ################################################## ############
    sub build_bunnies{

    #basic black rgb = 0,0,0
    my $bunny_b = decode_base64(
    'iVBORw0KGgoAAAANSUhEUgAAAB4AAAAjCAYAAACD1LrRAAAAB mJLR0QA/wD/AP+gvaeTAAAACXBI
    WXMAAAsSAAALEgHS3X78AAAAmElEQVRYw+1WQQ7AIAizxP9/mV121Gm0BZfYxNukKxSwlIPg75HG
    MrGALkykfHjPBKmf+tbIAad/0Ngp3CHGIrnvEoco7xFDTf6lGMIeH6YaBNVYrTEUKYfYUGDW0b OI
    t2qrTjVYNXa2f9SDAqt97I1AlMFST9pOIbjEYRuKsSS4fUZydc qSQOTzVubq/w+QmmVWtmIvFx08
    tNghLUXwK/sAAAAASUVORK5CYII=');


    my $bunny;
    my $img = Imager->new;
    $img->read(data=>$bunny_b, type=>'png')
    or die "Cannot read: ", $img->errstr;

    my @rgbs = ( [0,0,0], [127,127,127], [255,255,255],
    [255,0,0], [178,0,178], [255,0,255],
    [0,255,0], [255,255,0],[255,127,0],
    [0,0,255],[0,255,255],[145,145,0],
    );

    my $count = -1;
    foreach my $tref( @rgbs ){
    $count++;

    my $newimg = $img->copy();

    my($r,$g,$b) = @{$tref};
    print "$r $g $b\n";
    my $new_color = Imager::Color->new($r, $g, $b, 255 );

    $newimg->flood_fill(x=>15, y=>15, color=>$new_color);

    #$newimg->filter(type=>'contrast', intensity=> $c);
    $newimg->write( data => \$bunny, type=>'png' );

    # this properly renders it to pixbuf
    $bunnies{$count} = do {
    my $loader = Gtk2::Gdk::PixbufLoader->new();
    $loader->write( $bunny );
    $loader->close();
    $loader->get_pixbuf();
    };
    }

    print "count -> $count\n";
    }
    ################################################## ###################
    __END__



    --
    I'm not really a human, but I play one on earth.
    http://zentara.net/japh.html

+ Reply to Thread