GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Nov 01, 2014 5:29 am

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: PangoLayout fit pixels
PostPosted: Wed Feb 29, 2012 12:30 pm 
Offline
GTK+ Geek

Joined: Mon Mar 30, 2009 12:40 pm
Posts: 69
Location: España
Hi!

I want to print (in a printer) a GtkTreeview as I see in the screen.
But i can't get the exactly number of characters show in a column. I can get th entire text but not the text showned.
Then I try to fit the text i get in a PangoLayout in a width in pixels, but I cant get pixels, but i can't set the witdh in pixels, only in bytes.

How I cant put the with in a PangoLayout in pixels?.

Maybe this example code explain it better:

Code:
gint print_text(
            gchar *Text,
            gdouble X,
            gdouble Y,
            gint W,
            GtkPrintContext *context,
            cairo_t *cr)
{
PangoLayout *layout;
PangoFontDescription *font;
gint w,h;

layout = gtk_print_context_create_pango_layout(context);

pango_layout_set_text(layout, Text, -1);

font = pango_font_description_from_string("Arial 6");
pango_layout_set_font_description (layout,font);
pango_font_description_free(font);

//pango_layout_get_pixel_size(layout,&w,&h);


//How can I set the W (width) to the layout to limit this width to column width???????


cairo_move_to(cr,X,Y);   

pango_cairo_layout_path(cr,layout);


cairo_stroke_preserve(cr);
cairo_fill(cr);

g_object_unref(layout);

return h;
}     


static void draw_page (GtkPrintOperation *operation,GtkPrintContext *context,gint page_nr,GtkTreeView *tree)
{
cairo_t *cr;

cr = gtk_print_context_get_cairo_context(context);

GtkTreeModel *model;
GtkTreePath *path;
GtkTreeViewColumn *column;
GtkTreeIter iter;
GValue value = {0};
GString *text;
gint col_num;
gint x,y,w,h;
GdkRectangle rect;
gboolean visible;

texto = g_string_new("");

model = gtk_tree_view_get_model(GTK_TREE_VIEW(tree));
path = gtk_tree_path_new_first();

while(gtk_tree_model_get_iter(model,&iter,path) == TRUE)
     {
     x = 0
     for(col_num=0;col_num < gtk_tree_model_get_n_columns(model);col_num++)
        {
        column = gtk_tree_view_get_column(GTK_TREE_VIEW(tree),col_num);
        visible = gtk_tree_view_column_get_visible(column);
       
        if(visible1)
          {
         
          gtk_tree_model_get_value(modelo,&iterador,col_num,&valor);
       
          switch(G_VALUE_TYPE(&value))
                {
                case G_TYPE_STRING:
                     {
                     if(g_value_get_string(&value) != NULL) g_string_assign(text,g_value_get_string(&value));
                     } break;
                case G_TYPE_INT:
                     {
                     g_string_printf(text,"%d",g_value_get_int(&value));
                     } break;
                case G_TYPE_ULONG:
                     {
                     g_string_printf(text,"%lu",g_value_get_ulong(&value));
                     } break;
                }
     
          g_value_unset(&value);
         
          gtk_tree_view_get_cell_area(GTK_TREE_VIEW(tree),path,columna,&rect);
          h = rect.height;
         
          gtk_tree_view_get_background_area(GTK_TREE_VIEW(tree),NULL,column,&rect);
         
          w = rect.width;
         
         
          print_text(text->str,x,y,w,context,cr);
         
         
          x += (w+2);
         
          }
       
        }
     y += h;
     gtk_tree_path_next(path);
     }
gtk_tree_path_free(path);     
g_string_free(text,TRUE);


Thanks (i excuse my poor english)


Top
 Profile  
 
 Post subject: Re: PangoLayout fit pixels
PostPosted: Thu Mar 01, 2012 8:23 am 
Offline
GTK+ Geek

Joined: Mon Mar 30, 2009 12:40 pm
Posts: 69
Location: España
Ask myself:

Code:
gint print_text(
            gchar *Text,
            gdouble X,
            gdouble Y,
            gint W,
            GtkPrintContext *context,
            cairo_t *cr)
{
PangoLayout *layout;
PangoFontDescription *font;
gint w,h;

layout = gtk_print_context_create_pango_layout(context);

pango_layout_set_text(layout, Text, -1);

font = pango_font_description_from_string("Arial 6");
pango_layout_set_font_description (layout,font);
pango_font_description_free(font);

pango_layout_set_width(layout,W*PANGO_SCALE);
pango_layout_set_wrap(layout,PANGO_WRAP_CHAR);   
pango_layout_set_ellipsize(layout,PANGO_ELLIPSIZE_END);

cairo_move_to(cr,X,Y);   

pango_cairo_layout_path(cr,layout);


cairo_stroke_preserve(cr);
cairo_fill(cr);

g_object_unref(layout);

return h;
}     


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group