0

我正在用 C/GTK3+ 设计一个文本编辑器,使用 GtkSourceView 类能够通过方法显示行号 gtk_source_view_set_show_line_numbers。我可以修改 textview 和 text 的背景颜色,但不能修改包含行号的面板的背景颜色。默认情况下,它似乎是白色的。你能告诉我如何修改它吗?

在此处输入图像描述

这是代码:

#include <gtk/gtk.h>
#include <gtksourceview/gtksource.h>

void find (GtkTextView *text_view, const gchar *text, GtkTextIter *iter)
{
  GtkTextIter mstart, mend;
  GtkTextBuffer *buffer;
  gboolean found;

  buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
  found = gtk_text_iter_forward_search (iter, text, 0, &mstart, &mend, NULL);

  if (found)
    {
      gtk_text_buffer_select_range (buffer, &mstart, &mend);
      gtk_text_buffer_create_mark (buffer, "last_pos", &mend, FALSE);
    }
}

typedef struct App 
{
  GtkWidget *text_view;
  GtkWidget *search_entry;
} App;

/* Called when main window is destroyed. */
void
win_destroy (void)
{
  gtk_main_quit();
}

void
next_button_clicked (GtkWidget *next_button, App *app)
{
  const gchar *text;
  GtkTextBuffer *buffer;
  GtkTextMark *last_pos;
  GtkTextIter iter;

  text = gtk_entry_get_text (GTK_ENTRY (app->search_entry));
  
  buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (app->text_view));

  last_pos = gtk_text_buffer_get_mark (buffer, "last_pos");
  if (last_pos == NULL)
    return;

  gtk_text_buffer_get_iter_at_mark (buffer, &iter, last_pos);
  find (GTK_TEXT_VIEW (app->text_view), text, &iter);
}

/* Called when search button is clicked. */
void
search_button_clicked (GtkWidget *search_button, App *app)
{
  const gchar *text;
  GtkTextBuffer *buffer;
  GtkTextIter iter;

  text = gtk_entry_get_text (GTK_ENTRY (app->search_entry));

  buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (app->text_view));
  gtk_text_buffer_get_start_iter (buffer, &iter);
  
  find (GTK_TEXT_VIEW (app->text_view), text, &iter);
}


int 
main (int argc, char *argv[])
{
  GtkWidget *win;
  GtkWidget *vbox;
  GtkWidget *hbox;
  GtkWidget *search_button;
  GtkWidget *next_button;
  GtkWidget *swindow;
  GtkCssProvider *provider;
  GtkStyleContext *context;
  
  App app;

  gtk_init (&argc, &argv);

  /* Create a window with a search entry, search button and a text
     area. */
  win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  g_signal_connect (G_OBJECT (win), "destroy", win_destroy, NULL);

  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL,0);
  gtk_container_add (GTK_CONTAINER (win), vbox);

  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL,0);
  
  gtk_container_add(GTK_CONTAINER (vbox), hbox);
  
  app.search_entry = gtk_entry_new ();
  
  search_button = gtk_button_new_with_label ("Search");
  next_button = gtk_button_new_with_label ("Next");
  
  gtk_container_add(GTK_CONTAINER (hbox), app.search_entry);
  gtk_container_add(GTK_CONTAINER (hbox), search_button);
  gtk_container_add(GTK_CONTAINER (hbox), next_button);

  g_signal_connect (G_OBJECT (search_button), "clicked", 
                    G_CALLBACK (search_button_clicked), &app);
  g_signal_connect (G_OBJECT (next_button), "clicked",
                    G_CALLBACK (next_button_clicked), &app);
                    
  /* A scrolled window which automatically displays horizontal and
     vertical scrollbars when the text exceeds the text view's size. */
  swindow = gtk_scrolled_window_new (NULL, NULL);
  
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
                                  GTK_POLICY_AUTOMATIC,
                                  GTK_POLICY_AUTOMATIC);

  gtk_widget_set_vexpand (swindow, TRUE);
  
  gtk_container_add(GTK_CONTAINER (vbox), swindow);

  app.text_view = gtk_source_view_new ();
  
  provider = gtk_css_provider_new ();
  gtk_css_provider_load_from_data (provider,
        "textview{"
        "font-size: 30px;"
        "font-family: serif;"
        "} text{"
        "color: green;"
        "background: black;"
        "}",        
        -1,
         NULL);

  context = gtk_widget_get_style_context (app.text_view);
  gtk_style_context_add_provider (context,
    GTK_STYLE_PROVIDER (provider),
    GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
  gtk_container_add (GTK_CONTAINER (swindow), app.text_view);
  
  gtk_source_view_set_show_line_numbers (GTK_SOURCE_VIEW((app.text_view)), TRUE);

  gtk_widget_show_all (win);

  gtk_main();
}
4

1 回答 1

0

一般有两种可能。

完成此操作的典型方法不是通过更改 GtkSourceView 上的颜色,而是使用带有gtk_source_buffer_set_style_scheme(). 样式方案有一个"line-number""current-line-number"样式,它可以有一个与之关联的颜色。

另一种通常仅用于显示有关标记的信息的方法是使用GtkSourceMarkwithGtkSourceMarkAttributes:background集。然后,您可以在装订线中显示标记。

一般来说,当您达到一定程度的复杂性时,编写自己的排水沟渲染器来优化问题空间会更容易。

于 2021-07-25T20:28:53.990 回答