0

All,

As I continue to learn how to migrate code I built for GTK3 to GTK4, I continue to encounter instances where functions in GTK3 no longer exist in GTK4. As an example, I had built a small test program that uses a standard button with an image to launch the color chooser instead of using directly presenting a GTK color button. This was done for aesthetics. The method I used was having both a GTK color button that was not visible along with a standard button with an image. Within the program I connected the "button click" signal to function "on_buttoncolor_clicked" to the standard button. Following is the simple code within that function that then emits a "button click" signal for the color button.

void on_buttoncolor_clicked (GtkButton *b)
{
    gtk_button_clicked(GTK_BUTTON(button));
}

When I ported this over and attempted to compile this program with the GTK4 libraries, the compilation erred out indicating that this function was not found.

To get around this, I had attempted to attach an image to the GTK color button, but that produced spurious errors when clicking on the button. So, I would like to be able to replicate the "gtk_button_clicked" function. Any advice on this would be appreciated. FYI, I am attaching a small image of how the GTK3 version of the user interface looks like.

Regards,

Craig GTK3 standard button acting like a color button with an icon image

NoDakker
  • 3,390
  • 1
  • 10
  • 11

1 Answers1

0

All,

I did attempt to replicate sending a "button click" signal to the color button using the "g_signal_emit" function, but that signal was unknown to the color button.

After digging into the source code for the color button widget, I determined that a "button click" signal does get attached to a button element that is a part of the "GtkColorButton" structure. So, I directed the "g_signal_emit" function at the "button element" within the structure by revising my code.

First, I cloned the structure for the "GtkColorButton" widget which gave me access to the structure.

struct _GtkColorButton {
GtkWidget parent_instance;

GtkWidget *button;

GtkWidget *swatch;
GtkWidget *cs_dialog;

char *title;
GdkRGBA rgba;

guint use_alpha   : 1;
guint show_editor : 1;
guint modal       : 1;
};

Then, I revised the signal connection to use "g_signal_connect_swapped" to reference the color button to the signal.

g_signal_connect_swapped (button "clicked",
G_CALLBACK (on_button_clicked), buttoncolor);

Next, I revised the signal emission function to be directed at the button element within the "GtkColorButton" widget.

void on_button_clicked (GtkColorButton *b)
{
g_signal_emit (b->button, 
g_signal_lookup ("clicked", G_OBJECT_CLASS_TYPE 
(GTK_WIDGET_GET_CLASS(GTK_BUTTON(button)))), 0);
}

This resulted in the expected color selection dialog appearing when the standard label button is clicked as noted in the newly attached image.Resolved color button usage

With this resolution, I believe that this issue can be closed.

Regards,

Craig

NoDakker
  • 3,390
  • 1
  • 10
  • 11