From 0fbfc2d8f4c47e2c46685caf66ba12ea9374526e Mon Sep 17 00:00:00 2001
From: c vw <dl1ycf@darc.de>
Date: Thu, 21 Apr 2022 18:22:59 +0200
Subject: [PATCH] Optimization for ComboBoxes on touch-screens (on by default,
 can be switched off in radio menu)

---
 ant_menu.c   |  4 ++--
 discovery.c  |  4 ++--
 midi_menu.c  |  2 +-
 pa_menu.c    |  2 +-
 radio.c      | 33 +++++++++++++++++++++++++++++++++
 radio_menu.c | 32 +++++++++++++++++++++-----------
 rx_menu.c    |  2 +-
 sliders.c    |  3 +++
 tx_menu.c    |  4 ++--
 vfo_menu.c   |  4 ++--
 10 files changed, 68 insertions(+), 22 deletions(-)

diff --git a/ant_menu.c b/ant_menu.c
index 681b313..22669c3 100644
--- a/ant_menu.c
+++ b/ant_menu.c
@@ -444,7 +444,7 @@ g_print("rx_antennas=%ld\n",radio->info.soapy.rx_antennas);
 
       gtk_combo_box_set_active(GTK_COMBO_BOX(adc0_antenna_combo_box),adc[0].antenna);
       g_signal_connect(adc0_antenna_combo_box,"changed",G_CALLBACK(adc0_antenna_cb),&adc[0]);
-      gtk_grid_attach(GTK_GRID(grid),adc0_antenna_combo_box,1,1,1,1);
+      my_combo_attach(GTK_GRID(grid),adc0_antenna_combo_box,1,1,1,1);
     }
 
     if(can_transmit) {
@@ -461,7 +461,7 @@ g_print("rx_antennas=%ld\n",radio->info.soapy.rx_antennas);
   
         gtk_combo_box_set_active(GTK_COMBO_BOX(dac0_antenna_combo_box),dac[0].antenna);
         g_signal_connect(dac0_antenna_combo_box,"changed",G_CALLBACK(dac0_antenna_cb),&dac[0]);
-        gtk_grid_attach(GTK_GRID(grid),dac0_antenna_combo_box,1,2,1,1);
+        my_combo_attach(GTK_GRID(grid),dac0_antenna_combo_box,1,2,1,1);
       }
     }
 
diff --git a/discovery.c b/discovery.c
index d854d07..956439a 100644
--- a/discovery.c
+++ b/discovery.c
@@ -444,7 +444,7 @@ fprintf(stderr,"%p Protocol=%d name=%s\n",d,d->protocol,d->name);
                   "stemlab_sdr_transceiver_hpsdr");
             }
             gtk_widget_show(apps_combobox[row]);
-            gtk_grid_attach(GTK_GRID(grid), apps_combobox[row], 4, row, 1, 1);
+            my_combo_attach(GTK_GRID(grid), apps_combobox[row], 4, row, 1, 1);
           }
         }
 #endif
@@ -496,7 +496,7 @@ fprintf(stderr,"%p Protocol=%d name=%s\n",d,d->protocol,d->name);
     gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(gpio),NULL,"Controller2 V1");
     gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(gpio),NULL,"Controller2 V2");
     //gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(gpio),NULL,"Controller I2C");
-    gtk_grid_attach(GTK_GRID(grid),gpio,0,row,1,1);
+    my_combo_attach(GTK_GRID(grid),gpio,0,row,1,1);
 
     gtk_combo_box_set_active(GTK_COMBO_BOX(gpio),controller);
     g_signal_connect(gpio,"changed",G_CALLBACK(gpio_changed_cb),NULL);
diff --git a/midi_menu.c b/midi_menu.c
index e7f2bbd..0fbf391 100644
--- a/midi_menu.c
+++ b/midi_menu.c
@@ -895,7 +895,7 @@ void midi_menu(GtkWidget *parent) {
   newNote=gtk_label_new("");
   gtk_grid_attach(GTK_GRID(grid),newNote,col++,row,1,1);
   newType=gtk_combo_box_text_new();
-  gtk_grid_attach(GTK_GRID(grid),newType,col++,row,1,1);
+  my_combo_attach(GTK_GRID(grid),newType,col++,row,1,1);
   g_signal_connect(newType,"changed",G_CALLBACK(type_changed_cb),NULL);
   newVal=gtk_label_new("");
   gtk_grid_attach(GTK_GRID(grid),newVal,col++,row,1,1);
diff --git a/pa_menu.c b/pa_menu.c
index 43c3890..f9b8931 100644
--- a/pa_menu.c
+++ b/pa_menu.c
@@ -242,7 +242,7 @@ void pa_menu(GtkWidget *parent) {
   gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(max_power_b),NULL,"200W");
   gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(max_power_b),NULL,"500W");
   gtk_combo_box_set_active(GTK_COMBO_BOX(max_power_b),pa_power);
-  gtk_grid_attach(GTK_GRID(grid0),max_power_b,2,0,1,1);
+  my_combo_attach(GTK_GRID(grid0),max_power_b,2,0,1,1);
   g_signal_connect(max_power_b,"changed",G_CALLBACK(max_power_changed_cb),NULL);
 
   GtkWidget *tx_out_of_band_b=gtk_check_button_new_with_label("Transmit out of band");
diff --git a/radio.c b/radio.c
index bdd3a32..b879522 100644
--- a/radio.c
+++ b/radio.c
@@ -350,6 +350,7 @@ double meter_calibration=0.0;
 double display_calibration=0.0;
 
 int can_transmit=0;
+int optimize_for_touchscreen=1;
 
 gboolean duplex=FALSE;
 gboolean mute_rx_while_transmitting=FALSE;
@@ -2257,6 +2258,9 @@ g_print("radioRestoreState: %s\n",property_path);
       if(value) rx_gain_calibration=atoi(value);
     }
 
+    value=getProperty("optimize_touchscreen");
+    if (value) optimize_for_touchscreen=atoi(value);
+
 
     filterRestoreState();
     bandRestoreState();
@@ -2701,6 +2705,9 @@ g_print("radioSaveState: %s\n",property_path);
     sprintf(value,"%d",iqswap);
     setProperty("iqswap",value);
 	
+    sprintf(value,"%d",optimize_for_touchscreen);
+    setProperty("optimize_touchscreen", value);
+
 #ifdef CLIENT_SERVER
     sprintf(value,"%d",hpsdr_server);
     setProperty("radio.hpsdr_server",value);
@@ -2864,3 +2871,29 @@ int remote_start(void *data) {
   return 0;
 }
 #endif
+
+static gboolean eventbox_callback(GtkWidget *widget, GdkEvent *event, gpointer data)
+{
+  if (event->type == GDK_BUTTON_RELEASE) {
+    gtk_combo_box_popup(GTK_COMBO_BOX(data));
+    return TRUE;
+  }
+  return FALSE;
+}
+
+//
+// This function replaces gtk_grid_attach for a newly created combo-box
+//
+void my_combo_attach(GtkGrid *grid, GtkWidget *combo, int row, int col, int spanrow, int spancol)
+{
+    if (optimize_for_touchscreen) {
+      GtkWidget *eventbox = gtk_event_box_new();
+      g_signal_connect( eventbox, "event",   G_CALLBACK(eventbox_callback),   combo);
+      gtk_container_add(GTK_CONTAINER(eventbox), combo);
+      gtk_event_box_set_above_child(GTK_EVENT_BOX(eventbox), TRUE);
+      gtk_grid_attach(GTK_GRID(grid),eventbox,row,col,spanrow,spancol);
+    } else {
+      gtk_grid_attach(GTK_GRID(grid),combo,row,col,spanrow,spancol);
+    }
+}
+
diff --git a/radio_menu.c b/radio_menu.c
index 070f4ca..a1eee35 100644
--- a/radio_menu.c
+++ b/radio_menu.c
@@ -215,6 +215,10 @@ static void iqswap_cb(GtkWidget *widget, gpointer data) {
   iqswap=gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
 }
 
+static void touchscreen_cb(GtkWidget *widget, gpointer data) {
+  optimize_for_touchscreen=gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
+}
+
 static void split_cb(GtkWidget *widget, gpointer data) {
   int new=gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
   if (new != split) g_idle_add(ext_split_toggle, NULL);
@@ -476,7 +480,7 @@ void radio_menu(GtkWidget *parent) {
     gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(receivers_combo),NULL,"2");
   }
   gtk_combo_box_set_active(GTK_COMBO_BOX(receivers_combo),receivers - 1);
-  gtk_grid_attach(GTK_GRID(grid),receivers_combo,col,row,1,1);
+  my_combo_attach(GTK_GRID(grid),receivers_combo,col,row,1,1);
   g_signal_connect(receivers_combo,"changed",G_CALLBACK(receivers_cb),NULL);
   
   row++;
@@ -512,7 +516,7 @@ void radio_menu(GtkWidget *parent) {
             gtk_combo_box_set_active(GTK_COMBO_BOX(sample_rate_combo_box),3);
 	    break;
       }
-      gtk_grid_attach(GTK_GRID(grid),sample_rate_combo_box,col,row,1,1);
+      my_combo_attach(GTK_GRID(grid),sample_rate_combo_box,col,row,1,1);
       g_signal_connect(sample_rate_combo_box,"changed",G_CALLBACK(sample_rate_cb),NULL);
       row++;
       }
@@ -559,7 +563,7 @@ void radio_menu(GtkWidget *parent) {
         gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(sample_rate_combo_box),NULL,rate);
         gtk_combo_box_set_active(GTK_COMBO_BOX(sample_rate_combo_box),0);
       }
-      gtk_grid_attach(GTK_GRID(grid),sample_rate_combo_box,col,row,1,1);
+      my_combo_attach(GTK_GRID(grid),sample_rate_combo_box,col,row,1,1);
       g_signal_connect(sample_rate_combo_box,"changed",G_CALLBACK(sample_rate_cb),NULL);
       row++;
       }
@@ -597,7 +601,7 @@ void radio_menu(GtkWidget *parent) {
       gtk_combo_box_set_active(GTK_COMBO_BOX(rit_combo),2);
       break;
   }
-  gtk_grid_attach(GTK_GRID(grid), rit_combo, col, row, 1, 1);
+  my_combo_attach(GTK_GRID(grid), rit_combo, col, row, 1, 1);
   g_signal_connect(rit_combo,"changed",G_CALLBACK(rit_cb),NULL);
   row++;
 
@@ -611,7 +615,7 @@ void radio_menu(GtkWidget *parent) {
   gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(sat_combo),NULL,"SAT");
   gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(sat_combo),NULL,"RSAT");
   gtk_combo_box_set_active(GTK_COMBO_BOX(sat_combo),sat_mode);
-  gtk_grid_attach(GTK_GRID(grid),sat_combo,col,row,1,1);
+  my_combo_attach(GTK_GRID(grid),sat_combo,col,row,1,1);
   g_signal_connect(sat_combo,"changed",G_CALLBACK(sat_cb),NULL);
   row++;
 
@@ -629,7 +633,7 @@ void radio_menu(GtkWidget *parent) {
   gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(region_combo),NULL,"UK");
   gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(region_combo),NULL,"WRC15");
   gtk_combo_box_set_active(GTK_COMBO_BOX(region_combo),region);
-  gtk_grid_attach(GTK_GRID(grid),region_combo,col,row,1,1);
+  my_combo_attach(GTK_GRID(grid),region_combo,col,row,1,1);
   g_signal_connect(region_combo,"changed",G_CALLBACK(region_cb),NULL);
   row++;
 
@@ -665,7 +669,7 @@ void radio_menu(GtkWidget *parent) {
 	gtk_combo_box_set_active(GTK_COMBO_BOX(filter_combo),4);
         break;
     }
-    gtk_grid_attach(GTK_GRID(grid), filter_combo, col, row, 1, 1);
+    my_combo_attach(GTK_GRID(grid), filter_combo, col, row, 1, 1);
     g_signal_connect(filter_combo,"changed",G_CALLBACK(filter_cb),NULL);
 
     row++;
@@ -714,7 +718,7 @@ void radio_menu(GtkWidget *parent) {
     gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(ck10mhz_combo),NULL,"Penelope");
     gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(ck10mhz_combo),NULL,"Mercury");
     gtk_combo_box_set_active(GTK_COMBO_BOX(ck10mhz_combo),atlas_clock_source_10mhz);
-    gtk_grid_attach(GTK_GRID(grid),ck10mhz_combo,col+1,row,1,1);
+    my_combo_attach(GTK_GRID(grid),ck10mhz_combo,col+1,row,1,1);
     g_signal_connect(ck10mhz_combo,"changed",G_CALLBACK(ck10mhz_cb),NULL);
 
     row++;
@@ -727,7 +731,7 @@ void radio_menu(GtkWidget *parent) {
     gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(ck128mhz_combo),NULL,"Penelope");
     gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(ck128mhz_combo),NULL,"Mercury");
     gtk_combo_box_set_active(GTK_COMBO_BOX(ck128mhz_combo),atlas_clock_source_128mhz ? 1 : 0);
-    gtk_grid_attach(GTK_GRID(grid),ck128mhz_combo,col+1,row,1,1);
+    my_combo_attach(GTK_GRID(grid),ck128mhz_combo,col+1,row,1,1);
     g_signal_connect(ck128mhz_combo,"changed",G_CALLBACK(ck128mhz_cb),NULL);
 
     row++;
@@ -740,7 +744,7 @@ void radio_menu(GtkWidget *parent) {
     gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(micsource_combo),NULL,"Janus");
     gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(micsource_combo),NULL,"Penelope");
     gtk_combo_box_set_active(GTK_COMBO_BOX(micsource_combo),atlas_mic_source ? 1 : 0);
-    gtk_grid_attach(GTK_GRID(grid),micsource_combo,col+1,row,1,1);
+    my_combo_attach(GTK_GRID(grid),micsource_combo,col+1,row,1,1);
     g_signal_connect(micsource_combo,"changed",G_CALLBACK(micsource_cb),NULL);
 
     row++;
@@ -754,7 +758,7 @@ void radio_menu(GtkWidget *parent) {
     gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(tx_combo),NULL,"Penelope");
     gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(tx_combo),NULL,"Pennylane");
     gtk_combo_box_set_active(GTK_COMBO_BOX(tx_combo),atlas_penelope);
-    gtk_grid_attach(GTK_GRID(grid),tx_combo,col+1,row,1,1);
+    my_combo_attach(GTK_GRID(grid),tx_combo,col+1,row,1,1);
     g_signal_connect(tx_combo,"changed",G_CALLBACK(tx_cb),NULL);
 
     row++;
@@ -885,6 +889,12 @@ void radio_menu(GtkWidget *parent) {
     g_signal_connect(rx_gain_calibration_b,"value_changed",G_CALLBACK(rx_gain_calibration_value_changed_cb),NULL);
   }
 
+  col++;
+  GtkWidget *touchscreen_b=gtk_check_button_new_with_label("TouchScreen");
+  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (touchscreen_b), optimize_for_touchscreen);
+  gtk_grid_attach(GTK_GRID(grid),touchscreen_b,col,row,1,1);
+  g_signal_connect(touchscreen_b,"toggled",G_CALLBACK(touchscreen_cb),NULL);
+
   row++;
 
   if(row>temp_row) temp_row=row;
diff --git a/rx_menu.c b/rx_menu.c
index 54254f5..f1446e5 100644
--- a/rx_menu.c
+++ b/rx_menu.c
@@ -385,7 +385,7 @@ void rx_menu(GtkWidget *parent) {
       }
     }
 
-    gtk_grid_attach(GTK_GRID(grid),output,x,++row,1,1);
+    my_combo_attach(GTK_GRID(grid),output,x,++row,1,1);
     g_signal_connect(output,"changed",G_CALLBACK(local_output_changed_cb),NULL);
 
     row=0;
diff --git a/sliders.c b/sliders.c
index 91a82c0..5b937a6 100644
--- a/sliders.c
+++ b/sliders.c
@@ -864,6 +864,9 @@ fprintf(stderr,"sliders_init: width=%d height=%d\n", width,height);
   gtk_widget_override_font(c25_att_preamp_label, pango_font_description_from_string(SLIDERS_FONT));
   gtk_grid_attach(GTK_GRID(sliders), c25_att_preamp_label, 6, 0, 1, 1);
 
+  //
+  // Do not use my_combo_attach here because these widgets can be hidden/shown
+  //
   c25_att_combobox = gtk_combo_box_text_new();
   gtk_widget_override_font(c25_att_combobox, pango_font_description_from_string(SLIDERS_FONT));
   gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(c25_att_combobox), "0", "0 dB");
diff --git a/tx_menu.c b/tx_menu.c
index acd75b0..f7a7224 100644
--- a/tx_menu.c
+++ b/tx_menu.c
@@ -298,7 +298,7 @@ void tx_menu(GtkWidget *parent) {
       strcpy(transmitter->microphone_name,input_devices[0].name);
     }
 
-    gtk_grid_attach(GTK_GRID(grid),input,col,row,3,1);
+    my_combo_attach(GTK_GRID(grid),input,col,row,3,1);
     g_signal_connect(input,"changed",G_CALLBACK(local_input_changed_cb),NULL);
   }
 
@@ -508,7 +508,7 @@ void tx_menu(GtkWidget *parent) {
     gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(ctcss_frequency_b),NULL,temp);
   }
   gtk_combo_box_set_active(GTK_COMBO_BOX(ctcss_frequency_b),transmitter->ctcss);
-  gtk_grid_attach(GTK_GRID(grid),ctcss_frequency_b,col,row,1,1);
+  my_combo_attach(GTK_GRID(grid),ctcss_frequency_b,col,row,1,1);
   g_signal_connect(ctcss_frequency_b,"changed",G_CALLBACK(ctcss_frequency_cb),NULL);
 
 /*
diff --git a/vfo_menu.c b/vfo_menu.c
index 28a1fda..b246cf3 100644
--- a/vfo_menu.c
+++ b/vfo_menu.c
@@ -278,7 +278,7 @@ void vfo_menu(GtkWidget *parent,int vfo) {
       break;
   }
   g_signal_connect(rit_b,"changed",G_CALLBACK(rit_cb),NULL);
-  gtk_grid_attach(GTK_GRID(grid),rit_b,4,2,1,1);
+  my_combo_attach(GTK_GRID(grid),rit_b,4,2,1,1);
 
   GtkWidget *vfo_label=gtk_label_new("VFO step: ");
   gtk_grid_attach(GTK_GRID(grid),vfo_label,3,3,1,1);
@@ -292,7 +292,7 @@ void vfo_menu(GtkWidget *parent,int vfo) {
     }
   }
   g_signal_connect(vfo_b,"changed",G_CALLBACK(vfo_cb),NULL);
-  gtk_grid_attach(GTK_GRID(grid),vfo_b,4,3,1,1);
+  my_combo_attach(GTK_GRID(grid),vfo_b,4,3,1,1);
 
 
   if (!display_sliders) {
-- 
2.45.2