How to set font size of lablgtk widgets


#1

I am trying the introduction to gtk programming at https://ocaml.org/learn/tutorials/introduction_to_gtk.html#Lablgtk-and-writing-Gtk-applications. When I run the first program there that opens a screen with a menu and 1 large button, first thing I notice is that the fonts are too small for my eyes. So, first thing I try to change is the font size. I find that there is a font size in the Pango context, and that I can change it, I think, but I cannot get this to show up on the screen. My version of the program is create_window.ml, as follows:

open GMain

open GdkKeysyms

open Pango

open GPango

let locale = GtkMain.Main.init ()
let context = Gdk.Screen.get_pango_context ()
let fd = (Pango.Context.get_font_description context)

let main () =
Pango.Font.set_size fd 20;
Pango.Context.set_font_description context fd;
let window = GWindow.window ~width:320 ~height:240
~title:“Simple lablgtk program” () in

let vbox = GPack.vbox ~packing:window#add () in
window#connect#destroy ~callback:Main.quit;

(* Menu bar *)
let menubar = GMenu.menu_bar ~packing:vbox#pack () in
let factory = new GMenu.factory menubar in
let accel_group = factory#accel_group in
let file_menu = factory#add_submenu “File” in

(* File menu *)
let factory = new GMenu.factory file_menu ~accel_group in
factory#add_item “Quit” ~key:_Q ~callback: Main.quit;

(* Button *)
let button = GButton.button ~label:“Push me!”
~packing:vbox#add () in
button#connect#clicked ~callback: (fun () -> prerr_endline “Ouch!”);

(* Display the windows and enter Gtk+ main loop *)
window#add_accel_group accel_group;
window#show ();
Main.main ()

let () = main ()

My computer is linux, running fedora 29 with ocaml 4.07.1 and opam. I belive that both lablgtk2 and lablgtk3 are installed, because I get the same results when I compile with

$ ocamlfind ocamlc -g -package lablgtk2 -linkpkg create_window.ml -o create_window

and with

$ ocamlfind ocamlc -g -package lablgtk3 -linkpkg create_window.ml -o create_window

Which is that the text of the button, screen header and menu are not noticeably larger than with the original font size of 12.

What is the OCaml way of getting control of this?

TIA.


#2

The correct font size will depend on the user and screen, so it’s a very bad idea to hard-code it in your program. Instead, configure your system as a whole to show text at your preferred size by default. This works for me on Fedora 29:

gsettings set org.gnome.desktop.interface text-scaling-factor 2

Both the GTK 2 and GTK 3 binaries responded to changes to that setting, although oddly the GTK 3 one was always twice the size of the GTK 2 one for me! You may also have some graphical configuration tool with a “DPI” setting that might help.

For completeness, if you really did want just this particular button to have larger text, you could do:

let button = GButton.button ~packing:vbox#add () in
let _ : GMisc.label = GMisc.label ~packing:button#add
                                  ~markup:"<big>Push me!</big>" () in


#3

Thanks. The gsettings thing works, but it applies to all apps open on the machine and any apps opened subsequently (even after a system reboot!). I would advise caution before trying this, because some linux apps have pull-down menus and pop-up windows that won’t fit on the screen with that setting, leaving critical parts of the UI (like menu options and the OK and Cancel buttons) both unseen and unreachable.

Is there any way to wrap the markup around a window or a widget when I open it? I couldn’t get it to work on the text of menu items, and those are often the hardest to read because the menus have the lowest contrast.

The statement in my code:

let context = Gdk.Screen.get_pango_context ()

kind of makes me think that there ought to be a setter to go with this getter, but there is none that I can find. The getter offers an explanation of the settings with which one is stuck, but I still feel a little stuck. No way around that? Do other interfaces to gtk under linux, e.g. gtkserver, wx, tk, fltk, offer more flexibility?


#4

Is this problem happening specifically with the Lablgtk app? Are the fonts the same small size with other Gtk apps on your machine?


#5

Many apps give a way to control this. Ctrl-+ and Ctrl-- work in web browsers and the linux terminal. Most editors and IDE’s have a way to select font sizes for most or all of the text a user sees. Many other apps with a View pulldown menu have a zoom setting there. I can read most normal-sized text, but it is not easy for me, so I get a headache after a half hour or so on a screen that most people consider fine. Because most GUI apps have the font selection or the zoom menu item, I assume that most UI/GUI development libraries have a way to do the zooming, so that’s one of the first things with which I try to tinker when I use a new tool or library, so that I can enjoy it. If I come up with an app to distribute or share, I can expose the options to zoom in and out. If it’s just for me, I can just insert a covert call to set up what works for me. But as web front-ends become more popular, non-html GUI’s do not get much attention and these features are often now hard to find. Most OS’s meet legal requirements with some kind of magnifying glass accessibility feature, but those are way overkill for what I need and hideously cumbersome and inconvenient for me.


#6

You can try something like

button#misc#modify_font_by_name "Sans 14"

See also: https://github.com/search?q=modify_font_by_name&type=Code


#7

Thanks. That link has some pretty good info. I also see:

  menubar#misc# *modify_font_by_name* default_font;

Which might be what I need for the menus.

Are the methods of button#misc the same as the misc_ops that are described in the gtk docs? Is there a place where I can see the list of font names available?


#8

a) methods of button#misc are basically methods of widget#misc. So yes, misc_ops. See http://lablgtk.forge.ocamlcore.org/refdoc/GObj.widget-c.html

b) as for the list of font names, those are same the for all Gtk2 (3) applications and the ones shown in the system font selector


#9
open GMain
open GdkKeysyms

let locale = GtkMain.Main.init ()

let main () =
  let window = GWindow.window ~width:320 ~height:240
                              ~title:"Simple lablgtk program" () in
  let vbox = GPack.vbox ~packing:window#add () in
  window#connect#destroy ~callback:Main.quit;

  (* Menu bar *)
  let menubar = GMenu.menu_bar ~packing:vbox#pack () in
  let factory = new GMenu.factory menubar in
  let accel_group = factory#accel_group in
  let file_menu = factory#add_submenu "File" in

  (* File menu *)
  let factory = new GMenu.factory file_menu ~accel_group in
  factory#add_item "Quit" ~key:_Q ~callback: Main.quit;

  (* Button *)
  let button = GButton.button ~label:"Push me!"
                              ~packing:vbox#add () in
  button#misc#modify_font_by_name "Sans 16";
  button#connect#clicked ~callback: (fun () -> prerr_endline "Ouch!");
      
  (* Display the windows and enter Gtk+ main loop *)
  window#add_accel_group accel_group;
  window#show ();
  Main.main ()

let () = main ()

Compiles and runs without error, but no noticeable change in the font size. When I put the mouse cursor in VSCode over the font name parameter, a mouse-over type message from merlin tells me that the parameter will not be used (???).


#10

Can you try changing the modify_font_by_name line to

  button#child#misc#modify_font_by_name "Sans 16";

Now, what happens is that the that button#child gives the button label and the font is modified on label.

You could have been done that error in C as well. Actually, googling the C function name is what helped me find the cause.


#11

Yes, that works. Thanks!