sdk: const correctness
[sdk] / ide / src / documents / PictureEdit.ec
index 413f0fa..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
@@ -47,10 +47,11 @@ class PictureEdit : Window
    float zoomFactor;
    char fileName[MAX_LOCATION];
    Bitmap bitmap;
-   
+   Bitmap bitmapNotIndexed;
+
    //saveDialog = pictureEditFileDialog;
-   
-   Menu fileMenu { menu, $"File", f }
+
+   Menu fileMenu { menu, $"File", f };
       MenuItem { fileMenu, $"Save", s, ctrlS, NotifySelect = MenuFileSave };
       MenuItem { fileMenu, $"Save As...", a, NotifySelect = MenuFileSaveAs };
    Menu imageMenu { menu, $"Image", i };
@@ -60,15 +61,22 @@ class PictureEdit : Window
             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;
             }
          };
@@ -77,7 +85,9 @@ class PictureEdit : Window
             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,9 +100,12 @@ class PictureEdit : Window
             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,15 +116,18 @@ class PictureEdit : Window
             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
       {
@@ -125,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
@@ -135,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;
@@ -170,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);
          }
       }
    }
@@ -201,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);
@@ -221,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) };
 
@@ -243,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;
@@ -257,13 +282,18 @@ class PictureEdit : Window
       }
       return result;
    }
-   
+
    PictureEdit()
    {
       zoomFactor = 1.0f;
       return true;
    }
 
+   ~PictureEdit()
+   {
+      delete bitmap;
+      delete bitmapNotIndexed;
+   }
 }
 
 class PictureEditColorTable : Window
@@ -325,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;