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:

How to insert and display text at end of GtkTextView?


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:

  1. Go to end of buffer.
  2. Insert text.
  3. Scroll to end of text.

Related Links

User reviews   Average user ratings:    5.0   (from 3 users)
  1. David
    February 26, 2007 8:16pm

    Great !!! Exactly what I needed ! Thank you.

  2. t73net
    September 13, 2007 6: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.

  3. saul_fossil
    April 03, 2008 12:29pm

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

 
< Prev   Next >

Copyright © 2006-2008. kksou.com. All Rights Reserved