Author: pbartok
Date: 2006-01-18 23:49:12 -0500 (Wed, 18 Jan 2006)
New Revision: 55765

Modified:
   trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/ChangeLog
   trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/Cursor.cs
   trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/XplatUIWin32.cs
Log:
2006-01-18  Peter Dennis Bartok  <[EMAIL PROTECTED]>

        * Cursor.cs: Reimplemented creating cursor bitmaps without using
          the Bitmap(Stream) constructor which is semi-broken on MS GDI+.
          This fixes #77218
        * XplatUIWin32.cs: 
          - Reimplemented creating Bitmaps from DIBs since the Bitmap(Stream) 
            constructor creates images that can't be saved. Part of the fix
            for #76103
          - Added handling of CF_BITMAP as CF_DIB to clipboard code (Fixes 
#76103)
          - SetWindowState: Switched ShowWindow flags (part of an upcoming 
            bug fix for handling window state in forms properly)



Modified: trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/ChangeLog
===================================================================
--- trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/ChangeLog        
2006-01-19 01:52:23 UTC (rev 55764)
+++ trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/ChangeLog        
2006-01-19 04:49:12 UTC (rev 55765)
@@ -1,3 +1,16 @@
+2006-01-18  Peter Dennis Bartok  <[EMAIL PROTECTED]>
+
+       * Cursor.cs: Reimplemented creating cursor bitmaps without using
+         the Bitmap(Stream) constructor which is semi-broken on MS GDI+.
+         This fixes #77218
+       * XplatUIWin32.cs: 
+         - Reimplemented creating Bitmaps from DIBs since the Bitmap(Stream) 
+           constructor creates images that can't be saved. Part of the fix
+           for #76103
+         - Added handling of CF_BITMAP as CF_DIB to clipboard code (Fixes 
#76103)
+         - SetWindowState: Switched ShowWindow flags (part of an upcoming 
+           bug fix for handling window state in forms properly)
+
 2006-01-18  Alexander Olk  <[EMAIL PROTECTED]>
 
        * ThemeGtk.cs: Simplify ScrollBar drawing

Modified: trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/Cursor.cs
===================================================================
--- trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/Cursor.cs        
2006-01-19 01:52:23 UTC (rev 55764)
+++ trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/Cursor.cs        
2006-01-19 04:49:12 UTC (rev 55765)
@@ -28,6 +28,7 @@
 
 using System;
 using System.Drawing;
+using System.Drawing.Imaging;
 using System.IO;
 using System.ComponentModel;
 using System.Runtime.InteropServices;
@@ -506,7 +507,7 @@
                        reader.Close();
                }
 
-               private Bitmap ToBitmap (bool xor, bool transparent) {
+               private Bitmap ToBitmapOld(bool xor, bool transparent) {
                        Bitmap bmp;
 
                        if (cursor_data != null) {
@@ -623,6 +624,104 @@
                        return bmp;
                }
 
+               private Bitmap ToBitmap(bool xor, bool transparent) {
+                       CursorImage             ci;
+                       CursorInfoHeader        cih;
+                       int                     ncolors;
+                       Bitmap                  bmp;
+                       BitmapData              bits;
+                       ColorPalette            pal;
+                       int                     biHeight;
+
+                       if (cursor_data == null) {
+                               return new Bitmap(32, 32);
+                       }
+
+                       ci = cursor_data[this.id];
+                       cih = ci.cursorHeader;
+                       biHeight = cih.biHeight / 2;
+
+                       if (!xor) {
+                               // The AND mask is 1bit - very straightforward
+                               bmp = new Bitmap(cih.biWidth, biHeight, 
PixelFormat.Format1bppIndexed);
+                               pal = bmp.Palette;
+                               pal.Entries[0] = Color.FromArgb(0, 0, 0);
+                               pal.Entries[1] = 
Color.FromArgb(unchecked((int)0xffffffffff));
+                               bits = bmp.LockBits(new Rectangle(0, 0, 
bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);
+
+                               for (int y = 0; y < biHeight; y++) {
+                                       Marshal.Copy(ci.cursorAND, bits.Stride 
* y, (IntPtr)((int)bits.Scan0 + bits.Stride * (biHeight - 1 - y)), bits.Stride);
+                               }
+
+                               bmp.UnlockBits(bits);
+                       } else {
+                               ncolors = (int)cih.biClrUsed;
+                               if (ncolors == 0) {
+                                       if (cih.biBitCount < 24) {
+                                               ncolors = (int)(1 << 
cih.biBitCount);
+                                       }
+                               }
+
+                               switch(cih.biBitCount) {
+                                       case 1: {       // Monochrome
+                                               bmp = new Bitmap(cih.biWidth, 
biHeight, PixelFormat.Format1bppIndexed);
+                                               break;
+                                       }
+
+                                       case 4: {       // 4bpp
+                                               bmp = new Bitmap(cih.biWidth, 
biHeight, PixelFormat.Format4bppIndexed);
+                                               break;
+                                       }
+
+                                       case 8: {       // 8bpp
+                                               bmp = new Bitmap(cih.biWidth, 
biHeight, PixelFormat.Format8bppIndexed);
+                                               break;
+                                       }
+
+                                       case 24:
+                                       case 32: {      // 32bpp
+                                               bmp = new Bitmap(cih.biWidth, 
biHeight, PixelFormat.Format32bppArgb);
+                                               break;
+                                       }
+
+                                       default: {
+                                               throw new Exception("Unexpected 
number of bits:" + cih.biBitCount.ToString());
+                                       }
+                               }
+
+                               if (cih.biBitCount < 24) {
+                                       pal = bmp.Palette;                      
        // Managed palette
+
+                                       for (int i = 0; i < 
ci.cursorColors.Length; i++) {
+                                               pal.Entries[i] = 
Color.FromArgb((int)ci.cursorColors[i] & unchecked((int)0xff000000));
+                                       }
+                               }
+
+                               bits = bmp.LockBits(new Rectangle(0, 0, 
bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);
+
+                               for (int y = 0; y < biHeight; y++) {
+                                       Marshal.Copy(ci.cursorXOR, bits.Stride 
* y, (IntPtr)((int)bits.Scan0 + bits.Stride * (biHeight - 1 - y)), bits.Stride);
+                               }
+                               
+                               bmp.UnlockBits(bits);
+                       }
+
+                       if (transparent) {
+                               bmp = new Bitmap(bmp);  // This makes a 32bpp 
image out of an indexed one
+                               // Apply the mask to make properly transparent
+                               for (int y = 0; y < biHeight; y++) {
+                                       for (int x = 0; x < cih.biWidth / 8; 
x++) {
+                                               for (int bit = 7; bit >= 0; 
bit--) {
+                                                       if (((ci.cursorAND[y * 
cih.biWidth / 8 +x] >> bit) & 1) != 0) {
+                                                               
bmp.SetPixel(x*8 + 7-bit, biHeight - y - 1, Color.Transparent);
+                                                       }
+                                               }
+                                       }
+                               }
+                       }
+
+                       return bmp;
+               }
                #endregion      // Private Methods
        }
 }

Modified: 
trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/XplatUIWin32.cs
===================================================================
--- trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/XplatUIWin32.cs  
2006-01-19 01:52:23 UTC (rev 55764)
+++ trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/XplatUIWin32.cs  
2006-01-19 04:49:12 UTC (rev 55765)
@@ -715,31 +715,21 @@
                }
 
                internal static Image DIBtoImage(IntPtr dib_data) {
-                       MemoryStream            ms;
-                       byte[]                  header;
-                       byte[]                  buffer;
-                       Bitmap                  bmp;
                        BITMAPINFOHEADER        bmi;
                        int                     ncolors;
+                       int                     imagesize;
                        int                     palettesize;
-                       int                     imagesize;
-                       int                     size;
-                       int                     offset;
+                       Bitmap                  bmp;
+                       BitmapData              bits;
+                       ColorPalette            pal;
+                       int[]                   palette;
+                       byte[]                  imagebits;
 
-                       header = new byte[54];  // Size of a BMP file header, 
without palette
-                       // Grab the header
-                       header[0] = (byte)'B';
-                       header[1] = (byte)'M';
-                       // 2, 3, 4 and 5 = unsigned int size
-                       // 6, 7, 8 and 9 = reserved
-                       // 10, 11, 12 and 13 = offset to image data
-
-                       // Create a fake BMP header
                        bmi = 
(BITMAPINFOHEADER)Marshal.PtrToStructure(dib_data, typeof(BITMAPINFOHEADER));
 
                        ncolors = (int)bmi.biClrUsed;
                        if (ncolors == 0) {
-                               if (bmi.biBitCount != 24) {
+                               if (bmi.biBitCount < 24) {
                                        ncolors = (int)(1 << bmi.biBitCount);
                                }
                        }
@@ -750,30 +740,60 @@
                                imagesize = (int)(((((bmi.biWidth * 
bmi.biBitCount) + 31) & ~31) >> 3) * bmi.biHeight);
                        }
 
-                       size = 54 + palettesize + imagesize;
-                       offset = 54 + palettesize;
-                       buffer = new byte[size];
+                       switch(bmi.biBitCount) {
+                               case 1: {       // Monochrome
+                                       bmp = new Bitmap(bmi.biWidth, 
bmi.biHeight, PixelFormat.Format1bppIndexed);
+                                       palette = new int[2];
+                                       break;
+                               }
 
-                       // Copy the fake BMP file header
-                       header[2] = (byte)size;
-                       header[3] = (byte)(size >> 8);
-                       header[4] = (byte)(size >> 16);
-                       header[5] = (byte)(size >> 24);
+                               case 4: {       // 4bpp
+                                       bmp = new Bitmap(bmi.biWidth, 
bmi.biHeight, PixelFormat.Format4bppIndexed);
+                                       palette = new int[16];
+                                       break;
+                               }
 
-                       header[10] = (byte)offset;
-                       header[11] = (byte)(offset >> 8);
-                       header[12] = (byte)(offset >> 16);
-                       header[13] = (byte)(offset >> 24);
+                               case 8: {       // 8bpp
+                                       bmp = new Bitmap(bmi.biWidth, 
bmi.biHeight, PixelFormat.Format8bppIndexed);
+                                       palette = new int[256];
+                                       break;
+                               }
 
-                       Array.Copy(header, 0, buffer, 0, 14);
+                               case 24:
+                               case 32: {      // 32bpp
+                                       bmp = new Bitmap(bmi.biWidth, 
bmi.biHeight, PixelFormat.Format32bppArgb);
+                                       palette = new int[0];
+                                       break;
+                               }
 
-                       for (int i = 14; i < size; i++) {
-                               buffer[i] = Marshal.ReadByte(dib_data, i - 14);
+                               default: {
+                                       throw new Exception("Unexpected number 
of bits:" + bmi.biBitCount.ToString());
+                               }
                        }
 
-                       ms = new MemoryStream(buffer, 0, size, false);
-                       bmp = new Bitmap(ms);
-                       ms.Close();
+                       if (bmi.biBitCount < 24) {
+                               pal = bmp.Palette;                              
// Managed palette
+                               Marshal.Copy((IntPtr)((int)dib_data + 
Marshal.SizeOf(typeof(BITMAPINFOHEADER))), palette, 0, palette.Length);
+
+                               for (int i = 0; i < ncolors; i++) {
+                                       pal.Entries[i] = 
Color.FromArgb(palette[i] & unchecked((int)0xff000000));
+                               }
+                       }
+
+                       bits = bmp.LockBits(new Rectangle(0, 0, bmp.Width, 
bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);
+
+                       imagebits = new byte[bits.Stride];
+
+                       for (int y = 0; y < bmi.biHeight; y++) {
+                               // Copy from source to managed
+                               Marshal.Copy((IntPtr)((int)dib_data + 
Marshal.SizeOf(typeof(BITMAPINFOHEADER)) + palette.Length * 4 + bits.Stride * 
y), imagebits, 0, bits.Stride);
+
+                               // Copy from managed to dest
+                               Marshal.Copy(imagebits, 0, 
(IntPtr)((int)bits.Scan0 + bits.Stride * (bmi.biHeight - 1 - y)), 
imagebits.Length);
+                       }
+
+                       bmp.UnlockBits(bits);
+
                        return bmp;
                }
 
@@ -1022,7 +1042,6 @@
                        IntPtr  WindowHandle;
                        IntPtr  ParentHandle;
                        Hwnd    hwnd;
-                       IntPtr  lParam;
 
                        hwnd = new Hwnd();
 
@@ -1039,18 +1058,8 @@
                                ParentHandle = FosterParent;
                        }
 
-                       lParam = IntPtr.Zero;
-                       if (cp.Param != null && cp.Param is CLIENTCREATESTRUCT) 
{
-                               lParam = 
Marshal.AllocHGlobal(Marshal.SizeOf(cp.Param));
-                               Marshal.StructureToPtr(cp.Param, lParam, false);
-                       }
+                       WindowHandle = Win32CreateWindow((uint)cp.ExStyle, 
cp.ClassName, cp.Caption, (uint)cp.Style, cp.X, cp.Y, cp.Width, cp.Height, 
ParentHandle, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
 
-                       WindowHandle = Win32CreateWindow((uint)cp.ExStyle, 
cp.ClassName, cp.Caption, (uint)cp.Style, cp.X, cp.Y, cp.Width, cp.Height, 
ParentHandle, IntPtr.Zero, IntPtr.Zero, lParam);
-
-                       if (lParam != IntPtr.Zero) {
-                               Marshal.FreeHGlobal(lParam);
-                       }
-
                        if (WindowHandle==IntPtr.Zero) {
                                uint error = Win32GetLastError();
 
@@ -1106,17 +1115,17 @@
                internal override void SetWindowState(IntPtr hwnd, 
FormWindowState state) {
                        switch(state) {
                                case FormWindowState.Normal: {
-                                       Win32ShowWindow(hwnd, 
WindowPlacementFlags.SW_SHOWNORMAL);
+                                       Win32ShowWindow(hwnd, 
WindowPlacementFlags.SW_RESTORE);
                                        return;
                                }
 
                                case FormWindowState.Minimized: {
-                                       Win32ShowWindow(hwnd, 
WindowPlacementFlags.SW_SHOWMINIMIZED);
+                                       Win32ShowWindow(hwnd, 
WindowPlacementFlags.SW_MINIMIZE);
                                        return;
                                }
 
                                case FormWindowState.Maximized: {
-                                       Win32ShowWindow(hwnd, 
WindowPlacementFlags.SW_SHOWMAXIMIZED);
+                                       Win32ShowWindow(hwnd, 
WindowPlacementFlags.SW_MAXIMIZE);
                                        return;
                                }
                        }
@@ -2047,6 +2056,7 @@
                                        return;
                                }
 
+                               case ClipboardFormats.CF_BITMAP:
                                case ClipboardFormats.CF_DIB: {
                                        data = ImageToDIB((Image)obj);
 
@@ -2054,7 +2064,7 @@
                                        hmem_ptr = Win32GlobalLock(hmem);
                                        Marshal.Copy(data, 0, hmem_ptr, 
data.Length);
                                        Win32GlobalUnlock(hmem);
-                                       Win32SetClipboardData((uint)type, hmem);
+                                       
Win32SetClipboardData((uint)ClipboardFormats.CF_DIB, hmem);
                                        return;
                                }
 

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to