File: kasbarfiles.pas

package info (click to toggle)
doublecmd 0.7.7-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 32,764 kB
  • ctags: 34,016
  • sloc: pascal: 273,752; ansic: 5,938; sh: 733; makefile: 194; python: 116; xml: 107
file content (328 lines) | stat: -rw-r--r-- 11,108 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
{
   File name: kasbarfiles.pas

   Author:    Dmitry Kolomiets (B4rr4cuda@rambler.ru)
   Class working with *.bar files.

   Based on KASToolBar functions
   Copyright (C) 2006-2007  Koblov Alexander (Alexx2000@mail.ru)
    
   contributors:



   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   in a file called COPYING along with this program; if not, write to
   the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
   02139, USA.
}

unit KASBarFiles;


{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils,IniFiles;

type

  //Button property's type
  //------------------------------------------------------
    TInfor=(ButtonX,
            CmdX,
            ParamX,
            PathX,
            MenuX,
            IconicX,
            MiskX
           );
  //------------------------------------------------------

 //Class of button
 //---------------------------------
 TKButton=class
          ButtonX:string; //Icon
          CmdX:string;    //Command or path
          ParamX:string;  //parameters
          PathX:string;
          MenuX:string;   //Description
          IconicX:Integer; //-1 0 1 full default minimized ( as TC use)
          MiskX:string; //Aditional info (shortCut or extention or something else)
         end;
 //---------------------------------

 { TBarClass }

 TBarClass=class
             CurrentBar:string;
             
           private
             XButtons:Tlist;
             FEnvVar : String;
             FChangePath : String;
             function GetButton(Index: Integer): TKButton;
             function GetButtonCount: Integer;
             function GetCmdDirFromEnvVar(sPath: String): String;
             function SetCmdDirAsEnvVar(sPath: String): String;
             procedure SetButton(Index:Integer; const AValue: TKButton);
           //------------------------------------------------------

           public

             Constructor Create;
             destructor Destroy; override;
             //---------------------
             function GetButtonX(Index:integer; What:TInfor):string;
             function InsertButtonX(InsertAt: Integer; ButtonX, CmdX, ParamX, PathX, MenuX, MiskX: String): Integer;
             function AddButtonX(ButtonX, CmdX, ParamX, PathX, MenuX, MiskX: String): Integer;
             //---------------------
             procedure RemoveButton(Index: Integer);
             procedure DeleteAllButtons;
             procedure SetButtonX(Index:integer; What:Tinfor;Value: string);
             procedure LoadFromIniFile(IniFile : TIniFile);
             procedure SaveToIniFile(IniFile : TIniFile);
             procedure LoadFromFile(FileName : String);
             procedure LoadFromStringList(List:TStringList);
             procedure SaveToFile(FileName : String);
             //---------------------
             property ButtonCount: Integer read GetButtonCount;
             property Buttons[Index:Integer]: TKButton read GetButton write SetButton;
             property EnvVar : String read FEnvVar write FEnvVar;
             property ChangePath : String read FChangePath write FChangePath;
           //------------------------------------------------------
           end;
           
implementation
{ TBarClass }

constructor TBarClass.Create;
begin
  XButtons:=TList.Create;
end;

destructor TBarClass.Destroy;
var i:integer;
begin
    if Assigned(XButtons) then
    begin
      if XButtons.Count>0 then
        for I := 0 to XButtons.Count - 1 do
          TKButton(XButtons.Items[I]).Free;
      FreeAndNil(XButtons);
    end;

  inherited Destroy;
end;

procedure TBarClass.SetButtonX(Index: integer; What: Tinfor; Value: string);
begin
If Index>=XButtons.Count then XButtons.Add(TKButton.Create);

 case What of
  ButtonX: TKButton(XButtons.Items[Index]).ButtonX:=Value;
  cmdX:    TKButton(XButtons.Items[Index]).cmdX:=Value;
  paramX:  TKButton(XButtons.Items[Index]).paramX:=Value;
  pathX:   TKButton(XButtons.Items[Index]).pathX:=Value;
  MenuX:   TKButton(XButtons.Items[Index]).menuX:=Value;
  iconicX: begin
             if Value='' then
               TKButton(XButtons.Items[Index]).iconicX:=0
             else
               TKButton(XButtons.Items[Index]).iconicX:=StrToInt(Value);
           end;
  MiskX:   TKButton(XButtons.Items[Index]).MiskX:=Value;
 end;

end;

procedure TBarClass.LoadFromIniFile(IniFile: TIniFile);
var
  BtnCount, I : Integer;
begin
  BtnCount := IniFile.ReadInteger('Buttonbar', 'Buttoncount', 0);
  CurrentBar:= IniFile.FileName;
  for I := 1 to BtnCount do
    begin
       XButtons.Add(TKButton.Create);
           TKButton(XButtons[I-1]).ButtonX :=IniFile.ReadString('Buttonbar', 'button' + IntToStr(I), '');
           TKButton(XButtons[I-1]).CmdX := IniFile.ReadString('Buttonbar', 'cmd' + IntToStr(I), '');
           TKButton(XButtons[I-1]).ParamX := IniFile.ReadString('Buttonbar', 'param' + IntToStr(I), '');
           TKButton(XButtons[I-1]).PathX := IniFile.ReadString('Buttonbar', 'path' + IntToStr(I), '');
           TKButton(XButtons[I-1]).MenuX := IniFile.ReadString('Buttonbar', 'menu' + IntToStr(I), '');
           TKButton(XButtons[I-1]).IconicX := IniFile.ReadInteger('Buttonbar', 'icon' + IntToStr(I),0);
           TKButton(XButtons[I-1]).MiskX := IniFile.ReadString('Buttonbar', 'misk' + IntToStr(I), '');
    end;
end;

procedure TBarClass.SaveToIniFile(IniFile: TIniFile);
var
  I : Integer;
begin
  //For cleaning. Without this saved file will contain removed buttons
  IniFile.EraseSection('Buttonbar');
  IniFile.WriteInteger('Buttonbar', 'Buttoncount', XButtons.Count);

  for I := 0 to XButtons.Count - 1 do
    begin
      IniFile.WriteString('Buttonbar', 'button' + IntToStr(I + 1), SetCmdDirAsEnvVar(GetButtonX(I,ButtonX)));
      IniFile.WriteString('Buttonbar', 'cmd' + IntToStr(I + 1), SetCmdDirAsEnvVar(GetButtonX(I,CmdX)));
      IniFile.WriteString('Buttonbar', 'param' + IntToStr(I + 1), GetButtonX(I,ParamX) );
      IniFile.WriteString('Buttonbar', 'path' + IntToStr(I + 1), GetButtonX(I,PathX) );
      IniFile.WriteString('Buttonbar', 'menu' + IntToStr(I + 1),GetButtonX(I,MenuX) );
      IniFile.WriteString('Buttonbar', 'misk' + IntToStr(I + 1),GetButtonX(I,MiskX) );
    end;
end;

function TBarClass.GetButtonX(Index: integer; What: TInfor): string;
begin
if (index>=XButtons.Count) or (Index<0) then Exit;
      case What of
         ButtonX: Result := TKButton(XButtons.Items[Index]).ButtonX;
         cmdX:    Result := TKButton(XButtons.Items[Index]).CmdX;
         paramX:  Result := TKButton(XButtons.Items[Index]).ParamX;
         pathX:   Result := TKButton(XButtons.Items[Index]).PathX;
         menuX:   Result := TKButton(XButtons.Items[Index]).MenuX;
         iconicX: Result := IntToStr(TKButton(XButtons.Items[Index]).IconicX);
         MiskX:   Result := TKButton(XButtons.Items[Index]).MiskX;
      end;
end;

function TBarClass.InsertButtonX(InsertAt: Integer; ButtonX, CmdX, ParamX, PathX, MenuX, MiskX: String): Integer;
begin
  if InsertAt < 0 then
    InsertAt:= 0;
  if InsertAt > XButtons.Count then
    InsertAt:= XButtons.Count;

  XButtons.Insert(InsertAt, TKButton.Create);

  TKButton(XButtons[InsertAt]).CmdX:= CmdX;
  TKButton(XButtons[InsertAt]).ButtonX:= ButtonX;
  TKButton(XButtons[InsertAt]).ParamX:= ParamX;
  TKButton(XButtons[InsertAt]).PathX:= PathX;
  TKButton(XButtons[InsertAt]).MenuX:= MenuX;
  TKButton(XButtons[InsertAt]).MiskX:= MiskX;

  Result:= InsertAt;
end;

function TBarClass.AddButtonX(ButtonX, CmdX, ParamX, PathX, MenuX, MiskX: String): Integer;
begin
  Result := InsertButtonX(XButtons.Count, ButtonX, CmdX, ParamX, PathX, MenuX, MiskX);
end;

procedure TBarClass.LoadFromFile(FileName: String);
var
  IniFile : Tinifile;
begin
  DeleteAllButtons;
  IniFile := Tinifile.Create(FileName);
  LoadFromIniFile(IniFile);
  IniFile.Free;
end;


procedure TBarClass.LoadFromStringList(List: TStringList);

function ItemOfList(Item:string):string;
begin
if (List.IndexOfName(Item)>0) then
  Result:=List.ValueFromIndex[List.IndexOfName(Item)]
else
  Result:='';
end;

var   BtnCount, I : Integer;
begin
  DeleteAllButtons;
  if (List.IndexOfName('Buttoncount')<>0) then exit;
   BtnCount:=StrToInt(List.ValueFromIndex[List.IndexOfName('Buttoncount')]);
   
   CurrentBar:='Virtual';
  for I := 1 to BtnCount do
    begin
       XButtons.Add(TKButton.Create);
           TKButton(XButtons[I-1]).ButtonX :=ItemOfList('button' + IntToStr(I));
           TKButton(XButtons[I-1]).CmdX := ItemOfList('cmd' + IntToStr(I));
           TKButton(XButtons[I-1]).ParamX :=ItemOfList('param' + IntToStr(I));
           TKButton(XButtons[I-1]).PathX := ItemOfList('path' + IntToStr(I));
           TKButton(XButtons[I-1]).MenuX := ItemOfList('menu' + IntToStr(I));
           if (ItemOfList('icon' + IntToStr(I))<>'') then
           TKButton(XButtons[I-1]).IconicX := StrToInt(ItemOfList('icon' + IntToStr(I)));
           TKButton(XButtons[I-1]).MiskX := ItemOfList('misk' + IntToStr(I));
    end;

end;

procedure TBarClass.SaveToFile(FileName: String);
var
  IniFile : Tinifile;
begin
  IniFile := Tinifile.Create(FileName);
  SaveToIniFile(IniFile);
  IniFile.Free;
end;

procedure TBarClass.RemoveButton(Index: Integer);
begin
    TKButton(XButtons[Index]).Free;
    XButtons.Delete(Index);
end;

procedure TBarClass.DeleteAllButtons;
begin
    while XButtons.Count>0 do
      begin
        TKButton(XButtons[0]).Free;
        XButtons.Delete(0);
      end;
end;

function TBarClass.GetButtonCount: Integer;
begin
Result := XButtons.Count;
end;

function TBarClass.GetButton(Index:Integer): TKButton;
begin
  Result:=TKButton(XButtons[Index]);
end;

function TBarClass.GetCmdDirFromEnvVar(sPath: String): String;
begin
  DoDirSeparators(sPath);
  if Pos(FEnvVar, sPath) <> 0 then
    Result := StringReplace(sPath, FEnvVar, ExcludeTrailingPathDelimiter(FChangePath), [rfIgnoreCase])
  else
    Result := sPath;
end;

procedure TBarClass.SetButton(Index:Integer; const AValue: TKButton);
begin
 XButtons[Index]:=AValue;
end;

function TBarClass.SetCmdDirAsEnvVar(sPath: String): String;
begin
  DoDirSeparators(sPath);
  if Pos(FChangePath, sPath) <> 0 then
    Result := StringReplace(sPath, ExcludeTrailingPathDelimiter(FChangePath), FEnvVar, [rfIgnoreCase])
  else
    Result := sPath;
end;

end.