Sun, 15 Feb 2015 15:44:24 +0100
added toolbar (WPF)
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 2014 Olaf Wintermann. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include "../common/context.h" #include "../common/object.h" #include "container.h" #include "tree.h" void* ui_strmodel_getvalue(void *elm, int column) { return column == 0 ? elm : NULL; } UIWIDGET ui_listview_str(UiObject *obj, UiList *list, ui_callback f, void *udata) { return ui_listview(obj, list, ui_strmodel_getvalue, f, udata); } UIWIDGET ui_listview_var(UiObject *obj, UiListPtr *list, ui_model_getvalue_f getvalue, ui_callback f, void *udata) { // create treeview GtkWidget *view = gtk_tree_view_new(); GtkCellRenderer *renderer = gtk_cell_renderer_text_new(); GtkTreeViewColumn *column = gtk_tree_view_column_new_with_attributes(NULL, renderer, "text", 0, NULL); gtk_tree_view_append_column(GTK_TREE_VIEW(view), column); gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), FALSE); #ifdef UI_GTK3 gtk_tree_view_set_activate_on_single_click(GTK_TREE_VIEW(view), TRUE); #else #endif UiModelInfo *modelinfo = ui_model_info(obj->ctx, UI_STRING, "", -1); modelinfo->getvalue = getvalue; UiListModel *model = ui_list_model_new(list, modelinfo); gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(model)); // add TreeView as observer to the UiList to update the TreeView if the // data changes UiTableView *listview = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiTableView)); listview->widget = view; listview->list = list; listview->modelinfo = modelinfo; list->list->observers = ui_add_observer( list->list->observers, (ui_callback)ui_listview_update, listview); // add callback if(f) { UiTreeEventData *event = ui_malloc(obj->ctx, sizeof(UiTreeEventData)); event->obj = obj; event->userdata = udata; event->activate = f; event->selection = NULL; g_signal_connect( view, "row-activated", G_CALLBACK(ui_listview_activate_event), event); } // add widget to the current container GtkWidget *scroll_area = gtk_scrolled_window_new(NULL, NULL); gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(scroll_area), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); // GTK_POLICY_ALWAYS gtk_container_add(GTK_CONTAINER(scroll_area), view); UiContainer *ct = uic_get_current_container(obj); ct->add(ct, scroll_area, TRUE); // ct->current should point to view, not scroll_area, to make it possible // to add a context menu ct->current = view; return scroll_area; } UIWIDGET ui_listview(UiObject *obj, UiList *list, ui_model_getvalue_f getvalue, ui_callback f, void *udata) { UiListPtr *listptr = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiListPtr)); listptr->list = list; return ui_listview_var(obj, listptr, getvalue, f, udata); } UIWIDGET ui_listview_nv(UiObject *obj, char *varname, ui_model_getvalue_f getvalue, ui_callback f, void *udata) { UiVar *var = uic_connect_var(obj->ctx, varname, UI_VAR_LIST); if(var) { UiListVar *value = var->value; return ui_listview_var(obj, value->listptr, getvalue, f, udata); } else { // TODO: error } return NULL; } UIWIDGET ui_table_var(UiObject *obj, UiListPtr *list, UiModelInfo *modelinfo) { // create treeview GtkWidget *view = gtk_tree_view_new(); for(int i=0;i<modelinfo->columns;i++) { GtkCellRenderer *renderer = gtk_cell_renderer_text_new(); GtkTreeViewColumn *column = gtk_tree_view_column_new_with_attributes( modelinfo->titles[i], renderer, "text", i, NULL); gtk_tree_view_column_set_resizable(column, TRUE); gtk_tree_view_append_column(GTK_TREE_VIEW(view), column); } //gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), FALSE); #ifdef UI_GTK3 //gtk_tree_view_set_activate_on_single_click(GTK_TREE_VIEW(view), TRUE); #else #endif UiListModel *model = ui_list_model_new(list, modelinfo); gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(model)); // add TreeView as observer to the UiList to update the TreeView if the // data changes UiTableView *tableview = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiTableView)); tableview->widget = view; tableview->list = list; tableview->modelinfo = modelinfo; list->list->observers = ui_add_observer( list->list->observers, (ui_callback)ui_listview_update, tableview); // add callback UiTreeEventData *event = ui_malloc(obj->ctx, sizeof(UiTreeEventData)); event->obj = obj; event->activate = modelinfo->activate; event->selection = modelinfo->selection; event->userdata = modelinfo->userdata; if(modelinfo->activate) { g_signal_connect( view, "row-activated", G_CALLBACK(ui_listview_activate_event), event); } if(modelinfo->selection) { GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(view)); g_signal_connect( selection, "changed", G_CALLBACK(ui_listview_selection_event), event); } // TODO: destroy callback GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(view)); gtk_tree_selection_set_mode(selection, GTK_SELECTION_MULTIPLE); // add widget to the current container GtkWidget *scroll_area = gtk_scrolled_window_new(NULL, NULL); gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(scroll_area), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); // GTK_POLICY_ALWAYS gtk_container_add(GTK_CONTAINER(scroll_area), view); UiContainer *ct = uic_get_current_container(obj); ct->add(ct, scroll_area, TRUE); // ct->current should point to view, not scroll_area, to make it possible // to add a context menu ct->current = view; return scroll_area; } UIWIDGET ui_table(UiObject *obj, UiList *list, UiModelInfo *modelinfo) { UiListPtr *listptr = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiListPtr)); listptr->list = list; return ui_table_var(obj, listptr, modelinfo); } UIWIDGET ui_table_nv(UiObject *obj, char *varname, UiModelInfo *modelinfo) { UiVar *var = uic_connect_var(obj->ctx, varname, UI_VAR_LIST); if(var) { UiListVar *value = var->value; return ui_table_var(obj, value->listptr, modelinfo); } else { // TODO: error } return NULL; } void ui_listview_update(UiEvent *event, UiTableView *view) { UiListModel *model = ui_list_model_new(view->list, view->modelinfo); gtk_tree_view_set_model(GTK_TREE_VIEW(view->widget), GTK_TREE_MODEL(model)); // TODO: free old model } void ui_listview_activate_event( GtkTreeView *treeview, GtkTreePath *path, GtkTreeViewColumn *column, UiTreeEventData *event) { UiListSelection *selection = ui_listview_selection( gtk_tree_view_get_selection(treeview), event); UiEvent e; e.obj = event->obj; e.window = event->obj->window; e.document = event->obj->ctx->document; e.eventdata = selection; e.intval = selection->count > 0 ? selection->rows[0] : -1; event->activate(&e, event->userdata); if(selection->count > 0) { free(selection->rows); } free(selection); } void ui_listview_selection_event( GtkTreeSelection *treeselection, UiTreeEventData *event) { UiListSelection *selection = ui_listview_selection(treeselection, event); UiEvent e; e.obj = event->obj; e.window = event->obj->window; e.document = event->obj->ctx->document; e.eventdata = selection; e.intval = selection->count > 0 ? selection->rows[0] : -1; event->selection(&e, event->userdata); if(selection->count > 0) { free(selection->rows); } free(selection); } UiListSelection* ui_listview_selection( GtkTreeSelection *selection, UiTreeEventData *event) { GList *rows = gtk_tree_selection_get_selected_rows(selection, NULL); UiListSelection *ls = malloc(sizeof(UiListSelection)); ls->count = g_list_length(rows); ls->rows = calloc(ls->count, sizeof(int)); GList *r = rows; int i = 0; while(r) { GtkTreePath *path = r->data; ls->rows[i] = ui_tree_path_list_index(path); r = r->next; i++; } return ls; } int ui_tree_path_list_index(GtkTreePath *path) { int depth = gtk_tree_path_get_depth(path); if(depth == 0) { fprintf(stderr, "UiError: treeview selection: depth == 0\n"); return -1; } int *indices = gtk_tree_path_get_indices(path); return indices[depth - 1]; }