commandlinegamer
commandlinegamer

Reputation: 111

Get current/default font for widget(s) in gtkada (GTK3)

Have created a program that displays a Text_View. I have set up buttons to allow the user to change colour scheme and fonts. That works fine.

But when the program first starts it's using whatever font the current GTK3 theme has set.

What I'd like to do is find out what that current font is (before the user has selected a change in family, weight or size), get the font size, apply a scaling factor to it, to apply to a few lines of introductory text I output to the Text_View.

I've tried looking at older answers here, but they seem to be using deprecated options (GTK2), which don't seem to be usable.

Thanks in advance, for any help.

Upvotes: 2

Views: 1224

Answers (2)

commandlinegamer
commandlinegamer

Reputation: 111

Yeah, that helped point me in the right direction. I'd been going round in circles for a couple of days. Still have a bit to do, to tidy up the code, but found this works:

Declarations:

  The_Style_Context : Gtk_Style_Context;
  The_Font_Size     : Gint;
  The_Font_Desc     : Pango_Font_Description;
  The_State         : Gtk.Enums.Gtk_State_Flags := 1;
  The_Tag           : Gtk_Text_Tag;
  Tags              : Gtk_Text_Tag_Table;
  Iter, Start_Iter  : Gtk_Text_Iter;
  Title_Scale       : Constant := 2;
  TextBuffer        : Gtk_Text_Buffer;
  TextView          : Gtk_Text_View;

Assume we've created a new window (Win), any necessary boxes, etc, and the Text View. Now we do:

The_Style_Context := Get_Style_Context (Win);
The_Font_Desc := Get_Font (The_Style_Context, The_State);
The_Font_Name := To_Unbounded_String (Get_Family (The_Font_Desc));
The_Font_Size := Get_Size (The_Font_Desc);
Gtk_New (Tags);
Gtk_New (The_Tag, "title_tag");
Add (Tags, The_Tag);

Following line takes the size of the current font and multiplies it by the scale I chose back in the declaration section.

Set_Property (The_Tag, Size_Property, The_Font_Size * Title_Scale);

Create the text buffer and text view.

Gtk_New (TextBuffer, Tags);
Gtk_New (TextView, TextBuffer);

Assume we set margins for the Text_View and some other initial formatting options. Set some text, then apply the tag's formatting:

TextBuffer.Set_Text ("Hello, world.");

Get_End_Iter (TextBuffer, Iter);
Get_Start_Iter (TextBuffer, Start_Iter);
Apply_Tag (TextBuffer, The_Tag, Start_Iter, Iter);

Show the window, and we're done.

Upvotes: 2

flyx
flyx

Reputation: 39768

Not an expert in GtkAda, but to give you some pointers (links are to GtkAda sources):

In GTK+ 3, you query the style of a widget with Get_Style_Context. This returns a Gtk_Style_Context, which is, basically, a container for CSS-like properties.

You're looking for the font property font-size, which you can query with Get_Property.

After querying this, you create a Gtk_Text_Tag. The most convenient way to do this is query the view's buffer via Get_Buffer and then use Create_Tag.

You need to set Size_Property or Size_Points_Property based on the value you queried (add the desired additional size). I am unsure whether the size is returned in Pango units or points, you need to figure that one out. Regardless of that, you must set Size_Set_Property to True.

Finally, you call Apply_Tag on the view's buffer to apply the tag to text inside the given range.

Upvotes: 3

Related Questions