> Actually I have already spent so much time to configure progress bar of
> FLTK. but i couldn't manage it out.. I just want that when i open any
> file, by reading lines from the file, progress bar will show the
> percentage of completed lines.
> 
> but when the file reading loop starts, Fl::check() make time interval i
> think so. progress bar is showing without any problem. but the speed of
> line reading is reduced 100 times may be. I don't know whats wrong with
> it. How can i make a real-time progress bar that works in real-time. real-
> time means if i print out something from a loop, progress bar should show
> the same percentage and timing as cout or printf shows.


I just tried this (demo attached, sorry it's so long) and I'm not seeing a big 
performance hit - on this aged WinXP machine this rattles through a 300MB text 
file in hardly any time at all; I'm not seeing a significant difference whether 
I update the GUI or not...


----------
#include <stdio.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>

#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Progress.H>
#include <FL/Fl_Native_File_Chooser.H>
#include <FL/Fl_Button.H>

static Fl_Double_Window *main_win = NULL;
static Fl_Progress *prgbar = NULL;
static Fl_Native_File_Chooser *chooser_widget = NULL;
static Fl_Button *get_file_bt = NULL;

/*****************************************************************************/
static unsigned is_regular_file_read(const char *fn)
{
    struct stat statbuf;
    int res = stat(fn, &statbuf);

    if(res < 0) return 0; // doesn't exist

    if(S_ISDIR(statbuf.st_mode)) return 0; // it's a directory

    if(S_ISREG(statbuf.st_mode))
    {
        int res = access(fn, R_OK);
        if(res == 0) return statbuf.st_size;
    }
    return 0;
} // is_regular_file_read
/*****************************************************************************/
static void get_file(Fl_Button *, void *v)
{
    char buffer[512];
    /* Pop a file chooser to select the file to open */
    if (!chooser_widget)
    {
        chooser_widget = new Fl_Native_File_Chooser();
    }
    chooser_widget->type(Fl_Native_File_Chooser::BROWSE_FILE);
    // now show the file open dialog
    int canceled = chooser_widget->show();

    prgbar->label("Waiting...");
    prgbar->value(0.0);
    prgbar->redraw();

    /* Get the selected filename */
    if ((!canceled) && (chooser_widget->count() > 0))
    {
        unsigned sz = is_regular_file_read(chooser_widget->filename(0));
        if(!sz) return;

        FILE *fp = fopen(chooser_widget->filename(0), "rb");
        if(!fp) return;

        unsigned read = 0;
        float last = 0.0;
        float curr;
        prgbar->label("Reading...");
        while ((read < sz) && (!feof(fp)))
        {
            read += fread(buffer, 1, 512, fp);
            curr = (float)read * 100.0 / (float)sz;
            if((curr - last) > 3.0)
            {
                last = curr;
                prgbar->value(last);
                Fl::check();
            }
        }
        fclose(fp);
        prgbar->value(100.0);
        prgbar->label("Done!");
        prgbar->redraw();
    }
} // get_file
/*****************************************************************************/
static void do_exit(Fl_Button *, void *v)
{
    main_win->hide();
} // do_exit
/*****************************************************************************/
int main(int argc, char **argv)
{
    main_win = new Fl_Double_Window(500, 400);
    main_win->begin();

    prgbar = new Fl_Progress(90, 110, 305, 55, "Waiting...");
    prgbar->color((Fl_Color)5);
    prgbar->selection_color((Fl_Color)1);
    prgbar->minimum(0);
    prgbar->maximum(100);

    get_file_bt = new Fl_Button(90, 180, 60, 30, "Get...");
    get_file_bt->callback((Fl_Callback *)get_file);

    Fl_Button *exit_bt = new Fl_Button(430, 360, 60, 30, "EXIT");
    exit_bt->callback((Fl_Callback *)do_exit);

    main_win->end();

    main_win->show(argc, argv);

  return Fl::run();
}
/* end of file */


-------------



SELEX Galileo Ltd
Registered Office: Sigma House, Christopher Martin Road, Basildon, Essex SS14 
3EL
A company registered in England & Wales.  Company no. 02426132
********************************************************************
This email and any attachments are confidential to the intended
recipient and may also be privileged. If you are not the intended
recipient please delete it from your system and notify the sender.
You should not copy it or use it for any purpose nor disclose or
distribute its contents to any other person.
********************************************************************

_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to