How do I create a small Gtk.Button

hi, all !
Now I creating a gtk3 application by C. and I want to add “a small Button” like this:

  • text of button: “long-long-text”
  • width of button: 10px; // not enough to show a whole text
    want to display like:

| lo… | ← omitted text by “…”

I tried to write C like this:

GtkWidget *button = gtk_button_new_with_label("long-long-text");
GtkStyleContext *context = gtk_widget_get_style_context(button);
GtkStyleProvider *p = GTK_STYLE_PROVIDER(gtk_css_provider_new());
      "button {"
      "  min-width: 1px;"
      , -1, NULL);
gtk_style_context_add_provider(context, GTK_STYLE_PROVIDER(p), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);

gtk_container_add(GTK_CONTAINER(parent), button);

but above program show the whole text.

How do I specify the maximum width of button ?

Create the label manually, use :ellipsize as needed, and add it to the button.

You shouldn’t need a style provider here, but whenever you do, please use gtk_style_context_add_provider_for_screen() (in GTK4, _for_display()) because adding CSS to individual contexts is buggy and deprecated.

1 Like

thank you ! i’ll try that !

adding CSS to individual contexts is buggy and deprecated

oh, I dont know that. Do you have any reference for that ? (Gtk specific ? or common knowledgement of CSS ?)

It’s a GTK-specific limitation: adding a style provider to a widget’s specific style context does not cascade, and will not apply to any of the widget’s child.


thank you for reply.
okey, I understand that!

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