c - How to connect GtkTextView to GtkMenuItem -


I am using Glade, GtkBuilder, and C in GTK as a trivial text editor (as an exercise). Also edit a GTKtextview along with cut, copy and paste for the text. GtkTextView binds automatically cut / copy / paste sequences (and also provides menu items if you click OK). I want to add cut, copy, and paste menu items to my text view in my menu bar so that they can do the desired work.

Note that this is a two-way connection (1) When a copy of Cut, Copy or Paste GTK menu is active, it tells GtkTextView to do something. (2) When a selection is created or cleared in GtkTextView, cut and copy are enabled or disabled respectively (see Gedit as an example). Apart from this, (3) Paste is enabled or not, depending on the status of the clipboard.

How do I add my Cut, Copy, and Paste Menu items to text view? Is there any way to do this in the glade, or do my C program want some extra code for it? Is there a systematic way to do this or do I need to apply all three behaviors manually described in the paragraph above?

You will need some additional code, but no more. For this example I am assuming that you are using GtkAction s for your menu items, but if you do not have the solution then it should be same.

Callback, Cut, and Paste to First Cut Action in the Glade When you connect a signal, at least pointers cut, copy, and paste the action, and the text Pass as user data in the view.

  void on_cut (GtkAction * action, SomeStruct * data) {g_signal_emit_by_name (data-> view, "cut-clipboard", NULL); Gtk_action_set_sensitive (Data-> Paste_action, TRUE); } Zero on_copy (GtkAction * Action, SomeStruct * Data) {g_signal_emit_by_name (Data-> View, "Copy-Clipboard", NULL); Gtk_action_set_sensitive (Data-> Paste_action, TRUE); } Zero on_paste (GtkAction * action, someStruct * data) {g_signal_emit_by_name (data-> view, "paste-clipboard", NULL); }  

Next, connect to your GtkTextBuffer notify: signal-selection :

  Zero on_has_selection_notify (GtkTextBuffer * buffer, GParamSpec * pspec, SomeStruct * data) {gboolean has_selection = gtk_text_buffer_get_has_selection (buffer); Gtk_action_set_sensitive (Data-> Cut_Action, is_assess); Gtk_action_set_sensitive (Data-> Copy_Action, is_assigned); }  

To determine the initial position of the paste action (the text is on the clipboard when you start the application) when you create your interface, run the following code:

  gtk_action_set_sensitive (Data-> Paste_action, gtk_clipboard_wait_is_text_available (gtk_clipboard_get (GDK_SELECTION_CLIPBOARD)));  

It assumes that there is no way to manually clean the clipboard (most programs do not allow you to do this.)

Caveat Lector: Code All typed here are unheard of.


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -