Re: Scrolling text in Gtk

there is memory leak problem with your mymarqueetest code will u plz tell me
how to remove that memory leak
plz conact me or reply on pankaj chikhale gmail com its urgent plz reply me
as soon as possible

Micah Carrick wrote:

In the book "Foundations of GTK+ Development", there is a chapter on 
creating custom widgets in which a marquee widget is created (which 
scrolls text across the widget).

You could take a look at the source code from the book available at

- Micah Carrick

  Developer -
  GTK+ Forums -

Tuvok wrote:

What I need to do, is to make a bar with scrolling text inside it (like
the bars with stock prices on tv).

I've tried two methods, both of which are very slow. I've done it with
Canvas and with Cairo. Below follows code.

Did I do something wrong? Is there a way to make this code run faster?
Should I use some different libraries for it?

Rendering 4 such windows (1680x50) takes up 40-50% on both cores (AMD
X2, 2.4GHz).

Method I: Canvas

The rendering part of code:

   GtkWidget *canvasWdg;
   GnomeCanvasItem *text;
   GnomeCanvasGroup *rootGroup;

   ifstream in(filename);
   string textLine;
   getline(in, textLine);

   canvasWdg = gnome_canvas_new();

   gtk_container_add(GTK_CONTAINER(window), canvasWdg);

   gnome_canvas_set_scroll_region(GNOME_CANVAS(canvasWdg), 0.0, 0.0,
   rootGroup = gnome_canvas_root(GNOME_CANVAS(canvasWdg));

   text = gnome_canvas_item_new(rootGroup, GNOME_TYPE_CANVAS_TEXT, "x",
                             0.0, "y", 0.0, "text", textLine.c_str(),
                                "anchor", GTK_ANCHOR_NW,
                                "fill_color", "white", "size",
                                "size-set", TRUE, NULL);

   double x1, x2, y1, y2;
   gnome_canvas_item_get_bounds(GNOME_CANVAS_ITEM(text), &x1, &y1, &x2,
// align the text in window:
   gnome_canvas_item_move(GNOME_CANVAS_ITEM(text), 1.0*width,

   g_timeout_add(40, moveText, text);


and the timeout function:

gboolean moveText(void *text)

-2.0, 0.0);

   return TRUE;

Method II: Cairo

I've made a Gtk widget, which draws with cairo on expose event; I'll put
here only the expose function:

static gboolean mdk_text_scroller_expose(GtkWidget *textScroller,
GdkEventExpose *event)

   MdkTextScroller *ts = MDK_TEXT_SCROLLER(textScroller);

   cairo_t *cr;

   cr = gdk_cairo_create(textScroller->window);



   cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL,

   cairo_set_font_size(cr, ts->size);
   cairo_move_to(cr, ts->x+ts->xOffset, ts->y+ts->yOffset);
   cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
   cairo_show_text(cr, ts->text);

   if (ts->width == 0 || ts->height == 0)
      cairo_text_extents_t ext;
      cairo_text_extents(cr, ts->text, &ext);
      ts->width = ext.width;
      ts->height = ext.height;

   if (ts->width+ts->xOffset+100 < 0)
      cairo_move_to(cr, ts->x+ts->xOffset+ts->width+100, ts->y
      cairo_show_text(cr, ts->text);

   if (ts->x+ts->xOffset+ts->width < 0)



it's then redrawn with the same timeout call:

gboolean moveText(void *text)
   mdk_change_text_position((GtkWidget*)text, -1, 0);

   return TRUE;

gtk-app-devel-list mailing list
gtk-app-devel-list gnome org

View this message in context:
Sent from the Gtk+ - Apps Dev mailing list archive at

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