From 9b55dd183e299450546bec338d4bf64be2d0a8ab Mon Sep 17 00:00:00 2001
From: Christophe Geuzaine <cgeuzaine@ulg.ac.be>
Date: Mon, 28 Sep 2009 18:59:33 +0000
Subject: [PATCH] little hack to finish remote demonstrator (until we have
 proper serialization of options)

---
 Common/StringUtils.cpp | 11 +++++++++++
 Common/StringUtils.h   |  1 +
 Fltk/extraDialogs.cpp  | 40 +++++++++++++++++++++++-----------------
 Post/PViewDataRemote.h |  9 ++++++++-
 4 files changed, 43 insertions(+), 18 deletions(-)

diff --git a/Common/StringUtils.cpp b/Common/StringUtils.cpp
index f254172a49..9d178b7da4 100644
--- a/Common/StringUtils.cpp
+++ b/Common/StringUtils.cpp
@@ -129,3 +129,14 @@ std::string ReplacePercentS(std::string in, std::string val)
   }
   return out;
 }
+
+std::string ConvertFileToString(std::string fileName)
+{
+  FILE *fp = fopen(fileName.c_str(), "r");
+  if(!fp) return "";
+  std::string out;
+  char str[256];
+  while(!feof(fp) && fgets(str, sizeof(str), fp)) out += str;
+  fclose(fp);
+  return out;
+}
diff --git a/Common/StringUtils.h b/Common/StringUtils.h
index 4393b88361..9e66463848 100644
--- a/Common/StringUtils.h
+++ b/Common/StringUtils.h
@@ -18,5 +18,6 @@ std::string FixRelativePath(std::string reference, std::string in);
 std::vector<std::string> SplitFileName(std::string fileName);
 std::vector<std::string> SplitWhiteSpace(std::string in, unsigned int len);
 std::string ReplacePercentS(std::string in, std::string val);
+std::string ConvertFileToString(std::string fileName);
 
 #endif
diff --git a/Fltk/extraDialogs.cpp b/Fltk/extraDialogs.cpp
index d506218e80..dc64ac4f52 100644
--- a/Fltk/extraDialogs.cpp
+++ b/Fltk/extraDialogs.cpp
@@ -7,6 +7,7 @@
 //   Stephen Guzik
 //
 
+#include <stdio.h>
 #include <FL/Fl_Value_Slider.H>
 #include <FL/Fl_Menu_Window.H>
 #include <FL/Fl_Select_Browser.H>
@@ -211,10 +212,8 @@ class ConnectionBrowser : public Fl_Hold_Browser {
         int i = value();
         if(i){
           remove(i);
-          if(i <= size())
-            select(i);
-          else if(i > 1)
-            select(i - 1);
+          if(i <= size()) select(i);
+          else if(i > 1) select(i - 1);
         }
         return 1;
       }
@@ -227,17 +226,12 @@ class ConnectionBrowser : public Fl_Hold_Browser {
     : Fl_Hold_Browser(x, y, w, h, l) {}
   void save(Fl_Preferences &prefs)
   {
-    std::set<std::string> uniq;
-    for(int i = 0; i < size(); i++) uniq.insert(text(i + 1));
-    char name[256];
-    int j = 0;
-    for(std::set<std::string>::iterator it = uniq.begin(); it != uniq.end(); it++){
-      sprintf(name, "connection%02d", j++);
-      prefs.set(name, it->c_str());
-    }
-    for(int i = j; i < 100; i++){
+    for(int i = 0; i < 100; i++){
+      char name[256];
       sprintf(name, "connection%02d", i);
-      if(prefs.entryExists(name))
+      if(i < size())
+        prefs.set(name, text(i + 1));
+      else if(prefs.entryExists(name)) 
         prefs.deleteEntry(name);
     }
   }
@@ -288,7 +282,7 @@ std::string connectionChooser()
 
   int old = chooser->browser->value();
   chooser->browser->clear();
-  for (int i = 0; i < 100; i ++) {
+  for (int i = 0; i < 100; i++) {
     char name[256], value[1024];
     sprintf(name, "connection%02d", i);
     if(prefs.entryExists(name)){
@@ -301,7 +295,7 @@ std::string connectionChooser()
     if(old > 0 && old <= n)
       chooser->input->value(chooser->browser->text(old));
     else
-      chooser->input->value(chooser->browser->text(n));
+      chooser->input->value(chooser->browser->text(1));
   }
   else
     chooser->input->value("./gmsh ../tutorial/view3.pos");
@@ -314,7 +308,19 @@ std::string connectionChooser()
       Fl_Widget* o = Fl::readqueue();
       if (!o) break;
       if (o == chooser->ok) {
-        chooser->browser->add(chooser->input->value());
+        if(strlen(chooser->input->value())){
+          // insert choosen value at the top of the history if it's not
+          // already present
+          bool found = false;
+          for(int i = 0; i < chooser->browser->size(); i++){
+            if(!strcmp(chooser->input->value(), chooser->browser->text(i + 1))){
+              found = true;
+              break;
+            }
+          }
+          if(!found)
+            chooser->browser->insert(1, chooser->input->value());
+        }
         chooser->browser->save(prefs);
         chooser->window->hide();
         return chooser->input->value();
diff --git a/Post/PViewDataRemote.h b/Post/PViewDataRemote.h
index 2551740d60..69df8c7f17 100644
--- a/Post/PViewDataRemote.h
+++ b/Post/PViewDataRemote.h
@@ -13,6 +13,9 @@
 #include "GmshSocket.h"
 #include "PViewData.h"
 #include "SBoundingBox3d.h"
+#include "Options.h"
+#include "StringUtils.h"
+#include "Context.h"
 
 // The container for a remote dataset (does not contain any actual
 // data!)
@@ -64,7 +67,11 @@ class PViewDataRemote : public PViewData {
       return 1;
     }
     setDirty(true);
-    // server->SendString(GmshSocket::GMSH_PARSER_STRING, options);
+    std::string fileName = CTX::instance()->homeDir + CTX::instance()->tmpFileName;
+    // FIXME: until we rewrite the option code and allow nice serialization ;-)
+    PrintOptions(0, GMSH_FULLRC, 1, 0, fileName.c_str());
+    std::string options = ConvertFileToString(fileName);
+    server->SendString(GmshSocket::GMSH_PARSE_STRING, options.c_str());
     server->SendString(GmshSocket::GMSH_VERTEX_ARRAY, "Send the vertex arrays!");
     return 1;
   }
-- 
GitLab