Gtk2: GdkColor does not apply to GtkEntry after gtk_container_add()

,

hi all!

problem

I wrote following code in gtk2. Background color of GtkEntry is not filled by GdkColor.base[0].
I use gtk_widget_set_style() to change the color, and I found 2 patterns of the behavior:

  1. GtkColor is NOT applied:
    • set GtkStyle by gtk_widget_set_style after register to parent widget bygtk_container_add().
  2. GtkColor will apply:
    • set GtkStyle by gtk_widget_set_style before gtk_container_add().

fully code is followings (contains 2 patterns of gtk_container_add():

#include <gtk/gtk.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>

static void setc(GdkColor* dest, uint32_t color){
	dest->red   = ((color & 0xFF0000) >> 16) << 8;
	dest->green = ((color & 0x00FF00) >>  8) << 8;
	dest->blue  = ((color & 0x0000FF)      ) << 8;
}


int main( int argc, char *argv[] )
{
	GtkWidget* window;
	GtkWidget* text;
	GtkStyle*  style;

	gtk_init(&argc, &argv);

	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_widget_show_all(window);	
	// ^^ done base window ^^

	// VV append new widget VV
	text = gtk_entry_new();
	gtk_widget_show(text);
	gtk_entry_set_text(GTK_ENTRY(text), "test");

	// <1> background color of GtkEntry is not affected here.
    gtk_container_add(GTK_CONTAINER(window), text);

	style = gtk_style_copy(text->style);
	setc(&style->text[0], 0xFF0000);
	setc(&style->base[0], 0x0000FF);
	gtk_widget_set_style(text, style);
	gtk_style_unref(style);

	// <2> background color is filled by blue.
    //gtk_container_add(GTK_CONTAINER(window), text);

	while(1){
		if( gtk_events_pending() ){
			gtk_main_iteration();
		}
		else
		{
			usleep( 10*1000 );
		}
	}
	return 0;
}

that code will show:

  • NG pattern.
    • pattern_1_ng
  • OK pattern
    • pattern_2_expected

question

Is there any correct procedure to fill background color of GtkEntry ?

The question is: why. GTK 2 is EOL, and has been unmaintained since December 2020.

thanks, my understanding: should replace to ver3 or later .

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.