[gcalctool] Add spinner while calculating



commit 68ab3424ef83fe93dc14aaa0eda65a61d9a05719
Author: Robin Sonefors <ozamosi flukkost nu>
Date:   Wed Oct 13 08:15:03 2010 +0200

    Add spinner while calculating

 src/math-display.c  |   40 ++++++++++++++++++++++++++++++++++------
 src/math-display.h  |    4 ++--
 src/math-equation.c |   29 ++++++++++++++++-------------
 src/math-equation.h |    2 ++
 src/math-window.c   |    2 +-
 5 files changed, 55 insertions(+), 22 deletions(-)
---
diff --git a/src/math-display.c b/src/math-display.c
index 9343cc3..1b623ef 100644
--- a/src/math-display.c
+++ b/src/math-display.c
@@ -37,9 +37,12 @@ struct MathDisplayPrivate
 
     /* Buffer that shows errors etc */
     GtkTextBuffer *info_buffer;
+
+    /* Spinner widget that shows if we're calculating a response */
+    GtkWidget *spinner;
 };
 
-G_DEFINE_TYPE (MathDisplay, math_display, GTK_TYPE_VBOX);
+G_DEFINE_TYPE (MathDisplay, math_display, GTK_TYPE_VIEWPORT);
 
 #define GET_WIDGET(ui, name)  GTK_WIDGET(gtk_builder_get_object(ui, name))
 
@@ -239,15 +242,28 @@ static void
 status_changed_cb(MathEquation *equation, GParamSpec *spec, MathDisplay *display)
 {
     gtk_text_buffer_set_text(display->priv->info_buffer, math_equation_get_status(equation), -1);
+    if (math_equation_in_solve(equation) && !gtk_widget_get_visible(display->priv->spinner)) {
+        gtk_widget_show(display->priv->spinner);
+        gtk_spinner_start(GTK_SPINNER(display->priv->spinner));
+    }
+    else if (!math_equation_in_solve(equation) && gtk_widget_get_visible(display->priv->spinner)) {
+        gtk_widget_hide(display->priv->spinner);
+        gtk_spinner_stop(GTK_SPINNER(display->priv->spinner));
+    }
 }
 
 
 static void
 create_gui(MathDisplay *display)
 {
-    GtkWidget *info_view;
+    GtkWidget *info_view, *info_box, *main_box;
     PangoFontDescription *font_desc;
-  
+    int i;
+    GtkStyle *style;
+
+    main_box = gtk_vbox_new(false, 0);
+    gtk_container_add(GTK_CONTAINER(display), main_box);
+
     g_signal_connect(display, "key-press-event", G_CALLBACK(key_press_cb), display);
 
     display->priv->text_view = gtk_text_view_new_with_buffer(GTK_TEXT_BUFFER(display->priv->equation));
@@ -267,8 +283,11 @@ create_gui(MathDisplay *display)
     atk_object_set_role(gtk_widget_get_accessible(display->priv->text_view), ATK_ROLE_EDITBAR);
   //FIXME:<property name="AtkObject::accessible-description" translatable="yes" comments="Accessible description for the area in which results are displayed">Result Region</property>
     g_signal_connect(display->priv->text_view, "key-press-event", G_CALLBACK(display_key_press_cb), display);
-    gtk_box_pack_start(GTK_BOX(display), display->priv->text_view, TRUE, TRUE, 0);
-  
+    gtk_box_pack_start(GTK_BOX(main_box), display->priv->text_view, TRUE, TRUE, 0);
+
+    info_box = gtk_hbox_new(false, 6);
+    gtk_box_pack_start(GTK_BOX(main_box), info_box, FALSE, TRUE, 0);
+
     info_view = gtk_text_view_new();
     gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(info_view), GTK_WRAP_WORD);
     gtk_widget_set_can_focus(info_view, TRUE); // FIXME: This should be FALSE but it locks the cursor inside the main view for some reason
@@ -277,11 +296,20 @@ create_gui(MathDisplay *display)
     gtk_text_view_set_justification(GTK_TEXT_VIEW(info_view), GTK_JUSTIFY_RIGHT);
     /* TEMP: Disabled for now as GTK+ doesn't properly render a right aligned right margin, see bug #482688 */
     /*gtk_text_view_set_right_margin(GTK_TEXT_VIEW(info_view), 6);*/
-    gtk_box_pack_start(GTK_BOX(display), info_view, FALSE, TRUE, 0);
+    gtk_box_pack_start(GTK_BOX(info_box), info_view, TRUE, TRUE, 0);
     display->priv->info_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(info_view));
 
+    display->priv->spinner = gtk_spinner_new();
+    gtk_box_pack_end(GTK_BOX(info_box), display->priv->spinner, FALSE, FALSE, 0);
+    style = gtk_widget_get_style(info_view);
+    for (i = 0; i < 5; i++) {
+        gtk_widget_modify_bg(GTK_WIDGET(display), i, &style->base[i]);
+    }
+
+    gtk_widget_show(info_box);
     gtk_widget_show(info_view);
     gtk_widget_show(display->priv->text_view);
+    gtk_widget_show(main_box);
 
     g_signal_connect(display->priv->equation, "notify::status", G_CALLBACK(status_changed_cb), display);
     status_changed_cb(display->priv->equation, NULL, display);
diff --git a/src/math-display.h b/src/math-display.h
index d9801d0..8613280 100644
--- a/src/math-display.h
+++ b/src/math-display.h
@@ -32,13 +32,13 @@ typedef struct MathDisplayPrivate MathDisplayPrivate;
 
 typedef struct
 {
-    GtkVBox parent_instance;
+    GtkViewport parent_instance;
     MathDisplayPrivate *priv;
 } MathDisplay;
 
 typedef struct
 {
-    GtkVBoxClass parent_class;
+    GtkViewportClass parent_class;
 } MathDisplayClass;
 
 GType math_display_get_type(void);
diff --git a/src/math-equation.c b/src/math-equation.c
index 71b8727..7d771e3 100644
--- a/src/math-equation.c
+++ b/src/math-equation.c
@@ -677,6 +677,13 @@ math_equation_get_number_mode(MathEquation *equation)
 }
 
 
+gboolean
+math_equation_in_solve(MathEquation *equation)
+{
+    return equation->priv->in_solve;
+}
+
+
 const MPNumber *
 math_equation_get_answer(MathEquation *equation)
 {
@@ -983,35 +990,29 @@ math_equation_look_for_answer(gpointer data)
     MathEquation *equation = MATH_EQUATION(data);
     SolveData *result = g_async_queue_try_pop(equation->priv->queue);
 
-    math_equation_set_status(equation, "Calculating...");
-
     if (result == NULL)
         return true;
 
+    equation->priv->in_solve = false;
+
+    if (!result->error)
+        math_equation_set_status(equation, "");
+
     if (result->error != NULL) {
         math_equation_set_status(equation, result->error);
         g_free(result->error);
     }
-    else {
-        math_equation_set_status(equation, "");
-    }
-
-    if (result->number_result != NULL) {
+    else if (result->number_result != NULL) {
         math_equation_set_number(equation, result->number_result);
         g_slice_free(MPNumber, result->number_result);
     }
-
-    if (result->text_result != NULL) {
+    else if (result->text_result != NULL) {
         math_equation_set(equation, result->text_result);
         g_free(result->text_result);
     }
-
     g_slice_free(SolveData, result);
 
-    equation->priv->in_solve = false;
-
     g_signal_emit_by_name(equation, "answer-changed");
-
     return false;
 }
 
@@ -1035,6 +1036,7 @@ math_equation_solve(MathEquation *equation)
     }
 
     equation->priv->in_solve = true;
+    math_equation_set_status(equation, "Calculating");
 
     math_equation_set_number_mode(equation, NORMAL);
 
@@ -1099,6 +1101,7 @@ math_equation_factorize(MathEquation *equation)
     }
 
     equation->priv->in_solve = true;
+    math_equation_set_status(equation, "Calculating");
 
     g_thread_create(math_equation_factorize_real, equation, false, &error);
 
diff --git a/src/math-equation.h b/src/math-equation.h
index fbba59c..2060425 100644
--- a/src/math-equation.h
+++ b/src/math-equation.h
@@ -99,6 +99,8 @@ const gchar *math_equation_get_source_currency(MathEquation *equation);
 void math_equation_set_target_currency(MathEquation *equation, const gchar *currency);
 const gchar *math_equation_get_target_currency(MathEquation *equation);
 
+gboolean math_equation_in_solve(MathEquation *equation);
+
 const MPNumber *math_equation_get_answer(MathEquation *equation);
 MpSerializer *math_equation_get_serializer(MathEquation *equation);
 
diff --git a/src/math-window.c b/src/math-window.c
index ffece09..7516672 100644
--- a/src/math-window.c
+++ b/src/math-window.c
@@ -429,7 +429,7 @@ create_gui(MathWindow *window)
     gtk_widget_show(scrolled_window);
 
     window->priv->display = math_display_new_with_equation(window->priv->equation);
-    gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled_window), GTK_WIDGET(window->priv->display));
+    gtk_container_add(GTK_CONTAINER(scrolled_window), GTK_WIDGET(window->priv->display));
     gtk_widget_show(GTK_WIDGET(window->priv->display));
 
     window->priv->buttons = math_buttons_new(window->priv->equation);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]