Object Oriented PHP-GTK2 - Aligning Labels Naturally Object Oriented PHP-GTK2 - Aligning Labels Naturally
it's me, bob. lol. Object Oriented PHP-GTK2 OOPS

Aligning Labels Naturally

Align them without GtkAlignment containers.

Topic: GtkLabel (View All Tutorials)
Keywords: gtklabel set_alignment float
Updated: 304 Days Ago, 2008/02/01 23:02

This is perhaps one of the most simple tutorial topics I could possibly think of... but you have no idea how long this has eluded me, and I would end up taking the hard way to aligning the text in my labels. I was just asked about doing this the other day, so thankfully I was not the only one staring the manual point blank and just not seeing it.

The method we need is the set_alignment() from one of GtkLabel's parent classes, GtkMisc. It takes two argument, an X float and a Y float. To do this, know that 0 0 is top left, and 1 1 is bottom right. The default behaviour is to float the text smack in the middle when the label is larger than the text it contains.

See It Quick

  • set_alignment(0, 0); // float text top left.
  • set_alignment(0.5, 0.5); // float text to the centre.
  • set_alignment(1, 1); // float text bottom right.

More Generic Examples

These generic examples work well, for instance if you needed a label to stretch with the window, remain right aligned against a GtkEntry and also stay vertically centred on it, you would want the generic right align.

GtkLabel To The Right

  • set_alignment(0, 0.5); // generic left align.
  • set_alignment(1, 0.5); // generic right align.
  • set_alignment(0.5, 0); // generic top align.
  • set_alignment(0.5, 1); // generic bottom align.

 

Demo Application

#!/usr/bin/php -c/etc/gtk/php.ini
<?php

class bobWindow extends GtkWindow {

    public 
$vbox,$hbox;
    public 
$lbl;
    public 
$ent;

    public function 
__construct() {
        
parent::__construct();
        
        
$this->vbox = new GtkVBox;
        
$this->hbox = new GtkHBox;
        
$this->lbl = new GtkLabel;
        
$this->ent = new GtkEntry;
        
        
$this->lbl->set_text("Entry:");
        
$this->lbl->set_alignment(10.5);
        
// ^ this set the label to a generic right alignment to be positioned
        // nice against the GtkEntry widget.

        
$this->set_position(Gtk::WIN_POS_CENTER);
        
$this->set_title('Quick GtkLabel Alignment');
        
$this->set_size_request(300,75);
        
$this->connect_simple('delete-event',array($this,'on_quit'));
        
        
$this->hbox->pack_start($this->lbl,true,true,3);
        
$this->hbox->pack_start($this->ent,false,false,3);
        
$this->vbox->pack_start($this->hbox,true,true,3);
        
$this->add($this->vbox);        
        
$this->show_all();
        return;
    }
    
    public function 
on_quit() {
        
$this->hide();
        
Gtk::main_quit();
        return;
    }
}

$w = new bobWindow;
Gtk::main();
$w->destroy();
exit(
0);

?>

 

End Game

Was this document helpful? I appreciate your feedback.
What are the rules about reusing this code?


i can has web two point ooh // copyright © 2007-2008 bob majdak jr
[ xhtml css | firefox ie7 opera ]