heavy icon drawing sample
[lttv.git] / ltt / branches / poly / lttv / modules / guiControlFlow / Draw_Item.c
index 4ccd30e8760ab74f7fd4d27047d647c97b81e7b6..70befcc4005a93308be4a29faefaa5d7cf41a4d3 100644 (file)
  *   GQuark.) The habitual place for xpm icons is in
  *   ${prefix}/share/LinuxTraceToolkit.) + position (over or under line)
  * - line (color, width, style)
- * - Arc (can be seen as points) (color, size)
+ * - Arc (big points) (color, size)
  * - background color (color)
  *
+ * An item is a leaf of the attributes tree. It is, in that case, including
+ * all kind of events categories we can have. It then associates each category
+ * with one or more actions (drawing something) or nothing.
+ * 
  * Each item has an array of hooks (hook list). Each hook represents an
  * operation to perform. We seek the array each time we want to
  * draw an item. We execute each operation in order. An operation type
  * We use the lttv global attributes to keep track of the loaded icons.
  * If we need an icon, we look for it in the icons / icon name pathname.
  * If found, we use the pointer to it. If not, we load the pixmap in
- * memory and set the pointer to the GdkPixmap in the attributes.
+ * memory and set the pointer to the GdkPixmap in the attributes. The
+ * structure pointed to contains the pixmap and the mask bitmap.
  * 
  * Author : Mathieu Desnoyers, October 2003
  */
 
 #include <glib.h>
+#include <gtk/gtk.h>
+#include <gdk/gdk.h>
 #include <lttv/hook.h>
 #include <lttv/attribute.h>
 #include <lttv/iattribute.h>
+#include <string.h>
 
 #include <lttv/processTrace.h>
 #include <lttv/state.h>
 
+#include "Draw_Item.h"
+
+
+#define MAX_PATH_LEN 256
+
 /* The DrawContext keeps information about the current drawing position and
  * the previous one, so we can use both to draw lines.
  *
  * over : position for drawing over the middle line.
  * middle : middle line position.
  * under : position for drawing under the middle line.
+ *
+ * the modify_* are used to take into account that we should go forward
+ * when we draw a text, an arc or an icon, while it's unneeded when we
+ * draw a line or background.
+ *
  */
 struct _DrawContext {
        GdkDrawable     *drawable;
        GdkGC           *gc;
+       
 
        DrawInfo        *Current;
        DrawInfo        *Previous;
@@ -96,6 +115,10 @@ struct _DrawInfo {
        ItemInfo        *over;
        ItemInfo        *middle;
        ItemInfo        *under;
+       
+       ItemInfo        *modify_over;
+       ItemInfo        *modify_middle;
+       ItemInfo        *modify_under;
 };
 
 /* LttvExecutionState is accessible through the LttvTracefileState. Is has
@@ -121,6 +144,14 @@ struct _ItemInfo {
        LttvTracefileState      *tfs;
 };
 
+/*
+ * Structure used to keep information about icons.
+ */
+struct _IconStruct {
+       GdkPixmap *pixmap;
+       GdkBitmap *mask;
+};
+
 
 /*
  * The Item element is only used so the DrawOperation is modifiable by users.
@@ -141,11 +172,7 @@ struct _DrawOperation {
  * is in fact a rectangle, does not hide the line.
  */
 
-typedef enum _DrawableItems {
-       ITEM_TEXT, ITEM_ICON, ITEM_LINE, ITEM_POINT, ITEM_BACKGROUND
-} DrawableItems;
-
-static gchar * Items_Priorities = {
+static int Items_Priorities[] = {
        50,     /* ITEM_TEXT */
        40,     /* ITEM_ICON */
        20,     /* ITEM_LINE */
@@ -153,10 +180,6 @@ static gchar * Items_Priorities = {
        10      /* ITEM_BACKGROUND */
 };
 
-typedef enum _RelPos {
-       OVER, MIDDLE, UNDER
-} RelPos;
-
 /*
  * Here are the different structures describing each item type that can be
  * drawn. They contain the information necessary to draw the item : not the
@@ -199,42 +222,252 @@ struct _PropertiesBG {
 };
 
 
-
-
-
 /* Drawing hook functions */
-gboolean DrawText( void *hook_data, void *call_data)
+gboolean draw_text( void *hook_data, void *call_data)
 {
        PropertiesText *Properties = (PropertiesText*)hook_data;
        DrawContext *Draw_Context = (DrawContext*)call_data;
+
+       PangoContext *context;
+       PangoLayout *layout;
+       PangoFontDescription *FontDesc;// = pango_font_description_new();
+       gint Font_Size;
+       PangoRectangle ink_rect;
+               
+       gdk_gc_set_foreground(Draw_Context->gc, Properties->foreground);
+       gdk_gc_set_background(Draw_Context->gc, Properties->background);
+
+       layout = gtk_widget_create_pango_layout(GTK_WIDGET(Draw_Context->drawable), NULL);
+       context = pango_layout_get_context(layout);
+       FontDesc = pango_context_get_font_description(context);
+       Font_Size = pango_font_description_get_size(FontDesc);
+       pango_font_description_set_size(FontDesc, Properties->size*PANGO_SCALE);
+       
+       
+       pango_layout_set_text(layout, Properties->Text, -1);
+       pango_layout_get_pixel_extents(layout, &ink_rect, NULL);
+       switch(Properties->position) {
+               case OVER:
+                                                       gdk_draw_layout(Draw_Context->drawable, Draw_Context->gc,
+                                                               Draw_Context->Current->modify_over->x,
+                                                               Draw_Context->Current->modify_over->y,
+                                                               layout);
+                                                       Draw_Context->Current->modify_over->x += ink_rect.width;
+
+                       break;
+               case MIDDLE:
+                                                       gdk_draw_layout(Draw_Context->drawable, Draw_Context->gc,
+                                                               Draw_Context->Current->modify_middle->x,
+                                                               Draw_Context->Current->modify_middle->y,
+                                                               layout);
+                                                       Draw_Context->Current->modify_middle->x += ink_rect.width;
+                       break;
+               case UNDER:
+                                                       gdk_draw_layout(Draw_Context->drawable, Draw_Context->gc,
+                                                               Draw_Context->Current->modify_under->x,
+                                                               Draw_Context->Current->modify_under->y,
+                                                               layout);
+                                                       Draw_Context->Current->modify_under->x += ink_rect.width;
+                       break;
+       }
+
+
+       pango_font_description_set_size(FontDesc, Font_Size);
+       g_free(layout);
+       
+       return 0;
 }
 
-gboolean DrawIcon( void *hook_data, void *call_data)
+
+/* To speed up the process, search in already loaded icons list first. Only
+ * load it if not present.
+ */
+gboolean draw_icon( void *hook_data, void *call_data)
 {
        PropertiesIcon *Properties = (PropertiesIcon*)hook_data;
        DrawContext *Draw_Context = (DrawContext*)call_data;
 
+  LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes());
+       LttvAttributeValue value;
+       gchar icon_name[MAX_PATH_LEN] = "icons/";
+       IconStruct *icon_info;
+       
+       strcat(icon_name, Properties->icon_name);
+       
+  g_assert(lttv_iattribute_find_by_path(attributes, icon_name,
+      LTTV_POINTER, &value));
+       if(*(value.v_pointer) == NULL)
+       {
+       *(value.v_pointer) = icon_info = g_new(IconStruct,1);
+               
+               icon_info->pixmap = gdk_pixmap_create_from_xpm(Draw_Context->drawable,
+                                                                                                       &icon_info->mask, NULL, Properties->icon_name);
+       }
+       else
+       {
+               icon_info = *(value.v_pointer);
+       }
+       
+       gdk_gc_set_clip_mask(Draw_Context->gc, icon_info->mask);
+       
+       switch(Properties->position) {
+               case OVER:
+                                                       gdk_gc_set_clip_origin(
+                                                                       Draw_Context->gc,
+                                                                       Draw_Context->Current->modify_over->x,
+                                                                       Draw_Context->Current->modify_over->y);
+                                                       gdk_draw_drawable(Draw_Context->drawable, 
+                                                                       Draw_Context->gc,
+                                                                       icon_info->pixmap,
+                                                                       0, 0,
+                                                                       Draw_Context->Current->modify_over->x,
+                                                                       Draw_Context->Current->modify_over->y,
+                                                                       Properties->width, Properties->height);
+
+                                                       Draw_Context->Current->modify_over->x += Properties->width;
+
+                       break;
+               case MIDDLE:
+                                                       gdk_gc_set_clip_origin(
+                                                                       Draw_Context->gc,
+                                                                       Draw_Context->Current->modify_middle->x,
+                                                                       Draw_Context->Current->modify_middle->y);
+                                                       gdk_draw_drawable(Draw_Context->drawable, 
+                                                                       Draw_Context->gc,
+                                                                       icon_info->pixmap,
+                                                                       0, 0,
+                                                                       Draw_Context->Current->modify_middle->x,
+                                                                       Draw_Context->Current->modify_middle->y,
+                                                                       Properties->width, Properties->height);
+
+                                                       Draw_Context->Current->modify_middle->x += Properties->width;
+                       break;
+               case UNDER:
+                                                       gdk_gc_set_clip_origin(
+                                                                       Draw_Context->gc,
+                                                                       Draw_Context->Current->modify_under->x,
+                                                                       Draw_Context->Current->modify_under->y);
+                                                       gdk_draw_drawable(Draw_Context->drawable, 
+                                                                       Draw_Context->gc,
+                                                                       icon_info->pixmap,
+                                                                       0, 0,
+                                                                       Draw_Context->Current->modify_under->x,
+                                                                       Draw_Context->Current->modify_under->y,
+                                                                       Properties->width, Properties->height);
+
+                                                       Draw_Context->Current->modify_under->x += Properties->width;
+                       break;
+       }
+
+       gdk_gc_set_clip_origin(Draw_Context->gc, 0, 0);
+       gdk_gc_set_clip_mask(Draw_Context->gc, NULL);
+       
+       return 0;
 }
 
-gboolean DrawLine( void *hook_data, void *call_data)
+gboolean draw_line( void *hook_data, void *call_data)
 {
        PropertiesLine *Properties = (PropertiesLine*)hook_data;
        DrawContext *Draw_Context = (DrawContext*)call_data;
 
+       gdk_gc_set_foreground(Draw_Context->gc, Properties->color);
+       gdk_gc_set_line_attributes(     Draw_Context->gc,
+                                                                                                                       Properties->line_width,
+                                                                                                                       Properties->style,
+                                                                                                                       GDK_CAP_BUTT,
+                                                                                                                       GDK_JOIN_MITER);
+
+       switch(Properties->position) {
+               case OVER:
+                                                       drawing_draw_line(
+                                                               NULL, Draw_Context->drawable,
+                                                               Draw_Context->Previous->over->x,
+                                                               Draw_Context->Previous->over->y,
+                                                               Draw_Context->Current->over->x,
+                                                               Draw_Context->Current->over->y,
+                                                               Draw_Context->gc);
+                       break;
+               case MIDDLE:
+                                                       drawing_draw_line(
+                                                               NULL, Draw_Context->drawable,
+                                                               Draw_Context->Previous->middle->x,
+                                                               Draw_Context->Previous->middle->y,
+                                                               Draw_Context->Current->middle->x,
+                                                               Draw_Context->Current->middle->y,
+                                                               Draw_Context->gc);
+                       break;
+               case UNDER:
+                                                       drawing_draw_line(
+                                                               NULL, Draw_Context->drawable,
+                                                               Draw_Context->Previous->under->x,
+                                                               Draw_Context->Previous->under->y,
+                                                               Draw_Context->Current->under->x,
+                                                               Draw_Context->Current->under->y,
+                                                               Draw_Context->gc);
+
+                       break;
+       }
+
+       return 0;
 }
 
-gboolean DrawArc( void *hook_data, void *call_data)
+gboolean draw_arc( void *hook_data, void *call_data)
 {
        PropertiesArc *Properties = (PropertiesArc*)hook_data;
        DrawContext *Draw_Context = (DrawContext*)call_data;
 
+       gdk_gc_set_foreground(Draw_Context->gc, Properties->color);
+
+       switch(Properties->position) {
+               case OVER:
+                       gdk_draw_arc(Draw_Context->drawable, Draw_Context->gc,
+                                                       Properties->filled,
+                                                       Draw_Context->Current->modify_over->x,
+                                                       Draw_Context->Current->modify_over->y,
+                                                       Properties->size, Properties->size, 0, 360*64);
+                       Draw_Context->Current->modify_over->x += Properties->size;
+               
+                       break;
+               case MIDDLE:
+                       gdk_draw_arc(Draw_Context->drawable, Draw_Context->gc,
+                                                       Properties->filled,
+                                                       Draw_Context->Current->modify_middle->x,
+                                                       Draw_Context->Current->modify_middle->y,
+                                                       Properties->size, Properties->size, 0, 360*64);
+                       Draw_Context->Current->modify_middle->x += Properties->size;
+                       
+                       break;
+               case UNDER:
+                       gdk_draw_arc(Draw_Context->drawable, Draw_Context->gc,
+                                                       Properties->filled,
+                                                       Draw_Context->Current->modify_under->x,
+                                                       Draw_Context->Current->modify_under->y,
+                                                       Properties->size, Properties->size, 0, 360*64);
+                       Draw_Context->Current->modify_under->x += Properties->size;
+       
+                       break;
+       }
+
+       
+       return 0;
 }
 
-gboolean DrawBG( void *hook_data, void *call_data)
+gboolean draw_bg( void *hook_data, void *call_data)
 {
        PropertiesBG *Properties = (PropertiesBG*)hook_data;
        DrawContext *Draw_Context = (DrawContext*)call_data;
 
+       gdk_gc_set_foreground(Draw_Context->gc, Properties->color);
+
+
+       gdk_draw_rectangle(Draw_Context->drawable, Draw_Context->gc,
+                                       TRUE,
+                                       Draw_Context->Previous->over->x,
+                                       Draw_Context->Previous->over->y,
+                                       Draw_Context->Current->over->x - Draw_Context->Previous->over->x,
+                                       Draw_Context->Previous->under->y);
+
+       return 0;
 }
 
 
This page took 0.027252 seconds and 4 git commands to generate.