Problem
You want to paste text from clipboard into GtkTextView by pressing the Insert key, and you want any html tags to be removed before the text are inserted as shown below:
Solution
- In Part 1, we use GtkTextbuffer::paste_clipboard().
- As we need to do some processing before inserting the text, we use the method GtkClipboard::wait_for_text() to get the text first, strip away any html tags, then insert the text using GtkTextbuffer::insert_at_cursor()
Sample Code
Note: Copy some text into clipboard from other applications, then press the Insert button. Text will be inserted at the cursor position.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 18 19 20 21 22 23 24 25 26 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 48 49 50 53 54 57 59 60 61 62 63 64 65 66 67 | <?php $window = new GtkWindow(); $window->set_size_request(400, 240); $window->connect_simple('destroy', array('Gtk','main_quit')); $window->add($vbox = new GtkVBox()); // display title $title = new GtkLabel("Paste Text from clipboard into GtkTextView - Part 2\n". "Press Insert to paste the text.\n". "Will remove all html tags"); $title->modify_font(new PangoFontDescription("Times New Roman Italic 10")); $title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff")); $title->set_size_request(-1, 60); $title->set_justify(Gtk::JUSTIFY_CENTER); $alignment = new GtkAlignment(0.5, 0, 0, 0); $alignment->add($title); $vbox->pack_start($alignment, 0, 0); $vbox->pack_start(new GtkLabel(), 0, 0); // 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); $clipboard = new GtkClipboard($view->get_display(), Gdk::atom_intern('CLIPBOARD')); // note 1 $window->connect('key-press-event', 'on_keypress'); $window->show_all(); Gtk::main(); // process toolbar function on_keypress($widget, $event) { if ($event->keyval==Gdk::KEY_Insert) { // note 2 // use if ($event->keyval!=65379) if your version of php-gtk2 // does not have Gdk::KEY_Insert defined global $buffer, $clipboard; if ($clipboard->wait_is_text_available()) { // note 3 $text = $clipboard->wait_for_text(); // note 4 $text = strip_tags($text); // note 5 $buffer->insert_at_cursor($text); // note 6 } return true; } else { return false; } } ?> |
Output
As shown above.Explanation
- Set up the clipboard.
- Check if insert key is pressed.
- Check if there's any text in the clipboard.
- Get the text from the clipboard.
- Remove all the html tags.
- Insert the text at the cursor position.
Note
Note that you can still press Ctrl-V to paste the text. The difference is that Ctrl-V will paste the tags as it is, i.e. with all the html tags. However, if you use the Insert key, the html tags will be removed.
Read more...