ecere/gui/Window: Prevent uninitialized values if base Window methods not overridden...
[sdk] / ecere / src / gfx / BitmapResource.ec
1 namespace gfx;
2
3 import "Window"
4
5 static Array<FileFilter> filters
6 { [
7    {
8       $"Image Files (*.jpg, *.jpeg, *.bmp, *.pcx, *.png, *.gif)",
9       "jpg, jpeg, bmp, pcx, png, gif"
10    }
11 ] };
12 static Array<FileType> types
13 { [
14    { $"Image",              "jpg" }
15 ] };
16
17 static FileDialog fileDialog { autoCreate = false, filters = filters.array, sizeFilters = filters.count * sizeof(FileFilter), types = types.array, sizeTypes = types.count * sizeof(FileType), text = $"Select Image" };
18
19 public class BitmapResource : Resource
20 {
21    char * fileName;
22    Bitmap bitmap;
23    bool grayed, mono, transparent;
24    bool alphaBlend;
25    bool mipMaps;
26    int count;
27    bool keepData;
28
29    BitmapResource()
30    {
31       transparent = true;
32    }
33
34    ~BitmapResource()
35    {
36       if(bitmap != null)
37       {
38          //if(!grayed)
39             // Logf("Freeing %s (%d)\n", fileName, bitmap.picture);
40          // bitmap.Free(bitmap);
41          delete bitmap;
42       }
43       if(fileName)
44          delete fileName;
45    }
46
47    void Load(BitmapResource copy, DisplaySystem displaySystem)
48    {
49       delete fileName;
50       fileName = CopyString(copy.fileName);
51       grayed = copy.grayed;
52       mono = copy.mono;
53       transparent = copy.transparent;
54       alphaBlend = copy.alphaBlend;
55       mipMaps = copy.mipMaps;
56       keepData = copy.keepData;
57
58       if(fileName)
59       {
60          DisplaySystem ds = displaySystem;
61
62          bitmap = Bitmap { alphaBlend = (alphaBlend && displaySystem.pixelFormat != pixelFormat8), keepData = keepData };
63 #if defined(__WIN32__)
64          // if(bitmap.alphaBlend) ds = null;
65 #endif
66          if(
67             (mipMaps && !bitmap.LoadMipMaps(fileName, null, ds)) ||
68             (!mipMaps && mono && !bitmap.LoadMonochrome(fileName, null, ds)) ||
69             (!mipMaps && !mono && grayed && !bitmap.LoadGrayed(fileName, null, ds)) ||
70             (!mipMaps && !mono && !grayed && transparent && !bitmap.LoadT(fileName, null, ds)) ||
71             (!mipMaps && !mono && !grayed && !transparent && !bitmap.Load(fileName, null, ds)))
72                delete bitmap;
73          if(bitmap && bitmap.alphaBlend)
74          {
75             bitmap.Convert(null, pixelFormat888, null);
76             bitmap.displaySystem = displaySystem;
77          }
78       }
79    }
80
81    void Reference(BitmapResource reference)
82    {
83       bitmap = reference.bitmap;
84       count++;
85    }
86
87    void Dereference()
88    {
89       count--;
90       if(!count)
91          bitmap = null;
92    }
93
94    Window OnEdit(DataBox window, DataBox master, int x, int y, int w, int h, void * userData)
95    {
96       Window editData = class::OnEdit(window, master, x + 24, y, w - 48, h, userData);
97       Button browse
98       {
99          window, master = editData, inactive = true, text = "...", hotKey = f2,
100          position = { Max(x + 24, x + w - 24), y }, size = { 24, h };
101
102          bool NotifyClicked(Button button, int x, int y, Modifiers mods)
103          {
104             DataBox master = (DataBox)this.master;
105             fileDialog.master = rootWindow;
106             if(fileDialog.Modal() == ok)
107             {
108                const char * filePath = fileDialog.filePath;
109                BitmapResource resource { fileName = filePath };
110
111                master.SetData(resource, false);
112                // TOCHECK: Why do we need to Refresh here?
113                master.Refresh();
114
115                // TODO: This is a Button?
116                // contents = filePath;
117             }
118             return true;
119          }
120       };
121       editData.anchor.right = 24;
122       browse.Create();
123
124       return editData;
125    }
126
127    void OnDisplay(Surface surface, int x, int y, int width, void * fieldData, Alignment alignment, DataDisplayFlags displayFlags)
128    {
129       char * string = this ? fileName : null;
130       Bitmap bitmap = this ? this.bitmap : null;
131       // TODO: This isn't an ideal way of obtaining the clipped height, will fail on hidden areas
132       int yOffset = (1+surface.box.bottom - surface.box.top - 17)/2;
133
134 /* GCC-4.4 Bug!!
135       if(!string) string = "(none)";
136       surface.WriteTextDots(alignment, x + 24, y + 1, width - 24, string, strlen(string));
137 */
138       if(string)
139          surface.WriteTextDots(alignment, x + 24, y + 1, width - 24, string, strlen(string));
140       else
141          surface.WriteTextDots(alignment, x + 24, y + 1, width - 24, "(none)", 6);
142
143       y += yOffset-1;
144       surface.SetBackground(white);
145       surface.Area(x - 4, y, x + 20, y + 15);
146       if(bitmap)
147          surface.Filter(bitmap, x, y + 2, 0,0, 18, 12, bitmap.width, bitmap.height);
148
149       surface.SetForeground(black);
150       surface.Rectangle(x-1, y + 1, x + 18, y + 14);
151    }
152
153 public:
154    property const char * fileName
155    {
156       set
157       {
158          delete fileName;
159          fileName = CopyString(value);
160          if(value && SearchString(value, 0, ".png", false, true))
161             alphaBlend = true;
162       }
163       get { return this ? fileName : null; }
164    };
165    property bool grayed { set { grayed = value; } get { return this ? grayed : false; } };
166    property bool monochrome
167    {
168       set
169       {
170          mono = value;
171       }
172       get { return this ? mono : false; }
173    };
174    property bool transparent { set { transparent = value; } get { return this ? transparent : false; } isset { return (this && !transparent) ? true : false; } };
175    property bool alphaBlend
176    {
177       set { alphaBlend = value; }
178       get { return this ? alphaBlend : false; }
179       isset { return alphaBlend && (!fileName || !SearchString(fileName, 0, ".png", false, true)); }
180    };
181    property bool mipMaps
182    {
183       set { mipMaps = value; }
184       get { return this ? mipMaps : false; }
185       isset { return mipMaps; }
186    };
187    property bool keepData { set { keepData = value; } get { return this ? keepData : false; } };
188    property Bitmap bitmap { get { return this ? bitmap : null; } set { bitmap = value; if(bitmap) incref bitmap; } };
189    property Window window { set { if(value) value.AddResource(this); } };
190 };