sdk: const correctness
[sdk] / ide / src / documents / PictureEdit.ec
index e07b83b..ac8b790 100644 (file)
@@ -3,7 +3,7 @@
 
    Copyright (c) 2003 Jerome Jacovella-St-Louis
    All Rights Reserved.
-   
+
    pictureEdit.ec - Picture Editor Control
 ****************************************************************************/
 #ifdef ECERE_STATIC
@@ -16,26 +16,23 @@ import "ecere"
 #define ID_IMAGE_MODE_INDEXED    10
 #define ID_IMAGE_MODE_RGB        11
 
-static FileFilter filters[] =
-{
-   {
-      "Image Files (*.jpg, *.jpeg, *.bmp, *.pcx, *.png, *.gif)",
-      "jpg, jpeg, bmp, pcx, png, gif"
-   },
-   { "All files", null }
-};
+static Array<FileFilter> filters
+{ [
+   { $"Image Files (*.jpg, *.jpeg, *.bmp, *.pcx, *.png, *.gif)", "jpg, jpeg, bmp, pcx, png, gif" },
+   { $"All files", null }
+] };
 
-static FileType types[] =
-{
-   { "Based on extension", null,  never },
-   { "JPG Image",          "jpg", always },
-   { "BMP Image",          "bmp", always },
-   { "PCX Image",          "pcx", always },
-   { "PNG Image",          "png", always },
-   { "GIF Image",          "gif", always }
-};
+static Array<FileType> types
+{ [
+   { $"Based on extension", null,  never },
+   { $"JPG Image",          "jpg", always },
+   { $"BMP Image",          "bmp", always },
+   { $"PCX Image",          "pcx", always },
+   { $"PNG Image",          "png", always },
+   { $"GIF Image",          "gif", always }
+};
 
-FileDialog pictureEditFileDialog { filters = filters, sizeFilters = sizeof(filters), types = types, sizeTypes = sizeof(types) };
+FileDialog pictureEditFileDialog { filters = filters.array, sizeFilters = filters.count * sizeof(FileFilter), types = types.array, sizeTypes = types.count * sizeof(FileType) };
 
 class PictureEdit : Window
 {
@@ -50,37 +47,47 @@ class PictureEdit : Window
    float zoomFactor;
    char fileName[MAX_LOCATION];
    Bitmap bitmap;
-   
+   Bitmap bitmapNotIndexed;
+
    //saveDialog = pictureEditFileDialog;
-   
-   Menu fileMenu { menu, "File", f }
-      MenuItem { fileMenu, "Save", s, ctrlS, NotifySelect = MenuFileSave };
-      MenuItem { fileMenu, "Save As...", a, NotifySelect = MenuFileSaveAs };
-   Menu imageMenu { menu, "Image", i };
-      Menu modeMenu { imageMenu, "Mode", m };
+
+   Menu fileMenu { menu, $"File", f };
+      MenuItem { fileMenu, $"Save", s, ctrlS, NotifySelect = MenuFileSave };
+      MenuItem { fileMenu, $"Save As...", a, NotifySelect = MenuFileSaveAs };
+   Menu imageMenu { menu, $"Image", i };
+      Menu modeMenu { imageMenu, $"Mode", m };
          MenuItem imageModeIndexedItem
          {
-            modeMenu, "Indexed Color...", i, isRadio = true;
+            modeMenu, $"Indexed Color...", i, isRadio = true;
             bool NotifySelect(MenuItem selection, Modifiers mods)
             {
-               ColorAlpha * palette = bitmap.Quantize(0, 255);
-               /*
-               eBitmap_Convert(null, bitmap, PixelFormat8, palette);
-               bitmap.allocatePalette = true;
-               */               
-               
-               imageModeColorTableItem.disabled = false;
-               Update(null);
-               modifiedDocument = true;
+               if(bitmap)
+               {
+                  ColorAlpha * palette = bitmap.Quantize(0, 255);
+                  bitmapNotIndexed = { };
+                  bitmapNotIndexed.Copy(bitmap);
+                  bitmapNotIndexed.Convert(null, pixelFormat888, null);
+
+                  /*
+                  eBitmap_Convert(null, bitmap, PixelFormat8, palette);
+                  bitmap.allocatePalette = true;
+                  */
+
+                  imageModeColorTableItem.disabled = false;
+                  Update(null);
+                  modifiedDocument = true;
+               }
                return true;
             }
          };
          MenuItem imageModeRGBItem
          {
-            modeMenu, "RGB Color", r, isRadio = true;
+            modeMenu, $"RGB Color", r, isRadio = true;
             bool NotifySelect(MenuItem selection, Modifiers mods)
             {
-               bitmap.Convert(null, pixelFormat888, null);
+               if(bitmap)
+                  bitmap.Convert(null, pixelFormat888, null);
+               delete bitmapNotIndexed;
                imageModeColorTableItem.disabled = true;
                Update(null);
                modifiedDocument = true;
@@ -90,12 +97,15 @@ class PictureEdit : Window
          MenuDivider { modeMenu };
          MenuItem imageModeColorTableItem
          {
-            modeMenu, "Color Table", r;
+            modeMenu, $"Color Table", r;
             bool NotifySelect(MenuItem selection, Modifiers mods)
             {
-               PictureEditColorTable colorTable { master = this };
-               colorTable.Modal();
-               Update(null);
+               if(bitmap)
+               {
+                  PictureEditColorTable colorTable { master = this };
+                  colorTable.Modal();
+                  Update(null);
+               }
                return true;
             }
          };
@@ -103,18 +113,21 @@ class PictureEdit : Window
          MenuDivider { imageMenu };
          MenuItem adjustHSVItem
          {
-            imageMenu, "Adjust Hue, Saturation, Value", h;
+            imageMenu, $"Adjust Hue, Saturation, Value", h;
             bool NotifySelect(MenuItem selection, Modifiers mods)
             {
-               AdjustHSV adjustHSV { master = this };
-               adjustHSV.Modal();
-               Update(null);
+               if(bitmap)
+               {
+                  AdjustHSV adjustHSV { master = this };
+                  adjustHSV.Modal();
+                  Update(null);
+               }
                return true;
             }
          };
          #endif
-   
-   property char * bitmapFile
+
+   property const char * bitmapFile
    {
       set
       {
@@ -128,8 +141,16 @@ class PictureEdit : Window
             if(bitmap.Load(fileName, null, null))
             {
                if(bitmap.pixelFormat == pixelFormatRGBA)
+               {
+                  bitmap.alphaBlend = true;
                   bitmap.Convert(null, pixelFormat888, null);
-
+               }
+               if(bitmap.pixelFormat == pixelFormat8)
+               {
+                  bitmapNotIndexed = { };
+                  bitmapNotIndexed.Copy(bitmap);
+                  bitmapNotIndexed.Convert(null, pixelFormat888, null);
+               }
                //if(!eWindow_GetStartWidth(window) || !eWindow_GetStartHeight(window))
                {
                   Size size = initSize;  // what's the use of retrieving initSize
@@ -138,18 +159,18 @@ class PictureEdit : Window
                   clientSize = size;
 
                   /*
-                  Move(eWindow_GetStartX(window), eWindow_GetStartY(window), 
-                     (!eWindow_GetStartWidth(window)) ? (A_CLIENT|bitmap.width) : eWindow_GetStartWidth(window), 
+                  Move(eWindow_GetStartX(window), eWindow_GetStartY(window),
+                     (!eWindow_GetStartWidth(window)) ? (A_CLIENT|bitmap.width) : eWindow_GetStartWidth(window),
                      (!eWindow_GetStartHeight(window)) ? (A_CLIENT|bitmap.height) : eWindow_GetStartHeight(window));
                   */
 
                   /*
-                  Move(eWindow_GetStartX(window), eWindow_GetStartY(window), 
-                     (!) ? (A_CLIENT|bitmap.width) : eWindow_GetStartWidth(window), 
+                  Move(eWindow_GetStartX(window), eWindow_GetStartY(window),
+                     (!) ? (A_CLIENT|bitmap.width) : eWindow_GetStartWidth(window),
                      (!eWindow_GetStartHeight(window)) ? (A_CLIENT|bitmap.height) : eWindow_GetStartHeight(window));
                   */
                }
-               scrollArea = Size {bitmap.width, bitmap.height };
+               scrollArea = Size { bitmap.width, bitmap.height };
             }
             else
                delete bitmap;
@@ -173,22 +194,23 @@ class PictureEdit : Window
 
    void OnRedraw(Surface surface)
    {
-      if(bitmap)
+      Bitmap bmp = (bitmapNotIndexed && displaySystem.pixelFormat != pixelFormat8) ? bitmapNotIndexed : bitmap;
+      if(bmp)
       {
-         int w = (int)(bitmap.width * zoomFactor);
-         int h = (int)(bitmap.height * zoomFactor);
-         if(w == bitmap.width && h == bitmap.height)
+         int w = (int)(bmp.width * zoomFactor);
+         int h = (int)(bmp.height * zoomFactor);
+         if(w == bmp.width && h == bmp.height)
          {
-            surface.Blit(bitmap, 
-               Max(0, (clientSize.w - w) / 2), Max(0, (clientSize.h - h) / 2), 
+            surface.Blit(bmp,
+               Max(0, (clientSize.w - w) / 2), Max(0, (clientSize.h - h) / 2),
                scroll.x, scroll.y, w, h);
          }
          else
          {
-            surface.Filter(bitmap, 
-               Max(0, (clientSize.w - w) / 2), Max(0, (clientSize.h - h) / 2), 
-               (int)(scroll.x / zoomFactor), (int)(scroll.y / zoomFactor), w, h, 
-               bitmap.width, bitmap.height);
+            surface.Filter(bmp,
+               Max(0, (clientSize.w - w) / 2), Max(0, (clientSize.h - h) / 2),
+               (int)(scroll.x / zoomFactor), (int)(scroll.y / zoomFactor), w, h,
+               bmp.width, bmp.height);
          }
       }
    }
@@ -204,19 +226,19 @@ class PictureEdit : Window
       {
          case equal:
          case keyPadPlus:
-            if(zoomFactor < 25)
+            if(bitmap && zoomFactor < 25)
             {
                float x = 0.5f, y = 0.5f;
-               if(bitmap.width * zoomFactor > clientSize.w) 
+               if(bitmap.width * zoomFactor > clientSize.w)
                   x = scroll.x / (bitmap.width * zoomFactor - clientSize.w);
-               if(bitmap.height * zoomFactor > clientSize.h) 
+               if(bitmap.height * zoomFactor > clientSize.h)
                   y = scroll.y / (bitmap.height * zoomFactor - clientSize.h);
 
                zoomFactor *= 1.5;
                scrollArea = Size { bitmap.width * zoomFactor,  bitmap.height * zoomFactor };
 
-               scroll = Point { 
-                     (int)(Max(0, bitmap.width * zoomFactor - clientSize.w) * x), 
+               scroll = Point {
+                     (int)(Max(0, bitmap.width * zoomFactor - clientSize.w) * x),
                      (int)(Max(0, bitmap.height * zoomFactor - clientSize.h) * y) };
 
                Update(null);
@@ -224,18 +246,18 @@ class PictureEdit : Window
             break;
          case minus:
          case keyPadMinus:
-            if(zoomFactor > 0.05)
+            if(bitmap && zoomFactor > 0.05)
             {
                float x = 0.5f, y = 0.5f;
-               if(bitmap.width * zoomFactor > clientSize.w) 
+               if(bitmap.width * zoomFactor > clientSize.w)
                   x = scroll.x / (bitmap.width * zoomFactor - clientSize.w);
-               if(bitmap.height * zoomFactor > clientSize.w) 
+               if(bitmap.height * zoomFactor > clientSize.w)
                   y = scroll.y / (bitmap.height * zoomFactor - clientSize.h);
 
                zoomFactor /= 1.5;
                scrollArea = Size { bitmap.width * zoomFactor, bitmap.height * zoomFactor };
 
-               scroll = Point { 
+               scroll = Point {
                      (int)(Max(0, bitmap.width * zoomFactor - clientSize.w) * x),
                      (int)(Max(0, bitmap.height * zoomFactor - clientSize.h) * y) };
 
@@ -246,12 +268,12 @@ class PictureEdit : Window
       return true;
    }
 
-   bool OnSaveFile(char * fileName)
+   bool OnSaveFile(const char * fileName)
    {
       bool result = false;
       if(bitmap)
       {
-         if(bitmap.Save(fileName, 
+         if(bitmap.Save(fileName,
             ((FileType *)pictureEditFileDialog.types)[pictureEditFileDialog.fileType].typeExtension, (void *) bool::true))
          {
             modifiedDocument = false;
@@ -260,25 +282,30 @@ class PictureEdit : Window
       }
       return result;
    }
-   
+
    PictureEdit()
    {
       zoomFactor = 1.0f;
       return true;
    }
 
+   ~PictureEdit()
+   {
+      delete bitmap;
+      delete bitmapNotIndexed;
+   }
 }
 
 class PictureEditColorTable : Window
 {
    hasClose = true;
-   text = "Color Table";
-   background = activeBorder;
+   text = $"Color Table";
+   background = formColor;
    minClientSize = Size { 400, 400 };
 
    Button button
    {
-      parent = this, hotKey = escape, size = { 80 }, text = "Close";
+      parent = this, hotKey = escape, size = { 80 }, text = $"Close";
       anchor = Anchor { right = 10, bottom = 10 };
       NotifyClicked = ButtonCloseDialog;
    };
@@ -309,7 +336,7 @@ class AdjustHSV : Window
 {
    size = { 400, 300 };
 
-   background = activeBorder;
+   background = formColor;
    ColorHSV target;
    ColorHSV replace;
    replace = { 248, 100, 71 }; //Color { 26, 0, 183 };
@@ -318,7 +345,7 @@ class AdjustHSV : Window
 
    Button button1
    {
-      this, text = "Go", position = { 296, 104 }, isDefault = true;
+      this, text = $"Go", position = { 296, 104 }, isDefault = true;
 
       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
       {
@@ -328,7 +355,7 @@ class AdjustHSV : Window
          double tolH = 1;
          double tolS = 1;
          double tolV = 1;
-         
+
          h = target.h - replace.h;
          s = target.s / replace.s;
          v = target.v / replace.v;