GtkWidget *
cc_input_chooser_new (GtkWindow    *main_window,
                      GnomeXkbInfo *xkb_info,
                      GHashTable   *ibus_engines)
{
  GtkBuilder *builder;
  GtkWidget *chooser;
  CcInputChooserPrivate *priv;
  gint width;
  GError *error = NULL;

  builder = gtk_builder_new ();
  if (gtk_builder_add_from_resource (builder, "/org/gnome/initial-setup/input-chooser.ui", &error) == 0)
    {
      g_object_unref (builder);
      g_warning ("failed to load input chooser: %s", error->message);
      g_error_free (error);
      return NULL;
    }
  chooser = WID ("input-dialog");
  priv = g_new0 (CcInputChooserPrivate, 1);
  g_object_set_data_full (G_OBJECT (chooser), "private", priv, cc_input_chooser_private_free);
  g_object_set_data_full (G_OBJECT (chooser), "builder", builder, g_object_unref);

  priv->xkb_info = xkb_info;
  priv->ibus_engines = ibus_engines;

  priv->add_button = WID ("add-button");
  priv->filter_entry = WID ("filter-entry");
  priv->list = WID ("list");
  priv->scrolledwindow = WID ("scrolledwindow");
  priv->adjustment = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (priv->scrolledwindow));

  priv->more_item = g_object_ref_sink (more_widget_new ());
  priv->no_results = g_object_ref_sink (no_results_widget_new ());

  gtk_list_box_set_filter_func (GTK_LIST_BOX (priv->list), list_filter, chooser, NULL);
  gtk_list_box_set_sort_func (GTK_LIST_BOX (priv->list), list_sort, chooser, NULL);
  g_signal_connect (priv->list, "row-activated", G_CALLBACK (row_activated), chooser);
  g_signal_connect (priv->list, "row-selected", G_CALLBACK (row_selected), chooser);

  g_signal_connect_swapped (priv->filter_entry, "changed", G_CALLBACK (filter_changed), chooser);

  get_locale_infos (chooser);
#ifdef HAVE_IBUS
  get_ibus_locale_infos (chooser);
#endif  /* HAVE_IBUS */
  show_locale_widgets (chooser);

  /* Try to come up with a sensible width */
  gtk_window_get_size (main_window, &width, NULL);
  gtk_widget_set_size_request (chooser, width * MAIN_WINDOW_WIDTH_RATIO, -1);
  gtk_window_set_resizable (GTK_WINDOW (chooser), TRUE);

  gtk_window_set_transient_for (GTK_WINDOW (chooser), main_window);

  return chooser;
}
void
cc_input_chooser_set_ibus_engines (GtkWidget  *chooser,
                                   GHashTable *ibus_engines)
{
#ifdef HAVE_IBUS
  CcInputChooserPrivate *priv = GET_PRIVATE (chooser);

  /* This should only be called once when IBus shows up in case it
     wasn't up yet when the user opened the input chooser dialog. */
  g_return_if_fail (priv->ibus_engines == NULL);

  priv->ibus_engines = ibus_engines;
  get_ibus_locale_infos (chooser);
  show_locale_rows (chooser);
#endif  /* HAVE_IBUS */
}