Re: how to label a cairo drawings in gtk+-2.0
- From: Colomban Wendling <lists ban herbesfolles org>
- To: gtk-app-devel-list gnome org
- Subject: Re: how to label a cairo drawings in gtk+-2.0
- Date: Fri, 04 Oct 2013 13:55:28 +0200
Hi Mahesh,
Le 04/10/2013 07:41, Mahesh Chaudhari a écrit :
Hi List,
I have drawn five circles in GtkDrawingArea as below :
#include <gtk/gtk.h>
#include <cairo.h>
#include<math.h>
GtkWidget* window;
GtkWidget* darea;
static gboolean on_expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
{
cairo_t *cr;
cr = gdk_cairo_create(darea->window);
cairo_arc(cr,50,60,20,0,2*M_PI);
cairo_arc(cr,110,60,20,0,2*M_PI);
cairo_arc(cr,170,60,20,0,2*M_PI);
cairo_arc(cr,230,60,20,0,2*M_PI);
cairo_arc(cr,300,60,20,0,2*M_PI);
cairo_set_source_rgba (cr, 0.5, 0, 0, 0.5);
cairo_fill(cr);
You need to return a boolean here, see
https://developer.gnome.org/gtk2/stable/GtkWidget.html#GtkWidget-expose-event
You also should destroy the cairo_t you created at the start of the
function using cairo_destroy().
}
int main(int argc, char **argv)
{
gtk_init(&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window), 400, 100);
darea = gtk_drawing_area_new();
gtk_container_add(GTK_CONTAINER(window), darea);
g_signal_connect(darea, "expose-event",
G_CALLBACK(on_expose_event), NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
I want to put label(text) above each circle,
Use Pango and its Cairo rendering capabilities:
https://developer.gnome.org/pango/stable/pango-Cairo-Rendering.html
A very basic example could be:
PangoLayout *l = pango_cairo_create_layout (cr);
PangoFontDescription *font = pango_font_description_from_string ("sans
42");
pango_layout_set_text (l, "hello world", -1);
pango_layout_set_font_description (l, font);
pango_font_description_free (font);
pango_layout_set_width (l, width_you_want * PANGO_SCALE);
pango_layout_set_height (l, height_you_want * PANGO_SCALE);
pango_cairo_show_layout (cr, l);
g_object_unref (l);
Check the Pango docs for more details and features
https://developer.gnome.org/pango/stable/index.html
Also colour should be change after few seconds
Put a timeout (g_timeout_add()) and when it fires change the color and
queue a redraw (gtk_widget_queue_draw()).
How can I do this ? I have to use CentOS-5.3 which has gtk+-2.10.0
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]