lignin
Junior Member level 2
Hello,
I am trying to create new screen in gtk+.
So here is my code :
only case 0 is available for now.
when I am clicking the row in the screen, new screen is not created. I dont know why. So can you help me? thanks for your answers.
I am trying to create new screen in gtk+.
So here is my code :
Code:
#include<stdio.h>
#include<string.h>
#include<gtk/gtk.h>
#define COMPLETED 1
#define NOT_COMPLETED 0
#define WIDTH 240
#define HEIGHT 320
enum{
BUY_IT = 0,
QUANTITY,
PRODUCT,
COLUMNS
};
typedef struct{
gboolean buy;
gint quantity;
gchar *product;
}GroceryItem;
const GroceryItem list[] =
{
{TRUE,1,"Card"},
// {TRUE,2,"Camera"},
// {FALSE,1,"Validator"},
// {FALSE,3,"Recharger"},
{FALSE,0,NULL},
};
unsigned int firstEnter = NOT_COMPLETED;
GtkWindow *window = NULL;
GtkFixed *appbody = NULL;
GtkFixed *appview = NULL;
GtkFixed *cardinfo = NULL;
GtkFixed *camerainfo = NULL;
GtkFixed *validatorinfo = NULL;
GtkFixed *rechargerinfo = NULL;
GtkFixed *errorinfo = NULL;
static void setup_tree_view(GtkWidget*);
void constructCardInfoScreen(void);
//void constructCameraInfoScreen(void);
//void constructValidatorInfoScreen(void);
//void constructRechargerInfoScreen(void);
//void constructErrorInfoScreen(void);
void constructApplicationWindows(void);
void showScreen(GtkFixed*);
void on_changed(GtkWidget *widget , gpointer data){
unsigned int i = 0;
GtkTreeIter iter;
GtkTreeModel *model;
char *value;
if(gtk_tree_selection_get_selected(GTK_TREE_SELECTION(widget),&model,&iter) && (firstEnter != 0)){
gtk_tree_model_get(model,&iter,PRODUCT,&value,-1);
while(strcmp(value,list[i].product) != 0){
i++;
}
printf("%s is selected and i = %d \n",value,i);
switch(i){
case 0 :
printf("card info screen is selected ! \n");
showScreen(cardinfo);
break;
case 1 :
printf("%s is selected \n",value);
break;
case 2 :
printf("%s is selected \n",value);
break;
case 3 :
printf("%s is selected \n",value);
break;
default :
printf("ERR \n");
break;
}
g_free(value);
}
firstEnter = COMPLETED;
}
void back_to_main_window(GtkWidget *widget , gpointer data){
showScreen(appview);
}
int main(int argc , char *argv[]){
gint i = 0;
GtkWidget *treeview , *scrolled_win;
GtkListStore *store;
GtkTreeIter iter;
GtkTreeSelection *selection;
gtk_init(&argc,&argv);
window = GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL));
gtk_window_set_position(window , GTK_WIN_POS_CENTER);
gtk_window_set_title(window ,"Window");
gtk_widget_set_size_request(GTK_WIDGET(window),WIDTH,HEIGHT);
appview = GTK_FIXED(gtk_fixed_new());
gtk_widget_set_size_request(GTK_WIDGET(appview),WIDTH,HEIGHT);
treeview = gtk_tree_view_new(); /* creating tree view */
setup_tree_view(treeview); /* add three columns to the treeview and show columns as text.*/ /* columns and renderer adjustment */
store = gtk_list_store_new(COLUMNS,G_TYPE_BOOLEAN,G_TYPE_INT,G_TYPE_STRING); /* tree view columns are set up with desired renderers. so its time to create the tree model that will interface between renderers and the tree view. */ /* we create gtk_list_store */
while(list[i].product != NULL){
gtk_list_store_append(store,&iter); /* list store 'u olusturduk. simdi row'u olusturmam\u0131z gerekiyor. row olu\u015fturmay\u0131 gtk_list_store_append ile olusturuyoruz. */
gtk_list_store_set(store,&iter,BUY_IT,list[i].buy,QUANTITY,list[i].quantity,PRODUCT,list[i].product,-1); /* row'lar\u0131 olusturduktan sonra bu rowlara datalar\u0131 eklememiz gerekmektedir. gtk_list_store_set ile birlikte bu olusturdu\u011fumuz rowlara datalar\u0131 ekliyoruz.*/
i++;
}
gtk_tree_view_set_model(GTK_TREE_VIEW(treeview),GTK_TREE_MODEL(store));
g_object_unref(store);
scrolled_win = gtk_scrolled_window_new(NULL,NULL);
gtk_widget_set_size_request(GTK_WIDGET(scrolled_win),WIDTH,HEIGHT);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_win),GTK_POLICY_AUTOMATIC,GTK_POLICY_ALWAYS);
gtk_container_add(GTK_CONTAINER(scrolled_win),GTK_WIDGET(treeview));
selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
g_signal_connect(selection,"changed",G_CALLBACK(on_changed),NULL);
gtk_fixed_put(appview,GTK_WIDGET(scrolled_win),0,0);
constructApplicationWindows();
gtk_container_add(GTK_CONTAINER(window),GTK_WIDGET(appview));
gtk_widget_show_all(GTK_WIDGET(window));
gtk_main();
return 0;
}
static void setup_tree_view(GtkWidget *treeview){
GtkCellRenderer *renderer;
GtkTreeViewColumn *column;
renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes("Buy",renderer,"text",BUY_IT,NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(treeview),column);
renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes("Count",renderer,"text",QUANTITY,NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(treeview),column);
renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes("Product",renderer,"text",PRODUCT,NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(treeview),column);
}
void constructCardInfoScreen(){
cardinfo = GTK_FIXED(gtk_fixed_new());
gtk_widget_set_size_request(GTK_WIDGET(cardinfo),WIDTH,HEIGHT);
GtkButton *button = (GtkButton *)gtk_button_new_with_label("Cancel");
gtk_widget_set_size_request(GTK_WIDGET(button),60,60);
gtk_fixed_put(cardinfo , GTK_WIDGET(button),30,40);
g_signal_connect(button,"clicked",G_CALLBACK(back_to_main_window),NULL);
}
void showScreen(GtkFixed *screen){
if(appbody != NULL){
gtk_widget_hide_all(GTK_WIDGET(appbody));
gtk_widget_set_sensitive(GTK_WIDGET(screen),TRUE);
}
printf("show screen event \n");
appbody = screen;
gtk_widget_show_all(GTK_WIDGET(appbody));
}
void constructApplicationWindows(){
constructCardInfoScreen();
// constructCameraInfoScreen();
// constructValidatorInfoScreen();
// constructRechargerInfoScreen();
// constructErrorInfoScreen();
}
only case 0 is available for now.
when I am clicking the row in the screen, new screen is not created. I dont know why. So can you help me? thanks for your answers.