nope
nope

Reputation: 223

Unable to pass argument to GTK callback

When the program is initially run, the print statements, print a valid hex code corresponding to the pointer. However, when I click on the screen, and the handleClick method is called through the 'clicked' callback, 0x0 is printed to the screen. What happened? Why has my board object suddenly become null?

#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include "board.h"

void printBoard(Board *board);
void handleClick(GtkWidget *widget, GdkEventButton *event, cairo_t *cr, gpointer data);

  gboolean draw_cb(GtkWidget *widget, cairo_t *cr, gpointer data)
  {
    Board *temp = (Board *)data;
    printf("%p\n",temp);
    return TRUE;
  }

  void handleClick (GtkWidget *widget, GdkEventButton *event, cairo_t *cr, gpointer data)
  {
    Board *temp = (Board *)data;
    printf("%p\n",temp);
  }

  void start(Board *newBoard)
  {
      GtkWidget *window;
      GtkWidget *da;
      GtkWidget *frame;

      window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
      char string[40];
      snprintf(string, sizeof(string), "Connect %d-%d",newBoard->k,newBoard->n);

      gtk_window_set_default_size (GTK_WINDOW(window), 400, 400);
      gtk_window_move(GTK_WINDOW(window), 100, 100);     
      g_signal_connect (GTK_WINDOW(window), "destroy", G_CALLBACK (gtk_main_quit), NULL);

      da = gtk_drawing_area_new ();
      gtk_widget_set_size_request (da, 500, 500);

      frame = gtk_frame_new (NULL);
      gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
      gtk_container_add (GTK_CONTAINER (window), frame);

      gtk_container_add (GTK_CONTAINER (frame), da);

      gtk_widget_set_events (da, gtk_widget_get_events (da)
                     | GDK_BUTTON_PRESS_MASK);

      g_signal_connect (da, "draw",
              G_CALLBACK (draw_cb), newBoard);

       g_signal_connect (da, "button-press-event",
              G_CALLBACK (handleClick), newBoard);

      gtk_widget_show_all (window);
  }

Upvotes: 0

Views: 538

Answers (2)

liberforce
liberforce

Reputation: 11454

As Szilard said, you're using the wrong prototype for the handleClick function.

The button-press-event signal handler has the following prototype:

gboolean user_function (GtkWidget *widget,
                        GdkEvent  *event,
                        gpointer   user_data)

To fix your mistakes, you need to:

  • remove thecairo_t *cr parameter. It was receiving the value of data.
  • fix event type which is GdkEvent *, not GdkEventButton *
  • fix the return value, which is a boolean, not void, and which is important to determine if the event is propagated or not.

Upvotes: 0

Szil&#225;rd Pfeiffer
Szil&#225;rd Pfeiffer

Reputation: 1646

The prototype of the button-press-event signal handler is the following according to the reference manual.

gboolean user_function (GtkWidget *widget,
                        GdkEvent  *event,
                        gpointer   user_data)

If you remove cairo_t *cr parameter from the handleClick function data will be ok.

Upvotes: 1

Related Questions