// gcc  -Wall -Wextra -o combo combo.c $(pkg-config gtk+-3.0 --cflags --libs)
#include <stdlib.h>
#include <gtk/gtk.h>


GtkListStore *theListStore = NULL;
GtkListStore *comboBoxStore = NULL;


void on_quitButton_clicked(__attribute__((unused)) GtkWidget *widget, 
			   __attribute__((unused)) gpointer   data)
{
  g_print("%s called\n",__FUNCTION__);
    gtk_main_quit();
}
void on_testButton_clicked(__attribute__((unused)) GtkWidget *widget, 
			   __attribute__((unused)) gpointer   data)
{
  g_print("%s called\n",__FUNCTION__);
  
}

/*
void
user_function (GtkCellRendererCombo *combo,
               gchar                *path_string,
               GtkTreeIter          *new_iter,
               gpointer              user_data)
 */


void on_theComboRenderer_changed(__attribute__((unused)) GtkCellRendererCombo *combo,
				 gchar                *path_string,
				 GtkTreeIter          *new_iter,
				 __attribute__((unused)) gpointer              user_data)
{
  double value;
  gchar *text;
  
  GtkTreeIter listiter;
  
  g_print("%s called\n",__FUNCTION__);

  g_print("path_string = %s\n",path_string);

  gtk_tree_model_get( GTK_TREE_MODEL(comboBoxStore) ,new_iter,0,&text,2,&value,-1);
  g_print("value=%g text=%s\n",value,text);


  gtk_tree_model_get_iter_from_string (GTK_TREE_MODEL(theListStore),&listiter,path_string);

  gtk_list_store_set( theListStore,&listiter,3,value,4,text,-1);
  

  
}

/*
void
user_function (GtkCellRendererText *renderer,
               gchar               *path,
               gchar               *new_text,
               gpointer             user_data)

 */

void on_theComboRenderer_edited(__attribute__((unused)) GtkCellRendererText *renderer,
				gchar               *path,
				gchar               *new_text,
				__attribute__((unused)) gpointer             user_data)
{
  double value;
  GtkTreeIter listiter;
  
  g_print("%s called\n",__FUNCTION__);

  gtk_tree_model_get_iter_from_string (GTK_TREE_MODEL(theListStore),&listiter,path);

  sscanf(new_text,"%lg",&value);
  g_print("value=%lg text=%s\n",value,new_text);

  gtk_list_store_set( theListStore,&listiter,3,value,4,new_text,-1);

  
}

void on_theComboRenderer_editing_canceled(__attribute__((unused)) GtkWidget *widget, 
					  __attribute__((unused)) gpointer   data)
{
  g_print("%s called\n",__FUNCTION__);
}
void on_theComboRenderer_editing_started(__attribute__((unused)) GtkWidget *widget, 
					 __attribute__((unused)) gpointer   data)
{
  g_print("%s called\n",__FUNCTION__);
}

// Handle the user trying to close the window 
gboolean windowDelete(__attribute__((unused)) GtkWidget *widget, 
		      __attribute__((unused)) GdkEvent  *event,
		      __attribute__((unused)) gpointer   data)
{
    g_print("%s called.\n",__FUNCTION__);
    return TRUE;    // Returning TRUE stops the window being deleted.
                    // Returning FALSE allows deletion.   
}

int main ( int argc, char **argv) {

    GtkWidget *window;
    GtkBuilder *builder = NULL;
    GError *error = NULL;

    gtk_init (&argc , &argv);

    builder = gtk_builder_new();

    if( gtk_builder_add_from_file (builder,"tvtest.glade" , &error) == 0)
    {
        g_print("gtk_builder_add_from_file FAILED\n");
	g_print("%s\n",error->message);
	return EXIT_FAILURE;
    }

    window  = GTK_WIDGET (gtk_builder_get_object (builder,"theWindow"));
    theListStore = GTK_LIST_STORE (gtk_builder_get_object (builder,"theListStore"));
    comboBoxStore = GTK_LIST_STORE (gtk_builder_get_object (builder,"comboBoxStore"));
    
    gtk_builder_connect_signals(builder,NULL);

    gtk_widget_show_all (window);
    gtk_main ();
    return EXIT_SUCCESS;
}
