コード例 #1
0
static gboolean
dict_check_word_timeout_cb (IdeEditorSpellWidget *self)
{
  const gchar *word;
  g_autofree gchar *tooltip = NULL;
  gchar *icon_name;
  gboolean valid = FALSE;

  g_assert (IDE_IS_EDITOR_SPELL_WIDGET (self));

  self->dict_check_word_state = CHECK_WORD_CHECKING;

  word = gtk_entry_get_text (GTK_ENTRY (self->dict_word_entry));
  if (!ide_str_empty0 (word))
    {
      if (ide_editor_spell_dict_personal_contains (self->dict, word))
        gtk_widget_set_tooltip_text (self->dict_word_entry, _("This word is already in the personal dictionary"));
      else if (gspell_checker_check_word (self->checker, word, -1, NULL))
        {
          tooltip = g_strdup_printf (_("This word is already in the %s dictionary"), gspell_language_get_name (self->spellchecker_language));
          gtk_widget_set_tooltip_text (self->dict_word_entry, tooltip);
        }
      else
        {
          valid = TRUE;
          gtk_widget_set_tooltip_text (self->dict_word_entry, NULL);
        }

      icon_name = valid ? "" : "dialog-warning-symbolic";
    }
  else
    icon_name = "";

  gtk_widget_set_sensitive (GTK_WIDGET (self->dict_add_button), valid);
  gtk_entry_set_icon_from_icon_name (GTK_ENTRY (self->dict_word_entry),
                                     GTK_ENTRY_ICON_SECONDARY,
                                     icon_name);

  self->dict_check_word_state = CHECK_WORD_NONE;

  self->dict_check_word_timeout_id = 0;
  if (self->is_dict_check_word_invalid == TRUE)
    {
      self->dict_check_word_timeout_id = g_timeout_add_full (G_PRIORITY_DEFAULT,
                                                             DICT_CHECK_WORD_INTERVAL_MIN,
                                                             (GSourceFunc)dict_check_word_timeout_cb,
                                                             self,
                                                             NULL);
      self->dict_check_word_state = CHECK_WORD_IDLE;
      self->is_dict_check_word_invalid = FALSE;
    }

  return G_SOURCE_REMOVE;
}
コード例 #2
0
static gboolean
check_word_timeout_cb (IdeEditorSpellWidget *self)
{
  const gchar *word;
  g_autoptr(GError) error = NULL;
  gchar *icon_name;
  gboolean ret = TRUE;

  g_assert (IDE_IS_EDITOR_SPELL_WIDGET (self));

  self->check_word_state = CHECK_WORD_CHECKING;

  word = gtk_entry_get_text (self->word_entry);
  if (!ide_str_empty0 (word))
    {
      /* FIXME: suggestions can give a multiple-words suggestion
       * that failed to the checkword test, ex: auto tools
       */
      ret = gspell_checker_check_word (self->checker, word, -1, &error);
      if (error != NULL)
        {
          g_message ("check error:%s\n", error->message);
        }

      icon_name = ret ? "" : "dialog-warning-symbolic";
    }
  else
    icon_name = "";

  gtk_entry_set_icon_from_icon_name (self->word_entry,
                                     GTK_ENTRY_ICON_SECONDARY,
                                     icon_name);

  self->check_word_state = CHECK_WORD_NONE;
  self->is_word_entry_valid = ret;

  self->check_word_timeout_id = 0;
  if (self->is_check_word_invalid == TRUE)
    {
      self->check_word_timeout_id = g_timeout_add_full (G_PRIORITY_DEFAULT,
                                                        CHECK_WORD_INTERVAL_MIN,
                                                        (GSourceFunc)check_word_timeout_cb,
                                                        self,
                                                        NULL);
      self->check_word_state = CHECK_WORD_IDLE;
      self->is_check_word_invalid = FALSE;
    }

  return G_SOURCE_REMOVE;
}
コード例 #3
0
static gboolean
gbp_spell_navigator_goto_next (GspellNavigator  *navigator,
                               gchar           **word_p,
                               GspellChecker   **spell_checker_p,
                               GError          **error_p)
{
  GbpSpellNavigator *self = (GbpSpellNavigator *)navigator;
  GspellTextBuffer *gspell_buffer;
  GspellChecker *spell_checker;
  GtkTextIter word_start;
  GtkTextIter end;
  GtkTextTag *no_spell_check_tag;

  g_assert (GBP_IS_SPELL_NAVIGATOR (self));
  g_assert ((self->word_start == NULL && self->word_end == NULL) ||
            (self->word_start != NULL && self->word_end != NULL));

  gspell_buffer = gspell_text_buffer_get_from_gtk_text_buffer (self->buffer);
  spell_checker = gspell_text_buffer_get_spell_checker (gspell_buffer);

  if (spell_checker == NULL)
    return FALSE;

  if (gspell_checker_get_language (spell_checker) == NULL)
    {
      if (spell_checker_p != NULL)
        *spell_checker_p = g_object_ref (spell_checker);

      g_set_error (error_p,
                   GSPELL_CHECKER_ERROR,
                   GSPELL_CHECKER_ERROR_NO_LANGUAGE_SET,
                   "%s",
                   _("No language set. Check your dictionary installation."));

      return FALSE;
    }

  gtk_text_buffer_get_iter_at_mark (self->buffer, &end, self->end_boundary);

  if (self->word_start == NULL)
    {
      GtkTextIter start;

      gtk_text_buffer_get_iter_at_mark (self->buffer, &start, self->start_boundary);

      self->word_start = gtk_text_buffer_create_mark (self->buffer, NULL, &start, TRUE);
      self->word_end = gtk_text_buffer_create_mark (self->buffer, NULL, &start, FALSE);

      word_start = start;
    }
  else
    {
      GtkTextIter word_end;

      gtk_text_buffer_get_iter_at_mark (self->buffer, &word_end, self->word_end);

      if (gtk_text_iter_compare (&end, &word_end) <= 0)
        return FALSE;

      word_start = word_end;
    }

  no_spell_check_tag = gbp_spell_utils_get_no_spell_check_tag (self->buffer);

  while (TRUE)
    {
      GtkTextIter word_end;
      g_autofree gchar *word = NULL;
      gboolean correctly_spelled;
      GError *error = NULL;

      if (!gbp_spell_utils_text_iter_starts_word (&word_start))
        {
          GtkTextIter iter;

          iter = word_start;
          gbp_spell_utils_text_iter_forward_word_end (&word_start);

          if (gtk_text_iter_equal (&iter, &word_start))
            return FALSE;

          gbp_spell_utils_text_iter_backward_word_start (&word_start);
        }

        if (!gbp_spell_utils_skip_no_spell_check (no_spell_check_tag, &word_start, &end))
          return FALSE;

        g_return_val_if_fail (gbp_spell_utils_text_iter_starts_word (&word_start), FALSE);

        word_end = word_start;
        gbp_spell_utils_text_iter_forward_word_end (&word_end);

        if (gtk_text_iter_compare (&end, &word_end) < 0)
          return FALSE;

        word = gtk_text_buffer_get_text (self->buffer, &word_start, &word_end, FALSE);
        correctly_spelled = gspell_checker_check_word (spell_checker, word, -1, &error);

        if (error != NULL)
          {
            g_propagate_error (error_p, error);
            return FALSE;
          }

        if (!correctly_spelled)
          {
            /* Found! */
            gtk_text_buffer_move_mark (self->buffer, self->word_start, &word_start);
            gtk_text_buffer_move_mark (self->buffer, self->word_end, &word_end);
            gbp_spell_navigator_select_misspelled_word (self);

            if (spell_checker_p != NULL)
              *spell_checker_p = g_object_ref (spell_checker);

            if (word_p != NULL)
              *word_p = g_steal_pointer (&word);

            return TRUE;
         }

        word_start = word_end;
    }

  return FALSE;
}