LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   GtkEntry change font size, can it be done in the XML? (https://www.linuxquestions.org/questions/linux-software-2/gtkentry-change-font-size-can-it-be-done-in-the-xml-4175716206/)

jon Elson 08-29-2022 02:13 PM

GtkEntry change font size, can it be done in the XML?
 
I have a c app with a GUI built with glade. I need to change the font size, and it seems easiest to just change the XML file. Here's a snip of one of the GtkEntry sections:
<child>
<object class="GtkEntry" id="count1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="editable">False</property>
<property name="invisible_char">●</property>
<property name="activates_default">True</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
Can I just add a line there that will specify the font or just the font size?
Thanks,
Jon

EdGr 08-29-2022 06:44 PM

The short answer is no.
Ed

jon Elson 08-30-2022 09:27 AM

Ok, how about the long answer? I'm guessing I need to do this in the C code, with set attributes, but I haven't found clear documentation on how to do that. Can you give me a pointer?
Thanks,
Jon

boughtonp 08-30-2022 10:00 AM


 
Noooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo.

:)

(I don't know the answer.)

Doesn't Gtk use CSS? Does the XML have an equivalent to HTML's style attribute?

Also, are there not differences between Gtk 3 and Gtk 4 - you haven't specified which one you're using?


EdGr 08-30-2022 11:21 AM

Quote:

Originally Posted by jon Elson (Post 6377140)
Ok, how about the long answer? I'm guessing I need to do this in the C code, with set attributes, but I haven't found clear documentation on how to do that. Can you give me a pointer?
Thanks,
Jon

On GTK 3, you can call gtk_widget_override_font (). On GTK 4, I believe you call gtk_css_provider_load_from_data () with custom CSS. The latter is not clearly documented.
Ed

jon Elson 08-30-2022 12:34 PM

OK, I'm trying to do this in the C code. Here's what I did :
PangoAttrList *attr;
gtk_init (&argc, &argv);

builder = gtk_builder_new ();
gtk_builder_add_from_file (builder, "scaler.glade", NULL);
window = GTK_WIDGET (gtk_builder_get_object (builder, "window1"));
// Pscaler1 = GTK_ENTRY( gtk_builder_get_object(builder, "count1" ) );
Pscaler[0] = GTK_ENTRY( gtk_builder_get_object(builder, "count1" ) );
attr= pango_attr_list_new();
pango_attr_list_insert(attr,pango_attr_scale_new(PANGO_SCALE_XX_LARGE));
gtk_entry_set_attributes(Pscaler[0], attr);

But, I get a compile-time error :/home/elson/scaler3/scaler.c:246: undefined reference to `gtk_entry_set_attributes'

Any idea what is going on? This is an older Beagle Board system running Gtk 2.0 so may not have all the latest features.
And, if there's a way to bump up the size of all fonts, that might be fine, as this is a single-purpose embedded computer.
Thanks,
Jon

teckk 08-30-2022 01:02 PM

You still haven't told what version of gtk that you are using. An Example for gtk3:

PangoDem.c
Code:

#include <gtk/gtk.h>

static void
add_label (GtkWidget* window, gchar *text)
{
    GtkWidget *label = gtk_label_new(text);
    PangoAttrList *attrlist = pango_attr_list_new();
    PangoAttribute *attr = pango_attr_size_new_absolute(30 * PANGO_SCALE);
    pango_attr_list_insert(attrlist, attr);
    gtk_label_set_attributes(GTK_LABEL(label), attrlist);
    pango_attr_list_unref(attrlist);
    gtk_container_add (GTK_CONTAINER (window), label);
}

static void
activate (GtkApplication* app, gpointer user_data)
{
    GtkWidget *window;
    window = gtk_application_window_new (app);
    gtk_window_set_title (GTK_WINDOW (window), "Window1");
    gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
    add_label( window, "Hello world" );
    gtk_widget_show_all (window);
}

int
main (int argc, char **argv)
{
    GtkApplication *app;
    int status;

    app = gtk_application_new("org.gtk.example", G_APPLICATION_FLAGS_NONE);
    g_signal_connect( app, "activate", G_CALLBACK(activate), NULL);
    status = g_application_run(G_APPLICATION(app), argc, argv);
    g_object_unref (app);

    return status;
}

//gcc -lm PangoDem.c -o PangoDem $(pkg-config --cflags --libs gtk+-3.0)


teckk 08-30-2022 01:15 PM

Oh ok. I re-read post #6, you said gtk2, not sure if I have an example in my cache.

EdGr 08-30-2022 01:17 PM

Quote:

Originally Posted by jon Elson (Post 6377187)
But, I get a compile-time error :/home/elson/scaler3/scaler.c:246: undefined reference to `gtk_entry_set_attributes'

Any idea what is going on? This is an older Beagle Board system running Gtk 2.0 so may not have all the latest features.
And, if there's a way to bump up the size of all fonts, that might be fine, as this is a single-purpose embedded computer.
Thanks,
Jon

That call was added on GTK 3.

You should have said at the beginning that you are using GTK 2 and you want increase the font size globally.

In your ~/.gtkrc-2.0, add:

Code:

gtk-font-name = "Sans 14"
or larger.
Ed

jon Elson 08-30-2022 01:58 PM

Hmm, I am linked with gtk+-2.0, not sure if that makes a difference. Anyway, I have managed to change the font size of the toolbar items on emacs, but it had NO effect on the app in question. I can't find aywhere in the .c or .glade file that calls for a specific font.
There was no ~/.gtkrc-2.0 file, so I created one, and put the above text there, but it seemed to make no difference.
Thanks,
Jon

teckk 08-30-2022 02:24 PM

You did stop X, and then re start it?
If you are wanting to change gtk2 globally

~/.gtkrc-2.0
Code:

gtk-icon-theme-name="Adwaita"
gtk-theme-name="gtk2theme"
gtk-font-name="Sans 15"
gtk-cursor-theme-size=40
#gtk-toolbar-icon-size=GTK_ICON_SIZE_SMALL_TOOLBAR

gtk-icon-sizes = "panel-menu=32,32:panel=32,32:gtk-menu=32,32:gtk-large-toolbar=32,32:gtk-small-toolbar=32,32:gtk-button=32,32"

#16 24 32 48 64

#gtk-cursor-theme-name="Breeze_Amber"
#gtk-toolbar-style=GTK_TOOLBAR_BOTH_HORIZ
#gtk-button-images=0
#gtk-menu-images=0
#gtk-enable-event-sounds=0
#gtk-enable-input-feedback-sounds=0
#gtk-xft-antialias=1
#gtk-xft-hinting=1
#gtk-xft-hintstyle="hintslight"
#gtk-xft-rgba="rgb"


EdGr 08-30-2022 02:29 PM

Your desktop environment may have its own font settings that override ~/.gtkrc-2.0
Ed

jon Elson 08-30-2022 03:37 PM

Quote:

Originally Posted by EdGr (Post 6377206)
Your desktop environment may have its own font settings that override ~/.gtkrc-2.0
Ed

Umm, how do I figure that out? I didn't know about restarting X, I had just been logging out and back in. Now, I did do a reboot.
One issue is there may be very few fonts loaded on this system. I used fc-list and typed in several versions of the font name, last version of ~/.gtkrc-3.0 was:

gtk-icon-theme-name= "Tangerine"
gtk-theme-name= "dark"
gtk-font-name = "DejaVu Sans Mono:style=Bold 24"

But, I also tried it without the :style= part.
I am just pretty lost here.
Thanks,
Jon

jon Elson 08-30-2022 03:41 PM

Quote:

Originally Posted by EdGr (Post 6377206)
Your desktop environment may have its own font settings that override ~/.gtkrc-2.0
Ed

Hmmm, this embedded machine doesn't have a desktop environment, I am logging in via ssh -X from a Linux machine. In the final use, people will be logging in using putty and Xming from a Windows machine. Is there a way to set those up to make the text bigger? I was under the impression that an ssh -X session was a pixel-accurate representation of what the app had set up. This isn't being interpreted by an html browser.
Thanks,
Jon

EdGr 08-30-2022 08:04 PM

That should have worked. Run your program under strace to see which files it is reading.
Ed


All times are GMT -5. The time now is 05:33 PM.