Yes, I may have asked a similar question already, maybe 4 years ago. Can not really remember, but this one is different, as I am doing some custom drawing.
Yesterday I created a plain ColorPicker Button in Nim, which is just an ordinary GtkButton without text, and I do connect-after() to draw signal to draw a circle on it with its color. Works fine.
But then I noticed again that draw callback is called many, many times. Its a waste of resources, so question is why it occurs, and is there a chance for my app that I have to draw my colored circle not 26 times whenever user clicks button or moves mouse pointer over button. And, will GTK4 do fewer calls? This is for Gnome Wayland.
Here is a C code example based on ZCode – move mouse pointer over a button or click button.
// http://zetcode.com/gui/gtk2/gtkwidgets/
// gcc -o buttons buttons.c `pkg-config --libs --cflags gtk+-3.0`
#include <gtk/gtk.h>
#include <cairo.h>
int draw(cairo_t *cr) {
printf("draw\n");
return GDK_EVENT_PROPAGATE;
}
int main(int argc, char *argv[]) {
GtkWidget *window;
GtkWidget *box;
GtkWidget *btn;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "GtkButton");
gtk_window_set_default_size(GTK_WINDOW(window), 230, 150);
gtk_container_set_border_width(GTK_CONTAINER(window), 15);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
gtk_container_add(GTK_CONTAINER(window), box);
btn = gtk_button_new_with_label("B1");
gtk_widget_set_size_request(btn, 70, 30);
g_signal_connect(G_OBJECT(btn), "draw",
G_CALLBACK(draw), NULL);
gtk_container_add(GTK_CONTAINER(box), btn);
btn = gtk_button_new_with_label("B2");
gtk_widget_set_size_request(btn, 70, 30);
g_signal_connect(G_OBJECT(btn), "draw",
G_CALLBACK(draw), NULL);
gtk_container_add(GTK_CONTAINER(box), btn);
//g_signal_connect(G_OBJECT(btn), "clicked",
// G_CALLBACK(gtk_main_quit), G_OBJECT(window));
g_signal_connect(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}