GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Oct 25, 2014 9:10 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: how to react to a click on a Gtk::Progressbar ?
PostPosted: Sun Mar 03, 2013 4:24 pm 
Offline

Joined: Sun Mar 03, 2013 2:29 am
Posts: 2
Hello,
I write a Gtk+-based MPD-Client with gtkmm.
I want to set the progressbar'progress with a mouseclick on it, but the signal handler I assigned in the constructor seems to not get called.
Thank you for your help

OS : Ubuntu Gnome Remix 12.10
IDE: Eclipse CDT ( some basic components are loaded per Gtk::Builder from a .glade-file )
Language: C++
Gtk+-Version: 3.6 , 2.24.13

// Header showsong.h
Code:
#ifndef SHOWSONG_H_
#define SHOWSONG_H_

#include <gtkmm.h>
#include <iostream>
#include <fstream>
#include "fenster.h"

class song_ind : public Gtk::VBox
{
public:
   song_ind();
   virtual ~song_ind(){delete mylabel;delete myprogress;};
   bool configure(void);// description in the source file
private:
      Gtk::Label *mylabel;
      Gtk::ProgressBar *myprogress;
      void set_song_pos(GdkEventButton* event);//description in the source file
};

extern class app* myappptr;// app is a class containing references to every
                           // necessary widget in my program, so the pointer is
                           // used to connect the parts with another
extern std::ofstream file1;// test logging file

#endif /* SHOWSONG_H_ */

//Source showsong.cpp
Code:
#include "showsong.h"
// constructor
song_ind::song_ind()
{
       mylabel = new Gtk::Label;
       myprogress = new Gtk::ProgressBar;
       // place childs
       this->pack_start(*mylabel,true,true,5);
       this->pack_end(*myprogress,true,true,5);
       // progressbar
       myprogress->set_show_text(true);
       // the following signal seems to not get emitted!
       this->myprogress->signal_button_release_event().connect_notify
                       (sigc::mem_fun(*this,&song_ind::set_song_pos));
       // vbox
       this->set_size_request(300,50);
       this->show_all();
}
/*
* this will get the data from mpd and let the label & progressbar show it,
* it is called once every 1000 msec per timeout
* ->this works fine :)
*/
bool song_ind::configure(void)
{
   double fraction,elapsed,whole;
   elapsed = (double)myappptr->mympd->get_song_pos();
   whole = (double)myappptr->mympd->get_song_length();
   fraction = elapsed / whole ;
   myprogress->set_fraction(fraction);
   myprogress->set_text(sectomin(inttostr((unsigned int)elapsed)));
    this->mylabel->set_label(myappptr->mympd->get_song_name());
    return true;
}
/*
* this eventhandler shall react to the users click on the progressbar
* -> this is the problem, it does not get called
* purpose: on click it sets the new song position
*/
void song_ind::set_song_pos(GdkEventButton* event)
{
     file1<<"click auf progress"<<std::endl;
     if( (event->type == GDK_BUTTON_RELEASE) &&
        (event->button == 1) )// rechte maus
        {
         double fraction,whole;
         unsigned int elapsed;
         whole = (double)myappptr->mympd->get_song_length();
         fraction = (double)event->x / (double)this->myprogress->get_width();
         elapsed = (int)(whole*fraction);
            myappptr->mympd->set_song_pos(elapsed);
        }
}


Top
 Profile  
 
 Post subject: Re: how to react to a click on a Gtk::Progressbar ?
PostPosted: Sun Mar 03, 2013 5:29 pm 
Offline
GTK+ Geek

Joined: Fri Sep 04, 2009 6:53 am
Posts: 68
I am not an expert in gtkmm but in gtk you have to put your progressbar into an eventbox and connect the signal to the eventbox.

http://developer.gnome.org/gtk-tutorial/stable/c1226.html#SEC-EVENTBOX and http://developer.gnome.org/gtk3/stable/GtkEventBox.html#GtkEventBox.description


Top
 Profile  
 
 Post subject: Re: how to react to a click on a Gtk::Progressbar ?
PostPosted: Sun Mar 03, 2013 6:23 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 764
Location: UK
Hi,

What you are actually missing is enabling the receiving of events for the widget. This can be done using either of the member functions
Code:
void Gtk::Widget::set_events    (    Gdk::EventMask     events   );

or

void Gtk::Widget::add_events    (    Gdk::EventMask     events   );


In your case what you want to do is add
Code:
...
   myprogress->add_events(Gdk::BUTTON_RELEASE_MASK);
...

to enable receiving the Button Release Event.

_________________
E.


Top
 Profile  
 
 Post subject: Re: how to react to a click on a Gtk::Progressbar ?
PostPosted: Sun Mar 03, 2013 6:33 pm 
Offline

Joined: Sun Mar 03, 2013 2:29 am
Posts: 2
ok thank you very much, now it works perfectly. I used asdfgh2091's solution, it worked, then I was curious about the other way errol proposed , tried it, but it didn't work for me.
Thank you two again very much for the fast help


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 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