Making a GtkButton round

2020-04-17 03:42发布

问题:

How can I make GtkBtton round in shape?

回答1:

There are a few options.

  • Tweak the style. There is no roundness option in the base set. Some theme engines may provide border image specification or roundness but it will not work everywhere.
  • Force a theme that looks the way you want it too. This is not too hard you just load a gtkrc string that specifies the theme you want. You will need to ensure users have the theme or are ok with the fallback.
  • Subclass the button and draw it yourself. One way is to set border and relief to 0, pack an image, and respond to state change events to use appropriate pre-light (hover) images. Another way is to rewrite the expose event and draw the button as you wish.


回答2:

The accepted answer said:

There is no roundness option in the base set.

Well, there is now!

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

In special cases, buttons can be made round by adding the .circular style class.

So, e.g.:

gtk_style_context_add_class(
    gtk_widget_get_style_context( GTK_WIDGET(button) ),
    "circular"
);

This works using both of the standard themes, Adwaita and HighContrast.

There is an issue though: This only work satisfactorily for buttons that have no expand properties set or are not otherwise manually upsized. I've not figured out whether this can be made to work with GtkButtons having expanded size, as with the enlarged button that I wanted this for, I'm getting rounded corners but with flat regions at the sides. :/ My guess is the devs haven't thought, or cared... about supporting larger buttons having the circular style class. Maybe I'll ask on IRC sometime.

update: That was patched, so the curvature now extends all the way around, instead of having flat segments. You'll still get ovals rather than circles if you get the dimensions non-square, but they'll be curved all the way.



标签: gtk