[genius] Wed Sep 21 23:47:46 2016 Jiri (George) Lebl <jirka 5z com>



commit 26b5fa4386c57fa61e88b9ce20b8f6d02280b09c
Author: Jiri (George) Lebl <jiri lebl gmail com>
Date:   Wed Sep 21 23:47:49 2016 -0500

    Wed Sep 21 23:47:46 2016  Jiri (George) Lebl <jirka 5z com>
    
        * src/gnome-genius.c, src/examples.[ch]: split up examples into
          submenus by category
    
        * src/gnome-genius.c: fix warnings
    
        * examples/*.gel: fix some usage of undocumented syntax for
          LinePlotLineDraw

 ChangeLog                 |   10 +++++
 examples/linapprox.gel    |    8 ++--
 examples/lorenz.gel       |    4 +-
 examples/newton-sqrt2.gel |    6 +++-
 examples/shocks.gel       |    2 +-
 src/examples.c            |   82 +++++++++++++++++++++++++++++++++++---------
 src/examples.h            |    8 ++++-
 src/gnome-genius.c        |   60 +++++++++++++++++++--------------
 8 files changed, 129 insertions(+), 51 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index e9be7fc..f462d35 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+Wed Sep 21 23:47:46 2016  Jiri (George) Lebl <jirka 5z com>
+
+       * src/gnome-genius.c, src/examples.[ch]: split up examples into
+         submenus by category
+
+       * src/gnome-genius.c: fix warnings
+
+       * examples/*.gel: fix some usage of undocumented syntax for
+         LinePlotLineDraw
+
 Wed Sep 21 20:54:37 2016  Jiri (George) Lebl <jirka 5z com>
 
        * src/gnome-genius.c: run program from idle to avoid a queued up
diff --git a/examples/linapprox.gel b/examples/linapprox.gel
index 58054e8..194a4e5 100644
--- a/examples/linapprox.gel
+++ b/examples/linapprox.gel
@@ -13,10 +13,10 @@ function lf(x) = df(a)*(x-a)+f(a);
 function DoGraph(x) = (
   PlotCanvasFreeze();
   LinePlot(f,lf,[-0.5,5,-0.1,2]);
-  LinePlotDrawLine([x,0,x,min(f(x),lf(x))],"color","orange");
-  LinePlotDrawLine([x,min(f(x),lf(x)),x,max(f(x),lf(x))],"color","red");
-  LinePlotDrawLine([-0.5,f(x),5,f(x)],"color","black","thickness",1);
-  LinePlotDrawLine([-0.5,lf(x),5,lf(x)],"color","purple","thickness",1);
+  LinePlotDrawLine(x,0,x,min(f(x),lf(x)),"color","orange");
+  LinePlotDrawLine(x,min(f(x),lf(x)),x,max(f(x),lf(x)),"color","red");
+  LinePlotDrawLine(-0.5,f(x),5,f(x),"color","black","thickness",1);
+  LinePlotDrawLine(-0.5,lf(x),5,lf(x),"color","purple","thickness",1);
   PlotCanvasThaw();
   print("at x=" + x + "  error = |" + lf(x) + " - " + f(x) + "| = " + |lf(x)-f(x)|);
 );
diff --git a/examples/lorenz.gel b/examples/lorenz.gel
index a229dd9..17b091f 100644
--- a/examples/lorenz.gel
+++ b/examples/lorenz.gel
@@ -9,8 +9,8 @@
 # The Lorenz system (try playing around with the constants,
 # especially the 28)
 function lorenz(t,x) = [10*(x@(2)-x@(1)),
-                                         x@(1)*(28-x@(3))-x@(2),
-                                         x@(1)*x@(2)-(8/3)*x@(3)];
+                        x@(1)*(28-x@(3))-x@(2),
+                        x@(1)*x@(2)-(8/3)*x@(3)];
 
 # The [1,1,20] is an initial condition, try playing around with it,
 # the 15 is the time to follow a trajectory, 10000 is the number of
diff --git a/examples/newton-sqrt2.gel b/examples/newton-sqrt2.gel
index 40cfec3..4832e76 100644
--- a/examples/newton-sqrt2.gel
+++ b/examples/newton-sqrt2.gel
@@ -17,10 +17,12 @@ LinePlotDrawLine(c1,-100,c1,100,"color","red","thickness",1);
 
 AskButtons("We're starting with an estimate at x=2","OK");
 
-LinePlot(f,`(x)=df(c1)*(x-c1)+f(c1));
+LinePlot(f,
+         `(x)=df(c1)*(x-c1)+f(c1));
 c2=c1-f(c1)/df(c1);
 LinePlotDrawLine(c1,-100,c1,100,"color","black","thickness",1);
 LinePlotDrawLine(c2,-100,c2,100,"color","red","thickness",1);
+PlotWindowPresent(); # Make sure the window is raised
 AskButtons(float(c2)+" (real sqrt(2) is " + sqrt(2) + ")","OK");
 
 LinePlot(f,
@@ -29,6 +31,7 @@ c3=c2-f(c2)/df(c2);
 LinePlotDrawLine(c1,-100,c1,100,"color","black","thickness",1);
 LinePlotDrawLine(c2,-100,c2,100,"color","black","thickness",1);
 LinePlotDrawLine(c3,-100,c3,100,"color","red","thickness",1);
+PlotWindowPresent(); # Make sure the window is raised
 AskButtons(float(c3)+" (real sqrt(2) is " + sqrt(2) + ")","OK");
 
 LinePlot(f,
@@ -38,4 +41,5 @@ LinePlotDrawLine(c1,-100,c1,100,"color","black","thickness",1);
 LinePlotDrawLine(c2,-100,c2,100,"color","black","thickness",1);
 LinePlotDrawLine(c3,-100,c3,100,"color","black","thickness",1);
 LinePlotDrawLine(c4,-100,c4,100,"color","red","thickness",1);
+PlotWindowPresent(); # Make sure the window is raised
 AskButtons(float(c4)+" (real sqrt(2) is " + sqrt(2) + ")","OK");
diff --git a/examples/shocks.gel b/examples/shocks.gel
index e5fb2e6..e135098 100644
--- a/examples/shocks.gel
+++ b/examples/shocks.gel
@@ -51,5 +51,5 @@ function the_color(x0) = [phi(x0), #red
 
 # Draw characteristics
 for x0=xstart to xend by xstep do (
-  LinePlotDrawLine([x0,0,x0+g(phi(x0))*tend,tend], "color", the_color (x0))
+  LinePlotDrawLine([x0,0;x0+g(phi(x0))*tend,tend], "color", the_color (x0))
 )
diff --git a/src/examples.c b/src/examples.c
index 58dd6ad..f0b0632 100644
--- a/src/examples.c
+++ b/src/examples.c
@@ -1,5 +1,5 @@
 /* GENIUS Calculator
- * Copyright (C) 1997-2014 Jiri (George) Lebl
+ * Copyright (C) 1997-2016 Jiri (George) Lebl
  *
  * Author: Jiri (George) Lebl
  *
@@ -52,6 +52,40 @@
 #include "binreloc.h"
 
 GSList *gel_example_list = NULL;
+GSList *gel_example_categories_list = NULL;
+
+
+static GelExampleCategory *
+gel_get_example_category (const char *name)
+{
+       GSList *li;
+       static int k = 0;
+       static GelExampleCategory *last = NULL;
+       GelExampleCategory *cat;
+
+       if (last != NULL && strcmp(last->name, name) == 0) {
+               return last;
+       }
+
+       for (li = gel_example_categories_list; li != NULL; li = li->next) {
+               cat = li->data;
+               if (strcmp (cat->name, name) == 0) {
+                       last = cat;
+                       return cat;
+               }
+       }
+
+       cat = g_new (GelExampleCategory, 1);
+       cat->name = g_strdup (name);
+       cat->examples = NULL;
+
+       gel_example_categories_list =
+               g_slist_prepend (gel_example_categories_list, cat);
+
+       last = cat;
+
+       return cat;
+}
 
 static GelExample *
 gel_readexample (const char *dir_name, const char *file_name)
@@ -115,15 +149,6 @@ gel_readexample (const char *dir_name, const char *file_name)
 }
 
 static void
-free_example(GelExample *exam)
-{
-       g_free (exam->category);
-       g_free (exam->name);
-       g_free (exam->file);
-       g_free (exam);
-}
-
-static void
 read_examples_from_dir (const char *dir_name)
 {
        DIR *dir;
@@ -146,7 +171,12 @@ read_examples_from_dir (const char *dir_name)
                        continue;
                exam = gel_readexample (dir_name, dent->d_name);
                if (exam != NULL) {
-                       gel_example_list = g_slist_prepend (gel_example_list, exam);
+                       GelExampleCategory *cat;
+
+                       gel_example_list = g_slist_prepend (gel_example_list,
+                                                           exam);
+                       cat = gel_get_example_category (exam->category);
+                       cat->examples = g_slist_prepend (cat->examples, exam);
                }
        }
        closedir (dir);
@@ -161,17 +191,25 @@ compare_examples (GelExample *a, GelExample *b)
        return strcmp (a->name, b->name);
 }
 
+static int
+compare_examples_byname (GelExample *a, GelExample *b)
+{
+       return strcmp (a->name, b->name);
+}
+
+static int
+compare_example_categories (GelExampleCategory *a, GelExampleCategory *b)
+{
+       return strcmp (a->name, b->name);
+}
+
 void
 gel_read_example_list (void)
 {
+       GSList *li;
        char *dir_name;
        char *datadir;
 
-       /*free the previous list*/
-       g_slist_foreach (gel_example_list, (GFunc)free_example, NULL);
-       g_slist_free (gel_example_list);
-       gel_example_list = NULL;
-       
        datadir = gbr_find_data_dir (DATADIR);
        dir_name = g_build_filename (datadir, "genius", "examples", NULL);
        g_free (datadir);
@@ -183,7 +221,17 @@ gel_read_example_list (void)
        read_examples_from_dir (dir_name);
        g_free (dir_name);
 
-       /* FIXME: should do more */
        gel_example_list = g_slist_sort (gel_example_list,
                                         (GCompareFunc)compare_examples);
+
+       gel_example_categories_list =
+               g_slist_sort (gel_example_categories_list,
+                             (GCompareFunc)compare_example_categories);
+
+       for (li = gel_example_categories_list; li != NULL; li = li->next) {
+               GelExampleCategory *cat = li->data;
+               cat->examples =
+                       g_slist_sort (cat->examples,
+                                     (GCompareFunc)compare_examples_byname);
+       }
 }
diff --git a/src/examples.h b/src/examples.h
index 6d24a50..281e394 100644
--- a/src/examples.h
+++ b/src/examples.h
@@ -1,5 +1,5 @@
 /* GENIUS Calculator
- * Copyright (C) 1997-2014 Jiri (George) Lebl
+ * Copyright (C) 1997-2016 Jiri (George) Lebl
  *
  * Author: Jiri (George) Lebl
  *
@@ -28,7 +28,13 @@ typedef struct _GelExample {
        char *file;
 } GelExample;
 
+typedef struct _GelExampleCategory {
+       char *name;
+       GSList *examples;
+} GelExampleCategory;
+
 void gel_read_example_list(void);
 extern GSList *gel_example_list;
+extern GSList *gel_example_categories_list;
 
 #endif /* EXAMPLES_H__ */
diff --git a/src/gnome-genius.c b/src/gnome-genius.c
index fa60319..98c29ec 100644
--- a/src/gnome-genius.c
+++ b/src/gnome-genius.c
@@ -4236,12 +4236,12 @@ run_program_idle (gpointer data)
                                      _("<b>No program selected.</b>\n\n"
                                        "Create a new program, or select an "
                                        "existing tab in the notebook."));
-               return;
+               return FALSE;
        }
        buffer = selected_program->buffer;
        /* sanity */
        if (buffer == NULL)
-               return;
+               return FALSE;
        name = selected_program->name;
        vname = selected_program->vname;
        if (vname == NULL)
@@ -4249,7 +4249,7 @@ run_program_idle (gpointer data)
 
        if (gel_calc_running) {
                executing_warning ();
-               return;
+               return FALSE;
        } else {
                GtkTextIter iter, iter_end;
                char *prog;
@@ -4267,7 +4267,7 @@ run_program_idle (gpointer data)
                                 g_strerror (errno));
                        genius_display_error (NULL, err);
                        g_free (err);
-                       return;
+                       return FALSE;
                }
 
                gtk_text_buffer_get_iter_at_offset (buffer, &iter, 0);
@@ -4294,7 +4294,7 @@ run_program_idle (gpointer data)
                        genius_display_error (NULL,
                                              _("<b>Cannot execute program</b>\n\n"
                                                "Cannot fork."));
-                       return;
+                       return FALSE;
                }
 
                if (pid == 0) {
@@ -5401,32 +5401,42 @@ main (int argc, char *argv[])
        /* Read examples now */
        gel_read_example_list ();
        if (gel_example_list != NULL) {
-               GSList *li;
-               int i;
+               GSList *li, *l;
                GtkWidget *menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (gtk_ui_manager_get_widget 
(genius_ui, "/MenuBar/ExamplesMenu")));
 
-               for (i = 0, li = gel_example_list;
+               for (li = gel_example_categories_list;
                     li != NULL;
-                    li = li->next, i++) {
+                    li = li->next) {
+                       GtkWidget *submenu;
                        GtkWidget *item;
-                       GelExample *exam = li->data;
-                       char *s;
-
-                       s = g_strconcat (exam->category, ": ", exam->name, NULL);
-                       item = gtk_menu_item_new_with_label (s);
-                       g_free (s);
+                       GelExampleCategory *cat = li->data;
 
-                       g_signal_connect (item, "select",
-                                         G_CALLBACK (simple_menu_item_select_cb), 
-                                         exam->name);
-                       g_signal_connect (item, "deselect",
-                                         G_CALLBACK (simple_menu_item_deselect_cb), 
-                                         exam->name);
-                       gtk_widget_show (item);
-                       g_signal_connect (G_OBJECT (item), "activate",
-                                         G_CALLBACK (open_example_cb), exam);
+                       item = gtk_menu_item_new_with_label (cat->name);
                        gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
-                       example_count ++;
+
+                       submenu = gtk_menu_new ();
+                       gtk_menu_item_set_submenu (GTK_MENU_ITEM (item),
+                                                  submenu);
+                       gtk_widget_show (GTK_WIDGET (item));
+                       gtk_widget_show (GTK_WIDGET (submenu));
+
+                       for (l = cat->examples; l != NULL; l = l->next) {
+                               GelExample *exam = l->data;
+
+                               item = gtk_menu_item_new_with_label (exam->name);
+
+                               g_signal_connect (item, "select",
+                                                 G_CALLBACK (simple_menu_item_select_cb), 
+                                                 exam->name);
+                               g_signal_connect (item, "deselect",
+                                                 G_CALLBACK (simple_menu_item_deselect_cb), 
+                                                 exam->name);
+                               gtk_widget_show (item);
+                               g_signal_connect (G_OBJECT (item), "activate",
+                                                 G_CALLBACK (open_example_cb), exam);
+                               gtk_menu_shell_append (GTK_MENU_SHELL (submenu), item);
+                               example_count ++;
+                       }
                }
        }
        /* if no exampleials, hide the menu */


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