ui/gtk/tree.c

changeset 40
caa0df8ed095
parent 33
458831c574f4
child 41
394f3b06dba1
equal deleted inserted replaced
39:4e66271541e8 40:caa0df8ed095
58 gtk_tree_view_set_activate_on_single_click(GTK_TREE_VIEW(view), TRUE); 58 gtk_tree_view_set_activate_on_single_click(GTK_TREE_VIEW(view), TRUE);
59 #else 59 #else
60 60
61 #endif 61 #endif
62 62
63 UiListModel *model = ui_list_model_new(list, getvalue); 63 UiModelInfo *modelinfo = ui_model_info(obj->ctx, UI_STRING, "", -1);
64 modelinfo->getvalue = getvalue;
65 UiListModel *model = ui_list_model_new(list, modelinfo);
64 gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(model)); 66 gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(model));
65 67
66 // add TreeView as observer to the UiList to update the TreeView if the 68 // add TreeView as observer to the UiList to update the TreeView if the
67 // data changes 69 // data changes
68 UiListView *listview = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiListView)); 70 UiTableView *listview = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiTableView));
69 listview->widget = view; 71 listview->widget = view;
70 listview->list = list; 72 listview->list = list;
71 listview->getvalue = getvalue; 73 listview->modelinfo = modelinfo;
72 list->list->observers = ui_add_observer( 74 list->list->observers = ui_add_observer(
73 list->list->observers, 75 list->list->observers,
74 (ui_callback)ui_listview_update, 76 (ui_callback)ui_listview_update,
75 listview); 77 listview);
76 78
121 } 123 }
122 return NULL; 124 return NULL;
123 } 125 }
124 126
125 127
126 void ui_listview_update(UiEvent *event, UiListView *view) { 128 UIWIDGET ui_table_var(UiObject *obj, UiListPtr *list, UiModelInfo *modelinfo) {
129 // create treeview
130 GtkWidget *view = gtk_tree_view_new();
131 GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
132 for(int i=0;i<modelinfo->columns;i++) {
133 GtkTreeViewColumn *column = gtk_tree_view_column_new_with_attributes(
134 modelinfo->titles[i],
135 renderer,
136 "text",
137 0,
138 NULL);
139 gtk_tree_view_column_set_resizable(column, TRUE);
140 gtk_tree_view_append_column(GTK_TREE_VIEW(view), column);
141 }
142
143 //gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), FALSE);
144 #ifdef UI_GTK3
145 gtk_tree_view_set_activate_on_single_click(GTK_TREE_VIEW(view), TRUE);
146 #else
147
148 #endif
149
150 UiListModel *model = ui_list_model_new(list, modelinfo);
151 gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(model));
152
153 // add TreeView as observer to the UiList to update the TreeView if the
154 // data changes
155 UiTableView *tableview = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiTableView));
156 tableview->widget = view;
157 tableview->list = list;
158 tableview->modelinfo = modelinfo;
159 list->list->observers = ui_add_observer(
160 list->list->observers,
161 (ui_callback)ui_listview_update,
162 tableview);
163
164 // add callback
165 if(modelinfo->callback) {
166 UiEventData *event = ucx_mempool_malloc(
167 obj->ctx->mempool,
168 sizeof(UiEventData));
169 event->obj = obj;
170 event->user_data = modelinfo->userdata;
171 event->callback = modelinfo->callback;
172 event->value = 0;
173
174 g_signal_connect(
175 view,
176 "row-activated",
177 G_CALLBACK(ui_listview_selected),
178 event);
179 }
180
181 // add widget to the current container
182 GtkWidget *scroll_area = gtk_scrolled_window_new(NULL, NULL);
183 gtk_scrolled_window_set_policy(
184 GTK_SCROLLED_WINDOW(scroll_area),
185 GTK_POLICY_AUTOMATIC,
186 GTK_POLICY_AUTOMATIC); // GTK_POLICY_ALWAYS
187 gtk_container_add(GTK_CONTAINER(scroll_area), view);
188
189 UiContainer *ct = uic_get_current_container(obj);
190 ct->add(ct, scroll_area);
191
192 return scroll_area;
193 }
194
195 UIWIDGET ui_table(UiObject *obj, UiList *list, UiModelInfo *modelinfo) {
196 UiListPtr *listptr = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiListPtr));
197 listptr->list = list;
198 return ui_table_var(obj, listptr, modelinfo);
199 }
200
201 UIWIDGET ui_table_nv(UiObject *obj, char *varname, UiModelInfo *modelinfo) {
202 UiVar *var = uic_connect_var(obj->ctx, varname, UI_VAR_LIST);
203 if(var) {
204 UiListVar *value = var->value;
205 return ui_table_var(obj, value->listptr, modelinfo);
206 } else {
207 // TODO: error
208 }
209 return NULL;
210 }
211
212
213
214 void ui_listview_update(UiEvent *event, UiTableView *view) {
127 UiList *list = view->list->list; 215 UiList *list = view->list->list;
128 216 UiListModel *model = ui_list_model_new(view->list, view->modelinfo);
129 UiListModel *model = ui_list_model_new(view->list, view->getvalue);
130 gtk_tree_view_set_model(GTK_TREE_VIEW(view->widget), GTK_TREE_MODEL(model)); 217 gtk_tree_view_set_model(GTK_TREE_VIEW(view->widget), GTK_TREE_MODEL(model));
218
219 // TODO: free old model
131 } 220 }
132 221
133 void ui_listview_selected( 222 void ui_listview_selected(
134 GtkTreeView *treeview, 223 GtkTreeView *treeview,
135 GtkTreePath *path, 224 GtkTreePath *path,

mercurial