using System;
using System.Collections.Generic;
using System.Text;

//ttttttt

// hhhh
// fffffff
# gggg
// neu
using Microsoft.Win32;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Test_Syntaxhighlight {
public class Basis {

public static SolidColorBrush getColor(long color) {
int red = (int)((color >> 16) & 0xFF);
int green = (int)((color >> 8) & 0xFF);
int blue = (int)(color & 0xFF);
return new SolidColorBrush(Color.FromArgb((byte)255, (byte)red, (byte)green, (byte)blue));
}



public static void setWindowPositionFromRegistry(Window dialog, String regadr, String title) {
// 1:  ein Bildschirm nur PrimaryScreenWidth/PrimaryScreenHeight
// 2:  zwei Bildschirme  VirtualScreenWidth  0 bis Width(1)+Width(2)
// 3:  zwei Bildschirme  VirtualScreenWidth mit 0 bis Width(1)+Width(2)
//             width ist aber   -Width(2) bis Width(1)
int iTop = Basis.ReadRegInt(regadr, title + "_Top1");
int iLeft = Basis.ReadRegInt(regadr, title + "_Left1");
int iWidth = Basis.ReadRegInt(regadr, title + "_Width1");
int iHeight = Basis.ReadRegInt(regadr, title + "_Height1");
int w = (int)System.Windows.SystemParameters.PrimaryScreenWidth;
int h = (int)System.Windows.SystemParameters.PrimaryScreenHeight;

int virtualLeft = (int)System.Windows.SystemParameters.VirtualScreenLeft;
int virtualW = (int)System.Windows.SystemParameters.VirtualScreenWidth;  // beide Breiten zusammen positiv
int virtualH = (int)System.Windows.SystemParameters.VirtualScreenHeight;
// iLeft kann negativ sein   und zwar  -SecondWidth
if (iTop > 0 && (iTop - (iHeight >> 1) <= virtualH) && (iLeft >= virtualLeft) && iWidth > 50) {
dialog.Top = iTop;
dialog.Left = iLeft;
dialog.Width = iWidth;
dialog.Height = iHeight;
}
else {
dialog.Top = 100;
dialog.Left = 20;
}
}  // setWindowPositionFromRegistry

public static void writeWindowPosition2Registry(Window dialog, String regadr, String title) {
Basis.WriteRegInt(regadr, title + "_Top1", (int)dialog.Top);
Basis.WriteRegInt(regadr, title + "_Left1", (int)dialog.Left);
Basis.WriteRegInt(regadr, title + "_Width1", (int)dialog.Width);
Basis.WriteRegInt(regadr, title + "_Height1", (int)dialog.Height);
}  // writeWindowPosition2Registry


public static MenuItem createMenuItem2(String name, String title) {
MenuItem mn = new MenuItem();
mn.Name = name;
mn.Header = title;
return mn;
}


public static void Message(String text, String title) {
MessageBox.Show(text, title, MessageBoxButton.OK, MessageBoxImage.Information);
}

public static void ErrorMsg(String text, String title) {
MessageBox.Show(text, title, MessageBoxButton.OK, MessageBoxImage.Error);
}


public static bool CheckDateinamen(String filename) {
if (filename.Length == 0)
return false;
else {
bool result = true;
for (int i = 0; i < filename.Length; i++) {
String ch = filename.Substring(i, 1);
switch (ch) {
case "\\":
result = false;
break;
case "/":
result = false;
break;
case ":":
result = false;
break;
case "?":
result = false;
break;
case "*":
result = false;
break;
case "\"":
result = false;
break;
case "<":
result = false;
break;
case ">":
result = false;
break;
case "|":
result = false;
break;
}// case
}// for
return result;
}// if
}  //  pruefe_dateinamen 


/// <summary>
/// return the line of the editor
/// </summary>
/// <param name="row"></param>
/// <param name="text"></param>
/// <returns></returns>
public static String getLine(int row, String text) {
StringBuilder sb = new StringBuilder();
string[] splitStrings = { "\r\n" };
string[] lines = text.Split(splitStrings, StringSplitOptions.None);
if (row > 0 && row <= lines.Length) {
return lines[row - 1];
}
else {
return "Fehler in Modul getLine";
}
}

public static String[] getLines(String text) {
StringBuilder sb = new StringBuilder();
string[] splitStrings = { "\r\n" };  // { "\r", "\r\n", "\n" };
return text.Split(splitStrings, StringSplitOptions.None);
}

public static String reverseStringWord(String item) {
String item2 = "";
int rest = item.Length % 2;
if (rest == 1) {
ErrorMsg("Die Zahl hat eine ungerade Länge: " + item, "Fehler");
item2 = item;
}
else {
for (int i = 0; i < item.Length; i += 2) {
item2 = item.Substring(i, 2) + item2;
}
}

return item2;
}

/// <summary>
/// return the linenumber of the text, text from 0 to index
/// </summary>
/// <param name="rowindex"></param>
/// <param name="text"></param>
/// <returns></returns>
public static int getLinenr(int rowindex, String text) {
StringBuilder sb = new StringBuilder();
string[] splitStrings = { "\r\n" };  // { "\r", "\r\n", "\n" };
string[] lines = text.Split(splitStrings, StringSplitOptions.None);
int rowChars = 0;
for (int i = 0; i < lines.Length - 1; i++) {
String sLine = lines[i];
if (rowindex < rowChars) {
return i;
}
rowChars += lines[i].Length + 2;
}
return lines.Length;
}

/// <summary>
/// return only the name and the extention, test.txt
/// </summary>
/// <param name="sPathFilename"></param>
/// <returns></returns>
public static String getFilename(String sPathFilename) {
int k = sPathFilename.LastIndexOf('\\');
if (k >= 0) {
return sPathFilename.Substring(k + 1);
}
else {
return sPathFilename;
}
}  // getFilename

/// <summary>
/// return only the extention, sample: test.TXT return txt
/// </summary>
/// <param name="sPathFilename"></param>
/// <returns></returns>
public static String getFilenameExt(String sPathFilename) {
int k = sPathFilename.LastIndexOf('.');
if (k >= 0) {
return sPathFilename.Substring(k + 1).ToLower();
}
else {
return "";
}
}  // getFilename


public static void splitFilename(String pathFilename, out String path, out String filename) {
int i = pathFilename.LastIndexOf("\\");
if (i >= 0) {
path = pathFilename.Substring(0, i + 1);
filename = pathFilename.Substring(i + 1);
}
else {
filename = pathFilename;
path = "";
}
}


/// <summary>
/// split a filename into path, name and extention ( C:\\daten\\,  test1,  .asm )
/// </summary>
/// <param name="sPathFilename"></param>
/// <param name="path"></param>
/// <param name="name"></param>
/// <param name="ext"></param>
/// <returns></returns>
public static void splitFilename(String sPathFilename, out String path, out String name, out String ext) {
String filename = sPathFilename;
int k = filename.LastIndexOf('\\');
if (k >= 0) {
path = filename.Substring(0, k + 1);
filename = filename.Substring(k + 1);
}
else {
path = "";
}
k = filename.LastIndexOf('.');
if (k >= 0) {
name = filename.Substring(0, k);
ext = filename.Substring(k);  // .asm
}
else {
name = filename;
ext = "";
}
}  // splitFilename

/// <summary>
/// liefert den Programmpfad mit Backslash, z. B. fuer das Erstellen von Ordnern
/// </summary>
/// <returns></returns>
public static String getProgPath() {
string[] args = System.Environment.GetCommandLineArgs();
String exepath = args[0];
int i = exepath.LastIndexOf("\\");
String path = exepath.Substring(0, i + 1);
if (!path.EndsWith("\\"))
path += "\\";
return path;
}



/// <summary>
/// return the Path of a file
/// </summary>
/// <param name="sPathFilename"></param>
/// <returns></returns>
public static String getPath(String sPathFilename) {
int k = sPathFilename.LastIndexOf('\\');
if (k >= 0) {
return sPathFilename.Substring(0, k);
}
else {
return "";
}
}  // getPath

/// <summary>
/// return the last directory of a file
/// </summary>
/// <param name="sPath"></param>
/// <returns></returns>
public static String getLastPath(String sPath) {
if (sPath.Length <= 3) {
return sPath;
}

if (sPath.EndsWith("\\")) {
sPath = sPath.Substring(0, sPath.Length - 1);
}
int k = sPath.LastIndexOf('\\');
if (k >= 0) {
return sPath.Substring(k + 1, sPath.Length - k - 1);
}
else {
return "";
}
}  // getPath


/// <summary>
/// replace the extention from sPathFilename to sExt; c:\daten\1.xhtml , css ==> c:\c:\daten\1.css
/// </summary>
/// <param name="sPathFilename"></param>
/// <param name="sExt"></param>
/// <returns></returns>
public static String changeFilenameExt(String sPathFilename, string sExt) {
int k = sPathFilename.LastIndexOf('.');
if (k >= 0) {
return sPathFilename.Substring(0, k + 1) + sExt;  // k+1 fuer Punkt
}
else {
return sPathFilename;
}
}  // getFilename



/// <summary>
/// Prueft den Dateinamen auf fehlerhafte Zeichen
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
public static bool CheckFilename(String filename) {
if (filename.Length == 0)
return false;
else {
bool result = true;
for (int i = 0; i < filename.Length; i++) {
String ch = filename.Substring(i, 1);
switch (ch) {
case "\\":
result = false;
break;
case "/":
result = false;
break;
case ":":
result = false;
break;
case "?":
result = false;
break;
case "*":
result = false;
break;
case "\"":
result = false;
break;
case "<":
result = false;
break;
case ">":
result = false;
break;
case "|":
result = false;
break;
}// case
}// for
return result;
}// if
}  //  pruefe_dateinamen 

public static MessageBoxResult readYesNoCancel(string message, string title) {

return MessageBox.Show(message, title, MessageBoxButton.YesNoCancel, MessageBoxImage.Question);   // Exclamation
}

public static bool readYesNo(string message, string title) {
return MessageBox.Show(message, title, MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes;
}





public static String ReadRegString(string root, string KeyName) {
string sKey;
RegistryKey regist;
try {
regist = Registry.CurrentUser.OpenSubKey(root);
if (regist == null) {
//MessageBox.Show("Fehler", "Registry-Eintrag ist nicht vorhanden: " + KeyName.ToUpper());
return "";
}
else {
if (regist.GetValue(KeyName) == null)
sKey = "";
else {
sKey = regist.GetValue(KeyName).ToString();
}
return sKey;
}
}
catch (Exception e) {
// OOOOH, an Error
MessageBox.Show(e.ToString(), "Writing registry " + KeyName.ToUpper());
return "";
}
}  // readRegString


public static long ReadRegLong(string root, string KeyName) {
long lKey;
string sKey;
RegistryKey regist;
try {
regist = Registry.CurrentUser.OpenSubKey(root);
if (regist == null) {
//MessageBox.Show("Fehler", "Registry-Eintrag ist nicht vorhanden: " + KeyName.ToUpper());
return 0;
}
else {
if (regist.GetValue(KeyName) == null)
lKey = 0;
else {
sKey = regist.GetValue(KeyName).ToString();
lKey = (long)Convert.ToUInt64(sKey);
}
return lKey;
}
}
catch (Exception e) {
// OOOOH, an Error
MessageBox.Show(e.ToString(), "Reading registry long" + KeyName.ToUpper());
return 0;
}
}  // readRegLong


public static int ReadRegInt(string root, string KeyName) {
int lKey;
string sKey;
RegistryKey regist;
try {
regist = Registry.CurrentUser.OpenSubKey(root);
if (regist == null) {
//MessageBox.Show("Fehler", "Registry-Eintrag ist nicht vorhanden: " + KeyName.ToUpper());
return 0;
}
else {
if (regist.GetValue(KeyName) == null)
lKey = 0;
else {
sKey = regist.GetValue(KeyName).ToString();
lKey = (int)Convert.ToInt32(sKey);
//.ToUInt64(sKey);
}
return lKey;
}
}
catch (Exception e) {
// OOOOH, an Error
MessageBox.Show(e.ToString(), "Reading registry int" + KeyName.ToUpper());
return 0;
}
}  // readRegInt

public static ulong ReadRegULong(string root, string KeyName) {
string sKey;
ulong lKey = 0;
RegistryKey regist;
try {
regist = Registry.CurrentUser.OpenSubKey(root);
if (regist == null) {
//MessageBox.Show("Fehler", "Registry-Eintrag ist nicht vorhanden: " + KeyName.ToUpper());
return 0;
}
else {
if (regist.GetValue(KeyName) == null)
lKey = 0;
else {
sKey = regist.GetValue(KeyName).ToString();
lKey = Convert.ToUInt64(sKey);
}
return lKey;
}
}
catch (Exception e) {
// OOOOH, an Error
MessageBox.Show(e.ToString(), "Reading registry double" + KeyName.ToUpper());
return 0;
}
}  // readRegLong


public static double ReadRegDouble(string root, string KeyName) {
string sKey;
double dKey = 0;
RegistryKey regist;
try {
regist = Registry.CurrentUser.OpenSubKey(root);
if (regist == null) {
//MessageBox.Show("Fehler", "Registry-Eintrag ist nicht vorhanden: " + KeyName.ToUpper());
return 0.0;
}
else {
if (regist.GetValue(KeyName) == null)
dKey = 0.0;
else {
sKey = regist.GetValue(KeyName).ToString();
dKey = Convert.ToDouble(sKey);
}
return dKey;
}
}
catch (Exception e) {
// OOOOH, an Error
MessageBox.Show(e.ToString(), "Reading registry double" + KeyName.ToUpper());
return 0.0;
}
}  // readRegDouble




// WriteWriteWriteWriteWriteWriteWriteWriteWriteWriteWriteWrite


public static bool WriteRegString(string root, string KeyName, string Value) {
try {
RegistryKey rKey = Registry.CurrentUser.CreateSubKey(root);
if (rKey == null) {
//MessageBox.Show("Fehler", "Registry-Eintrag ist nicht vorhanden: " + KeyName.ToUpper());
return false;
}
else {
rKey.SetValue(KeyName, Value, RegistryValueKind.String);
return true;
}
}
catch (Exception e) {
// OOOOH, an Error
MessageBox.Show(e.ToString(), "Writing registry string" + KeyName.ToUpper());
return false;
}
}



public static bool WriteRegULong(string root, string KeyName, ulong Value) {
try {
RegistryKey rKey = Registry.CurrentUser.CreateSubKey(root);
if (rKey == null) {
//MessageBox.Show("Fehler", "Registry-Eintrag ist nicht vorhanden: " + KeyName.ToUpper());
return false;
}
else {
rKey.SetValue(KeyName, Value, RegistryValueKind.DWord);
return true;
}
}
catch (Exception e) {
// OOOOH, an Error
MessageBox.Show(e.ToString(), "Writing registry long" + KeyName.ToUpper());
return false;
}
}

// fuer long int etc
public static bool WriteRegInt(string root, string KeyName, int Value) {
try {
RegistryKey rKey = Registry.CurrentUser.CreateSubKey(root);
if (rKey == null) {
//MessageBox.Show("Fehler", "Registry-Eintrag ist nicht vorhanden: " + KeyName.ToUpper());
return false;
}
else {
rKey.SetValue(KeyName, Value, RegistryValueKind.DWord);
return true;
}
}
catch (Exception e) {
// OOOOH, an Error
MessageBox.Show(e.ToString(), "Writing registry int" + KeyName.ToUpper());
return false;
}
}

// fuer long int etc
public static bool WriteRegLong(string root, string KeyName, long Value) {
try {
RegistryKey rKey = Registry.CurrentUser.CreateSubKey(root);
if (rKey == null) {
//MessageBox.Show("Fehler", "Registry-Eintrag ist nicht vorhanden: " + KeyName.ToUpper());
return false;
}
else {
rKey.SetValue(KeyName, Value, RegistryValueKind.DWord);
return true;
}
}
catch (Exception e) {
// OOOOH, an Error
MessageBox.Show(e.ToString(), "Writing registry long" + KeyName.ToUpper());
return false;
}
}


public static bool WriteRegDouble(string root, string KeyName, double Value) {
try {
RegistryKey rKey = Registry.CurrentUser.CreateSubKey(root);
if (rKey == null) {
//MessageBox.Show("Fehler", "Registry-Eintrag ist nicht vorhanden: " + KeyName.ToUpper());
return false;
}
else {
rKey.SetValue(KeyName, Value.ToString(), RegistryValueKind.String);
return true;
}
}
catch (Exception e) {
// OOOOH, an Error
MessageBox.Show(e.ToString(), "Writing registry double" + KeyName.ToUpper());
return false;
}
}

public static bool ReadRegBool(string root, string KeyName) {
bool bKey;
RegistryKey regist;
try {
regist = Registry.CurrentUser.OpenSubKey(root);
if (regist == null) {
//MessageBox.Show("Fehler", "Registry-Eintrag ist nicht vorhanden: " + KeyName.ToUpper());
return false;
}
else {
if (regist.GetValue(KeyName) == null)
bKey = false;
else {
int value = (int)regist.GetValue(KeyName);
bKey = value == 1;
}
return bKey;
}
}
catch (Exception e) {
// OOOOH, an Error
MessageBox.Show(e.ToString(), "Reading registry bool" + KeyName.ToUpper());
return false;
}
}  // readRegBool

public static bool WriteRegBool(string root, string KeyName, bool Value) {
try {
RegistryKey rKey = Registry.CurrentUser.CreateSubKey(root);
if (rKey == null) {
//MessageBox.Show("Fehler", "Registry-Eintrag ist nicht vorhanden: " + KeyName.ToUpper());
return false;
}
else {
if (Value)
rKey.SetValue(KeyName, 1, RegistryValueKind.DWord);  // Bool geht nicht                    
else
rKey.SetValue(KeyName, 0, RegistryValueKind.DWord);  // Bool geht nicht                    
return true;
}
}
catch (Exception e) {
// OOOOH, an Error
MessageBox.Show(e.ToString(), "Writing registry bool" + KeyName.ToUpper());
return false;
}
}

}
}
