GtkScale(1)
GtkScale(2)
GtkScale(3)
GtkScale(4)
GtkScale(5)
这篇将学习GtkScale
链接CSS相关操作。
例子
#include <gtk/gtk.h>
static void load_css(void);
static GtkWidget *create_window(const gint, const gint);
static gchar *format_value_callback(GtkScale *scale, gdouble value)
{
return g_strdup_printf("-->\%0.*f<--",
gtk_scale_get_digits(scale), value);
}
void value_changed_callback(GtkRange *scale)
{
if (GTK_IS_SCALE(scale))
{
gdouble value = gtk_range_get_value(scale);
g_print("%f\n", value);
}
}
void move_slider_callback(GtkRange *scale, GtkScrollType step)
{
if (GTK_IS_SCALE(scale))
{
if (step == GTK_SCROLL_STEP_UP)
g_print("GTK_SCROLL_STEP_UP: %f\n", gtk_range_get_value(scale));
else if (step == GTK_SCROLL_STEP_DOWN)
g_print("GTK_SCROOL_STEP_DOWN: %f\n", gtk_range_get_value(scale));
/// .....
}
}
int main(int argc, char *argv[])
{
gtk_init(&argc, &argv);
load_css();
GtkWidget *window;
GtkAdjustment *adjustment;
GtkWidget *scale;
/// *** Create a Window
window = create_window(400, 300);
/// *** Create the adjustment
adjustment = gtk_adjustment_new(0.0, 0.0, 10.0, 1.0, 2.0, 0.0);
/// *** Create the scale
scale = gtk_scale_new(GTK_ORIENTATION_HORIZONTAL, adjustment);
/// *** add mark
gtk_scale_add_mark(GTK_SCALE(scale), 3, GTK_POS_TOP, "<span>3</span>");
gtk_scale_add_mark(GTK_SCALE(scale), 6, GTK_POS_BOTTOM, "<span>6</span>");
/// *** properties
g_object_set(scale,
/// *** GtkScale 特性
"digits", 2,
"parent", window,
NULL);
/// *** signals
g_signal_connect_swapped(scale, "format-value", G_CALLBACK(format_value_callback), scale);
gtk_widget_show_all(window);
gtk_main();
}
static GtkWidget *create_window(const gint w, const gint h)
{
GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_window_set_default_size(GTK_WINDOW(window), w, h);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_title(GTK_WINDOW(window), "Hello GTK3");
gtk_container_set_border_width(GTK_CONTAINER(window), 50);
return window;
}
static void load_css(void)
{
GtkCssProvider *provider;
GdkDisplay *display;
GdkScreen *screen;
const char *css_style_file = "style.css";
GFile *css_fp = g_file_new_for_path(css_style_file);
GError *error = 0;
provider = gtk_css_provider_new();
display = gdk_display_get_default();
screen = gdk_display_get_default_screen(display);
gtk_style_context_add_provider_for_screen(screen, GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
gtk_css_provider_load_from_file(provider, css_fp, &error);
g_object_unref(provider);
}
// style.css
window {
background-color: cadetblue;
}
/*
scale {
background-color: coral;
}
scale:focus {
background-color: red;
}
scale:hover {
background-color: brown;
} */
scale marks.top {
background-color: orange;
color: palegoldenrod;
}
scale marks.bottom {
background-color:peachpuff;
color: black;
}
scale value {
background-color: blueviolet;
color: white;
font-size: 30px;
font-weight: bold;
}
scale mark > label {
background-color: blue;
color: white;
font-weight:lighter;
}
scale > contents > trough {
background-color: black;
}
scale > contents > trough > highlight {
background-color: white;
}
- 运行效果