testentryicons.c 5.88 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-info", 2, 2);
21 22 23 24

  g_print ("drag begin %d\n", pos);
}

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
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)
    {
#if 0
      gint start, end;
      
      if (gtk_editable_get_selection_bounds (GTK_EDITABLE (widget), &start, &end))
        {
          gchar *str;
          
          str = gtk_editable_get_chars (GTK_EDITABLE (widget), start, end);
          gtk_selection_data_set_text (data, str, -1);
          g_free (str);
        }
#else
      gtk_selection_data_set_text (data, "XXX", -1);
#endif
    }
}
55 56 57 58 59

int
main (int argc, char **argv)
{
  GtkWidget *window;
60
  GtkWidget *grid;
61 62 63
  GtkWidget *label;
  GtkWidget *entry;
  GIcon *icon;
64
  GtkTargetList *tlist;
65 66 67 68 69 70 71 72 73 74

  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);

75 76 77 78
  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);
79 80 81 82 83

  /*
   * Open File - Sets the icon using a GIcon
   */
  label = gtk_label_new ("Open File:");
84
  gtk_grid_attach (GTK_GRID (grid), label, 0, 0, 1, 1);
85 86
  gtk_widget_set_halign (label, GTK_ALIGN_START);
  gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
87 88

  entry = gtk_entry_new ();
89 90
  gtk_widget_set_hexpand (entry, TRUE);
  gtk_grid_attach (GTK_GRID (grid), entry, 1, 0, 1, 1);
91

92 93
  icon = g_themed_icon_new ("folder-symbolic");
  g_themed_icon_append_name (G_THEMED_ICON (icon), "folder-symbolic");
94

95 96 97 98
  gtk_entry_set_icon_from_gicon (GTK_ENTRY (entry),
				 GTK_ENTRY_ICON_PRIMARY,
				 icon);
  gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
99
			        GTK_ENTRY_ICON_PRIMARY,
100 101 102 103 104 105 106
				FALSE);

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

  /*
107
   * Save File - sets the icon using an icon name.
108 109
   */
  label = gtk_label_new ("Save File:");
110
  gtk_grid_attach (GTK_GRID (grid), label, 0, 1, 1, 1);
111 112
  gtk_widget_set_halign (label, GTK_ALIGN_START);
  gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
113 114

  entry = gtk_entry_new ();
115 116
  gtk_widget_set_hexpand (entry, TRUE);
  gtk_grid_attach (GTK_GRID (grid), entry, 1, 1, 1, 1);
117 118
  gtk_entry_set_text (GTK_ENTRY (entry), "‏Right-to-left");
  gtk_widget_set_direction (entry, GTK_TEXT_DIR_RTL);
119
  
120 121 122
  gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
                                     GTK_ENTRY_ICON_PRIMARY,
                                     "document-save-symbolic");
123 124 125
  gtk_entry_set_icon_tooltip_text (GTK_ENTRY (entry),
				   GTK_ENTRY_ICON_PRIMARY,
				   "Save a file");
126 127 128 129 130
  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); 
131 132
  g_signal_connect_after (entry, "drag-begin", 
                          G_CALLBACK (drag_begin_cb), NULL);
133 134 135
  g_signal_connect (entry, "drag-data-get", 
                    G_CALLBACK (drag_data_get_cb), NULL);
  gtk_target_list_unref (tlist);
136 137 138 139 140

  /*
   * Search - Uses a helper function
   */
  label = gtk_label_new ("Search:");
141
  gtk_grid_attach (GTK_GRID (grid), label, 0, 2, 1, 1);
142 143
  gtk_widget_set_halign (label, GTK_ALIGN_START);
  gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
144 145

  entry = gtk_entry_new ();
146 147
  gtk_widget_set_hexpand (entry, TRUE);
  gtk_grid_attach (GTK_GRID (grid), entry, 1, 2, 1, 1);
148

149 150 151
  gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
                                     GTK_ENTRY_ICON_PRIMARY,
                                     "edit-find-symbolic");
152

153 154 155
  gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
                                     GTK_ENTRY_ICON_SECONDARY,
                                     "edit-clear-symbolic");
156

157
  g_signal_connect (entry, "icon-press", G_CALLBACK (clear_pressed), NULL);
158

159
  /*
160
   * Password - Sets the icon using an icon name
161 162
   */
  label = gtk_label_new ("Password:");
163
  gtk_grid_attach (GTK_GRID (grid), label, 0, 3, 1, 1);
164 165
  gtk_widget_set_halign (label, GTK_ALIGN_START);
  gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
166 167

  entry = gtk_entry_new ();
168 169
  gtk_widget_set_hexpand (entry, TRUE);
  gtk_grid_attach (GTK_GRID (grid), entry, 1, 3, 1, 1);
170 171
  gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);

172 173 174
  gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
                                     GTK_ENTRY_ICON_PRIMARY,
                                     "dialog-password-symbolic");
175

176 177 178 179
  gtk_entry_set_icon_activatable (GTK_ENTRY (entry),
				  GTK_ENTRY_ICON_PRIMARY,
				  FALSE);

180 181
  /* Name - Does not set any icons. */
  label = gtk_label_new ("Name:");
182
  gtk_grid_attach (GTK_GRID (grid), label, 0, 4, 1, 1);
183 184
  gtk_widget_set_halign (label, GTK_ALIGN_START);
  gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
185 186

  entry = gtk_entry_new ();
187 188
  gtk_widget_set_hexpand (entry, TRUE);
  gtk_grid_attach (GTK_GRID (grid), entry, 1, 4, 1, 1);
189 190 191 192 193 194 195

  gtk_widget_show_all (window);

  gtk_main();

  return 0;
}