예제 #1
0
파일: pager.c 프로젝트: psachin/lxpanel
/* Handler for configure_event on drawing area. */
static gboolean desk_configure_event(GtkWidget * widget, GdkEventConfigure * event, PagerDesk * d)
{
    /* Allocate pixmap and statistics buffer without border pixels. */
#if GTK_CHECK_VERSION(2,18,0)
    GtkAllocation *allocation = g_new0 (GtkAllocation, 1);
    gtk_widget_get_allocation(GTK_WIDGET(widget), allocation);
    int new_pixmap_width = allocation->width;
    int new_pixmap_height = allocation->height;
#else
    int new_pixmap_width = widget->allocation.width;
    int new_pixmap_height = widget->allocation.height;
#endif
    if ((new_pixmap_width > 0) && (new_pixmap_height > 0))
    {
        /* Allocate a new pixmap of the allocated size. */
        if (d->pixmap != NULL)
            cairo_surface_destroy(d->pixmap);
        d->pixmap = cairo_image_surface_create(CAIRO_FORMAT_RGB24, new_pixmap_width, new_pixmap_height);
        cairo_t *cr = cairo_create(d->pixmap);
        cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
        cairo_paint(cr);
	check_cairo_status(cr);
        cairo_destroy(cr);
	check_cairo_surface_status(&d->pixmap);

        /* Compute the horizontal and vertical scale factors, and mark the desktop for redraw. */
#if GTK_CHECK_VERSION(2,18,0)
        d->scale_y = (gfloat) allocation->height / (gfloat) gdk_screen_height();
        d->scale_x = (gfloat) allocation->width  / (gfloat) gdk_screen_width();
#else
        d->scale_y = (gfloat) allocation->height / (gfloat) gdk_screen_height();
        d->scale_x = (gfloat) allocation->width  / (gfloat) gdk_screen_width();
#endif
        desk_set_dirty(d);
     }

    /* Resize to optimal size. */
    gtk_widget_set_size_request(widget,
        (d->pg->plugin->panel->icon_size - BORDER_WIDTH * 2) * d->pg->aspect_ratio,
        d->pg->plugin->panel->icon_size - BORDER_WIDTH * 2);
#if GTK_CHECK_VERSION(2,18,0)
    g_free (allocation);
#endif
    return FALSE;
}
예제 #2
0
void dump_graph_to_png_file(const AdjacencyList &adjacency_list,
                            const VertexList &vertex_list,
                            const char *png_filename)
{
    auto bbox = get_aabb(vertex_list);

    double width = bbox.maxx - bbox.minx;
    double height = bbox.maxy - bbox.miny;

    auto surface = std::shared_ptr<cairo_surface_t>(
        cairo_image_surface_create(CAIRO_FORMAT_RGB24, width, height),
        cairo_surface_destroy);
    check_cairo_surface_status(surface.get());

    auto cr = std::shared_ptr<cairo_t>(
        cairo_create(surface.get()),
        cairo_destroy);
    check_cairo_status(cr.get());

    cairo_set_line_width(cr.get(), 1.0);

    for (const auto &edge: adjacency_list) {
        auto u = vertex_list.find(std::get<0>(edge));

        if (u == vertex_list.end()) {
            std::cout << "[WARNING] reference to non-existing vertex "
                      << std::get<0>(edge) << std::endl;
            continue;
        }

        for (const auto &way: std::get<1>(edge)) {
            auto v = vertex_list.find(way.destination);

            if (v == vertex_list.end()) {
                std::cout << "[WARNING] reference to non-existing vertex "
                          << way.destination << std::endl;
                continue;
            }

            switch (way.type) {
            case 0:
                cairo_set_source_rgb(cr.get(), 1.0, 0.0, 0.0);
                break;

            case 1:
                cairo_set_source_rgb(cr.get(), 0.0, 1.0, 0.0);
                break;

            case 2:
                cairo_set_source_rgb(cr.get(), 0.0, 0.0, 1.0);
                break;

            default:
                std::cout << "[WARNING] Wrong type of arc" << std::endl;
            }

            cairo_move_to(cr.get(),
                          u->second.x - bbox.minx,
                          height - (u->second.y - bbox.miny));
            cairo_line_to(cr.get(),
                          v->second.x - bbox.minx,
                          height - (v->second.y - bbox.miny));
            cairo_stroke(cr.get());
        }
    }

    {
        auto status = cairo_surface_write_to_png(surface.get(), png_filename);
        cairo_status_to_exception(status);
    }
}
예제 #3
0
파일: monitors.c 프로젝트: psachin/lxpanel
/******************************************************************************
 *                            Basic events handlers                           *
 ******************************************************************************/
static gboolean
configure_event(GtkWidget* widget, GdkEventConfigure* dummy, gpointer data) 
{
    (void) dummy;

    int new_pixmap_width, new_pixmap_height;

    new_pixmap_width = widget->allocation.width - BORDER_SIZE * 2;
    new_pixmap_height = widget->allocation.height - BORDER_SIZE *2;
    Monitor *m;

    m = (Monitor *) data;

    if (new_pixmap_width > 0 && new_pixmap_height > 0)
    {
        /*
         * If the stats buffer does not exist (first time we get inside this
         * function) or its size changed, reallocate the buffer and preserve
         * existing data.
         */
        if (!m->stats || (new_pixmap_width != m->pixmap_width))
        {
            stats_set *new_stats = g_new0(stats_set, new_pixmap_width);
            
            if (!new_stats)
                return TRUE;

            if (m->stats)
            {
                /* New allocation is larger.
                 * Add new "oldest" samples of zero following the cursor*/
                if (new_pixmap_width > m->pixmap_width)
                {
                    /* Number of values between the ring cursor and the end of
                     * the buffer */
                    int nvalues = m->pixmap_width - m->ring_cursor;

                    memcpy(new_stats,
                           m->stats,
                           m->ring_cursor * sizeof (stats_set));
                    memcpy(new_stats + nvalues,
                           m->stats + m->ring_cursor,
                           nvalues * sizeof(stats_set));
                }
                /* New allocation is smaller, but still larger than the ring
                 * buffer cursor */
                else if (m->ring_cursor <= new_pixmap_width)
                {
                    /* Numver of values that can be stored between the end of
                     * the new buffer and the ring cursor */
                    int nvalues = new_pixmap_width - m->ring_cursor;
                    memcpy(new_stats,
                           m->stats,
                           m->ring_cursor * sizeof(stats_set));
                    memcpy(new_stats + m->ring_cursor,
                           m->stats + m->pixmap_width - nvalues,
                           nvalues * sizeof(stats_set));
                }
                /* New allocation is smaller, and also smaller than the ring
                 * buffer cursor.  Discard all oldest samples following the ring 
                 * buffer cursor and additional samples at the beginning of the 
                 * buffer. */
                else
                {
                    memcpy(new_stats,
                           m->stats + m->ring_cursor - new_pixmap_width,
                           new_pixmap_width * sizeof(stats_set));
                }
                g_free(m->stats);
            }
            m->stats = new_stats;
        }

        m->pixmap_width = new_pixmap_width;
        m->pixmap_height = new_pixmap_height;
        if (m->pixmap)
            cairo_surface_destroy(m->pixmap);
        m->pixmap = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
                                   m->pixmap_width,
                                   m->pixmap_height);
        check_cairo_surface_status(&m->pixmap);
        redraw_pixmap(m);
    }
    
    return TRUE;
}