Advertisement
Mark2020H

GTK C file For instrumentation Meter Part 2

Oct 31st, 2020
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.04 KB | None | 0 0
  1. /* GTK C file For instrumentation Meter Part 2
  2. MD Harrington  */
  3.  
  4.  
  5. //  g++ or gcc -Wall -Wextra -o main main.cpp $(pkg-config gtk+-3.0 --cflags --libs) -lm -rdynamic
  6.  
  7.  #include <gtk/gtk.h>
  8.  #include <stdlib.h>
  9.  #include <math.h>
  10.  
  11.  
  12.  
  13.  
  14.  void quitButtonClicked(__attribute__((unused)) GtkWidget *widget,
  15.                         __attribute__((unused)) gpointer   data)
  16.  {
  17.      g_print("Quit Clicked\n");
  18.      gtk_main_quit();
  19.  }
  20.  
  21.  
  22.  // Handle the user trying to close the window
  23.  gboolean windowDelete(__attribute__((unused)) GtkWidget *widget,
  24.                        __attribute__((unused)) GdkEvent  *event,
  25.                        __attribute__((unused)) gpointer   data)
  26.  {
  27.      return TRUE;    // Returning TRUE stops the window being deleted.
  28.                      // Returning FALSE allows deletion.  
  29.  }
  30.  
  31.  // This just causes the drawing area to be redrawn when the slider is moved.
  32.  // user_data is a pointer to drawingarea1
  33.  void on_scale1_value_changed(__attribute__((unused)) GtkRange *range,
  34.                               gpointer  user_data)
  35.  {
  36.      
  37.      gtk_widget_queue_draw(GTK_WIDGET(user_data));
  38.  }
  39.  
  40.  
  41.  // user_data is a pointer to slider1
  42.  gboolean on_drawingarea1_draw(__attribute__((unused)) GtkWidget *widget,
  43.                                       cairo_t *cr,
  44.                                       gpointer user_data)
  45.  {
  46.      gdouble marks;
  47.      char text[10];
  48.      
  49.      static PangoLayout *layout = NULL;  // Retained between calls
  50.      PangoFontDescription *desc;
  51.      int width,height;
  52.  
  53.      gdouble value;
  54.  
  55.      // Do some pango setup on the first call.
  56.      if(layout == NULL)
  57.      {
  58.          layout = pango_cairo_create_layout (cr);
  59.          desc = pango_font_description_from_string ("Sans Bold 15");
  60.          pango_layout_set_font_description (layout, desc);
  61.          pango_font_description_free (desc);
  62.      }
  63.  
  64.      cairo_set_source_rgb(cr, 0, 0, 0);
  65.      cairo_set_line_width(cr, 1.0);
  66.  
  67.      // Set origin as middle of bottom edge of the drawing area.
  68.      // Window is not resizable so "magic numbers" will work here.
  69.      cairo_translate(cr,200,190);
  70.  
  71.  
  72.      // Draw the black radial scale marks and labels
  73.      for(marks = 0.0; marks <= 100.0; marks += 10.0)
  74.      {
  75.          // Draw the radial marks
  76.          cairo_move_to(cr, 150.0 * cos(M_PI * marks * 0.01),   -150.0 * sin(M_PI * marks * 0.01) );
  77.          cairo_line_to(cr, 160.0 * cos(M_PI * marks * 0.01),   -160.0 * sin(M_PI * marks * 0.01) );
  78.  
  79.          // Set the text to print
  80.          sprintf(text,"%2.0f", marks);
  81.          pango_layout_set_text (layout, text, -1);
  82.  
  83.          // Redo layout for new text
  84.          pango_cairo_update_layout (cr, layout);
  85.  
  86.          // Retrieve the size of the rendered text
  87.          pango_layout_get_size (layout, &width, &height);
  88.  
  89.          // Position the text at the end of the radial marks
  90.          cairo_move_to(cr,
  91.                        (-180.0 * cos(M_PI *marks * 0.01)) - ((double)width /PANGO_SCALE / 2.0),
  92.                        (-180.0 * sin(M_PI *marks * 0.01)) - ((double)height/PANGO_SCALE / 2.0));
  93.          // Render the text
  94.          pango_cairo_show_layout (cr, layout);
  95.      }
  96.      cairo_stroke(cr);
  97.  
  98.      // Retrieve the new slider value
  99.      value = gtk_range_get_value(GTK_RANGE(user_data));
  100.  
  101.      cairo_set_source_rgb(cr, 1.0, 0, 0);
  102.      cairo_set_line_width(cr, 1.5);
  103.  
  104.      // Draw the meter pointer
  105.      cairo_move_to(cr, 0.0,0.0);
  106.      cairo_line_to(cr, -150.0 * cos(M_PI *value * 0.01),  -150.0 * sin(M_PI *value * 0.01) );
  107.      cairo_stroke(cr);  
  108.  
  109.      return FALSE;
  110.  }
  111.  
  112.  
  113.  // main is a generic version for a glade project.
  114.  
  115. int main(int argc, char **argv)
  116. {
  117.     GtkBuilder *builder = NULL;
  118.  
  119.     gtk_init (&argc , &argv);
  120.  
  121.     builder = gtk_builder_new();
  122.  
  123.     if( gtk_builder_add_from_file (builder,"Meter1.glade" , NULL) == 0)
  124.     {
  125.          printf("gtk_builder_add_from_file FAILED\n");
  126.          return EXIT_FAILURE;
  127.     }
  128.  
  129.     gtk_builder_connect_signals(builder,NULL);
  130.  
  131.     gtk_main ();
  132.     return EXIT_SUCCESS;
  133. }
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement