{
  Autor   = Michael Springwald
  Datum   = 30.04.2006
  Updatet = -
  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
}

unit uTool_Config;


interface


uses Forms,menus,StdCtrls,ExtCtrls,ComCtrls,spin,CheckLst,SysUtils,Classes,tools,Dialogs;

// Speichert die einstellungen eines fourmulars
procedure SaveConfig(f:TFileName);

// Setzt die Einstellungen eines formulas
procedure LoadConfig(f:TFileName);

// Speichert den inhalt eine ListView ab
function SaveListView(ListView:TListView; fFileName:TFileName;extra:String):TStrings;

implementation

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,fo,ci,k:Integer;
  c:TComponent;
  str,j:String;
begin
  s:=TStringList.Create;
  if FileExists(f) then begin
    s.LoadFromFile(f);
    for i:=0 to s.count-1 do begin
      fo:=StrToInt(GetToken(s.strings[i],'.',1)); // Form index holen
      for ci:=0 to Screen.Forms[fo].ComponentCount-1 do begin
        c:=Screen.Forms[fo].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;
  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:=intToStr(fo)+'.'+c.Name;
      if c is TMenuItem then begin
        if c.Tag = -3 then
          s.add(str+ '.Checkeck=' + BoolToStr(TMenuItem(c).Checked,True) );
      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,true))
      end; // TCheckbox

      if c is TRadioButton then begin
        s.Add(str + '.Checked=' + BoolToStr(TRadioButton(c).checked,true))
      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,True)
          else
            l:=l+'|'+TToolBar(c).Buttons[k].Name+','+ BoolToStr(TToolBar(c).Buttons[k].Down,True)
        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.SaveToFile(f + '.tmp');
  if FileExists(f) then DeleteFile(PChar(f));
  RenameFile(f+'.tmp',f);
end;
end.
