ecere/i18n: Added GUI property categories strings
[sdk] / ecere / src / gui / controls / Picture.ec
1 namespace gui::controls;
2
3 import "Window"
4
5 public class Picture : CommonControl
6 {
7    class_property(icon) = "<:ecere>mimeTypes/image.png";
8
9    background = black;
10    opacity = 0;
11    drawBehind = true;
12    inactive = true;
13
14 public:
15    property Color tint { set { tint = value; } };   
16    property BitmapResource image
17    {
18       property_category $"Appearance" 
19       set
20       {
21          if(this)
22          {
23             AddResource(value);
24             RemoveResource(bitmapRes);
25             bitmapRes = value;
26             defaultBitmap = value ? false : true;
27             //if(created)
28                OnLoadGraphics();
29          }
30       }
31
32       get
33       {
34          if(this)
35             return defaultBitmap ? null : bitmapRes;
36          return null;
37       }
38    }
39    property bool filter { property_category $"Appearance" set { filter = value; } get { return filter; } };
40    property Bitmap bitmapImage
41    {
42       set
43       {
44          OnUnloadGraphics();
45
46          bitmapImage = value;         
47
48          OnLoadGraphics();
49       }
50    }
51
52 private:
53    BitmapResource bitmapRes;
54    float zoomFactor;
55    Color tint;
56    bool zoom, filter;
57    bool defaultBitmap;
58    Bitmap bitmapImage;
59    Bitmap bitmap;
60
61    Picture()
62    {
63       zoomFactor = 1.0f;
64       tint = white;
65
66       if(isDocument)
67       {
68          Menu fileMenu;
69          menu = Menu { };
70          fileMenu = Menu { menu, "File", f };
71          MenuItem { fileMenu, $"Save\tCtrl+S", s, ctrlS, NotifySelect = MenuFileSave };
72          MenuItem { fileMenu, $"Save As...",   a, NotifySelect = MenuFileSaveAs };
73       }
74       return true;
75    }
76
77    ~Picture()
78    {
79       if(bitmapImage)
80          delete bitmap;
81    }
82
83    bool OnLoadGraphics()
84    {
85       bool result = false;
86
87       if(bitmapImage)
88       {
89          if(!bitmap)
90          {
91             bitmap = { };
92             
93             if(hasHorzScroll || hasVertScroll)
94                bitmap.Copy(bitmapImage);
95             else if(bitmap.Allocate(null, clientSize.w, clientSize.h, 0, bitmapImage.pixelFormat, false))
96             {
97                Surface s = bitmap.GetSurface(0,0,null);
98                s.Filter(bitmapImage, 0,0,0,0, bitmap.width, bitmap.height, bitmapImage.width, bitmapImage.height);
99                delete s;
100             }
101          }
102          bitmap.MakeDD(displaySystem);
103       }
104       else
105       {
106          if(!bitmapRes)
107          {
108             bitmapRes = BitmapResource { fileName = ":mimeTypes/image.png", alphaBlend = true, window = this };
109             defaultBitmap = true;
110             bitmap = null;
111          }
112          else
113             bitmap = bitmapRes.bitmap;
114       }
115
116       SetInitSize(initSize);
117
118       if(bitmap)
119       {
120          if(zoom)
121             SetScrollArea(bitmap.width, bitmap.height, false);
122          result = true;
123       }
124       else if(!isDocument)
125          result = true;
126       return result;
127    }
128
129    void OnUnloadGraphics()
130    {
131       if(bitmapImage)
132       {
133          delete bitmap;
134       }
135    }
136
137    bool OnResizing(int * w, int * h)
138    {
139       Size size = initSize;
140       Anchor anchor = this.anchor;
141
142       if(!size.w && (!anchor.left.type || !anchor.right.type))
143       {
144          if(bitmap)
145             *w = bitmap.width;
146          else
147             *w = 80;
148       }
149          
150       if(!size.h && (!anchor.top.type || !anchor.bottom.type))
151       {
152          
153          if(bitmap)
154             *h = bitmap.height;
155          else
156             *h = 80;
157       }
158       return true;
159    }
160
161    void OnScroll(ScrollBarAction action, int position, Key key)
162    {
163       Update(null);
164    }
165
166    OnHScroll = OnScroll;
167    OnVScroll = OnScroll;
168
169    bool OnKeyHit(Key key, unichar ch)
170    {
171       Bitmap bitmap = this.bitmap;
172       if(zoom)
173       {
174          switch(key)
175          {
176             case equal:
177             case keyPadPlus:
178                if(zoomFactor < 25)
179                {
180                   float x = 0.5f, y = 0.5f;
181                   if(bitmap.width * zoomFactor > clientSize.w) 
182                      x = scroll.x / (bitmap.width * zoomFactor - clientSize.w);
183                   if(bitmap.height * zoomFactor > clientSize.h) 
184                      y = scroll.y / (bitmap.height * zoomFactor - clientSize.h);
185
186                   zoomFactor *= 1.5;
187                   SetScrollArea(
188                      (int)(bitmap.width * zoomFactor), 
189                      (int)(bitmap.height * zoomFactor), false);
190
191                   SetScrollPosition(
192                      (int)(Max(0, bitmap.width * zoomFactor - clientSize.w) * x),
193                      (int)(Max(0, bitmap.height * zoomFactor - clientSize.h) * y));
194
195                   Update(null);
196                }
197                break;
198             case minus:
199             case keyPadMinus:
200                if(zoomFactor > 0.05)
201                {
202                   float x = 0.5f, y = 0.5f;
203                   if(bitmap.width * zoomFactor > clientSize.w) 
204                      x = scroll.x / (bitmap.width * zoomFactor - clientSize.w);
205                   if(bitmap.height * zoomFactor > clientSize.h) 
206                      y = scroll.y / (bitmap.height * zoomFactor - clientSize.h);
207
208                   zoomFactor /= 1.5;
209                   SetScrollArea(
210                      (int)(bitmap.width * zoomFactor), 
211                      (int)(bitmap.height * zoomFactor), false);
212
213                   SetScrollPosition(
214                      (int)(Max(0, bitmap.width * zoomFactor - clientSize.w) * x),
215                      (int)(Max(0, bitmap.height * zoomFactor - clientSize.h) * y));
216
217                   Update(null);
218                }
219                break;
220          }
221       }
222       return true;
223    }
224
225    void OnRedraw(Surface surface)
226    {
227       Bitmap bitmap = this.bitmap;
228       if(bitmap)
229       {
230          surface.blitTint = tint;
231          if(zoom)
232          {
233             int w = (int)(bitmap.width * zoomFactor);
234             int h = (int)(bitmap.height * zoomFactor);
235             if(w == bitmap.width && h == bitmap.height)
236             {
237                surface.Blit(bitmap, 
238                   Max(0, (clientSize.w - w) / 2), Max(0, (clientSize.h - h) / 2), 
239                   scroll.x,scroll.y, w, h);
240             }
241             else
242             {
243                if(filter)
244                   surface.Filter(bitmap, 
245                      Max(0, (clientSize.w - w) / 2), Max(0, (clientSize.h - h) / 2), 
246                      (int)(scroll.x / zoomFactor), (int)(scroll.y / zoomFactor), w, h, 
247                      bitmap.width, bitmap.height);
248                else
249                   surface.Stretch(bitmap, 
250                      Max(0, (clientSize.w - w) / 2), Max(0, (clientSize.h - h) / 2), 
251                      (int)(scroll.x / zoomFactor), (int)(scroll.y / zoomFactor), w, h, 
252                      bitmap.width, bitmap.height);
253             }
254          }
255          else
256          {
257             int w = bitmap.width;
258             int h = bitmap.height;
259             if(clientSize.w == bitmap.width && clientSize.h == bitmap.height)
260                surface.Blit(bitmap, 
261                   Max(0, (clientSize.w - w) / 2), Max(0, (clientSize.h - h) / 2),
262                   scroll.x,scroll.y, 
263                   hasHorzScroll ? clientSize.w : bitmap.width, hasVertScroll ? clientSize.h : bitmap.height);
264             else if(filter)
265                surface.Filter(bitmap, 
266                   Max(0, (clientSize.w - w) / 2), Max(0, (clientSize.h - h) / 2), 
267                   scroll.x,scroll.y, clientSize.w, clientSize.h, 
268                   hasHorzScroll ? clientSize.w : bitmap.width, hasVertScroll ? clientSize.h : bitmap.height);
269             else
270                surface.Stretch(bitmap, 
271                   Max(0, (clientSize.w - w) / 2), Max(0, (clientSize.h - h) / 2),
272                   scroll.x,scroll.y, clientSize.w, clientSize.h,
273                   hasHorzScroll ? clientSize.w : bitmap.width, hasVertScroll ? clientSize.h : bitmap.height);
274          }
275       }
276    }
277 }