示例#1
0
/**
 * Render the Preview
 * @param Cairo::Context cr
 * @return Gtk::Window::on_draw(cr)
 */
bool Preview::on_draw(const Cairo::RefPtr<Cairo::Context>& cr)
{
      
    Configuration::Theme theme = Configuration::getTheme();
    if (theme.forPreview().enabled()) {


        cr->set_source_rgba(
                theme.forPreview().background().red,
                theme.forPreview().background().green,
                theme.forPreview().background().blue,
                theme.forPreview().background().alpha);

        Utilities::RoundedRectangle(cr, 0, 0, this->get_width(), this->get_height(),
                theme.forPreview().roundedRatious());

        cr->fill();

    }




    if (!m_isActive) {
        return Gtk::Window::on_draw(cr);
    }


    if (m_previewtems.size() < 1) {
        this->hideMe();
        return Gtk::Window::on_draw(cr);
    }



    int idx = 0;
    for (DockItem* item : m_previewtems) {
        if (item->m_window == NULL || item->m_xid == 0 || !item->visible)
            continue;

        int pos_x = 20 + (m_previewWidth * idx);
        int pos_y = 16;
        int pos_width = m_previewWidth - DEF_PREVIEW_LEFT_MARGING;
        int pos_height = 20;


        cr->set_source_rgba(
                theme.forPreview().foreground().red,
                theme.forPreview().foreground().green,
                theme.forPreview().foreground().blue,
                theme.forPreview().foreground().alpha);

        cr->set_line_width(theme.forPreview().lineWith());

        if (item->m_imageLoadedRequired)
            cr->set_line_width(theme.forPreview().lineWith() + 0.5);

        if (theme.getPreviewBinaryValue() == 0) {
            Utilities::RoundedRectangle(cr,
                    DEF_PREVIEW_LEFT_MARGING + (m_previewWidth * idx),
                    16, m_previewWidth,
                    m_previewHeight - DEF_PREVIEW_RIGHT_MARGING,
                    theme.forPreview().roundedRatious());

            cr->stroke();
        } else {
            cr->set_line_width(theme.forPreview().lineWith());
            int pos_x = DEF_PREVIEW_LEFT_MARGING + (m_previewWidth * idx);
            int y_pos = 16;
            int width = m_previewWidth;
            int height = m_previewHeight;
            int value = theme.getPreviewBinaryValue();
            /* Vertical Left*/
            int bit = CHECK_BIT(value, 3);
            if (bit == 1) {

                cr->set_line_width(theme.forPreview().lineWith());
                cr->move_to(pos_x, y_pos);
                cr->line_to(pos_x, y_pos);
                cr->line_to(pos_x, y_pos + (m_previewHeight - DEF_PREVIEW_RIGHT_MARGING));
                cr->stroke();

            }
            /* Top */
            bit = CHECK_BIT(value, 2);
            if (bit == 1) {
                cr->set_line_width(theme.forPreview().lineWith());
                cr->move_to(pos_x, y_pos);
                cr->line_to(pos_x, y_pos);
                cr->line_to(pos_x + (m_previewWidth), y_pos);
                cr->stroke();
            }

            /* Right vertical */
            bit = CHECK_BIT(value, 1);
            if (bit == 1) {
                cr->set_line_width(theme.forPreview().lineWith());
                cr->move_to(pos_x + m_previewWidth, y_pos);
                cr->line_to(pos_x + m_previewWidth, y_pos);
                cr->line_to(pos_x + m_previewWidth, y_pos + (m_previewHeight - DEF_PREVIEW_RIGHT_MARGING));
                cr->stroke();
            }

            bit = CHECK_BIT(value, 0);
            if (bit == 1) {
                cr->set_line_width(theme.forPreview().lineWith());
                cr->move_to(pos_x, y_pos + (m_previewHeight - DEF_PREVIEW_RIGHT_MARGING));
                cr->line_to(pos_x, y_pos + (m_previewHeight - DEF_PREVIEW_RIGHT_MARGING));
                cr->line_to(pos_x + m_previewWidth, y_pos + (m_previewHeight - DEF_PREVIEW_RIGHT_MARGING));
                cr->stroke();
            }

        }


        // TEXT 
        // draw title and create clipping rectangle
        //  JG        
        cr->set_source_rgba(
                theme.forPreviewTitle().background().red,
                theme.forPreviewTitle().background().green,
                theme.forPreviewTitle().background().blue,
                theme.forPreviewTitle().foreground().alpha);
        cr->rectangle(pos_x, pos_y + 2, pos_width + 2, pos_height);
        // cr->clip_preserve();
        cr->fill();
        //    JG
        cr->set_source_rgba(
                theme.forPreviewTitle().foreground().red,
                theme.forPreviewTitle().foreground().green,
                theme.forPreviewTitle().foreground().blue,
                theme.forPreviewTitle().foreground().alpha);
        cr->set_line_width(theme.forPreviewTitle().lineWith());
        cr->rectangle(pos_x, pos_y + 2, pos_width + 2, pos_height);
        cr->stroke();



        // draw title the clipping rectangle
        //cr->set_source_rgba(1.0, 1.0, 1.0, 0.0);

        cr->rectangle(pos_x, pos_y + 2, pos_width, pos_height);
        cr->clip_preserve();
        cr->stroke();




        cr->set_source_rgba(
                theme.forPreviewTitleText().foreground().red,
                theme.forPreviewTitleText().foreground().green,
                theme.forPreviewTitleText().foreground().blue,
                theme.forPreviewTitleText().foreground().alpha);

        auto layout = create_pango_layout(item->m_titlename);
        layout->set_font_description(font);
        //cr->set_source_rgba(1.0, 1.0, 1.0, 1.0); // white text
        cr->move_to(pos_x, pos_y + 2);
        layout->show_in_cairo_context(cr);
        cr->reset_clip(); // Reset the clipping 



        if (item->m_imageLoadedRequired) {
            GdkPixbuf *scaledpb = getScaledPixbuf(item);

            if (scaledpb == nullptr) {
                continue;
            }

            // show the loaded image. 
            showPreview(cr, scaledpb, idx);

            // Checks if the image have movement.
            if (item->isMovementDetected(scaledpb) && !item->m_isDynamic) {
                item->m_isDynamic = true;
            }

            // if no movement has been detected, means that the image 
            // is static and we don't need to reload it again 
            if (++item->m_frames > 3 && !item->m_isDynamic) {
                item->m_scaledPixbuf = scaledpb;
                item->m_frames = 0;
                item->m_imageLoadedRequired = false;
            }

            // if the image is static do not unreference the scaledpb.
            if (item->m_imageLoadedRequired)
                g_object_unref(scaledpb);

        } else {
            // show the loaded static image. 
            showPreview(cr, item->m_scaledPixbuf, idx);

        }



        // selector
        if (m_currentIndex == idx) {

            // rectangle background selector
            pos_x = DEF_PREVIEW_LEFT_MARGING + (m_previewWidth * m_currentIndex);
            pos_y = 16;
            pos_width = m_previewWidth + 1;
            pos_height = m_previewHeight - DEF_PREVIEW_RIGHT_MARGING;

            if (m_previewtems.size() > 1) {
                cr->set_source_rgba(
                        theme.forPreviewSelector().background().red,
                        theme.forPreviewSelector().background().green,
                        theme.forPreviewSelector().background().blue,
                        theme.forPreviewSelector().background().alpha);

                Utilities::RoundedRectangle(cr, pos_x, pos_y, pos_width, pos_height,
                        theme.forPreviewSelector().roundedRatious());
                cr->fill();
            }

            cr->set_source_rgba(
                    theme.forPreviewSelector().foreground().red,
                    theme.forPreviewSelector().foreground().green,
                    theme.forPreviewSelector().foreground().blue,
                    theme.forPreviewSelector().foreground().alpha);
            cr->set_line_width(theme.forPreviewSelector().lineWith());

            if (theme.getPreviewSelectorBinaryValue() == 0) {

                Utilities::RoundedRectangle(cr, pos_x, pos_y, pos_width, pos_height,
                        theme.forPreviewSelector().roundedRatious());
                cr->stroke();
            } else {


                int value = theme.getPreviewSelectorBinaryValue();
                //                /* Vertical Left*/
                int bit = CHECK_BIT(value, 3);
                if (bit == 1) {


                    cr->move_to(pos_x, pos_y);
                    cr->line_to(pos_x, pos_y);
                    cr->line_to(pos_x, pos_y + (pos_height));
                    cr->stroke();

                }
                /* Top */
                bit = CHECK_BIT(value, 2);
                if (bit == 1) {

                    cr->move_to(pos_x, pos_y);
                    cr->line_to(pos_x, pos_y);
                    cr->line_to(pos_x + (pos_width), pos_y);
                    cr->stroke();
                }

                /* Right vertical */
                bit = CHECK_BIT(value, 1);
                if (bit == 1) {

                    cr->move_to(pos_x + pos_width, pos_y);
                    cr->line_to(pos_x + pos_width, pos_y);
                    cr->line_to(pos_x + pos_width, pos_y + (m_previewHeight - DEF_PREVIEW_RIGHT_MARGING));
                    cr->stroke();
                }

                bit = CHECK_BIT(value, 0);
                if (bit == 1) {

                    cr->move_to(pos_x, pos_y + (m_previewHeight - DEF_PREVIEW_RIGHT_MARGING));
                    cr->line_to(pos_x, pos_y + (m_previewHeight - DEF_PREVIEW_RIGHT_MARGING));
                    cr->line_to(pos_x + m_previewWidth, pos_y + (m_previewHeight - DEF_PREVIEW_RIGHT_MARGING));
                    cr->stroke();
                }
            }


            cr->set_source_rgba(
                    theme.forPreviewSelectorClose().background().red,
                    theme.forPreviewSelectorClose().background().green,
                    theme.forPreviewSelectorClose().background().blue,
                    theme.forPreviewSelectorClose().background().alpha);


            pos_x = (m_previewWidth - 5) + (m_previewWidth * m_currentIndex);
            cr->move_to(pos_x + 3, DEF_PREVIEW_RIGHT_MARGING);

            cr->rectangle(pos_x, 18, DEF_PREVIEW_LEFT_MARGING, DEF_PREVIEW_LEFT_MARGING + 2);
            cr->fill();

            cr->set_source_rgba(
                    theme.forPreviewSelectorClose().foreground().red,
                    theme.forPreviewSelectorClose().foreground().green,
                    theme.forPreviewSelectorClose().foreground().blue,
                    theme.forPreviewSelectorClose().foreground().alpha);
            cr->set_line_width(theme.forPreviewSelectorClose().lineWith());

            // Close X text
            cr->move_to(pos_x + 3, DEF_PREVIEW_RIGHT_MARGING - 1);
            cr->show_text("X");


        }




        idx++;
    }
    return Gtk::Window::on_draw(cr);
}