SHARE
TWEET

Untitled

a guest Nov 19th, 2012 36 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Test text tag's */
  2. #include <gtk/gtk.h>
  3.  
  4. #define TAG_NAME "BOLD"
  5.  
  6. static void
  7. setup_iters(GtkTextBuffer *buff, GtkTextIter *start, GtkTextIter *end)
  8. {
  9.   if (!gtk_text_buffer_get_selection_bounds(buff, start, end)) {
  10.     GtkTextMark *mark = gtk_text_buffer_get_insert(buff);
  11.     gtk_text_buffer_get_iter_at_mark(buff, start, mark);
  12.     *end = *start;
  13.   }
  14. }
  15.  
  16. static void
  17. activate_tag(GtkButton *button, GtkTextView *tv)
  18. {
  19.   GtkTextBuffer *buffer;
  20.   GtkTextIter start, end;
  21.   static gboolean tag_state = FALSE;
  22.  
  23.   tag_state = !tag_state;
  24.  
  25.   buffer = gtk_text_view_get_buffer(tv);
  26.   setup_iters(buffer, &start, &end);
  27.  
  28.   if(tag_state) {
  29.     g_print("Tag mode activated\n");
  30.     gtk_text_buffer_apply_tag_by_name(buffer, TAG_NAME, &start, &end);
  31.   }
  32.   else {
  33.     g_print("Tag mode deactivated\n");
  34.     gtk_text_buffer_remove_tag_by_name(buffer, TAG_NAME, &start, &end);
  35.   }
  36. }
  37.  
  38. int main(int argc, char **argv)
  39. {
  40.   GtkWidget *main_window;
  41.   GtkWidget *button;
  42.   GtkWidget *vbox;
  43.   GtkWidget *text_view;
  44.   GtkTextBuffer *buffer;
  45.  
  46.   gtk_init(&argc, &argv);
  47.  
  48.   main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  49.   gtk_window_set_position(GTK_WINDOW(main_window), GTK_WIN_POS_CENTER);
  50.   gtk_window_set_default_size(GTK_WINDOW(main_window), 250, 200);
  51.   gtk_window_set_title(GTK_WINDOW(main_window), "Text tag's test");
  52.  
  53.   vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);// gtk+ 3.0
  54. //  vbox = gtk_vbox_new(FALSE, 0); // gtk+ 2.0
  55.   gtk_container_add(GTK_CONTAINER(main_window), vbox);
  56.  
  57.   button = gtk_button_new_with_label("Tag activate");
  58.   gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
  59.  
  60.   text_view = gtk_text_view_new();
  61.   buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_view));
  62.   gtk_text_buffer_create_tag(buffer, TAG_NAME, "weight", PANGO_WEIGHT_BOLD, NULL);
  63.   gtk_box_pack_start(GTK_BOX(vbox), text_view, TRUE, TRUE, 0);
  64.  
  65.   g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(activate_tag), text_view);
  66.  
  67.   g_signal_connect(G_OBJECT(main_window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
  68.  
  69.   gtk_widget_show_all(main_window);
  70.  
  71.   gtk_main();
  72.  
  73.   return 0;
  74. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top