Re: progress bars
| From: | nathan at the-ring dot homelinux dot net | Date: | Fri, 14 May 2004 09:57:29 +0000 |
| Subject: | Re: progress bars | ||
| References: | 1 | Groups: | php.gtk.general |
| Request: | Send a blank email to php-gtk-general+get-4481@lists.php.net to get a copy of this message | ||
> Is anyone familiar with using a progress bar with output from a shell
> command?
>
> I have a program that downloads a file and burns it to a CD and I want
> the progress bar to update more than twice (set to zero and 100), but I
> seem to have a problem doing this. If anyone could help me with this,
> that would be stupendous.
>
> Thanks,
> Craig
>
i'm not sure but hope this will help, so here's my way to use windowed
progressbar:
/**
* a progressbar in a window ready to use
* @package GUI_OBJECTS
* @param string $headstring header format string
* @param GTK_WINDOW_TYPE $type default is GTK_WINDOW_DIALOG
* @licence LGPL
*/
class progress_window{
function progress_window( $headerstring='',$type = GTK_WINDOW_DIALOG){
$this->gtkwindow = &new GtkWindow($type);
$this->gtkwindow->realize();
$this->gtkwindow->set_title('Working in progress');
$this->gtkwindow->set_position('center');
$this->gdkwindow = $this->gtkwindow->window;
$this->adj = &new GtkAdjustment (0.0, 0.0, 100.0, 0.0, 0.0, 0.0);
$this->value = $this->adj->value;
$this->progress = &new GtkProgressBar($this->adj);
$this->progress->set_show_text(true);
$this->progress->set_text_alignment(0.5, 0.5);
$this->progress->set_format_string("$headerstring %v%% complete");
$this->progress->set_usize(250, 25);
$this->gtkwindow->add($this->progress);
}
function set_activity_mode($bool=TRUE,$auto=FALSE){
$this->progress->set_activity_mode($bool);
if($auto)
$this->auto_activity_mode = $auto;
}
function set_title($title){
$this->gtkwindow->set_title($title);
}
function activity_step(){
if($this->value >= 100)
$reverse=TRUE;
if($this->value<=0)
$reverse=FALSE;
if($reverse)
$this->value -= 1;
else
$this->value += 1;
$this->adj->set_value($this->value);
$this->force_refresh();
return TRUE;
}
function show(){
$this->progress->show_all();
$this->gtkwindow->show_now();
if(! $this->auto_activity_mode){
$this->set_pos(0);
}else{
$this->loop = gtk::timeout_add(25,array(&$this,'activity_step'));
}
}
function set_header($headerstring){
$this->progress->set_format_string("$headerstring %v%% complete");
$this->force_refresh();
}
function force_refresh(){
while(gtk::events_pending()) gtk::main_iteration();//seems to be the
better way to refresh
}
function kill(){
if($this->loop)
gtk::timeout_remove($this->loop);
$this->gtkwindow->destroy();
}
function set_pos($value){
if($this->value != $value)
$this->value=$value;
$this->adj->set_value($value);
$this->force_refresh();
}
}
you can then use it like this:
$p = &new progress_window( 'Transfering data',GTK_WINDOW_DIALOG);
$p->show();
# do 10% of job then
$p->set_pos(10);
#do job until 50%
$p->set_pos(50);
....
#and then kill
$p->kill();
you can also set it to activity mode like this
$p = &new progress_window( 'Transfering data',GTK_WINDOW_DIALOG);
$p->set_activity_mode();
$p->show();
#do your work
....
#and then kill
$p->kill();
but I think that downloading file or burning cdrom will block the script
execution while running, so you can display a GDK_WATCH cursor while
working.
you must have to set the position of the cursor regarding of the status
(amount downloaded or burned) and i don't see how you can get the value in
your script updated.
for download you perhaps can get the total filesize and then split the
download in 100 part and set the position between each one but i've not
tryed yet.
nathan
ps: if achieve to have a working status display on file down/upload thanks
to let me know