]> Pileus Git - ~andy/gtk/blob - tests/testentryicons.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / tests / testentryicons.c
1 #include <gtk/gtk.h>
2 #include <stdio.h>
3 #include "prop-editor.h"
4
5 static gboolean
6 delete_event_cb (GtkWidget *editor,
7                  gint       response,
8                  gpointer   user_data)
9 {
10   gtk_widget_hide (editor);
11
12   return TRUE;
13 }
14
15 static void
16 properties_cb (GtkWidget *button,
17                GObject   *entry)
18 {
19   GtkWidget *editor;
20
21   editor = g_object_get_data (entry, "properties-dialog");
22
23   if (editor == NULL)
24     {
25       editor = create_prop_editor (G_OBJECT (entry), G_TYPE_INVALID);
26       gtk_container_set_border_width (GTK_CONTAINER (editor), 12);
27       gtk_window_set_transient_for (GTK_WINDOW (editor),
28                                     GTK_WINDOW (gtk_widget_get_toplevel (button)));
29       g_signal_connect (editor, "delete-event", G_CALLBACK (delete_event_cb), NULL);
30       g_object_set_data (entry, "properties-dialog", editor);
31     }
32
33   gtk_window_present (GTK_WINDOW (editor));
34 }
35
36 static void
37 clear_pressed (GtkEntry *entry, gint icon, GdkEvent *event, gpointer data)
38 {
39    if (icon == GTK_ENTRY_ICON_SECONDARY)
40      gtk_entry_set_text (entry, "");
41 }
42
43 static void
44 drag_begin_cb (GtkWidget      *widget,
45                GdkDragContext *context,
46                gpointer        user_data)
47 {
48   gint pos;
49
50   pos = gtk_entry_get_current_icon_drag_source (GTK_ENTRY (widget));
51   if (pos != -1)
52     gtk_drag_set_icon_stock (context, GTK_STOCK_INFO, 2, 2);
53
54   g_print ("drag begin %d\n", pos);
55 }
56
57 static void
58 drag_data_get_cb (GtkWidget        *widget,
59                   GdkDragContext   *context,
60                   GtkSelectionData *data,
61                   guint             info,
62                   guint             time,
63                   gpointer          user_data)
64 {
65   gint pos;
66
67   pos = gtk_entry_get_current_icon_drag_source (GTK_ENTRY (widget));
68
69   if (pos == GTK_ENTRY_ICON_PRIMARY)
70     {
71 #if 0
72       gint start, end;
73       
74       if (gtk_editable_get_selection_bounds (GTK_EDITABLE (widget), &start, &end))
75         {
76           gchar *str;
77           
78           str = gtk_editable_get_chars (GTK_EDITABLE (widget), start, end);
79           gtk_selection_data_set_text (data, str, -1);
80           g_free (str);
81         }
82 #else
83       gtk_selection_data_set_text (data, "XXX", -1);
84 #endif
85     }
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 *button;
96   GIcon *icon;
97   GtkTargetList *tlist;
98
99   gtk_init (&argc, &argv);
100
101   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
102   gtk_window_set_title (GTK_WINDOW (window), "Gtk Entry Icons Test");
103   gtk_container_set_border_width (GTK_CONTAINER (window), 12);
104
105   g_signal_connect (G_OBJECT (window), "destroy",
106                     G_CALLBACK (gtk_main_quit), NULL);
107
108   grid = gtk_grid_new ();
109   gtk_container_add (GTK_CONTAINER (window), grid);
110   gtk_grid_set_row_spacing (GTK_GRID (grid), 6);
111   gtk_grid_set_column_spacing (GTK_GRID (grid), 6);
112
113   /*
114    * Open File - Sets the icon using a GIcon
115    */
116   label = gtk_label_new ("Open File:");
117   gtk_grid_attach (GTK_GRID (grid), label, 0, 0, 1, 1);
118   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
119
120   entry = gtk_entry_new ();
121   gtk_widget_set_hexpand (entry, TRUE);
122   gtk_grid_attach (GTK_GRID (grid), entry, 1, 0, 1, 1);
123
124   icon = g_themed_icon_new ("folder");
125   g_themed_icon_append_name (G_THEMED_ICON (icon), "folder");
126
127   gtk_entry_set_icon_from_gicon (GTK_ENTRY (entry),
128                                  GTK_ENTRY_ICON_PRIMARY,
129                                  icon);
130   gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
131                                 GTK_ENTRY_ICON_PRIMARY,
132                                 FALSE);
133
134   gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
135                                    GTK_ENTRY_ICON_PRIMARY,
136                                    "Open a file");
137
138   button = gtk_button_new_with_label ("Properties");
139   gtk_grid_attach (GTK_GRID (grid), button, 2, 0, 1, 1);
140   g_signal_connect (button, "clicked", 
141                     G_CALLBACK (properties_cb), entry);                    
142
143   
144   /*
145    * Save File - sets the icon using a stock id.
146    */
147   label = gtk_label_new ("Save File:");
148   gtk_grid_attach (GTK_GRID (grid), label, 0, 1, 1, 1);
149   gtk_misc_set_alignment (GTK_MISC(label), 0.0, 0.5);
150
151   entry = gtk_entry_new ();
152   gtk_widget_set_hexpand (entry, TRUE);
153   gtk_grid_attach (GTK_GRID (grid), entry, 1, 1, 1, 1);
154   gtk_entry_set_text (GTK_ENTRY (entry), "ā€¸Right-to-left");
155   gtk_widget_set_direction (entry, GTK_TEXT_DIR_RTL);
156   
157   gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
158                                  GTK_ENTRY_ICON_PRIMARY,
159                                  GTK_STOCK_SAVE);
160   gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
161                                    GTK_ENTRY_ICON_PRIMARY,
162                                    "Save a file");
163   tlist = gtk_target_list_new (NULL, 0);
164   gtk_target_list_add_text_targets (tlist, 0);
165   gtk_entry_set_icon_drag_source (GTK_ENTRY (entry),
166                                   GTK_ENTRY_ICON_PRIMARY,
167                                   tlist, GDK_ACTION_COPY); 
168   g_signal_connect_after (entry, "drag-begin", 
169                           G_CALLBACK (drag_begin_cb), NULL);
170   g_signal_connect (entry, "drag-data-get", 
171                     G_CALLBACK (drag_data_get_cb), NULL);
172   gtk_target_list_unref (tlist);
173
174   button = gtk_button_new_with_label ("Properties");
175   gtk_grid_attach (GTK_GRID (grid), button, 2, 1, 1, 1);
176   g_signal_connect (button, "clicked", 
177                     G_CALLBACK (properties_cb), entry);                    
178
179   /*
180    * Search - Uses a helper function
181    */
182   label = gtk_label_new ("Search:");
183   gtk_grid_attach (GTK_GRID (grid), label, 0, 2, 1, 1);
184   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
185
186   entry = gtk_entry_new ();
187   gtk_widget_set_hexpand (entry, TRUE);
188   gtk_grid_attach (GTK_GRID (grid), entry, 1, 2, 1, 1);
189
190   gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
191                                  GTK_ENTRY_ICON_PRIMARY,
192                                  GTK_STOCK_FIND);
193
194   gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
195                                  GTK_ENTRY_ICON_SECONDARY,
196                                  GTK_STOCK_CLEAR);
197
198   g_signal_connect (entry, "icon-press", G_CALLBACK (clear_pressed), NULL);
199
200   button = gtk_button_new_with_label ("Properties");
201   gtk_grid_attach (GTK_GRID (grid), button, 2, 2, 1, 1);
202   g_signal_connect (button, "clicked", 
203                     G_CALLBACK (properties_cb), entry);                    
204
205   /*
206    * Password - Sets the icon using a stock id
207    */
208   label = gtk_label_new ("Password:");
209   gtk_grid_attach (GTK_GRID (grid), label, 0, 3, 1, 1);
210   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
211
212   entry = gtk_entry_new ();
213   gtk_widget_set_hexpand (entry, TRUE);
214   gtk_grid_attach (GTK_GRID (grid), entry, 1, 3, 1, 1);
215   gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
216
217   gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
218                                  GTK_ENTRY_ICON_PRIMARY,
219                                  GTK_STOCK_DIALOG_AUTHENTICATION);
220
221   gtk_entry_set_icon_activatable (GTK_ENTRY (entry),
222                                   GTK_ENTRY_ICON_PRIMARY,
223                                   FALSE);
224
225   button = gtk_button_new_with_label ("Properties");
226   gtk_grid_attach (GTK_GRID (grid), button, 2, 3, 1, 1);
227   g_signal_connect (button, "clicked", 
228                     G_CALLBACK (properties_cb), entry);                    
229
230   /* Name - Does not set any icons. */
231   label = gtk_label_new ("Name:");
232   gtk_grid_attach (GTK_GRID (grid), label, 0, 4, 1, 1);
233   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
234
235   entry = gtk_entry_new ();
236   gtk_widget_set_hexpand (entry, TRUE);
237   gtk_grid_attach (GTK_GRID (grid), entry, 1, 4, 1, 1);
238
239   button = gtk_button_new_with_label ("Properties");
240   gtk_grid_attach (GTK_GRID (grid), button, 2, 4, 1, 1);
241   g_signal_connect (button, "clicked", 
242                     G_CALLBACK (properties_cb), entry);                    
243
244   gtk_widget_show_all (window);
245
246   gtk_main();
247
248   return 0;
249 }