"Fossies" - the Fresh Open Source Software Archive

Member "gtkdatabox-1.0.0/examples/lissajous.c" (16 Apr 2021, 5434 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 "lissajous.c": 0.9.3.1_vs_1.0.0.

    1 /* $Id: lissajous.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 <math.h>
   26 
   27 #define POINTS 2000
   28 #define FRAME_RATE 50
   29 
   30 /*----------------------------------------------------------------
   31  *  databox lissajous
   32  *----------------------------------------------------------------*/
   33 
   34 static gfloat *lissajousX = NULL;
   35 static gfloat *lissajousY = NULL;
   36 
   37 static gint lissajous_timeout_id;
   38 static gfloat lissajous_frequency;
   39 static GtkWidget *lissajous_label;
   40 static guint lissajous_counter;
   41 
   42 static gboolean
   43 lissajous_draw_func (GtkDatabox * box)
   44 {
   45    gfloat freq;
   46    gfloat off;
   47    gchar label[10];
   48    gint i;
   49 
   50    if (!GTK_IS_DATABOX (box))
   51       return FALSE;
   52 
   53    lissajous_frequency += 0.025;
   54    off = lissajous_counter * 4 * G_PI / POINTS;
   55 
   56    freq = 14 + 10 * sin (lissajous_frequency);
   57    for (i = 0; i < POINTS; i++)
   58    {
   59       lissajousX[i] = 100. * sin (i * 4 * G_PI / POINTS + off);
   60       lissajousY[i] = 100. * cos (i * freq * G_PI / POINTS + off);
   61    }
   62 
   63 
   64    gtk_widget_queue_draw (GTK_WIDGET (box));
   65 
   66    sprintf (label, "%d", lissajous_counter++);
   67    gtk_entry_set_text (GTK_ENTRY (lissajous_label), label);
   68 
   69    return TRUE;
   70 }
   71 
   72 static void
   73 create_lissajous (void)
   74 {
   75    GtkWidget *window = NULL;
   76    GtkWidget *box1;
   77    GtkWidget *box2;
   78    GtkWidget *close_button;
   79    GtkWidget *box;
   80    GtkWidget *label;
   81    GtkWidget *table;
   82    GtkWidget *separator;
   83    GtkDataboxGraph *graph;
   84    GdkRGBA color;
   85    gint i;
   86 
   87    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
   88    gtk_widget_set_size_request (window, 500, 500);
   89 
   90    g_signal_connect (G_OBJECT (window), "destroy",
   91              G_CALLBACK (gtk_main_quit), NULL);
   92 
   93    gtk_window_set_title (GTK_WINDOW (window),
   94              "GtkDatabox: Lissajous Example");
   95    gtk_container_set_border_width (GTK_CONTAINER (window), 0);
   96 
   97    box1 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
   98    gtk_container_add (GTK_CONTAINER (window), box1);
   99 
  100    label =
  101       gtk_label_new
  102       ("This example resembles an oscilloscope\nreceiving two signals, one is a sine (horizontal),\nthe other is a cosine with ever changing frequency (vertical).\nThe counter is synchron with the updates.");
  103    gtk_box_pack_start (GTK_BOX (box1), label, FALSE, FALSE, 0);
  104    separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
  105    gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, FALSE, 0);
  106    lissajous_label = gtk_entry_new ();
  107    gtk_entry_set_text (GTK_ENTRY (lissajous_label), "0");
  108    gtk_box_pack_start (GTK_BOX (box1), lissajous_label, FALSE, FALSE, 0);
  109    separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
  110    gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, FALSE, 0);
  111 
  112    lissajous_timeout_id = 0;
  113    lissajous_frequency = 3. * G_PI / 2.;
  114    lissajous_counter = 0;
  115 
  116    /* Create a GtkDatabox widget along with scrollbars and rulers */
  117    gtk_databox_create_box_with_scrollbars_and_rulers (&box, &table,
  118                               TRUE, TRUE, TRUE, TRUE);
  119 
  120    g_object_set(G_OBJECT(box), "expand", TRUE, NULL);
  121 
  122    gtk_box_pack_start (GTK_BOX (box1), table, TRUE, TRUE, 0);
  123 
  124    gtk_databox_set_bg_color (GTK_DATABOX (box), "#008");
  125 
  126    lissajousX = g_new0 (gfloat, POINTS);
  127    lissajousY = g_new0 (gfloat, POINTS);
  128 
  129    for (i = 0; i < POINTS; i++)
  130    {
  131       lissajousX[i] = 100. * sin (i * 4 * G_PI / POINTS);
  132       lissajousY[i] = 100. * cos (i * 4 * G_PI / POINTS);
  133    }
  134    color.red = 1;
  135    color.green = 1;
  136    color.blue = 0;
  137    color.alpha = 1;
  138 
  139    graph = gtk_databox_lines_new (POINTS, lissajousX, lissajousY, &color, 1);
  140    gtk_databox_graph_add (GTK_DATABOX (box), graph);
  141 
  142    gtk_databox_auto_rescale (GTK_DATABOX (box), 0.05);
  143 
  144    separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
  145    gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
  146 
  147    box2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 10);
  148    gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
  149    gtk_box_pack_end (GTK_BOX (box1), box2, FALSE, TRUE, 0);
  150    close_button = gtk_button_new_with_label ("close");
  151 
  152    g_signal_connect_swapped (G_OBJECT (close_button), "clicked",
  153                  G_CALLBACK (gtk_main_quit), G_OBJECT (box));
  154 
  155    gtk_box_pack_start (GTK_BOX (box2), close_button, TRUE, TRUE, 0);
  156    gtk_widget_set_can_default(close_button, TRUE);
  157    gtk_widget_grab_default (close_button);
  158    lissajous_timeout_id = g_timeout_add (1000 / FRAME_RATE,
  159                (GSourceFunc) lissajous_draw_func, box);
  160 
  161    gtk_widget_show_all (window);
  162 }
  163 
  164 gint
  165 main (gint argc, char *argv[])
  166 {
  167    gtk_init (&argc, &argv);
  168 
  169    create_lissajous ();
  170    gtk_main ();
  171 
  172    return 0;
  173 }