Re: Scrolling text in Gtk
- From: Micah Carrick <email micahcarrick com>
- To: gtk-app-devel-list gnome org
- Subject: Re: Scrolling text in Gtk
- Date: Wed, 28 May 2008 12:29:40 -0700
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
www.gtkbook.com
- Micah Carrick
Developer - http://www.micahcarrick.com
GTK+ Forums - http://www.gtkforums.com
Tuvok wrote:
Hi,
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,
width*1.0,
height*1.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",
(int)((height*0.7)*1000),
"size-set", TRUE, NULL);
double x1, x2, y1, y2;
gnome_canvas_item_get_bounds(GNOME_CANVAS_ITEM(text), &x1, &y1, &x2,
&y2);
// align the text in window:
gnome_canvas_item_move(GNOME_CANVAS_ITEM(text), 1.0*width,
((height*1.0)/2-(y2-y1)/2));
g_timeout_add(40, moveText, text);
gtk_widget_show_all(window);
and the timeout function:
gboolean moveText(void *text)
{
gnome_canvas_item_move(GNOME_CANVAS_ITEM((GnomeCanvasItem*)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);
gdk_cairo_region(cr,
event->region);
cairo_clip(cr);
cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL,
CAIRO_FONT_WEIGHT_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
+ts->yOffset);
cairo_show_text(cr, ts->text);
}
if (ts->x+ts->xOffset+ts->width < 0)
{
ts->xOffset=ts->xOffset+ts->width+100;
}
cairo_stroke(cr);
cairo_destroy(cr);
}
it's then redrawn with the same timeout call:
gboolean moveText(void *text)
{
mdk_change_text_position((GtkWidget*)text, -1, 0);
gtk_widget_queue_draw((GtkWidget*)text);
return TRUE;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]