Re: crash in custom widget using gnome canvas



This time with the files, doh! I need sleep...

On 06 May 2001 07:50:21 +0100, Bastien Nocera wrote:
> Hi,
> 
> I'm trying to create a custom button for one of my projects. It's a
> Canvas, with a GdkPixbuf CanvasItem inside that can get the clicked
> signal. Internally, it will also use the mouse_over signal to create a
> saturated version of the button. (Much like the icons in the panel, but
> stand-alone).
<snip>
> I attached the 3 files to this mail, hope it's not too big. Compile
> with:
>  gcc -Wall -g -o ttt_test -I. ttt_test.c tictactoe.c `gnome-config
> --cflags --libs gtk glib gnomeui gdk_pixbuf gnomecanvaspixbuf`
<snip>

Cheers

-- 
/Bastien Nocera
http://hadess.net
/* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

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

enum {
  CLICKED_SIGNAL,
  LAST_SIGNAL
};

enum {
  ARG_0,
  ARG_IMAGE,
  ARG_IMAGE_CLICKED
};

static void tictactoe_class_init          (TictactoeClass *klass);
static void tictactoe_init                (Tictactoe      *ttt);
static void tictactoe_toggle              (GtkWidget *widget, Tictactoe *ttt);
static gint gtk_ttt_set_image_clicked (Tictactoe *ttt, const gchar *path);
static gint gtk_ttt_set_image (Tictactoe *ttt, const gchar *path);

static gint tictactoe_signals[LAST_SIGNAL] = { 0 };

static GnomeCanvasClass *parent_class = NULL;

guint
tictactoe_get_type ()
{
  static GtkType ttt_type = 0;

  if (!ttt_type)
    {
      GtkTypeInfo ttt_info =
      {
	"Tictactoe",
	sizeof (Tictactoe),
	sizeof (TictactoeClass),
	(GtkClassInitFunc) tictactoe_class_init,
	(GtkObjectInitFunc) tictactoe_init,
	/* reserved_1 */ NULL,
	/* reserved_2 */ NULL,
	(GtkClassInitFunc) NULL,
      };

      ttt_type = gtk_type_unique (gnome_canvas_get_type(), &ttt_info);
    }

  return ttt_type;
}

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

  object_class = (GtkObjectClass*) klass;

  parent_class = gtk_type_class (GNOME_TYPE_CANVAS);

  tictactoe_signals[CLICKED_SIGNAL] = gtk_signal_new ("clicked",
			 GTK_RUN_FIRST,
			 object_class->type,
			 GTK_SIGNAL_OFFSET (TictactoeClass, clicked),
			 gtk_signal_default_marshaller, GTK_TYPE_NONE, 0);


  gtk_object_class_add_signals (object_class, tictactoe_signals, LAST_SIGNAL);

  klass->clicked = NULL;
}

static gint gtk_ttt_set_image (Tictactoe *ttt, const gchar *path)
{
  GdkPixbuf *pixbuf;
  double x, y;

  pixbuf = gdk_pixbuf_new_from_file(path);
  if (pixbuf == NULL)
    return -1;

  x = gdk_pixbuf_get_width(pixbuf);
  y = gdk_pixbuf_get_height(pixbuf);

  gnome_canvas_set_scroll_region(GNOME_CANVAS(ttt->canvas),
      0.0, 0.0, x, y);
  ttt->image = gnome_canvas_item_new
    (gnome_canvas_root(GNOME_CANVAS(ttt->canvas)),
    gnome_canvas_pixbuf_get_type(),
    "pixbuf", pixbuf,
    NULL);

  ttt->path = g_strdup(path);
  ttt->normal = pixbuf;

  return 0;
}

static void
tictactoe_init (Tictactoe *ttt)
{
  ttt->canvas = NULL;
  ttt->image = NULL;

  ttt->normal = NULL;
  ttt->path = NULL;
  ttt->mouse_over = NULL;

  ttt->clicked = NULL;
  ttt->path_clicked = NULL;
}

GtkWidget*
tictactoe_new (const gchar *path, const gchar *path_clicked)
{
  GtkWidget *canvas;
  Tictactoe *ttt;

  if (!path || !path_clicked)
	  return NULL;

  ttt = gtk_type_new (tictactoe_get_type ());

  gdk_rgb_init();
  gtk_widget_push_visual (gdk_rgb_get_visual ());
  gtk_widget_push_colormap (gdk_rgb_get_cmap ());
  canvas = gnome_canvas_new_aa();
  ttt->canvas = canvas;
  gtk_widget_pop_colormap ();
  gtk_widget_pop_visual ();

  gtk_ttt_set_image(ttt, path);
  gtk_ttt_set_image_clicked(ttt, path_clicked);
  
  return GTK_WIDGET (ttt);
}

static gint gtk_ttt_set_image_clicked (Tictactoe *ttt, const gchar *path)
{
  GdkPixbuf *pixbuf;

  pixbuf = gdk_pixbuf_new_from_file(path);
  if (pixbuf == NULL)
    return -1;

  ttt->path_clicked = g_strdup(path);
  ttt->clicked = pixbuf;

  return 0;
}

static void
tictactoe_toggle (GtkWidget *widget, Tictactoe *ttt)
{
#if 0
  int i,k;

  static int rwins[8][3] = { { 0, 0, 0 }, { 1, 1, 1 }, { 2, 2, 2 },
			     { 0, 1, 2 }, { 0, 1, 2 }, { 0, 1, 2 },
			     { 0, 1, 2 }, { 0, 1, 2 } };
  static int cwins[8][3] = { { 0, 1, 2 }, { 0, 1, 2 }, { 0, 1, 2 },
			     { 0, 0, 0 }, { 1, 1, 1 }, { 2, 2, 2 },
			     { 0, 1, 2 }, { 2, 1, 0 } };

  int success, found;

  for (k=0; k<8; k++)
    {
      success = TRUE;
      found = FALSE;

      for (i=0;i<3;i++)
	{
	  success = success && 
	    GTK_TOGGLE_BUTTON(ttt->buttons[rwins[k][i]][cwins[k][i]])->active;
	  found = found ||
	    ttt->buttons[rwins[k][i]][cwins[k][i]] == widget;
	}
      
      if (success && found)
	{
	  gtk_signal_emit (GTK_OBJECT (ttt),
			   tictactoe_signals[TICTACTOE_SIGNAL]);
	  break;
	}
    }
#endif
}

/* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
#ifndef __TICTACTOE_H__
#define __TICTACTOE_H__


#include <gnome.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gdk-pixbuf/gnome-canvas-pixbuf.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
{
  GtkWidget *canvas;
  GnomeCanvasItem *image;

  GdkPixbuf *normal;
  GdkPixbuf *mouse_over;
  GdkPixbuf *clicked;
  
  gchar *path;
  gchar *path_clicked;
};

struct _TictactoeClass
{
  GnomeCanvasClass parent_class;

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

GtkType          tictactoe_get_type        (void);
GtkWidget* tictactoe_new (const gchar *path, const gchar *path_clicked);
//void	       tictactoe_clear           (Tictactoe *ttt);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* __TICTACTOE_H__ */
#include <gnome.h>
#include "tictactoe.h"

void
win (GtkWidget *widget, gpointer data)
{
  g_print ("Yay!\n");
//  tictactoe_clear (TICTACTOE (widget));
}

int 
main (int argc, char *argv[])
{
  GtkWidget *window;
  GtkWidget *ttt;
  
  gnome_init ("dial-test", "1.0", argc, argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  
  gtk_window_set_title (GTK_WINDOW (window), "Aspect Frame");
  
  gtk_signal_connect (GTK_OBJECT (window), "destroy",
		      GTK_SIGNAL_FUNC (gtk_exit), NULL);
  
  gtk_container_border_width (GTK_CONTAINER (window), 10);

  gdk_rgb_init ();
  gtk_widget_push_visual (gdk_rgb_get_visual ());
  gtk_widget_push_colormap (gdk_rgb_get_cmap ());
  ttt = tictactoe_new ("play.png", "play.png");
  gtk_widget_pop_colormap ();
  gtk_widget_pop_visual ();
  
  gtk_container_add (GTK_CONTAINER (window), ttt);
  gtk_widget_show (ttt);

  gtk_signal_connect (GTK_OBJECT (ttt), "tictactoe",
		      GTK_SIGNAL_FUNC (win), NULL);

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


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