-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.pas
1517 lines (1380 loc) · 42.7 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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, Menus, jpeg, pngimage, ShellAPI, ComCtrls, System.UITypes,
System.Types, System.Win.Registry, System.Math, Vcl.AppEvnts;
type
TGridCell=record
Col, Row: Integer;
end;
type
TConfig=record
Bg: TColor;
Font: TColor;
FontName: string[255];
Criteria: Integer;
Alternative: Integer;
Filename: string[255];
Modified: Boolean;
Separator: Char;
SelRow: Integer;
SelCol: Integer;
Cell: TGridCell;
MinFill: Integer;
MaxFill: Integer;
DefaultDec: Integer;
ForceClose: Boolean;
end;
type
TGridSize=record
X, Y: Integer;
end;
type
TFormMain=class(TForm)
Grid: TStringGrid;
MainMenu: TMainMenu;
PopupMenu: TPopupMenu;
MainTable: TMenuItem;
SubNewFile: TMenuItem;
SubEmpty: TMenuItem;
SubFill: TMenuItem;
SubFormat: TMenuItem;
SubSaveAs: TMenuItem;
MainFile: TMenuItem;
SaveTable: TSaveDialog;
MainTools: TMenuItem;
SubEnumerate: TMenuItem;
SubOpen: TMenuItem;
OpenTable: TOpenDialog;
SubFontSize: TMenuItem;
SubIncFont: TMenuItem;
SubDecFont: TMenuItem;
MainAbout: TMenuItem;
SubExit: TMenuItem;
SubSave: TMenuItem;
N1: TMenuItem;
MainWindow: TMenuItem;
SubMinimizeWin: TMenuItem;
Status: TStatusBar;
SubSettings: TMenuItem;
SubJump: TMenuItem;
MainMethod: TMenuItem;
SubCompromiseProg: TMenuItem;
SubResults: TMenuItem;
N2: TMenuItem;
SubAutoCells: TMenuItem;
SubRestart: TMenuItem;
N3: TMenuItem;
SubClose: TMenuItem;
SubAlternatives: TMenuItem;
SubPosition: TMenuItem;
SubCRL: TMenuItem;
SubAllResults: TMenuItem;
N4: TMenuItem;
SubView: TMenuItem;
SubFullscreen: TMenuItem;
SubStandard: TMenuItem;
SubAuthor: TMenuItem;
SubWebsite: TMenuItem;
PopDelete: TMenuItem;
SubFind: TMenuItem;
App: TApplicationEvents;
PopAdd: TMenuItem;
procedure GridKeyPress(Sender: TObject; var Key: Char);
procedure SubNewFileClick(Sender: TObject);
procedure SubEmptyClick(Sender: TObject);
procedure SubFillClick(Sender: TObject);
procedure MainTableClick(Sender: TObject);
procedure SubFormatClick(Sender: TObject);
procedure SubSaveAsClick(Sender: TObject);
procedure MainFileClick(Sender: TObject);
procedure MainToolsClick(Sender: TObject);
procedure SubEnumerateClick(Sender: TObject);
procedure SubOpenClick(Sender: TObject);
procedure SubIncFontClick(Sender: TObject);
procedure SubDecFontClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure SubExitClick(Sender: TObject);
procedure SubMinimizeWinClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure SubJumpClick(Sender: TObject);
procedure MainWindowClick(Sender: TObject);
procedure SubSettingsClick(Sender: TObject);
procedure FormKeyPress(Sender: TObject; var Key: Char);
procedure SubAutoCellsClick(Sender: TObject);
procedure SubRestartClick(Sender: TObject);
procedure GridDrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure GridKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure StatusHint(Sender: TObject);
procedure SubResultsClick(Sender: TObject);
procedure SubCloseClick(Sender: TObject);
procedure SubAlternativesClick(Sender: TObject);
procedure SubPositionClick(Sender: TObject);
procedure SubCRLClick(Sender: TObject);
procedure SubFullscreenClick(Sender: TObject);
procedure SubStandardClick(Sender: TObject);
procedure SubSaveClick(Sender: TObject);
procedure SubCompromiseProgClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure SubWebsiteClick(Sender: TObject);
procedure SubAuthorClick(Sender: TObject);
procedure GridMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure GridSetEditText(Sender: TObject; ACol, ARow: Integer;
const Value: string);
procedure GridClick(Sender: TObject);
procedure GridMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure PopDeleteClick(Sender: TObject);
procedure MenuDrawItem(Sender: TObject; ACanvas: TCanvas;
ARect: TRect; Selected: Boolean);
procedure MenuMeasureItem(Sender: TObject; ACanvas: TCanvas;
var Width, Height: Integer);
procedure SubFindClick(Sender: TObject);
procedure GridMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure SubAllResultsClick(Sender: TObject);
procedure AppException(Sender: TObject; E: Exception);
procedure PopupMenuPopup(Sender: TObject);
procedure PopAddClick(Sender: TObject);
private
public
procedure Calculate;
procedure AcceptFiles(var Msg: TWMDropFiles); message WM_DROPFILES;
procedure OpenExFile(const Filename: string);
procedure SaveExFile(const Filename: string);
procedure SaveAsHTML(const StringGrid: TStringGrid; const Filename: string);
procedure GenerateGrid(const Cols, Rows: Integer; const ColWidth, RowHeight: Integer); overload;
procedure GenerateGrid(const Criteria, Alternatives: Integer); overload;
procedure EnumerateGrid;
procedure FormatCells;
procedure EmptyCells(const Forced: Boolean=False);
procedure FillCells(const Min: Integer=0; Max: Integer=0);
procedure JumpTo(const Criteria, Alternative: Integer);
procedure LoadConfig;
procedure AdaptToWidth(const IsBlank: Boolean=False);
procedure GetBitmap (const ABitmap: TBitmap);
procedure WriteConfig;
procedure InvokeBrowser(const Filename: string);
procedure SetSelection(const Col, Row: Integer);
procedure DeleteRow;
procedure DeleteColumn;
procedure SetModifiedState(const Value: Boolean=True);
procedure DoneCalculating(Sender: TObject);
procedure AddAlternative;
procedure AddCriteria;
function HasEmptyCells(const StringGrid: TStringGrid): Boolean;
function GetRelativeImportance(const Number: Integer): Double;
function GetDemand(const Number: Integer): Integer;
function GetMax(const Number: Integer): Double;
function GetMin(const Number: Integer): Double;
function GetDesiredValue(const Number: Integer): Double;
function GetNonDesiredValue(const Number: Integer): Double;
function GetSelectedValue(const Col, Row: Integer): Double;
function GetGridSize: TGridSize;
function GetNegativeColor(const InputColor: TColor): TColor;
function GetModifiedState: Boolean;
end;
const
MAX_FIELDS=300;
var
FormMain: TFormMain;
Config: TConfig;
HTML_Icon: TBytes;
implementation
{$R *.dfm}
{$SetPEFlags IMAGE_FILE_RELOCS_STRIPPED}
{$SetPEFlags IMAGE_FILE_LINE_NUMS_STRIPPED}
{$SetPEFlags IMAGE_FILE_LOCAL_SYMS_STRIPPED}
{$SetPEFlags IMAGE_FILE_DEBUG_STRIPPED}
{$SetPEFlags IMAGE_FILE_EXECUTABLE_IMAGE}
uses Settings, Results, Lists, PositionList, About, Find;
procedure TFormMain.OpenExFile(const Filename: string);
var
FS: TFileStream;
Crit, Alt, I, J: Integer;
Data: Double;
begin
Grid.Hide;
Status.SimpleText:='Učitavanje datoteke u toku...';
FS:=TFileStream.Create(Filename, fmOpenRead);
try
FS.Read(Crit, 4);
FS.Read(Alt, 4);
If (Crit<1) Or (Crit>MAX_FIELDS) Or (Alt<1) Or (Alt>MAX_FIELDS) Then
begin
MessageBeep(MB_ICONERROR);
MessageDlg('Datoteka nije validna!', mtError, [mbOK], 0, mbOK);
Status.SimpleText:='';
Exit;
end;
GenerateGrid(Crit, Alt);
For I:=1 To Grid.ColCount-1 Do
For J:=1 To Grid.RowCount-1 Do
begin
FS.Read(Data, SizeOf(Double));
If Data=INFINITE Then
Grid.Cells[I, J]:=''
Else
Grid.Cells[I, J]:=FloatToStr(Data);
end;
finally
FS.Free;
end;
Config.Criteria:=Crit;
Config.Alternative:=Alt;
Status.SimpleText:='Učitana datoteka: '+ExtractFileName(Filename);
Caption:='CalculusEx - '+Copy(ChangeFileExt(ExtractFileName(Filename), ''), 1, 50);
AdaptToWidth;
Grid.Show;
Config.Filename:=Filename;
end;
procedure TFormMain.PopAddClick(Sender: TObject);
begin
If (Config.SelCol>2) Then
AddAlternative
Else
If (Config.SelRow>0) Then
AddCriteria;
SetSelection(-1, -1);
end;
procedure TFormMain.PopDeleteClick(Sender: TObject);
begin
If (Config.SelCol>2) Then
DeleteColumn
Else
If (Config.SelRow>0) Then
DeleteRow;
SetSelection(-1, -1);
end;
procedure TFormMain.PopupMenuPopup(Sender: TObject);
begin
If Config.SelCol>2 Then
begin
PopDelete.Caption:='Izbriši alternativu';
PopAdd.Caption:='Dodaj alternativu';
end
Else
If Config.SelRow>0 Then
begin
PopDelete.Caption:='Izbriši kriterijum';
PopAdd.Caption:='Dodaj kriterijum';
end;
end;
procedure TFormMain.SaveExFile(const Filename: string);
var
FS: TFileStream;
I, J: Integer;
Data: Double;
begin
FS:=TFileStream.Create(ChangeFileExt(FileName, '.cxf'), fmCreate);
try
FS.Write(Config.Criteria, 4);
FS.Write(Config.Alternative, 4);
For I:=1 To Grid.ColCount-1 Do
For J:=1 To Grid.RowCount-1 Do
begin
If Length(Grid.Cells[I, J])>0 Then
Data:=StrToFloat(Grid.Cells[I, J])
Else
Data:=INFINITE;
FS.Write(Data, SizeOf(Double));
end;
finally
Config.Filename:=String(ChangeFileExt(FileName, '.cxf'));
FS.Free;
end;
end;
procedure TFormMain.SaveAsHTML(const StringGrid: TStringGrid; const Filename: string);
var
HTMLFile: TStringStream;
Data: String;
rgbBg, rgbText: string;
TD, TR: Integer;
begin
Config.Filename:=ChangeFileExt(Filename, '.html');
rgbBg:='rgb('+GetRValue(ColorToRGB(Config.Bg)).ToString+', '+GetGValue(ColorToRGB(Config.Bg)).ToString+', '+GetBValue(ColorToRGB(Config.Bg)).ToString+')';
rgbText:='rgb('+GetRValue(ColorToRGB(Config.Font)).ToString+', '+GetGValue(ColorToRGB(Config.Font)).ToString+', '+GetBValue(ColorToRGB(Config.Font)).ToString+')';
Data:=
'<!doctype html>'+#13#10+'<html>'
+#13#10+'<head>' +#13#10
+'<title>Metoda kompromisnog programiranja - '+ChangeFileExt(ExtractFileName(FileName), '')+'</title>'
+#13#10
+'<meta http-equiv="content-type" content="text/html; charset=UTF-8" />'
+#13#10
+'<meta charset="utf-8" />'+#13#10
+'<link rel="icon" href="'+TEncoding.UTF8.GetString(HTML_Icon)+'" />'
+#13#10
+'<style>'
+#13#10
+'table {margin: auto auto; table-layout: fixed; font-size: x-large; color: '+rgbText+';'
+' border: '+(StringGrid.Canvas.Pen.Width.toString)+'px solid rgb(0, 0, 0); text-align: center;}'
+#13#10
+'th {background-color:rgb(0, 132, 255); padding: 10px; border: '+(StringGrid.Canvas.Pen.Width.toString)+'px solid rgb(0, 0, 0);}'
+#13#10
+'tr {background-color: '+rgbBg+'; padding: 10px; border: '+(StringGrid.Canvas.Pen.Width.toString)+'px solid rgb(0, 0, 0);}'
+#13#10
+'td {border: '+(StringGrid.Canvas.Pen.Width.toString)+'px solid rgb(0, 0, 0);}'
+#13#10
+'</style>'
+#13#10+'</head>'
+#13#10
+'<body>'+#13#10
+'<table><caption>Metoda kompromisnog programiranja</caption>';
For TR:=0 To StringGrid.RowCount-1 Do
begin
Data:=Data+'<tr>';
For TD:=0 To StringGrid.ColCount-1 Do
If (TR>0) And (TD>0) Then
Data:=Data+'<td>'+StringGrid.Cells[TD, TR]+'</td>'
Else
Data:=Data+'<th>'+StringGrid.Cells[TD, TR]+'</th>';
Data:=Data+'</tr>';
end;
Data:=Data+'</table>'+#13#10+'</body>'+#13#10+'</html>';
HTMLFile:=TStringStream.Create(Data, TEncoding.UTF8);
try
HTMLFile.SaveToFile(String(Config.Filename));
finally
HTMLFile.Free;
end;
end;
procedure TFormMain.AcceptFiles(var Msg: TWMDropFiles);
var
Filename: Array[0..MAX_PATH] Of Char;
begin
DragQueryFile(Msg.Drop, 0, Filename, MAX_PATH);
OpenExFile(Filename);
DragFinish(Msg.Drop);
end;
procedure TFormMain.FormShow(Sender: TObject);
begin
DragAcceptFiles(Handle, True);
If FileExists(ParamStr(1)) Then
OpenExFile(ParamStr(1));
end;
procedure TFormMain.GenerateGrid(const Cols, Rows: Integer; const ColWidth, RowHeight: Integer);
begin
Grid.DefaultColWidth:=ColWidth;
Grid.DefaultRowHeight:=RowHeight;
If (Cols<=0) Or (Rows<=0) Then
begin
Grid.RowCount:=(ClientHeight Div Grid.DefaultRowHeight)-1;
Grid.ColCount:=(ClientWidth Div Grid.DefaultColWidth)-1;
end
Else
begin
Grid.RowCount:=Rows+1;
Grid.ColCount:=Cols+1;
end;
EnumerateGrid;
end;
procedure TFormMain.GenerateGrid(const Criteria: Integer; const Alternatives: Integer);
begin
Grid.ColCount:=Alternatives+3;
Grid.RowCount:=Criteria+1;
Grid.FixedCols:=1;
EnumerateGrid;
If (Assigned(Lists.FormList)) And (Assigned(PositionList.FormPosition)) Then
begin
Lists.FormList.Grid.ColCount:=4;
Lists.FormList.Grid.RowCount:=Alternatives+1;
PositionList.FormPosition.Grid.ColCount:=3;
PositionList.FormPosition.Grid.RowCount:=Alternatives+1;
end;
end;
procedure TFormMain.EnumerateGrid;
var
Cols, Rows, CurrAlt: Integer;
begin
CurrAlt:=1;
Grid.Perform(WM_SETREDRAW, 0, 0);
Grid.Cells[0, 0]:='Broj';
Grid.Cells[1, 0]:='W';
Grid.Cells[2, 0]:='Zahtev (0/1)';
For Cols:=3 To Grid.ColCount-1 Do
begin
Grid.Cells[Cols, 0]:='a'+IntToStr(CurrAlt);
Inc(CurrAlt);
end;
For Rows:=1 To Grid.RowCount-1 Do
Grid.Cells[0, Rows]:='f'+IntToStr(Rows);
Grid.Perform(WM_SETREDRAW, 1, 0);
Grid.Repaint;
Grid.Show;
end;
procedure TFormMain.FormatCells;
var
I, J: Integer;
NumDec: Integer;
StrNum: string;
begin
StrNum:=InputBox('Formatiranje podataka', 'Unesite broj decimala:', Config.DefaultDec.ToString);
If (Not TryStrToInt(StrNum, NumDec)) Or ((NumDec>16) Or (NumDec<0)) Then
begin
MessageBeep(MB_ICONERROR);
MessageDlg('Uneta vrednost nije validna!'+#13#10+'Unesite vrednost u rasponu od 0 do 16.', mtError, [mbOK], 0, mbOK);
Exit;
end;
If (NumDec>=0) And (NumDec<=16) Then
begin
Grid.Perform(WM_SETREDRAW, 0, 0);
For I:=3 To Grid.ColCount-1 Do
For J:=1 To Grid.RowCount-1 Do
begin
If Grid.Cells[I, J]<>'' Then
Grid.Cells[I, J]:=Format('%0.'+IntToStr(NumDec)+'f', [StrToFloat(Grid.Cells[I, J])]);
end;
AdaptToWidth;
end;
end;
procedure TFormMain.JumpTo(const Criteria: Integer; const Alternative: Integer);
begin
Grid.Col:=Alternative+2;
Grid.Row:=Criteria;
end;
procedure TFormMain.LoadConfig;
var
RegistryObj: TRegistry;
begin
RegistryObj:=TRegistry.Create;
try
RegistryObj.RootKey:=HKEY_LOCAL_MACHINE;
RegistryObj.OpenKey('SOFTWARE\NeoVisio\CalculusEx', False);
If (RegistryObj.ReadString('BgColor')<>'') Then
Config.Bg:=StringToColor(RegistryObj.ReadString('BgColor'));
If (RegistryObj.ReadString('TextColor')<>'') Then
Config.Font:=StringToColor(RegistryObj.ReadString('TextColor'));
If (RegistryObj.ReadString('FontName')<>'') Then
Config.FontName:= RegistryObj.ReadString('FontName');
If (RegistryObj.ReadInteger('FontSize')>4) And (RegistryObj.ReadInteger('FontSize')<501) Then
Grid.Canvas.Font.Size:=RegistryObj.ReadInteger('FontSize');
If (RegistryObj.ReadInteger('FormatNum')>-1) And (RegistryObj.ReadInteger('FormatNum')<10) Then
Config.DefaultDec:=RegistryObj.ReadInteger('FormatNum');
If (RegistryObj.ReadInteger('FillFrom')>-1) And (RegistryObj.ReadInteger('FillFrom')<MaxInt) Then
Config.MinFill:=RegistryObj.ReadInteger('FillFrom');
If (RegistryObj.ReadInteger('FillTo')>-1) And (RegistryObj.ReadInteger('FillTo')<MaxInt) Then
Config.MaxFill:=RegistryObj.ReadInteger('FillTo');
RegistryObj.CloseKey;
finally
RegistryObj.Free;
end;
end;
function TFormMain.GetRelativeImportance(const Number: Integer): Double;
begin
Result:=0;
If (Grid.RowCount>Number) Then
Result:=(Grid.Cells[1, Number]).ToDouble;
end;
function TFormMain.GetDemand(const Number: Integer): Integer;
begin
Result:=0;
If (Grid.RowCount>Number) Then
Result:=(Grid.Cells[2, Number].ToInteger);
end;
function TFormMain.GetMax(const Number: Integer): Double;
var
Col: Integer;
begin
Result:=0;
If (Grid.RowCount>Number) Then
For Col:=3 To Grid.ColCount-1 Do
If (Result<(Grid.Cells[Col, Number]).ToDouble) Then
Result:=(Grid.Cells[Col, Number].ToDouble);
end;
function TFormMain.GetMin(const Number: Integer): Double;
var
Col: Integer;
begin
Result:=(Grid.Cells[3, 1]).ToDouble;
If (Grid.RowCount>Number) Then
For Col:=3 To Grid.ColCount-1 Do
If (Result>(Grid.Cells[Col, Number]).ToDouble) Then
Result:=(Grid.Cells[Col, Number].ToDouble);
end;
function TFormMain.GetDesiredValue(const Number: Integer): Double;
begin
If (GetDemand(Number)=0) Then
Result:=GetMin(Number)
Else
Result:=GetMax(Number);
end;
function TFormMain.GetNonDesiredValue(const Number: Integer): Double;
begin
If (GetDemand(Number)=0) Then
Result:=GetMax(Number)
Else
Result:=GetMin(Number);
end;
function TFormMain.GetSelectedValue(const Col, Row: Integer): Double;
begin
Result:=0;
If (Grid.ColCount>Col) And (Grid.RowCount>Row) Then
Result:=(Grid.Cells[Col, Row].toDouble);
end;
function TFormMain.GetGridSize: TGridSize;
var
CellRect: TRect;
begin
CellRect:=Grid.CellRect(Grid.ColCount-1, Grid.RowCount-1);
Result.X:=CellRect.BottomRight.X;
Result.Y:=CellRect.BottomRight.Y;
end;
procedure TFormMain.GetBitmap(const ABitmap: TBitmap);
var
ControlCanvas: TControlCanvas;
Size: TGridSize;
begin
ControlCanvas:=TControlCanvas.Create;
Size:=GetGridSize;
try
ControlCanvas.Handle:=Grid.Canvas.Handle;
ABitmap.SetSize(Size.X, Size.Y);
BitBlt(ABitmap.Canvas.Handle, 0, 0, ABitmap.Width, ABitmap.Height, ControlCanvas.Handle, 0, 0, SRCCOPY);
finally
ReleaseDC(Grid.Handle, ControlCanvas.Handle);
ControlCanvas.Free;
end;
end;
procedure TFormMain.WriteConfig;
var
PC: TRegistry;
begin
PC:=TRegistry.Create;
try
PC.RootKey:=HKEY_LOCAL_MACHINE;
PC.OpenKey('SOFTWARE', False);
PC.OpenKey('NeoVisio', True);
PC.OpenKey('CalculusEx', True);
PC.WriteString('BgColor', ColorToString(Config.Bg));
PC.WriteString('TextColor', ColorToString(Config.Font));
PC.WriteString('FontName', String(Config.FontName));
PC.WriteInteger('FontSize', Grid.Canvas.Font.Size);
PC.WriteInteger('FormatNum', Config.DefaultDec);
PC.WriteInteger('FillFrom', Config.MinFill);
PC.WriteInteger('FillTo', Config.MaxFill);
PC.CloseKey;
finally
PC.Free;
end;
end;
procedure TFormMain.InvokeBrowser(const Filename: string);
var
Dialog: Integer;
begin
Dialog:=MessageDlg('Da li želite da pregledate HTML dokument?', mtConfirmation, [mbYes, mbNo], 0, mbNo);
If Dialog=mrYes Then
ShellExecute(Handle, 'open', PWideChar(Filename), Nil, Nil, SW_SHOWNORMAL);
end;
procedure TFormMain.SetSelection(const Col: Integer; const Row: Integer);
begin
If (Col>2) Or (Row>0) Then
Grid.PopupMenu:=PopupMenu
Else
Grid.PopupMenu:=Nil;
Config.SelRow:=Row;
Config.SelCol:=Col;
Grid.Repaint;
end;
function TFormMain.GetNegativeColor(const InputColor: TColor): TColor;
begin
Result:=RGB(255-GetRValue(InputColor), 255-GetGValue(InputColor), 255-GetBValue(InputColor))
end;
procedure TFormMain.DeleteRow;
var
I: Integer;
begin
If Config.SelRow<Grid.RowCount Then
begin
For I:=Config.SelRow To Grid.RowCount-2 Do
Grid.Rows[I].Assign(Grid.Rows[I+1]);
Grid.RowCount:=Grid.RowCount-1;
EnumerateGrid;
end;
end;
procedure TFormMain.DeleteColumn;
var
I: Integer;
begin
If Config.SelCol<Grid.ColCount Then
begin
For I:=Config.SelCol To Grid.ColCount-2 Do
Grid.Cols[I].Assign(Grid.Cols[I+1]);
Grid.ColCount:=Grid.ColCount-1;
EnumerateGrid;
end;
end;
procedure TFormMain.SetModifiedState(const Value: Boolean);
begin
If Config.Modified<>Value Then
Config.Modified:=Value;
end;
procedure TFormMain.AddAlternative;
begin
GenerateGrid(Config.Criteria, Config.Alternative+1);
Inc(Config.Alternative);
end;
procedure TFormMain.AddCriteria;
begin
GenerateGrid(Config.Criteria+1, Config.Alternative);
Inc(Config.Criteria);
end;
function TFormMain.GetModifiedState;
begin
Result:=Config.Modified;
end;
procedure TFormMain.DoneCalculating(Sender: TObject);
begin
Status.SimpleText:='';
Results.FormResults.Show;
end;
{********************************************************
*********************************************************}
procedure TFormMain.FormClose(Sender: TObject; var Action: TCloseAction);
Var
Dialog: Integer;
begin
If Config.ForceClose Then Exit;
If GetModifiedState Then
begin
Action:=caNone;
Beep;
Dialog:=MessageDlg('Da li želite da sačuvate aktivnu datoteku?', mtConfirmation, [mbYes, mbClose, mbCancel], 0, mbYes);
If Dialog=mrYes Then
begin
If FileExists(String(Config.Filename)) Then
SaveExFile(String(Config.Filename))
Else
SubSaveAs.Click;
end
Else
If Dialog=mrClose Then
begin
Action:=caFree;
DragAcceptFiles(Handle, False);
end;
end;
end;
procedure TFormMain.FormCreate(Sender: TObject);
var
Format: TFormatSettings;
RS: TResourceStream;
begin
{$FINITEFLOAT OFF}
SetErrorMode(SEM_FAILCRITICALERRORS);
LoadConfig;
Format:=FormatSettings;
Config.Separator:=Format.DecimalSeparator;
Application.OnException:=AppException;
SetModifiedState(False);
Screen.MenuFont.Style:=[fsBold];
Config.Cell.Col:=-1;
Config.Cell.Row:=-1;
If (FindResource(HInstance, 'html_icon', 'HTML')<>0) Then
begin
RS:=TResourceStream.Create(HInstance, 'html_icon', 'HTML');
try
SetLength(HTML_Icon, RS.Size);
RS.Read(HTML_Icon[0], RS.Size);
finally
RS.Free;
end;
end;
end;
procedure TFormMain.FormKeyPress(Sender: TObject; var Key: Char);
begin
If (Key=#116) Then
Repaint;
end;
procedure TFormMain.EmptyCells(const Forced: Boolean=False);
var
I, J: Integer;
begin
If Not Forced Then
MessageBeep(MB_ICONEXCLAMATION);
If ((Not Forced) And (MessageDlg('Da li ste sigurni da želite da ispraznite trenutnu tabelu?', mtConfirmation, [mbYes, mbNo], 0, mbYes)=mrYes)) Or (Forced) Then
begin
Grid.Perform(WM_SETREDRAW, 1, 0);
For I:=1 To Grid.ColCount-1 Do
For J:=1 To Grid.RowCount-1 Do
Grid.Cells[I, J]:='';
Grid.Perform(WM_SETREDRAW, 1, 0);
Grid.Repaint;
SetModifiedState;
end;
end;
procedure TFormMain.FillCells(const Min: Integer = 0; Max: Integer = 0);
var
I, J: Integer;
MaxVal, MinVal: Integer;
begin
Randomize;
MaxVal:=1000;
MinVal:=99;
If (Min>-1) And (Max>-1) And (Min<Max) Then
begin
MaxVal:=Max;
MinVal:=Min;
end;
Grid.Perform(WM_SETREDRAW, 0, 0);
For I:=1 To Grid.ColCount-1 Do
For J:=1 To Grid.RowCount-1 Do
If (I>2) Then
Grid.Cells[I, J]:=FloatToStr(StrToFloat(IntToStr(RandomRange(MinVal, MaxVal)+1)+Config.Separator+IntToStr(Random(99)+1)))
Else
If (I=2) Then
Grid.Cells[I, J]:=FloatToStr(StrToFloat(IntToStr(RandomFrom([0, 1]))))
Else
If (I=1) Then
Grid.Cells[I, J]:=FloatToStr(StrToFloat('0'+Config.Separator+IntToStr(RandomRange(1, 99))));
Grid.Perform(WM_SETREDRAW, 1, 0);
Grid.Repaint;
SetModifiedState;
end;
procedure TFormMain.AdaptToWidth(const IsBlank: Boolean=False);
var
I, J, CalculatedWidth, CalculatedHeight, TestWidth: Integer;
Size: TGridSize;
begin
Grid.Perform(WM_SETREDRAW, 0, 0);
If (Not IsBlank) Then
begin
CalculatedHeight:=Grid.Canvas.TextHeight('0')+4;
For I:=0 To Grid.ColCount-1 Do
begin
CalculatedWidth:=0;
For J:=0 To Grid.RowCount-1 Do
If (Length(Grid.Cells[I, J])>0) Then
begin
TestWidth:=(Grid.Canvas.TextWidth('0')*(Grid.Cells[I, J].Length+4));
If (TestWidth>CalculatedWidth) Then
CalculatedWidth:=TestWidth;
end;
Grid.ColWidths[I]:=CalculatedWidth;
end;
Grid.DefaultRowHeight:=CalculatedHeight;
end;
Size:=GetGridSize;
If (Screen.WorkAreaWidth>Size.X) And (Screen.WorkAreaHeight>Size.Y) Then
begin
Constraints.MinWidth:=Size.X;
Constraints.MinHeight:=Size.Y;
end
Else
begin
Constraints.MinWidth:=0;
Constraints.MinHeight:=0;
end;
Grid.Perform(WM_SETREDRAW, 1, 0);
Grid.Repaint;
Repaint;
end;
procedure TFormMain.AppException(Sender: TObject; E: Exception);
begin
If (E is EDivByZero) Or (E is EZeroDivide) Or (E is EInvalidOperation) Or (E is EMathError) Then
ShowMessage('Greška pri proračunu!'+#13#10+'Proverite unete podatke i pokušajte ponovo');
end;
function TFormMain.HasEmptyCells(const StringGrid: TStringGrid): Boolean;
var
I, J: Integer;
begin
Result:=False;
If (Not StringGrid.Visible) Then
begin
Result:=True;
Exit;
end;
For I:=1 To StringGrid.ColCount-1 Do
For J:=0 To StringGrid.RowCount-1 Do
If (StringGrid.Cells[I, J]='') Then
begin
Result:=True;
Exit;
end;
end;
procedure TFormMain.GridClick(Sender: TObject);
var
Col, Row: Integer;
begin
Col:=Grid.Col;
Row:=Grid.Row;
Config.Cell.Col:=-1;
Config.Cell.Row:=-1;
Config.SelCol:=-1;
Config.SelRow:=-1;
Grid.Repaint;
If (Col>-1) And (Row>-1) And (Grid.Cells[Col, Row]<>'') And (Grid.Cells[Col, Row]<>Status.SimpleText) Then
Status.SimpleText:='['+(Grid.Col.ToString)+', '+(Grid.Row.ToString)+'] = '+Grid.Cells[Col, Row];
end;
procedure TFormMain.GridDrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
CellString: string;
Format: Integer;
begin
Format:=(DT_VCENTER Or DT_CENTER Or DT_SINGLELINE);
CellString:=Grid.Cells[ACol, ARow];
Grid.Canvas.Brush.Color:=Config.Bg;
Grid.Canvas.Font.Color:=Config.Font;
Grid.Canvas.Font.Name:=String(Config.FontName);
Grid.Canvas.Font.Style:=[];
If gdSelected In State Then
begin
Grid.Canvas.Brush.Color:=Config.Font;
Grid.Canvas.Font.Color:=Config.Bg;
end
Else
If gdFixed In State Then
begin
Grid.Canvas.Brush.Color:=$00FF9900;
Grid.Canvas.Font.Color:=clWhite;
Grid.Canvas.Font.Style:=[fsBold];
end;
If (Config.SelRow>0) And (ARow=Config.SelRow) Then
begin
Grid.Canvas.Brush.Color:=GetNegativeColor(Config.Bg);
Grid.Canvas.Font.Color:=GetNegativeColor(Config.Font);
Grid.Canvas.Font.Style:=[fsBold];
If (ACol=0) Or (ARow=0) Then
begin
Grid.Canvas.Brush.Color:=clBlue;
Grid.Canvas.Font.Color:=clWhite;
end;
end;
If (Config.SelCol>2) And (ACol=Config.SelCol) Then
begin
Grid.Canvas.Brush.Color:=GetNegativeColor(Config.Bg);
Grid.Canvas.Font.Color:=GetNegativeColor(Config.Font);
Grid.Canvas.Font.Style:=[fsBold];
If (ACol=0) Or (ARow=0) Then
begin
Grid.Canvas.Brush.Color:=clBlue;
Grid.Canvas.Font.Color:=clWhite;
end;
end;
If (Config.Cell.Col=ACol) And (Config.Cell.Row=ARow) Then
begin
Grid.Canvas.Brush.Color:=GetNegativeColor(Config.Bg);
Grid.Canvas.Font.Color:=GetNegativeColor(Config.Font);
Grid.Canvas.Font.Style:=[fsItalic, fsBold];
end;
Grid.Canvas.FillRect(Rect);
DrawText(Grid.Canvas.Handle, CellString, CellString.Length, Rect, Format);
end;
procedure TFormMain.GridKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
If Key=VK_F5 Then
Grid.Repaint;
end;
procedure TFormMain.GridKeyPress(Sender: TObject; var Key: Char);
var
Cell: string;
begin
Cell:=Grid.Cells[Grid.Col, Grid.Row];
If (Grid.Col=2) Then
begin
If ((Key<>'0') And (Key<>'1') And (Key<>#8)) Or ((Cell.Length>=1) And (Key<>#8)) Then
begin
Beep;
Key:=#0;
end;
end
Else
If (Not(Ord(Key) In ([48..57])) And (Not(Ord(Key) In [8..13])) And ((Key<>Config.Separator))
And (Not(Ord(Key)=45))) Then
begin
Beep;
Key:=#0;
end
Else
If (((Cell.CountChar(Config.Separator)>0) Or (Cell.Length<1)) And (Key=Config.Separator)) Then
begin
Beep;
Key:=#0;
end;
end;
procedure TFormMain.GridMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
Col, Row: Integer;
Coords: TPoint;
begin
Grid.MouseToCell(X, Y, Col, Row);
ClientToScreen(Coords);
If (Col>2) Or (Row>0) Then
Grid.Options:=Grid.Options+[goColMoving, goRowMoving]
Else
Grid.Options:=Grid.Options-[goColMoving, goRowMoving];
end;
procedure TFormMain.GridMouseMove(Sender: TObject; Shift: TShiftState; X,