Search code examples
gtk4

How to prevent text view from outgrowing window gtk4


Suppose I have the following layout in GTK4: A vertical box in a window containing three child widgets:

┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ 
┃             Widget 1              ┃
┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
┃                                   ┃
┃                                   ┃
┃           Text View               ┃
┃                                   ┃
┃                                   ┃
┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
┃            Widget 2               ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ 

Where The Text View has the vexpand property set to true. I'm running into the following issue: when the text view gets large enough (by inserting lines), the box expands beyond the size of the window, pushing widget 2 out of visibility.

How can I make sure that the text view is limited in size by the parent box/window?

From what I can see/find, the solution is likely related to size requests, but I'm not sure how to dynamically calculate what the maximum size of the text view should be. (Note that I'm working on a way to add multiple text views which are vertically/horizontally separated, so Ideally I'd like a general solution where the widget can still resize when the conditions/layout change.


Solution

  • After some experimentation, I've found a solution. By placing the text view in a Gtk.Scrolled Window, the text view will scroll instead of growing beyond the limits of the window.

    This is somewhat confusing to me, given that Gtk.TextView supposedly implements the Gtk.Scrollable interface, but I may be misunderstanding the purpose of this interface.