0

I'm creating a derived button class. I'm trying to set a default label of the button in it's _init method. However, it seems be get overwritten at the end of the method. It only works, if I apply it within the _new method.

edit: Intention of this class: Have a class for a GtkButton with a default label.

Code excerpt

static void 
xbutton_init ( Xbutton* self )
{  
  // init variables  
  gtk_button_set_label(GTK_BUTTON(self), "Xbutton"); // APPARENTLY OVERWRITTEN AFTER THIS FUNCTION
}

// =======================

GtkWidget*
xbutton_new ()
{
  GtkWidget * w = g_object_new (xbutton_get_type(), NULL);   
  gtk_button_set_label(GTK_BUTTON(w), "Xbutton");  // WORKS
  return w;
}

Full source

header

#ifndef __XBUTTON_H__
#define __XBUTTON_H__

#include <gtk/gtk.h>

G_BEGIN_DECLS

#ifndef CLASS_DEF
#define CLASS_DEF(_classname) typedef struct _##_classname _classname; \
                          struct _##_classname
#endif                          


// ------------------------------------
// ---- Define class (meta class) -----
// ----- (e.g. functions/signals) -----
// ------------------------------------
CLASS_DEF(XbuttonClass)
{
  GtkButtonClass parent_class;
};

// --------------------------------------------------
// ---- Instance related things (e.g. variables) ----
// --------------------------------------------------
CLASS_DEF(Xbutton)
{
  GtkButton parent;
};
// ------------------------------------------------

// --------------------------
// ---- Public functions ----
// --------------------------
#define XBUTTON_TYPE          (xbutton_get_type ())
#define XBUTTON(obj)          GTK_CHECK_CAST       (obj, xbutton_get_type (), Xbutton)
#define IS_XBUTTON(obj)       GTK_CHECK_TYPE       (obj, xbutton_get_type ())
#define XBUTTON_CLASS(klass)  GTK_CHECK_CLASS_CAST (klass, xbutton_get_type (), Xbutton)


GType          xbutton_get_type (void);
GtkWidget*     xbutton_new      (void);



G_END_DECLS

#endif /* include-protector */

c-file

#include "xbutton.h"

// -------------------------------------------------------------------

G_DEFINE_TYPE(Xbutton, xbutton,  GTK_TYPE_BUTTON);

// -------------------------------------------------------------------

static gboolean
on_click_callback (GtkWidget       *button,
                  GdkEventButton  *event)
{ 
  printf("button was clicked\n");
  // don't forget the call to parent
  return GTK_WIDGET_CLASS(xbutton_parent_class)->button_press_event(button,event);
}                       

// ===================================================================

// @brief: init class stuff like methods
static void xbutton_class_init( XbuttonClass* klass )
{
  // override class methods from GtkButton:
  GtkButtonClass * parent_button = GTK_BUTTON_CLASS(klass);  

  // override class methods from GtkWidget:
  GtkWidgetClass * parent_widget = GTK_WIDGET_CLASS(klass);  
  parent_widget->button_press_event = on_click_callback;  
}

// ===================================================================

static void xbutton_init( Xbutton* self )
{  
  // init variables  
  gtk_button_set_label(GTK_BUTTON(self), "Xbutton"); // APPARENTLY OVERWRITTEN AFTER THIS FUNCTION
}

// ===================================================================

GtkWidget*
xbutton_new ()
{
  GtkWidget * w = g_object_new (xbutton_get_type(), NULL);   
  gtk_button_set_label(GTK_BUTTON(w), "Xbutton");  // WORKS
  return w;
}

// ===================================================================
DarkTrick
  • 2,447
  • 1
  • 21
  • 39
  • Have you considered to create a Label property for that button? – Michi May 14 '20 at 19:48
  • By the way, it does not work in the _new function like you are expecting to work. You just set the label property which came from GtkButton. Did you considered to work with define code from private? There you create your own label property and you can set it through the _init function. – Michi May 14 '20 at 19:50
  • I'm intending to set the label property of the GtkButton. The intention is to have a class for a button with a default label. (updating question). – DarkTrick May 15 '20 at 07:33

0 Answers0