Example #1
0
GtkWidget *
do_entry_completion (GtkWidget *do_widget)
{
  GtkWidget *vbox;
  GtkWidget *label;
  GtkWidget *entry;
  GtkEntryCompletion *completion;
  GtkTreeModel *completion_model;
  
  if (!window)
  {
    window = gtk_dialog_new_with_buttons ("GtkEntryCompletion",
					  GTK_WINDOW (do_widget),
					  0,
					  GTK_STOCK_CLOSE,
					  GTK_RESPONSE_NONE,
					  NULL);
    gtk_window_set_resizable (GTK_WINDOW (window), FALSE);

    g_signal_connect (window, "response",
		      G_CALLBACK (gtk_widget_destroy), NULL);
    g_signal_connect (window, "destroy",
		      G_CALLBACK (gtk_widget_destroyed), &window);

    vbox = gtk_vbox_new (FALSE, 5);
    gtk_box_pack_start (GTK_BOX (GTK_DIALOG (window)->vbox), vbox, TRUE, TRUE, 0);
    gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);

    label = gtk_label_new (NULL);
    gtk_label_set_markup (GTK_LABEL (label), "Completion demo, try writing <b>total</b> or <b>gnome</b> for example.");
    gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);

    /* Create our entry */
    entry = gtk_entry_new ();
    gtk_box_pack_start (GTK_BOX (vbox), entry, FALSE, FALSE, 0);

    /* Create the completion object */
    completion = gtk_entry_completion_new ();

    /* Assign the completion to the entry */
    gtk_entry_set_completion (GTK_ENTRY (entry), completion);
    g_object_unref (completion);
    
    /* Create a tree model and use it as the completion model */
    completion_model = create_completion_model ();
    gtk_entry_completion_set_model (completion, completion_model);
    g_object_unref (completion_model);
    
    /* Use model column 0 as the text column */
    gtk_entry_completion_set_text_column (completion, 0);
  }

  if (!GTK_WIDGET_VISIBLE (window))
    gtk_widget_show_all (window);
  else
    gtk_widget_destroy (window);

  return window;
}
Example #2
0
GtkWidget *
entry_create_widget (GtkWidget * dlg)
{
  GtkWidget *c, *l = NULL, *w = NULL;

#if !GTK_CHECK_VERSION(3,0,0)
  w = gtk_hbox_new (FALSE, 5);
#else
  w = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
#endif

  if (options.entry_data.entry_label)
    {
      l = gtk_label_new (NULL);
      if (options.data.no_markup)
        gtk_label_set_text_with_mnemonic (GTK_LABEL (l), options.entry_data.entry_label);
      else
        gtk_label_set_markup_with_mnemonic (GTK_LABEL (l), options.entry_data.entry_label);
      gtk_widget_set_name (l, "yad-entry-label");
      gtk_box_pack_start (GTK_BOX (w), l, FALSE, FALSE, 1);
    }

  if (options.entry_data.numeric)
    {
      gdouble min, max, step, val;
      guint prec;

      min = 0.0;
      max = 65535.0;
      step = 1.0;
      prec = 0;

      if (options.extra_data && options.extra_data[0])
        {
          min = g_ascii_strtod (options.extra_data[0], NULL);
          if (options.extra_data[1])
            max = g_ascii_strtod (options.extra_data[1], NULL);
          if (options.extra_data[2])
            step = g_ascii_strtod (options.extra_data[2], NULL);
          if (options.extra_data[3])
            {
              prec = (guint) g_ascii_strtoull (options.extra_data[3], NULL, 0);
              if (prec > 20)
                prec = 20;
            }
        }

      c = entry = gtk_spin_button_new_with_range (min, max, step);
      gtk_entry_set_alignment (GTK_ENTRY (c), 1.0);
      gtk_spin_button_set_digits (GTK_SPIN_BUTTON (c), prec);
      gtk_widget_set_name (entry, "yad-entry-spin");

      if (options.entry_data.entry_text)
        {
          val = g_ascii_strtod (options.entry_data.entry_text, NULL);

          if (min >= max)
            {
              g_printerr (_("Maximum value must be greater than minimum value.\n"));
              min = 0.0;
              max = 65535.0;
            }

          if (val < min)
            {
              g_printerr (_("Initial value less than minimal.\n"));
              val = min;
            }
          else if (val > max)
            {
              g_printerr (_("Initial value greater than maximum.\n"));
              val = max;
            }

          gtk_spin_button_set_value (GTK_SPIN_BUTTON (c), val);
        }
    }
  else if (!options.entry_data.completion && options.extra_data && *options.extra_data)
    {
      gint active, i;

      if (options.common_data.editable || settings.combo_always_editable)
        {
#if GTK_CHECK_VERSION(2,24,0)
          c = gtk_combo_box_text_new_with_entry ();
#else
          c = gtk_combo_box_entry_new_text ();
#endif
          gtk_widget_set_name (c, "yad-entry-edit-combo");
          entry = gtk_bin_get_child (GTK_BIN (c));
          if (options.entry_data.licon)
            {
              GdkPixbuf *pb = get_pixbuf (options.entry_data.licon, YAD_SMALL_ICON);

              if (pb)
                gtk_entry_set_icon_from_pixbuf (GTK_ENTRY (entry), GTK_ENTRY_ICON_PRIMARY, pb);
            }
          if (options.entry_data.ricon)
            {
              GdkPixbuf *pb = get_pixbuf (options.entry_data.ricon, YAD_SMALL_ICON);

              if (pb)
                gtk_entry_set_icon_from_pixbuf (GTK_ENTRY (entry), GTK_ENTRY_ICON_SECONDARY, pb);
            }
        }
      else
        {
#if GTK_CHECK_VERSION(2,24,0)
          c = entry = gtk_combo_box_text_new ();
#else
          c = entry = gtk_combo_box_new_text ();
#endif
          gtk_widget_set_name (c, "yad-entry-combo");
          is_combo = TRUE;
        }

      i = 0;
      active = -1;
      while (options.extra_data[i] != NULL)
        {
          if (options.entry_data.entry_text &&
              g_ascii_strcasecmp (options.extra_data[i], options.entry_data.entry_text) == 0)
            active = i;
#if GTK_CHECK_VERSION(2,24,0)
          gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (c), options.extra_data[i]);
#else
          gtk_combo_box_append_text (GTK_COMBO_BOX (c), options.extra_data[i]);
#endif
          i++;
        }

      if (options.entry_data.entry_text && active == -1)
        {
#if GTK_CHECK_VERSION(2,24,0)
          gtk_combo_box_text_prepend_text (GTK_COMBO_BOX_TEXT (c), options.entry_data.entry_text);
#else
          gtk_combo_box_prepend_text (GTK_COMBO_BOX (c), options.entry_data.entry_text);
#endif
        }

      /* set first iter active */
      if (!options.common_data.editable)
        gtk_combo_box_set_active (GTK_COMBO_BOX (c), (active != -1 ? active : 0));
    }
  else
    {
      c = entry = gtk_entry_new ();
      gtk_widget_set_name (c, "yad-entry-widget");

      gtk_entry_set_activates_default (GTK_ENTRY (entry), TRUE);

      if (options.entry_data.entry_text)
        gtk_entry_set_text (GTK_ENTRY (entry), options.entry_data.entry_text);

      if (options.entry_data.hide_text)
        g_object_set (G_OBJECT (entry), "visibility", FALSE, NULL);

      if (options.entry_data.completion)
        {
          GtkEntryCompletion *completion;
          GtkTreeModel *completion_model;

          completion = gtk_entry_completion_new ();
          gtk_entry_set_completion (GTK_ENTRY (entry), completion);

          completion_model = create_completion_model ();
          gtk_entry_completion_set_model (completion, completion_model);
          g_object_unref (completion_model);

          gtk_entry_completion_set_text_column (completion, 0);
          g_object_unref (completion);
        }

      if (options.entry_data.licon)
        {
          GdkPixbuf *pb = get_pixbuf (options.entry_data.licon, YAD_SMALL_ICON);

          if (pb)
            gtk_entry_set_icon_from_pixbuf (GTK_ENTRY (entry), GTK_ENTRY_ICON_PRIMARY, pb);
        }
      if (options.entry_data.ricon)
        {
          GdkPixbuf *pb = get_pixbuf (options.entry_data.ricon, YAD_SMALL_ICON);

          if (pb)
            gtk_entry_set_icon_from_pixbuf (GTK_ENTRY (entry), GTK_ENTRY_ICON_SECONDARY, pb);
        }
    }

  if (l)
    gtk_label_set_mnemonic_widget (GTK_LABEL (l), entry);

  if (!is_combo)
    g_signal_connect (G_OBJECT (entry), "activate", G_CALLBACK (entry_activate_cb), dlg);
  else
    g_signal_connect (G_OBJECT (entry), "key-press-event", G_CALLBACK (combo_activate_cb), dlg);

  if (options.entry_data.licon || options.entry_data.ricon)
    g_signal_connect (G_OBJECT (entry), "icon-press", G_CALLBACK (icon_cb), NULL);

  gtk_box_pack_start (GTK_BOX (w), c, TRUE, TRUE, 1);

  return w;
}
Example #3
0
int 
main (int argc, char *argv[])
{
  GtkWidget *vbox;
  GtkWidget *label;
  GtkWidget *entry;
  GtkEntryCompletion *completion;
  GtkTreeModel *completion_model;
  GtkCellRenderer *cell;

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_container_set_border_width (GTK_CONTAINER (window), 5);
  g_signal_connect (window, "delete_event", gtk_main_quit, NULL);
  
  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 2);
  gtk_container_add (GTK_CONTAINER (window), vbox);
    
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
  
  label = gtk_label_new (NULL);

  gtk_label_set_markup (GTK_LABEL (label), "Completion demo, try writing <b>total</b> or <b>gnome</b> for example.");
  gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);

  /* Create our first entry */
  entry = gtk_entry_new ();
  
  /* Create the completion object */
  completion = gtk_entry_completion_new ();
  gtk_entry_completion_set_inline_completion (completion, TRUE);
  
  /* Assign the completion to the entry */
  gtk_entry_set_completion (GTK_ENTRY (entry), completion);
  g_object_unref (completion);
  
  add_with_prop_edit_button (vbox, entry, completion);

  /* Create a tree model and use it as the completion model */
  completion_model = create_simple_completion_model ();
  gtk_entry_completion_set_model (completion, completion_model);
  g_object_unref (completion_model);
  
  /* Use model column 0 as the text column */
  gtk_entry_completion_set_text_column (completion, 0);

  /* Create our second entry */
  entry = gtk_entry_new ();

  /* Create the completion object */
  completion = gtk_entry_completion_new ();
  
  /* Assign the completion to the entry */
  gtk_entry_set_completion (GTK_ENTRY (entry), completion);
  g_object_unref (completion);
  
  add_with_prop_edit_button (vbox, entry, completion);

  /* Create a tree model and use it as the completion model */
  completion_model = create_completion_model ();
  gtk_entry_completion_set_model (completion, completion_model);
  gtk_entry_completion_set_minimum_key_length (completion, 2);
  g_object_unref (completion_model);
  
  /* Use model column 1 as the text column */
  cell = gtk_cell_renderer_pixbuf_new ();
  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion), cell, FALSE);
  gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (completion), cell, 
				  "pixbuf", 0, NULL); 

  cell = gtk_cell_renderer_text_new ();
  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion), cell, FALSE);
  gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (completion), cell, 
				  "text", 1, NULL); 
  
  gtk_entry_completion_set_match_func (completion, match_func, NULL, NULL);
  g_signal_connect (completion, "match-selected", 
		    G_CALLBACK (match_selected_cb), NULL);

  gtk_entry_completion_insert_action_text (completion, 100, "action!");
  gtk_entry_completion_insert_action_text (completion, 101, "'nother action!");
  g_signal_connect (completion, "action_activated", G_CALLBACK (activated_cb), NULL);

  /* Create our third entry */
  entry = gtk_entry_new ();

  /* Create the completion object */
  completion = gtk_entry_completion_new ();
  
  /* Assign the completion to the entry */
  gtk_entry_set_completion (GTK_ENTRY (entry), completion);
  g_object_unref (completion);
  
  add_with_prop_edit_button (vbox, entry, completion);

  /* Create a tree model and use it as the completion model */
  completion_model = GTK_TREE_MODEL (gtk_list_store_new (1, G_TYPE_STRING));

  gtk_entry_completion_set_model (completion, completion_model);
  g_object_unref (completion_model);

  /* Use model column 0 as the text column */
  gtk_entry_completion_set_text_column (completion, 0);

  /* Fill the completion dynamically */
  gdk_threads_add_timeout (1000, (GSourceFunc) animation_timer, completion);

  /* Fourth entry */
  gtk_box_pack_start (GTK_BOX (vbox), gtk_label_new ("Model-less entry completion"), FALSE, FALSE, 0);

  entry = gtk_entry_new ();

  /* Create the completion object */
  completion = gtk_entry_completion_new ();
  
  /* Assign the completion to the entry */
  gtk_entry_set_completion (GTK_ENTRY (entry), completion);
  g_object_unref (completion);
  
  add_with_prop_edit_button (vbox, entry, completion);

  gtk_widget_show_all (window);

  gtk_main ();
  
  return 0;
}