Textview Formatierung



  • Hallo,

    bin recht neu in C.
    Zu zeit mache ich was mit GTK eine Art Editor.

    Kurz zur Problembeschreibung:

    Ich kann sowohl Fett Formatieren als auch Kursiv etc. Das geht alles.
    Aber sobald ich diese Formatierung zurück zur Normal Schrift setzen will.
    Kann ich diese nicht wieder Fett machen.

    "bla bla bla"(fett) -> "bla bla bla"(normal) -> "bla bla bla"(fett bleibt aus)
    mit italic und underline das selbe.
    --------------

    kleines Code Beispiel:

    gtk_text_buffer_create_tag (buffer, "bold","weight", PANGO_WEIGHT_BOLD, NULL);
    gtk_text_buffer_create_tag (buffer, "unbold","weight", PANGO_WEIGHT_NORMAL, NULL);
    
    ...
    ...
    ...
    
    g_signal_connect ( (gpointer) button_bold, "clicked",G_CALLBACK (setBold),(gpointer) textview);
    g_signal_connect ( (gpointer) button_normal, "clicked",G_CALLBACK (setNormal),(gpointer) textview);	
    ...
    ...
    ...
    static gboolean setBold(GtkWidget *widget, gpointer data)
    {
      GtkTextBuffer *textbuffer = NULL;
      GtkTextIter start, end;
    
      textbuffer=gtk_text_view_get_buffer(GTK_TEXT_VIEW(data));
    gtk_text_buffer_get_selection_bounds(textbuffer,&start,&end);                                      
      gtk_text_buffer_apply_tag_by_name ( textbuffer, "bold", &start, &end);
    
    }
    static gboolean setNormal(GtkWidget *widget, gpointer data)
    {
    
    	GtkTextBuffer *textbuffer = NULL;
    	GtkTextIter start, end;
    
    	textbuffer=gtk_text_view_get_buffer(GTK_TEXT_VIEW(data));
    
    	gtk_text_buffer_get_selection_bounds (textbuffer,&start,&end);
                                            */
    	gtk_text_buffer_apply_tag_by_name ( textbuffer, "unbold",&start, &end);
    
    }
    

    Hat da jemand eine Lösung wieso es nicht hin/her schaltet zwischen den zwei Formatierungen? (wenn ihr mehr Infos braucht, sagt bescheid)

    Muss ich irgendwas anders zurücksetzen oder refreshen? Habe es auch in vielen Code Beispielen so ähnlich gesehen.

    Danke schonmal für Antworten.

    Bye Bye



  • Dieser Thread wurde von Moderator/in rüdiger aus dem Forum ANSI C in das Forum Andere GUIs - Qt, GTK+, wxWidgets verschoben.

    Im Zweifelsfall bitte auch folgende Hinweise beachten:
    C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?

    Dieses Posting wurde automatisch erzeugt.



  • Hm, hab grad in meinem alten gtkmm code nachgesehen... da wähle ich die gleiche Vorgehensweise und es funzt... hast du vllt. ein kompaktes, kompilierfähiges Beispiel?



  • Hallo,

    glaube gibt auch im Internet ein Beispiel.. welches ich getestet hab und auch nicht ging. Aber vom Quellcode prinzipell das selbe war.

    http://www.bravegnu.org/gtktext/x113.html#AEN248



  • Habe das Beispiel aus dem Link mal etwas erweitert und es läuft tadellos.
    Die Funktion "on_clear_button_clicked" geht jetzt einfach hin und entfernt die "bold"-tags aus der momentanen Auswahl.

    #include <gtk/gtk.h>
    
    void
    on_window_destroy (GtkWidget *widget, gpointer data)
    {
      gtk_main_quit ();
    }
    
    /* Callback for buttons in the toolbar. */
    void
    on_format_button_clicked (GtkWidget *button, GtkTextBuffer *buffer)
    {
      GtkTextIter start, end;
      gchar *tag_name;
    
      /* Get iters at the beginning and end of current selection. */
      gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
      /* Find out what tag to apply. The key "tag" is set to the
         appropriate tag name when the button widget was created. */
      tag_name = g_object_get_data (G_OBJECT (button), "tag");
      /* Apply the tag to the selected text. */
      gtk_text_buffer_apply_tag_by_name (buffer, tag_name, &start, &end);
    }
    
    void on_clear_button_clicked (GtkWidget *button, GtkTextBuffer *buffer) {
    	GtkTextIter start, end;	
    	gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
    
    	gchar *tag_name = "bold";
    	gtk_text_buffer_remove_tag_by_name(buffer, tag_name, &start, &end);
    }
    
    /* Callback for the close button. */
    void
    on_close_button_clicked (GtkWidget *button, GtkTextBuffer *buffer)
    {
      GtkTextIter start;
      GtkTextIter end;
    
      gchar *text;
    
      /* Get iters at the beginning and end of the buffer. */
      gtk_text_buffer_get_bounds (buffer, &start, &end);
      /* Retrieve the text. */
      text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
      /* Print the text. */
      g_print ("%s", text);
    
      g_free (text);
    
      gtk_main_quit ();
    }
    
    int 
    main(int argc, char *argv[])
    {
      GtkWidget *window;
      GtkWidget *vbox;
      GtkWidget *bbox;
    
      GtkWidget *bold_button;
      GtkWidget *italic_button;
      GtkWidget *font_button;
      GtkWidget *clear_button;
    
      GtkWidget *text_view;
      GtkTextBuffer *buffer;
    
      GtkWidget *close_button;
    
      gtk_init (&argc, &argv);
    
      window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
      gtk_window_set_title (GTK_WINDOW (window), "Formatted multiline text widget");
      gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
      g_signal_connect (G_OBJECT (window), "destroy", 
                        G_CALLBACK (on_window_destroy),
                        NULL);
    
      vbox = gtk_vbox_new (FALSE, 2);
      gtk_container_add (GTK_CONTAINER (window), vbox);
    
      /* Create a button box that will serve as a toolbar. */
      bbox = gtk_hbutton_box_new ();
      gtk_box_pack_start (GTK_BOX (vbox), bbox, 0, 0, 0);
    
      /* Create the text view widget and set some default text. */
      text_view = gtk_text_view_new ();
      gtk_box_pack_start (GTK_BOX (vbox), text_view, 1, 1, 0);
      buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
      gtk_text_buffer_set_text (buffer, "Hello Text View!", -1);
    
      /* Create tags associated with the buffer. */
      /* Tag with weight bold and tag name "bold" . */
      gtk_text_buffer_create_tag (buffer, "bold", 
                                  "weight", PANGO_WEIGHT_BOLD, 
                                  NULL);
      /* Tag with style italic and tag name "italic". */
      gtk_text_buffer_create_tag (buffer, "italic",
                                  "style", PANGO_STYLE_ITALIC,
                                  NULL);
      /* Tag with font fixed and tag name "font". */
      gtk_text_buffer_create_tag (buffer, "font",
                                  "font", "fixed", 
                                  NULL); 
    
      /* Create button for bold and add them the to the tool bar. */
      bold_button = gtk_button_new_with_label ("Bold");
      gtk_container_add (GTK_CONTAINER (bbox), bold_button);
      /* Connect the common signal handler on_format_button_clicked. This
         signal handler is common to all the buttons. A key called "tag"
         is associated with the buttons, which speicifies the tag name
         that the button is supposed to apply. The handler reads this key
         and applies the appropriate tag. Thus only one handler is needed
         for any number of buttons in the toolbar. */
      g_signal_connect (G_OBJECT (bold_button), "clicked",
                        G_CALLBACK (on_format_button_clicked),
                        buffer);
      g_object_set_data (G_OBJECT (bold_button), "tag", "bold");
    
      /* Create button for italic. */
      italic_button = gtk_button_new_with_label ("Italic");
      gtk_container_add (GTK_CONTAINER (bbox), italic_button);
      g_signal_connect (G_OBJECT (italic_button), "clicked",
                        G_CALLBACK (on_format_button_clicked),
                        buffer);
      g_object_set_data (G_OBJECT (italic_button), "tag", "italic");
    
      /* Create button for fixed font. */
      font_button = gtk_button_new_with_label ("Font Fixed");
      gtk_container_add (GTK_CONTAINER (bbox), font_button);
      g_signal_connect (G_OBJECT (font_button), "clicked",
                        G_CALLBACK (on_format_button_clicked),
                        buffer);
      g_object_set_data (G_OBJECT (font_button), "tag", "font");
    
      clear_button = gtk_button_new_with_label ("Clear bold");
      gtk_container_add (GTK_CONTAINER (bbox), clear_button);
      g_signal_connect (G_OBJECT (clear_button), "clicked",
    					G_CALLBACK (on_clear_button_clicked),
    								buffer);
    
      /* Create the close button. */
      close_button = gtk_button_new_with_label ("Close");
      gtk_box_pack_start (GTK_BOX (vbox), close_button, 0, 0, 0);
      g_signal_connect (G_OBJECT (close_button), "clicked", 
                        G_CALLBACK (on_close_button_clicked),
                        buffer);
    
      gtk_widget_show_all (window);
    
      gtk_main ();
      return 0;
    }
    

    Cheers

    GPC



  • Hallo,

    Vielen Dank es geht in dem Beispiel. Ich versuch es jetzt mal einzubauen 🙂

    *virtuelles Bier ausgeb*

    😃

    bb


Anmelden zum Antworten