0

I'm designing a text editor in C/GTK3+, using the GtkSourceView class to be able to show the line numbers via the method gtk_source_view_set_show_line_numbers. I'm able to modify the background color of textview and text but not that of panel containing line numbers. By default it seems to be white. Can you show me how to modify it?

enter image description here

This is the code:

#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();
}
gioretikto
  • 229
  • 2
  • 9

1 Answers1

0

There are generally two possibilities.

The typical way this is done is not by changing colors on the GtkSourceView, but to instead use a style scheme with gtk_source_buffer_set_style_scheme(). The style scheme has a "line-number" and "current-line-number" style which can have a color associated with it.

Another way, that is typically only used to show information about marks, is to have a GtkSourceMark with GtkSourceMarkAttributes:background set. Then you can show marks in the gutter.

Generally speaking, when you get to certain levels of complexity, it's just easier to write your own gutter renderer to optimize for the problem space.