PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 200: How to set GtkTextView to look like GtkLabel?
Written by kksou   
Friday, 16 March 2007
Problem

A standard GtkTextView has a white background as shown below:

You would like to set the background color of the GtkTextView to the default gray color of the window so that the GtkTextView looks like a GtkLabel as shown below:

How to set GtkTextView to look like GtkLabel?


Solution

Sample Code
1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
16   
17   
18   
19   
20   
21   
22   
23   
24   
25   
26   
27   
28   
29   
<?php
$window = &new GtkWindow();
$window->connect_simple('destroy', array( 'Gtk', 'main_quit'));
$window->set_size_request(400,200);
$window->add($vbox = new GtkVBox());

// display title
$title = new GtkLabel("Set bg color of textview to default bg color");
$title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
$title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
$title->set_size_request(-1, 40);
$title->set_justify(Gtk::JUSTIFY_CENTER);
$alignment = new GtkAlignment(0.5, 0, 0, 0);
$alignment->add($title);
$vbox->pack_start($alignment, 0, 0);
$vbox->pack_start(new GtkLabel(), 0, 0);

$window->realize(); // note 1
$org_bg = $window->get_style()->bg[Gtk::STATE_NORMAL]; // note 2

// setup textview and textbuffer
$buffer = new GtkTextBuffer();
$view = new GtkTextView();
$view->set_buffer($buffer);
$view->modify_font(new PangoFontDescription("Arial 12"));
$view->set_wrap_mode(Gtk::WRAP_WORD);

$scrolled_win = new GtkScrolledWindow();
$scrolled_win->set_policy( Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
  • Note that this is only 70% of the sample code. You have to be a registered member to see the entire sample code. Please login or register.
  • Registration is free and immediate.
  • Have some doubt about the registration? Please read this forum article.
Explanation
  1. Remember to realize the window before making a copy of the original background color. Try commenting this line out and you will find that you won't get the original color.
  2. Make a copy of the original background color.
  3. Set the GtkTextView to the default gray, making it look like a GtkLabel.

Note

Note that we make use of exactly the same technique as we have used in How to set the background to original default color?


Related Links
 
< Prev   Next >

Blog - Forum - Privacy Policy - Contact Us
Copyright © 2006-2015. kksou.com. All Rights Reserved