"Fossies" - the Fresh Open Source Software Archive

Member "gtkdatabox-1.0.0/examples/colors.c" (31 Mar 2021, 6054 Bytes) of package /linux/privat/gtkdatabox-1.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "colors.c": 0.9.3.1_vs_1.0.0.

    1 /* $Id: colors.c 4 2008-06-22 09:19:11Z rbock $ */
    2 /* GtkDatabox - An extension to the gtk+ library
    3  * Copyright (C) 1998-2008  Dr. Roland Bock
    4  *
    5  * This library is free software; you can redistribute it and/or
    6  * modify it under the terms of the GNU Library General Public
    7  * License as published by the Free Software Foundation; either
    8  * version 2 of the License, or (at your option) any later version.
    9  *
   10  * This library is distributed in the hope that it will be useful,
   11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   13  * Library General Public License for more details.
   14  *
   15  * You should have received a copy of the GNU Library General Public
   16  * License along with this library; if not, write to the
   17  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 
   18  * Boston, MA 02110-1301 USA.
   19  */
   20 #include <stdio.h>
   21 #include <gtk/gtk.h>
   22 #include <gtkdatabox.h>
   23 #include <gtkdatabox_points.h>
   24 #include <math.h>
   25 
   26 #define POINTS 2000
   27 #define NO_OF_DATASETS 5
   28 
   29 typedef struct
   30 {
   31    GtkWidget *selector;
   32    GtkWidget *box;
   33    GtkDataboxGraph *graph;
   34    gint index;
   35 }
   36 col_sel;
   37 
   38 static void
   39 get_color_cb(GtkDialog *dialog, gint response_id, gpointer user_data)
   40 {
   41     GdkRGBA rgba;
   42     col_sel *sel = (col_sel *)user_data;
   43 
   44     g_return_if_fail (GTK_IS_DATABOX (sel->box));
   45 
   46     if (response_id == GTK_RESPONSE_OK)
   47     {
   48         gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(dialog), &rgba);
   49         gtk_databox_graph_set_color (sel->graph, &rgba);
   50         gtk_widget_queue_draw (GTK_WIDGET (sel->box));
   51     }
   52     gtk_widget_destroy(GTK_WIDGET(dialog));
   53 }
   54 
   55 static void
   56 menu_color_change_cb (col_sel * sel)
   57 {
   58    GtkWidget *selector;
   59    gchar title[20];
   60    GdkRGBA rgba;
   61 
   62    sprintf (title, "Choose color #%d", sel->index);
   63    selector = gtk_color_chooser_dialog_new(title, NULL);
   64    sel->selector = selector;
   65 
   66    rgba = *gtk_databox_graph_get_color (sel->graph);
   67    gtk_color_chooser_set_rgba(GTK_COLOR_CHOOSER(sel->selector), &rgba);
   68    gtk_color_chooser_set_use_alpha(GTK_COLOR_CHOOSER(sel->selector), FALSE);
   69    g_signal_connect(G_OBJECT(sel->selector), "response", G_CALLBACK(get_color_cb), (gpointer)sel);
   70    gtk_widget_show (GTK_WIDGET (sel->selector));
   71 
   72    return;
   73 }
   74 
   75 static void
   76 create_menu_entry (GtkWidget * menu, int index,
   77            GtkWidget * box, GtkDataboxGraph * graph)
   78 {
   79    GtkWidget *menu_items;
   80    char buf[128];
   81    col_sel *sel = g_new0 (col_sel, 1);
   82 
   83    sel->index = index;
   84    sel->box = box;
   85    sel->graph = graph;
   86    sprintf (buf, "Change Color #%d", index);
   87    menu_items = gtk_menu_item_new_with_label (buf);
   88    gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_items);
   89 
   90    g_signal_connect_swapped (G_OBJECT (menu_items), "activate",
   91                  G_CALLBACK (menu_color_change_cb),
   92                  (gpointer) sel);
   93 
   94    gtk_widget_show (menu_items);
   95 }
   96 
   97 static void
   98 create_colors (void)
   99 {
  100    static GtkWidget *window = NULL;
  101    GtkWidget *box1 = NULL;
  102    GtkWidget *box2 = NULL;
  103    GtkWidget *close_button = NULL;
  104    GtkWidget *box = NULL;
  105    GtkWidget *separator;
  106    GtkWidget *label;
  107    GtkWidget *menu;
  108    GtkWidget *root_menu;
  109    GtkWidget *menu_bar;
  110    gfloat *X = NULL;
  111    gfloat *Y = NULL;
  112    gint i, j;
  113    GtkDataboxGraph *graph;
  114    GdkRGBA color;
  115 
  116    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  117    gtk_widget_set_size_request (window, 500, 300);
  118 
  119    g_signal_connect (G_OBJECT (window), "destroy",
  120              G_CALLBACK (gtk_main_quit), NULL);
  121 
  122    gtk_window_set_title (GTK_WINDOW (window), "GtkDatabox: Colors");
  123    gtk_container_set_border_width (GTK_CONTAINER (window), 0);
  124 
  125    box1 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
  126    gtk_container_add (GTK_CONTAINER (window), box1);
  127 
  128    box = gtk_databox_new ();
  129    gtk_databox_set_enable_selection (GTK_DATABOX (box), FALSE);
  130    gtk_databox_set_enable_zoom (GTK_DATABOX (box), FALSE);
  131 
  132    gtk_databox_set_bg_color (GTK_DATABOX (box), "black");
  133 
  134    menu = gtk_menu_new ();
  135    root_menu = gtk_menu_item_new_with_label ("Color Menu");
  136    gtk_menu_item_set_submenu (GTK_MENU_ITEM (root_menu), menu);
  137    menu_bar = gtk_menu_bar_new ();
  138    gtk_menu_shell_append (GTK_MENU_SHELL (menu_bar), root_menu);
  139    gtk_box_pack_start (GTK_BOX (box1), menu_bar, FALSE, TRUE, 0);
  140 
  141 
  142    label =
  143       gtk_label_new
  144       ("You can change the colors of the shown datasets via the menu.\n\n");
  145    gtk_box_pack_start (GTK_BOX (box1), label, FALSE, FALSE, 0);
  146    separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
  147    gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, FALSE, 0);
  148 
  149    g_signal_connect (G_OBJECT (box), "destroy",
  150              G_CALLBACK (gtk_main_quit), NULL);
  151 
  152    color.alpha = 1.0;
  153    for (i = 0; i < NO_OF_DATASETS; i++)
  154    {
  155       if (!i)
  156      X = g_new0 (gfloat, POINTS);
  157       Y = g_new0 (gfloat, POINTS);
  158       for (j = 0; j < POINTS; j++)
  159       {
  160      X[j] = j;
  161      Y[j] = 100. * sin ((i + 1) * 2 * j * G_PI / POINTS);
  162       }
  163       color.red = 1 * ((i + 1) % 2);
  164       color.green = (1 / 2) * ((i + 1) % 3);
  165       color.blue = (1 / 3) * ((i + 1) % 4);
  166       graph = gtk_databox_points_new (POINTS, X, Y, &color, 1);
  167       gtk_databox_graph_add (GTK_DATABOX (box), graph);
  168       create_menu_entry (menu, i, box, graph);
  169    }
  170 
  171    gtk_databox_auto_rescale (GTK_DATABOX (box), 0.05);
  172 
  173    gtk_box_pack_start (GTK_BOX (box1), box, TRUE, TRUE, 0);
  174 
  175    separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
  176    gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
  177 
  178    box2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 10);
  179    gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
  180    gtk_box_pack_end (GTK_BOX (box1), box2, FALSE, TRUE, 0);
  181    close_button = gtk_button_new_with_label ("close");
  182    g_signal_connect (G_OBJECT (close_button), "clicked",
  183              G_CALLBACK (gtk_main_quit), (gpointer) NULL);
  184    gtk_box_pack_start (GTK_BOX (box2), close_button, TRUE, TRUE, 0);
  185    gtk_widget_set_can_default(close_button, TRUE);
  186    gtk_widget_grab_default (close_button);
  187 
  188 
  189 
  190 
  191 
  192    gtk_widget_show_all (window);
  193 
  194 }
  195 
  196 gint
  197 main (gint argc, char *argv[])
  198 {
  199    gtk_init (&argc, &argv);
  200 
  201    create_colors ();
  202    gtk_main ();
  203 
  204    return 0;
  205 }