Why does GTK beep when calling `gtk_entry_set_text` (while resizing a window)?

26 views Asked by At

I'm the maintainer of xnec2c and we found that this code was causing xnec2c to beep through the PC speaker (excessively and annoyingly) while resizing a window or rotating an object:

      snprintf( txt, sizeof(txt), "%7.2f", Viewer_Gain(proj_params, calc_data.freq_step) );
      gtk_entry_set_text( GTK_ENTRY(Builder_Get_Object(builder, widget)), txt );

Why would gtk_entry_set_text beep?

1

There are 1 answers

0
KJ7LNW On

After much troubleshooting, the "%7.2f" was creating text that was too long for the text field defined by widget (NB, widget is a gchar[] string naming the widget, not a widget object).

The length of txt exceeded the text field's max-length as defined by the Glade XML, so gtk_entry_set_text would trigger a beep event every time the widget text was updated.

In case it helps someone else, this was the fix:

-      snprintf( txt, sizeof(txt), "%7.2f", Viewer_Gain(proj_params, calc_data.freq_step) );
+      snprintf( txt, sizeof(txt)-1, "%.2f", Viewer_Gain(proj_params, calc_data.freq_step) );
       gtk_entry_set_text( GTK_ENTRY(Builder_Get_Object(builder, widget)), txt );
...
-        <property name="max-length">6</property>
+        <property name="max-length">9</property>

and this is the commit that solved our issue if you wish to review the entire thing.