Labels are used a lot in Ruby-GNOME2, and are relatively simple. Labels emit no signals as they do not have an associated X window. If you need to catch signals, or do clipping, place it inside a Gtk::EventBox widget or a Gtk::Button widget.
To create a new label, use one of the following:
Gtk::Label.new( label ) Gtk::Label.new( label, use_mnemonic ) Gtk::Label.new
The first method's argument of label, is the string that you wish to display. In the second method, you will notice an extra argument of use_mnemonic, which allows you to assign a mnemonic (underline accelerator) to the label. It accepts a boolean true or false. The third, and final method simply creates a blank label.
To change the label's text after creation, use the method:
Gtk::Label#set_text( "New line of text" ) Gtk::Label#label=( "Another line of text" )
The only argument is the new string you wish to change the label to.
The space needed for the new string will be automatically adjusted if needed. You can produce multi-line labels by putting line breaks in the label string.
To retrieve the current string, use one of these methods:
Gtk::Label#label
The label text can be justified using:
Gtk::Label#set_justify( justify_type ) Gtk::Label#justify=( justify_type )
The only argument is justify_type, which accepts the following values:
Gtk::JUSTIFY_LEFT Gtk::JUSTIFY_RIGHT Gtk::JUSTIFY_CENTER Gtk::JUSTIFY_FILL
Gtk::JUSTIFY_LEFT places the text at the left edge of the label. Gtk::JUSTIFY_RIGHT is just the opposite and places the text at the right edge of the label. Gtk::JUSTIFY_CENTER centers the entire line of text with in the label. Last, but not least, is Gtk::JUSTIFY_FILL, in which the text is distributed across the label evenly.
The label widget is also capable of line wrapping the text automatically. This can be activated using:
Gtk::Label#set_wrap( wrap ) Gtk::Label#wrap=( wrap )
The wrap argument takes a boolean true or false value.
If you want your label underlined, then you can set a pattern on the label:
Gtk::Label#set_pattern( pattern ) Gtk::Label#pattern=( pattern )
The pattern argument indicates how the underlining should look. It consists of a string of underscroe and space characters. An underscrore indicates that the corresponding character in the label should be underlined. For example, the string "__ __" would underline the first two characters, and the eighth and ninth characters.
If you simply want to have an underlined accelerator ("mnemonic") in your label, you should use ????? or ??????, not Gtk::Label#set_pattern.
Below is a short example to illustrate these methods. This example makes use of the Frame widget to better demonstrate the label styles. You can ignore this for now as the Frame widget is explained later on.
In Ruby-GNOME2, label texts can contain markup for font and other text attribute changes, and labels may be selectable (for copy-and-paste). These advanced features won't be explained here.
require 'gtk2' # Intialise GTK Gtk.init window = Gtk::Window.new( Gtk::Window::TOPLEVEL ) window.signal_connect( "destroy" ) { Gtk.main_quit } window.title=( "Label" ) vbox = Gtk::VBox.new( false, 5 ) hbox = Gtk::HBox.new( false, 5 ) window.add( hbox ) window.set_border_width( 5 ) frame = Gtk::Frame.new( "Normal Label" ) label = Gtk::Label.new( "This is a normal label." ) frame.add( label ) vbox.pack_start( frame, false, false, 0 ) frame = Gtk::Frame.new( "Multi-line Label" ) label = Gtk::Label.new( "This is a multi-line label.\nSecond line.\n" ) frame.add( label ) vbox.pack_start( frame, false, false, 0 ) frame = Gtk::Frame.new( "Left Justified Label" ) label = Gtk::Label.new( "This is a left-justified\nmulti-line label.\nThird line") label.justify=( Gtk::JUSTIFY_LEFT ) frame.add( label ) vbox.pack_start( frame, false, false, 0 ) frame = Gtk::Frame.new( "Right Justified Label" ) label = Gtk::Label.new( "This is a right-justified\nmulti-line label.\nFourth line, (j/k)" ) label.justify=( Gtk::JUSTIFY_RIGHT ) frame.add( label ) vbox.pack_start( frame, false, false, 0 ) hbox.pack_start( vbox, false, false, 0 ) vbox = Gtk::VBox.new( false, 5 ) hbox.pack_start( vbox, false, false, 0 ) frame = Gtk::Frame.new( "Line wrapped label" ) label = Gtk::Label.new( "This is an example of a line-wrapped label. It " + "should not be taking up the entire " + " width allocated to it, but automatically " + "wraps the words to fit. " + "The time has come for all good men, to come to " + "the aid of thier party. " + "The sixth sheik's six sheep's sick.\n" + "It supports multiple paragraphs correctly, " + "and correctly adds " + "many extra spaces. " ) label.set_wrap( true ) frame.add( label ) vbox.pack_start( frame, false, false, 0 ) frame = Gtk::Frame.new( "Filled, wrapped label" ) label = Gtk::Label.new( "This is an example of a line-wrapped, filled label. " + "It should be taking " + "up the entire width allocated to it. " + "Here is a sentence to prove " + "my point. Here is another sentence. " + "Here comes the sun, do de do de do.\n" + " This is a new paragraph.\n" + " This is another newer, longer, better " + "paragraph. It is coming to an end, " + "fortunately." ) label.wrap=( true ) label.justify=( Gtk::JUSTIFY_FILL ) frame.add( label ) vbox.pack_start( frame, false, false, 0 ) frame = Gtk::Frame.new( "Underlined Label" ) label = Gtk::Label.new( "This label is underlined!\n" + "This one is underlined in quite a funky fashion" ) label.justify=( Gtk::JUSTIFY_LEFT ) label.set_pattern( "_________________________ _ _________________ _ ____________ _ _ ________" ) frame.add( label ) vbox.pack_start( frame, false, false, 0 ) # This is a shorthand way to display all widgets packed # into the window. This avoids having to show each and # every widget when you are done with them. window.show_all Gtk.main
Prev | Next |