::주인장  ::리눅스  ::프로그래밍  ::자유게시판 
::로그인     ::회원가입

 프로그래밍
::C,C++   ::소스   ::만든 프로그램  
GTK Listview 구현
GTK Listview 구현
시티에이스   좋아! 0   별로.. 0   2021-01-09 11:33:33
출처 : http://zetcode.com/gui/gtk2/gtktreeview/

#include <gtk/gtk.h>

enum {

LIST_ITEM = 0,
N_COLUMNS
};

void init_list(GtkWidget *list) {

GtkCellRenderer *renderer;
GtkTreeViewColumn *column;
GtkListStore *store;

renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes("List Items",
renderer, "text", LIST_ITEM, NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);

store = gtk_list_store_new(N_COLUMNS, G_TYPE_STRING);

gtk_tree_view_set_model(GTK_TREE_VIEW(list),
GTK_TREE_MODEL(store));

g_object_unref(store);
}

void add_to_list(GtkWidget *list, const gchar *str) {

GtkListStore *store;
GtkTreeIter iter;

store = GTK_LIST_STORE(gtk_tree_view_get_model
(GTK_TREE_VIEW(list)));

gtk_list_store_append(store, &iter);
gtk_list_store_set(store, &iter, LIST_ITEM, str, -1);
}

void on_changed(GtkWidget *widget, gpointer label) {

GtkTreeIter iter;
GtkTreeModel *model;
gchar *value;

if (gtk_tree_selection_get_selected(
GTK_TREE_SELECTION(widget), &model, &iter)) {

gtk_tree_model_get(model, &iter, LIST_ITEM, &value, -1);
gtk_label_set_text(GTK_LABEL(label), value);
g_free(value);
}
}

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

GtkWidget *window;
GtkWidget *list;

GtkWidget *vbox;
GtkWidget *label;
GtkTreeSelection *selection;

gtk_init(&argc, &argv);

window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
list = gtk_tree_view_new();

gtk_window_set_title(GTK_WINDOW(window), "List view");
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
gtk_window_set_default_size(GTK_WINDOW(window), 270, 250);

gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(list), FALSE);

vbox = gtk_vbox_new(FALSE, 0);

gtk_box_pack_start(GTK_BOX(vbox), list, TRUE, TRUE, 5);

label = gtk_label_new("");
gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);

gtk_container_add(GTK_CONTAINER(window), vbox);

init_list(list);
add_to_list(list, "Aliens");
add_to_list(list, "Leon");
add_to_list(list, "The Verdict");
add_to_list(list, "North Face");
add_to_list(list, "Der Untergang");

selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));

g_signal_connect(selection, "changed",
G_CALLBACK(on_changed), label);

g_signal_connect(G_OBJECT (window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);

gtk_widget_show_all(window);

gtk_main();

return 0;
}


  좋아!   별로..
 GTK Treeview
 GTK API 준비중인 자료