testentryicons.c 9.14 KB
Newer Older
1 2
#include <gtk/gtk.h>
#include <stdio.h>
3

4 5 6 7 8 9 10
static void
clear_pressed (GtkEntry *entry, gint icon, GdkEvent *event, gpointer data)
{
   if (icon == GTK_ENTRY_ICON_SECONDARY)
     gtk_entry_set_text (entry, "");
}

11 12 13 14 15 16 17 18 19
static void
drag_begin_cb (GtkWidget      *widget,
               GdkDragContext *context,
               gpointer        user_data)
{
  gint pos;

  pos = gtk_entry_get_current_icon_drag_source (GTK_ENTRY (widget));
  if (pos != -1)
20
    gtk_drag_set_icon_name (context, "dialog-information", 2, 2);
21 22
}

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
static void
drag_data_get_cb (GtkWidget        *widget,
                  GdkDragContext   *context,
                  GtkSelectionData *data,
                  guint             info,
                  guint             time,
                  gpointer          user_data)
{
  gint pos;

  pos = gtk_entry_get_current_icon_drag_source (GTK_ENTRY (widget));

  if (pos == GTK_ENTRY_ICON_PRIMARY)
    {
      gint start, end;
38

39 40 41
      if (gtk_editable_get_selection_bounds (GTK_EDITABLE (widget), &start, &end))
        {
          gchar *str;
42

43 44 45 46
          str = gtk_editable_get_chars (GTK_EDITABLE (widget), start, end);
          gtk_selection_data_set_text (data, str, -1);
          g_free (str);
        }
47 48
      else
        gtk_selection_data_set_text (data, "XXX", -1);
49 50
    }
}
51

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
static void
set_blank (GtkWidget *button,
           GtkEntry  *entry)
{
  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
    gtk_entry_set_icon_from_icon_name (entry, GTK_ENTRY_ICON_SECONDARY, NULL);
}

static void
set_icon_name (GtkWidget *button,
               GtkEntry  *entry)
{
  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
    gtk_entry_set_icon_from_icon_name (entry, GTK_ENTRY_ICON_SECONDARY, "media-floppy");
}

static void
set_gicon (GtkWidget *button,
           GtkEntry  *entry)
{
  GIcon *icon;

 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
    {
      icon = g_themed_icon_new ("gtk-yes");
      gtk_entry_set_icon_from_gicon (entry, GTK_ENTRY_ICON_SECONDARY, icon);
      g_object_unref (icon);
    }
}

static void
set_pixbuf (GtkWidget *button,
            GtkEntry  *entry)
{
  GdkPixbuf *pixbuf;

  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
    {
      pixbuf = gdk_pixbuf_new_from_resource ("/org/gtk/libgtk/inspector/logo.png", NULL);
      gtk_entry_set_icon_from_pixbuf (entry, GTK_ENTRY_ICON_SECONDARY, pixbuf);
      g_object_unref (pixbuf);
    }
}

96 97 98 99
int
main (int argc, char **argv)
{
  GtkWidget *window;
100
  GtkWidget *grid;
101 102
  GtkWidget *label;
  GtkWidget *entry;
103 104 105 106 107
  GtkWidget *box;
  GtkWidget *button1;
  GtkWidget *button2;
  GtkWidget *button3;
  GtkWidget *button4;
108
  GIcon *icon;
109
  GtkTargetList *tlist;
110 111 112 113 114 115 116 117 118 119

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "Gtk Entry Icons Test");
  gtk_container_set_border_width (GTK_CONTAINER (window), 12);

  g_signal_connect (G_OBJECT (window), "destroy",
		    G_CALLBACK (gtk_main_quit), NULL);

120 121 122 123
  grid = gtk_grid_new ();
  gtk_container_add (GTK_CONTAINER (window), grid);
  gtk_grid_set_row_spacing (GTK_GRID (grid), 6);
  gtk_grid_set_column_spacing (GTK_GRID (grid), 6);
124 125 126 127 128

  /*
   * Open File - Sets the icon using a GIcon
   */
  label = gtk_label_new ("Open File:");
129
  gtk_grid_attach (GTK_GRID (grid), label, 0, 0, 1, 1);
130 131
  gtk_widget_set_halign (label, GTK_ALIGN_START);
  gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
132 133

  entry = gtk_entry_new ();
134 135
  gtk_widget_set_hexpand (entry, TRUE);
  gtk_grid_attach (GTK_GRID (grid), entry, 1, 0, 1, 1);
136

137 138
  icon = g_themed_icon_new ("folder-symbolic");
  g_themed_icon_append_name (G_THEMED_ICON (icon), "folder-symbolic");
139

140 141 142 143
  gtk_entry_set_icon_from_gicon (GTK_ENTRY (entry),
				 GTK_ENTRY_ICON_PRIMARY,
				 icon);
  gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
144
			        GTK_ENTRY_ICON_PRIMARY,
145 146 147 148 149 150 151
				FALSE);

  gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
				   GTK_ENTRY_ICON_PRIMARY,
				   "Open a file");

  /*
152
   * Save File - sets the icon using an icon name.
153 154
   */
  label = gtk_label_new ("Save File:");
155
  gtk_grid_attach (GTK_GRID (grid), label, 0, 1, 1, 1);
156 157
  gtk_widget_set_halign (label, GTK_ALIGN_START);
  gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
158 159

  entry = gtk_entry_new ();
160 161
  gtk_widget_set_hexpand (entry, TRUE);
  gtk_grid_attach (GTK_GRID (grid), entry, 1, 1, 1, 1);
162 163
  gtk_entry_set_text (GTK_ENTRY (entry), "‏Right-to-left");
  gtk_widget_set_direction (entry, GTK_TEXT_DIR_RTL);
164
  
165 166 167
  gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
                                     GTK_ENTRY_ICON_PRIMARY,
                                     "document-save-symbolic");
168 169 170
  gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
				   GTK_ENTRY_ICON_PRIMARY,
				   "Save a file");
171 172 173 174 175
  tlist = gtk_target_list_new (NULL, 0);
  gtk_target_list_add_text_targets (tlist, 0);
  gtk_entry_set_icon_drag_source (GTK_ENTRY (entry),
                                  GTK_ENTRY_ICON_PRIMARY,
                                  tlist, GDK_ACTION_COPY); 
176 177
  g_signal_connect_after (entry, "drag-begin", 
                          G_CALLBACK (drag_begin_cb), NULL);
178 179 180
  g_signal_connect (entry, "drag-data-get", 
                    G_CALLBACK (drag_data_get_cb), NULL);
  gtk_target_list_unref (tlist);
181 182 183 184 185

  /*
   * Search - Uses a helper function
   */
  label = gtk_label_new ("Search:");
186
  gtk_grid_attach (GTK_GRID (grid), label, 0, 2, 1, 1);
187 188
  gtk_widget_set_halign (label, GTK_ALIGN_START);
  gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
189 190

  entry = gtk_entry_new ();
191 192
  gtk_widget_set_hexpand (entry, TRUE);
  gtk_grid_attach (GTK_GRID (grid), entry, 1, 2, 1, 1);
193

194 195 196
  gtk_entry_set_placeholder_text (GTK_ENTRY (entry),
                                  "Type some text, then click an icon");

197 198 199
  gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
                                     GTK_ENTRY_ICON_PRIMARY,
                                     "edit-find-symbolic");
200

201 202 203 204
  gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
                                   GTK_ENTRY_ICON_PRIMARY,
                                   "Clicking the other icon is more interesting!");

205 206 207
  gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
                                     GTK_ENTRY_ICON_SECONDARY,
                                     "edit-clear-symbolic");
208

209 210 211 212
  gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
                                   GTK_ENTRY_ICON_SECONDARY,
                                   "Clear");

213
  g_signal_connect (entry, "icon-press", G_CALLBACK (clear_pressed), NULL);
214

215
  /*
216
   * Password - Sets the icon using an icon name
217 218
   */
  label = gtk_label_new ("Password:");
219
  gtk_grid_attach (GTK_GRID (grid), label, 0, 3, 1, 1);
220 221
  gtk_widget_set_halign (label, GTK_ALIGN_START);
  gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
222 223

  entry = gtk_entry_new ();
224 225
  gtk_widget_set_hexpand (entry, TRUE);
  gtk_grid_attach (GTK_GRID (grid), entry, 1, 3, 1, 1);
226 227
  gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);

228 229 230
  gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
                                     GTK_ENTRY_ICON_PRIMARY,
                                     "dialog-password-symbolic");
231

232 233 234 235
  gtk_entry_set_icon_activatable (GTK_ENTRY (entry),
				  GTK_ENTRY_ICON_PRIMARY,
				  FALSE);

236 237 238 239
  gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
                                   GTK_ENTRY_ICON_PRIMARY,
                                   "The password is hidden for security");

240 241
  /* Name - Does not set any icons. */
  label = gtk_label_new ("Name:");
242
  gtk_grid_attach (GTK_GRID (grid), label, 0, 4, 1, 1);
243 244
  gtk_widget_set_halign (label, GTK_ALIGN_START);
  gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
245 246

  entry = gtk_entry_new ();
247
  gtk_widget_set_hexpand (entry, TRUE);
248 249 250 251 252
  gtk_entry_set_placeholder_text (GTK_ENTRY (entry),
                                  "Use the RadioButtons to choose an icon");
  gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
                                   GTK_ENTRY_ICON_SECONDARY,
                                   "Use the RadioButtons to change this icon");
253
  gtk_grid_attach (GTK_GRID (grid), entry, 1, 4, 1, 1);
254

255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
  box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
  gtk_grid_attach (GTK_GRID (grid), box, 0, 5, 3, 1);

  button1 = gtk_radio_button_new_with_label (NULL, "Blank");
  g_signal_connect (button1, "toggled", G_CALLBACK (set_blank), entry);
  gtk_container_add (GTK_CONTAINER (box), button1);
  button2 = gtk_radio_button_new_with_label (NULL, "Icon Name");
  gtk_radio_button_join_group (GTK_RADIO_BUTTON (button2), GTK_RADIO_BUTTON (button1));
  g_signal_connect (button2, "toggled", G_CALLBACK (set_icon_name), entry);
  gtk_container_add (GTK_CONTAINER (box), button2);
  button3 = gtk_radio_button_new_with_label (NULL, "GIcon");
  gtk_radio_button_join_group (GTK_RADIO_BUTTON (button3), GTK_RADIO_BUTTON (button1));
  g_signal_connect (button3, "toggled", G_CALLBACK (set_gicon), entry);
  gtk_container_add (GTK_CONTAINER (box), button3);
  button4 = gtk_radio_button_new_with_label (NULL, "Pixbuf");
  gtk_radio_button_join_group (GTK_RADIO_BUTTON (button4), GTK_RADIO_BUTTON (button1));
  g_signal_connect (button4, "toggled", G_CALLBACK (set_pixbuf), entry);
  gtk_container_add (GTK_CONTAINER (box), button4);

274 275 276 277 278 279
  gtk_widget_show_all (window);

  gtk_main();

  return 0;
}