PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 84: How to apply styles to GtkTextView using GtkTextTag - Part 1?
Written by kksou   
Friday, 03 November 2006
Problem

You want to apply styles to GtkTextView / GtkTextBuffer (e.g. bold, italic, underline, font color and highlight) as shown below:

How to apply styles to GtkTextView using GtkTextTag - Part 1?


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   
30   
31   
32   
33   
34   
35   
36   
37   
38   
39   
40   
41   
42   
43   
44   
45   
46   
47   
48   
49   
50   
51   
52   
53   
<?php
$window = new GtkWindow();
$window->set_size_request(400, 240);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

// define menu definition
$toolbar_definition = array('Bold', 'Italic', 'Underline', '<hr>',
    'Blue', '<hr>', 'Highlight');
setup_toolbar($vbox, $toolbar_definition);

// Create a new buffer and a new view to show the buffer.
$buffer = new GtkTextBuffer();
$view = new GtkTextView();
$view->set_buffer($buffer);
$view->modify_font(new PangoFontDescription("Arial 12"));
$view->set_wrap_mode(Gtk::WRAP_WORD);
$vbox->pack_start($view);
$view->grab_focus();

// define the tags
$tag_table = $buffer->get_tag_table(); 
// note 1
$tag['Bold'] = new GtkTextTag();
$tag['Bold']->set_property('weight', Pango::WEIGHT_BOLD);
$tag_table->add($tag['Bold']);

$tag['Italic'] = new GtkTextTag();
$tag['Italic']->set_property('style', Pango::STYLE_ITALIC);
$tag_table->add($tag['Italic']);

$tag['Underline'] = new GtkTextTag();
$tag['Underline']->set_property('underline', Pango::UNDERLINE_SINGLE);
$tag_table->add($tag['Underline']);

$tag['Blue'] = new GtkTextTag();
$tag['Blue']->set_property('foreground', "#0000ff");
$tag_table->add($tag['Blue']);

$tag['Highlight'] = new GtkTextTag();
$tag['Highlight']->set_property('background', "#ffff00");
$tag_table->add($tag['Highlight']);


$window->show_all();
Gtk::main();

// setup toolbar
function setup_toolbar($vbox, $toolbar_definition) {
    $toolbar = new GtkToolBar();
    $vbox->pack_start($toolbar, 0, 0);
    foreach($toolbar_definition as $item) {
        if ($item=='<hr>') {
            $toolbar->insert(new GtkSeparatorToolItem(), -1);
  • 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

We make use of the code in How to set up toolbar? to display the toolbar.

What's new here:

  1. Get the tag table name.
  2. Get the start and end iters of the selected text.
  3. Apply the tag!

Note

The sample code above only performs bold. When you press the bold button on a "bolded" text, it doesn't unbold. We will improve on this in Part 2.


Related Links

User reviews   Average user ratings:    4.0   (from 9 users)
  1. k8 from italy
    June 26, 2007 8:34am

    Dont Works.
    keit@darkstar:~/Desktop/PHase Current$ php-gtk2 prova
    toolbar clicked: Bold

    Warning: GtkTextBuffer::get_selection_bounds() requires exactly 2 arguments, 0 given in /home/keit/Desktop/PHase Current/prova on line 72

  2. kksou
    June 26, 2007 8:57am

    I just tried it. Works on Gnope version, the latest php-gtk2 beta release, and builds by Elizabeth Smith. Which version of php-gtk2 are you using? And on what platform?

  3. k8 from italy
    June 26, 2007 6:35pm

    php-gtk2 On Linux Slackware

  4. kksou
    June 26, 2007 7:04pm

    When was the last time you compiled your version of php-gtk2? Can you give the version number for some of the key ones - gtk, php and php-gtk2? If you have a more recent build of php-gtk2, I think it should work.

  5. José Franco
    March 04, 2008 10:09am
    Save styles

    How can i save all applied styles as html on to a txt file ???

  6. Rouven
    April 03, 2008 3:19am
    @Jose

    this will be a very nasty process as you would need the full plain text and you need to parse every thing in the gtktexttagstable->foreach() and apply the corresponding html tags to the text.

    i dunno whether there is a simple "export()" but i guess there aint no function like that.

  7. Dysmas
    April 08, 2008 9:39am
    Perfect demonstration

    I tried it and it worked perfectly out of the box. Understanding the tags is not easy for the beginner, and this example makes things very simple.

  8. telekidz
    February 26, 2009 5:27pm
    button blue change to gtkcolorselectiondialog

    hi there

    how to change button blue become gtkcolorselectiondialog so not 1 color can we change but many color.

  9. blarg
    March 01, 2009 12:16pm
    Formating text without selection?

    Hello,

    I have been trying to figure this out for a while and I guess im at the point of asking for help.

    How do you automatically format text (ie; colors, etc) with tags (?) that is being inserted in a GtkTextView widget (without the user selecting it).

    ex. How do you make the word "colorful" blue? in :

    $msg="Hello colorful world!";

    // do some color magic here

    $this->text_buffer->insert_at_cursor($msg);

    Thanks,

    Regards,

    Max.

Note: You have to be a registered member to leave a comment. Free registration here.

 
< Prev   Next >

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