Is it possible to shrink Text in GtkEntry

, ,

Hi,

I’m working on GTK3 application in cpp. I want to shrink the text in GtkEntry while the text size is beyond the widget size.

Like this -
tp

Changing the font size also decreases the text height with width, is there a way to shrink it with only reducing the width

Is it posible to achieve this in GTK3?

Hi,

I don’t think there is a way to do that automatically.

Pango (the GTK text renderer) supports properties like font-stretch and letter-spacing, which are also exposed to CSS, that could be something to explore.

Hi @gwillems , i tried using it in this way.

void update_font_stretch(GtkEntry *entry) {
    PangoLayout *layout = gtk_entry_get_layout(entry);

    // Get the width of the GtkEntry widget
    int widget_width = gtk_widget_get_allocated_width(GTK_WIDGET(entry));

    // Get the current text width and height (height is not used here)
    int text_width, text_height;
    pango_layout_get_size(layout, &text_width, &text_height);

    text_width /= PANGO_SCALE;

    // Calculate the desired stretch factor
    double stretch_factor = (text_width > widget_width) ? (double)widget_width / text_width : 1.0;

    // Create a Pango attribute for font stretch
    PangoAttrList *attr_list = pango_attr_list_new();
    PangoAttribute *attr = pango_attr_stretch_new((PangoStretch)(PANGO_STRETCH_NORMAL * stretch_factor));
    pango_attr_list_insert(attr_list, attr);

    gtk_entry_set_attributes(entry, attr_list);

    pango_attr_list_unref(attr_list);
}

I called this function when every time user pressed something. But it did not work. Can you help me get it right

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