Added a graphical image representing increase/decrease in price.
[stockwidget] / lib-stock-home-widget.c
index 9f9e054..13891cf 100644 (file)
 HD_DEFINE_PLUGIN_MODULE (StockPlugin, stock_plugin, HD_TYPE_HOME_PLUGIN_ITEM)
 
 static void
-OnClickRefresh(GtkWidget *widget, StockPluginContext *psContext)
+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 */
-       gtk_widget_set_sensitive(GTK_WIDGET(psContext->psButton),FALSE);
        {
+               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);
                        strftime(szTime,80,"(%I:%M%p)",timeinfo);
                        
+                       /* 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,"%s %.2f %.2f%% %s",psContext->psSettings->ppszTickers[0],fPrice,fChange,szTime);
+                       sprintf(szBuffer,
+                                       "%s %.2f <span foreground=\"%s\">%.2f%%</span> <span foreground=\"grey\">%s</span>",
+                                       psContext->psSettings->ppszTickers[0],fPrice,pszColour,fChange,szTime);
 
-                       hildon_button_set_title(HILDON_BUTTON(psContext->psButton),szBuffer);
-                       gtk_widget_queue_draw(GTK_WIDGET(psContext->psBox));
+                       gtk_label_set_markup(GTK_LABEL(psContext->psLabel),szBuffer);
+                       gtk_widget_queue_draw(GTK_WIDGET(psContext->psParent));
                }
                /*
                  If it fails, leave the old price as the user can see the timestamp
@@ -46,10 +86,10 @@ OnClickRefresh(GtkWidget *widget, StockPluginContext *psContext)
                 */
                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));
                }
        }
-       gtk_widget_set_sensitive(GTK_WIDGET(psContext->psButton),TRUE);
 }
 
 static gboolean
@@ -67,7 +107,7 @@ UpdateStockOnTimeout(gpointer data)
                return FALSE;
 
        /* Update stock price */
-       OnClickRefresh(GTK_WIDGET(psContext->psButton),psContext);
+       OnClickRefresh(GTK_WIDGET(psContext->psLabel),NULL,psContext);
        return TRUE;
 }
 
@@ -231,7 +271,7 @@ stock_show_settings_dialog(GtkWidget* widget, gpointer data)
                        }
 
                        /* Force a refresh as the ticker changed */
-                       OnClickRefresh(GTK_WIDGET(psContext->psButton),psContext);
+                       OnClickRefresh(GTK_WIDGET(psContext->psLabel),NULL,psContext);
                }
 
                uiUpdateSelection = hildon_touch_selector_get_active(HILDON_TOUCH_SELECTOR(timeselector),0);
@@ -270,24 +310,42 @@ stock_show_settings_dialog(GtkWidget* widget, gpointer data)
 static GtkWidget *stock_create_gui(gchar                               *pszStockLabel,
                                                                   StockPluginContext   *psContext)
 {
-       HildonButton *psButton = NULL;
+       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_box_pack_start(GTK_BOX(hbox), arrow, FALSE, FALSE, 0);
+       gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
        
-       psButton = (HildonButton*)hildon_button_new_with_text (HILDON_SIZE_AUTO_WIDTH | HILDON_SIZE_FINGER_HEIGHT,
-                                                                                       HILDON_BUTTON_ARRANGEMENT_VERTICAL,
-                                                                                       pszStockLabel,
-                                                                                       NULL);
+       gtk_container_add(GTK_CONTAINER(eventbox),hbox);
 
-       psContext->psButton = psButton;
+       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);
 
-       /* Setup button click signal for refreshing stocks */
-       gtk_signal_connect (GTK_OBJECT(psButton),"clicked",GTK_SIGNAL_FUNC(OnClickRefresh),(gpointer)psContext);
+       /* Display all but the arrow */
+       gtk_widget_show_all(eventbox);
+       gtk_widget_hide(arrow); 
+       psContext->bArrowActive = FALSE;        
 
-       /* Display */
-       gtk_widget_show_all(GTK_WIDGET(psButton));
+       psContext->psArrow        = arrow;
+       psContext->psLabel        = label;
+       psContext->psEventBox = eventbox;
        
-       return GTK_WIDGET(psButton);
+       return eventbox;
 }
 
 static void
@@ -314,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);
                
@@ -347,12 +411,13 @@ stock_plugin_init (StockPlugin *desktop_plugin)
        stock_read_settings(psContext->psSettings);
 
        /* Create GUI widget interface (and sets links in context) */
-       psContext->psBox = stock_create_gui("Click to Update",psContext);
+       stock_create_gui("Click to Update",psContext);
        
-       gtk_container_add (GTK_CONTAINER (desktop_plugin), psContext->psBox);
+       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;
@@ -379,8 +444,67 @@ stock_plugin_init (StockPlugin *desktop_plugin)
 }
 
 static void
+stock_plugin_realize(GtkWidget* widget)
+{
+       GdkScreen *screen = gtk_widget_get_screen(widget);
+       gtk_widget_set_colormap(widget, gdk_screen_get_rgba_colormap(screen));
+       gtk_widget_set_app_paintable(widget, TRUE);
+       GTK_WIDGET_CLASS(stock_plugin_parent_class)->realize(widget);
+}
+
+void
+cairo_rounded_rectangle(cairo_t *cr,
+                                               double x,
+                                               double y,
+                                               double width,
+                                               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          degrees           = 3.14159265 / 180.0;
+
+       cairo_new_sub_path (cr);
+       cairo_arc (cr, x + width - radius, y + radius, radius, -90 * degrees, 0 * degrees);
+       cairo_arc (cr, x + width - radius, y + height - radius, radius, 0 * degrees, 90 * degrees);
+       cairo_arc (cr, x + radius, y + height - radius, radius, 90 * degrees, 180 * degrees);
+       cairo_arc (cr, x + radius, y + radius, radius, 180 * degrees, 270 * degrees);
+       cairo_close_path (cr);
+
+       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_set_line_width (cr, 0.0);
+       cairo_stroke (cr);
+}
+
+static gboolean
+stock_plugin_expose(GtkWidget* widget, GdkEventExpose *event)
+{
+       cairo_t*                         cr;
+       
+       cr = gdk_cairo_create(GDK_DRAWABLE(widget->window));
+       
+       cairo_rounded_rectangle(cr, 0, 0, event->area.width, event->area.height, 10.0);
+       cairo_destroy(cr);
+       
+       return GTK_WIDGET_CLASS(stock_plugin_parent_class)->expose_event(widget, event);
+}
+
+static void
 stock_plugin_class_init (StockPluginClass *klass)
 {
+       GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
+       widget_class->realize                   = stock_plugin_realize;
+       widget_class->expose_event              = stock_plugin_expose;
        G_OBJECT_CLASS(klass)->finalize = stock_plugin_finalize;
 }