Skip to content

Instantly share code, notes, and snippets.

@NuckChorris
Created June 15, 2013 08:07
Show Gist options
  • Select an option

  • Save NuckChorris/5787346 to your computer and use it in GitHub Desktop.

Select an option

Save NuckChorris/5787346 to your computer and use it in GitHub Desktop.
#include <gtk/gtk.h>
#include "main.h"
GtkWidget *window, *grid, *drawing_area, *spn_percent, *chk_critical, *chk_charging;
gboolean toggle_callback (GtkWidget *widget, gpointer data)
{
(void)(data);
(void)(widget);
gtk_widget_queue_draw(drawing_area);
return FALSE;
}
gboolean update_spinner (GtkWidget *widget, gpointer data)
{
(void)(data);
(void)(widget);
gtk_widget_queue_draw(drawing_area);
return FALSE;
}
gboolean battery_get_critical()
{
return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(chk_critical));
}
gboolean battery_get_charging()
{
return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(chk_charging));
}
gdouble battery_get_percent()
{
return gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(spn_percent)) / 100.0;
}
gboolean draw_callback (GtkWidget *widget, cairo_t *cr, gpointer data)
{
(void)(data);
guint width, height;
cairo_pattern_t *color;
// Grab our widget size
width = gtk_widget_get_allocated_width(widget);
height = gtk_widget_get_allocated_height(widget);
// Decide on a color
color = cairo_pattern_create_rgb(COLOR_DEFAULT);
if (battery_get_charging())
color = cairo_pattern_create_rgb(COLOR_CHARGING);
if (battery_get_critical())
color = cairo_pattern_create_rgb(COLOR_CRITICAL);
// Draw the main border of the battery
cairo_rectangle(cr, CENTERED(width, BATTERY_WIDTH),
CENTERED(height, BATTERY_HEIGHT),
BATTERY_WIDTH,
BATTERY_HEIGHT);
cairo_set_source(cr, color);
cairo_set_line_width(cr, STROKE_WIDTH);
cairo_stroke(cr);
// Draw the peg on the positive end of the battery
cairo_rectangle(cr, CENTERED(width, BATTERY_WIDTH) + BATTERY_WIDTH + (STROKE_WIDTH / 2),
CENTERED(height, PEG_HEIGHT),
PEG_WIDTH,
PEG_HEIGHT);
cairo_fill(cr);
// Draw the delicious cream filling
cairo_rectangle(cr, CENTERED(width, BATTERY_WIDTH),
CENTERED(height, BATTERY_HEIGHT),
(int)(BATTERY_WIDTH * battery_get_percent()),
BATTERY_HEIGHT);
cairo_fill(cr);
return FALSE;
}
static void activate (GtkApplication *app, gpointer user_data)
{
(void)(user_data);
GtkAdjustment *stepper_adjustment;
// Initialize the widgets
window = gtk_application_window_new(app);
gtk_window_set_title(GTK_WINDOW(window), "Battery Indicator");
gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
grid = gtk_grid_new();
gtk_container_add(GTK_CONTAINER(window), grid);
drawing_area = gtk_drawing_area_new();
gtk_grid_attach(GTK_GRID(grid), drawing_area, 0, 0, 1, 1);
gtk_widget_set_size_request(drawing_area, BATTERY_WIDTH + MARGIN, BATTERY_HEIGHT + MARGIN);
g_signal_connect(G_OBJECT(drawing_area), "draw", G_CALLBACK(draw_callback), NULL);
stepper_adjustment = gtk_adjustment_new(50, 0, 100, 1, 0, 0);
spn_percent = gtk_spin_button_new(stepper_adjustment, 1.0, 0);
gtk_grid_attach(GTK_GRID(grid), spn_percent, 0, 1, 1, 1);
g_signal_connect(G_OBJECT(spn_percent), "value-changed", G_CALLBACK(update_spinner), NULL);
chk_critical = gtk_check_button_new_with_label("Critical");
gtk_grid_attach(GTK_GRID(grid), chk_critical, 0, 2, 1, 1);
g_signal_connect(G_OBJECT(chk_critical), "toggled", G_CALLBACK(toggle_callback), NULL);
chk_charging = gtk_check_button_new_with_label("Charging");
gtk_grid_attach(GTK_GRID(grid), chk_charging, 0, 3, 1, 1);
g_signal_connect(G_OBJECT(chk_charging), "toggled", G_CALLBACK(toggle_callback), NULL);
// Display it
gtk_widget_show_all(window);
}
int main (int argc, char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new("org.gtk.gtkapplicationwindow-example", G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref (app);
return status;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment