| 1 | /* |
| 2 | * This file is part of DisOrder. |
| 3 | * Copyright (C) 2006, 2007 Richard Kettlewell |
| 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 2 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, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * 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, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 18 | * USA |
| 19 | */ |
| 20 | /** @file disobedience/progress.c |
| 21 | * @brief Progress bar support |
| 22 | */ |
| 23 | |
| 24 | #include "disobedience.h" |
| 25 | |
| 26 | /** @brief State for progress windows */ |
| 27 | struct progress_window { |
| 28 | /** @brief The window */ |
| 29 | GtkWidget *window; |
| 30 | /** @brief The bar */ |
| 31 | GtkWidget *bar; |
| 32 | }; |
| 33 | |
| 34 | /** @brief Create a progress window */ |
| 35 | struct progress_window *progress_window_new(const char *title) { |
| 36 | struct progress_window *pw = xmalloc(sizeof *pw); |
| 37 | |
| 38 | pw->window = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
| 39 | gtk_window_set_transient_for(GTK_WINDOW(pw->window), |
| 40 | GTK_WINDOW(toplevel)); |
| 41 | g_signal_connect(pw->window, "destroy", |
| 42 | G_CALLBACK(gtk_widget_destroyed), &pw->window); |
| 43 | gtk_window_set_default_size(GTK_WINDOW(pw->window), 360, -1); |
| 44 | gtk_window_set_title(GTK_WINDOW(pw->window), title); |
| 45 | pw->bar = gtk_progress_bar_new(); |
| 46 | gtk_container_add(GTK_CONTAINER(pw->window), pw->bar); |
| 47 | gtk_widget_show_all(pw->window); |
| 48 | return pw; |
| 49 | } |
| 50 | |
| 51 | /** @brief Report current progress |
| 52 | * The window is automatically destroyed if @p progress >= @p limit. |
| 53 | * To cancel a window just call with both set to 0. |
| 54 | */ |
| 55 | void progress_window_progress(struct progress_window *pw, |
| 56 | int progress, |
| 57 | int limit) { |
| 58 | if(!pw) |
| 59 | return; |
| 60 | /* Maybe the user closed the window */ |
| 61 | if(!pw->window) |
| 62 | return; |
| 63 | /* Clamp insane or inconvenient values */ |
| 64 | if(limit <= 0) |
| 65 | progress = limit = 1; |
| 66 | if(progress < 0) |
| 67 | progress = 0; |
| 68 | /* Maybe we're done */ |
| 69 | if(progress >= limit) { |
| 70 | gtk_widget_destroy(pw->window); |
| 71 | pw->window = pw->bar = 0; |
| 72 | return; |
| 73 | } |
| 74 | /* Display current progress */ |
| 75 | gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(pw->bar), |
| 76 | (double)progress / limit); |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | Local Variables: |
| 81 | c-basic-offset:2 |
| 82 | comment-column:40 |
| 83 | fill-column:79 |
| 84 | indent-tabs-mode:nil |
| 85 | End: |
| 86 | */ |