Re: Problem with sizes of pixbuf in left gutter



 
It looks like the pixbuf sizes rendered are going to be tied to the size of the font in the sourceview. If 
the pixbufs are bigger than the gutter has space for then they get clipped. I don't know how the sourceview 
works very well myself. Gave it a try. Maybe it will help.
Eric
//gcc -Wall source_view2.c -o source_view2 `pkg-config --cflags --libs gtk+-3.0 gtksourceview-3.0`
//Tested with GTK3.18 and Ubuntu16.04

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

static GdkPixbuf* draw_pixbuf1();
static GdkPixbuf* draw_pixbuf2();
static void redraw_gutter(GtkWidget *button, gpointer renderer);
static void check_renderer1(GtkSourceGutterRenderer *renderer, GtkTextIter *start, GtkTextIter *end, 
GtkSourceGutterRendererState state, GdkPixbuf *pixbufs[2]);

int main (int argc, char *argv[])
  {
    gtk_init(&argc, &argv);

    GtkWidget *window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "Source View");
    gtk_window_set_default_size(GTK_WINDOW(window), 400, 400);
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
    
    //Highlight for the C language.
    GtkSourceLanguageManager *lm=gtk_source_language_manager_get_default();
    GtkSourceLanguage *language=gtk_source_language_manager_get_language(lm, "c");
    GtkSourceBuffer *s_buffer=gtk_source_buffer_new_with_language(language);
    gtk_source_buffer_set_language(s_buffer, language);
    gtk_source_buffer_set_highlight_syntax(s_buffer, TRUE);

    GtkWidget *source_view=gtk_source_view_new_with_buffer(s_buffer);
    gtk_source_view_set_show_line_numbers(GTK_SOURCE_VIEW(source_view), TRUE);
    gtk_source_view_set_show_line_marks(GTK_SOURCE_VIEW(source_view), TRUE);
    gtk_source_view_set_highlight_current_line(GTK_SOURCE_VIEW(source_view), TRUE);
    gtk_widget_set_vexpand(source_view, TRUE);
    gtk_widget_set_hexpand(source_view, TRUE);

    //Get some pixbufs to test.
    GdkPixbuf *pixbuf1=draw_pixbuf1();
    GdkPixbuf *pixbuf2=draw_pixbuf2();
    GdkPixbuf *pixbufs[2]={pixbuf1, pixbuf2};

    //Get the left gutter.
    GtkSourceGutter *gutter=gtk_source_view_get_gutter(GTK_SOURCE_VIEW(source_view), GTK_TEXT_WINDOW_LEFT);

    GtkSourceGutterRenderer *renderer1=gtk_source_gutter_renderer_pixbuf_new();
    g_signal_connect(renderer1, "query-data", G_CALLBACK(check_renderer1), pixbufs);
    gtk_source_gutter_renderer_set_alignment_mode(renderer1, GTK_SOURCE_GUTTER_RENDERER_ALIGNMENT_MODE_FIRST);
    gtk_source_gutter_renderer_set_size(renderer1, 32);
    gtk_source_gutter_insert(gutter, GTK_SOURCE_GUTTER_RENDERER(renderer1), 0);

    //Create a mark on the first line.
    GtkTextIter start;
    gtk_text_buffer_get_start_iter(GTK_TEXT_BUFFER(s_buffer), &start);
    gtk_source_buffer_create_source_mark(s_buffer, "mark_name", "mark_category", &start);
    
    //Color the first line green.
    GdkRGBA bg;
    gdk_rgba_parse(&bg, "#00FF00");
    GtkSourceMarkAttributes *att=gtk_source_mark_attributes_new();
    gtk_source_mark_attributes_set_background(att, &bg);
    gtk_source_view_set_mark_attributes(GTK_SOURCE_VIEW(source_view), "mark_category", att, 1);

    gtk_text_buffer_set_text(GTK_TEXT_BUFFER(s_buffer), "//gcc -Wall hello1.c -o 
hello1\n#include<stdio.h>\n\nint main()\n  {\n    printf(\"Hello World\\n\");\n    return 0;\n  }\n", -1);

    GtkWidget *button=gtk_button_new_with_label("Redraw Gutter");
    gtk_widget_set_hexpand(button, TRUE);
    g_signal_connect(button, "clicked", G_CALLBACK(redraw_gutter), renderer1);

    GtkWidget *grid=gtk_grid_new();
    gtk_grid_attach(GTK_GRID(grid), source_view, 0, 0, 1, 1);
    gtk_grid_attach(GTK_GRID(grid), button, 0, 1, 1, 1);
    
    gtk_container_add(GTK_CONTAINER(window), grid);

     //Setup CSS for the program.
    GError *css_error=NULL;
    gint minor_version=gtk_get_minor_version();
    gchar *css_string1=NULL;
    //GTK CSS changed in 3.20.
    if(minor_version>20)
      {
        css_string1=g_strdup("textview{background-color: blue; color: yellow; font-size: 26px;}");
      }
    else
      {
        //On GTK3.18 background works and background-color doesn't.
        css_string1=g_strdup("GtkTextView{background: blue; color: yellow; font-size: 26px;}");
      }
    GtkCssProvider *provider=gtk_css_provider_new();
    GdkDisplay *display=gdk_display_get_default();
    GdkScreen *screen=gdk_display_get_default_screen(display);
    gtk_style_context_add_provider_for_screen(screen, GTK_STYLE_PROVIDER(provider), 
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
    gtk_css_provider_load_from_data(provider, css_string1, -1, &css_error);
    //Print out the provider for some help.
    if(css_error==NULL)
      {
        gchar *string=gtk_css_provider_to_string(provider);
        g_print("%s\n", string);
        g_free(string);
      }
    else
      {
        g_print("CSS loader error %s\n", css_error->message);
        g_error_free(css_error);
      }
    g_object_unref(provider);
    g_free(css_string1);

    gtk_widget_show_all(window);

    gtk_main();

    g_object_unref(pixbuf1);
    g_object_unref(pixbuf2);

    return 0;
  }
static void redraw_gutter(GtkWidget *button, gpointer renderer)
  {
    g_print("Queue Draw\n");
    gtk_source_gutter_renderer_queue_draw(GTK_SOURCE_GUTTER_RENDERER(renderer));   
  }
static void check_renderer1(GtkSourceGutterRenderer *renderer, GtkTextIter *start, GtkTextIter *end, 
GtkSourceGutterRendererState state, GdkPixbuf *pixbufs[2])
  {
    static gint i=1;
    g_print("Check Renderer1 %i\n", i);
    if(i%2==0) gtk_source_gutter_renderer_pixbuf_set_pixbuf(GTK_SOURCE_GUTTER_RENDERER_PIXBUF(renderer), 
pixbufs[0]);
    else gtk_source_gutter_renderer_pixbuf_set_pixbuf(GTK_SOURCE_GUTTER_RENDERER_PIXBUF(renderer), 
pixbufs[1]);
    i++;
  }
static GdkPixbuf* draw_pixbuf1()
  { 
    cairo_surface_t *surface=cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 32, 32);
    cairo_t *cr=cairo_create(surface);
    
    cairo_set_source_rgba(cr, 0.0, 1.0, 1.0, 1.0);
    cairo_paint(cr);

    cairo_set_source_rgba(cr, 1.0, 0.0, 1.0, 1.0);
    cairo_translate(cr, 32/2, 32/2);
    cairo_arc(cr, 0, 0, 5, 0, 2.0*G_PI);
    cairo_fill(cr); 
    
    GdkPixbuf *pixbuf_drawing=gdk_pixbuf_get_from_surface(surface, 0, 0, 32, 32);

    cairo_destroy(cr);
    cairo_surface_destroy(surface); 
    return pixbuf_drawing;
  }
static GdkPixbuf* draw_pixbuf2()
  { 
    cairo_surface_t *surface=cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 32, 32);
    cairo_t *cr=cairo_create(surface);
    
    cairo_set_source_rgba(cr, 0.0, 1.0, 1.0, 1.0);
    cairo_paint(cr);

    cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 1.0);
    cairo_translate(cr, 32/2, 32/2);
    cairo_arc(cr, 0, 0, 5, 0, 2.0*G_PI);
    cairo_fill(cr); 
    
    GdkPixbuf *pixbuf_drawing=gdk_pixbuf_get_from_surface(surface, 0, 0, 32, 32);

    cairo_destroy(cr);
    cairo_surface_destroy(surface); 
    return pixbuf_drawing;
  }


 
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]