1 | /* Pickers |
2 | * #Keywords: GtkColorChooser, GtkFontChooser, GtkApplicationChooser |
3 | * |
4 | * These widgets are mainly intended for use in preference dialogs. |
5 | * They allow to select colors, fonts, directories and applications. |
6 | * |
7 | * This demo shows both the default appearance for these dialogs, |
8 | * as well as some of the customizations that are possible. |
9 | */ |
10 | |
11 | #include <gtk/gtk.h> |
12 | |
13 | static gboolean |
14 | filter_font_cb (const PangoFontFamily *family, |
15 | const PangoFontFace *face, |
16 | gpointer data) |
17 | { |
18 | const char *alias_families[] = { |
19 | "Cursive" , |
20 | "Fantasy" , |
21 | "Monospace" , |
22 | "Sans" , |
23 | "Serif" , |
24 | "System-ui" , |
25 | NULL |
26 | }; |
27 | const char *family_name; |
28 | |
29 | family_name = pango_font_family_get_name (PANGO_FONT_FAMILY (family)); |
30 | |
31 | return g_strv_contains (strv: alias_families, str: family_name); |
32 | } |
33 | |
34 | #define COLOR(r,g,b) { r/255., g/255., b/255., 1.0 } |
35 | |
36 | GtkWidget * |
37 | do_pickers (GtkWidget *do_widget) |
38 | { |
39 | static GtkWidget *window = NULL; |
40 | GtkWidget *table, *label, *picker; |
41 | GdkRGBA solarized[] = { |
42 | COLOR (0xff, 0xff, 0xff), |
43 | COLOR (0x07, 0x36, 0x42), |
44 | COLOR (0xdc, 0x32, 0x2f), |
45 | COLOR (0x85, 0x99, 0x00), |
46 | COLOR (0xb5, 0x89, 0x00), |
47 | COLOR (0x26, 0x8b, 0xd2), |
48 | COLOR (0xd3, 0x36, 0x82), |
49 | COLOR (0x2a, 0xa1, 0x98), |
50 | COLOR (0xee, 0xe8, 0xd5), |
51 | |
52 | COLOR (0x00, 0x00, 0x00), |
53 | COLOR (0x00, 0x2b, 0x36), |
54 | COLOR (0xcb, 0x4b, 0x16), |
55 | COLOR (0x58, 0x6e, 0x75), |
56 | COLOR (0x65, 0x7b, 0x83), |
57 | COLOR (0x83, 0x94, 0x96), |
58 | COLOR (0x6c, 0x71, 0xc4), |
59 | COLOR (0x93, 0xa1, 0xa1), |
60 | COLOR (0xfd, 0xf6, 0xe3), |
61 | }; |
62 | |
63 | if (!window) |
64 | { |
65 | window = gtk_window_new (); |
66 | gtk_window_set_display (GTK_WINDOW (window), |
67 | display: gtk_widget_get_display (widget: do_widget)); |
68 | gtk_window_set_title (GTK_WINDOW (window), title: "Pickers" ); |
69 | g_object_add_weak_pointer (G_OBJECT (window), weak_pointer_location: (gpointer *)&window); |
70 | |
71 | table = gtk_grid_new (); |
72 | gtk_widget_set_margin_start (widget: table, margin: 20); |
73 | gtk_widget_set_margin_end (widget: table, margin: 20); |
74 | gtk_widget_set_margin_top (widget: table, margin: 20); |
75 | gtk_widget_set_margin_bottom (widget: table, margin: 20); |
76 | gtk_grid_set_row_spacing (GTK_GRID (table), spacing: 3); |
77 | gtk_grid_set_column_spacing (GTK_GRID (table), spacing: 10); |
78 | gtk_window_set_child (GTK_WINDOW (window), child: table); |
79 | |
80 | label = gtk_label_new (str: "Standard" ); |
81 | gtk_widget_add_css_class (widget: label, css_class: "title-4" ); |
82 | gtk_grid_attach (GTK_GRID (table), child: label, column: 1, row: -1, width: 1, height: 1); |
83 | label = gtk_label_new (str: "Custom" ); |
84 | gtk_widget_add_css_class (widget: label, css_class: "title-4" ); |
85 | gtk_grid_attach (GTK_GRID (table), child: label, column: 2, row: -1, width: 1, height: 1); |
86 | |
87 | label = gtk_label_new (str: "Color:" ); |
88 | gtk_widget_set_halign (widget: label, align: GTK_ALIGN_START); |
89 | gtk_widget_set_valign (widget: label, align: GTK_ALIGN_CENTER); |
90 | gtk_widget_set_hexpand (widget: label, TRUE); |
91 | gtk_grid_attach (GTK_GRID (table), child: label, column: 0, row: 0, width: 1, height: 1); |
92 | |
93 | picker = gtk_color_button_new (); |
94 | gtk_grid_attach (GTK_GRID (table), child: picker, column: 1, row: 0, width: 1, height: 1); |
95 | |
96 | picker = gtk_color_button_new (); |
97 | gtk_color_button_set_title (GTK_COLOR_BUTTON (picker), title: "Solarized colors" ); |
98 | gtk_color_chooser_add_palette (GTK_COLOR_CHOOSER (picker), |
99 | orientation: GTK_ORIENTATION_HORIZONTAL, |
100 | colors_per_line: 9, |
101 | n_colors: 18, |
102 | colors: solarized); |
103 | gtk_grid_attach (GTK_GRID (table), child: picker, column: 2, row: 0, width: 1, height: 1); |
104 | |
105 | label = gtk_label_new (str: "Font:" ); |
106 | gtk_widget_set_halign (widget: label, align: GTK_ALIGN_START); |
107 | gtk_widget_set_valign (widget: label, align: GTK_ALIGN_CENTER); |
108 | gtk_widget_set_hexpand (widget: label, TRUE); |
109 | gtk_grid_attach (GTK_GRID (table), child: label, column: 0, row: 1, width: 1, height: 1); |
110 | |
111 | picker = gtk_font_button_new (); |
112 | gtk_grid_attach (GTK_GRID (table), child: picker, column: 1, row: 1, width: 1, height: 1); |
113 | |
114 | picker = gtk_font_button_new (); |
115 | gtk_font_chooser_set_level (GTK_FONT_CHOOSER (picker), |
116 | level: GTK_FONT_CHOOSER_LEVEL_FAMILY | |
117 | GTK_FONT_CHOOSER_LEVEL_SIZE); |
118 | gtk_font_chooser_set_filter_func (GTK_FONT_CHOOSER (picker), filter: filter_font_cb, NULL, NULL); |
119 | |
120 | gtk_grid_attach (GTK_GRID (table), child: picker, column: 2, row: 1, width: 1, height: 1); |
121 | |
122 | label = gtk_label_new (str: "Mail:" ); |
123 | gtk_widget_set_halign (widget: label, align: GTK_ALIGN_START); |
124 | gtk_widget_set_valign (widget: label, align: GTK_ALIGN_CENTER); |
125 | gtk_widget_set_hexpand (widget: label, TRUE); |
126 | picker = gtk_app_chooser_button_new (content_type: "x-scheme-handler/mailto" ); |
127 | gtk_app_chooser_button_set_show_dialog_item (GTK_APP_CHOOSER_BUTTON (picker), TRUE); |
128 | gtk_grid_attach (GTK_GRID (table), child: label, column: 0, row: 3, width: 1, height: 1); |
129 | gtk_grid_attach (GTK_GRID (table), child: picker, column: 1, row: 3, width: 1, height: 1); |
130 | } |
131 | |
132 | if (!gtk_widget_get_visible (widget: window)) |
133 | gtk_widget_show (widget: window); |
134 | else |
135 | gtk_window_destroy (GTK_WINDOW (window)); |
136 | |
137 | return window; |
138 | } |
139 | |