Problem
You want to insert and display text at the end of the GtkTextView as shown below:
Solution
- Use GtkWidget::key-press-event() to detect when user press Enter.
- Get the newly typed text with GtkTextbuffer::get_text().
- Use the method as described in How to insert and display text at end of GtkTextView? to append the newly typed message to the end of the convesation window.
Sample Code
Note: If you have installed php-gtk2 using Gnope Installer on Windows, and if running the sample code below gives you warning that the Symbolic names for keys (e.g. Gdk::KEY_Return) is not defined, you might want to update your php-gtk2 with the latest php-gtk2.dll available here. Simply download the php-gtk2.dll and replace the copy in the folder php-gtk2xt. The latest compilation has put in the Symbolic names for keys listed here.
Note: Click the "Insert Text" toolbar button to insert text at the end of textview.
1 2 3 4 5 8 9 10 11 12 13 14 15 16 17 22 23 24 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 45 46 47 49 50 51 52 53 54 55 56 57 58 59 60 61 62 64 65 66 67 68 69 70 71 72 73 74 75 76 | <?php $window = new GtkWindow(); $window->set_size_request(400, 300); $window->connect_simple('destroy', array('Gtk','main_quit')); $window->add($vbox = new GtkVBox()); $vbox->pack_start($hbox = new GtkHBox(), 0, 0); $hbox->set_size_request(-1, 2); // Create the top textview to display the conversation $buffer1 = new GtkTextBuffer(); $view1 = new GtkTextView(); $view1->set_buffer($buffer1); $view1->modify_font(new PangoFontDescription("Arial 9")); $view1->set_wrap_mode(Gtk::WRAP_WORD); $scrolled_win1 = new GtkScrolledWindow(); $scrolled_win1->set_policy( Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC); $frame1 = new GtkFrame(); $frame1->add($scrolled_win1); $vbox->pack_start($frame1, 0, 0); $scrolled_win1->add($view1); $scrolled_win1->set_size_request(400,200); $vbox->pack_start($hbox = new GtkHBox(), 0, 0); $hbox->set_size_request(-1, 1); // Create the bottom textview to type your message $buffer2 = new GtkTextBuffer(); $view2 = new GtkTextView(); $view2->set_buffer($buffer2); $view2->modify_font(new PangoFontDescription("Arial 9")); $view2->set_wrap_mode(Gtk::WRAP_WORD); $scrolled_win2 = new GtkScrolledWindow(); $scrolled_win2->set_policy( Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC); $frame2 = new GtkFrame(); $frame2->add($scrolled_win2); $vbox->pack_start($frame2, 0, 0); $scrolled_win2->add($view2); $view2->connect('key-press-event', 'on_key_press', $view1, $buffer1, $view2, $buffer2); // note 1 $scrolled_win2->set_size_request(400,80); $view2->grab_focus(); $window->show_all(); Gtk::main(); function on_key_press($widget, $event, $view1, $buffer1, $view2, $buffer2) { if ($event->keyval==Gdk::KEY_Return) { if ($event->state & Gdk::SHIFT_MASK) return false; // note 4 $input = $buffer2->get_text($buffer2->get_start_iter(), $buffer2->get_end_iter()); // note 2 $iter = $buffer1->get_end_iter(); // note 3 $buffer1->insert($iter, "You say: $input\n\n" ); // note 3 $view1->scroll_to_mark($buffer1->get_insert(), 0); // note 3 $buffer2->set_text(''); // empty buffer2 return true; } else { return false; } } ?> |
Output
As shown above.Explanation
The above example make use of the sample code in How to insert and display text at end of GtkTextView?
What's new here:
- Grab the Enter key with key-press-event.
- Get the new message typed.
- Go to end of buffer, insert the text, and scroll to end of text.
- If it's shift-Enter, leave it as line breaks.
Read more...