Demo of using
multiple gtk combo boxes
Using glade I made a window with multiple combo boxes
What I’m doing is I’m storing day, month or year depending
upon what is selected in the combo box which is right above the show button, into
a text file.
If you are not able to make window, see my window hierarchy
in below image.
I’m not putting validation if nothing is selected or if
texts boxes are empty this is the rough code I’ve written you can make changes
according to your comfort.
#include <stdlib.h>
#include <gtk/gtk.h>
#include <string.h>
//a structure for the widgets of the window.
struct settings_window
{
GtkEntry *e3;
GtkComboBox *cb1;
GtkEntry *e4;
GtkComboBox *cb2;
GtkComboBox *cb3;
GtkEntry *e5;
GtkComboBox *cb4;
};
typedef struct settings_window Widgets6;
G_MODULE_EXPORT void on_save_button_clicked ( GtkButton
*button,Widgets6 *widg6)
{
GtkWidget
*dialog=NULL;
int i,j,k;
FILE *fp;
/*getting text from text boxes and combo boxes*/
const gchar
*text1 = gtk_entry_get_text( widg6->e3 );
const gchar
*text2 = gtk_combo_box_get_active_text( widg6->cb1 );
const gchar
*text3 = gtk_entry_get_text( widg6->e4 );
const gchar
*text4 = gtk_combo_box_get_active_text( widg6->cb2 );
const gchar
*text5 = gtk_combo_box_get_active_text( widg6->cb3 );
const gchar
*text6 = gtk_entry_get_text( widg6->e5 );
const gchar
*text7 = gtk_combo_box_get_active_text( widg6->cb4 );
//opening file you know why.. J
fp=fopen("save.txt","w");
if(fp==NULL)
{
puts("Cannot
open file.\n");
exit(1);
}
//checking which option is selected in the combo box which
is above the show button
i=strcmp(text7,"Day");
j=strcmp(text7,"Month");
k=strcmp(text7,"Year");
//according to selected option putting the text into the
file see below if conditions.
if(i==0)
{
fputs(text4,fp);
fputc('-',fp);
fputs(text5,fp);
fputc('-',fp);
fputs(text6,fp);
fclose(fp);
puts("hello");
}
else if(j==0)
{
fputs(text2,fp);
fputc('-',fp);
fputs(text3,fp);
fclose(fp);
}
else if(k==0)
{
fputs(text1,fp);
fclose(fp);
}
else
{
dialog =
gtk_message_dialog_new (GTK_WINDOW (dialog), GTK_DIALOG_MODAL,
GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, "not selected any mode please select
one");
gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
gtk_dialog_run
(GTK_DIALOG (dialog));
gtk_widget_destroy
(dialog);
}
}
int main(int argc, char *argv[])
{
GtkBuilder
*gtkBuilder;
GtkWidget *win;
Widgets6 widg6;
gtk_init(&argc, &argv);
gtkBuilder =
gtk_builder_new();
gtk_builder_add_from_file(gtkBuilder, "combobox.glade", NULL);
/ *I have used widge6 which is a variable of our structure
type and connecting each widget to its appropriate structure member.*/
win = GTK_WIDGET(
gtk_builder_get_object( gtkBuilder, "window1") );
widg6.e3 =
GTK_ENTRY( gtk_builder_get_object( gtkBuilder, "year_entry" ) );
widg6.cb1 =
GTK_COMBO_BOX( gtk_builder_get_object( gtkBuilder, "combobox1" ) );
widg6.e4 =
GTK_ENTRY( gtk_builder_get_object( gtkBuilder, "month_year_entry" )
);
widg6.cb2 =
GTK_COMBO_BOX( gtk_builder_get_object( gtkBuilder, "combobox2" ) );
widg6.cb3 =
GTK_COMBO_BOX( gtk_builder_get_object( gtkBuilder, "combobox3" ) );
widg6.e5 =
GTK_ENTRY( gtk_builder_get_object( gtkBuilder,
"date_month_year_entry" ) );
widg6.cb4 =
GTK_COMBO_BOX( gtk_builder_get_object( gtkBuilder, "combobox4" ) );
gtk_builder_connect_signals( gtkBuilder,&widg6 );
g_object_unref(G_OBJECT(gtkBuilder));
gtk_widget_show(win);
gtk_main();
return 0;
}
No comments:
Post a Comment