"Fossies" - the Fresh Open Source Software Archive

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

    1 /* $Id: logarithmic.c 4 2008-06-22 09:19:11Z rbock $ */
    2 /* GtkDatabox - An extension to the gtk+ library
    3  * Copyright (C) 2011 - 2012  Dr. Matt Flax <flatmax@>
    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_ruler.h>
   25 #include <math.h>
   26 
   27 #define POINTS 100000
   28 /*
   29 #define STEPS 50
   30 #define BARS 25
   31 #define MARKER 10
   32 */
   33 #define NO_BOXES 4
   34 
   35 /*----------------------------------------------------------------
   36  *  databox rulers
   37  *----------------------------------------------------------------*/
   38 
   39 static void
   40 create_rulers (void)
   41 {
   42     GtkWidget *window = NULL;
   43     GtkWidget *hbox;
   44     GtkWidget *vbox;
   45     GtkWidget *close_button;
   46     GtkWidget *box[NO_BOXES];
   47     GtkWidget *label;
   48     GtkWidget *separator;
   49     GtkWidget *table;
   50     GtkDataboxGraph *graph;
   51     gfloat min_x, max_x;
   52     gfloat min_y, max_y;
   53     gfloat *X;
   54     gfloat *Y;
   55     GdkRGBA color;
   56     gint i;
   57     GtkDataboxRuler *ruler;
   58 
   59     guint manual_tick_cnt=4;
   60     gfloat manual_ticks[]={100., 300., 500., 700.};
   61 
   62     guint manual_tick_cnt_box2=5;
   63     gfloat manual_ticks_box2[]={.01, .1, 1., 10., 100.};
   64     gchar *manual_tick_labels_box2[]={"these", "are", "manual", "tick", "labels"};
   65 
   66 
   67     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
   68     gtk_widget_set_size_request (window, 1000, 500);
   69 
   70     g_signal_connect (G_OBJECT (window), "destroy",
   71                       G_CALLBACK (gtk_main_quit), NULL);
   72 
   73     gtk_window_set_title (GTK_WINDOW (window),
   74                           "GtkDatabox: Ruler option example plots");
   75     gtk_container_set_border_width (GTK_CONTAINER (window), 0);
   76 
   77     vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
   78     gtk_container_add (GTK_CONTAINER (window), vbox);
   79 
   80     label = gtk_label_new
   81             ("demonstrates diferent ruler settings\n");
   82     gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
   83     separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
   84     gtk_box_pack_start (GTK_BOX (vbox), separator, FALSE, FALSE, 0);
   85 
   86     hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
   87     gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
   88 
   89     label = gtk_label_new ("Rulers on the other side:");
   90     gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
   91 
   92     label = gtk_label_new ("Horizontal y axis text\n manual x axis ticks example\n right aligned y text:");
   93     gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
   94 
   95     label = gtk_label_new ("Subticks turned off on x and y,\n manual tick labels on x:");
   96     gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
   97 
   98     label = gtk_label_new ("X ticks turned off, no shadow\n horizontal y axis text,\n altered y label format example:");
   99     gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
  100 
  101     separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
  102     gtk_box_pack_start (GTK_BOX (vbox), separator, FALSE, FALSE, 0);
  103 
  104     /* Create the databoxes */
  105     hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
  106     gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
  107 
  108     for (i = 0; i < NO_BOXES; ++i)
  109     {
  110         if ((i==0) || (i==NO_BOXES-1)) /* the first is normal but with the rulers on opposite sides to the default */
  111             gtk_databox_create_box_with_scrollbars_and_rulers_positioned (&box[i], &table,TRUE, TRUE, TRUE,TRUE,FALSE,FALSE);
  112         else
  113             gtk_databox_create_box_with_scrollbars_and_rulers (&box[i], &table,TRUE, TRUE, TRUE,TRUE);
  114         gtk_box_pack_start (GTK_BOX (hbox), table, TRUE, TRUE, 0);
  115     }
  116 
  117     /* set box 1's y ruler to have horizontal text */
  118     ruler=gtk_databox_get_ruler_y(GTK_DATABOX (box[1]));
  119     gtk_databox_ruler_set_text_orientation(ruler, GTK_ORIENTATION_HORIZONTAL);
  120     gtk_databox_ruler_set_box_shadow(ruler, GTK_SHADOW_ETCHED_OUT);
  121     gtk_databox_ruler_set_text_alignment (ruler, PANGO_ALIGN_RIGHT);
  122     gtk_databox_set_ruler_y (GTK_DATABOX (box[1]), ruler);
  123     ruler=gtk_databox_get_ruler_x(GTK_DATABOX (box[1]));     /* set box 1's x ruler to have manual ticks */
  124     gtk_databox_ruler_set_manual_tick_cnt(ruler, manual_tick_cnt);
  125     gtk_databox_ruler_set_manual_ticks(ruler, manual_ticks);
  126     gtk_databox_set_ruler_x (GTK_DATABOX (box[1]), ruler);
  127 
  128     /* set box 2 to have no subticks */
  129     ruler=gtk_databox_get_ruler_y(GTK_DATABOX (box[2]));
  130     gtk_databox_ruler_set_draw_subticks(ruler, FALSE);
  131     gtk_databox_set_ruler_y (GTK_DATABOX (box[2]), ruler);
  132     ruler=gtk_databox_get_ruler_x(GTK_DATABOX (box[2])); /* put manual labels on x */
  133     gtk_databox_ruler_set_draw_subticks(ruler, FALSE);
  134     gtk_databox_ruler_set_manual_tick_cnt(ruler, manual_tick_cnt_box2);
  135     gtk_databox_ruler_set_manual_ticks(ruler, manual_ticks_box2);
  136     gtk_databox_ruler_set_manual_tick_labels(ruler, manual_tick_labels_box2);
  137     gtk_databox_set_ruler_x (GTK_DATABOX (box[2]), ruler);
  138 
  139     /* set box 3's y ruler to have horizontal text */
  140     ruler=gtk_databox_get_ruler_y(GTK_DATABOX (box[3]));
  141     gtk_databox_ruler_set_text_orientation(ruler, GTK_ORIENTATION_HORIZONTAL);
  142     gtk_databox_set_ruler_y (GTK_DATABOX (box[3]), ruler);
  143     /* set box 3 to have no x ticks */
  144     ruler=gtk_databox_get_ruler_x(GTK_DATABOX (box[3]));
  145     gtk_databox_ruler_set_draw_ticks(ruler, FALSE);
  146     /* box[3] to have different shadow on the ruler */
  147     gtk_databox_ruler_set_box_shadow(ruler, GTK_SHADOW_NONE);
  148     gtk_databox_set_ruler_x (GTK_DATABOX (box[3]), ruler);
  149     ruler=gtk_databox_get_ruler_y(GTK_DATABOX (box[3]));
  150     gtk_databox_ruler_set_draw_subticks(ruler, FALSE);
  151     /* set box[3] to have different formatting */
  152     gtk_databox_ruler_set_linear_label_format(ruler, "%%-%dg");
  153     gtk_databox_set_ruler_y (GTK_DATABOX (box[3]), ruler);
  154 
  155     /* set the background */
  156     for (i = 0; i < NO_BOXES; ++i)
  157       gtk_databox_set_bg_color (GTK_DATABOX (box[i]), "#555");
  158 
  159     /* add a sinus^2 */
  160     X = g_new0 (gfloat, POINTS);
  161     Y = g_new0 (gfloat, POINTS);
  162 
  163     for (i = 0; i < POINTS; i++)
  164     {
  165         X[i] = i / 100.;
  166         Y[i] = 0.01 + 1000 * pow (sin (i * G_PI / POINTS), 2);
  167     }
  168 
  169     color.red = 0;
  170     color.green = 1;
  171     color.blue = 0;
  172     color.alpha = 1;
  173 
  174     graph = gtk_databox_points_new (POINTS, X, Y, &color, 1);
  175     for (i = 0; i < NO_BOXES; ++i)
  176         gtk_databox_graph_add (GTK_DATABOX (box[i]), graph);
  177 
  178     /* add an exp function */
  179     X = g_new0 (gfloat, POINTS);
  180     Y = g_new0 (gfloat, POINTS);
  181 
  182     for (i = 0; i < POINTS; i++)
  183     {
  184         X[i] = i / 100.;
  185         Y[i] = exp (log (1000) * i / ((gfloat) POINTS));
  186     }
  187 
  188     color.red = 1;
  189     color.green = 0;
  190     color.blue = 0;
  191     color.alpha = 1;
  192 
  193     graph = gtk_databox_points_new (POINTS, X, Y, &color, 1);
  194     for (i = 0; i < NO_BOXES; ++i)
  195         gtk_databox_graph_add (GTK_DATABOX (box[i]), graph);
  196 
  197     /* add an 1/x function */
  198     X = g_new0 (gfloat, POINTS);
  199     Y = g_new0 (gfloat, POINTS);
  200 
  201     for (i = 0; i < POINTS; i++)
  202     {
  203         X[i] = i / 100.;
  204         Y[i] = 1000. / (100. * (gfloat) i / POINTS);
  205     }
  206 
  207     color.red = 1;
  208     color.green = 0;
  209     color.blue = 1;
  210     color.alpha = 1;
  211 
  212     graph = gtk_databox_points_new (POINTS, X, Y, &color, 1);
  213     for (i = 0; i < NO_BOXES; ++i)
  214         gtk_databox_graph_add (GTK_DATABOX (box[i]), graph);
  215 
  216     /* add an x^2 function */
  217     X = g_new0 (gfloat, POINTS);
  218     Y = g_new0 (gfloat, POINTS);
  219 
  220     for (i = 0; i < POINTS; i++)
  221     {
  222         X[i] = i / 100.;
  223         Y[i] = 1000. * (gfloat) i / POINTS * (gfloat) i / POINTS;
  224     }
  225 
  226     color.red = 1;
  227     color.green = 1;
  228     color.blue = 0;
  229     color.alpha = 1;
  230 
  231     graph = gtk_databox_points_new (POINTS, X, Y, &color, 1);
  232     for (i = 0; i < NO_BOXES; ++i)
  233         gtk_databox_graph_add (GTK_DATABOX (box[i]), graph);
  234 
  235     /* set the extrema */
  236     min_x = 0.01;
  237     max_x = 1000.;
  238     min_y = 1000.;
  239     max_y = 0.01;
  240 
  241     for (i = 0; i < NO_BOXES-1; ++i)
  242         gtk_databox_set_total_limits (GTK_DATABOX (box[i]), min_x, max_x, min_y,
  243                                       max_y);
  244     gtk_databox_set_total_limits (GTK_DATABOX (box[i]), 8, max_x, min_y,
  245                                   max_y);
  246 
  247     gtk_databox_set_scale_type_y (GTK_DATABOX (box[1]), GTK_DATABOX_SCALE_LOG);
  248     gtk_databox_set_scale_type_x (GTK_DATABOX (box[2]), GTK_DATABOX_SCALE_LOG);
  249     gtk_databox_set_scale_type_y (GTK_DATABOX (box[2]), GTK_DATABOX_SCALE_LOG);
  250     gtk_databox_set_scale_type_x (GTK_DATABOX (box[3]), GTK_DATABOX_SCALE_LOG2);
  251 
  252     separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
  253     gtk_box_pack_start (GTK_BOX (vbox), separator, FALSE, TRUE, 0);
  254 
  255     close_button = gtk_button_new_with_label ("close");
  256     g_signal_connect (G_OBJECT (close_button), "clicked",
  257                       G_CALLBACK (gtk_main_quit), NULL);
  258     gtk_box_pack_start (GTK_BOX (vbox), close_button, FALSE, FALSE, 0);
  259     gtk_widget_set_can_default(close_button, TRUE);
  260     gtk_widget_grab_default (close_button);
  261     gtk_widget_grab_focus (close_button);
  262 
  263     gtk_widget_show_all (window);
  264     for (i = 0; i < NO_BOXES; ++i)
  265        gdk_window_set_cursor (gtk_widget_get_window(box[i]), gdk_cursor_new_for_display (gdk_display_get_default (), GDK_CROSS));
  266 
  267 }
  268 
  269 gint
  270 main (gint argc, char *argv[])
  271 {
  272     gtk_init (&argc, &argv);
  273 
  274     create_rulers();
  275     gtk_main ();
  276 
  277     return 0;
  278 }