Added a graphical image representing increase/decrease in price.
authorJonathan Parr <jon@jdtp>
Sun, 20 Mar 2011 20:57:55 +0000 (20:57 +0000)
committerJonathan Parr <jon@jdtp>
Sun, 20 Mar 2011 20:57:55 +0000 (20:57 +0000)
Makefile
StockPluginDecrease.png [new file with mode: 0644]
StockPluginIncrease.png [new file with mode: 0644]
lib-stock-home-widget.c
lib-stock-home-widget.h
lib-stock-settings.h

index 709015a..8aa1eb4 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -9,6 +9,7 @@ all: stock-home-widget
 SOURCES=lib-stock-home-widget.c lib-stock-settings.c stockgetter.c
 TARGET =lib-stock-home-widget.so
 DESKTOP=stock-home-widget.desktop
+RESOURCES=StockPluginIncrease.png StockPluginDecrease.png
 
 lib-stock-home-widget.c : lib-stock-home-widget.h
 stockgetter.c : stockgetter.h
@@ -25,3 +26,5 @@ clobber:
 install:
        @cp -f $(TARGET) `pkg-config libhildondesktop-1 --variable=hildondesktoplibdir`/.
        @cp -f $(DESKTOP) `pkg-config libhildondesktop-1 --variable=hildonhomedesktopentrydir`/.
+       @cp -f $(RESOURCES) /etc/hildon/theme/images/.
+
diff --git a/StockPluginDecrease.png b/StockPluginDecrease.png
new file mode 100644 (file)
index 0000000..256653a
Binary files /dev/null and b/StockPluginDecrease.png differ
diff --git a/StockPluginIncrease.png b/StockPluginIncrease.png
new file mode 100644 (file)
index 0000000..0a9313e
Binary files /dev/null and b/StockPluginIncrease.png differ
index 14a2b81..13891cf 100644 (file)
 HD_DEFINE_PLUGIN_MODULE (StockPlugin, stock_plugin, HD_TYPE_HOME_PLUGIN_ITEM)
 
 static void
+OnClickUpdate(GtkWidget *widget, GdkEventButton *event, StockPluginContext *psContext)
+{
+       char szBuffer[1024];
+
+       DebugOut(("OnClickUpdate"));
+       
+       sprintf(szBuffer,"Retrieving <span foreground=\"red\">%s</span>...",psContext->psSettings->ppszTickers[0]);
+
+       /* Hide the stock arrow when we're updating */
+       psContext->bArrowActive = FALSE;
+       gtk_widget_hide(GTK_WIDGET(psContext->psArrow));
+       
+       gtk_label_set_markup(GTK_LABEL(psContext->psLabel),szBuffer);
+       gtk_widget_queue_draw(GTK_WIDGET(psContext->psParent));
+}
+
+static void
 OnClickRefresh(GtkWidget *widget, GdkEventButton *event, StockPluginContext *psContext)
 {
        DebugOut(("OnClickRefresh"));
        
        /* Disable button while getting latest stocks */
        {
+               char    szBuffer[1024]; 
                float   fPrice;
                float   fChange;
-               char    szBuffer[1024];         
 
                if(RetrieveStockPrice(psContext->hSG,psContext->psSettings->ppszTickers[0],&fPrice,NULL,&fChange) == SG_OK)
                {
-                       char             szTime[80];
                        char            *pszColour = NULL;
                        time_t           rawtime;
                        struct tm       *timeinfo;
+                       char             szTime[80];
+                       int                      nImageSel;
 
                        time(&rawtime);
                        timeinfo = localtime(&rawtime);
@@ -36,9 +54,23 @@ OnClickRefresh(GtkWidget *widget, GdkEventButton *event, StockPluginContext *psC
                        
                        /* Highlight  the change in price */
                        if(fChange > 0.0f)
+                       {
+                               nImageSel = STOCK_IMAGE_INCREASE;
                                pszColour = "green";
+                       }
                        else
+                       {
+                               nImageSel = STOCK_IMAGE_DECREASE;
                                pszColour = "red";
+                       }
+
+                       gtk_image_set_from_pixbuf(GTK_IMAGE(psContext->psArrow),psContext->asStockImage[nImageSel]);
+
+                       if(!psContext->bArrowActive)
+                       {
+                               gtk_widget_show(psContext->psArrow);
+                               psContext->bArrowActive = TRUE;
+                       }
 
                        /* Update to latest price */
                        sprintf(szBuffer,
@@ -54,7 +86,8 @@ OnClickRefresh(GtkWidget *widget, GdkEventButton *event, StockPluginContext *psC
                 */
                else
                {
-                       hildon_banner_show_information(GTK_WIDGET(widget),NULL,"Failed to retrieve stock information, check ticker symbol and try again.");
+                       gtk_label_set_markup(GTK_LABEL(psContext->psLabel),"<span foreground=\"red\"><b>Failed</b></span>: Check ticker and try again.");
+                       gtk_widget_queue_draw(GTK_WIDGET(psContext->psParent));
                }
        }
 }
@@ -277,25 +310,39 @@ stock_show_settings_dialog(GtkWidget* widget, gpointer data)
 static GtkWidget *stock_create_gui(gchar                               *pszStockLabel,
                                                                   StockPluginContext   *psContext)
 {
+       GtkWidget *hbox;
+       GtkWidget *arrow;
        GtkWidget *label;
        GtkWidget *eventbox;
 
        DebugOut(("stock_create_gui"));
+
+       psContext->asStockImage[STOCK_IMAGE_INCREASE] = gdk_pixbuf_new_from_file(STOCK_PLUGIN_RESOURCE_UP_ARROW,   NULL);
+       psContext->asStockImage[STOCK_IMAGE_DECREASE] = gdk_pixbuf_new_from_file(STOCK_PLUGIN_RESOURCE_DOWN_ARROW, NULL);
        
        label    = gtk_label_new(pszStockLabel);
        eventbox = gtk_event_box_new();
+       arrow    = gtk_image_new_from_pixbuf(psContext->asStockImage[0]);
+       hbox     = gtk_hbox_new(FALSE,5);
 
        gtk_event_box_set_visible_window(GTK_EVENT_BOX(eventbox),FALSE);
        gtk_container_set_border_width(GTK_CONTAINER(eventbox),10);
 
-       gtk_container_add(GTK_CONTAINER(eventbox),label);
+       gtk_box_pack_start(GTK_BOX(hbox), arrow, FALSE, FALSE, 0);
+       gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
+       
+       gtk_container_add(GTK_CONTAINER(eventbox),hbox);
 
-       g_signal_connect(GTK_CONTAINER(eventbox),"button-press-event",G_CALLBACK(OnClickRefresh),(gpointer)psContext);
+       g_signal_connect(GTK_CONTAINER(eventbox),"button-press-event",G_CALLBACK(OnClickUpdate),(gpointer)psContext);
+       g_signal_connect(GTK_CONTAINER(eventbox),"button-release-event",G_CALLBACK(OnClickRefresh),(gpointer)psContext);
 
-       /* Display */
-       gtk_widget_show_all(GTK_WIDGET(eventbox));
+       /* Display all but the arrow */
+       gtk_widget_show_all(eventbox);
+       gtk_widget_hide(arrow); 
+       psContext->bArrowActive = FALSE;        
 
-       psContext->psLabel = label;
+       psContext->psArrow        = arrow;
+       psContext->psLabel        = label;
        psContext->psEventBox = eventbox;
        
        return eventbox;
@@ -325,6 +372,12 @@ stock_plugin_finalize (GObject *object)
                        psContext->nTimerID = -1;
                }
 
+               if(psContext->asStockImage[STOCK_IMAGE_DECREASE] != NULL)
+                       g_object_unref(psContext->asStockImage[STOCK_IMAGE_DECREASE]);
+               
+               if(psContext->asStockImage[STOCK_IMAGE_INCREASE] != NULL)
+                       g_object_unref(psContext->asStockImage[STOCK_IMAGE_INCREASE]);
+
                if(psContext->hSG)
                        FreeStockGetter(psContext->hSG);
                
@@ -359,7 +412,7 @@ stock_plugin_init (StockPlugin *desktop_plugin)
 
        /* Create GUI widget interface (and sets links in context) */
        stock_create_gui("Click to Update",psContext);
-
+       
        gtk_container_add(GTK_CONTAINER(desktop_plugin), psContext->psEventBox);
        
        psContext->hSG          = hSG;
index 1f48194..0a6935e 100644 (file)
@@ -9,6 +9,7 @@
 /* Standard includes */
 #include <glib-object.h>
 #include <libhildondesktop/libhildondesktop.h>
+#include <gdk-pixbuf/gdk-pixbuf.h>
 
 /* Custom includes */
 #include "stockgetter.h"
@@ -35,9 +36,16 @@ typedef struct _StockPluginContext
 {
        /* Used for updating when stock price changes */
        GtkWidget       *psLabel;
+       GtkWidget       *psArrow;
        GtkWidget       *psEventBox;
        GtkWidget       *psParent;
 
+       /* Arrow active */
+       gboolean bArrowActive;
+       
+       /* PixBuf of stock images */
+       GdkPixbuf *asStockImage[2];
+
        /* Stock Getter Handle */
        SGHandle hSG;
 
@@ -64,6 +72,9 @@ GType stock_home_plugin_get_type(void);
 
 G_END_DECLS
 
+#define STOCK_IMAGE_INCREASE 0
+#define STOCK_IMAGE_DECREASE 1
+
 #define DEBUG
 
 /* Debug print only */
index 02683e9..72e9efc 100644 (file)
@@ -8,11 +8,13 @@
 #include <gtk/gtk.h>
 
 /* Configuration file entries */
-#define STOCK_PLUGIN_APPLET_ID                 "stockwidget"
-#define STOCK_PLUGIN_TICKERS                   "tickers"
-#define STOCK_PLUGIN_UPDATE_TIME               "updatetime"
-#define STOCK_PLUGIN_SETTINGS_FILE             "/stockwidget.config"
-#define STOCK_PLUGIN_DEFAULT_TICKER            "IMG.L"
+#define STOCK_PLUGIN_APPLET_ID                                 "stockwidget"
+#define STOCK_PLUGIN_TICKERS                                   "tickers"
+#define STOCK_PLUGIN_UPDATE_TIME                               "updatetime"
+#define STOCK_PLUGIN_SETTINGS_FILE                             "/stockwidget.config"
+#define STOCK_PLUGIN_DEFAULT_TICKER                            "IMG.L"
+#define STOCK_PLUGIN_RESOURCE_UP_ARROW                 "/etc/hildon/theme/images/StockPluginIncrease.png"
+#define STOCK_PLUGIN_RESOURCE_DOWN_ARROW               "/etc/hildon/theme/images/StockPluginDecrease.png"
 
 /*
  * Struct used for holding settings stored out and loaded