Re: how to programmatically set colors of a GtkTextView



Boncek, John ha scritto lo scorso 10/02/2005 18.36:
I'm trying to set the text color and background color of a GtkTextView.
I've read Havoc Pennington's mini-FAQ.  He gives the following example of
how to set a color in GTK 2.0:

  GdkColor color;

  gdk_color_parse ("red", &color);

  gtk_widget_modify_fg (widget, GTK_STATE_NORMAL, &color);

But this doesn't seem to work in this case, using GTK 2.2.4.  Here's the
code used, with silly colors just to make it obvious as to whether it's
working.

        // create the TextView
        m_pTextView = gtk_text_view_new();
        
        GdkColor color;

        gdk_color_parse ("red", &color);

        gtk_widget_modify_fg (m_pTextView, GTK_STATE_NORMAL, &color);

        gdk_color_parse ("blue", &color);

        gtk_widget_modify_bg (m_pTextView, GTK_STATE_NORMAL, &color);

The text view does display and accepts keyboard input but the colors
displayed are black-on-white.  The mini-FAQ also says that some widgets
use the "text" color instead of the foreground color but doesn't say how
to set that.

I'm not sure it's exactly what you need, but generally I use tags to
accomplish this task; you can define, once for all, tags related to your
textbuffer:

gtk_text_buffer_create_tag (text_buffer,
                            "text",
                            "editable", FALSE,
                            "foreground", "blue",
                            "background", "yellow",
                            "weight", "550",
                            NULL);

and then use them each time you need with:

gtk_text_buffer_insert_with_tags_by_name (text_buffer,
                                          & here,
                                          buffer,
                                          -1,
                                          "text",
                                          NULL);

HTH,
Carlo





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