I have developed a financial calculator in c - Black & Scholes. My teacher wants me to implement a GUI to this and he suggested GTK. Due to my poor knowledge in c (java programmer from the start) this is almost too much for me at the moment because the deadline is in 4 hours. But after hard work and good help here on stackoverflow I think I have a chanche to make it!. But right now there is one irritating obstacle to overide -
That is to pass more than one entry to a method. In short - there are five inputfields and what I want is to send all these via g_signal_connect_swapped to a method where the invoked string i extracted. The thing is that I am able to do this (thanks to help) as long as there are only one string (one entry). But how do I send all these entrys to the function?
I have tried to declare a vector without success. It wont even compile due to errors.
static GtkWidget entry[5]
Since an array is a pointer (to the first element) it should work - but not!
And more strange - I had another idea - instead of sending the entrys I tried to extract the string value in the main-method - exaxtly the same syntax as in the function - but without succes. The value is null.
const gchar *text;
text = gtk_entry_get_text(GTK_ENTRY (entry_a));
printf ("Result: %s
", text);
the above snippet works as expexted in the function - but not in the main-function. Why???
Hope you understand my question - here follows the code that generates the GUI. The code is without error - that is where I send ONE entry-value to the callback-function.
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
static GtkWidget *asset_label;
static GtkWidget *frame;
static GtkWidget *entry_a, *entry_s, *entry_v, *entry_t, *entry_r;
static GtkWidget *label_a, *label_s, *label_v, *label_t, *label_r;
static GtkWidget *window, *result_label, *button;
static GtkWidget *table;
static void entry_Submit(GtkWidget *entry, GtkWidget *widget) {
const gchar *text;
text = gtk_entry_get_text(GTK_ENTRY (entry_a));
printf ("Result: %s
", text);
gtk_widget_destroy(GTK_WIDGET(label_a));
label_a = gtk_label_new (text);
gtk_grid_attach (GTK_GRID (table), label_a, 1, 0, 1, 1);
gtk_widget_show(label_a);
}
static void destroy(GtkWidget *widget, gpointer data) {
gtk_main_quit ();
}
static void initialize_window(GtkWidget* window) {
gtk_window_set_title(GTK_WINDOW(window),"My Window"); //Set window title
gtk_window_set_default_size (GTK_WINDOW (window), 400, 200); //Set default size for the window
g_signal_connect (window, "destroy", G_CALLBACK (destroy), NULL); //End application when close button clicked
}
int main (int argc, char *argv[]) {
//GtkWidget *window,*table,*label, *button;
gtk_init(&argc, &argv);
//Create the main window
//window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
//initialize_window(window);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_widget_set_size_request (GTK_WIDGET (window), 300, 300);
gtk_window_set_title (GTK_WINDOW (window), "FINANCIAL CALCULATOR");
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
g_signal_connect_swapped (window, "delete-event", G_CALLBACK (gtk_widget_destroy), window);
/* Create a 1x2 table */
table = gtk_grid_new ();
gtk_container_add (GTK_CONTAINER (window), table);
//create a text box (asset price)
entry_a = gtk_entry_new ();
gtk_grid_attach (GTK_GRID (table), entry_a, 0, 0, 1, 1);
// create a new label.
label_a = gtk_label_new (" ASSET PRICE" );
gtk_grid_attach (GTK_GRID (table), label_a, 1, 0, 1, 1);
//create a text box (strike price)
entry_s = gtk_entry_new ();
gtk_grid_attach (GTK_GRID (table), entry_s, 0, 1, 1, 1);
// create a new label.
label_s = gtk_label_new (" STRIKE PRICE" );
gtk_grid_attach (GTK_GRID (table), label_s, 1, 1, 1, 1);
//create a text box (time to maturity)
entry_t = gtk_entry_new ();
gtk_grid_attach (GTK_GRID (table), entry_t, 0, 2, 1, 1);
// create a new label.
label_t = gtk_label_new (" TIME TO MATURITY" );
gtk_grid_attach (GTK_GRID (table), label_t, 1, 2, 1, 1);
//create a text box (volatility)
entry_v = gtk_entry_new ();
gtk_grid_attach (GTK_GRID (table), entry_v, 0, 3, 1, 1);
// create a new label.
label_v = gtk_label_new (" VOLATILITY" );
gtk_grid_attach (GTK_GRID (table), label_v, 1, 3, 1, 1);
//create a text box (interest rate)
entry_r = gtk_entry_new ();
gtk_grid_attach (GTK_GRID (table), entry_r, 0, 4, 1, 1);
// create a new label.
label_r = gtk_label_new (" INTEREST RATE" );
gtk_grid_attach (GTK_GRID (table), label_r, 1, 4, 1, 1);
button = gtk_button_new_with_label("Calculate");
g_signal_connect_swapped (button, "clicked", G_CALLBACK (entry_Submit), entry_a);
gtk_grid_attach (GTK_GRID (table), button, 0, 5, 2, 1);
gtk_widget_show_all(window);
gtk_main ();
return 0;
}
these are the errors when I declare an array of GtkWidget
1) In the entry_submit callback-function () where I declare the following:
text = gtk_entry_get_text(GTK_ENTRY (entry[0]));
compile error: error: subscripted value is neither array nor pointer nor vector
2) In the main function where I declare the following:
entry[0] = gtk_entry_new ();
error: incompatible types when assigning to type 'GtkWidget' from type 'struct GtkWidget *'
See Question&Answers more detail:
os