Blame platform-demos/de/scale.c.page

Packit 1470ea
Packit 1470ea
<page xmlns="http://projectmallard.org/1.0/" xmlns:its="http://www.w3.org/2005/11/its" xmlns:xi="http://www.w3.org/2001/XInclude" type="guide" style="task" id="scale.c" xml:lang="de">
Packit 1470ea
  <info>
Packit 1470ea
    <title type="text">Scale (C)</title>
Packit 1470ea
    <link type="guide" xref="c#entry"/>
Packit 1470ea
    <link type="seealso" xref="grid.c"/>
Packit 1470ea
    <link type="seealso" xref="label.c"/>
Packit 1470ea
    <revision version="0.2" date="2012-07-04" status="draft"/>
Packit 1470ea
Packit 1470ea
    <credit type="author copyright">
Packit 1470ea
      <name>Monica Kochofar</name>
Packit 1470ea
      <email its:translate="no">monicakochofar@gmail.com</email>
Packit 1470ea
      <years>2012</years>
Packit 1470ea
    </credit>
Packit 1470ea
Packit 1470ea
    <desc>A slider widget for selecting a value from a range</desc>
Packit 1470ea
  
Packit 1470ea
    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
Packit 1470ea
      <mal:name>Mario Blättermann</mal:name>
Packit 1470ea
      <mal:email>mario.blaettermann@gmail.com</mal:email>
Packit 1470ea
      <mal:years>2011, 2013</mal:years>
Packit 1470ea
    </mal:credit>
Packit 1470ea
  </info>
Packit 1470ea
Packit 1470ea
  <title>Scale</title>
Packit 1470ea
Packit 1470ea
  <media type="image" mime="image/png" src="media/scale2.png"/>
Packit 1470ea
  

Slide the scales!

Packit 1470ea
Packit 1470ea
      
Packit 1470ea
#include <gtk/gtk.h>
Packit 1470ea
Packit 1470ea
Packit 1470ea
Packit 1470ea
/* This is the callback function. 
Packit 1470ea
 * It is a handler function which reacts to the signal. 
Packit 1470ea
 * In this case, it will notify the user the value of their scale as a label.
Packit 1470ea
 */
Packit 1470ea
static void
Packit 1470ea
hscale_moved (GtkRange *range,
Packit 1470ea
              gpointer  user_data)
Packit 1470ea
{
Packit 1470ea
   GtkWidget *label = user_data;
Packit 1470ea
Packit 1470ea
   /* Get the value of the range, and convert it into a string which will be
Packit 1470ea
    * used as a new label for the horizontal scale.
Packit 1470ea
    * %.0f - stands for a double that will have 0 decimal places.
Packit 1470ea
    */
Packit 1470ea
   gdouble pos = gtk_range_get_value (range);
Packit 1470ea
   /* Note: Using g_strdup_printf returns a string that must be freed. 
Packit 1470ea
    * (In which is done below)
Packit 1470ea
    */
Packit 1470ea
   gchar *str = g_strdup_printf ("Horizontal scale is %.0f", pos);
Packit 1470ea
   gtk_label_set_text (GTK_LABEL (label), str);
Packit 1470ea
Packit 1470ea
   g_free(str);
Packit 1470ea
}
Packit 1470ea
Packit 1470ea
Packit 1470ea
Packit 1470ea
/* This is the second callback function. It is a handler function which 
Packit 1470ea
 * reacts to the signal. It does the same thing as the function above, except with
Packit 1470ea
 * the vertical scale.
Packit 1470ea
 */
Packit 1470ea
vscale_moved (GtkRange *range,
Packit 1470ea
              gpointer  user_data)
Packit 1470ea
{
Packit 1470ea
   GtkWidget *label = user_data;
Packit 1470ea
   
Packit 1470ea
   gdouble pos = gtk_range_get_value (range);
Packit 1470ea
   /* %.1f - stands for a double that will have 1 decimal place */
Packit 1470ea
   gchar *str = g_strdup_printf ("Vertical scale is %.1f", pos);
Packit 1470ea
   gtk_label_set_text (GTK_LABEL (label), str);
Packit 1470ea
Packit 1470ea
   
Packit 1470ea
   g_free (str);
Packit 1470ea
}
Packit 1470ea
Packit 1470ea
Packit 1470ea
Packit 1470ea
static void
Packit 1470ea
activate (GtkApplication *app,
Packit 1470ea
          gpointer        user_data)
Packit 1470ea
{
Packit 1470ea
  /* Declare variables */
Packit 1470ea
  GtkWidget *window;
Packit 1470ea
  GtkWidget *h_scale;
Packit 1470ea
  GtkWidget *v_scale;
Packit 1470ea
  GtkWidget *hlabel;
Packit 1470ea
  GtkWidget *vlabel;
Packit 1470ea
  GtkWidget *grid;
Packit 1470ea
Packit 1470ea
  /* The Adjustment object represents a value 
Packit 1470ea
   * which has an associated lower and upper bound.
Packit 1470ea
   */
Packit 1470ea
  GtkAdjustment *hadjustment;
Packit 1470ea
  GtkAdjustment *vadjustment;
Packit 1470ea
Packit 1470ea
  /* Create a window with a title and a default size */
Packit 1470ea
  window = gtk_application_window_new (app);
Packit 1470ea
  gtk_window_set_title (GTK_WINDOW (window), "Scale Example");
Packit 1470ea
  gtk_window_set_default_size (GTK_WINDOW (window), 400, 300);
Packit 1470ea
  gtk_container_set_border_width (GTK_CONTAINER (window), 5);
Packit 1470ea
Packit 1470ea
  /* Two labels to be shown in the window */
Packit 1470ea
  hlabel = gtk_label_new ("Move the scale handle...");
Packit 1470ea
  vlabel = gtk_label_new ("Move the scale handle...");
Packit 1470ea
Packit 1470ea
   
Packit 1470ea
  /* gtk_adjustment_new takes six parameters, three of which 
Packit 1470ea
   * may be difficult to understand:
Packit 1470ea
   * step increment- move the handle with the arrow keys on your keyboard to see.
Packit 1470ea
   * page increment - move the handle by clicking away from it 
Packit 1470ea
   * on the scale to see.
Packit 1470ea
   * page size - not used here.
Packit 1470ea
   */
Packit 1470ea
  hadjustment = gtk_adjustment_new (0, 0, 100, 5, 10, 0);
Packit 1470ea
  vadjustment = gtk_adjustment_new (50, 0, 100, 5, 10, 0); 
Packit 1470ea
Packit 1470ea
  /* Create the Horizontal scale, making sure the 
Packit 1470ea
   * digits used have no decimals.
Packit 1470ea
   */
Packit 1470ea
  h_scale = gtk_scale_new (GTK_ORIENTATION_HORIZONTAL, hadjustment);
Packit 1470ea
  gtk_scale_set_digits (GTK_SCALE (h_scale), 0); 
Packit 1470ea
Packit 1470ea
  /* Allow it to expand horizontally (if there's space), and 
Packit 1470ea
   * set the vertical alignment
Packit 1470ea
   */
Packit 1470ea
  gtk_widget_set_hexpand (h_scale, TRUE);
Packit 1470ea
  gtk_widget_set_valign (h_scale, GTK_ALIGN_START);
Packit 1470ea
  
Packit 1470ea
  /* Connecting the "value-changed" signal for the horizontal scale 
Packit 1470ea
   * to the appropriate callback function. 
Packit 1470ea
   * take note that GtkRange is part of GtkScale's Object Hierarchy.
Packit 1470ea
   */
Packit 1470ea
  g_signal_connect (h_scale, 
Packit 1470ea
                    "value-changed", 
Packit 1470ea
                    G_CALLBACK (hscale_moved), 
Packit 1470ea
                    hlabel);
Packit 1470ea
Packit 1470ea
Packit 1470ea
Packit 1470ea
  /* Create the Vertical scale. This time, we will see what happens 
Packit 1470ea
   * when the digits arent initially set.
Packit 1470ea
   */
Packit 1470ea
  v_scale = gtk_scale_new (GTK_ORIENTATION_VERTICAL, vadjustment);
Packit 1470ea
  gtk_widget_set_vexpand (v_scale, TRUE);
Packit 1470ea
Packit 1470ea
  /* Connecting the "value-changed" signal for the vertical scale to 
Packit 1470ea
   * the appropriate callback function.
Packit 1470ea
   */
Packit 1470ea
  g_signal_connect (v_scale, 
Packit 1470ea
                    "value-changed", 
Packit 1470ea
                    G_CALLBACK (vscale_moved), 
Packit 1470ea
                    vlabel);
Packit 1470ea
Packit 1470ea
  /* Create a grid and arrange everything accordingly */
Packit 1470ea
  grid = gtk_grid_new ();
Packit 1470ea
  gtk_grid_set_column_spacing (GTK_GRID (grid), 10);
Packit 1470ea
  gtk_grid_set_column_homogeneous (GTK_GRID (grid), TRUE);
Packit 1470ea
  gtk_grid_attach (GTK_GRID (grid), h_scale, 0, 0, 1, 1);
Packit 1470ea
  gtk_grid_attach (GTK_GRID (grid), v_scale, 1, 0, 1, 1);
Packit 1470ea
  gtk_grid_attach (GTK_GRID (grid), hlabel, 0, 1, 1, 1);
Packit 1470ea
  gtk_grid_attach (GTK_GRID (grid), vlabel, 1, 1, 1, 1);
Packit 1470ea
  
Packit 1470ea
Packit 1470ea
  gtk_container_add (GTK_CONTAINER (window), grid);
Packit 1470ea
Packit 1470ea
  gtk_widget_show_all (window);
Packit 1470ea
}
Packit 1470ea
Packit 1470ea
Packit 1470ea
Packit 1470ea
int
Packit 1470ea
main (int argc, char **argv)
Packit 1470ea
{
Packit 1470ea
  GtkApplication *app;
Packit 1470ea
  int status;
Packit 1470ea
Packit 1470ea
  app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
Packit 1470ea
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
Packit 1470ea
  status = g_application_run (G_APPLICATION (app), argc, argv);
Packit 1470ea
  g_object_unref (app);
Packit 1470ea
Packit 1470ea
  return status;
Packit 1470ea
}
Packit 1470ea
Packit 1470ea

Packit 1470ea
  In this sample we used the following:
Packit 1470ea

Packit 1470ea
<list>
Packit 1470ea
  <item>

<link href="http://developer.gnome.org/gtk3/3.4/GtkApplication.html">GtkApplication</link>

</item>
Packit 1470ea
  <item>

<link href="http://developer.gnome.org/gtk3/3.4/GtkWindow.html">GtkWindow</link>

</item>
Packit 1470ea
  <item>

<link href="http://developer.gnome.org/gtk3/stable/GtkScale.html">GtkScale</link>

</item>
Packit 1470ea
  <item>

<link href="http://developer.gnome.org/gtk3/stable/GtkRange.html">GtkRange</link>

</item>
Packit 1470ea
  <item>

<link href="http://developer.gnome.org/gtk3/stable/GtkAdjustment.html">GtkAdjustment</link>

</item>
Packit 1470ea
  <item>

<link href="http://developer.gnome.org/glib/stable/glib-String-Utility-Functions.html#g-strdup-printf">String Utility Functions</link>

</item>
Packit 1470ea
</list>
Packit 1470ea
</page>