1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
#include <gtk/gtk.h>
#include <stdlib.h>
#define TITLE "GtkComboBox..."
#define WIDTH 300
#define HEIGHT 200
typedef struct _WindowData WindowData;
struct _WindowData
{
GtkWidget *window;
GtkWidget *in_item;
GtkWidget *cb_item;
};
gchar *dialog_get_string (gchar *message_string, WindowData *data);
void on_add_item_clicked (GtkWidget *widget, WindowData *data);
void on_edit_item_clicked (GtkWidget *widget, WindowData *data);
int main (int argc, char **argv)
{
GtkWidget *vbox;
GtkWidget *hbox_frm_in;
GtkWidget *vbox_in, *hbox_lbl, *hbox_in, *hbox_op;
GtkWidget *frm_in;
GtkWidget *lbl_item, *lbl_item_in;
GtkWidget *btn_add, *btn_edit, *btn_remove;
GtkTreeIter iter;
GtkListStore *store;
GtkCellRenderer *cell;
_WindowData win;
gtk_init (&argc, &argv);
win.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW(win.window), TITLE);
gtk_window_set_default_size (GTK_WINDOW(win.window), WIDTH, HEIGHT);
//gtk_window_set_resizable(GTK_WINDOW(win.window), FALSE);
gtk_window_set_position (GTK_WINDOW(win.window), GTK_WIN_POS_CENTER);
g_signal_connect (G_OBJECT(win.window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
vbox = gtk_vbox_new (FALSE, 0); /* Create a vbox to contain all the GUI */
gtk_container_add (GTK_CONTAINER(win.window), vbox); /* Adds vbox to container */
hbox_frm_in = gtk_hbox_new (FALSE, 0); /* Create a vbox to attach the frame */
gtk_box_pack_start (GTK_BOX(vbox), hbox_frm_in, FALSE, FALSE, 0);
frm_in = gtk_frame_new ("Input Data...");
gtk_box_pack_start (GTK_BOX(hbox_frm_in), frm_in, TRUE, TRUE, 5);
vbox_in = gtk_vbox_new (FALSE, 0);
gtk_container_add (GTK_CONTAINER(frm_in), vbox_in);
hbox_lbl = gtk_hbox_new (TRUE, 0);
gtk_box_pack_start (GTK_BOX(vbox_in), hbox_lbl, FALSE, FALSE, 0);
lbl_item = gtk_label_new ("Select an Item");
gtk_box_pack_start (GTK_BOX(hbox_lbl), lbl_item, TRUE, FALSE, 0);
lbl_item_in = gtk_label_new ("Input a new item");
gtk_box_pack_start (GTK_BOX(hbox_lbl), lbl_item_in, TRUE, FALSE, 0);
hbox_in = gtk_hbox_new (TRUE, 0);
gtk_box_pack_start (GTK_BOX(vbox_in), hbox_in, FALSE, FALSE, 0);
/**
* Create a GtkListStore to store items
* The GtkListSore consists in one column
* and one text field
*/
store = gtk_list_store_new (1, G_TYPE_STRING);
/**
* gtk_list_store_set (GtkListStore *list_store, GtkTreeIter *iter, ...);
* For example, to set column 0 with type G_TYPE_STRING to "Foo",
* you would write gtk_list_store_set (store, iter, 0, "Foo", -1).
*/
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, 0, "Linux", -1);
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, 0, "Mac OS X", -1);
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, 0, "Windows 7", -1);
win.cb_item = gtk_combo_box_new_with_model (GTK_TREE_MODEL(store));
gtk_box_pack_start (GTK_BOX(hbox_in), win.cb_item, TRUE, FALSE, 0);
/* Remove our reference from store to avoid memory leak. */
g_object_unref (G_OBJECT(store));
cell = gtk_cell_renderer_text_new (); /* Create a cell renderer */
gtk_cell_layout_pack_start (GTK_CELL_LAYOUT(win.cb_item), cell, TRUE); /* Pack it into the combo box */
gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT(win.cb_item), cell, "text", 0, NULL);
gtk_combo_box_set_active (GTK_COMBO_BOX(win.cb_item), 0);
win.in_item = gtk_entry_new ();
gtk_entry_set_max_length (GTK_ENTRY(win.in_item), 6);
gtk_box_pack_start (GTK_BOX(hbox_in), win.in_item, TRUE, FALSE, 0);
hbox_op = gtk_hbox_new (TRUE, 0);
gtk_box_pack_start (GTK_BOX(vbox_in), hbox_op, FALSE, FALSE, 0);
btn_add = gtk_button_new_with_label ("Add item");
gtk_box_pack_start (GTK_BOX(hbox_op), btn_add, TRUE, TRUE, 0);
g_signal_connect (G_OBJECT(btn_add), "clicked", G_CALLBACK(on_add_item_clicked), &win);
btn_edit = gtk_button_new_with_label ("Edit item");
gtk_box_pack_start (GTK_BOX(hbox_op), btn_edit, TRUE, TRUE, 0);
g_signal_connect (G_OBJECT(btn_edit), "clicked", G_CALLBACK(on_edit_item_clicked), &win);
btn_remove = gtk_button_new_with_label ("Remove item");
gtk_box_pack_start (GTK_BOX(hbox_op), btn_remove, TRUE, TRUE, 0);
gtk_widget_show_all (win.window);
gtk_main ();
return EXIT_SUCCESS;
}
gchar *dialog_get_string (gchar *message_string, WindowData *data)
{
GtkWidget *dialog;
GtkWidget *in_str;
gchar *string = NULL;
dialog = gtk_dialog_new_with_buttons (message_string,
GTK_WINDOW(data->window),
GTK_DIALOG_MODAL,
GTK_STOCK_OK,
GTK_RESPONSE_OK,
GTK_STOCK_CANCEL,
GTK_RESPONSE_CANCEL,
NULL);
in_str = gtk_entry_new ();
gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), in_str, TRUE, FALSE, 0);
gtk_widget_show_all (GTK_DIALOG(dialog)->vbox);
switch (gtk_dialog_run(GTK_DIALOG(dialog)))
{
case GTK_RESPONSE_OK:
string = (gchar *) gtk_entry_get_text(GTK_ENTRY(in_str));
break;
case GTK_RESPONSE_CANCEL:
case GTK_RESPONSE_NONE:
default:
break;
}
gtk_widget_destroy (dialog);
return string;
}
void on_add_item_clicked (GtkWidget *widget, WindowData *data)
{
GtkTreeIter iter;
GtkListStore *store;
gchar *string = NULL;
string = (gchar *) gtk_entry_get_text (GTK_ENTRY(data->in_item));
store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX(data->cb_item)));
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, 0, string, -1);
g_print ("\nItem %s added...", string);
gtk_editable_delete_text (GTK_EDITABLE(data->in_item), 0, -1);
//Don't work in windows7
//if (string) /* Free string (if not NULL) */
// g_free (string);
}
void on_edit_item_clicked (GtkWidget *widget, WindowData *data)
{
GtkTreeIter iter;
GtkListStore *store;
gchar *string = NULL;
if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX(data->cb_item), &iter))
{
//store = GTK_LIST_STORE(gtk_combo_box_get_model(GTK_COMBO_BOX(data->cb_item)));
string = dialog_get_string ("Input the new item!", data);
gtk_entry_set_text (GTK_ENTRY(data->in_item), string);
g_print ("\nNew item: %s", string);
}
} |
Partager