"Fossies" - the Fresh Open Source Software Archive

Member "gretl-2020e/plugin/progress_bar.c" (2 Oct 2015, 4769 Bytes) of package /linux/misc/gretl-2020e.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "progress_bar.c" see the Fossies "Dox" file reference documentation.

    1 /* 
    2  *  gretl -- Gnu Regression, Econometrics and Time-series Library
    3  *  Copyright (C) 2001 Allin Cottrell and Riccardo "Jack" Lucchetti
    4  * 
    5  *  This program is free software: you can redistribute it and/or modify
    6  *  it under the terms of the GNU General Public License as published by
    7  *  the Free Software Foundation, either version 3 of the License, or
    8  *  (at your option) any later version.
    9  * 
   10  *  This program is distributed in the hope that it will be useful,
   11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13  *  GNU General Public License for more details.
   14  * 
   15  *  You should have received a copy of the GNU General Public License
   16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
   17  * 
   18  */
   19 
   20 /* progress bar implementation for gretl */
   21 
   22 #include "libgretl.h"
   23 
   24 #ifdef WIN32
   25 # include "gretl_win32.h"
   26 #endif
   27 
   28 #include <gtk/gtk.h>
   29 
   30 typedef struct _ProgressData {
   31     GtkWidget *window;
   32     GtkWidget *label;
   33     GtkWidget *pbar;
   34     int *cancel;
   35 } ProgressData;
   36 
   37 static void destroy_progress (GtkWidget *widget, ProgressData **ppdata)
   38 {
   39     (*ppdata)->window = NULL;
   40     free(*ppdata);
   41     *ppdata = NULL;
   42 }
   43 
   44 static ProgressData *build_progress_window (int flag, int *cancel)
   45 {
   46     ProgressData *pdata;
   47     GtkWidget *align, *vbox;
   48 
   49     pdata = malloc(sizeof *pdata);
   50     if (pdata == NULL) {
   51     return NULL;
   52     }
   53 
   54     pdata->cancel = cancel;
   55 
   56     pdata->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   57     gtk_window_set_resizable(GTK_WINDOW(pdata->window), FALSE);
   58 
   59     if (flag == SP_LOAD_INIT) {
   60     gtk_window_set_title(GTK_WINDOW(pdata->window), _("gretl: loading data"));
   61     } else if (flag == SP_SAVE_INIT) {
   62     gtk_window_set_title(GTK_WINDOW(pdata->window), _("gretl: storing data"));
   63     } else if (flag == SP_FONT_INIT) {
   64     gtk_window_set_title(GTK_WINDOW(pdata->window), _("gretl: scanning fonts"));
   65     }
   66     
   67     gtk_container_set_border_width(GTK_CONTAINER(pdata->window), 0);
   68 
   69     vbox = gtk_vbox_new(FALSE, 5);
   70     gtk_container_set_border_width(GTK_CONTAINER(vbox), 10);
   71     gtk_container_add(GTK_CONTAINER(pdata->window), vbox);
   72 
   73     /* Add a label */
   74     pdata->label = gtk_label_new("");
   75     gtk_box_pack_start(GTK_BOX(vbox), pdata->label, FALSE, FALSE, 0);
   76         
   77     /* Create a centering alignment object */
   78     align = gtk_alignment_new(0.5, 0.5, 0, 0);
   79     gtk_box_pack_start(GTK_BOX(vbox), align, FALSE, FALSE, 5);
   80 
   81     /* Create the GtkProgressBar */
   82     pdata->pbar = gtk_progress_bar_new();
   83     gtk_container_add(GTK_CONTAINER(align), pdata->pbar);
   84 
   85     gtk_widget_show_all(pdata->window);
   86 
   87     return pdata;
   88 }
   89 
   90 int show_progress (double res, double expected, int flag)
   91 {
   92     static ProgressData *pdata;
   93     static double offs;
   94     static int cancel;
   95 
   96     if (expected == 0) {
   97     return SP_RETURN_DONE;
   98     }
   99 
  100     if (res < 0 || flag == SP_FINISH) {
  101     fprintf(stderr, "prog: got SP_FINISH\n");
  102     /* clean up and get out */
  103     if (pdata != NULL && pdata->window != NULL) {
  104         gtk_widget_destroy(GTK_WIDGET(pdata->window)); 
  105         while (gtk_events_pending()) {
  106         gtk_main_iteration();
  107         }       
  108     }
  109     return SP_RETURN_DONE;
  110     }
  111 
  112     if (flag == SP_LOAD_INIT || flag == SP_SAVE_INIT || flag == SP_FONT_INIT) {
  113     /* initialize the progress bar */
  114     gchar *bytestr = NULL;
  115 
  116     offs = 0;
  117     cancel = 0;
  118 
  119     pdata = build_progress_window(flag, &cancel);
  120     if (pdata == NULL) {
  121         return 0; 
  122     }
  123 
  124     g_signal_connect(G_OBJECT(pdata->window), "destroy",
  125              G_CALLBACK(destroy_progress),
  126              &pdata);
  127 
  128     gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(pdata->pbar), 0.0);
  129 
  130     if (flag == SP_LOAD_INIT || flag == SP_SAVE_INIT) {
  131         int Kb = (int) (expected / 1024);
  132         
  133         bytestr = g_strdup_printf("%s %d Kbytes",
  134                       flag == SP_LOAD_INIT ?
  135                       _("Retrieving") : _("Storing"),
  136                       Kb);
  137     } else if (flag == SP_FONT_INIT) {
  138         bytestr = g_strdup_printf(_("Scanning %d fonts"), (int) expected);
  139     }
  140 
  141     gtk_label_set_text(GTK_LABEL(pdata->label), bytestr);
  142     g_free(bytestr);
  143 
  144     while (gtk_events_pending()) {
  145         gtk_main_iteration();
  146     }
  147     }
  148 
  149     if ((flag == SP_NONE || flag == SP_TOTAL) && cancel) {
  150     /* the user canceled */
  151     cancel = 0;
  152     return SP_RETURN_CANCELED;
  153     }    
  154 
  155     if ((flag == SP_NONE || flag == SP_TOTAL) &&
  156     (pdata == NULL || pdata->window == NULL)) {
  157     /* something has gone wrong */
  158     return 0;
  159     }
  160 
  161     if (flag == SP_TOTAL) {
  162     offs = res;
  163     } else {
  164     offs += res;
  165     }
  166 
  167     if (pdata != NULL) {
  168     if (offs < expected) {
  169         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(pdata->pbar), 
  170                       offs / expected);
  171         while (gtk_events_pending()) {
  172         gtk_main_iteration();
  173         }
  174     } else {
  175         gtk_widget_destroy(GTK_WIDGET(pdata->window)); 
  176         return SP_RETURN_DONE;
  177     }
  178     }
  179     
  180     return SP_RETURN_OK;
  181 }
  182