styles - changing after show()



>> Can I change widget style after it's been showed? .. Before
>> gtk_widget_show() I can change widget's background color but after it
it
>> just doesn't change.

>.. and to be bit more specific since this seems to work with other
>widgets:) .. this happens with GtkText and when setting it's
background
>color, it just draws some thin borders and the rest is white..
background
>pixmap seemed to work.

I see the same thing with GtkCombo. I have attached a small example that

uses gtk--. There is a label, a combo box, a button to toggle styles,
and a
button to quit. The label changes color from black on white to green on
red.
The combo just changes the outside rim of the entry field. If you click
into
the entry, the whole thing turns red. When you push the button to toggle

the colors again, the label returns to normal, but the combo leaves a
thin
red line.

I looked through the sources but am unable to figure out how this is
supposed
to work. Does anyone have any idea how this is supposed to work?

thanks
Todd

#- makefile
TARGET = colorChange
OBJS = \
 colorChange.o \

CC = g++

#X11_INC = -I /usr/include/X11
#XEXT_INC = -I /usr/X/include/X11/extensions
#XR6EXT_INC = -I /usr/X11R6/include/X11/extensions

INCDIR = -I ..
LIBDIR = -L../lib -L../../contrib/lib -L/usr/X11R6/lib
DEBUG = -g
D_HAVE_CONFIG = -DHAVE_CONFIG_H
CCFLAGS = -Wall $(OPTIMIZE) $(DEBUG) $(INCDIR) $(D_HAVE_CONFIG)
`gtk-config --cflags`
LIB_FLAGS = $(LIBDIR) $(LIBS) `gtk-config --libs` -lgtkmm
SRC =$(OBJS:.o=.C) $(OBJS:.o=.h)

.c.o:
 $(CC) $(CCFLAGS) -c $<

.C.o:
 $(CC) $(CCFLAGS) -c $<

$(TARGET): $(OBJS)
 $(CC) $(LK_FLAGS) -o $@ $(OBJS) $(LIB_FLAGS)

clean:
 touch dummy
 rm $(TARGET) $(OBJS) dummy

depend:
 makedepend -- $(CCFLAGS) -- $(SRC)

# DO NOT DELETE
#-- end of makefile

//-- colorChange.C
#include <stream.h>
#include <gdk/gdkx.h>
#include <gtk--.h>

static char * pc_rcs_h = "global_h";
static char * pc_rcs = "$Id: addTest.C,v 1.1 1998/04/24 21:55:33 tdukes
Exp $";

#define USE(var) static void * use_##var = (void *) var
USE( pc_rcs_h);
USE( pc_rcs);

class MyWindow : public Gtk_Window {
public:
    MyWindow();
    Gtk_Button b;
    Gtk_Button b2;
    Gtk_Label * p;
  Gtk_Combo * pCombo;
    Gtk_VBox vbox;
    void changeColor();
    bool styleChanged;
    Gtk_Style * pStyle;
};

MyWindow::MyWindow():
        b("Quit"),
        b2("change color"),
        p(new Gtk_Label("a label")),
 pCombo( new Gtk_Combo),
        styleChanged(false),
        pStyle ( 0) {
    b.show();
    b2.show();
    p->show();
    pCombo->show();
    GList * pList = g_list_prepend( NULL, "item1");
    pList = g_list_append( pList, "item2");
    pCombo->set_popdown_strings(pList);
    pCombo->entry.set_text("entry text");
    vbox.show();
    add(&vbox);
    vbox.pack_start(p);
    vbox.pack_start(pCombo);
    vbox.pack_start(&b2);
    vbox.pack_start(&b);
    connect_to_method( b.clicked, Gtk_Main::instance(),
&Gtk_Main::quit);
    connect_to_method( b2.clicked, this, &MyWindow::changeColor);
}

void MyWindow::changeColor() {
    cout << "changeColor" << endl;
    if ( styleChanged) {
        cout << "style has been changed, restoring default style" <<
endl;
        p->restore_default_style();
        gtk_style_set_background(p->get_style()->gtkstyle,
p->get_window(),
                                 GTK_STATE_NORMAL);
 pCombo->entry.restore_default_style();
        gtk_style_set_background(p->get_style()->gtkstyle,
pCombo->entry.get_window(),
                                 GTK_STATE_NORMAL);
        styleChanged = false;
        return;
    }
    if ( pStyle == 0) {
        cout << "pStyle has not been initialized" << endl;
        GdkColor redColor;
        if (!gdk_color_parse("red", &redColor)) { // returns FALSE if
failed.
            cout << "parsing red failed" << endl;
        }
        if (!gdk_color_alloc(p->get_colormap(), &redColor)) {
            cout << "allocating red failed" << endl;
        }
        cout << "red rgb = " << hex << redColor.red << " "
             << redColor.green << " "
             << redColor.blue <<  " "
             << dec
             << endl;
 GdkColor greenColor;
 gdk_color_parse( "green", &greenColor);
 gdk_color_alloc( p->get_colormap(), &greenColor);
        GtkStyle * pS = gtk_style_copy(p->get_style()->gtkstyle);
        pStyle = new Gtk_Style(pS);
        cout << "p->get_window() is " << hex << p->get_window() << dec
<< endl;
        for (int i = 0; i < 5; i++) {
            memcpy( &(pS->bg[i]), &redColor, sizeof(GdkColor));
            memcpy( &(pS->fg[i]), &greenColor, sizeof(GdkColor));
     memcpy( &(pS->light[i]), &redColor, sizeof(GdkColor));
     memcpy( &(pS->dark[i]), &redColor, sizeof(GdkColor));
     memcpy( &(pS->mid[i]), &redColor, sizeof(GdkColor));
     memcpy( &(pS->text[i]), &greenColor, sizeof(GdkColor));
     memcpy( &(pS->base[i]), &redColor, sizeof(GdkColor));
        }
    }
    p->set_style(pStyle);
    gtk_style_set_background(pStyle->gtkstyle, p->get_window(),
                             GTK_STATE_NORMAL);
    pCombo->entry.set_style(pStyle);
    gtk_style_set_background(pStyle->gtkstyle,
pCombo->entry.get_window(),
                             GTK_STATE_NORMAL);
    styleChanged = true;
}

int main(int argc, char *argv[]) {
  Gtk_Main m(&argc, &argv);
  XSynchronize( GDK_DISPLAY(), False); /* true makes synchronous,
                                            false is asynchronous */
  MyWindow myWindow;
  myWindow.show();
  m.run();
  return 0;
}



--
   ==============================================================
   | Todd Dukes                      E-MAIL:  tdukes@ibmoto.com |
   | Motorola Somerset                  Phone:   (512) 424-8008 |
   | MS OE70                                                    |
   | 6300 Bridgepoint Parkway Building #3                       |
   | Austin, Texas 78730                                        |
   ==============================================================





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