Search code examples
gtkgtk3

GTK: How to get the nicks and values from a GSchema enum programmatically


I have the following GSchema:

<schemalist gettext-domain="example">
    <enum id="com.example.unit.temperature">
        <value nick="Celsius" value="0"/>
        <value nick="Fahrenheit" value="1"/>
    </enum>
    <schema id="com.example.corefreqgtk" path="/com/example/">
        <key name="unit-of-temperature" enum="com.example.corefreqgtk.unit.temperature">
            <default>'Celsius'</default>
            <summary>Unit of temperature</summary>
            <description>The unit of temperature used across the app</description>
        </key>
    </schema>
</schemalist>

And I'm looking for a way to get the nicks and values of the enum com.example.unit.temperature programmatically.

What I would like to do is to bind the unit-of-temperature key to a ComboBoxText to allow the user to select the unit of temperature, but I would like to avoid to define twice the enum inside the GScheme and in code.

My goal would be to initialize the ComboBoxText reading the possible values directly form the GSettings.


Solution

  • You can use GSettingsSchema to gather the data inside a GSettings schema. For keys associated with enumeration values, g_settings_schema_key_get_range() will return all possible values as an array.