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:

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:
- Get the tag table name.
- Get the start and end iters of the selected text.
- 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 7 users) Note: You have to be a registered member to leave a comment. Free registration here. |
|
June 26, 2007 9: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
June 26, 2007 9: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?
June 26, 2007 7:35pm
php-gtk2 On Linux Slackware
June 26, 2007 8: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.
March 04, 2008 11:09am
How can i save all applied styles as html on to a txt file ???
April 03, 2008 4:19am
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.
April 08, 2008 10:39am
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.