Different Units of Measurement in Libreoffice


Table of Contents

Git Git Git Git Git Git

Why work on this

Working on this involves understanding the controlls, specially the spinedit, finding some way to modify it so that clicking on the units “the last 2 characters of the label in the spinedit entry” shows a dropdown, from which we can select a different unit.

Mockup

I find working on this as a great opportunity to get deeper insights into widget implementations

Finding a Starting Point

I had no idea about where to start. One approach that appealed to me was if somehow I find the cursor position when the spinedit goes into edit mode, and compare it to the text length in the GtkEntry (internal) of the spinedit, then I can easily say that if the cursor is on the last 2 characters, then show a dropdown here. But that would not work, because “creating a new widget” ==> the logic should be built into the widget itself (I don’t know, it’s just guess-work).

Then I went to #gtk IRC channel, and asked about how to approach this problem.

IRC Conversation
sahil_ hi, I have a task to write a custom spinedit control, which will have a fontsize field (with a number and a unit like mm, pt, etc). It should be such that if I click on the unit with mouse, then it should show me a dropdown to use different units. Mockup Here’s the mockup. How can I approach it?
sahil_ Libreoffice uses glade and custom welding for widgets. And from the research I did on it, I found that a normal gtkentry is aware of the cursor position, which can be used like if the cursor is on the last 2 characters, then show the popup.
sahil_ But a new control has to be created for that (in my understanding).
mclasen The pieces are GtkText and GtkGestureClick. You just have to glue them together in the right way
sahil_ mclasen: and for the dropdown?
mclasen GtkPopover is what is used for that
sahil_ Also the spinedit doesn’t expose the gtkEntry (it’s internal). So do I have to create one from scratch?
sahil_ or does it?
mclasen you can take a look at how GtkEntry, GtkSpinButton, etc are put together nowadays they are all just wrappers around a GtkText widget

This was more than enough to get started.

Starting the Hunt

Looking into it, I found that I was mixing up GtkComboBox, which is used as fontsizebox, and GtkSpinButton, which is what I was supposed to explore and modify. weld::SpinButton, which is libreoffice’s wrapper over GtkSpinButton (if I am not wrong) has functions which give access to the cursor location in the Entry. It would require some playing around to understand the dynamics though.

I am also curious about how a dialog, or any UI element, which is created using glade, which usually has the widgets as Gtk types (GtkLabel, GtkButton etc.) is loaded into different backends. Knowing how the welding works would answer it I think.