1 | #include <gtk/gtk.h> |
2 | #include <stdio.h> |
3 | |
4 | static void |
5 | clear_pressed (GtkEntry *entry, int icon, gpointer data) |
6 | { |
7 | if (icon == GTK_ENTRY_ICON_SECONDARY) |
8 | gtk_editable_set_text (GTK_EDITABLE (entry), text: "" ); |
9 | } |
10 | |
11 | static void |
12 | set_blank (GtkWidget *button, |
13 | GtkEntry *entry) |
14 | { |
15 | if (gtk_check_button_get_active (GTK_CHECK_BUTTON (button))) |
16 | gtk_entry_set_icon_from_icon_name (entry, icon_pos: GTK_ENTRY_ICON_SECONDARY, NULL); |
17 | } |
18 | |
19 | static void |
20 | set_icon_name (GtkWidget *button, |
21 | GtkEntry *entry) |
22 | { |
23 | if (gtk_check_button_get_active (GTK_CHECK_BUTTON (button))) |
24 | gtk_entry_set_icon_from_icon_name (entry, icon_pos: GTK_ENTRY_ICON_SECONDARY, icon_name: "media-floppy" ); |
25 | } |
26 | |
27 | static void |
28 | set_gicon (GtkWidget *button, |
29 | GtkEntry *entry) |
30 | { |
31 | GIcon *icon; |
32 | |
33 | if (gtk_check_button_get_active (GTK_CHECK_BUTTON (button))) |
34 | { |
35 | icon = g_themed_icon_new (iconname: "gtk-yes" ); |
36 | gtk_entry_set_icon_from_gicon (entry, icon_pos: GTK_ENTRY_ICON_SECONDARY, icon); |
37 | g_object_unref (object: icon); |
38 | } |
39 | } |
40 | |
41 | static void |
42 | set_texture (GtkWidget *button, |
43 | GtkEntry *entry) |
44 | { |
45 | GdkTexture *texture; |
46 | |
47 | if (gtk_check_button_get_active (GTK_CHECK_BUTTON (button))) |
48 | { |
49 | texture = gdk_texture_new_from_resource (resource_path: "/org/gtk/libgtk/icons/32x32/places/network-workgroup.png" ); |
50 | gtk_entry_set_icon_from_paintable (entry, icon_pos: GTK_ENTRY_ICON_SECONDARY, paintable: GDK_PAINTABLE (ptr: texture)); |
51 | g_object_unref (object: texture); |
52 | } |
53 | } |
54 | |
55 | static const char cssdata[] = |
56 | ".entry-frame:not(:focus) { " |
57 | " border: 2px solid alpha(gray,0.3);" |
58 | "}" |
59 | ".entry-frame:focus { " |
60 | " border: 2px solid red;" |
61 | "}" |
62 | ".entry-frame entry { " |
63 | " border: none; " |
64 | " box-shadow: none; " |
65 | "}" ; |
66 | |
67 | static void |
68 | icon_pressed_cb (GtkGesture *gesture, |
69 | int n_press, |
70 | double x, |
71 | double y, |
72 | gpointer data) |
73 | { |
74 | g_print (format: "You clicked me!\n" ); |
75 | } |
76 | |
77 | static void |
78 | quit_cb (GtkWidget *widget, |
79 | gpointer data) |
80 | { |
81 | gboolean *done = data; |
82 | |
83 | *done = TRUE; |
84 | |
85 | g_main_context_wakeup (NULL); |
86 | } |
87 | |
88 | int |
89 | main (int argc, char **argv) |
90 | { |
91 | GtkWidget *window; |
92 | GtkWidget *grid; |
93 | GtkWidget *label; |
94 | GtkWidget *entry; |
95 | GtkWidget *box; |
96 | GtkWidget *image; |
97 | GtkWidget *button1; |
98 | GtkWidget *button2; |
99 | GtkWidget *button3; |
100 | GtkWidget *button4; |
101 | GIcon *icon; |
102 | GdkContentProvider *content; |
103 | gboolean done = FALSE; |
104 | |
105 | gtk_init (); |
106 | |
107 | window = gtk_window_new (); |
108 | gtk_window_set_title (GTK_WINDOW (window), title: "Gtk Entry Icons Test" ); |
109 | |
110 | g_signal_connect (G_OBJECT (window), "destroy" , |
111 | G_CALLBACK (quit_cb), &done); |
112 | |
113 | grid = gtk_grid_new (); |
114 | gtk_window_set_child (GTK_WINDOW (window), child: grid); |
115 | gtk_grid_set_row_spacing (GTK_GRID (grid), spacing: 6); |
116 | gtk_grid_set_column_spacing (GTK_GRID (grid), spacing: 6); |
117 | gtk_widget_set_margin_start (widget: grid, margin: 10); |
118 | gtk_widget_set_margin_end (widget: grid, margin: 10); |
119 | gtk_widget_set_margin_top (widget: grid, margin: 10); |
120 | gtk_widget_set_margin_bottom (widget: grid, margin: 10); |
121 | |
122 | /* |
123 | * Open File - Sets the icon using a GIcon |
124 | */ |
125 | label = gtk_label_new (str: "Open File:" ); |
126 | gtk_grid_attach (GTK_GRID (grid), child: label, column: 0, row: 0, width: 1, height: 1); |
127 | gtk_widget_set_halign (widget: label, align: GTK_ALIGN_START); |
128 | gtk_widget_set_valign (widget: label, align: GTK_ALIGN_CENTER); |
129 | |
130 | entry = gtk_entry_new (); |
131 | gtk_widget_set_hexpand (widget: entry, TRUE); |
132 | gtk_grid_attach (GTK_GRID (grid), child: entry, column: 1, row: 0, width: 1, height: 1); |
133 | |
134 | icon = g_themed_icon_new (iconname: "folder-symbolic" ); |
135 | g_themed_icon_append_name (G_THEMED_ICON (icon), iconname: "folder-symbolic" ); |
136 | |
137 | gtk_entry_set_icon_from_gicon (GTK_ENTRY (entry), |
138 | icon_pos: GTK_ENTRY_ICON_PRIMARY, |
139 | icon); |
140 | g_object_unref (object: icon); |
141 | gtk_entry_set_icon_sensitive (GTK_ENTRY (entry), |
142 | icon_pos: GTK_ENTRY_ICON_PRIMARY, |
143 | FALSE); |
144 | |
145 | gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry), |
146 | icon_pos: GTK_ENTRY_ICON_PRIMARY, |
147 | tooltip: "Open a file" ); |
148 | |
149 | /* |
150 | * Save File - sets the icon using an icon name. |
151 | */ |
152 | label = gtk_label_new (str: "Save File:" ); |
153 | gtk_grid_attach (GTK_GRID (grid), child: label, column: 0, row: 1, width: 1, height: 1); |
154 | gtk_widget_set_halign (widget: label, align: GTK_ALIGN_START); |
155 | gtk_widget_set_valign (widget: label, align: GTK_ALIGN_CENTER); |
156 | |
157 | entry = gtk_entry_new (); |
158 | gtk_widget_set_hexpand (widget: entry, TRUE); |
159 | gtk_grid_attach (GTK_GRID (grid), child: entry, column: 1, row: 1, width: 1, height: 1); |
160 | gtk_editable_set_text (GTK_EDITABLE (entry), text: "ā€¸Right-to-left" ); |
161 | gtk_widget_set_direction (widget: entry, dir: GTK_TEXT_DIR_RTL); |
162 | |
163 | gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry), |
164 | icon_pos: GTK_ENTRY_ICON_PRIMARY, |
165 | icon_name: "document-save-symbolic" ); |
166 | gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry), |
167 | icon_pos: GTK_ENTRY_ICON_PRIMARY, |
168 | tooltip: "Save a file" ); |
169 | |
170 | content = gdk_content_provider_new_typed (G_TYPE_STRING, "Amazing" ); |
171 | gtk_entry_set_icon_drag_source (GTK_ENTRY (entry), |
172 | icon_pos: GTK_ENTRY_ICON_PRIMARY, |
173 | provider: content, actions: GDK_ACTION_COPY); |
174 | g_object_unref (object: content); |
175 | |
176 | /* |
177 | * Search - Uses a helper function |
178 | */ |
179 | label = gtk_label_new (str: "Search:" ); |
180 | gtk_grid_attach (GTK_GRID (grid), child: label, column: 0, row: 2, width: 1, height: 1); |
181 | gtk_widget_set_halign (widget: label, align: GTK_ALIGN_START); |
182 | gtk_widget_set_valign (widget: label, align: GTK_ALIGN_CENTER); |
183 | |
184 | entry = gtk_entry_new (); |
185 | gtk_widget_set_hexpand (widget: entry, TRUE); |
186 | gtk_grid_attach (GTK_GRID (grid), child: entry, column: 1, row: 2, width: 1, height: 1); |
187 | |
188 | gtk_entry_set_placeholder_text (GTK_ENTRY (entry), |
189 | text: "Type some text, then click an icon" ); |
190 | |
191 | gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry), |
192 | icon_pos: GTK_ENTRY_ICON_PRIMARY, |
193 | icon_name: "edit-find-symbolic" ); |
194 | |
195 | gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry), |
196 | icon_pos: GTK_ENTRY_ICON_PRIMARY, |
197 | tooltip: "Clicking the other icon is more interesting!" ); |
198 | |
199 | gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry), |
200 | icon_pos: GTK_ENTRY_ICON_SECONDARY, |
201 | icon_name: "edit-clear-symbolic" ); |
202 | |
203 | gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry), |
204 | icon_pos: GTK_ENTRY_ICON_SECONDARY, |
205 | tooltip: "Clear" ); |
206 | |
207 | g_signal_connect (entry, "icon-press" , G_CALLBACK (clear_pressed), NULL); |
208 | |
209 | /* |
210 | * Password - Sets the icon using an icon name |
211 | */ |
212 | label = gtk_label_new (str: "Password:" ); |
213 | gtk_grid_attach (GTK_GRID (grid), child: label, column: 0, row: 3, width: 1, height: 1); |
214 | gtk_widget_set_halign (widget: label, align: GTK_ALIGN_START); |
215 | gtk_widget_set_valign (widget: label, align: GTK_ALIGN_CENTER); |
216 | |
217 | entry = gtk_password_entry_new (); |
218 | gtk_password_entry_set_show_peek_icon (GTK_PASSWORD_ENTRY (entry), TRUE); |
219 | gtk_widget_set_hexpand (widget: entry, TRUE); |
220 | gtk_grid_attach (GTK_GRID (grid), child: entry, column: 1, row: 3, width: 1, height: 1); |
221 | |
222 | /* Name - Does not set any icons. */ |
223 | label = gtk_label_new (str: "Name:" ); |
224 | gtk_grid_attach (GTK_GRID (grid), child: label, column: 0, row: 4, width: 1, height: 1); |
225 | gtk_widget_set_halign (widget: label, align: GTK_ALIGN_START); |
226 | gtk_widget_set_valign (widget: label, align: GTK_ALIGN_CENTER); |
227 | |
228 | entry = gtk_entry_new (); |
229 | gtk_widget_set_hexpand (widget: entry, TRUE); |
230 | gtk_entry_set_placeholder_text (GTK_ENTRY (entry), |
231 | text: "Use the RadioButtons to choose an icon" ); |
232 | gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry), |
233 | icon_pos: GTK_ENTRY_ICON_SECONDARY, |
234 | tooltip: "Use the RadioButtons to change this icon" ); |
235 | gtk_grid_attach (GTK_GRID (grid), child: entry, column: 1, row: 4, width: 1, height: 1); |
236 | |
237 | box = gtk_box_new (orientation: GTK_ORIENTATION_HORIZONTAL, spacing: 6); |
238 | gtk_widget_set_vexpand (GTK_WIDGET (box), TRUE); |
239 | gtk_grid_attach (GTK_GRID (grid), child: box, column: 0, row: 5, width: 3, height: 1); |
240 | |
241 | button1 = gtk_check_button_new_with_label (label: "Blank" ); |
242 | gtk_widget_set_valign (widget: button1, align: GTK_ALIGN_START); |
243 | g_signal_connect (button1, "toggled" , G_CALLBACK (set_blank), entry); |
244 | gtk_box_append (GTK_BOX (box), child: button1); |
245 | button2 = gtk_check_button_new_with_label (label: "Icon Name" ); |
246 | gtk_widget_set_valign (widget: button2, align: GTK_ALIGN_START); |
247 | gtk_check_button_set_group (GTK_CHECK_BUTTON (button2), GTK_CHECK_BUTTON (button1)); |
248 | g_signal_connect (button2, "toggled" , G_CALLBACK (set_icon_name), entry); |
249 | gtk_box_append (GTK_BOX (box), child: button2); |
250 | button3 = gtk_check_button_new_with_label (label: "GIcon" ); |
251 | gtk_widget_set_valign (widget: button3, align: GTK_ALIGN_START); |
252 | gtk_check_button_set_group (GTK_CHECK_BUTTON (button3), GTK_CHECK_BUTTON (button1)); |
253 | g_signal_connect (button3, "toggled" , G_CALLBACK (set_gicon), entry); |
254 | gtk_box_append (GTK_BOX (box), child: button3); |
255 | button4 = gtk_check_button_new_with_label (label: "Texture" ); |
256 | gtk_widget_set_valign (widget: button4, align: GTK_ALIGN_START); |
257 | gtk_check_button_set_group (GTK_CHECK_BUTTON (button4), GTK_CHECK_BUTTON (button1)); |
258 | g_signal_connect (button4, "toggled" , G_CALLBACK (set_texture), entry); |
259 | gtk_box_append (GTK_BOX (box), child: button4); |
260 | |
261 | label = gtk_label_new (str: "Emoji:" ); |
262 | gtk_grid_attach (GTK_GRID (grid), child: label, column: 0, row: 6, width: 1, height: 1); |
263 | gtk_widget_set_halign (widget: label, align: GTK_ALIGN_START); |
264 | gtk_widget_set_valign (widget: label, align: GTK_ALIGN_CENTER); |
265 | |
266 | entry = gtk_entry_new (); |
267 | g_object_set (object: entry, first_property_name: "show-emoji-icon" , TRUE, NULL); |
268 | gtk_widget_set_hexpand (widget: entry, TRUE); |
269 | gtk_grid_attach (GTK_GRID (grid), child: entry, column: 1, row: 6, width: 1, height: 1); |
270 | |
271 | box = gtk_box_new (orientation: GTK_ORIENTATION_HORIZONTAL, spacing: 0); |
272 | gtk_widget_add_css_class (widget: box, css_class: "view" ); |
273 | gtk_widget_add_css_class (widget: box, css_class: "entry-frame" ); |
274 | gtk_widget_set_cursor_from_name (widget: box, name: "text" ); |
275 | entry = gtk_entry_new (); |
276 | gtk_widget_set_hexpand (widget: entry, TRUE); |
277 | gtk_box_append (GTK_BOX (box), child: entry); |
278 | image = gtk_image_new_from_icon_name (icon_name: "edit-find-symbolic" ); |
279 | gtk_widget_set_cursor_from_name (widget: image, name: "default" ); |
280 | gtk_widget_set_margin_start (widget: image, margin: 6); |
281 | gtk_widget_set_margin_end (widget: image, margin: 6); |
282 | gtk_widget_set_margin_top (widget: image, margin: 6); |
283 | gtk_widget_set_margin_bottom (widget: image, margin: 6); |
284 | gtk_widget_set_tooltip_text (widget: image, text: "Click me" ); |
285 | |
286 | GtkGesture *gesture; |
287 | gesture = gtk_gesture_click_new (); |
288 | g_signal_connect (gesture, "pressed" , G_CALLBACK (icon_pressed_cb), NULL); |
289 | gtk_widget_add_controller (widget: image, GTK_EVENT_CONTROLLER (gesture)); |
290 | gtk_box_append (GTK_BOX (box), child: image); |
291 | image = gtk_image_new_from_icon_name (icon_name: "document-save-symbolic" ); |
292 | gtk_widget_set_margin_start (widget: image, margin: 6); |
293 | gtk_widget_set_margin_end (widget: image, margin: 6); |
294 | gtk_widget_set_margin_top (widget: image, margin: 6); |
295 | gtk_widget_set_margin_bottom (widget: image, margin: 6); |
296 | gtk_box_append (GTK_BOX (box), child: image); |
297 | gtk_grid_attach (GTK_GRID (grid), child: box, column: 1, row: 7, width: 1, height: 1); |
298 | |
299 | GtkCssProvider *provider; |
300 | provider = gtk_css_provider_new (); |
301 | gtk_css_provider_load_from_data (css_provider: provider, data: cssdata, length: -1); |
302 | gtk_style_context_add_provider_for_display (display: gdk_display_get_default (), GTK_STYLE_PROVIDER (provider), priority: 800); |
303 | gtk_widget_show (widget: window); |
304 | |
305 | while (!done) |
306 | g_main_context_iteration (NULL, TRUE); |
307 | |
308 | return 0; |
309 | } |
310 | |