{
  Autor   = Michael Springwald
  Datum   = 30.04.2006
  Updates = Mittwoch, 25.Juli.2007, Freitag, 31.Augst.2007

  Beschreibung: Speichert folgende Komponenten ab
  - TMenuItem, TEdit, TCheckBox, TRadioButton, TRadioButton, TListBox, TComboBox,
    TPageControl, TTrackBar, TSpinEdit, TToolBar, TCheckListBox
  wenn ein TMenuItem abgespeichert werden soll muss die Tag eigenschaft auf -3 gesetzt werden
  das gleiche gilt für ComboBox, ListBox sonst wird nur der ItemIndex abgespeichert
  und nicht die Items selbst.

  Wenn beim laden nicht die zulesetzt abgespeicherte seite eines
  Pagescontrols geladen werden soll muss die Tag eigenschaft auf -3 gesetzt
  werden.

  Zuerst wird eine Stringlist gespeichert und dann wird die orginal datei gelöscht und
  dann wird die gepspeicherte Datei umbenannt.

  Weil was ist wenn beim löschen der Datei, das programm abstürzt und die config dann
  nochmal erneuert werden muss.

  Status: zu 90% fertig nur noch die ListView muss eingebunden werden
  C0 = items|0
  C1 = Items|0
  C2 = Items|0
  Speicher und Lade Procedure der ListView muss noch reingeschrieben
  werden und von der TreeView
}

unit uTool_Config;


interface


uses Forms,menus,StdCtrls,ExtCtrls,ComCtrls,spin,CheckLst,SysUtils,Classes,Tools,Dialogs;
type
  TConfigArray = record
    Key,Value:String;
  end;
  
// Speichert die einstellungen eines fourmulars
procedure SaveConfig(f:TFileName);

// Setzt die Einstellungen eines formulas
procedure LoadConfig(f:TFileName);

// FÃ¼g dem ConfigArray neue eintrÃ¤ge zu
procedure AddConfigItem(const Key, Value:String);
// Speichert den inhalt eine ListView ab
function SaveListView(ListView:TListView; fFileName:TFileName;extra:String):TStrings;

// Sucht in im Array ob es den SchlÃ¼ssel schon gibt
function CheckConfigItem(const key:String):Integer;

var
  ConfigArray:array of TConfigArray;

implementation

function CheckConfigItem(const key:String):Integer;
var
  i,z:Integer;
begin
  z:=-1;
  for i:=0 to High(ConfigArray) do begin
    if ConfigArray[i].Key = key then begin
      z:=i;
      break;
    end;
  end;
  result:=z;
end;

procedure AddConfigItem(const Key,Value: String);
var
  z:Integer;
begin
  z:=CheckConfigItem(key);
  if z = -1 then begin
    SetLength(ConfigArray,high(ConfigArray)+2);
    ConfigArray[high(ConfigArray)].Key:=Key;
    ConfigArray[high(ConfigArray)].Value:=Value;
  end
  else
    ConfigArray[z].Value:=Value;
end;

function SaveListView(ListView:TListView; fFileName:TFileName;extra:String):TStrings;
var
  i, // i forschleife bei den alle Zeilen, einer ListView durchgegangen werden
  c:Integer; // c für die forschleife bei den ale Spalten durchgegangen werden
  s:TStringList;
  str:String;
begin
  s:=TStringList.Create;
  for i:=0 to ListView.Items.Count-1 do begin
    str:='=' ;
    for c:=0 to ListView.Columns.Count-1 do begin
//      ListView.Columns.Items[c].Caption.;
    end; // for c
    str:=str+ ',' + ListView.Items[i].Caption;
    s.Add(extra +str)
  end; // for i
  result:=s;
end;

procedure LoadConfig(f:TFileName);
var
  s:TStringList;
  i,ci,k:Integer;
  c:TComponent;
  str,j,fo:String;
  af:TCustomForm;
  ok:Boolean;
begin
  s:=TStringList.Create;
  if FileExists(f) then begin
    s.LoadFromFile(f);
    ok:=True;
    for i:=0 to s.count-1 do begin
      if (s.strings[i] <> 'ConfigArray') and (OK) and (pos('.',s.strings[i]) > 0) then begin
        fo:=GetToken(s.strings[i],'.',1); // Form index holen
        af:=Screen.FindForm(fo);
        for ci:=0 to af.ComponentCount-1 do begin
          c:=af.Components[ci];
          str:=GetToken(s.strings[i],'=',2);
          if c.Name = GetToken(s.strings[i],'.',2) then begin
            if c is TMenuItem then begin
              TMenuItem(c).Checked:=StrToBool(str);
            end; // TMenuItem

            if c is TCustomEdit then begin
              TCustomEdit(c).Text:=str;
            end; // TEdit

            if c is TCheckbox then begin
              TCheckbox(c).Checked:=StrToBool(str);
            end; // TCheckBox

            if c is TRadioButton then begin
              TRadioButton(c).Checked:=StrToBool(str);
            end; // TRadioButton

            if c is TRadioGroup then begin
              TRadioGroup(c).ItemIndex:=StrToInt(str);
            end; // TRadiogrupbox

            if c is TListBox then begin
              TListBox(c).items.Clear;
              TListBox(c).Items:=StrToTSTrings(str);
              if pos(c.name,s.strings[i-1]) > 0 then
                TListBox(c).ItemIndex:=StrToInt(GetToken(s.strings[i-1],'=',2));
            end; // TListBox

            if c is TComboBox then begin
              if (c.Tag <> -3) and (pos('items',s.strings[i]) >0) then begin
                TComboBox(c).items.Clear;
                TComboBox(c).Items:=StrToTSTrings(str);
              end;

              if pos('itemIndex',s.strings[i]) > 0 then begin
                TComboBox(c).ItemIndex:=StrToInt(GetToken(s.strings[i],'=',2));
              end;
            end; // TComboBox

            if (c is TPageControl) and (c.tag = -3) then begin
              TPageControl(c).ActivePageIndex:=StrToInt(str);
            end; // TPageControl

            if c is TTrackBar then begin
              TTrackBar(c).Position:=strToInt(str);
            end; // TTrackBar

            if c is TSpinEdit then begin
              TSpinEdit(c).Value:=strToInt(str);
            end; // TSpinEdit

  {       if c is TToolBar then begin
            for k:=1 to NumToken(str,'|') do begin
              j:=GetToken(str,'|',k);
              TToolbar(c).Buttons[k-1].Down:=StrToBool(GetToken(j,',',2));
            end;
          end; // TToolBar}

            if c is TCheckListBox then begin
              for k:=1 to NumToken(str,'|') do begin
                TCheckListBox(c).Checked[k-1]:=StrToBool(GetToken(str,'|',k))
              end;
            end; // TCheckListBox
          end; // c = strings
        end;
      end // if str <> 'ConfigArray'
      else begin
        ok:=False;
        if pos('=',s.Strings[i]) > 0 then begin
          AddConfigItem(GetToken(s.Strings[i],'=',1),GetToken(s.Strings[i],'=',2));
        end;
      end;
    end;
  end
  else begin
    ShowMessage('Die angeben Konfiguations Datei ist nicht vorhanden ! ' + #13 + f);
  end;
end;
  
procedure SaveConfig(f:TFileName);
var
  s:TStringList;
  i,fo,k:Integer;
  c:TComponent;
  str,l:String;
begin
  s:=TStringList.Create;
  for fo:=0 to Screen.FormCount-1 do begin
    for i:=0 to Screen.Forms[fo].ComponentCount-1 do begin
      c:=Screen.Forms[fo].Components[i];
      str:=Screen.Forms[fo].Name+'.'+c.Name;
      if c is TMenuItem then begin
        if c.Tag = -3 then
          s.add(str+ '.Checkeck=' + BoolToStr(TMenuItem(c).Checked) );
      end; // TMenuItem

      if c is TEdit then begin
        s.Add(str + '.Text=' + TEdit(c).Text);   
      end; // TEdit

      if c is TCustomCheckBox then begin
        s.Add(str + '.Checked=' + BoolToStr(TCheckBox(c).Checked))
      end; // TCheckbox

      if c is TRadioButton then begin
        s.Add(str + '.Checked=' + BoolToStr(TRadioButton(c).checked))
      end; // TRadioButton

      if c is TRadioGroup then begin
        s.Add(str + '.itemIndex=' + IntTostr(TRadioGroup(c).ItemIndex))
      end; // TRadioGroup

      if c is TCustomListBox then begin
        if TCustomListBox(c).ItemIndex > -1 then
          s.Add(str + '.itemIndex=' + IntTostr(TCustomListBox(c).ItemIndex))
        else
          s.Add(str + '.itemIndex=0');

        if c.Tag = -3 then
          s.Add(str + '.items=' + TStringsToStr(TCustomListBox(c).Items))
      end; // TListBox

      if c is TCustomComboBox then begin
        if TComboBox(c).ItemIndex > -1 then
          s.Add(str + '.itemIndex=' + IntTostr(TComboBox(c).ItemIndex))
        else
          s.Add(str + '.itemIndex=0');

        if c.Tag = -3 then
          s.Add(str + '.items=' + TStringsToStr(TComboBox(c).Items))
      end; // TComboBox
      
      if c is TPageControl then begin
        s.add(str + '.ItemIndex=' +IntToStr(TPageControl(c).ActivePageIndex));
      end; // TPageControl

      if c is TTrackBar then begin
        s.add(str + '.Position=' + intTostr(TTrackBar(c).Position));
      end; // TTrackBar

      if c is TSpinEdit then begin
        s.add(str + '.Value=' + intTostr(TSpinEdit(c).Value));
      end; // TSpinEdit

     { if c is TToolBar then begin
        for k:=0 to TToolBar(c).ButtonCount -1 do begin
          if k = 0 then
            l:=TToolBar(c).Buttons[k].Name+','+ BoolToStr(TToolBar(c).Buttons[k].Down)
          else
            l:=l+'|'+TToolBar(c).Buttons[k].Name+','+ BoolToStr(TToolBar(c).Buttons[k].Down)
        end;
        s.add(str + '.Down=' +l);
      end; // TToolBar }

      if c is TCheckListBox then begin
        for k:=0 to TChecklistbox(c).Count-1 do begin
          if k = 0 then
            l:=BoolToStr(TCheckListBox(c).Checked[k])
          else
            l:=l+'|'+BoolToStr(TCheckListBox(c).Checked[k])
        end;
        s.add(str + '.checked=' + l);
      end; // TCheckListBox

//      if c is TListView then begin
//        s.Add(SaveListView(TListView(c),f,str).Text);
//      end;
    end;
  end;
  s.add('ConfigArray');
  for i:=0 to High(ConfigArray) do begin
    Writeln(i,' Key:',ConfigArray[i].Key);;
    s.add(ConfigArray[i].Key + '=' + ConfigArray[i].Value);
  end;
  
  s.SaveToFile(f + '.tmp');
  if FileExists(f) then DeleteFile(PChar(f));
  RenameFile(f+'.tmp',f);
end;


end.
