Back to index

lightning-sunbird  0.9+nobinonly
progressui_gtk.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* vim:set ts=2 sw=2 sts=2 et cindent: */
00003 /* ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is mozilla.org code.
00017  *
00018  * The Initial Developer of the Original Code is Google Inc.
00019  * Portions created by the Initial Developer are Copyright (C) 2005
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *  Darin Fisher <darin@meer.net>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include <stdio.h>
00040 #include <gtk/gtk.h>
00041 #include <unistd.h>
00042 #include "progressui.h"
00043 #include "readstrings.h"
00044 #include "errors.h"
00045 
00046 #define TIMER_INTERVAL 100
00047 
00048 static float    sProgressVal;  // between 0 and 100
00049 static gboolean sQuit = FALSE;
00050 static gboolean sEnableUI;
00051 static guint    sTimerID;
00052 
00053 static GtkWidget *sWin;
00054 static GtkWidget *sLabel;
00055 static GtkWidget *sProgressBar;
00056 
00057 static const char *sProgramPath;
00058 
00059 static gboolean
00060 UpdateDialog(gpointer data)
00061 {
00062   if (sQuit)
00063   {
00064     gtk_widget_hide(sWin);
00065     gtk_main_quit();
00066   }
00067 
00068   float progress = sProgressVal;
00069 
00070   gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(sProgressBar),
00071                                 progress / 100.0);
00072 
00073   return TRUE;
00074 }
00075 
00076 static gboolean
00077 OnDeleteEvent(GtkWidget *widget, GdkEvent *event, gpointer user_data)
00078 {
00079   return TRUE;
00080 }
00081 
00082 int
00083 InitProgressUI(int *pargc, char ***pargv)
00084 {
00085   sProgramPath = (*pargv)[0];
00086 
00087   sEnableUI = gtk_init_check(pargc, pargv);
00088   return 0;
00089 }
00090 
00091 int
00092 ShowProgressUI()
00093 {
00094   if (!sEnableUI)
00095     return -1;
00096 
00097   // Only show the Progress UI if the process is taking significant time.
00098   // Here we measure significant time as taking more than one second.
00099 
00100   usleep(500000);
00101 
00102   if (sQuit || sProgressVal > 50.0f)
00103     return 0;
00104 
00105   char path[PATH_MAX];
00106   snprintf(path, sizeof(path), "%s.ini", sProgramPath);
00107 
00108   StringTable strings;
00109   if (ReadStrings(path, &strings) != OK)
00110     return -1;
00111   
00112   sWin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
00113   if (!sWin)
00114     return -1;
00115 
00116   // GTK 2.2 seems unable to prevent our dialog from being closed when
00117   // the user hits the close button on the dialog.  This problem only
00118   // occurs when either one of the following methods are called:
00119   //   gtk_window_set_position
00120   //   gtk_window_set_type_hint
00121   // For this reason, we disable window decorations.
00122  
00123   g_signal_connect(G_OBJECT(sWin), "delete_event",
00124                    G_CALLBACK(OnDeleteEvent), NULL);
00125 
00126   gtk_window_set_title(GTK_WINDOW(sWin), strings.title);
00127   gtk_window_set_type_hint(GTK_WINDOW(sWin), GDK_WINDOW_TYPE_HINT_DIALOG);
00128   gtk_window_set_position(GTK_WINDOW(sWin), GTK_WIN_POS_CENTER_ALWAYS);
00129   gtk_window_set_resizable(GTK_WINDOW(sWin), FALSE);
00130   gtk_window_set_decorated(GTK_WINDOW(sWin), FALSE);
00131 
00132   GtkWidget *vbox = gtk_vbox_new(TRUE, 6);
00133   sLabel = gtk_label_new(strings.info);
00134   gtk_misc_set_alignment(GTK_MISC(sLabel), 0.0f, 0.0f);
00135   sProgressBar = gtk_progress_bar_new();
00136 
00137   gtk_box_pack_start(GTK_BOX(vbox), sLabel, FALSE, FALSE, 0);
00138   gtk_box_pack_start(GTK_BOX(vbox), sProgressBar, TRUE, TRUE, 0);
00139 
00140   sTimerID = g_timeout_add(TIMER_INTERVAL, UpdateDialog, NULL);
00141 
00142   gtk_container_set_border_width(GTK_CONTAINER(sWin), 10);
00143   gtk_container_add(GTK_CONTAINER(sWin), vbox);
00144   gtk_widget_show_all(sWin);
00145 
00146   gtk_main();
00147   return 0;
00148 }
00149 
00150 // Called on a background thread
00151 void
00152 QuitProgressUI()
00153 {
00154   sQuit = TRUE;
00155 }
00156 
00157 // Called on a background thread
00158 void
00159 UpdateProgressUI(float progress)
00160 {
00161   sProgressVal = progress;  // 32-bit writes are atomic
00162 }