1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
|
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Copyright (c) 2004-2010 Novell, Inc.
//
// Authors:
// Peter Bartok pbartok@novell.com
//
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Reflection;
namespace System.Windows.Forms {
[Editor("System.Drawing.Design.CursorEditor, " + Consts.AssemblySystem_Drawing_Design, typeof(System.Drawing.Design.UITypeEditor))]
[Serializable]
[TypeConverter(typeof(CursorConverter))]
public sealed class Cursor : IDisposable, ISerializable {
#region Internal Structs
[StructLayout (LayoutKind.Sequential)]
private struct CursorDir {
internal ushort idReserved; // Reserved
internal ushort idType; // resource type (2 for cursors)
internal ushort idCount; // how many cursors
internal CursorEntry[] idEntries; // the entries for each cursor
};
[StructLayout (LayoutKind.Sequential)]
private struct CursorEntry {
internal byte width; // Width of cursor
internal byte height; // Height of cursor
internal byte colorCount; // colors in cursor
internal byte reserved; // Reserved
internal ushort xHotspot; // Hotspot X
internal ushort yHotspot; // Hotspot Y
internal ushort bitCount; // Bits per pixel
internal uint sizeInBytes; // size of (CursorInfoHeader + ANDBitmap + ORBitmap)
internal uint fileOffset; // position in file
};
[StructLayout(LayoutKind.Sequential)]
private struct CursorInfoHeader {
internal uint biSize;
internal int biWidth;
internal int biHeight;
internal ushort biPlanes;
internal ushort biBitCount;
internal uint biCompression;
internal uint biSizeImage;
internal int biXPelsPerMeter;
internal int biYPelsPerMeter;
internal uint biClrUsed;
internal uint biClrImportant;
};
[StructLayout(LayoutKind.Sequential)]
private struct CursorImage {
internal CursorInfoHeader cursorHeader; // image header
internal uint[] cursorColors; // colors table
internal byte[] cursorXOR; // bits for XOR mask
internal byte[] cursorAND; // bits for AND mask
};
#endregion // Internal structs
#region Local Variables
private static Cursor current;
private CursorDir cursor_dir;
private CursorImage[] cursor_data;
private int id;
internal IntPtr handle;
private Size size;
private Bitmap shape;
private Bitmap mask;
private Bitmap cursor;
internal string name;
private StdCursor std_cursor = (StdCursor) (-1);
private object tag;
#endregion // Local Variables
#region Public Constructors
private void CreateCursor (Stream stream)
{
InitFromStream(stream);
this.shape = ToBitmap(true, false);
this.mask = ToBitmap(false, false);
handle = XplatUI.DefineCursor(shape, mask, Color.FromArgb(255, 255, 255), Color.FromArgb(255, 255, 255), cursor_dir.idEntries[id].xHotspot, cursor_dir.idEntries[id].yHotspot);
this.shape.Dispose();
this.shape = null;
this.mask.Dispose();
this.mask = null;
if (handle != IntPtr.Zero) {
this.cursor = ToBitmap(true, true);
}
}
internal Cursor (StdCursor cursor) : this (XplatUI.DefineStdCursor (cursor))
{
std_cursor = cursor;
}
private Cursor(SerializationInfo info, StreamingContext context)
{
}
private Cursor()
{
}
~Cursor()
{
Dispose();
}
// This is supposed to take a Win32 handle
public Cursor (IntPtr handle)
{
this.handle = handle;
}
public Cursor (Stream stream)
{
CreateCursor(stream);
}
public Cursor (string fileName)
{
using (FileStream fs = File.OpenRead (fileName)) {
CreateCursor (fs);
}
}
public Cursor(Type type, string resource) {
using (Stream s = type.Assembly.GetManifestResourceStream (type, resource)) {
if (s != null) {
CreateCursor (s);
return;
}
}
// Try a different way, previous failed
using (Stream s = Assembly.GetExecutingAssembly ().GetManifestResourceStream (resource)) {
if (s != null) {
CreateCursor (s);
return;
}
}
throw new FileNotFoundException ("Resource name was not found: `" + resource + "'");
}
#endregion // Public Constructors
#region Public Static Properties
public static Rectangle Clip {
get {
IntPtr handle;
bool confined;
Rectangle rect;
Size size;
XplatUI.GrabInfo (out handle, out confined, out rect);
if (handle != IntPtr.Zero) {
return rect;
}
XplatUI.GetDisplaySize (out size);
rect.X = 0;
rect.Y = 0;
rect.Width = size.Width;
rect.Height = size.Height;
return rect;
}
[MonoTODO ("Stub, does nothing")]
[MonoInternalNote ("First need to add ability to set cursor clip rectangle to XplatUI drivers to implement this property")]
set {
;
}
}
public static Cursor Current {
get {
if (current != null)
return current;
return Cursors.Default;
}
set {
if (current == value)
return;
current = value;
if (current == null){
// FIXME - define and set empty cursor
XplatUI.OverrideCursor(IntPtr.Zero);
} else
XplatUI.OverrideCursor(current.handle);
}
}
public static Point Position {
get {
int x;
int y;
XplatUI.GetCursorPos (IntPtr.Zero, out x, out y);
return new Point (x, y);
}
set {
XplatUI.SetCursorPos(IntPtr.Zero, value.X, value.Y);
}
}
#endregion // Public Static Properties
#region Public Instance Properties
public IntPtr Handle {
get {
return handle;
}
}
[MonoTODO ("Implemented for Win32, X11 always returns 0,0")]
public Point HotSpot {
get {
int cursor_w, cursor_h, hot_x, hot_y;
XplatUI.GetCursorInfo (Handle, out cursor_w, out cursor_h, out hot_x, out hot_y);
return new Point (hot_x, hot_y);
}
}
public Size Size {
get {
return size;
}
}
[Localizable (false)]
[Bindable (true)]
[TypeConverter (typeof (StringConverter))]
[DefaultValue (null)]
[MWFCategory ("Data")]
public object Tag {
get { return this.tag; }
set { this.tag = value; }
}
#endregion // Public Instance Properties
#region Public Static Methods
public static void Hide ()
{
XplatUI.ShowCursor(false);
}
public static void Show ()
{
XplatUI.ShowCursor(true);
}
public static bool operator != (Cursor left, Cursor right) {
if ((object)left == (object)right)
return false;
if ((object)left == null || (object)right == null)
return true;
if (left.handle == right.handle)
return false;
return true;
}
public static bool operator ==(Cursor left, Cursor right)
{
if ((object)left == (object)right)
return true;
if ((object)left == null || (object)right == null)
return false;
if (left.handle == right.handle)
return true;
return false;
}
#endregion // Public Static Methods
#region Public Instance Methods
public IntPtr CopyHandle() {
return handle;
}
public void Dispose ()
{
if (cursor != null) {
cursor.Dispose ();
cursor = null;
}
if (shape != null) {
shape.Dispose ();
shape = null;
}
if (mask != null) {
mask.Dispose ();
mask = null;
}
GC.SuppressFinalize (this);
}
public void Draw (Graphics g, Rectangle targetRect)
{
if (cursor == null && std_cursor != (StdCursor) (-1))
cursor = XplatUI.DefineStdCursorBitmap (std_cursor);
if (cursor != null) {
// Size of the targetRect is not considered at all
g.DrawImage (cursor, targetRect.X, targetRect.Y);
}
}
public void DrawStretched (Graphics g, Rectangle targetRect)
{
if (cursor == null && std_cursor != (StdCursor)(-1))
cursor = XplatUI.DefineStdCursorBitmap (std_cursor);
if (cursor != null) {
g.DrawImage (cursor, targetRect, new Rectangle(0, 0, cursor.Width, cursor.Height), GraphicsUnit.Pixel);
}
}
public override bool Equals (object obj)
{
if (!(obj is Cursor))
return false;
if (((Cursor)obj).handle == handle)
return true;
return false;
}
public override int GetHashCode()
{
return base.GetHashCode ();
}
public override string ToString()
{
if (name != null) {
return "[Cursor:" + name + "]";
}
throw new FormatException("Cannot convert custom cursors to string.");
}
void ISerializable.GetObjectData (SerializationInfo si, StreamingContext context)
{
MemoryStream ms;
BinaryWriter wr;
CursorImage ci;
ms = new MemoryStream ();
wr = new BinaryWriter (ms);
ci = cursor_data [this.id];
// Build the headers, first the CursorDir
wr.Write ((ushort) 0); // Reserved
wr.Write ((ushort) 2); // Resource type
wr.Write ((ushort) 1); // Count
// Next the CursorEntry
wr.Write ((byte)cursor_dir.idEntries [this.id].width);
wr.Write ((byte)cursor_dir.idEntries [this.id].height);
wr.Write ((byte)cursor_dir.idEntries [this.id].colorCount);
wr.Write ((byte)cursor_dir.idEntries [this.id].reserved);
wr.Write ((ushort)cursor_dir.idEntries [this.id].xHotspot);
wr.Write ((ushort)cursor_dir.idEntries [this.id].yHotspot);
wr.Write ((uint)(40 + (ci.cursorColors.Length * 4) + ci.cursorXOR.Length + ci.cursorAND.Length));
wr.Write ((uint)(6 + 16)); // CursorDir + CursorEntry size
// Then the CursorInfoHeader
wr.Write (ci.cursorHeader.biSize);
wr.Write (ci.cursorHeader.biWidth);
wr.Write (ci.cursorHeader.biHeight);
wr.Write (ci.cursorHeader.biPlanes);
wr.Write (ci.cursorHeader.biBitCount);
wr.Write (ci.cursorHeader.biCompression);
wr.Write (ci.cursorHeader.biSizeImage);
wr.Write (ci.cursorHeader.biXPelsPerMeter);
wr.Write (ci.cursorHeader.biYPelsPerMeter);
wr.Write (ci.cursorHeader.biClrUsed);
wr.Write (ci.cursorHeader.biClrImportant);
for (int i = 0; i < ci.cursorColors.Length; i++)
wr.Write(ci.cursorColors[i]);
wr.Write (ci.cursorXOR);
wr.Write (ci.cursorAND);
wr.Flush ();
si.AddValue ("CursorData", ms.ToArray ());
}
#endregion // Public Instance Methods
#region Private Methods
private void InitFromStream (Stream stream)
{
ushort entry_count;
CursorEntry ce;
uint largest;
//read the cursor header
if (stream == null || stream.Length == 0)
throw new ArgumentException ("The argument 'stream' must be a picture that can be used as a cursor", "stream");
BinaryReader reader = new BinaryReader (stream);
cursor_dir = new CursorDir ();
cursor_dir.idReserved = reader.ReadUInt16();
cursor_dir.idType = reader.ReadUInt16();
if (cursor_dir.idReserved != 0 || !(cursor_dir.idType == 2 || cursor_dir.idType == 1))
throw new ArgumentException ("Invalid Argument, format error", "stream");
entry_count = reader.ReadUInt16();
cursor_dir.idCount = entry_count;
cursor_dir.idEntries = new CursorEntry[entry_count];
cursor_data = new CursorImage[entry_count];
//now read in the CursorEntry structures
for (int i=0; i < entry_count; i++){
ce = new CursorEntry();
ce.width = reader.ReadByte();
ce.height = reader.ReadByte();
ce.colorCount = reader.ReadByte();
ce.reserved = reader.ReadByte();
ce.xHotspot = reader.ReadUInt16();
ce.yHotspot = reader.ReadUInt16();
if (cursor_dir.idType == 1) {
ce.xHotspot = (ushort)(ce.width / 2);
ce.yHotspot = (ushort)(ce.height / 2);
}
ce.sizeInBytes = reader.ReadUInt32();
ce.fileOffset = reader.ReadUInt32();
cursor_dir.idEntries[i] = ce;
}
// If we have more than one pick the largest cursor
largest = 0;
for (int j = 0; j < entry_count; j++){
if (cursor_dir.idEntries[j].sizeInBytes >= largest) {
largest = cursor_dir.idEntries[j].sizeInBytes;
this.id = (ushort)j;
this.size.Height = cursor_dir.idEntries[j].height;
this.size.Width = cursor_dir.idEntries[j].width;
}
}
//now read in the cursor data
for (int j = 0; j < entry_count; j++) {
CursorImage curdata;
CursorInfoHeader cih;
byte[] buffer;
BinaryReader cih_reader;
int num_colors;
int cursor_height;
int bytes_per_line;
int xor_size;
int and_size;
curdata = new CursorImage();
cih = new CursorInfoHeader();
stream.Seek (cursor_dir.idEntries[j].fileOffset, SeekOrigin.Begin);
buffer = new byte [cursor_dir.idEntries[j].sizeInBytes];
stream.Read (buffer, 0, buffer.Length);
cih_reader = new BinaryReader(new MemoryStream(buffer));
cih.biSize = cih_reader.ReadUInt32 ();
if (cih.biSize != 40) {
throw new ArgumentException ("Invalid cursor file", "stream");
}
cih.biWidth = cih_reader.ReadInt32 ();
cih.biHeight = cih_reader.ReadInt32 ();
cih.biPlanes = cih_reader.ReadUInt16 ();
cih.biBitCount = cih_reader.ReadUInt16 ();
cih.biCompression = cih_reader.ReadUInt32 ();
cih.biSizeImage = cih_reader.ReadUInt32 ();
cih.biXPelsPerMeter = cih_reader.ReadInt32 ();
cih.biYPelsPerMeter = cih_reader.ReadInt32 ();
cih.biClrUsed = cih_reader.ReadUInt32 ();
cih.biClrImportant = cih_reader.ReadUInt32 ();
curdata.cursorHeader = cih;
//Read the number of colors used and corresponding memory occupied by
//color table. Fill this memory chunk into rgbquad[]
switch (cih.biBitCount){
case 1: num_colors = 2; break;
case 4: num_colors = 16; break;
case 8: num_colors = 256; break;
default: num_colors = 0; break;
}
curdata.cursorColors = new uint[num_colors];
for (int i = 0; i < num_colors; i++) {
curdata.cursorColors[i] = cih_reader.ReadUInt32 ();
}
//XOR mask is immediately after ColorTable and its size is
//icon height* no. of bytes per line
//cursor height is half of BITMAPINFOHEADER.biHeight, since it contains
//both XOR as well as AND mask bytes
cursor_height = cih.biHeight/2;
//bytes per line should should be uint aligned
bytes_per_line = ((((cih.biWidth * cih.biPlanes * cih.biBitCount)+ 31)>>5)<<2);
//Determine the XOR array Size
xor_size = bytes_per_line * cursor_height;
curdata.cursorXOR = new byte[xor_size];
for (int i = 0; i < xor_size; i++) {
curdata.cursorXOR[i] = cih_reader.ReadByte();
}
//Determine the AND array size
and_size = (int)(cih_reader.BaseStream.Length - cih_reader.BaseStream.Position);
curdata.cursorAND = new byte[and_size];
for (int i = 0; i < and_size; i++) {
curdata.cursorAND[i] = cih_reader.ReadByte();
}
cursor_data[j] = curdata;
cih_reader.Close();
}
reader.Close();
}
private Bitmap ToBitmap(bool xor, bool transparent)
{
CursorImage ci;
CursorInfoHeader cih;
int ncolors;
Bitmap bmp;
BitmapData bits;
ColorPalette pal;
int biHeight;
int bytesPerLine;
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)(bits.Scan0.ToInt64() + 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));
bmp.Palette = pal;
}
bytesPerLine = (int)((((cih.biWidth * cih.biBitCount) + 31) & ~31) >> 3);
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, bytesPerLine * y, (IntPtr)(bits.Scan0.ToInt64() + bits.Stride * (biHeight - 1 - y)), bytesPerLine);
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
}
}
|