GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Thu Nov 27, 2014 8:44 pm

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Drawing on a widget in gtkmm
PostPosted: Sun Nov 04, 2012 5:27 pm 
Offline
GTK+ Geek

Joined: Sun Apr 03, 2011 11:12 am
Posts: 63
I want to draw a line on a gtkmm widget of blue color. That widget has some background color. When I draw over that widget, then the background color and blue color gets mixed to give some other color. How can I draw the line with the blue color. I'm modifying the expose event. This is a custom widget, and m_refGdkWindow represents the GdkWindow of the widget.

Code:
bool gtk_vbox::on_expose_event(GdkEventExpose* event)
{
    //std::cout<<"Widget_expose_event"<<std::endl;
    if(m_refGdkWindow)
    {
        Cairo::RefPtr<Cairo::Context> cr = m_refGdkWindow->create_cairo_context();
        //std::cout<<"X"<<event->area.x<<"Y"<<event->area.y<<std::endl;
        if(event)
        {
            cr->rectangle(event->area.x,event->area.y,event->area.width,event->area.height);
            cr->clip();
            }
        Gdk::Cairo::set_source_color(cr,get_style()->get_bg(Gtk::STATE_NORMAL));
        cr->paint();

        ///Here's the drawing code
        Gdk::Cairo::set_source_color(cr,Gdk::Color("blue"));
        for(int i = 0;i<4;i++)
        {
            if(lines_draw_array[i]&&lines_draw_array[i]->width!=-1)
            {
                cr->set_line_width(lines_draw_array[i]->width);
                cr->move_to(lines_draw_array[i]->x1,lines_draw_array[i]->y1);
                cr->line_to(lines_draw_array[i]->x2,lines_draw_array[i]->y2);           
                cr->stroke();
                }
            }   
         /////////////------------------------------
   
        Gdk::Cairo::set_source_color(cr,Gdk::Color("white"));
        cr->set_line_width(0.5);
        int i=0,j=0;
        int height = get_height();
        int width = get_width();
        for(i=10;i<height;i+=10)
        {
            cr->move_to(0,i);
            cr->line_to(width,i);
            }
         for(j=10;j<width;j+=10)
         {
            cr->move_to(j,0);
            cr->line_to(j,height);
            }       
        cr->stroke();
        cr->set_line_width(1.0);
        Gdk::Cairo::set_source_color(cr,Gdk::Color("black"));
        if(number_of_widgets!=slots)
        {           
            //std::cout<<"NUMBER OF WIDGETS != SLOTS"<<std::endl;
            int height_of_slot = get_height()/slots;
            //std::cout<<"slots"<<slots<<"heightofslot"<<height_of_slot<<std::endl;
            //std::cout<<"spacing"<<spacing<<std::endl;
            for(int i=1;i<slots;i++)
            {
                cr->move_to(0,i*height_of_slot);           
                cr->line_to(get_width(),i*height_of_slot);
                }           
            }
        cr->stroke();
        }
    if(event)
        Gtk::Container::on_expose_event(event);
    return false;
    }


Top
 Profile  
 
 Post subject: Re: Drawing on a widget in gtkmm
PostPosted: Sun Nov 04, 2012 9:33 pm 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
The effect you are getting is called compositing. A tutorial on the different types can be found at http://zetcode.com/tutorials/cairographicstutorial/compositing/.
How come you are using gdk_cairo functions for drawing? You would be better calling the cairo functions directly. You also don't get alpha capability in gdk_cairo which can be set explicitly with cairo_set_source_rgba


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: Yahoo [Bot] and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group