-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPicture.cpp
More file actions
165 lines (136 loc) · 4.59 KB
/
Picture.cpp
File metadata and controls
165 lines (136 loc) · 4.59 KB
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
/*
Open source not for commercial deployment
*/
//---------------------------------------------------------------------------
#pragma hdrstop
#include "Picture.h"
#include "GlobalUnit.h"
#include "OutputScreenUnit.h"
void Picture::staticInit(String dir)
{
Picture::ImageDir = dir + "Data\\background\\";
Picture::ImageDir = Picture::ImageDir.UpperCase();
}
Picture::Picture(TWinControl* Owner, int left, int top, int width, int height, String clickCommand, String dblClickCommand):
TImage(Owner)
{
Parent = Owner;
Left = left;
Top = top;
Width = width;
Height = height;
OnClick = &ImageClick;
OnDblClick = &ImageDblClick;
OnMouseDown = &ImageMouseDown;
OnClickCommand = clickCommand;
OnDubbleClickCommand = dblClickCommand;
Clear();
}
void Picture::Clear()
{
Canvas->Brush->Color = OutputScreen->curScreenSettings.CurSettings.BackgroundPicture.BackGroundColor;
Canvas->FillRect(TRect(0,0,Width,Height));
imageName = "";
}
bool Picture::LoadPictureFromFile( String s)
{
Clear();
String FileName = expandPicutureFileName(s);
if (FileName.Length()==0) return false;
// first check if the picture file exist.
ifstream pictureFile(FileName.c_str());
if (!pictureFile.is_open()) return false;
pictureFile.close();
setImageName(s);
bool BmpFile = FileName.UpperCase().Pos(".BMP")+3 == FileName.Length();
JPEGImage = new TJPEGImage;
BMPImage = new Graphics::TBitmap();
if (BmpFile)
{
// bmp file can just be loaded.
BMPImage->LoadFromFile(FileName);
}
else
{
//convert the JPG file to a BMP
JPEGImage->LoadFromFile(FileName);
BMPImage->Assign ( JPEGImage );
}
//calculate the resize factor to let the piture fill the screen.
double WidthOriginal = BMPImage->Width;
double HeightOriginal = BMPImage->Height;
double fraction;
double ymargin=0;
double xmargin=0;
bool smallEdges=false;
if ( WidthOriginal/HeightOriginal > Width/Height)
{
fraction = WidthOriginal / Width;
ymargin = (Height - HeightOriginal/fraction )/2;
if (ymargin < Height/30) smallEdges=true;
}
else
{
fraction = HeightOriginal / Height;
xmargin = ( Width - WidthOriginal/fraction) /2;
if (xmargin < Width/30) smallEdges=true;
}
TRect maxPictureSize;
if (smallEdges)
maxPictureSize = TRect(0,0,Width, Height ); // full screen
else
maxPictureSize = TRect(xmargin,ymargin,xmargin+WidthOriginal/fraction, ymargin+HeightOriginal/fraction );
// convert and resize to fill the screen, position the bmp on the Image object.
Canvas->StretchDraw(maxPictureSize, BMPImage);
delete BMPImage;
delete JPEGImage;
return true;
}
//---------------------------------------------------------------------------
void __fastcall Picture::ImageClick(TObject *Sender)
{
Picture::LastClickedPicture = this;
MainForm->ProcessCommands(OnClickCommand);
}
//---------------------------------------------------------------------------
void __fastcall Picture::ImageDblClick(TObject *Sender)
{
Picture::LastClickedPicture = this;
MainForm->ProcessCommands(OnDubbleClickCommand);
}
void Picture::SelectNewPicture()
{
if (MainForm->BackgroundImageDialog->Execute())
{
String s = MainForm->BackgroundImageDialog->FileName;
int pos = s.UpperCase().Pos(Picture::ImageDir);
if (pos==1)
s = s.SubString(Picture::ImageDir.Length()+1,s.Length());
LoadPictureFromFile(s);
}
}
void Picture::command(String s)
{
if (s=="ToOutput") MainForm->ProcessCommands("BG_"+Picture::LastClickedPicture->imageName);
else if (s=="ToPreview") MainForm->ProcessCommands("Preview_Load_BG_"+Picture::LastClickedPicture->imageName);
else if (s=="OpenFile") Picture::LastClickedPicture->SelectNewPicture();
else if (s=="Clear") Picture::LastClickedPicture->Clear();
}
//---------------------------------------------------------------------------
void __fastcall Picture::ImageMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
String t = Sender->ClassName();
Picture::LastClickedPicture = this;
}
//---------------------------------------------------------------------------
String Picture::getImageName()
{
return imageName;
}
void Picture::setImageName(String s)
{
if (s.UpperCase().Pos(ImageDir)==1)
imageName = getAfter(s,ImageDir);
else imageName = s;
}
#pragma package(smart_init)