GtkTreeView overlay using cairo



Hello List,

I want to draw on top of a gtktreeview. This is what I came up with. Looking for some expert advice on how I can improve it.

thanks,

Vik.


/*
gcc -g -Wall `pkg-config gtk+-2.0 --cflags --libs` hello.c   -o hello
*/

#include <gtk/gtk.h>
#include <cairo.h>

gboolean paint(GtkTreeView *widget, GdkEventExpose *event, gpointer udata){
  cairo_t *cr;
  cr = gdk_cairo_create (gtk_tree_view_get_bin_window(widget));
  cairo_select_font_face (cr,"Sans",CAIRO_FONT_SLANT_NORMAL,
    CAIRO_FONT_WEIGHT_BOLD);
  cairo_set_font_size (cr, 72);
  cairo_move_to (cr, 50, 150);
  cairo_set_source_rgba (cr, 0.3,0.3,0.3,0.5);
  cairo_set_operator(cr,CAIRO_OPERATOR_MULTIPLY);
  cairo_show_text (cr, "Hello World");
  cairo_destroy (cr);
  return FALSE;
}

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

  GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  g_signal_connect (G_OBJECT (window), "delete-event",
    G_CALLBACK(gtk_main_quit), NULL);

  gpointer swindow = gtk_scrolled_window_new(NULL,NULL);
  GtkAdjustment *vadj = gtk_scrolled_window_get_vadjustment(swindow);

  gpointer list = gtk_list_store_new(1,G_TYPE_STRING);
  gpointer view = gtk_tree_view_new_with_model(list);

  g_signal_connect_swapped(vadj,"value-changed",
    G_CALLBACK(gtk_widget_queue_draw),view);

  gtk_tree_view_insert_column_with_attributes(view,0,"strings",
    gtk_cell_renderer_text_new(),"text",0,NULL);

  int i;
  GtkTreeIter iter;
  for(i=0;i<200;i++){
    gtk_list_store_insert_before(list,&iter,NULL);
    gtk_list_store_set(list,&iter,0,"This is a really cool line of text ok",-1);
  }

  gtk_widget_set_size_request (view,640,480);
  g_signal_connect_after(view, "expose-event",G_CALLBACK(paint),NULL);

  gtk_container_add (GTK_CONTAINER (swindow), view);
  gtk_container_add (GTK_CONTAINER (window), swindow);

  gtk_widget_show_all (window);
  gtk_main ();
  return 0;
}





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