GtkButton and GtkColorButton

First question: Can I have a GtkButton where I can click with left or right mouse button and can get different actions?

Second: I would like to have a GtkColorButton which opens only the palette dialog when I click with right button, but give me only the current color when I click with left button.

Related question: I know that I can somehow set background color of a GtkButton with CSS. Generally one does that when one creates that button. Can I set background also dynamically?

Reason for these questions: I may have some kind of simple drawing app, and I may have some predefined colors which the user can select from – and the user should be also able to modify these predefined colors.



What about using a GtkMenuButton instead? You could do combining features as using GtkCheckMenuItem or a single action using GtkMenuItem. Further 2 button mouse is not supported by every device.

But I think this would do it for you:

Then connect to GtkWidget::button-release-event().

struct MyData
  /* ... */

my_button_release_callback(GtkWidget *widget, GdkEvent *event, gpointer user_data)
  if(event->button == 1){
    /* mouse button 1 released */
  }else if(event->button == 2){
    /* mouse button 2 released */


GtkEventBox *event_box;

struct MyData my_data;

event_box = gtk_event_box_new();

g_signal_connect(event_box, "button-release",
  G_CALLBACK(my_button_release_callback), &my_data);


I think it is due to the design of GtkButton, it has only these signals:

  • GtkButton::activate()
  • GtkButton::clicked()
  • GtkButton::enter()
  • GtkButton::leave()
  • GtkButton::pressed()
  • GtkButton::released()

related #1

Why not connect to GtkWidget::draw() and do draw your BG using cairo?

GtkAllocation allocation;

gdouble red, green, blue;
gdouble alpha;

red = 1.0;
green = 0.0;
blue = 0.0;
alpha = 0.25;

gtk_widget_get_allocation((GtkWidget *) event_box, &allocation);

/* clear with background color */
                0.0, 0.0,
                (double) allocation.width, (double) allocation.height);

regards, Joël

You can add a second GtkGestureMultiPress to your button

I don’t think GtkColorChooserDialog let’s you do that, so sounds like you need to write it yourself.

You can generate CSS dymanically of course, but if you want an application-provided color that is independent of the theme then writing your own widget and drawing the provided color yourself is indeed what you should do.

1 Like

Thanks for all of your replies, I will read them carefully.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.