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.

Hi,

#1

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.

https://developer.gnome.org/gtk3/stable/GtkMenuButton.html

But I think this would do it for you:

https://developer.gnome.org/gtk3/stable/GtkEventBox.html

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

https://developer.gnome.org/gtk3/stable/GtkWidget.html#GtkWidget-button-release-event

struct MyData
{
  /* ... */
};

gboolean
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 */
  }

  return(FALSE);
}

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);

2

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?

https://developer.gnome.org/gtk3/stable/GtkWidget.html#GtkWidget-draw

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 */
cairo_set_source_rgba(cr,
                      red,
                      green,
                      blue,
                      alpha);
cairo_rectangle(cr,
                0.0, 0.0,
                (double) allocation.width, (double) allocation.height);
cairo_fill(cr);

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.