-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.pas
435 lines (397 loc) · 11.2 KB
/
main.pas
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
{ Convertor - A free & open sorce unit converter
Copyright (C) 2012-2016 H. Raz [email protected]
This source 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 code 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.
A copy of the GNU General Public License is available on the World Wide Web
at <http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing
to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA.
}
unit main;
{$mode objfpc}{$H+}
interface
uses
SysUtils, Forms, Controls, Dialogs, StdCtrls, Menus, ExtCtrls, IniPropStorage,
Buttons, Classes, types, LResources, ClipBrd, Grids, fpexprpars2, unit1;
type
{ TForm1 }
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
GroupBox1: TGroupBox;
GroupBox2: TGroupBox;
IniFile: TIniPropStorage;
AutoClose: TMenuItem;
Panel1: TPanel;
arrow: TImage;
Toggle: TSpeedButton;
U1: TStaticText;
U2: TStaticText;
CatListBox: TListBox;
Panel2: TPanel;
StayOnTop: TMenuItem;
About: TMenuItem;
PopupMenu1: TPopupMenu;
SysTrayIcon: TTrayIcon;
UnitGrid: TStringGrid;
procedure AboutClick(Sender: TObject);
procedure CatListBoxMouseMove(Sender: TObject; Shift: TShiftState;
X, Y: integer);
procedure CatListBoxMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: integer);
procedure EditEditingDone(Sender: TObject);
procedure EditChange(Sender: TObject);
procedure EditEnter(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormWindowStateChange(Sender: TObject);
procedure IniFileRestoreProperties(Sender: TObject);
procedure AutoCloseClick(Sender: TObject);
procedure StayOnTopClick(Sender: TObject);
procedure SysTrayIconClick(Sender: TObject);
procedure ToggleChange(Sender: TObject);
procedure UnitGridCheckboxToggled(sender: TObject; aCol, aRow: Integer;
aState: TCheckboxState);
procedure UnitGridContextPopup(Sender: TObject; MousePos: TPoint;
var Handled: boolean);
procedure UnitGridGetCellHint(Sender: TObject; ACol, ARow: Integer;
var HintText: String);
procedure ChangeValue;
procedure ChooseUnit(UCat: TUnitCategory; i, side: integer);
procedure ToggleUnitsPanel(shw: shortstring);
private
{ private declarations }
FExpParser: TFPExpressionParser;
FParserResult: TFPExpressionResult;
FFromVal: TEdit;
FToVal: TEdit;
FFromUnit: TUnitData;
FToUnit: TUnitData;
FUCat: TUnitCategory;
public
{ public declarations }
end;
const
cABOUT =
'Convertor is a free unit converter.' + sLineBreak +
'By H. Raz ©2012-2017' + sLineBreak +
'https://hdrz.github.io/convertor' + sLineBreak +
sLineBreak +
'Simple expressions are supported:' + sLineBreak +
' +, -, *, /, ^' + sLineBreak +
' cos, sin, arctan, abs, sqr, sqrt, exp,' + sLineBreak +
' ln, log, frac, int, round, trunc' + sLineBreak +
' Angles are in radians.' + sLineBreak +
sLineBreak +
'Using:' + sLineBreak +
' - Up && Down blue arrows by' + sLineBreak +
' Yusuke Kamiyamane (CC BY 3.0)' + sLineBreak +
' - Other icons by RRZE (CC BY-SA 3.0)';
var
Form1: TForm1;
ActiveSide: string; // 'left' or 'right'
Shrink: boolean;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
// read the .xml file and populate the string lists
UnitInit;
// show categories
CatListBox.Items := CatList;
CatListBox.Selected[0] := True;
arrow.Picture.LoadFromLazarusResource('right');
Toggle.LoadGlyphFromLazarusResource('up');
ActiveSide := 'left';
Shrink := False;
FExpParser := TFPExpressionParser.Create(nil);
FExpParser.Builtins := [bcMath];
UnitGrid.FocusRectVisible := False;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FExpParser.Free;
end;
procedure TForm1.FormWindowStateChange(Sender: TObject);
// change visibility of window and tray icon
begin
if Form1.WindowState = wsMinimized then
begin
Form1.WindowState := wsMinimized;
Form1.Hide;
SysTrayIcon.Show;
end
else
begin
Form1.WindowState := wsNormal;
Form1.Show;
SysTrayIcon.Hide;
end;
end;
procedure TForm1.AboutClick(Sender: TObject);
// about message
begin
ShowMessage(cABOUT);
end;
procedure TForm1.CatListBoxMouseMove(Sender: TObject; Shift: TShiftState;
X, Y: integer);
// show hints for categories
var
Item: integer;
begin
Item := CatListBox.ItemAtPos(Point(X, Y), True);
if item >= 0 then
CatListBox.Hint := CatListBox.Items[item]
else
CatListBox.Hint := '';
end;
procedure TForm1.CatListBoxMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: integer);
// select unit category, show units in list
var
i: integer;
Ucat: TUnitCategory;
begin
i := CatListBox.GetIndexAtY(Y);
IniFile.StoredValue['activecat'] := IntToStr(i);
if (i >= 0) and (Button = mbLeft) then
begin
UCat := (CatList.Objects[i] as TUnitCategory);
UnitGrid.RowCount := Ucat.UnitList.Count;
UnitGrid.Cols[1].Assign(UCat.UnitList);
for i:=0 to UnitGrid.RowCount-1 do
begin
UnitGrid.Cells[0, i] := '0';
UnitGrid.Cells[2, i] := '0';
end;
UnitGrid.Cells[0, UCat.LIndx] := '1';
UnitGrid.Cells[2, UCat.RIndx] := '1';
ChooseUnit(UCat, UCat.LIndx, 0);
ChooseUnit(UCat, UCat.RIndx, 2);
Form1.Caption := UCat.Name + ' Convertor';
end;
end;
procedure TForm1.EditEditingDone(Sender: TObject);
// copy result to clipboard
begin
if ActiveSide = 'left' then
Clipboard.AsText := Edit2.Text
else
Clipboard.AsText := Edit1.Text;
end;
procedure TForm1.EditChange(Sender: TObject);
// convert one value to the other
begin
ChangeValue;
end;
procedure TForm1.EditEnter(Sender: TObject);
// hide the lists when typing
begin
if Shrink then
begin
ToggleUnitsPanel('hide');
end;
if Form1.ActiveControl.Name = 'Edit1' then
begin
arrow.Picture.LoadFromLazarusResource('right');
ActiveSide := 'left';
end
else
begin
arrow.Picture.LoadFromLazarusResource('left');
ActiveSide := 'right';
end;
end;
procedure TForm1.IniFileRestoreProperties(Sender: TObject);
// restore application properties from .ini file
var
UCat: TUnitCategory;
i, lu, ru: Integer;
begin
if StrToBool(IniFile.StoredValue['ontop']) then
begin
Form1.formstyle := fsSystemStayOnTop;
StayOnTop.Checked := True;
end
else
begin
Form1.formstyle := fsNormal;
end;
Shrink := StrToBool(IniFile.StoredValue['shrink']);
AutoClose.Checked := Shrink;
if not StrToBool(IniFile.StoredValue['unitsopen']) then
ToggleUnitsPanel('hide');
Edit1.Text := IniFile.StoredValue['value'];
CatListBox.Selected[StrToInt(IniFile.StoredValue['activecat'])] := True;
UCat := (CatListBox.Items.Objects[CatListBox.ItemIndex] as TUnitCategory);
UnitGrid.RowCount := Ucat.UnitList.Count;
UnitGrid.Cols[1].Assign(UCat.UnitList);
lu := StrToInt(IniFile.StoredValue['leftunit']);
ru := StrToInt(IniFile.StoredValue['rightunit']);
for i:=0 to UnitGrid.RowCount-1 do
begin
UnitGrid.Cells[0, i] := '0';
UnitGrid.Cells[2, i] := '0';
end;
UnitGrid.Cells[0, lu] := '1';
UnitGrid.Cells[2, ru] := '1';
ChooseUnit(UCat, lu, 0);
ChooseUnit(UCat, ru, 2);
end;
procedure TForm1.AutoCloseClick(Sender: TObject);
begin
Shrink := not Shrink;
IniFile.StoredValue['shrink'] := BoolToStr(Shrink);
end;
procedure TForm1.StayOnTopClick(Sender: TObject);
// toggle the 'stay on top' state
begin
if Form1.FormStyle = fsSystemStayOnTop then
begin
Form1.formstyle := fsNormal;
IniFile.StoredValue['ontop'] := BoolToStr(False);
end
else
begin
Form1.formstyle := fsSystemStayOnTop;
IniFile.StoredValue['ontop'] := BoolToStr(True);
end;
end;
procedure TForm1.SysTrayIconClick(Sender: TObject);
// restore application from the tray icon
begin
Form1.WindowState := wsNormal;
Form1.Show;
SysTrayIcon.Hide;
end;
procedure TForm1.ToggleChange(Sender: TObject);
// show and hide units panel with click
begin
ToggleUnitsPanel('toggle');
end;
procedure TForm1.UnitGridCheckboxToggled(sender: TObject; aCol, aRow: Integer;
aState: TCheckboxState);
var
i: Integer;
UCat: TUnitCategory;
begin
for i:=0 to UnitGrid.RowCount-1 do
if i<>aRow then
UnitGrid.Cells[aCol,i] := '0'
else
UnitGrid.Cells[aCol,i] := '1';
UCat := (CatListBox.Items.Objects[CatListBox.ItemIndex] as TUnitCategory);
ChooseUnit(Ucat, aRow, aCol);
end;
procedure TForm1.UnitGridContextPopup(Sender: TObject; MousePos: TPoint;
var Handled: boolean);
// disable popup menu on unit listbox
begin
Handled := True;
end;
procedure TForm1.UnitGridGetCellHint(Sender: TObject; ACol, ARow: Integer;
var HintText: String);
var
item: integer;
obj: TUnitData;
// show hints for units
begin
obj := TUnitData(UnitGrid.Cols[1].Objects[ARow]);
if Assigned(obj) then
begin
if obj.Info <> '' then
HintText := obj.Info
else
HintText := obj.Name;
end
else
HintText := '';
end;
procedure TForm1.ChangeValue;
// convert one value to the other
var
resultValue: double;
indx: Integer;
begin
FUCat := (CatListBox.Items.Objects[CatListBox.ItemIndex] as TUnitCategory);
if (FUCat.LeftUnit <> nil) and (FUCat.RightUnit <> nil) then
begin
if ActiveSide = 'left' then
begin
FFromVal := Edit1;
FToVal := Edit2;
FFromUnit := FUCat.LeftUnit;
FToUnit := FUCat.RightUnit;
end
else
begin
FFromVal := Edit2;
FToVal := Edit1;
FFromUnit := FUCat.RightUnit;
FToUnit := FUCat.LeftUnit;
end;
// convert:
try
FExpParser.Expression := FFromVal.Text;
resultValue := ArgToFloat(FExpParser.Evaluate);
FToVal.Text := FloatToStr(FToUnit.ToUnit(FFromUnit.FromUnit(resultValue)));
except
FToVal.Text := '';
end;
end;
IniFile.StoredValue['value'] := Edit1.Text;
end;
procedure TForm1.ChooseUnit(UCat: TUnitCategory; i, side: integer);
var
item: TUnitData;
abbr: string;
begin
item := (UnitGrid.Cols[1].Objects[i] as TUnitData);
if item.abbr <> '' then
abbr := item.ABBR
else
abbr := item.Name;
if side = 0 then
begin
U1.Caption := abbr;
UCat.LeftUnit := item;
IniFile.StoredValue['leftunit'] := IntToStr(i);
UCat.LIndx := i;
ChangeValue;
end
else
begin
U2.Caption := abbr;
UCat.RightUnit := item;
IniFile.StoredValue['rightunit'] := IntToStr(i);
UCat.RIndx := i;
ChangeValue;
end;
end;
procedure TForm1.ToggleUnitsPanel(shw: shortstring);
// show and hide units panel
begin
if Panel2.Visible or (shw = 'hide') then
begin
Panel2.Hide;
Toggle.LoadGlyphFromLazarusResource('down');
IniFile.StoredValue['unitsopen'] := BoolToStr(False);
end
else
begin
Panel2.Show;
Toggle.LoadGlyphFromLazarusResource('up');
IniFile.StoredValue['unitsopen'] := BoolToStr(True);
end;
end;
// load icons resource file
initialization
{$I icons.lrs}
end.