i18n: Update
[sdk] / ide / src / IDESettings.ec
index f3ec35e..d542dab 100644 (file)
@@ -1,7 +1,7 @@
 #ifdef ECERE_STATIC
-import static "ecere"
+public import static "ecere"
 #else
-import "ecere"
+public import "ecere"
 #endif
 
 import "StringsBox"
@@ -16,13 +16,38 @@ define defaultCompilerName = "Default";
 
 define defaultObjDirExpression = "obj/$(CONFIG).$(PLATFORM)$(COMPILER_SUFFIX)$(DEBUG_SUFFIX)";
 
-char * settingsDirectoryNames[DirTypes] = 
+char * settingsDirectoryNames[DirTypes] =
 {
    "Include Files",
    "Library Files",
    "Executable Files"
 };
 
+// This function cannot accept same pointer for source and output
+// todo: rename ReplaceSpaces to EscapeSpaceAndSpecialChars or something
+void ReplaceSpaces(char * output, char * source)
+{
+   int c, dc;
+   char ch, pch = 0;
+
+   for(c = 0, dc = 0; (ch = source[c]); c++, dc++)
+   {
+      if(ch == ' ') output[dc++] = '\\';
+      if(ch == '\"') output[dc++] = '\\';
+      if(ch == '&') output[dc++] = '\\';
+      if(pch != '$')
+      {
+         if(ch == '(' || ch == ')') output[dc++] = '\\';
+         pch = ch;
+      }
+      else if(ch == ')')
+         pch = 0;
+      output[dc] = ch;
+   }
+   output[dc] = '\0';
+}
+
+
 enum GlobalSettingsChange { none, editorSettings, projectOptions, compilerSettings };
 
 enum PathRelationship { unrelated, identical, siblings, subPath, parentPath, insuficientInput, pathEmpty, toEmpty, pathNull, toNull, bothEmpty, bothNull };
@@ -84,7 +109,7 @@ char * CopyValidateMakefilePath(char * path)
          char * tmp = copy;
          char * start = tmp;
          Array<char *> parts { };
-         
+
          for(c=0; c<len; c++)
          {
             if(tmp[c] == '$')
@@ -125,6 +150,50 @@ char * CopyValidateMakefilePath(char * path)
    return copy;
 }
 
+void ValidPathBufCopy(char *output, char *input)
+{
+#ifdef __WIN32__
+   bool volumePath = false;
+#endif
+   strcpy(output, input);
+   TrimLSpaces(output, output);
+   TrimRSpaces(output, output);
+   MakeSystemPath(output);
+#ifdef __WIN32__
+   if(output[0] && output[1] == ':')
+   {
+      output[1] = '_';
+      volumePath = true;
+   }
+#endif
+   {
+      char * chars = "*|:\",<>?";
+      char ch, * s = output, * o = output;
+      while((ch = *s++)) { if(!strchr(chars, ch)) *o++ = ch; }
+      *o = '\0';
+   }
+#ifdef __WIN32__
+   if(volumePath && output[0])
+      output[1] = ':';
+#endif
+}
+
+void RemoveTrailingPathSeparator(char *path)
+{
+   int len;
+   len = strlen(path);
+   if(len>1 && path[len-1] == DIR_SEP)
+      path[--len] = '\0';
+}
+
+void BasicValidatePathBoxPath(PathBox pathBox)
+{
+   char path[MAX_LOCATION];
+   ValidPathBufCopy(path, pathBox.path);
+   RemoveTrailingPathSeparator(path);
+   pathBox.path = path;
+}
+
 CompilerConfig MakeDefaultCompiler(char * name, bool readOnly)
 {
    CompilerConfig defaultCompiler
@@ -160,6 +229,8 @@ class IDESettingsContainer : GlobalSettings
    char moduleLocation[MAX_LOCATION];
 
 private:
+   FileSize settingsFileSize;
+
    IDESettingsContainer()
    {
       char path[MAX_LOCATION];
@@ -184,7 +255,7 @@ private:
          PathCat(defaultConfigFilePath, "App");
          PathCat(defaultConfigFilePath, "DefaultData");
          PathCat(defaultConfigFilePath, "ecereIDE.ini");
-         
+
          if(FileExists(defaultConfigFilePath))
          {
             if(!FileExists(configFilePath))
@@ -196,9 +267,9 @@ private:
             }
             PathCat(path, "Data");
             // the forced settings location will only be
-            // used if the running ide's path matches 
+            // used if the running ide's path matches
             // the PortableApps.com directory structure
-            // and the default ini file is found in 
+            // and the default ini file is found in
             // the DefaultData directory
             settingsLocation = path;
             portable = true;
@@ -208,12 +279,31 @@ private:
 
    void OnAskReloadSettings()
    {
-      /*if(MessageBox { type = YesNo, master = this, 
-            text = "Global Settings Modified Externally", 
+      /*if(MessageBox { type = YesNo, master = this,
+            text = "Global Settings Modified Externally",
             contents = "The global settings were modified by another instance.\n"
             "Would you like to reload them?" }.Modal() == Yes)*/
       {
-         Load();
+         double o, n;
+         FileSize newSettingsFileSize;
+         FileGetSize(settingsFilePath, &newSettingsFileSize);
+         o = settingsFileSize;
+         n = newSettingsFileSize;
+         if(o - n < 2048)
+            Load();
+         else
+         {
+            GuiApplication app = ((GuiApplication)__thisModule.application);
+            Window w;
+            for(w = app.desktop.firstChild; w && (!w.created || !w.visible); w = w.next);
+            MessageBox { master = w, type = ok,
+                  text = "Global Settings Modified Externally",
+                  contents = "The global settings were modified by another process and a drastic shrinking of the settings file was detected.\n"
+                  "The new settings will not be loaded to prevent loss of your ide settings.\n"
+                  "Please check your settings file and make sure to save this IDE's global settings if your settings file has been compromised."
+                  }.Create();
+            //Save();
+         }
       }
    }
 
@@ -254,7 +344,7 @@ private:
                delete data.displayDriver; data.displayDriver = CopyString(oldSettings.displayDriver);
                data.projectDefaultTargetDir = oldSettings.projectDefaultTargetDir;
                data.projectDefaultIntermediateObjDir = oldSettings.projectDefaultIntermediateObjDir;
-                        
+
                Save();
                result = success;
             }
@@ -263,9 +353,9 @@ private:
          if(result == fileNotFound || !data)
          {
             data = (IDESettings)this.data;
-            data.useFreeCaret = true;
+            data.useFreeCaret = false; //true;
             data.showLineNumbers = true;
-            data.caretFollowsScrolling = true;
+            data.caretFollowsScrolling = false; //true;
          }
       }
       // Ensure we have a default compiler
@@ -280,6 +370,7 @@ private:
       defaultCompiler._refCount = 0;
 
       CloseAndMonitor();
+      FileGetSize(settingsFilePath, &settingsFileSize);
       if(data.compilerConfigs)
       {
          for(c : data.compilerConfigs)
@@ -313,6 +404,7 @@ private:
       if(portable && moduleLocation[0] && FileExists(moduleLocation).isDirectory)
          data.ManagePortablePaths(moduleLocation, true);
       CloseAndMonitor();
+      FileGetSize(settingsFilePath, &settingsFileSize);
       return result;
    }
 }
@@ -345,7 +437,7 @@ public:
    bool showLineNumbers;
    bool caretFollowsScrolling;
    char * displayDriver;
-   
+
    // TODO: Classify settings
    //EditorSettings editor { };
 
@@ -376,6 +468,17 @@ public:
       isset { return defaultCompiler && defaultCompiler[0]; }
    }
 
+   property String language
+   {
+      set
+      {
+         delete language;
+         language = CopyString(value);
+      }
+      get { return language; }
+      isset { return language != null; }
+   }
+
 private:
    char * docDir;
    char * ideFileDialogLocation;
@@ -384,6 +487,7 @@ private:
    char * projectDefaultIntermediateObjDir;
    char * compilerConfigsDir;
    char * defaultCompiler;
+   String language;
 
    CompilerConfig GetCompilerConfig(String compilerName)
    {
@@ -413,7 +517,7 @@ private:
       recentProjects.Free();
       delete recentProjects;
       delete docDir;
-   
+
       delete projectDefaultTargetDir;
       delete projectDefaultIntermediateObjDir;
       delete compilerConfigsDir;
@@ -651,7 +755,7 @@ public enum CompilerType
    {
       get { return OnGetString(null, null, null); }
       set
-      {  
+      {
          if(value)
          {
             Platform c;
@@ -766,6 +870,7 @@ public:
       get { return executableLauncher; }
       isset { return executableLauncher && executableLauncher[0]; }
    }
+   // TODO: implement CompilerConfig::windresCommand
    bool ccacheEnabled;
    bool distccEnabled;
    // deprecated
@@ -873,6 +978,34 @@ public:
       get { return excludeLibs; }
       isset { return excludeLibs.count != 0; }
    }
+   property Array<String> eCcompilerFlags
+   {
+      set
+      {
+         eCcompilerFlags.Free();
+         if(value)
+         {
+            delete eCcompilerFlags;
+            eCcompilerFlags = value;
+         }
+      }
+      get { return eCcompilerFlags; }
+      isset { return eCcompilerFlags.count != 0; }
+   }
+   property Array<String> compilerFlags
+   {
+      set
+      {
+         compilerFlags.Free();
+         if(value)
+         {
+            delete compilerFlags;
+            compilerFlags = value;
+         }
+      }
+      get { return compilerFlags; }
+      isset { return compilerFlags.count != 0; }
+   }
    property Array<String> linkerFlags
    {
       set
@@ -896,6 +1029,8 @@ private:
    Array<NamedString> environmentVars { };
    Array<String> prepDirectives { };
    Array<String> excludeLibs { };
+   Array<String> eCcompilerFlags { };
+   Array<String> compilerFlags { };
    Array<String> linkerFlags { };
    char * name;
    char * makeCommand;
@@ -937,6 +1072,8 @@ private:
       if(executableDirs) { executableDirs.Free(); }
       if(prepDirectives) { prepDirectives.Free(); }
       if(excludeLibs) { excludeLibs.Free(); }
+      if(compilerFlags) { compilerFlags.Free(); }
+      if(eCcompilerFlags) { eCcompilerFlags.Free(); }
       if(linkerFlags) { linkerFlags.Free(); }
    }
    CompilerConfig Copy()
@@ -970,9 +1107,212 @@ private:
       for(ns : environmentVars) copy.environmentVars.Add(NamedString { name = ns.name, string = ns.string });
       for(s : prepDirectives) copy.prepDirectives.Add(CopyString(s));
       for(s : excludeLibs) copy.excludeLibs.Add(CopyString(s));
+      for(s : compilerFlags) copy.compilerFlags.Add(CopyString(s));
+      for(s : eCcompilerFlags) copy.eCcompilerFlags.Add(CopyString(s));
       for(s : linkerFlags) copy.linkerFlags.Add(CopyString(s));
 
       incref copy;
       return copy;
    }
 }
+
+struct LanguageOption
+{
+   String name;
+   String bitmap;
+   String code;
+   BitmapResource res;
+
+   char * OnGetString(char * tempString, void * fieldData, bool * needClass)
+   {
+      return name;
+   }
+
+   void OnDisplay(Surface surface, int x, int y, int width, void * data, Alignment alignment, DataDisplayFlags flags)
+   {
+      Bitmap icon = res ? res.bitmap : null;
+      int w = 8 + 16;
+      if(icon)
+         surface.Blit(icon, x + (16 - icon.width) / 2,y+2,0,0, icon.width, icon.height);
+      class::OnDisplay(surface, x + w, y, width - w, null, alignment, flags);
+   }
+};
+
+Array<LanguageOption> languages
+{ [
+   { "English",            ":countryCode/gb.png", "" },
+   { "汉语",                ":countryCode/cn.png", "zh_CN" },
+   { "Español",            ":countryCode/es.png", "es" },
+   { "Русский (43%)",      ":countryCode/ru.png", "ru" },
+   { "Português (28%)",    ":countryCode/pt.png", "pt_BR" },
+   { "Nederlandse (13%)",  ":countryCode/nl.png", "nl" },
+   { "Tiếng Việt (12%)",   ":countryCode/vn.png", "vi" },
+   { "मराठी (10%)",          ":countryCode/in.png", "mr" },
+   { "Hebrew (8%)",        ":countryCode/il.png", "he" },
+   { "Magyar (8%)",        ":countryCode/hu.png", "hu" }
+] };
+
+String GetLanguageString()
+{
+   String language = getenv("LANGUAGE");
+   if(!language) language = getenv("LC_ALL");
+   if(!language) language = getenv("LC_MESSAGES");
+   if(!language) language = getenv("LANG");
+   if(!language) language = "";
+   return language;
+}
+
+bool LanguageRestart(char * code, Application app, IDESettings settings, IDESettingsContainer settingsContainer, Window ide, Window projectView, bool wait)
+{
+   bool restart = true;
+   String command = null;
+   int arg0Len = strlen(app.argv[0]);
+   int len = arg0Len;
+   int j;
+   char ch;
+
+   if(ide)
+   {
+      Window w;
+
+      if(projectView)
+      {
+         Window w;
+         for(w = ide.firstChild; w; w = w.next)
+         {
+            if(w.isActiveClient && w.isDocument)
+            {
+               if(!w.CloseConfirmation(true))
+               {
+                  restart = false;
+                  break;
+               }
+            }
+         }
+         if(restart)
+         {
+            if(!projectView.CloseConfirmation(true))
+               restart = false;
+            if(projectView.fileName)
+            {
+               char * name = projectView.fileName;
+               if(name)
+               {
+                  for(j = 0; (ch = name[j]); j++)
+                     len += (ch == ' ' || ch == '\"' || ch == '&' || ch == '$' || ch == '(' || ch == ')') ? 2 : 1;
+               }
+            }
+
+            command = new char[len + 1];
+
+            strcpy(command, app.argv[0]);
+            len = arg0Len;
+            if(projectView.fileName)
+            {
+               strcat(command, " ");
+               len++;
+               ReplaceSpaces(command + len, projectView.fileName);
+            }
+         }
+         if(restart)
+         {
+            for(w = ide.firstChild; w; w = w.next)
+               if(w.isActiveClient && w.isDocument)
+                  w.modifiedDocument = false;
+            projectView.modifiedDocument = false;
+         }
+      }
+      else
+      {
+         for(w = ide.firstChild; w; w = w.next)
+         {
+            if(w.isActiveClient && w.isDocument)
+            {
+               if(!w.CloseConfirmation(true))
+               {
+                  restart = false;
+                  break;
+               }
+               if(w.fileName)
+               {
+                  char * name = w.fileName;
+                  len++;
+                  for(j = 0; (ch = name[j]); j++)
+                     len += (ch == ' ' || ch == '\"' || ch == '&' || ch == '$' || ch == '(' || ch == ')') ? 2 : 1;
+               }
+            }
+         }
+
+         if(restart)
+         {
+            command = new char[len + 1];
+            strcpy(command, app.argv[0]);
+            len = arg0Len;
+
+            for(w = ide.firstChild; w; w = w.next)
+            {
+               if(w.isActiveClient && w.isDocument)
+               {
+                  char * name = w.fileName;
+                  if(name)
+                  {
+                     strcat(command, " ");
+                     len++;
+                     ReplaceSpaces(command + len, name);
+                     len = strlen(command);
+                  }
+               }
+            }
+         }
+         if(restart)
+         {
+            for(w = ide.firstChild; w; w = w.next)
+               if(w.isActiveClient && w.isDocument)
+                  w.modifiedDocument = false;
+         }
+      }
+      if(restart)
+      {
+         settings.language = code;
+         settingsContainer.Save();
+         if(eClass_IsDerived(app._class, class(GuiApplication)))
+         {
+            GuiApplication guiApp = (GuiApplication)app;
+            guiApp.desktop.Destroy(0);
+         }
+      }
+   }
+   else
+   {
+      int i;
+      for(i = 1; i < app.argc; i++)
+      {
+         char * arg = app.argv[i];
+         len++;
+         for(j = 0; (ch = arg[j]); j++)
+            len += (ch == ' ' || ch == '\"' || ch == '&' || ch == '$' || ch == '(' || ch == ')') ? 2 : 1;
+      }
+
+      command = new char[len + 1];
+      strcpy(command, app.argv[0]);
+      len = arg0Len;
+      for(i = 1; i < app.argc; i++)
+      {
+         strcat(command, " ");
+         len++;
+         ReplaceSpaces(command + len, app.argv[i]);
+         len = strlen(command);
+      }
+   }
+
+   if(restart)
+   {
+      SetEnvironment("LANGUAGE", code);
+      if(wait)
+         ExecuteWait(command);
+      else
+         Execute(command);
+   }
+   delete command;
+   return restart;
+}