Chinaunix首页 | 论坛 | 博客
  • 博客访问: 676619
  • 博文数量: 156
  • 博客积分: 6010
  • 博客等级: 准将
  • 技术积分: 1201
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-05 20:08
文章分类

全部博文(156)

文章存档

2010年(13)

2008年(39)

2007年(104)

我的朋友

分类: C/C++

2007-07-22 22:07:56

conky.c中 画图的主要函数 draw_line(),draw_text (),还有draw_stuff()
 
 

static void draw_line(char *s)//画线,^_^,够复杂的

{
#ifdef X11
    char *p;
    cur_x = text_start_x;
    cur_y += font_ascent();
    int cur_y_add = 0;
    short font_h = font_height();

    /* find specials and draw stuff */
    p = s;
    while (*p) {
        if (*p == SPECIAL_CHAR) {
            int w = 0;

            /* draw string before special */
            *p = '\0';
            draw_string(s);
            *p = SPECIAL_CHAR;
            s = p + 1;

            /* draw special */
            switch (specials[special_index].type) {
            case HORIZONTAL_LINE:
                {
                    int h =
                     specials[special_index].height;
                    int mid = font_ascent() / 2;
                    w = text_start_x + text_width -
                     cur_x;

                    XSetLineAttributes(display,
                             window.gc, h,
                             LineSolid,
                             CapButt,
                             JoinMiter);
                    XDrawLine(display, window.drawable,
                         window.gc, cur_x,
                         cur_y - mid / 2,
                         cur_x + w,
                         cur_y - mid / 2);
                }
                break;

            case STIPPLED_HR:
                {
                    int h =
                     specials[special_index].height;
                    int s =
                     specials[special_index].arg;
                    int mid = font_ascent() / 2;
                    char ss[2] = { s, s };
                    w = text_start_x + text_width -
                     cur_x - 1;

                    XSetLineAttributes(display,
                             window.gc, h,
                             LineOnOffDash,
                             CapButt,
                             JoinMiter);
                    XSetDashes(display, window.gc, 0,
                         ss, 2);
                    XDrawLine(display, window.drawable,
                         window.gc, cur_x,
                         cur_y - mid / 2,
                         cur_x + w,
                         cur_y - mid / 2);
                }
                break;

            case BAR:
                {
                    if (cur_x - text_start_x > maximum_width && maximum_width > 0) {
                        break;
                    }
                    int h =
                     specials[special_index].height;
                    int bar_usage =
                     specials[special_index].arg;
                    int by = cur_y - (font_ascent() / 2) - 1;
                    if (h < font_height()) {
                        by -= h / 2 - 1;
                    }
                    w = specials[special_index].width;
                    if (w == 0)
                        w = text_start_x +
                         text_width - cur_x - 1;
                    if (w < 0)
                        w = 0;

                    XSetLineAttributes(display,
                             window.gc, 1,
                             LineSolid,
                             CapButt,
                             JoinMiter);

                    XDrawRectangle(display,
                         window.drawable,
                         window.gc, cur_x,
                         by, w, h);
                    XFillRectangle(display,
                         window.drawable,
                         window.gc, cur_x,
                         by,
                         w * bar_usage / 255,
                         h);
                    if (specials[special_index].
                     height > cur_y_add
                     && specials[special_index].
                     height > font_h) {
                        cur_y_add =
                         specials
                         [special_index].height;
                    }
                }
                break;

            case GRAPH:
            {
                    if (cur_x - text_start_x > maximum_width && maximum_width > 0) {
                        break;
                    }
                    int h =
                     specials[special_index].height;
                    unsigned long last_colour = current_color;
                    int by = cur_y - (font_ascent()/2) - 1;
                    if (h < font_height()) {
                        by -= h / 2 - 1;
                    }
                    w = specials[special_index].width;
                    if (w == 0)
                        w = text_start_x + text_width - cur_x - 1;
                    if (w < 0)
                        w = 0;
                    if (draw_graph_borders) {
                        XSetLineAttributes(display, window.gc, 1, LineSolid, CapButt, JoinMiter);
                        XDrawRectangle(display,window.drawable, window.gc, cur_x, by, w, h);
                    }
                    XSetLineAttributes(display,
                             window.gc, 1,
                             LineSolid,
                             CapButt,
                             JoinMiter);
    int i;
    int j = 0;
    int gradient_size = 0;
    float gradient_factor = 0;
    float gradient_update = 0;
    unsigned long tmpcolour = current_color;
    if (specials[special_index].last_colour != 0 || specials[special_index].first_colour != 0) {
        tmpcolour = specials[special_index].last_colour;
        gradient_size = gradient_max(specials[special_index].last_colour, specials[special_index].first_colour);
        gradient_factor = (float)gradient_size / (w - 2);
    }
    for (i = w - 2; i > -1; i--) {
        if (specials[special_index].last_colour != 0 || specials[special_index].first_colour != 0) {
            XSetForeground(display, window.gc, tmpcolour);
            gradient_update += gradient_factor;
            while (gradient_update > 0) {
                tmpcolour = do_gradient(tmpcolour, specials[special_index].first_colour);
                gradient_update--;
            }
        }
        if ((w - i) / ((float) (w - 2) / (specials[special_index].graph_width)) > j && j < MAX_GRAPH_DEPTH - 3) {
            j++;
        }
                        XDrawLine(display, window.drawable, window.gc, cur_x + i + 1, by + h, cur_x + i + 1, by + h - specials[special_index].graph[j] * (h - 1) / specials[special_index].graph_scale);    /* this is mugfugly, but it works */
                    }
                    if (specials[special_index].
                     height > cur_y_add
                     && specials[special_index].
                     height > font_h) {
                        cur_y_add =
                         specials
                         [special_index].height;
                    }
/*                if (draw_mode == BG) {
                    set_foreground_color(default_bg_color);
                }
                else if (draw_mode == OUTLINE) {
                    set_foreground_color(default_out_color);
                } else {
                    set_foreground_color(default_fg_color);
                }*/

                set_foreground_color(last_colour);
            }
                break;
            
                case FONT:
                {
                    int old = font_ascent();
                    cur_y -= font_ascent();
                    selected_font = specials[special_index].font_added;
                    if (cur_y + font_ascent() < cur_y + old) {
                        cur_y += old;
                    } else {
                        cur_y += font_ascent();
                    }
                    set_font();
                }
                break;
            case FG:
                if (draw_mode == FG)
                    set_foreground_color(specials
                             [special_index].
                             arg);
                break;

            case BG:
                if (draw_mode == BG)
                    set_foreground_color(specials
                             [special_index].
                             arg);
                break;

            case OUTLINE:
                if (draw_mode == OUTLINE)
                    set_foreground_color(specials
                             [special_index].
                             arg);
                break;

            case OFFSET:
                w += specials[special_index].arg;
                break;
        
            case VOFFSET:
                cur_y += specials[special_index].arg;
                break;

            case GOTO:
                if (specials[special_index].arg >= 0)
                    cur_x = (int)specials[special_index].arg;
                break;

            case TAB:
                {
                    int start = specials[special_index].arg;
                    int step = specials[special_index].width;
                    if (!step || step < 0)
                        step = 10;
                    w = step - (cur_x - text_start_x - start) % step;
                }
                break;

            case ALIGNR:
                {
                    int pos_x = text_start_x + text_width - get_string_width_special(s) /*+ border_margin*/;
                    /*printf("pos_x %i text_start_x %i text_width %i cur_x %i get_string_width(p) %i gap_x %i specials[special_index].arg %i border_margin %i border_width %i\n", pos_x, text_start_x, text_width, cur_x, get_string_width_special(s), gap_x, specials[special_index].arg, border_margin, border_width);*/
                    if (pos_x > specials[special_index].arg && pos_x > cur_x) {
                        cur_x = pos_x - specials[special_index].arg;
                }
                }
                break;

            case ALIGNC:
                {
                    int pos_x = (text_width)/2 - get_string_width_special(s)/2 - (cur_x - text_start_x);
                    /*int pos_x = text_start_x + text_width/2 - get_string_width_special(s)/2;*/
                    /*printf("pos_x %i text_start_x %i text_width %i cur_x %i get_string_width(p) %i gap_x %i specials[special_index].arg %i\n", pos_x, text_start_x, text_width, cur_x, get_string_width(s), gap_x, specials[special_index].arg);*/
                    if (pos_x >
                     specials[special_index].arg)
                        w = pos_x -
                         specials
                         [special_index].arg;
                }
                break;

            }

            cur_x += w;

            special_index++;
        }

        p++;
    }
#else
    draw_string(s);
#endif
#ifdef X11
    if (cur_y_add > 0) {
        cur_y += cur_y_add;
        cur_y -= font_descent();
    }

    draw_string(s);

    cur_y += font_descent();
#endif /* X11 */
}

static void draw_text()//画出文本

{
#ifdef X11
    cur_y = text_start_y;

    /* draw borders */
    if (draw_borders && border_width > 0) {
        unsigned int b = (border_width + 1) / 2;

        if (stippled_borders) {
            char ss[2] =
             { stippled_borders, stippled_borders };
            XSetLineAttributes(display, window.gc,
                     border_width, LineOnOffDash,
                     CapButt, JoinMiter);
            XSetDashes(display, window.gc, 0, ss, 2);
        } else {
            XSetLineAttributes(display, window.gc,
                     border_width, LineSolid,
                     CapButt, JoinMiter);
        }

        XDrawRectangle(display, window.drawable, window.gc,
             text_start_x - border_margin + b,
             text_start_y - border_margin + b,
             text_width + border_margin * 2 - 1 - b * 2,
             text_height + border_margin * 2 - 1 -
             b * 2);
    }

    /* draw text */
    special_index = 0;
#endif /* X11 */
    for_each_line(text_buffer, draw_line);//每一个text区域都要使用draw_line来更新

}

static void draw_stuff()//主要的画图函数

{
#ifdef X11
    selected_font = 0;
    if (draw_shades && !draw_outline) {
        text_start_x++;
        text_start_y++;
        set_foreground_color(default_bg_color);
        draw_mode = BG;
        draw_text();
        text_start_x--;
        text_start_y--;
    }

    if (draw_outline) {
        selected_font = 0;
        int i, j;
        for (i = -1; i < 2; i++)
            for (j = -1; j < 2; j++) {
                if (i == 0 && j == 0)
                    continue;
                text_start_x += i;
                text_start_y += j;
                set_foreground_color(default_out_color);
                draw_mode = OUTLINE;
                draw_text();
                text_start_x -= i;
                text_start_y -= j;
            }
    }

    set_foreground_color(default_fg_color);
    draw_mode = FG;
#endif /* X11 */
    draw_text();
#ifdef X11
#ifdef HAVE_XDBE
    if (use_xdbe) {
        XdbeSwapInfo swap;
        swap.swap_window = window.window;
        swap.swap_action = XdbeBackground;
        XdbeSwapBuffers(display, &swap, 1);
    }
#endif
#endif /* X11 */
}

阅读(2720) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~