PHP-GTK2 Newsletter
PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou
Sample Code 87: How to insert and display text at end of GtkTextView? |
|
Written by kksou
|
|
Monday, 06 November 2006 |
|
Problem You want to insert and display text at the end of the GtkTextView as shown below:

Solution
Sample Code Note: Click the "Insert Text" toolbar button to insert text at the end of textview.
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
| <?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('Insert Text'); 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);
$scrolled_win = new GtkScrolledWindow(); $scrolled_win->set_policy( Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC); $vbox->pack_start($scrolled_win);
$scrolled_win->add($view);
$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); } else { $stock_image_name = 'Gtk::STOCK_'.strtoupper($item); if (defined($stock_image_name)) { $toolbar_item = GtkToolButton::new_from_stock(constant($stock_image_name)); } else { $toolbar_item = new GtkToolButton(); // no stock item
$toolbar_item->set_label("\n".$item); // just display the text label
} $toolbar->insert($toolbar_item, -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:
- Go to end of buffer.
- Insert text.
- Scroll to end of text.
Related Links
User reviews Average user ratings: 4.5 (from 8 users) Note: You have to be a registered member to leave a comment. Free registration here. |
|
February 26, 2007 7:16pm
Great !!! Exactly what I needed ! Thank you.
September 13, 2007 5:43am
Good example. One note though, If using glade files for the GUI Construct, be sure that your textView widget is not contained inside of a viewport. This is the default action for Glade 3.2 and gave me a ton of grief until I realized that I had 1 extra widget in my glade file compared to the example above.
April 03, 2008 11:29am
April 08, 2009 9:37am
April 13, 2009 7:36pm
May 24, 2009 4:40am
June 23, 2009 12:13pm
Thanks, works great !
About t73net comment : With glade 4 and a TextView inside a ScrolledWindow, the above code works without problem.
June 02, 2010 8:02am
Thanks, was a great help :)