Re: turn on italics in TextView



​Thanks again.  That works, but ​the state is lost when the cursor is
moved.  Here is some code to discover multiple styles after the cursor is
moved.  The pointers to the style tags are saved in globals, then in the
callback for the cursor move, we look for those pointers in the linked-list
returned by gtk_text_iter_get_tag().  I tried to use some of the other
link-list functions/macros, such as g_slist_position, but could only
get gtk_text_iter_get_tag() to work.  Maybe there's a more efficient way to
do this.

// globals
GSList *gtk_text_iter_get_tags (const GtkTextIter *iter);
GtkTextTag *italp, *boldp, *ulinep;

// call back after cursor is moved
  void
get_style(GtkTextView *tv, GtkMovementStep step,
        gint count, gboolean extend, gpointer *user_data)  {
GSList *tlist;
GtkTextIter start;
GtkTextMark *mark;
GtkTextBuffer *buf;

buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(tv));

mark = gtk_text_buffer_get_insert(buf);
gtk_text_buffer_get_iter_at_mark (buf, &start, mark);

tlist = gtk_text_iter_get_tags(&start);

// look for each style -- could used to set bits in a bitfield
if (g_slist_find(tlist, italp))
    g_printerr("got match for ital\n");
if (g_slist_find(tlist, boldp))
    g_printerr("got match for bold\n");
if (g_slist_find(tlist, ulinep))
    g_printerr("got match for uline\n");

}

. . .
main(...)
 . . .
italp = gtk_text_buffer_create_tag(buffer, "ital", "style",
PANGO_STYLE_ITALIC, NULL);
boldp =gtk_text_buffer_create_tag(buffer, "bold", "weight",
PANGO_WEIGHT_BOLD, NULL);
ulinep = gtk_text_buffer_create_tag(buffer, "uline", "underline",
PANGO_UNDERLINE_SINGLE, NULL);

g_signal_connect_after(text_view, "move-cursor", G_CALLBACK(get_style),
NULL);
. . .



On Wed, Jun 14, 2017 at 10:48 AM, <cecashon aol com> wrote:


Here are a few things that might improve the above code a little. Use

gtk_button_set_focus_on_click(GTK_BUTTON(toggle1), FALSE);

instead of a grab. Also the global gboolean can be eliminated if you pass
the toggle button pointer to the "insert-text" callback. Then you can just
use

if(gtk_toggle_button_get_active(user_data))

For the sequence of events it is my understanding that the text changes
will be made during the event cycle before the window gets re-painted.

https://developer.gnome.org/gtk3/stable/chap-drawing-model.html

Eric



-----Original Message-----
From: Doug McCasland <dougm bravoecho net>
To: cecashon <cecashon aol com>
Sent: Tue, Jun 13, 2017 4:09 pm
Subject: Re: turn on italics in TextView

cecashon, thanks so much for your great reply!

I had tried all those calls, but I hadn't put them together as you did,
nor did I apply the tag in the way your code does.  And I didn't think of
having one signal/function for the button-down and another for the
apply_tag_by_name.  Cool!

So your code gets a signal after the character(s) is entered. Then the
style is applied to that char, without moving the insert point.  Do you
know if the un-tagged char is visibly displayed first, and then altered?
Or is all the processing somehow finished before the display changes, and
then only the tagged char is put on the screen.






-- 
Doug McCasland   <dougm bravoecho net>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]