/*************************************************************************** * Copyright (C) 04/2008 by Olaf Rempel * * razzor@kopf-tisch.de * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; version 2 of the License * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include #include #include #include #include #include #include "tdc_variable.h" #include "gui_graph_tab.h" #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) #define HISTORY 1000 static unsigned int used_colors; static GdkColor colors[] = { { .red = 0x0000, .green = 0x0000, .blue = 0xFF00, }, { .red = 0xFF00, .green = 0x0000, .blue = 0x0000, }, { .red = 0x0000, .green = 0xDF00, .blue = 0x0000, }, { .red = 0x0000, .green = 0xFF00, .blue = 0xFF00, }, { .red = 0xFF00, .green = 0x0000, .blue = 0xFF00, }, { .red = 0xA500, .green = 0x2A00, .blue = 0x2A00, }, { .red = 0xFF00, .green = 0xA500, .blue = 0x0000, }, { .red = 0x8000, .green = 0x8000, .blue = 0x8000, }, }; static GtkWidget *box; static GtkWidget *legend; static GList *graphlist; struct xygraph { GtkDataboxGraph *graph; struct tdc_var *var; gfloat xarr[HISTORY]; gfloat yarr[HISTORY]; GdkColor *color; struct timeval base; }; static GdkColor * get_color(void) { int i; for (i = 0; i < ARRAY_SIZE(colors); i++) { if (used_colors & (1 << i)) continue; used_colors |= (1 << i); return colors +i; } return NULL; } static void free_color(GdkColor *color) { int i; for (i = 0; i < ARRAY_SIZE(colors); i++) if (color == colors +i) used_colors &= ~(1 << i); } static void update_legend_cb(gpointer data, gpointer user_data) { struct xygraph *graph = (struct xygraph *)data; GString *tmp = (GString *)user_data; g_string_append_printf(tmp,"%s\n", (graph->color->red >> 8) & 0xFF, (graph->color->green >> 8) & 0xFF, (graph->color->blue >> 8) & 0xFF, graph->var->name); } static void update_legend(void) { GString *tmp; tmp = g_string_new(""); g_list_foreach(graphlist, &update_legend_cb, tmp); tmp->str[tmp->len -1] = 0x00; gtk_label_set_markup(GTK_LABEL(legend), tmp->str); g_string_free(tmp, TRUE); } gint gui_graphtab_init(GtkNotebook *notebook) { GtkWidget *table = gtk_table_new(5, 4, FALSE); box = gtk_databox_new(); gtk_table_attach(GTK_TABLE(table), box, 1, 4, 1, 2, GTK_FILL | GTK_EXPAND | GTK_SHRINK, GTK_FILL | GTK_EXPAND | GTK_SHRINK, 0, 0); GtkWidget *scrollbar = gtk_hscrollbar_new(gtk_databox_get_hadjustment(GTK_DATABOX(box))); gtk_table_attach(GTK_TABLE(table), scrollbar, 1, 4, 2, 3, GTK_FILL | GTK_EXPAND | GTK_SHRINK, GTK_FILL, 0, 0); scrollbar = gtk_vscrollbar_new(gtk_databox_get_vadjustment(GTK_DATABOX(box))); gtk_table_attach(GTK_TABLE(table), scrollbar, 4, 5, 1, 2, GTK_FILL, GTK_FILL | GTK_EXPAND | GTK_SHRINK, 0, 0); GtkWidget *ruler = gtk_hruler_new(); gtk_table_attach(GTK_TABLE(table), ruler, 1, 4, 0, 1, GTK_FILL | GTK_EXPAND | GTK_SHRINK, GTK_FILL, 0, 0); gtk_databox_set_hruler(GTK_DATABOX(box), GTK_RULER(ruler)); ruler = gtk_vruler_new(); gtk_table_attach (GTK_TABLE (table), ruler, 0, 1, 1, 2, GTK_FILL, GTK_FILL | GTK_EXPAND | GTK_SHRINK, 0, 0); gtk_databox_set_vruler(GTK_DATABOX(box), GTK_RULER(ruler)); legend = gtk_label_new(NULL); gtk_table_attach(GTK_TABLE(table), legend, 1, 2, 3, 4, 0, 0, 10, 10); GtkWidget *button2 = gtk_button_new_with_label("Mode"); gtk_table_attach(GTK_TABLE(table), button2, 2, 3, 3, 4, 0, 0, 10, 10); GtkWidget *button3 = gtk_button_new_with_label("Stopp"); gtk_table_attach(GTK_TABLE(table), button3, 3, 4, 3, 4, 0, 0, 10, 10); struct tdc_var *var = tdcvar_create(1, 0, "test", 4); gui_graphtab_add_var(var); gui_graphtab_add_var(var); gui_graphtab_add_var(var); gui_graphtab_add_var(var); gui_graphtab_add_var(var); gui_graphtab_add_var(var); gui_graphtab_add_var(var); gui_graphtab_add_var(var); GtkWidget *label = gtk_label_new(" Graph "); return gtk_notebook_append_page(GTK_NOTEBOOK(notebook), table, label); } int gui_graphtab_add_var(struct tdc_var *var) { GdkColor *color = get_color(); if (color == NULL) return -1; struct xygraph *graph = g_malloc0(sizeof(struct xygraph)); graph->graph = gtk_databox_lines_new(HISTORY, graph->xarr, graph->yarr, color, 1); graph->var = var; graph->color = color; var->privdata_graphtab = graph; int i; for (i = 0; i < HISTORY; i++) { graph->xarr[i] = i - HISTORY; graph->yarr[i] = sin(((int)graph % 127) + i * 4 * M_PI / 1024); } graphlist = g_list_append(graphlist, graph); gtk_databox_graph_add(GTK_DATABOX(box), graph->graph); gtk_databox_auto_rescale(GTK_DATABOX(box), 0.05); update_legend(); return 0; } void gui_graphtab_remove_var(struct tdc_var *var) { struct xygraph *graph = (struct xygraph *)var->privdata_graphtab; gtk_databox_graph_remove(GTK_DATABOX(box), graph->graph); g_free(graph->graph); free_color(graph->color); graphlist = g_list_remove(graphlist, graph); graph->var->privdata_graphtab = NULL; g_free(graph); } void gui_graphtab_update_var(struct tdc_var *var) { struct xygraph *graph = (struct xygraph *)var->privdata_graphtab; // do some updates, i think }