Changing GtkButton color when GtkWindow is changed

ε祈祈猫儿з 提交于 2019-12-24 10:47:14

问题


I am trying a simple sample Gtk program and using GtkTable to align my widgets.

My layout should be:

Window background is black. The buttons should be white while the text on the buttons should be black. All labels should be white so they can be seen against the black window background.

With this in mind. Here is what I have done so far.

Code:

#include <gtk/gtk.h>
#include <glib.h>
#include <stdlib.h>     /* for atoi() and exit() */
#include <stdio.h>      /* standard in and output*/


typedef struct cmd_widgets{
    GtkWidget *button1;
    GtkWidget *combo;
    GtkWidget *label;

}my_cmd_widgets;

static gboolean close_application( GtkWidget *widget, GdkEvent  *event, gpointer   data )
{
  gtk_main_quit ();
  return FALSE;
}

static void UpdateChoice( GtkWidget *widget, gpointer data)
{
    my_cmd_widgets *widgrp;
    widgrp = (my_cmd_widgets *)data;
    gchar *text =  gtk_combo_box_get_active_text(GTK_COMBO_BOX(widgrp->combo));
    GString *val = g_string_new("You have chosen :  ");
    g_string_append(val, text);
    gtk_label_set_text(GTK_LABEL(widgrp->label), val->str);
    g_free(text);
}


int main(int argc, char *argv[]) {

  GtkWidget *window;
  GtkWidget *table;

  GtkWidget *label1;
  GtkWidget *label2;
  GtkStyle  *style;

  GtkWidget *align;

  my_cmd_widgets grp_widgets;
  GtkWidget *image;

  gtk_init(&argc, &argv);
  gtk_rc_parse("fonts.rc");

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  gtk_window_set_title(GTK_WINDOW(window), "Tour App");
  gtk_container_set_border_width(GTK_CONTAINER(window), 10);

  table = gtk_table_new(7, 1, FALSE);
  gtk_container_add(GTK_CONTAINER(window), table);


  label1 = gtk_label_new("Our Tour Package Offers");
  align = gtk_alignment_new(0.0, 0.5, 0.0, 0.0);
  gtk_container_add(GTK_CONTAINER(align), label1);
  gtk_table_attach(GTK_TABLE(table), align, 0, 1, 1, 2, GTK_FILL, GTK_FILL, 0, 10);


  label2 = gtk_label_new("Select City: ");
  align = gtk_alignment_new(0.0, 0.5, 0.0, 0.0);
  gtk_container_add(GTK_CONTAINER(align), label2);
  gtk_table_attach(GTK_TABLE(table), align,0, 1, 2, 3, GTK_FILL, GTK_FILL, 0, 0);
  gtk_widget_show(align);
  grp_widgets.combo = gtk_combo_box_new_text();
  gtk_combo_box_append_text(GTK_COMBO_BOX(grp_widgets.combo), "Paris");
  gtk_combo_box_append_text(GTK_COMBO_BOX(grp_widgets.combo), "London");
  gtk_combo_box_append_text(GTK_COMBO_BOX(grp_widgets.combo), "Tokyo");
  gtk_combo_box_append_text(GTK_COMBO_BOX(grp_widgets.combo), "New York");

  /* Creates a new button1. */

   grp_widgets.button1 = gtk_button_new_with_label ("See Itinerary");

   grp_widgets.label = gtk_label_new("");
   align = gtk_alignment_new(0.0, 0.5, 0.0, 0.0);
   gtk_container_add(GTK_CONTAINER(align), grp_widgets.label);
   gtk_table_attach(GTK_TABLE(table), align,0, 1, 5, 6, GTK_FILL, GTK_FILL, 0, 0);
   gtk_widget_show(align);


  image = gtk_image_new ();
  gtk_image_set_from_file (GTK_IMAGE(image), "Images/olympics_logo.gif");

  //column 1
  gtk_table_attach(GTK_TABLE(table), image, 0, 1, 0, 1,
      GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK, 0, 0);
  gtk_table_attach(GTK_TABLE(table), grp_widgets.combo, 0, 1, 3, 4,
      GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK, 0, 15);
  gtk_table_attach(GTK_TABLE(table), grp_widgets.button1, 0, 1, 4, 5,
      GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK, 5, 15);



  g_signal_connect(window, "destroy",G_CALLBACK(gtk_main_quit), NULL);
  g_signal_connect (window,"delete-event",G_CALLBACK (close_application), NULL);
  g_signal_connect_swapped(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), G_OBJECT(window));
  g_signal_connect (G_OBJECT(grp_widgets.button1), "clicked", G_CALLBACK (UpdateChoice), (gpointer)&grp_widgets);


  gtk_widget_show(table);

  gtk_widget_show_all(window);

  gtk_main();

  return 0;
}

Here is the rc file with properties for TABLE AND WINDOW:

style "window"
{
  font_name = "fixed 12"
  bg[NORMAL] = { 0.0, 0.0, 0.0 }
}
style "table"
{
  bg[NORMAL] = { 1.0, 1.0, 1.0 }
  fg[NORMAL] = { 1.0, 1.0, 1.0 }
}

style "container" {
        font_name = "bold 12"
        xthickness = 10
        ythickness = 10
        bg[NORMAL] = { 1.0, 1.0, 1.0 }
        fg[NORMAL] = { 1.0, 1.0,1.0 }          
}

style "button" {
        font_name = "bold 12"
        xthickness = 10
        ythickness = 10 
        #bg[NORMAL] = { 0.0, 0.0, 0.0 }   
        fg[NORMAL] = { 0.0, 0.0, 0.0 }  
}

#widget_class "*GtkButton*" style "button"
widget_class "GtkWindow" style "window"
widget_class "*GtkTable*" style "table"

The problem in the above code is, the Button's text(label) remains white when I change the fg to white on either table or window styles. Even after changing the fg to black(as below), the label text on my button remains white. Also how can I stretch the image and all the widgets on the window so that whenever I resize my window all widgets are resized to fit screen as well?


回答1:


All labels should be white so they can be seen against the black window background.

According to what I've found online:

GtkLabel Widget is one of a few GTK+ widgets that don't create their own window to render themselves into. This menas in order to set the bg color for a GtkLabel Widget,you need to change the bg color of its parent.

So in that case, for a simple example, I packed only one of the labels in a GtkEventBox.
Code Piece:

GtkWidget *eventBox;
...
eventBox = gtk_event_box_new();

label1 = gtk_label_new("Our Tour Package Offers");
gtk_container_add(GTK_CONTAINER(eventBox), label1);
align = gtk_alignment_new(0.0, 0.5, 0.0, 0.0);
gtk_container_add(GTK_CONTAINER(align), eventBox);
...

simplified rc file:

style "window"
{
  font_name = "fixed 12"
  bg[NORMAL] = { 0.0, 0.0, 0.0 }
}

style "label"{
  bg[NORMAL] = { 1.0, 1.0, 1.0 }
}

widget_class "GtkWindow" style "window"
widget_class "*GtkAlignment*GtkEventBox*" style "label"

and result:



来源:https://stackoverflow.com/questions/10722708/changing-gtkbutton-color-when-gtkwindow-is-changed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!