"Fossies" - the Fresh Open Source Software Archive

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

    1 /* $Id: signals.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 <math.h>
   25 
   26 #define POINTS 2000
   27 
   28 /*----------------------------------------------------------------
   29  *  databox signals
   30  *----------------------------------------------------------------*/
   31 
   32 const gchar *
   33 get_name_of_current_signal (gpointer instance)
   34 {
   35    GSignalInvocationHint *ihint;
   36 
   37    ihint = g_signal_get_invocation_hint (instance);
   38 
   39    return g_signal_name (ihint->signal_id);
   40 }
   41 
   42 /* 
   43  * Signal handlers 
   44  */
   45 static gint
   46 handle_signal_zoomed (GtkDatabox * box)
   47 {
   48    gfloat left, right, top, bottom;
   49 
   50    gtk_databox_get_visible_limits (box, &left, &right, &top, &bottom);
   51    printf ("Name of the signal: %s\n", get_name_of_current_signal (box));
   52    printf ("It tells you that the GtkDatabox has zoomed to the following\n");
   53    printf
   54       ("rectangle (data coordindates only, pixels don't make sense here):\n");
   55    printf ("top_left (X,Y)=(%g, %g), bottom_right (X,Y)=(%g, %g)\n",
   56        left, top, right, bottom);
   57 
   58    return 0;
   59 }
   60 
   61 static gint
   62 handle_signal_selection_finalized (GtkDatabox * box,
   63                  GtkDataboxValueRectangle * selectionValues
   64                  /*, void *unused */ )
   65 {
   66    printf ("Name of the signal: %s\n", get_name_of_current_signal (box));
   67    printf ("It tells you that the user has stopped changing the selection\n");
   68    printf ("box, i.e. the mouse button is released now.\n");
   69    printf ("Data: corner1 (X,Y)=(%g, %g), corner2 (X,Y)=(%g, %g)\n",
   70        selectionValues->x1, selectionValues->y1, selectionValues->x2,
   71        selectionValues->y2);
   72 
   73    return 0;
   74 }
   75 
   76 static gint
   77 handle_signal_selection_started (GtkDatabox * box /*, void *unused */ )
   78 {
   79    printf ("Name of the signal: %s\n", get_name_of_current_signal (box));
   80    printf ("It tells you that the user has started a the selection box\n");
   81 
   82    return 0;
   83 }
   84 
   85 static gint
   86 handle_signal_selection_canceled (GtkDatabox * box /*, void *unused */ )
   87 {
   88    printf ("Name of the signal: %s\n", get_name_of_current_signal (box));
   89    printf ("It tells you that the user has dismissed the selection box\n");
   90 
   91    return 0;
   92 }
   93 
   94 enum
   95 {
   96    SHOW_BOX,
   97    SHOW_ACTUAL_X,
   98    SHOW_ACTUAL_Y,
   99    SHOW_MARKED_X,
  100    SHOW_MARKED_Y,
  101    SHOW_DELTA_X,
  102    SHOW_DELTA_Y,
  103    SHOW_NUM_ENTRIES
  104 };
  105 
  106 
  107 static GtkWidget *
  108 show_entry (GtkWidget * hbox, gchar * text)
  109 {
  110    GtkWidget *frame;
  111    GtkWidget *entry;
  112 
  113    frame = gtk_frame_new (text);
  114    gtk_container_add (GTK_CONTAINER (hbox), frame);
  115    entry = gtk_entry_new ();
  116    gtk_widget_set_size_request (entry, 20, -1);
  117    gtk_editable_set_editable (GTK_EDITABLE (entry), FALSE);
  118    gtk_container_add (GTK_CONTAINER (frame), entry);
  119 
  120    return entry;
  121 }
  122 
  123 static gint
  124 show_motion_notify_cb (GtkWidget ** entries, GdkEventMotion * event
  125                /*, GtkWidget *widget */ )
  126 {
  127    gfloat x, y;
  128    gchar *text;
  129    GtkDatabox *box = GTK_DATABOX (entries[SHOW_BOX]);
  130 
  131    x = gtk_databox_pixel_to_value_x (box, event->x);
  132    y = gtk_databox_pixel_to_value_y (box, event->y);
  133 
  134    text = g_strdup_printf ("%g", x);
  135    gtk_entry_set_text (GTK_ENTRY (entries[SHOW_ACTUAL_X]), text);
  136    g_free ((gpointer) text);
  137    text = g_strdup_printf ("%g", y);
  138    gtk_entry_set_text (GTK_ENTRY (entries[SHOW_ACTUAL_Y]), text);
  139    g_free ((gpointer) text);
  140 
  141    return FALSE;
  142 }
  143 
  144 static gint
  145 show_button_press_cb (GtkDatabox * box, GdkEventButton * event,
  146               GtkWidget ** entries)
  147 {
  148    gfloat x, y;
  149    gchar *text;
  150 
  151    if (!(event->button == 1 || event->button == 2))
  152       return FALSE;
  153 
  154    x = gtk_databox_pixel_to_value_x (box, event->x);
  155    y = gtk_databox_pixel_to_value_y (box, event->y);
  156 
  157    text = g_strdup_printf ("%g", x);
  158    gtk_entry_set_text (GTK_ENTRY (entries[SHOW_MARKED_X]), text);
  159    g_free ((gpointer) text);
  160    text = g_strdup_printf ("%g", y);
  161    gtk_entry_set_text (GTK_ENTRY (entries[SHOW_MARKED_Y]), text);
  162    g_free ((gpointer) text);
  163 
  164    return FALSE;
  165 }
  166 
  167 static void
  168 show_changed_cb (GtkDatabox * box,
  169          GtkDataboxValueRectangle * selectionValues,
  170          GtkWidget ** entries)
  171 {
  172    gchar *text;
  173    g_return_if_fail (GTK_IS_DATABOX (box));
  174 
  175    text = g_strdup_printf ("%g", selectionValues->x2 - selectionValues->x1);
  176    gtk_entry_set_text (GTK_ENTRY (entries[SHOW_DELTA_X]), text);
  177    g_free ((gpointer) text);
  178    text = g_strdup_printf ("%g", selectionValues->y2 - selectionValues->y1);
  179    gtk_entry_set_text (GTK_ENTRY (entries[SHOW_DELTA_Y]), text);
  180    g_free ((gpointer) text);
  181 
  182    text = g_strdup_printf ("%g", selectionValues->x2);
  183    gtk_entry_set_text (GTK_ENTRY (entries[SHOW_ACTUAL_X]), text);
  184    g_free ((gpointer) text);
  185    text = g_strdup_printf ("%g", selectionValues->y2);
  186    gtk_entry_set_text (GTK_ENTRY (entries[SHOW_ACTUAL_Y]), text);
  187    g_free ((gpointer) text);
  188 }
  189 
  190 
  191 
  192 static void
  193 create_signals (void)
  194 {
  195    GtkWidget *window = NULL;
  196    GtkWidget *box1;
  197    GtkWidget *box2;
  198    GtkWidget *close_button;
  199    GtkWidget *box;
  200    GtkWidget *table;
  201    GtkWidget *label;
  202    GtkWidget *separator;
  203    gfloat *X;
  204    gfloat *Y;
  205    GtkDataboxGraph *graph;
  206    GdkRGBA color;
  207    gint i;
  208    GtkWidget **entries;
  209    GtkWidget *hbox;
  210 
  211    entries = g_new0 (GtkWidget *, SHOW_NUM_ENTRIES);
  212 
  213    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  214    gtk_widget_set_size_request (window, 500, 500);
  215 
  216    g_signal_connect (G_OBJECT (window), "destroy",
  217              G_CALLBACK (gtk_main_quit), NULL);
  218 
  219    gtk_window_set_title (GTK_WINDOW (window), "GtkDatabox: Signals Examples");
  220    gtk_container_set_border_width (GTK_CONTAINER (window), 0);
  221 
  222    box1 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
  223    gtk_container_add (GTK_CONTAINER (window), box1);
  224 
  225    label =
  226       gtk_label_new
  227       ("The output on the shell and in the text boxes below\nshow you the information that you can get\n by using signals.\n\nSee basics for a usage of this window...");
  228 
  229    gtk_box_pack_start (GTK_BOX (box1), label, FALSE, FALSE, 0);
  230    separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
  231    gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, FALSE, 0);
  232 
  233    hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 3);
  234    gtk_box_pack_start (GTK_BOX (box1), hbox, FALSE, TRUE, 0);
  235 
  236    entries[SHOW_ACTUAL_X] = show_entry (hbox, "Actual X");
  237    entries[SHOW_ACTUAL_Y] = show_entry (hbox, "Actual Y");
  238    entries[SHOW_MARKED_X] = show_entry (hbox, "Marked X");
  239    entries[SHOW_MARKED_Y] = show_entry (hbox, "Marked Y");
  240    entries[SHOW_DELTA_X] = show_entry (hbox, "Delta X");
  241    entries[SHOW_DELTA_Y] = show_entry (hbox, "Delta Y");
  242 
  243    /* Create a GtkDatabox widget along with scrollbars and rulers */
  244    gtk_databox_create_box_with_scrollbars_and_rulers (&box, &table,
  245                               TRUE, TRUE, TRUE, TRUE);
  246 
  247    gtk_box_pack_start (GTK_BOX (box1), table, TRUE, TRUE, 0);
  248 
  249    entries[SHOW_BOX] = box;
  250 
  251    X = g_new0 (gfloat, POINTS);
  252    Y = g_new0 (gfloat, POINTS);
  253 
  254    for (i = 0; i < POINTS; i++)
  255    {
  256       X[i] = i+100.;
  257       Y[i] = 100. * sin (i * 2 * G_PI / POINTS);
  258    }
  259    color.red = 0;
  260    color.green = 1;
  261    color.blue = 0;
  262    color.alpha = 1;
  263 
  264    graph = gtk_databox_points_new (POINTS, X, Y, &color, 1);
  265    gtk_databox_graph_add (GTK_DATABOX (box), graph);
  266 
  267    Y = g_new0 (gfloat, POINTS);
  268 
  269    for (i = 0; i < POINTS; i++)
  270    {
  271       Y[i] = 100. * cos (i * 2 * G_PI / POINTS);
  272    }
  273    color.red = 1;
  274    color.green = 0;
  275    color.blue = 0;
  276    color.alpha = 1;
  277 
  278    graph = gtk_databox_points_new (POINTS, X, Y, &color, 1);
  279    gtk_databox_graph_add (GTK_DATABOX (box), graph);
  280 
  281    gtk_databox_auto_rescale (GTK_DATABOX (box), 0.00);
  282 
  283    separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
  284    gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
  285 
  286    box2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 10);
  287    gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
  288    gtk_box_pack_end (GTK_BOX (box1), box2, FALSE, TRUE, 0);
  289    close_button = gtk_button_new_with_label ("close");
  290    g_signal_connect_swapped (G_OBJECT (close_button), "clicked",
  291                  G_CALLBACK (gtk_main_quit), G_OBJECT (box));
  292    gtk_box_pack_start (GTK_BOX (box2), close_button, TRUE, TRUE, 0);
  293    gtk_widget_set_can_default(close_button, TRUE);
  294    gtk_widget_grab_default (close_button);
  295 
  296    g_signal_connect (G_OBJECT (box), "zoomed",
  297              G_CALLBACK (handle_signal_zoomed), NULL);
  298    g_signal_connect (G_OBJECT (box), "selection-started",
  299              G_CALLBACK (handle_signal_selection_started), NULL);
  300    g_signal_connect (G_OBJECT (box), "selection-finalized",
  301              G_CALLBACK (handle_signal_selection_finalized), NULL);
  302    g_signal_connect (G_OBJECT (box), "selection-canceled",
  303              G_CALLBACK (handle_signal_selection_canceled), NULL);
  304    g_signal_connect_swapped (G_OBJECT (box),
  305                  "motion_notify_event",
  306                  G_CALLBACK (show_motion_notify_cb), entries);
  307    g_signal_connect (G_OBJECT (box), "button_press_event",
  308              G_CALLBACK (show_button_press_cb), entries);
  309    g_signal_connect (G_OBJECT (box), "selection-changed",
  310              G_CALLBACK (show_changed_cb), entries);
  311 
  312    gtk_widget_show_all (window);
  313 
  314 }
  315 
  316 gint
  317 main (gint argc, char *argv[])
  318 {
  319    gtk_init (&argc, &argv);
  320 
  321    create_signals ();
  322    gtk_main ();
  323 
  324    return 0;
  325 }