Пример #1
0
void Highlight::highlight()
// This does the actual highlighting.
// Ensure that applying a tag does not change the modified status of the textbuffer.
{
  bool cursor_set = false;
  for (unsigned int i = 0; i < highlightwordstarts.size(); i++) {
    GtkTextIter start, end;
    gtk_text_buffer_get_iter_at_offset(highlightbuffers[i], &start, highlightwordstarts[i]);
    gtk_text_buffer_get_iter_at_offset(highlightbuffers[i], &end, highlightwordends[i]);
    bool modified_status = gtk_text_buffer_get_modified(highlightbuffers[i]);
    gtk_text_buffer_apply_tag(highlightbuffers[i], mytag, &start, &end);
    if (!modified_status)
      gtk_text_buffer_set_modified(highlightbuffers[i], false);
    if (!cursor_set) {
      gtk_text_buffer_place_cursor(highlightbuffers[i], &start);
      screen_scroll_to_iterator(highlightviews[i], &start);
      cursor_set = true;
    }
  }
}
Пример #2
0
void MergeDialog::load_text(ustring text)
{
  // Variables for loading text in the textview.
  size_t pos;
  GtkTextIter iter;

  // Preprocess empty replacements.
  preprocess_empty_replacements(text);

  // Goo through the text looking for markers and processing them.
  pos = text.find(merge_conflict_markup(1));
  while (pos != string::npos) {

    // Insert the bit of text before the first conflict marker.
    gtk_text_buffer_get_end_iter(textbuffer, &iter);
    gtk_text_buffer_insert(textbuffer, &iter, text.substr(0, pos).c_str(), -1);
    text.erase(0, pos + merge_conflict_markup(1).length());

    // Retrieve the first alternative.
    ustring alternative1;
    pos = text.find(merge_conflict_markup(2));
    if (pos != string::npos) {
      alternative1 = text.substr(1, pos - 2);
      text.erase(0, pos + merge_conflict_markup(2).length());
      if (alternative1.empty())
        alternative1 = empty_text();
    }
    // Insert a button with the first alternative as a label.
    gtk_text_buffer_get_end_iter(textbuffer, &iter);
    GtkTextChildAnchor *childanchor1 = gtk_text_buffer_create_child_anchor(textbuffer, &iter);
    GtkWidget *button1 = gtk_button_new_with_label(alternative1.c_str());
    gtk_text_view_add_child_at_anchor(GTK_TEXT_VIEW(textview), button1, childanchor1);
    gtk_widget_show_all(button1);
    g_signal_connect((gpointer) button1, "clicked", G_CALLBACK(on_mergebutton_clicked), gpointer(this));

    // Store data about first alternative.
    MergeButton mergebutton1;
    mergebutton1.childanchor = childanchor1;
    mergebutton1.button = button1;
    mergebutton1.text = alternative1;

    // Retrieve the second alternative.
    ustring alternative2;
    pos = text.find(merge_conflict_markup(3));
    if (pos != string::npos) {
      alternative2 = text.substr(1, pos - 2);
      text.erase(0, pos + merge_conflict_markup(3).length());
      if (alternative2.empty())
        alternative2 = empty_text();
    }
    // Insert a button with the second alternative as a label.
    gtk_text_buffer_get_end_iter(textbuffer, &iter);
    GtkTextChildAnchor *childanchor2 = gtk_text_buffer_create_child_anchor(textbuffer, &iter);
    GtkWidget *button2 = gtk_button_new_with_label(alternative2.c_str());
    gtk_text_view_add_child_at_anchor(GTK_TEXT_VIEW(textview), button2, childanchor2);
    gtk_widget_show_all(button2);
    g_signal_connect((gpointer) button2, "clicked", G_CALLBACK(on_mergebutton_clicked), gpointer(this));

    // Store data about second alternative.
    MergeButton mergebutton2;
    mergebutton2.childanchor = childanchor2;
    mergebutton2.button = button2;
    mergebutton2.text = alternative2;

    // Store the button pair.
    MergeButtonPair mergebuttonpair;
    mergebuttonpair.button1 = mergebutton1;
    mergebuttonpair.button2 = mergebutton2;
    buttonpairs.push_back(mergebuttonpair);

    // Next iteration.
    pos = text.find(merge_conflict_markup(1));
  }

  // Load remaining text in textview.
  gtk_text_buffer_get_end_iter(textbuffer, &iter);
  gtk_text_buffer_insert(textbuffer, &iter, text.substr(0, pos).c_str(), -1);

  // Scroll to beginning of buffer.
  gtk_text_buffer_get_start_iter(textbuffer, &iter);
  gtk_text_buffer_place_cursor(textbuffer, &iter);
  screen_scroll_to_iterator(GTK_TEXT_VIEW(textview), &iter);
}