Re: not able to change color of widget



OK, now I see your problem. You set new color for the button, and
don't really allow GTK to redraw it. I.e. you change color variable,
but actual widget is not updated. Moreover, as your program is in busy
waiting, the user can't interact with it.

Let's think busy waiting is by design. Now you need to update your
button, in other words, you want to process all pending events. This
can be done by adding the following code at the end of your while()
loop:

while(gtk_events_pending())
    gtk_main_iteration_do(FALSE);

This will do gtk_main iteration for all pending events, then let you
continue your outer while() loop.

On Fri, Nov 1, 2013 at 3:56 PM, Mahesh Chaudhari
<mahesh chaudhari ymail com> wrote:
thanks Ruslan,
it worked, it meant problem lies with other logic (using INOTIFY to wait for
file change)
The entire code (clean compilable) is

#include<gtk/gtk.h>
#include<stdlib.h>
#include<sys/inotify.h>
#define EVENT_SIZE  ( sizeof (struct inotify_event) )
#define BUF_LEN     ( 1024 * ( EVENT_SIZE + 16 ) )
GtkWidget *button1, *button2, *button3, *button4, *button5;
 void click_button1(GtkWidget *widget, gpointer data)
{
  printf("in click_button1\n");
  GdkColor color;
  color.red = 27000;
  color.green = 30325;
  color.blue = 34181;
//  gtk_widget_modify_fg(GTK_WIDGET(widget), GTK_STATE_NORMAL, &color);
  gtk_widget_modify_bg(GTK_WIDGET(button1), GTK_STATE_NORMAL, &color);
}
 void click_button2(GtkWidget *widget, gpointer data)
{
  printf("in click_button2\n");
  GdkColor color;
  color.red = 27000;
  color.green = 30325;
  color.blue = 34181;
//  gtk_widget_modify_fg(GTK_WIDGET(widget), GTK_STATE_NORMAL, &color);
  gtk_widget_modify_bg(GTK_WIDGET(button2), GTK_STATE_NORMAL, &color);
}
void click_button_start(GtkWidget *widget, gpointer data)
{
        int notifyfd, watchfd, len=0;
        notifyfd = inotify_init();
        if (notifyfd<0)perror("inotify_init failed\n");
        watchfd = inotify_add_watch(notifyfd,
"/home/user/Desktop/mahesh/GTK+/trials/putty.log", IN_MODIFY);
        if(watchfd<0)perror("inotify_add_watch failed\n");
        while(1)
        {
        printf("waiting for file change\n");
        struct inotify_event* event = malloc(BUF_LEN);
        len = read(notifyfd, event, BUF_LEN);
        if(len>0){
                gtk_button_clicked(GTK_BUTTON(button1));
        }
        free(event);
        }
 }

int main (int argc,char **argv)
{
    GtkWidget *button_start;
    GtkWidget *window;

    GtkWidget *hbox,*vbox;
    GdkColor color;
    gdk_color_parse ("blue", &color);
    gtk_init (&argc,&argv);

    /*...............create top level window...................*/

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "LED status");
    gtk_window_set_default_size(GTK_WINDOW(window), 250, 80);
    gtk_container_set_border_width(GTK_CONTAINER(window), 5);
    g_signal_connect (G_OBJECT(window), "destroy",
            G_CALLBACK (gtk_main_quit), NULL);

    /*................. create buttons ......................*/
    button_start = gtk_button_new_with_label("start");
    g_signal_connect(G_OBJECT(button_start), "clicked",
    G_CALLBACK(click_button_start), NULL);
    button1 = gtk_button_new_with_label ("button1");
    g_signal_connect(G_OBJECT(button1), "clicked",
    G_CALLBACK(click_button1), NULL);
    button2 = gtk_button_new_with_label ("button2");
    g_signal_connect(G_OBJECT(button2), "clicked",
    G_CALLBACK(click_button2), NULL);
    button3 = gtk_button_new_with_label ("button3");
    button4 = gtk_button_new_with_label ("button4");
    button5 = gtk_button_new_with_label ("button5");

    /*.................. Alignements......................*/

    hbox = gtk_hbox_new(TRUE,1);
    vbox = gtk_vbox_new(TRUE,1);
    gtk_box_pack_start(GTK_BOX(hbox), button1, 1, 1, 0);
    gtk_box_pack_start(GTK_BOX(hbox), button2, 1, 1, 0);
    gtk_box_pack_start(GTK_BOX(hbox), button3, 1, 1, 0);
    gtk_box_pack_start(GTK_BOX(hbox), button4, 1, 1, 0);
    gtk_box_pack_start(GTK_BOX(hbox), button5, 1, 1, 0);
    gtk_box_pack_start(GTK_BOX(vbox), button_start, 1, 1, 0);
    gtk_box_pack_start(GTK_BOX(vbox), hbox, 1, 1, 0);
    gtk_container_add (GTK_CONTAINER (window), vbox);

    /*....................show...............................*/

    gtk_widget_show_all (window);
     gtk_main();
    return 0;
}


On Friday, 1 November 2013 1:43 AM, Ruslan Kabatsayev
<b7 10110111 gmail com> wrote:
Well, it worked. Didn't it work for you?
Also, in the future please give _compilable_ code - it's no fun to fix
compilation errors just to try it out.

On Thu, Oct 31, 2013 at 3:11 PM, Mahesh Chaudhari
<mahesh chaudhari ymail com> wrote:
Ruslan ,
 that is working with mouse clicks (works for me also)
what I wanted is without clicking with mouse, generate a click signal
event
(through a  program only) and and thus color should be changed
try this one if possible (click only "start" button with mouse and color
of
button1 should be changed)

GtkWidget *button1, *button2, *button3, *button4, *button5;
 void click_button1(GtkWidget *widget, gpointer data)
{
 printf("in click_button1\n");
 GdkColor color;
 color.red = 27000;
 color.green = 30325;
 color.blue = 34181;
//  gtk_widget_modify_fg(GTK_WIDGET(widget), GTK_STATE_NORMAL, &color);
 gtk_widget_modify_bg(GTK_WIDGET(button1), GTK_STATE_NORMAL, &color);
}

void click_button_start(GtkWidget *widget, gpointer data)
{
                 gtk_button_clicked(GTK_BUTTON(button1));
 }

int main (int argc,char **argv)
{
   GtkWidget *button_start;
   GtkWidget *hbox,*vbox;
   GdkColor color;
   gdk_color_parse ("blue", &color);
   gtk_init (&argc,&argv);

   /*...............create top level window...................*/

   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
   gtk_window_set_title(GTK_WINDOW(window), "LED status");
   gtk_window_set_default_size(GTK_WINDOW(window), 250, 80);
   gtk_container_set_border_width(GTK_CONTAINER(window), 5);
   g_signal_connect (G_OBJECT(window), "destroy",
           G_CALLBACK (gtk_main_quit), NULL);

   /*................. create buttons ......................*/

   button_start = gtk_button_new_with_label("start");
   g_signal_connect(G_OBJECT(button_start), "clicked",
   G_CALLBACK(click_button_start), NULL);
   button1 = gtk_button_new_with_label ("button1");
   g_signal_connect(G_OBJECT(button1), "clicked",
   G_CALLBACK(click_button1), NULL);
   button2 = gtk_button_new_with_label ("button2");
   g_signal_connect(G_OBJECT(button2), "clicked",
   G_CALLBACK(click_button2), NULL);
   button3 = gtk_button_new_with_label ("button3");
   button4 = gtk_button_new_with_label ("button4");
   button5 = gtk_button_new_with_label ("button5");

   /*.................. Alignements......................*/

   hbox = gtk_hbox_new(TRUE,1);
   vbox = gtk_vbox_new(TRUE,1);
   gtk_box_pack_start(GTK_BOX(hbox), button1, 1, 1, 0);
   gtk_box_pack_start(GTK_BOX(hbox), button2, 1, 1, 0);
   gtk_box_pack_start(GTK_BOX(hbox), button3, 1, 1, 0);
   gtk_box_pack_start(GTK_BOX(hbox), button4, 1, 1, 0);
   gtk_box_pack_start(GTK_BOX(hbox), button5, 1, 1, 0);
   gtk_box_pack_start(GTK_BOX(vbox), button_start, 1, 1, 0);
   gtk_box_pack_start(GTK_BOX(vbox), hbox, 1, 1, 0);
   gtk_container_add (GTK_CONTAINER (window), vbox);

   /*....................show...............................*/

   gtk_widget_show_all (window);
   gtk_main();
   return 0;


On Thursday, 31 October 2013 3:09 AM, Ruslan Kabatsayev
<b7 10110111 gmail com> wrote:
Hi,

I've tried your code, it works for me. Did you try moving cursor out
of the button after you click it? If the cursor is still inside, the
button is in hovered state, not STATE_NORMAL.
If it still doesn't work, then what widget theme do you use? Maybe it
interferes with your actions (older versions of oxygen-gtk had this
problem, for example).

Regards,
Ruslan

On Thu, Oct 31, 2013 at 1:07 PM, Mahesh Chaudhari
<mahesh chaudhari ymail com> wrote:
what am I doing wrong in Following Code :

GtkWidget *button1;
 void click_button1(GtkWidget *widget, gpointer data)
 {
   printf("I am in click_button\n");                    // able to print
this
   GdkColor color;
   color.red = 27000;
   color.green = 30325;
   color.blue = 34181;
   gtk_widget_modify_bg(GTK_WIDGET(widget), GTK_STATE_NORMAL, &color);
//gtk_widget_modify_fg(GTK_WIDGET(widget), GTK_STATE_NORMAL, &color);
 }


 some_callback_func()
 {  ....
     gtk_button_clicked(GTK_BUTTON(button1));
   ..... }




int main(int argc, char** argv)
{
 gtk_init(&argc,&argv);
   button1 = gtk_button_new_with_label ("button1");
     g_signal_connect(G_OBJECT(button1), "clicked",
G_CALLBACK(click_button1), NULL);
     gtk_widget_show_all (window);
     gtk_main();
     return 0;
 }
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list






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