"Fossies" - the Fresh Open Source Software Archive

Member "gtkdatabox-1.0.0/examples/keycontrol.c" (31 Mar 2021, 6253 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 "keycontrol.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: keycontrol.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_lines.h>
   24 #include <gtkdatabox_markers.h>
   25 #include <gdk/gdkkeysyms.h>
   26 #include <glib/gprintf.h>
   27 #include <math.h>
   28 
   29 #define POINTS 10
   30 
   31 /*----------------------------------------------------------------
   32  *  databox basics
   33  *----------------------------------------------------------------*/
   34 
   35 gfloat *X;
   36 gfloat *Y;
   37 gfloat *markX;
   38 gfloat *markY;
   39 guint markIndex;
   40 gchar markLabel[100];
   41 
   42 static void
   43 shift_markers (GtkDatabox * box, GtkDataboxMarkers * markers)
   44 {
   45    markX[0] = X[markIndex];
   46    markY[0] = Y[markIndex];
   47 
   48    g_sprintf (markLabel, "(%2.2f, %2.2f)", X[markIndex], Y[markIndex]);
   49    gtk_databox_markers_set_label (markers, 1,
   50                  GTK_DATABOX_MARKERS_TEXT_SW, markLabel, TRUE);
   51    gtk_widget_queue_draw (GTK_WIDGET(box));
   52 }
   53 
   54 static gint
   55 key_press_cb (GtkDatabox * box,
   56           GdkEventKey * event, GtkDataboxMarkers * markers)
   57 {
   58    if (event->type != GDK_KEY_PRESS)
   59       return FALSE;
   60 
   61    switch (event->keyval)
   62    {
   63    case GDK_KEY_KP_Right:
   64       if (markIndex < POINTS - 1)
   65       {
   66      markIndex++;
   67      shift_markers (box, markers);
   68       }
   69       break;
   70    case GDK_KEY_KP_Left:
   71       if (markIndex > 0)
   72       {
   73      markIndex--;
   74      shift_markers (box, markers);
   75       }
   76       break;
   77    case GDK_KEY_c:;
   78       break;
   79    case GDK_KEY_n:
   80       break;
   81    case GDK_KEY_p:
   82       g_printerr ("p");
   83       break;
   84    case GDK_KEY_h:
   85       gtk_databox_zoom_home (box);
   86       break;
   87    case GDK_KEY_o:
   88       gtk_databox_zoom_out (box);
   89       break;
   90    default:
   91       break;
   92    }
   93 
   94    return FALSE;
   95 }
   96 
   97 
   98 static void
   99 create_basics (void)
  100 {
  101    GtkWidget *window = NULL;
  102    GtkWidget *box1;
  103    GtkWidget *box2;
  104    GtkWidget *close_button;
  105    GtkWidget *box;
  106    GtkWidget *label;
  107    GtkWidget *separator;
  108    GtkWidget *table;
  109    GtkDataboxGraph *graph;
  110    GdkRGBA color;
  111    gfloat left, right, top, bottom;
  112    gint i;
  113 
  114    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  115    gtk_widget_set_size_request (window, 400, 400);
  116 
  117    g_signal_connect (G_OBJECT (window), "destroy",
  118              G_CALLBACK (gtk_main_quit), NULL);
  119 
  120    gtk_window_set_title (GTK_WINDOW (window), "GtkDatabox: Key Controls");
  121    gtk_container_set_border_width (GTK_CONTAINER (window), 0);
  122 
  123    box1 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
  124    gtk_container_add (GTK_CONTAINER (window), box1);
  125 
  126    label =
  127       gtk_label_new
  128       ("As long as the GtkDatabox has the focus,\nthe following keys will work in this example:\nKeypad-Right: Move markers to next data point\nKeypad-Left: Move markers to previous data point\no: zoom out (only useful if you zoomed in previously)\nh: zoom_home (only useful if you zoomed in previously)");
  129    gtk_box_pack_start (GTK_BOX (box1), label, FALSE, FALSE, 0);
  130    separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
  131    gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, FALSE, 0);
  132 
  133    gtk_databox_create_box_with_scrollbars_and_rulers (&box, &table,
  134                               TRUE, TRUE, TRUE, TRUE);
  135    gtk_widget_add_events (box, GDK_KEY_PRESS_MASK);
  136    gtk_widget_set_can_focus(box, TRUE);
  137    gtk_widget_set_can_default(box, TRUE);
  138 
  139    gtk_box_pack_start (GTK_BOX (box1), table, TRUE, TRUE, 0);
  140 
  141    gtk_databox_set_bg_color (GTK_DATABOX (box), "#545454");
  142 
  143    X = g_new0 (gfloat, POINTS);
  144    Y = g_new0 (gfloat, POINTS);
  145 
  146    for (i = 0; i < POINTS; i++)
  147    {
  148       X[i] = i;
  149       Y[i] = 100. * sin (i * 2 * G_PI / (POINTS - 1));
  150    }
  151    color.red = 0;
  152    color.green = 1;
  153    color.blue = 0;
  154    color.alpha = 1;
  155 
  156    graph = gtk_databox_lines_new (POINTS, X, Y, &color, 1);
  157    gtk_databox_graph_add (GTK_DATABOX (box), graph);
  158 
  159    markX = g_new0 (gfloat, 2);
  160    markY = g_new0 (gfloat, 2);
  161 
  162    color.red = 1;
  163    color.green = 0;
  164    color.blue = 0;
  165    color.alpha = 1;
  166 
  167    graph = gtk_databox_markers_new (2, markX, markY, &color, 7,
  168                    GTK_DATABOX_MARKERS_TRIANGLE);
  169    gtk_databox_markers_set_position (GTK_DATABOX_MARKERS (graph), 0,
  170                     GTK_DATABOX_MARKERS_S);
  171    gtk_databox_markers_set_position (GTK_DATABOX_MARKERS (graph), 1,
  172                     GTK_DATABOX_MARKERS_N);
  173    markIndex = 0;
  174    shift_markers (GTK_DATABOX (box), GTK_DATABOX_MARKERS (graph));
  175    gtk_databox_graph_add (GTK_DATABOX (box), graph);
  176 
  177    g_signal_connect (G_OBJECT (box), "key_press_event",
  178              G_CALLBACK (key_press_cb), graph);
  179 
  180    gtk_databox_auto_rescale (GTK_DATABOX (box), 0.05);
  181 
  182    gtk_databox_get_total_limits (GTK_DATABOX (box), &left, &right, &top,
  183                  &bottom);
  184 
  185    markX[1] = right;
  186    markY[1] = top;
  187 
  188    separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
  189    gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
  190 
  191    box2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 10);
  192    gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
  193    gtk_box_pack_end (GTK_BOX (box1), box2, FALSE, TRUE, 0);
  194    close_button = gtk_button_new_with_label ("close");
  195    g_signal_connect_swapped (G_OBJECT (close_button), "clicked",
  196                  G_CALLBACK (gtk_main_quit), G_OBJECT (box));
  197    gtk_box_pack_start (GTK_BOX (box2), close_button, TRUE, TRUE, 0);
  198    gtk_widget_grab_default (box);
  199    gtk_widget_grab_focus (box);
  200 
  201    gtk_widget_show_all (window);
  202    gdk_window_set_cursor (gtk_widget_get_window(box), gdk_cursor_new_for_display (gdk_display_get_default (), GDK_CROSS));
  203 }
  204 
  205 gint
  206 main (gint argc, char *argv[])
  207 {
  208    gtk_init (&argc, &argv);
  209 
  210    create_basics ();
  211    gtk_main ();
  212 
  213    return 0;
  214 }