Fixed redrawing causing corruption of background.
[stockwidget] / lib-stock-home-widget.c
index 9f9e054..3ee9f82 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
-OnClickRefresh(GtkWidget *widget, StockPluginContext *psContext)
+OnClickRefresh(GtkWidget *widget, GdkEventButton *event, StockPluginContext *psContext)
 {
        DebugOut(("OnClickRefresh"));
        
        /* Disable button while getting latest stocks */
 {
        DebugOut(("OnClickRefresh"));
        
        /* Disable button while getting latest stocks */
-       gtk_widget_set_sensitive(GTK_WIDGET(psContext->psButton),FALSE);
        {
                float   fPrice;
                float   fChange;
        {
                float   fPrice;
                float   fChange;
@@ -27,6 +26,7 @@ OnClickRefresh(GtkWidget *widget, StockPluginContext *psContext)
                if(RetrieveStockPrice(psContext->hSG,psContext->psSettings->ppszTickers[0],&fPrice,NULL,&fChange) == SG_OK)
                {
                        char             szTime[80];
                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;
 
                        time_t           rawtime;
                        struct tm       *timeinfo;
 
@@ -34,11 +34,19 @@ OnClickRefresh(GtkWidget *widget, StockPluginContext *psContext)
                        timeinfo = localtime(&rawtime);
                        strftime(szTime,80,"(%I:%M%p)",timeinfo);
                        
                        timeinfo = localtime(&rawtime);
                        strftime(szTime,80,"(%I:%M%p)",timeinfo);
                        
+                       /* Highlight  the change in price */
+                       if(fChange > 0.0f)
+                               pszColour = "green";
+                       else
+                               pszColour = "red";
+
                        /* Update to latest price */
                        /* 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
                }
                /*
                  If it fails, leave the old price as the user can see the timestamp
@@ -49,7 +57,6 @@ OnClickRefresh(GtkWidget *widget, StockPluginContext *psContext)
                        hildon_banner_show_information(GTK_WIDGET(widget),NULL,"Failed to retrieve stock information, check ticker symbol and try again.");
                }
        }
                        hildon_banner_show_information(GTK_WIDGET(widget),NULL,"Failed to retrieve stock information, check ticker symbol and try again.");
                }
        }
-       gtk_widget_set_sensitive(GTK_WIDGET(psContext->psButton),TRUE);
 }
 
 static gboolean
 }
 
 static gboolean
@@ -67,7 +74,7 @@ UpdateStockOnTimeout(gpointer data)
                return FALSE;
 
        /* Update stock price */
                return FALSE;
 
        /* Update stock price */
-       OnClickRefresh(GTK_WIDGET(psContext->psButton),psContext);
+       OnClickRefresh(GTK_WIDGET(psContext->psLabel),NULL,psContext);
        return TRUE;
 }
 
        return TRUE;
 }
 
@@ -231,7 +238,7 @@ stock_show_settings_dialog(GtkWidget* widget, gpointer data)
                        }
 
                        /* Force a refresh as the ticker changed */
                        }
 
                        /* 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);
                }
 
                uiUpdateSelection = hildon_touch_selector_get_active(HILDON_TOUCH_SELECTOR(timeselector),0);
@@ -270,24 +277,28 @@ 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)
 {
-       HildonButton *psButton = NULL;
+       GtkWidget *label;
+       GtkWidget *eventbox;
 
        DebugOut(("stock_create_gui"));
        
 
        DebugOut(("stock_create_gui"));
        
-       psButton = (HildonButton*)hildon_button_new_with_text (HILDON_SIZE_AUTO_WIDTH | HILDON_SIZE_FINGER_HEIGHT,
-                                                                                       HILDON_BUTTON_ARRANGEMENT_VERTICAL,
-                                                                                       pszStockLabel,
-                                                                                       NULL);
+       label    = gtk_label_new(pszStockLabel);
+       eventbox = gtk_event_box_new();
+
+       gtk_event_box_set_visible_window(GTK_EVENT_BOX(eventbox),FALSE);
+       gtk_container_set_border_width(GTK_CONTAINER(eventbox),10);
 
 
-       psContext->psButton = psButton;
+       gtk_container_add(GTK_CONTAINER(eventbox),label);
 
 
-       /* Setup button click signal for refreshing stocks */
-       gtk_signal_connect (GTK_OBJECT(psButton),"clicked",GTK_SIGNAL_FUNC(OnClickRefresh),(gpointer)psContext);
+       g_signal_connect(GTK_CONTAINER(eventbox),"button-press-event",G_CALLBACK(OnClickRefresh),(gpointer)psContext);
 
        /* Display */
 
        /* Display */
-       gtk_widget_show_all(GTK_WIDGET(psButton));
+       gtk_widget_show_all(GTK_WIDGET(eventbox));
+
+       psContext->psLabel = label;
+       psContext->psEventBox = eventbox;
        
        
-       return GTK_WIDGET(psButton);
+       return eventbox;
 }
 
 static void
 }
 
 static void
@@ -347,12 +358,13 @@ stock_plugin_init (StockPlugin *desktop_plugin)
        stock_read_settings(psContext->psSettings);
 
        /* Create GUI widget interface (and sets links in context) */
        stock_read_settings(psContext->psSettings);
 
        /* Create GUI widget interface (and sets links in context) */
-       psContext->psBox = stock_create_gui("Click to Update",psContext);
-       
-       gtk_container_add (GTK_CONTAINER (desktop_plugin), psContext->psBox);
+       stock_create_gui("Click to Update",psContext);
+
+       gtk_container_add(GTK_CONTAINER(desktop_plugin), psContext->psEventBox);
        
        psContext->hSG          = hSG;
        psContext->nTimerID = -1;
        
        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;
@@ -379,8 +391,76 @@ stock_plugin_init (StockPlugin *desktop_plugin)
 }
 
 static void
 }
 
 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)
+{
+       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);
+
+       cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.25);
+       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_stroke (cr);
+}
+
+static gboolean
+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));
+
+       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);
+       #if 1
+       cairo_rounded_rectangle(cr, 0, 0, width, height, 10.0);
+       #else
+       cairo_rectangle(cr, 0, 0, width, height);
+       #endif
+       cairo_fill(cr);
+       
+       cairo_destroy(cr);
+       return GTK_WIDGET_CLASS(stock_plugin_parent_class)->expose_event(widget, event);
+}
+
+static void
 stock_plugin_class_init (StockPluginClass *klass)
 {
 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;
 }
 
        G_OBJECT_CLASS(klass)->finalize = stock_plugin_finalize;
 }