Esempio n. 1
0
void Gobby::EditCommands::on_mark_set()
{
	g_assert(m_current_view != NULL);
	GtkTextBuffer* buffer =
		GTK_TEXT_BUFFER(m_current_view->get_text_buffer());

	m_header.action_edit_copy->set_sensitive(
		gtk_text_buffer_get_has_selection(buffer));

	if(m_current_view->get_active_user() != NULL)
	{
		m_header.action_edit_cut->set_sensitive(
			gtk_text_buffer_get_has_selection(buffer));
	}
}
Esempio n. 2
0
static void
gimp_text_style_editor_size_changed (GimpSizeEntry       *entry,
                                     GimpTextStyleEditor *editor)
{
  GtkTextBuffer *buffer = GTK_TEXT_BUFFER (editor->buffer);
  GList         *insert_tags;
  GList         *remove_tags;

  if (gtk_text_buffer_get_has_selection (buffer))
    {
      GtkTextIter start, end;
      gdouble     points;

      gtk_text_buffer_get_selection_bounds (buffer, &start, &end);

      points = gimp_units_to_points (gimp_size_entry_get_refval (entry, 0),
                                     GIMP_UNIT_PIXEL,
                                     editor->resolution_y);

      gimp_text_buffer_set_size (editor->buffer, &start, &end,
                                 PANGO_SCALE * points);
    }

  insert_tags = gimp_text_style_editor_list_tags (editor, &remove_tags);
  gimp_text_buffer_set_insert_tags (editor->buffer, insert_tags, remove_tags);
}
Esempio n. 3
0
static void ygtk_text_view_populate_popup (GtkTextView *view, GtkMenu *menu)
#endif
{
	if (gtk_text_view_get_editable (view))
		return;
	GtkTextBuffer *buffer = gtk_text_view_get_buffer (view);

	GList *items = gtk_container_get_children (GTK_CONTAINER (menu)), *i;
	for (i = items; i; i = i->next)
		gtk_container_remove (GTK_CONTAINER (menu), i->data);
	g_list_free (items);

	GtkWidget *item;
	item = gtk_menu_item_new();
	gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
	if (gtk_text_buffer_get_has_selection (buffer))
		g_signal_connect (item, "activate", G_CALLBACK (copy_activate_cb), buffer);
	else
		gtk_widget_set_sensitive (item, FALSE);
	item = gtk_separator_menu_item_new();
	gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
	item = gtk_menu_item_new();
	gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
	g_signal_connect (item, "activate", G_CALLBACK (select_all_activate_cb), buffer);
	gtk_widget_show_all (GTK_WIDGET (menu));
}
Esempio n. 4
0
static void
gimp_text_tool_enter_text (GimpTextTool *text_tool,
                           const gchar  *str)
{
  GtkTextBuffer *buffer = GTK_TEXT_BUFFER (text_tool->buffer);
  gboolean       had_selection;

  had_selection = gtk_text_buffer_get_has_selection (buffer);

  gtk_text_buffer_begin_user_action (buffer);

  gimp_text_tool_delete_selection (text_tool);

  if (! had_selection && text_tool->overwrite_mode && strcmp (str, "\n"))
    {
      GtkTextIter cursor;

      gtk_text_buffer_get_iter_at_mark (buffer, &cursor,
                                        gtk_text_buffer_get_insert (buffer));

      if (! gtk_text_iter_ends_line (&cursor))
        gimp_text_tool_delete_from_cursor (text_tool, GTK_DELETE_CHARS, 1);
    }

  gimp_text_buffer_insert (text_tool->buffer, str);

  gtk_text_buffer_end_user_action (buffer);
}
Esempio n. 5
0
static gboolean ygtk_text_view_button_press_event (GtkWidget *widget, GdkEventButton *event)
{	// on right-click, select word under cursor if there is no selection
	if (event->button == 3) {
		GtkTextView *view = GTK_TEXT_VIEW (widget);
		GtkTextBuffer *buffer = gtk_text_view_get_buffer (view);
		if (!gtk_text_buffer_get_has_selection (buffer)) {
			gint buffer_x, buffer_y;
			gtk_text_view_window_to_buffer_coords (view,
				GTK_TEXT_WINDOW_WIDGET, (gint) event->x, (gint) event->y, &buffer_x, &buffer_y);
			GtkTextIter iter;
			gtk_text_view_get_iter_at_location (view, &iter, buffer_x, buffer_y);

			if (!is_space (gtk_text_iter_get_char (&iter))) {
				GtkTextIter start, end = iter, temp = iter;
				do {
					start = temp;
					if (!gtk_text_iter_backward_char (&temp))
						break;
				} while (!is_space (gtk_text_iter_get_char (&temp)));
				do {
					if (!gtk_text_iter_forward_char (&end))
						break;
				} while (!is_space (gtk_text_iter_get_char (&end)));

				gtk_text_buffer_select_range (buffer, &start, &end);
			}
		}
	}
	return GTK_WIDGET_CLASS (ygtk_text_view_parent_class)->button_press_event (widget, event);
}
Esempio n. 6
0
static void
pgd_text_buffer_selection_changed (GtkTextBuffer *buffer,
                                   GParamSpec    *pspec,
                                   GtkWidget    *textview)
{
        gtk_widget_set_has_tooltip (textview, gtk_text_buffer_get_has_selection (buffer));
}
static void
update_ui_real (CeditWindow  *window,
		WindowData   *data)
{
	GtkTextView *view;
	GtkAction *action;
	gboolean sensitive = FALSE;

	cedit_debug (DEBUG_PLUGINS);

	view = GTK_TEXT_VIEW (cedit_window_get_active_view (window));

	if (view != NULL)
	{
		GtkTextBuffer *buffer;

		buffer = gtk_text_view_get_buffer (view);
		sensitive = (gtk_text_view_get_editable (view) &&
			     gtk_text_buffer_get_has_selection (buffer));
	}

	action = gtk_action_group_get_action (data->action_group,
					      "ChangeCase");
	gtk_action_set_sensitive (action, sensitive);
}
Esempio n. 8
0
static void
gimp_text_style_editor_tag_toggled (GtkToggleButton     *toggle,
                                    GimpTextStyleEditor *editor)
{
  GtkTextBuffer *buffer = GTK_TEXT_BUFFER (editor->buffer);
  GtkTextTag    *tag    = g_object_get_data (G_OBJECT (toggle), "tag");
  GList         *insert_tags;
  GList         *remove_tags;

  if (gtk_text_buffer_get_has_selection (buffer))
    {
      GtkTextIter start, end;

      gtk_text_buffer_get_selection_bounds (buffer, &start, &end);

      gtk_text_buffer_begin_user_action (buffer);

      if (gtk_toggle_button_get_active (toggle))
        {
          gtk_text_buffer_apply_tag (buffer, tag, &start, &end);
        }
      else
        {
          gtk_text_buffer_remove_tag (buffer, tag, &start, &end);
        }

      gtk_text_buffer_end_user_action (buffer);
    }

  insert_tags = gimp_text_style_editor_list_tags (editor, &remove_tags);
  gimp_text_buffer_set_insert_tags (editor->buffer, insert_tags, remove_tags);
}
Esempio n. 9
0
static gboolean
gb_vim_jump_to_line (GtkSourceView  *source_view,
                     const gchar    *command,
                     const gchar    *options,
                     GError        **error)
{
  GtkTextBuffer *buffer;
  gboolean extend_selection;
  gint line;

  if (!IDE_IS_SOURCE_VIEW (source_view))
    return TRUE;

  if (!int32_parse (&line, options, 0, G_MAXINT32, "line number", error))
    return FALSE;

  buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (source_view));
  extend_selection = gtk_text_buffer_get_has_selection (buffer);
  ide_source_view_set_count (IDE_SOURCE_VIEW (source_view), line);

  g_signal_emit_by_name (source_view,
                         "movement",
                         IDE_SOURCE_VIEW_MOVEMENT_NTH_LINE,
                         extend_selection, TRUE, TRUE);

  g_signal_emit_by_name (source_view, "save-insert-mark");

  return TRUE;
}
Esempio n. 10
0
void Gobby::EditCommands::on_active_user_changed(InfUser* active_user)
{
	g_assert(m_current_view != NULL);

	if(active_user != NULL)
	{
		InfTextSession* session = m_current_view->get_session();
		InfAdoptedAlgorithm* algorithm =
			inf_adopted_session_get_algorithm(
				INF_ADOPTED_SESSION(session));
		GtkTextBuffer* buffer = GTK_TEXT_BUFFER(
			m_current_view->get_text_buffer());

		m_header.action_edit_undo->set_sensitive(
			inf_adopted_algorithm_can_undo(
				algorithm, INF_ADOPTED_USER(active_user)));
		m_header.action_edit_redo->set_sensitive(
			inf_adopted_algorithm_can_redo(
				algorithm, INF_ADOPTED_USER(active_user)));

		m_header.action_edit_cut->set_sensitive(
			gtk_text_buffer_get_has_selection(buffer));
		m_header.action_edit_paste->set_sensitive(true);
	}
	else
	{
		m_header.action_edit_undo->set_sensitive(false);
		m_header.action_edit_redo->set_sensitive(false);
		m_header.action_edit_cut->set_sensitive(false);
		m_header.action_edit_paste->set_sensitive(false);
	}
}
Esempio n. 11
0
static guint
get_selection_anchor_point (FcitxIMContext *fcitxcontext,
                            guint cursor_pos,
                            guint surrounding_text_len)
{
    GtkWidget *widget;
    if (fcitxcontext->client_window == NULL) {
        return cursor_pos;
    }
    gdk_window_get_user_data (fcitxcontext->client_window, (gpointer *)&widget);

    if (!GTK_IS_TEXT_VIEW (widget)) {
        return cursor_pos;
    }

    GtkTextView *text_view = GTK_TEXT_VIEW (widget);
    GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);

    if (!gtk_text_buffer_get_has_selection (buffer)) {
        return cursor_pos;
    }

    GtkTextIter start_iter, end_iter, cursor_iter;
    if (!gtk_text_buffer_get_selection_bounds (buffer, &start_iter, &end_iter)) {
        return cursor_pos;
    }

    gtk_text_buffer_get_iter_at_mark (buffer,
                                      &cursor_iter,
                                      gtk_text_buffer_get_insert (buffer));

    guint start_index = gtk_text_iter_get_offset (&start_iter);
    guint end_index = gtk_text_iter_get_offset (&end_iter);
    guint cursor_index = gtk_text_iter_get_offset (&cursor_iter);

    guint anchor;

    if (start_index == cursor_index) {
        anchor = end_index;
    } else if (end_index == cursor_index) {
        anchor = start_index;
    } else {
        return cursor_pos;
    }

    // Change absolute index to relative position.
    guint relative_origin = cursor_index - cursor_pos;

    if (anchor < relative_origin) {
        return cursor_pos;
    }
    anchor -= relative_origin;

    if (anchor > surrounding_text_len) {
        return cursor_pos;
    }

    return anchor;
}
Esempio n. 12
0
//範囲を選択しているかどうかの状態が変わったときの動作
void change_selection(void)
{
    gboolean flag = gtk_text_buffer_get_has_selection(GTK_TEXT_BUFFER(buffer));
    
    set_action("Cut", flag);
    set_action("Copy", flag);
    set_action("Delete", flag);
}
Esempio n. 13
0
static guint console_menu_extra_state(void)
{
#ifdef G_OS_UNIX
	if (debug_console)
		return vte_terminal_get_has_selection(debug_console) << DS_INDEX_1;
#endif
	return gtk_text_buffer_get_has_selection(context) << DS_INDEX_1;
}
Esempio n. 14
0
//!
//! @brief Pagination algorithm to calculate how many pages are needed
//! @param operation Unused GtkPrintOperation
//! @param context Pointer co a GtkPrintContext to draw on
//! @param page_nr Integer of the current page number being draw
//! @param data Painter to a PageInfo struct to use for information
//! @return Return true when pagination finishes
//! @sa _done() _begin_print() draw() _begin_print()
//!
static gboolean _paginate (GtkPrintOperation *operation,
                           GtkPrintContext   *context,
                           GwPrintData       *data      )
{
    printf("paginate!\n");

    //Declarations
    GwPageInfo *page;
    GwPageInfo *prev_page;
    GList *iter;
    GtkTextIter end_bound;
    GtkTextIter start_bound;
    GtkTextView *view;
    GtkTextBuffer *buffer;

    //Initializations
    view = gw_searchwindow_get_current_textview (data->window);
    buffer = gtk_text_view_get_buffer (view);

    //Get the draw bounds
    if (gtk_text_buffer_get_has_selection (buffer))
    {
      gtk_text_buffer_get_selection_bounds (buffer, &start_bound, &end_bound);
    }
    else
    {
      gtk_text_buffer_get_start_iter (buffer, &start_bound);
      gtk_text_buffer_get_end_iter (buffer, &end_bound);
    }

    //Create the first page
    if (data->pages == NULL)
    {
      page = gw_pageinfo_new (start_bound, end_bound);
      _draw_page_results (context, page, data);
      data->pages = g_list_append (data->pages, page);
      return FALSE;
    }

    iter = g_list_last (data->pages);
    prev_page = GW_PAGEINFO (iter->data);

    //Finish paginating
    if (gtk_text_iter_get_line (&(prev_page->end)) == gtk_text_iter_get_line (&end_bound))
    {
      gtk_print_operation_set_n_pages (operation, g_list_length (data->pages));
      return TRUE;
    }

    //Add another page
    page = gw_pageinfo_new (prev_page->end, end_bound);
    _draw_page_results (context, page, data);
    data->pages = g_list_append (data->pages, page);

    return FALSE;
}
Esempio n. 15
0
File: key.c Progetto: kawatea/cedit
//特定のキーが押された時に動作する
gboolean key_press(GtkWidget *widget, GdkEventKey *event)
{
    if (event->keyval == GDK_Tab && (state & auto_mask)) {
        if (gtk_text_buffer_get_has_selection(GTK_TEXT_BUFFER(buffer))) {
            set_indent_all();
        } else {
            set_indent();
        }
        
        return TRUE;
    } else if (event->keyval == GDK_BackSpace && (state & delete_mask) && !gtk_text_buffer_get_has_selection(GTK_TEXT_BUFFER(buffer))) {
        if (delete_hungry_backward()) return TRUE;
    } else if (event->keyval == GDK_Delete && (state & delete_mask) && !gtk_text_buffer_get_has_selection(GTK_TEXT_BUFFER(buffer))) {
        if (delete_hungry_forward()) return TRUE;
    }
    
    if (event->keyval == GDK_braceright && (state & auto_mask)) press_brace = 1;
    
    return FALSE;
}
Esempio n. 16
0
static gboolean
gb_vim_jump_to_line (GtkWidget      *active_widget,
                     const gchar    *command,
                     const gchar    *options,
                     GError        **error)
{
  g_assert (GTK_IS_WIDGET (active_widget));

  if (IDE_IS_EDITOR_VIEW (active_widget))
    {
      GtkSourceView  *source_view = GTK_SOURCE_VIEW (IDE_EDITOR_VIEW (active_widget)->frame1->source_view);
      GtkTextBuffer *buffer;
      gboolean extend_selection;
      gint line;

      if (!int32_parse (&line, options, 0, G_MAXINT32, "line number", error))
        return FALSE;

      buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (source_view));
      extend_selection = gtk_text_buffer_get_has_selection (buffer);
      ide_source_view_set_count (IDE_SOURCE_VIEW (source_view), line);

      if (line == 0)
        {
          GtkTextIter iter;

          /*
           * Zero is not a valid line number, and IdeSourceView treats
           * that as a move to the end of the buffer. Instead, we want
           * line 1 to be like vi/vim.
           */
          gtk_text_buffer_get_start_iter (buffer, &iter);
          gtk_text_buffer_select_range (buffer, &iter, &iter);
          gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (source_view),
                                        gtk_text_buffer_get_insert (buffer),
                                        0.0, FALSE, 0.0, 0.0);
        }
      else
        {
          g_signal_emit_by_name (source_view,
                                 "movement",
                                 IDE_SOURCE_VIEW_MOVEMENT_NTH_LINE,
                                 extend_selection, TRUE, TRUE);
        }

      ide_source_view_set_count (IDE_SOURCE_VIEW (source_view), 0);

      g_signal_emit_by_name (source_view, "save-insert-mark");

      return TRUE;
    }
  else
    return gb_vim_set_source_view_error (error);
}
static gboolean
chat_text_view_get_has_selection (EmpathyChatView *view)
{
	GtkTextBuffer *buffer;
	
	g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
	
	buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
	
	return gtk_text_buffer_get_has_selection (buffer);
}
Esempio n. 18
0
G_MODULE_EXPORT
void on_menu_edit_activate(GtkWidget *widget, void *user)
{
  if (g_active_editor == NULL)
    return;

  if (gtk_text_buffer_get_has_selection
      (GTK_TEXT_BUFFER(g_active_editor->buffer))) {
    gtk_widget_set_sensitive(GTK_WIDGET(gui->menugui->menu_cut), TRUE);
    gtk_widget_set_sensitive(GTK_WIDGET(gui->menugui->menu_copy), TRUE);
    return;
  }

  gtk_widget_set_sensitive(GTK_WIDGET(gui->menugui->menu_cut), FALSE);
  gtk_widget_set_sensitive(GTK_WIDGET(gui->menugui->menu_copy), FALSE);
}
Esempio n. 19
0
void
gimp_text_tool_editor_button_release (GimpTextTool *text_tool)
{
  GtkTextBuffer *buffer = GTK_TEXT_BUFFER (text_tool->buffer);

  if (gtk_text_buffer_get_has_selection (buffer))
    {
      GimpTool         *tool  = GIMP_TOOL (text_tool);
      GimpDisplayShell *shell = gimp_display_get_shell (tool->display);
      GtkClipboard     *clipboard;

      clipboard = gtk_widget_get_clipboard (GTK_WIDGET (shell),
                                            GDK_SELECTION_PRIMARY);

      gtk_text_buffer_copy_clipboard (buffer, clipboard);
    }
}
Esempio n. 20
0
static void
gimp_text_style_editor_clear_tags (GtkButton           *button,
                                   GimpTextStyleEditor *editor)
{
  GtkTextBuffer *buffer = GTK_TEXT_BUFFER (editor->buffer);

  if (gtk_text_buffer_get_has_selection (buffer))
    {
      GtkTextIter start, end;

      gtk_text_buffer_get_selection_bounds (buffer, &start, &end);

      gtk_text_buffer_begin_user_action (buffer);

      gtk_text_buffer_remove_all_tags (buffer, &start, &end);

      gtk_text_buffer_end_user_action (buffer);
    }
}
Esempio n. 21
0
static void
gimp_text_tool_backspace (GimpTextTool *text_tool)
{
  GtkTextBuffer *buffer = GTK_TEXT_BUFFER (text_tool->buffer);

  gimp_text_tool_reset_im_context (text_tool);

  if (gtk_text_buffer_get_has_selection (buffer))
    {
      gtk_text_buffer_delete_selection (buffer, TRUE, TRUE);
    }
  else
    {
      GtkTextIter cursor;

      gtk_text_buffer_get_iter_at_mark (buffer, &cursor,
                                        gtk_text_buffer_get_insert (buffer));

      gtk_text_buffer_backspace (buffer, &cursor, TRUE, TRUE);
    }
}
Esempio n. 22
0
static void
gimp_text_style_editor_color_changed (GimpColorButton     *button,
                                      GimpTextStyleEditor *editor)
{
  GtkTextBuffer *buffer = GTK_TEXT_BUFFER (editor->buffer);
  GList         *insert_tags;
  GList         *remove_tags;

  if (gtk_text_buffer_get_has_selection (buffer))
    {
      GtkTextIter start, end;
      GimpRGB     color;

      gtk_text_buffer_get_selection_bounds (buffer, &start, &end);

      gimp_color_button_get_color (button, &color);
      gimp_text_buffer_set_color (editor->buffer, &start, &end, &color);
    }

  insert_tags = gimp_text_style_editor_list_tags (editor, &remove_tags);
  gimp_text_buffer_set_insert_tags (editor->buffer, insert_tags, remove_tags);
}
Esempio n. 23
0
static void
gimp_text_style_editor_font_changed (GimpContext         *context,
                                     GimpFont            *font,
                                     GimpTextStyleEditor *editor)
{
  GtkTextBuffer *buffer = GTK_TEXT_BUFFER (editor->buffer);
  GList         *insert_tags;
  GList         *remove_tags;

  if (gtk_text_buffer_get_has_selection (buffer))
    {
      GtkTextIter start, end;

      gtk_text_buffer_get_selection_bounds (buffer, &start, &end);

      gimp_text_buffer_set_font (editor->buffer, &start, &end,
                                 gimp_context_get_font_name (context));
    }

  insert_tags = gimp_text_style_editor_list_tags (editor, &remove_tags);
  gimp_text_buffer_set_insert_tags (editor->buffer, insert_tags, remove_tags);
}
Esempio n. 24
0
static gboolean
gb_vim_command_search (GtkSourceView  *source_view,
                       const gchar    *command,
                       const gchar    *options,
                       GError        **error)
{
  GtkTextBuffer *buffer;
  const gchar *search_begin = NULL;
  const gchar *search_end = NULL;
  const gchar *replace_begin = NULL;
  const gchar *replace_end = NULL;
  gchar *search_text = NULL;
  gchar *replace_text = NULL;
  gunichar separator;

  g_assert (g_str_has_prefix (command, "%s") || g_str_has_prefix (command, "s"));

  if (*command == '%')
    command++;
  command++;

  separator = g_utf8_get_char (command);
  if (!separator)
    goto invalid_request;

  search_begin = command = g_utf8_next_char (command);

  for (; *command; command = g_utf8_next_char (command))
    {
      if (*command == '\\')
        {
          command = g_utf8_next_char (command);
          if (!*command)
            goto invalid_request;
          continue;
        }

      if (g_utf8_get_char (command) == separator)
        {
          search_end = command;
          break;
        }
    }

  if (!search_end)
    goto invalid_request;

  replace_begin = command = g_utf8_next_char (command);

  for (; *command; command = g_utf8_next_char (command))
    {
      if (*command == '\\')
        {
          command = g_utf8_next_char (command);
          if (!*command)
            goto invalid_request;
          continue;
        }

      if (g_utf8_get_char (command) == separator)
        {
          replace_end = command;
          break;
        }
    }

  if (!replace_end)
    goto invalid_request;

  command = g_utf8_next_char (command);

  if (*command)
    {
      for (; *command; command++)
        {
          switch (*command)
            {
            case 'g':
              break;

            /* what other options are supported? */
            default:
              break;
            }
        }
    }

  search_text = g_strndup (search_begin, search_end - search_begin);
  replace_text = g_strndup (replace_begin, replace_end - replace_begin);

  buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (source_view));

  if (gtk_text_buffer_get_has_selection (buffer))
    {
      GtkTextIter begin;
      GtkTextIter end;

      gtk_text_buffer_get_selection_bounds (buffer, &begin, &end);
      gtk_text_iter_order (&begin, &end);
      gb_vim_do_search_and_replace (buffer, &begin, &end, search_text, replace_text, FALSE);
    }
  else
    gb_vim_do_search_and_replace (buffer, NULL, NULL, search_text, replace_text, TRUE);

  g_free (search_text);
  g_free (replace_text);

  return TRUE;

invalid_request:
  g_set_error (error,
               GB_VIM_ERROR,
               GB_VIM_ERROR_UNKNOWN_OPTION,
               _("Invalid search and replace request"));
  return FALSE;
}
Esempio n. 25
0
static gboolean
gb_vim_command_search (GtkWidget      *active_widget,
                       const gchar    *command,
                       const gchar    *options,
                       GError        **error)
{
  GtkSourceView  *source_view;
  GtkTextBuffer *buffer;
  const gchar *search_begin = NULL;
  const gchar *search_end = NULL;
  const gchar *replace_begin = NULL;
  const gchar *replace_end = NULL;
  gchar *search_text = NULL;
  gchar *replace_text = NULL;
  gunichar separator;
  gboolean confirm_replace = FALSE;

  g_assert (GTK_IS_WIDGET (active_widget));
  g_assert (g_str_has_prefix (command, "%s") || g_str_has_prefix (command, "s"));

  if (IDE_IS_EDITOR_VIEW (active_widget))
    source_view = GTK_SOURCE_VIEW (IDE_EDITOR_VIEW (active_widget)->frame1->source_view);
  else
    return gb_vim_set_source_view_error (error);

  if (*command == '%')
    command++;
  command++;

  separator = g_utf8_get_char (command);
  if (!separator)
    goto invalid_request;

  search_begin = command = g_utf8_next_char (command);

  for (; *command; command = g_utf8_next_char (command))
    {
      if (*command == '\\')
        {
          command = g_utf8_next_char (command);
          if (!*command)
            goto invalid_request;
          continue;
        }

      if (g_utf8_get_char (command) == separator)
        {
          search_end = command;
          break;
        }
    }

  if (!search_end)
    goto invalid_request;

  replace_begin = command = g_utf8_next_char (command);

  for (; *command; command = g_utf8_next_char (command))
    {
      if (*command == '\\')
        {
          command = g_utf8_next_char (command);
          if (!*command)
            goto invalid_request;
          continue;
        }

      if (g_utf8_get_char (command) == separator)
        {
          replace_end = command;
          break;
        }
    }

  if (!replace_end)
    goto invalid_request;

  command = g_utf8_next_char (command);

  if (*command)
    {
      for (; *command; command++)
        {
          switch (*command)
            {
            case 'c':
              confirm_replace = TRUE;
              break;

            case 'g':
              break;

            /* what other options are supported? */
            default:
              break;
            }
        }
    }

  search_text = g_strndup (search_begin, search_end - search_begin);
  replace_text = g_strndup (replace_begin, replace_end - replace_begin);

  if (confirm_replace)
    {
      GVariant *variant;
      GVariantBuilder builder;

      g_variant_builder_init (&builder, G_VARIANT_TYPE_STRING_ARRAY);
      g_variant_builder_add (&builder, "s", search_text);
      g_variant_builder_add (&builder, "s", replace_text);
      variant = g_variant_builder_end (&builder);

      ide_widget_action (GTK_WIDGET (IDE_EDITOR_VIEW (active_widget)->frame1),
                         "frame",
                         "replace-confirm",
                         variant);
      return TRUE;
    }

  buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (source_view));

  if (gtk_text_buffer_get_has_selection (buffer))
    {
      GtkTextIter begin;
      GtkTextIter end;

      gtk_text_buffer_get_selection_bounds (buffer, &begin, &end);
      gtk_text_iter_order (&begin, &end);
      gb_vim_do_search_and_replace (buffer, &begin, &end, search_text, replace_text, FALSE);
    }
  else
    gb_vim_do_search_and_replace (buffer, NULL, NULL, search_text, replace_text, TRUE);

  g_free (search_text);
  g_free (replace_text);

  return TRUE;

invalid_request:
  g_set_error (error,
               GB_VIM_ERROR,
               GB_VIM_ERROR_UNKNOWN_OPTION,
               _("Invalid search and replace request"));
  return FALSE;
}
Esempio n. 26
0
static gboolean
gimp_text_style_editor_update_idle (GimpTextStyleEditor *editor)
{
  GtkTextBuffer *buffer = GTK_TEXT_BUFFER (editor->buffer);

  if (editor->update_idle_id)
    {
      g_source_remove (editor->update_idle_id);
      editor->update_idle_id = 0;
    }

  if (gtk_text_buffer_get_has_selection (buffer))
    {
      GtkTextIter  start, end;
      GtkTextIter  iter;
      GList       *list;
      gboolean     any_toggle_active = TRUE;
      gboolean     font_differs      = FALSE;
      gboolean     color_differs     = FALSE;
      gboolean     size_differs      = FALSE;
      gboolean     baseline_differs  = FALSE;
      gboolean     kerning_differs   = FALSE;
      GtkTextTag  *font_tag          = NULL;
      GtkTextTag  *color_tag         = NULL;
      GtkTextTag  *size_tag          = NULL;
      GtkTextTag  *baseline_tag      = NULL;
      GtkTextTag  *kerning_tag       = NULL;

      gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
      gtk_text_iter_order (&start, &end);

      /*  first, switch all toggles on  */
      for (list = editor->toggles; list; list = g_list_next (list))
        {
          GtkToggleButton *toggle = list->data;

          gimp_text_style_editor_set_toggle (editor, toggle, TRUE);
        }

      /*  and get some initial values  */
      font_tag     = gimp_text_buffer_get_iter_font (editor->buffer,
                                                     &start, NULL);
      color_tag    = gimp_text_buffer_get_iter_color (editor->buffer,
                                                      &start, NULL);
      size_tag     = gimp_text_buffer_get_iter_size (editor->buffer,
                                                     &start, NULL);
      baseline_tag = gimp_text_buffer_get_iter_baseline (editor->buffer,
                                                         &start, NULL);
      kerning_tag  = gimp_text_buffer_get_iter_kerning (editor->buffer,
                                                        &start, NULL);

      for (iter = start;
           gtk_text_iter_in_range (&iter, &start, &end);
           gtk_text_iter_forward_cursor_position (&iter))
        {
          if (any_toggle_active)
            {
              any_toggle_active = FALSE;

              for (list = editor->toggles; list; list = g_list_next (list))
                {
                  GtkToggleButton *toggle = list->data;
                  GtkTextTag      *tag    = g_object_get_data (G_OBJECT (toggle),
                                                               "tag");

                  if (! gtk_text_iter_has_tag (&iter, tag))
                    {
                      gimp_text_style_editor_set_toggle (editor, toggle, FALSE);
                    }
                  else
                    {
                      any_toggle_active = TRUE;
                    }
                }
            }

          if (! font_differs)
            {
              GtkTextTag *tag;

              tag = gimp_text_buffer_get_iter_font (editor->buffer, &iter,
                                                    NULL);

              if (tag != font_tag)
                font_differs = TRUE;
            }

          if (! color_differs)
            {
              GtkTextTag *tag;

              tag = gimp_text_buffer_get_iter_color (editor->buffer, &iter,
                                                     NULL);

              if (tag != color_tag)
                color_differs = TRUE;
            }

          if (! size_differs)
            {
              GtkTextTag *tag;

              tag = gimp_text_buffer_get_iter_size (editor->buffer, &iter,
                                                    NULL);

              if (tag != size_tag)
                size_differs = TRUE;
            }

          if (! baseline_differs)
            {
              GtkTextTag *tag;

              tag = gimp_text_buffer_get_iter_baseline (editor->buffer, &iter,
                                                        NULL);

              if (tag != baseline_tag)
                baseline_differs = TRUE;
            }

          if (! kerning_differs)
            {
              GtkTextTag *tag;

              tag = gimp_text_buffer_get_iter_kerning (editor->buffer, &iter,
                                                       NULL);

              if (tag != kerning_tag)
                kerning_differs = TRUE;
            }

          if (! any_toggle_active &&
              color_differs       &&
              font_differs        &&
              size_differs        &&
              baseline_differs    &&
              kerning_differs)
            break;
       }

      if (font_differs)
        gimp_text_style_editor_set_font (editor, NULL);
      else if (font_tag)
        gimp_text_style_editor_set_font (editor, font_tag);
      else
        gimp_text_style_editor_set_default_font (editor);

      if (color_differs)
        gimp_text_style_editor_set_color (editor, NULL);
      else if (color_tag)
        gimp_text_style_editor_set_color (editor, color_tag);
      else
        gimp_text_style_editor_set_default_color (editor);

      if (size_differs)
        gimp_text_style_editor_set_size (editor, NULL);
      else if (size_tag)
        gimp_text_style_editor_set_size (editor, size_tag);
      else
        gimp_text_style_editor_set_default_size (editor);

      if (baseline_differs)
        gtk_entry_set_text (GTK_ENTRY (editor->baseline_spinbutton), "");
      else
        gimp_text_style_editor_set_baseline (editor, baseline_tag);

      if (kerning_differs)
        gtk_entry_set_text (GTK_ENTRY (editor->kerning_spinbutton), "");
      else
        gimp_text_style_editor_set_kerning (editor, kerning_tag);
    }
  else /* no selection */
    {
      GtkTextIter  cursor;
      GSList      *tags;
      GSList      *tags_on;
      GSList      *tags_off;
      GList       *list;

      gtk_text_buffer_get_iter_at_mark (buffer, &cursor,
                                        gtk_text_buffer_get_insert (buffer));

      tags     = gtk_text_iter_get_tags (&cursor);
      tags_on  = gtk_text_iter_get_toggled_tags (&cursor, TRUE);
      tags_off = gtk_text_iter_get_toggled_tags (&cursor, FALSE);

      for (list = editor->buffer->font_tags; list; list = g_list_next (list))
        {
          GtkTextTag *tag = list->data;

          if ((g_slist_find (tags, tag) &&
               ! g_slist_find (tags_on, tag)) ||
              g_slist_find (tags_off, tag))
            {
              gimp_text_style_editor_set_font (editor, tag);
              break;
            }
        }

      if (! list)
        gimp_text_style_editor_set_default_font (editor);

      for (list = editor->buffer->color_tags; list; list = g_list_next (list))
        {
          GtkTextTag *tag = list->data;

          if ((g_slist_find (tags, tag) &&
               ! g_slist_find (tags_on, tag)) ||
              g_slist_find (tags_off, tag))
            {
              gimp_text_style_editor_set_color (editor, tag);
              break;
            }
        }

      if (! list)
        gimp_text_style_editor_set_default_color (editor);

      for (list = editor->buffer->size_tags; list; list = g_list_next (list))
        {
          GtkTextTag *tag = list->data;

          if ((g_slist_find (tags, tag) &&
               ! g_slist_find (tags_on, tag)) ||
              g_slist_find (tags_off, tag))
            {
              gimp_text_style_editor_set_size (editor, tag);
              break;
            }
        }

      if (! list)
        gimp_text_style_editor_set_default_size (editor);

      for (list = editor->buffer->baseline_tags; list; list = g_list_next (list))
        {
          GtkTextTag *tag = list->data;

          if ((g_slist_find (tags, tag) &&
               ! g_slist_find (tags_on, tag)) ||
              g_slist_find (tags_off, tag))
            {
              gimp_text_style_editor_set_baseline (editor, tag);
              break;
            }
        }

      if (! list)
        gimp_text_style_editor_set_baseline (editor, NULL);

      for (list = editor->toggles; list; list = g_list_next (list))
        {
          GtkToggleButton *toggle = list->data;
          GtkTextTag      *tag    = g_object_get_data (G_OBJECT (toggle),
                                                       "tag");

          gimp_text_style_editor_set_toggle (editor, toggle,
                                             (g_slist_find (tags, tag) &&
                                              ! g_slist_find (tags_on, tag)) ||
                                             g_slist_find (tags_off, tag));
        }

      {
        GtkTextTag *tag;

        tag = gimp_text_buffer_get_iter_kerning (editor->buffer, &cursor, NULL);
        gimp_text_style_editor_set_kerning (editor, tag);
      }

      g_slist_free (tags);
      g_slist_free (tags_on);
      g_slist_free (tags_off);
    }

  return FALSE;
}
Esempio n. 27
0
File: myre.c Progetto: vobiscum/myre
/*-------------------------------textview------------------------------*/
static gboolean
textview2_key_press_event_cb(GtkWidget *winParent, GdkEvent *event, gpointer userdata) {
    gboolean setFalseG = TRUE;

    /*Receive input in search mode*/
    if (inSearch == TRUE)
    {
        unsigned int c = event->key.keyval;
        /*get keyval as char*/
        if( c != GDK_KEY_KP_Enter && c != GDK_Return ) {
            if(isascii(c))/*can not input cjk thus not accounted*/
                strOper++[0] = c;
            else if(c == GDK_BackSpace && strOper > searchStr)
                strOper--;
            else {
                ;/*skip*/
            }
            strOper[0] = '\0';
        }
        else {
            /*highlight and try moving forward to target on Enter*/
            if(strlen(searchStr) != 0) {
                my_text_buffer_highlight(text2, searchStr, 4);
                myre_move_to_string(text2, searchStr, 1);
            }

            inSearch = FALSE;
        }

        if(inSearch)
            gtk_statusbar_push(status,
                               gtk_statusbar_get_context_id(status,"status"),
                               searchStr);
        else
            gtk_statusbar_push(status,
                               gtk_statusbar_get_context_id(status, "status"),
                               "End Search");

        return TRUE;
    }
    switch(event->key.keyval)
    {
    case GDK_i:
        gtk_widget_grab_focus(entry);
        break;

    case GDK_I:
        gtk_widget_grab_focus(entry);
        gtk_editable_set_position((GtkEditable *)entry, 0);
        break;

    case GDK_a:
    case GDK_A:
        gtk_widget_grab_focus(entry);
        gtk_editable_set_position((GtkEditable *)entry, -1);
        break;

    case GDK_x:
    case GDK_X:
        button2_clicked_cb();
        return TRUE;

    case GDK_j:
    case GDK_KEY_Down:
        g_signal_emit_by_name(textview2, "move-cursor", GTK_MOVEMENT_DISPLAY_LINES,  1, inSelect && inCursor, NULL);
        break;

    case GDK_k:
    case GDK_KEY_Up:
        g_signal_emit_by_name(textview2, "move-cursor", GTK_MOVEMENT_DISPLAY_LINES, -1, inSelect && inCursor, NULL);
        break;

    case GDK_h:
    case GDK_KEY_Left:
        g_signal_emit_by_name(textview2, "move-cursor", GTK_MOVEMENT_VISUAL_POSITIONS, -1, inSelect && inCursor, NULL);
        break;

    case GDK_l:
    case GDK_KEY_Right:
        g_signal_emit_by_name(textview2, "move-cursor", GTK_MOVEMENT_VISUAL_POSITIONS,  1, inSelect && inCursor, NULL);
        break;

    case GDK_w:
    case GDK_e:
        g_signal_emit_by_name(textview2, "move-cursor", GTK_MOVEMENT_WORDS,  1, inSelect && inCursor, NULL);
        break;
    case GDK_b:
        g_signal_emit_by_name(textview2, "move-cursor", GTK_MOVEMENT_WORDS, -1, inSelect && inCursor, NULL);
        break;

    case GDK_n:
        myre_move_to_string(text2, searchStr, 1);
        break;
    case GDK_N:
        myre_move_to_string(text2, searchStr, 0);
        break;

    case GDK_g:
        if(inVimG)
        {
            gtk_text_buffer_get_iter_at_offset(text2, &current, 0);
            gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(textview2), &current,
                                         0,    /* 0.0-0.5 screen */
                                         TRUE, /* use xy align   */
                                         0,    /* x */
                                         0     /* y */
                                        );
            gtk_text_buffer_place_cursor(text2, &current);
        }
        inVimG = !inVimG;
        setFalseG = FALSE;
        break;

    case GDK_G:
        gtk_text_iter_forward_to_end (&current);
        gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(textview2), &current,
                                     0,
                                     TRUE,
                                     0,
                                     0
                                    );
        gtk_text_buffer_place_cursor(text2, &current);
        break;

    case GDK_KEY_slash: /* toggle search */
        inSearch = !inSearch;
        if(NULL == searchStr) {
            searchStr = malloc(MAX_LENGTH);
        }
        /* try unmarking last search */
        if (strlen(searchStr) != 0)
        {
            my_text_buffer_highlight(text2, searchStr, 5);
        }
        memset(searchStr, '\0', MAX_LENGTH);
        strOper = searchStr;
        gtk_statusbar_push(status,
                           gtk_statusbar_get_context_id(status, "status"),
                           "--Search--");
        break;

    case GDK_KEY_KP_Enter:
    case GDK_Return:
        /*end search string input*/
        inSearch = FALSE;
        break;

    case GDK_p:
        /* put selection to entry */
        gtk_widget_grab_focus(entry);
        g_signal_emit_by_name(entry, "paste-clipboard", entry, NULL);
        break;

    case GDK_y:
        /* copy to clipboard */
        if(gtk_text_buffer_get_has_selection(text2))
        {
            g_signal_emit_by_name(textview2, "copy-clipboard", GTK_TEXT_VIEW(textview2), NULL);
            g_signal_emit_by_name(textview2, "move-cursor", GTK_MOVEMENT_VISUAL_POSITIONS,  0, FALSE, NULL);
            gtk_statusbar_push(status,
                               gtk_statusbar_get_context_id(status, "status"),
                               "Selection copied to clipboard.");

            /* cursor off after yank */
            inCursor = FALSE;
            gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(textview2), inCursor);
            /* selection off */
            inSelect = FALSE;
        }
        else
            /* vacant yank attempts */
            gtk_statusbar_push(status,
                               gtk_statusbar_get_context_id(status, "status"),
                               "--Search--Empty Yank Ignored");

        break;

    case GDK_v:
        /* toggle select mode */
        if(!inCursor) /* make sure cursor is shown */
        {
            inCursor = TRUE;
            vCursor  = TRUE;
            gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(textview2), inCursor);
        }
        if (inSelect)
        {
            /* clear selection */
            if(gtk_text_buffer_get_has_selection(text2))
                g_signal_emit_by_name(textview2, "move-cursor", GTK_MOVEMENT_VISUAL_POSITIONS,  0, FALSE, NULL);
            gtk_statusbar_push(status,
                               gtk_statusbar_get_context_id(status, "status"),
                               "");

            /* exit vCursor after move-cursor dependency */
            if (inCursor && vCursor)
            {
                gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(textview2), FALSE);
                inCursor = FALSE;
                vCursor  = FALSE;
            }

        }
        else
        {
            gtk_statusbar_push(status,
                               gtk_statusbar_get_context_id(status, "status"),
                               "--Selection--");
        }

        inSelect = !inSelect;
        break;
    case GDK_c:
        /* always expire select mode */
        inSelect = FALSE;
        /* clear selection, should be inCursor */
        if(gtk_text_buffer_get_has_selection(text2))
            g_signal_emit_by_name(textview2, "move-cursor", GTK_MOVEMENT_VISUAL_POSITIONS,  0, FALSE, NULL);
        gtk_statusbar_push(status,
                           gtk_statusbar_get_context_id(status, "status"),
                           "");

        /* show cursor for selection */
        inCursor = !inCursor;
        gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(textview2), inCursor);

        break;


    default:
        return FALSE;
    }

    if(setFalseG)
        inVimG = FALSE;

    if( !(inSearch || inSelect) )
    {
        gtk_text_view_place_cursor_onscreen(GTK_TEXT_VIEW(textview2));
    }
    return TRUE;
}
Esempio n. 28
0
static void
gimp_text_tool_delete_from_cursor (GimpTextTool  *text_tool,
                                   GtkDeleteType  type,
                                   gint           count)
{
  GtkTextBuffer *buffer = GTK_TEXT_BUFFER (text_tool->buffer);
  GtkTextIter    cursor;
  GtkTextIter    end;

  GIMP_LOG (TEXT_EDITING, "%s count = %d",
            g_enum_get_value (g_type_class_ref (GTK_TYPE_DELETE_TYPE),
                              type)->value_name,
            count);

  gimp_text_tool_reset_im_context (text_tool);

  gtk_text_buffer_get_iter_at_mark (buffer, &cursor,
                                    gtk_text_buffer_get_insert (buffer));
  end = cursor;

  switch (type)
    {
    case GTK_DELETE_CHARS:
      if (gtk_text_buffer_get_has_selection (buffer))
        {
          gtk_text_buffer_delete_selection (buffer, TRUE, TRUE);
          return;
        }
      else
        {
          gtk_text_iter_forward_cursor_positions (&end, count);
        }
      break;

    case GTK_DELETE_WORD_ENDS:
      if (count < 0)
        {
          if (! gtk_text_iter_starts_word (&cursor))
            gtk_text_iter_backward_visible_word_starts (&cursor, 1);
        }
      else if (count > 0)
        {
          if (! gtk_text_iter_ends_word (&end) &&
              ! gtk_text_iter_forward_visible_word_ends (&end, 1))
            gtk_text_iter_forward_to_line_end (&end);
        }
      break;

    case GTK_DELETE_WORDS:
      if (! gtk_text_iter_starts_word (&cursor))
        gtk_text_iter_backward_visible_word_starts (&cursor, 1);

      if (! gtk_text_iter_ends_word (&end) &&
          ! gtk_text_iter_forward_visible_word_ends (&end, 1))
        gtk_text_iter_forward_to_line_end (&end);
      break;

    case GTK_DELETE_DISPLAY_LINES:
      break;

    case GTK_DELETE_DISPLAY_LINE_ENDS:
      break;

    case GTK_DELETE_PARAGRAPH_ENDS:
      if (count < 0)
        {
          gtk_text_iter_set_line_offset (&cursor, 0);
        }
      else if (count > 0)
        {
          if (! gtk_text_iter_ends_line (&end))
            gtk_text_iter_forward_to_line_end (&end);
          else
            gtk_text_iter_forward_cursor_positions (&end, 1);
        }
      break;

    case GTK_DELETE_PARAGRAPHS:
      break;

    case GTK_DELETE_WHITESPACE:
      find_whitepace_region (&cursor, &cursor, &end);
      break;
    }

  if (! gtk_text_iter_equal (&cursor, &end))
    {
      gtk_text_buffer_delete_interactive (buffer, &cursor, &end, TRUE);
    }
}
Esempio n. 29
0
static gboolean
gb_vim_command_substitute (GtkWidget    *active_widget,
                           const gchar  *command,
                           const gchar  *options,
                           GError      **error)
{
  IdeSourceView  *source_view;
  GtkTextBuffer *buffer;
  const gchar *search_begin = NULL;
  const gchar *search_end = NULL;
  const gchar *replace_begin = NULL;
  const gchar *replace_end = NULL;
  g_autofree gchar *search_text = NULL;
  g_autofree gchar *replace_text = NULL;
  GtkTextIter *substitute_begin = NULL;
  GtkTextIter *substitute_end = NULL;
  gunichar separator;
  gboolean replace_in_every_line = FALSE;
  gboolean replace_every_occurence_in_line = FALSE;
  gboolean replace_ask_for_confirmation = FALSE;
  GtkTextIter selection_begin, selection_end;

  g_assert (GTK_IS_WIDGET (active_widget));
  g_assert (g_str_has_prefix (command, "%s") || g_str_has_prefix (command, "s"));

  if (IDE_IS_EDITOR_PAGE (active_widget))
    source_view = ide_editor_page_get_view (IDE_EDITOR_PAGE (active_widget));
  else
    return gb_vim_set_source_view_error (error);

  if (command[0] == '%')
    {
      replace_in_every_line = TRUE;
      command++;
    }

  command++;

  separator = g_utf8_get_char (command);
  if (!separator)
    goto invalid_request;

  search_begin = command = g_utf8_next_char (command);

  for (; *command; command = g_utf8_next_char (command))
    {
      if (*command == '\\')
        {
          command = g_utf8_next_char (command);
          if (!*command)
            goto invalid_request;
          continue;
        }

      if (g_utf8_get_char (command) == separator)
        {
          search_end = command;
          break;
        }
    }

  if (search_end == NULL)
    {
      search_text = g_strdup (search_begin);
      replace_text = g_strdup ("");
    }
  else
    {
      search_text = g_strndup (search_begin, search_end - search_begin);

      replace_begin = command = g_utf8_next_char (command);

      for (; *command; command = g_utf8_next_char (command))
        {
          if (*command == '\\')
            {
              command = g_utf8_next_char (command);
              if (!*command)
                goto invalid_request;
              continue;
            }

          if (g_utf8_get_char (command) == separator)
            {
              replace_end = command;
              break;
            }
        }

      if (replace_end == NULL)
        replace_text = g_strdup (replace_begin);
      else
        {
          replace_text = g_strndup (replace_begin, replace_end - replace_begin);
          command = g_utf8_next_char (command);
        }

      if (*command)
        {
          for (; *command; command++)
            {
              switch (*command)
                {
                case 'c':
                  replace_ask_for_confirmation = TRUE;
                  break;

                case 'g':
                  replace_every_occurence_in_line = TRUE;
                  break;

                /* what other options are supported? */
                default:
                  break;
                }
            }
        }
    }

  if (replace_ask_for_confirmation)
    {
      GVariant *variant;
      GVariantBuilder builder;

      g_variant_builder_init (&builder, G_VARIANT_TYPE_STRING_ARRAY);
      g_variant_builder_add (&builder, "s", search_text);
      g_variant_builder_add (&builder, "s", replace_text);
      variant = g_variant_builder_end (&builder);

      dzl_gtk_widget_action (active_widget, "editor-page", "replace-confirm", variant);

      return TRUE;
    }

  buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (source_view));

  if (gtk_text_buffer_get_has_selection (buffer))
    {
      gtk_text_buffer_get_selection_bounds (buffer, &selection_begin, &selection_end);
      substitute_begin = &selection_begin;
      substitute_end = &selection_end;
    }

  gtk_text_buffer_begin_user_action (buffer);
  gb_vim_do_substitute (buffer, substitute_begin, substitute_end, search_text, replace_text, replace_every_occurence_in_line, replace_in_every_line);
  gtk_text_buffer_end_user_action (buffer);

  return TRUE;

invalid_request:
  g_set_error (error,
               GB_VIM_ERROR,
               GB_VIM_ERROR_UNKNOWN_OPTION,
               _("Invalid search and replace request"));
  return FALSE;
}
Esempio n. 30
0
static void
ide_editor_frame_actions_find (GSimpleAction *action,
                              GVariant      *variant,
                              gpointer       user_data)
{
  IdeEditorFrame *self = user_data;
  GtkTextBuffer *buffer;
  GtkDirectionType search_direction;

  g_assert (IDE_IS_EDITOR_FRAME (self));

  gtk_widget_set_visible (GTK_WIDGET (self->replace_entry), FALSE);
  gtk_widget_set_visible (GTK_WIDGET (self->replace_button), FALSE);
  gtk_widget_set_visible (GTK_WIDGET (self->replace_all_button), FALSE);

  search_direction = (GtkDirectionType) g_variant_get_int32 (variant);
  ide_source_view_set_search_direction (self->source_view,
                                        search_direction);

  buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->source_view));

  /*
   * If the buffer currently has a selection, we prime the search entry with the
   * selected text. If not, we use our previous search text in the case that it was
   * cleared by the IdeSourceView internal state.
   */

  if (gtk_text_buffer_get_has_selection (buffer))
    {
      GtkTextIter start_sel;
      GtkTextIter end_sel;
      g_autofree gchar *selected_text = NULL;
      g_autofree gchar *escaped_selected_text = NULL;
      GtkSourceSearchContext *search_context;
      GtkSourceSearchSettings *search_settings;

      gtk_text_buffer_get_selection_bounds (buffer, &start_sel, &end_sel);
      selected_text = gtk_text_buffer_get_text (buffer, &start_sel, &end_sel, FALSE);

      search_context = ide_source_view_get_search_context (self->source_view);
      search_settings = gtk_source_search_context_get_settings (search_context);

      if (gtk_source_search_settings_get_regex_enabled (search_settings))
        escaped_selected_text = g_regex_escape_string (selected_text, -1);
      else
        escaped_selected_text = gtk_source_utils_escape_search_text (selected_text);

      gtk_entry_set_text (GTK_ENTRY (self->search_entry), escaped_selected_text);
    }
  else
    {
      GtkSourceSearchContext *search_context;
      GtkSourceSearchSettings *search_settings;
      const gchar *search_text;

      search_context = ide_source_view_get_search_context (self->source_view);
      search_settings = gtk_source_search_context_get_settings (search_context);
      search_text = gtk_source_search_settings_get_search_text (search_settings);

      if ((search_text != NULL) && (search_text [0] != '\0'))
        gtk_entry_set_text (GTK_ENTRY (self->search_entry), search_text);
      else if (self->previous_search_string != NULL)
        gtk_entry_set_text (GTK_ENTRY (self->search_entry), self->previous_search_string);
    }

  gtk_revealer_set_reveal_child (self->search_revealer, TRUE);
  gtk_widget_grab_focus (GTK_WIDGET (self->search_entry));
}