"Fossies" - the Fresh Open Source Software Archive

Member "gtkdatabox-1.0.0/examples/basics2.c" (31 Mar 2021, 8965 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 "basics2.c": 0.9.3.1_vs_1.0.0.

A hint: This file contains one or more very long lines, so maybe it is better readable using the pure text view mode that shows the contents as wrapped lines within the browser window.


    1 /* $Id: basics2.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 program is free software; you can redistribute it and/or
    6  * modify it under the terms of the GNU Lesser General Public License
    7  * as published by the Free Software Foundation; either version 2.1
    8  * of the License, or (at your option) any later version.
    9  *
   10  * This program 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
   13  * GNU Lesser General Public License for more details.
   14  *
   15  * You should have received a copy of the GNU General Public License
   16  * along with this program; if not, write to the Free Software
   17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
   18  */
   19 #include <stdio.h>
   20 
   21 #include <gtk/gtk.h>
   22 #include <gtkdatabox.h>
   23 #include <gtkdatabox_points.h>
   24 #include <gtkdatabox_lines.h>
   25 #include <gtkdatabox_bars.h>
   26 #include <gtkdatabox_offset_bars.h>
   27 #include <gtkdatabox_regions.h>
   28 #include <gtkdatabox_cross_simple.h>
   29 #include <gtkdatabox_ruler.h>
   30 #include <math.h>
   31 
   32 #define POINTS 2000
   33 #define STEPS 50
   34 #define BARS 25
   35 #define OFFSET_BARS 25
   36 #define REGIONS 25
   37 #define MARKER 10
   38 
   39 /*----------------------------------------------------------------
   40  *  databox basics2
   41  *----------------------------------------------------------------*/
   42 
   43 static void
   44 create_basics (void)
   45 {
   46    GtkWidget *window = NULL;
   47    GtkWidget *vbox;
   48    GtkWidget *close_button;
   49    GtkWidget *box;
   50    GtkWidget *label;
   51    GtkWidget *separator;
   52    GtkWidget *grid;
   53    GtkWidget *scrollbar;
   54    GtkWidget *ruler;
   55    GtkDataboxGraph *graph;
   56    gfloat min_x, max_x;
   57    gfloat min_y, max_y;
   58    gfloat *X;
   59    gfloat *Y;
   60    gfloat *Y1; /*  for the offset bars and regions*/
   61    gfloat *Y2; /* for the offset bars and regions */
   62    gfloat buffer;
   63    GdkRGBA color;
   64    gint i;
   65 
   66    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
   67    gtk_widget_set_size_request (window, 500, 500);
   68 
   69    g_signal_connect (G_OBJECT (window), "destroy",
   70              G_CALLBACK (gtk_main_quit), NULL);
   71 
   72    gtk_window_set_title (GTK_WINDOW (window), "GtkDatabox: Basics");
   73    gtk_container_set_border_width (GTK_CONTAINER (window), 0);
   74 
   75    vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
   76    gtk_container_add (GTK_CONTAINER (window), vbox);
   77 
   78    label =
   79       gtk_label_new
   80       ("\nThe code for this example demonstrates\n the the basic use of elementary functions\nfor utilizing the GtkDatabox widget.\n\nUsage:\nDraw a selection with the left button pressed,\nThan click into the selection.\nUse the right mouse button to zoom out.\nShift+ right mouse button zooms to default.");
   81    gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
   82    separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
   83    gtk_box_pack_start (GTK_BOX (vbox), separator, FALSE, FALSE, 0);
   84 
   85 
   86    /* Instead of the following stuff, you could also call
   87     * gtk_databox_create_box_with_scrollbars_and_rulers
   88     * as it is done in the other examples.
   89     * Of course, you are more flexible in using scrollbars and rulers
   90     * by doing it yourself. */
   91    grid = gtk_grid_new ();
   92 
   93    box = gtk_databox_new ();
   94 
   95    g_object_set(G_OBJECT(box), "expand", TRUE, NULL);
   96 
   97    gtk_grid_attach (GTK_GRID (grid), box, 1, 1, 1, 1);
   98 
   99    /* You can associate a scrollbar with a GtkDatabox widget either
  100     * this way ...*/
  101    scrollbar = gtk_scrollbar_new (GTK_ORIENTATION_HORIZONTAL, NULL);
  102    gtk_databox_set_adjustment_x (GTK_DATABOX (box),
  103                 gtk_range_get_adjustment (GTK_RANGE
  104                               (scrollbar)));
  105    gtk_grid_attach (GTK_GRID (grid), scrollbar, 1, 2, 1, 1);
  106    /* or this way ... */
  107    scrollbar = gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL,
  108       (gtk_databox_get_adjustment_y (GTK_DATABOX (box))));
  109    gtk_grid_attach (GTK_GRID (grid), scrollbar, 2, 1, 1, 1);
  110 
  111 
  112    ruler = gtk_databox_ruler_new (GTK_ORIENTATION_HORIZONTAL);
  113    gtk_widget_set_sensitive (ruler, FALSE);
  114 
  115    gtk_grid_attach (GTK_GRID (grid), ruler, 1, 0, 1, 1);
  116    gtk_databox_set_ruler_x (GTK_DATABOX (box), GTK_DATABOX_RULER (ruler));
  117 
  118    ruler = gtk_databox_ruler_new (GTK_ORIENTATION_VERTICAL);
  119    gtk_widget_set_sensitive (ruler, FALSE);
  120    gtk_grid_attach (GTK_GRID (grid), ruler, 0, 1, 1, 1);
  121    gtk_databox_set_ruler_y (GTK_DATABOX (box), GTK_DATABOX_RULER (ruler));
  122    /* end of gtk_databox_create_box_with_scrollbars_and_rulers */
  123 
  124    gtk_box_pack_start (GTK_BOX (vbox), grid, TRUE, TRUE, 0);
  125 
  126    gtk_databox_set_bg_color (GTK_DATABOX (box), "#333333");
  127 
  128    X = g_new0 (gfloat, POINTS);
  129    Y = g_new0 (gfloat, POINTS);
  130 
  131    for (i = 0; i < POINTS; i++)
  132    {
  133       X[i] = i;
  134       Y[i] = 100. * sin (i * 2 * G_PI / POINTS);
  135    }
  136    color.red = 0;
  137    color.green = 1;
  138    color.blue = 0;
  139    color.alpha = 1;
  140 
  141    graph = gtk_databox_points_new (POINTS, X, Y, &color, 1);
  142    gtk_databox_graph_add (GTK_DATABOX (box), graph);
  143 
  144    X = g_new0 (gfloat, STEPS);
  145    Y = g_new0 (gfloat, STEPS);
  146 
  147    for (i = 0; i < STEPS / 2; i++)
  148    {
  149       X[i * 2] = X[i * 2 + 1] = 2 * (i + .5) * POINTS / STEPS;
  150 
  151       if (i > 0)
  152      Y[i * 2 - 1] = 100. * sin ((i * 2) * 2 * G_PI / STEPS);
  153       Y[i * 2] = 100. * sin ((i * 2) * 2 * G_PI / STEPS);
  154       if (i == STEPS / 2 - 1)
  155      Y[i * 2 + 1] = 100. * sin (((i + 1) * 2) * 2 * G_PI / STEPS);
  156    }
  157    color.red = 1;
  158    color.green = 0;
  159    color.blue = 0;
  160    color.alpha = 1;
  161 
  162    graph = gtk_databox_lines_new (STEPS, X, Y, &color, 1);
  163    gtk_databox_graph_add (GTK_DATABOX (box), graph);
  164 
  165    X = g_new0 (gfloat, BARS);
  166    Y = g_new0 (gfloat, BARS);
  167 
  168    for (i = 0; i < BARS; i++)
  169    {
  170       X[i] = i * POINTS / BARS;
  171       Y[i] = 80. * sin (i * 2 * G_PI / BARS);
  172    }
  173 
  174    color.red = 0;
  175    color.green = 1;
  176    color.blue = 1;
  177    color.alpha = 1;
  178 
  179    graph = gtk_databox_bars_new (BARS, X, Y, &color, 1);
  180    gtk_databox_graph_add (GTK_DATABOX (box), graph);
  181 
  182    color.red = 0.5;
  183    color.green = 0.5;
  184    color.blue = 0.5;
  185    color.alpha = 1;
  186 
  187    graph = gtk_databox_cross_simple_new (&color, 0);
  188    gtk_databox_graph_add (GTK_DATABOX (box), graph);
  189 
  190    X = g_new0 (gfloat, OFFSET_BARS);
  191    Y1 = g_new0 (gfloat, OFFSET_BARS);
  192    Y2 = g_new0 (gfloat, OFFSET_BARS);
  193 
  194    for (i = 0; i < OFFSET_BARS; i++)
  195    {
  196       X[i] = (i+0.5) * POINTS / OFFSET_BARS;
  197       Y1[i] = 80. * sin ((i+0.5) * 2 * G_PI / OFFSET_BARS);
  198       Y2[i] = -0.5*(80. * sin ((i+0.5) * 2 * G_PI / OFFSET_BARS));
  199    }
  200    color.red = 1;
  201    color.green = 0;
  202    color.blue = 1;
  203    color.alpha = 1;
  204 
  205    graph = gtk_databox_offset_bars_new (BARS, X, Y1, Y2, &color, 1);
  206    gtk_databox_graph_add (GTK_DATABOX (box), graph);
  207 
  208    X = g_new0 (gfloat, REGIONS);
  209    Y1 = g_new0 (gfloat, REGIONS);
  210    Y2 = g_new0 (gfloat, REGIONS);
  211 
  212    for (i = 0; i < REGIONS; i++)
  213    {
  214       X[i] = (i+0.5) * POINTS / REGIONS;
  215       Y1[i] = .5*80. * sin ((i+0.5) * 2 * G_PI / REGIONS);
  216       Y2[i] = .5*-0.5*(80. * sin ((i+0.5) * 2 * G_PI / REGIONS));
  217    }
  218    color.red = 0.5;
  219    color.green = 0.25;
  220    color.blue = 0.5;
  221    color.alpha = 1;
  222 
  223    graph = gtk_databox_regions_new (BARS, X, Y1, Y2, &color);
  224    gtk_databox_graph_add (GTK_DATABOX (box), graph);
  225 
  226    /* Instead of doing stuff what you see below, you could call
  227     * gtk_databox_auto_rescale (GTK_DATABOX (box), 0.05);
  228     * as is done in the other examples
  229     * Of course, you are more flexible in adjusting the scaling by doing
  230     * it yourself, though. */
  231    if (0 >
  232        gtk_databox_calculate_extrema (GTK_DATABOX (box), &min_x, &max_x,
  233                       &min_y, &max_y))
  234    {
  235       g_warning ("Calculating extrema failed. Resorting to default values");
  236       min_x = -100.;
  237       min_y = -100.;
  238       max_x = +100.;
  239       max_y = +100.;
  240    }
  241    else
  242    {
  243       gfloat width = max_x - min_x;
  244       gfloat height = max_y - min_y;
  245       gfloat border = 0.05;
  246 
  247       min_x -= border * width;
  248       min_y -= border * height;
  249       max_x += border * width;
  250       max_y += border * height;
  251    }
  252 
  253    /* We want larger y values to be higher on the screen */
  254    buffer = min_y;
  255    min_y = max_y;
  256    max_y = buffer;
  257 
  258    gtk_databox_set_total_limits (GTK_DATABOX (box), min_x, max_x, min_y,
  259                  max_y);
  260    /* end of gtk_databox_auto_rescale */
  261 
  262    separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
  263    gtk_box_pack_start (GTK_BOX (vbox), separator, FALSE, TRUE, 0);
  264 
  265    close_button = gtk_button_new_with_label ("close");
  266    g_signal_connect_swapped (G_OBJECT (close_button), "clicked",
  267                  G_CALLBACK (gtk_main_quit), G_OBJECT (box));
  268    gtk_box_pack_start (GTK_BOX (vbox), close_button, FALSE, FALSE, 0);
  269    gtk_widget_set_can_default(close_button, TRUE);
  270    gtk_widget_grab_default (close_button);
  271    gtk_widget_grab_focus (close_button);
  272 
  273    gtk_widget_show_all (window);
  274    gdk_window_set_cursor (gtk_widget_get_window(box), gdk_cursor_new_for_display (gdk_display_get_default (), GDK_CROSS));
  275 }
  276 
  277 gint
  278 main (gint argc, char *argv[])
  279 {
  280    gtk_init (&argc, &argv);
  281 
  282    create_basics ();
  283    gtk_main ();
  284 
  285    return 0;
  286 }