Re: setting background color of eventbox ... how?



Hello all,

thanks for your help.
Apparently I'm too dumb to make anything of it. Please have look at the attached
code (sorry for the large attachment).
I tried to reduce to the max. You will see I built upon the tictactoe example, here.

I tried to do the gtk_widget_modify_bg on various containers to no avail in
tictactoe_init() (using blue here).
The only visible effect I can see is still if I set the background of the eventbox in an event handler for "realized" and "state_changed" events. But this only flashes
the desired background colour up for a moment (black here).

I must be missing smth. here.

Karl.


/* GTK - The GIMP Toolkit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#include <stdio.h>
#include <gtk/gtkeventbox.h>
#include <gtk/gtklabel.h>

#include "gtk/gtksignal.h"
#include "tictactoe.h"

enum {
  TICTACTOE_SIGNAL,
  LAST_SIGNAL
};

static void tictactoe_class_init          (TictactoeClass *klass);
static void tictactoe_init                (Tictactoe      *ttt);
static void event_box_state_changed       (GtkWidget *w);

static gint tictactoe_signals[LAST_SIGNAL] = { 0 };

GType
tictactoe_get_type ()
{
  static GType ttt_type = 0;

  if (!ttt_type)
    {
      static const GTypeInfo ttt_info =
      {
        sizeof (TictactoeClass),
        NULL,
        NULL,
        (GClassInitFunc) tictactoe_class_init,
        NULL,
        NULL,
        sizeof (Tictactoe),
        0,
        (GInstanceInitFunc) tictactoe_init,
      };

      ttt_type = g_type_register_static (GTK_TYPE_VBOX, "Tictactoe", &ttt_info, 0);
    }

  return ttt_type;
}

static void
tictactoe_class_init (TictactoeClass *class)
{
  GtkObjectClass *object_class;

  object_class = (GtkObjectClass*) class;
  
  tictactoe_signals[TICTACTOE_SIGNAL] = g_signal_new ("tictactoe",
                                         G_TYPE_FROM_CLASS (object_class),
                                         G_SIGNAL_RUN_FIRST,
                                         0,
                                         NULL, 
                                         NULL,                
                                         g_cclosure_marshal_VOID__VOID,
                                         G_TYPE_NONE, 0, NULL);


  class->tictactoe = NULL;
}

static void
tictactoe_init (Tictactoe *ttt)
{
        GdkColor        g_col;
        GtkWidget       *label;

        ttt->ebox = gtk_event_box_new ();
        gtk_container_add (GTK_CONTAINER (ttt), ttt->ebox);
        g_signal_connect(GTK_OBJECT(ttt->ebox), "state_changed",
                     GTK_SIGNAL_FUNC (event_box_state_changed), NULL);
        g_signal_connect(GTK_OBJECT(ttt->ebox), "realize",
                     GTK_SIGNAL_FUNC (event_box_state_changed), NULL); 

        g_col.red = 0;
        g_col.green = 0;
        g_col.blue = 0xFFFF;
        gtk_widget_modify_bg (ttt->ebox, GTK_STATE_NORMAL, &g_col);
        gtk_widget_modify_bg (GTK_WIDGET(ttt), GTK_STATE_NORMAL, &g_col);
        gtk_widget_modify_bg (GTK_WIDGET(&(ttt->vbox)), GTK_STATE_NORMAL, &g_col);

        label = gtk_label_new("blub");
        gtk_container_add (GTK_CONTAINER (ttt->ebox), label);
        gtk_widget_show (label);

        gtk_widget_show (ttt->ebox);
}

GtkWidget*
tictactoe_new ()
{
  return GTK_WIDGET (g_object_new (tictactoe_get_type (), NULL));
}


void
event_box_state_changed (GtkWidget *w)
{
        GdkColor        g_col;
        GdkColormap     *colormap;

        colormap = NULL;
        colormap = gdk_drawable_get_colormap (w->window);
        printf("ev: window: %p colormap: %p \n", w->window, colormap);

        if (gdk_color_parse("black", &g_col)){
                if (gdk_colormap_alloc_color(colormap, &g_col, FALSE, TRUE)) {
                        printf("ev: setting bg color next\n");
                        gdk_window_set_background (GDK_WINDOW(w->window), &g_col);
                }
        }
}



/* GTK - The GIMP Toolkit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#ifndef __TICTACTOE_H__
#define __TICTACTOE_H__


#include <gdk/gdk.h>
#include <gtk/gtkvbox.h>

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

#define TICTACTOE(obj)          GTK_CHECK_CAST (obj, tictactoe_get_type (), Tictactoe)
#define TICTACTOE_CLASS(klass)  GTK_CHECK_CLASS_CAST (klass, tictactoe_get_type (), TictactoeClass)
#define IS_TICTACTOE(obj)       GTK_CHECK_TYPE (obj, tictactoe_get_type ())


typedef struct _Tictactoe       Tictactoe;
typedef struct _TictactoeClass  TictactoeClass;

struct _Tictactoe
{
        GtkVBox         vbox;
        GtkWidget       *ebox;
        GtkWidget       *da[10];
};

struct _TictactoeClass
{
  GtkVBoxClass parent_class;

  void (* tictactoe) (Tictactoe *ttt);
};

GtkType        tictactoe_get_type        (void);
GtkWidget*     tictactoe_new             (void);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* __TICTACTOE_H__ */


#include <stdlib.h>
#include <gtk/gtk.h>
#include "tictactoe.h"

int main( int   argc,
          char *argv[] )
{
  GtkWidget *window;
  GtkWidget *ttt;
  
  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_resizable (GTK_WINDOW(window), TRUE);
  gtk_window_set_title (GTK_WINDOW (window), "Aspect Frame");
  g_signal_connect (G_OBJECT (window), "destroy",
                    G_CALLBACK (exit), NULL);
  gtk_container_set_border_width (GTK_CONTAINER (window), 50);

  ttt = tictactoe_new ();
  gtk_container_add (GTK_CONTAINER (window), ttt);
  gtk_widget_show (ttt);

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



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