Added a graphical image representing increase/decrease in price.
[stockwidget] / lib-stock-home-widget.c
index 628d73f..13891cf 100644 (file)
 HD_DEFINE_PLUGIN_MODULE (StockPlugin, stock_plugin, HD_TYPE_HOME_PLUGIN_ITEM)
 
 static void
 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 */
        {
 OnClickRefresh(GtkWidget *widget, GdkEventButton *event, StockPluginContext *psContext)
 {
        DebugOut(("OnClickRefresh"));
        
        /* Disable button while getting latest stocks */
        {
+               char    szBuffer[1024]; 
                float   fPrice;
                float   fChange;
                float   fPrice;
                float   fChange;
-               char    szBuffer[1024];         
 
                if(RetrieveStockPrice(psContext->hSG,psContext->psSettings->ppszTickers[0],&fPrice,NULL,&fChange) == SG_OK)
                {
 
                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            *pszColour = NULL;
                        time_t           rawtime;
                        struct tm       *timeinfo;
+                       char             szTime[80];
+                       int                      nImageSel;
 
                        time(&rawtime);
                        timeinfo = localtime(&rawtime);
 
                        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)
                        
                        /* Highlight  the change in price */
                        if(fChange > 0.0f)
+                       {
+                               nImageSel = STOCK_IMAGE_INCREASE;
                                pszColour = "green";
                                pszColour = "green";
+                       }
                        else
                        else
+                       {
+                               nImageSel = STOCK_IMAGE_DECREASE;
                                pszColour = "red";
                                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,
 
                        /* Update to latest price */
                        sprintf(szBuffer,
@@ -46,7 +78,7 @@ OnClickRefresh(GtkWidget *widget, GdkEventButton *event, StockPluginContext *psC
                                        psContext->psSettings->ppszTickers[0],fPrice,pszColour,fChange,szTime);
 
                        gtk_label_set_markup(GTK_LABEL(psContext->psLabel),szBuffer);
                                        psContext->psSettings->ppszTickers[0],fPrice,pszColour,fChange,szTime);
 
                        gtk_label_set_markup(GTK_LABEL(psContext->psLabel),szBuffer);
-                       gtk_widget_queue_draw(GTK_WIDGET(psContext->psLabel));
+                       gtk_widget_queue_draw(GTK_WIDGET(psContext->psParent));
                }
                /*
                  If it fails, leave the old price as the user can see the timestamp
                }
                /*
                  If it fails, leave the old price as the user can see the timestamp
@@ -54,7 +86,8 @@ OnClickRefresh(GtkWidget *widget, GdkEventButton *event, StockPluginContext *psC
                 */
                else
                {
                 */
                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)
 {
 static GtkWidget *stock_create_gui(gchar                               *pszStockLabel,
                                                                   StockPluginContext   *psContext)
 {
+       GtkWidget *hbox;
+       GtkWidget *arrow;
        GtkWidget *label;
        GtkWidget *eventbox;
 
        DebugOut(("stock_create_gui"));
        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();
        
        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_event_box_set_visible_window(GTK_EVENT_BOX(eventbox),FALSE);
-       gtk_container_set_border_width(GTK_CONTAINER(eventbox),3);
+       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;
        psContext->psEventBox = eventbox;
        
        return eventbox;
@@ -325,6 +372,12 @@ stock_plugin_finalize (GObject *object)
                        psContext->nTimerID = -1;
                }
 
                        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);
                
                if(psContext->hSG)
                        FreeStockGetter(psContext->hSG);
                
@@ -359,11 +412,12 @@ stock_plugin_init (StockPlugin *desktop_plugin)
 
        /* Create GUI widget interface (and sets links in context) */
        stock_create_gui("Click to Update",psContext);
 
        /* 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;
        psContext->nTimerID = -1;
        gtk_container_add(GTK_CONTAINER(desktop_plugin), psContext->psEventBox);
        
        psContext->hSG          = hSG;
        psContext->nTimerID = -1;
+       psContext->psParent = GTK_WIDGET(desktop_plugin);
 
        /* Assign private data to plugin */
        desktop_plugin->context = psContext;
 
        /* Assign private data to plugin */
        desktop_plugin->context = psContext;
@@ -406,6 +460,8 @@ cairo_rounded_rectangle(cairo_t *cr,
                                                double height,
                                                double corner_div)
 {
                                                double height,
                                                double corner_div)
 {
+       cairo_pattern_t *pat;
+       
        double          aspect            = 1.0;
        double          corner_radius = height / corner_div;
        double          radius            = corner_radius / aspect;
        double          aspect            = 1.0;
        double          corner_radius = height / corner_div;
        double          radius            = corner_radius / aspect;
@@ -418,10 +474,15 @@ cairo_rounded_rectangle(cairo_t *cr,
        cairo_arc (cr, x + radius, y + radius, radius, 180 * degrees, 270 * degrees);
        cairo_close_path (cr);
 
        cairo_arc (cr, x + radius, y + radius, radius, 180 * degrees, 270 * degrees);
        cairo_close_path (cr);
 
-       cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.25);
+       pat = cairo_pattern_create_linear(0.0, 0.0, 0.0, height);
+
+       cairo_pattern_add_color_stop_rgba(pat,0.0, 0.8, 0.8, 0.8, 0.7);
+       cairo_pattern_add_color_stop_rgba(pat,1.0, 0.0, 0.0, 0.0, 0.7);
+
+       cairo_set_source(cr,pat);
        cairo_fill_preserve (cr);
        cairo_fill_preserve (cr);
-       cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 1.0);
-       cairo_set_line_width (cr, 1.0);
+
+       cairo_set_line_width (cr, 0.0);
        cairo_stroke (cr);
 }
 
        cairo_stroke (cr);
 }
 
@@ -429,24 +490,12 @@ static gboolean
 stock_plugin_expose(GtkWidget* widget, GdkEventExpose *event)
 {
        cairo_t*                         cr;
 stock_plugin_expose(GtkWidget* widget, GdkEventExpose *event)
 {
        cairo_t*                         cr;
-       int                                      width  = 0;
-       int                                      height = 0;
-       GdkRectangle            *psRect;
        
        cr = gdk_cairo_create(GDK_DRAWABLE(widget->window));
        
        cr = gdk_cairo_create(GDK_DRAWABLE(widget->window));
-
-       psRect = &event->area;
-
-       width = psRect->width;
-       height = psRect->height;
-
-       DebugOut(("X: %d Y: %d Width: %d Height %d",psRect->x,psRect->y,psRect->width,psRect->height));
-       
-       cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.5);
-       cairo_rounded_rectangle(cr, 0, 0, width, height, 10.0);
-       cairo_fill(cr);
        
        
+       cairo_rounded_rectangle(cr, 0, 0, event->area.width, event->area.height, 10.0);
        cairo_destroy(cr);
        cairo_destroy(cr);
+       
        return GTK_WIDGET_CLASS(stock_plugin_parent_class)->expose_event(widget, event);
 }
 
        return GTK_WIDGET_CLASS(stock_plugin_parent_class)->expose_event(widget, event);
 }
 
@@ -454,10 +503,8 @@ static void
 stock_plugin_class_init (StockPluginClass *klass)
 {
        GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
 stock_plugin_class_init (StockPluginClass *klass)
 {
        GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
-       #if 1
        widget_class->realize                   = stock_plugin_realize;
        widget_class->expose_event              = stock_plugin_expose;
        widget_class->realize                   = stock_plugin_realize;
        widget_class->expose_event              = stock_plugin_expose;
-       #endif
        G_OBJECT_CLASS(klass)->finalize = stock_plugin_finalize;
 }
 
        G_OBJECT_CLASS(klass)->finalize = stock_plugin_finalize;
 }