CursorFactory.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.Collections.Generic;
  3. using Avalonia.Gtk3.Interop;
  4. using Avalonia.Input;
  5. using Avalonia.Platform;
  6. using Avalonia.Platform.Interop;
  7. using CursorType = Avalonia.Gtk3.GdkCursorType;
  8. namespace Avalonia.Gtk3
  9. {
  10. class CursorFactory : IStandardCursorFactory
  11. {
  12. private static readonly Dictionary<StandardCursorType, object> CursorTypeMapping = new Dictionary
  13. <StandardCursorType, object>
  14. {
  15. {StandardCursorType.None, CursorType.Blank},
  16. {StandardCursorType.AppStarting, CursorType.Watch},
  17. {StandardCursorType.Arrow, CursorType.LeftPtr},
  18. {StandardCursorType.Cross, CursorType.Cross},
  19. {StandardCursorType.Hand, CursorType.Hand1},
  20. {StandardCursorType.Ibeam, CursorType.Xterm},
  21. {StandardCursorType.No, "gtk-cancel"},
  22. {StandardCursorType.SizeAll, CursorType.Sizing},
  23. //{ StandardCursorType.SizeNorthEastSouthWest, 32643 },
  24. {StandardCursorType.SizeNorthSouth, CursorType.SbVDoubleArrow},
  25. //{ StandardCursorType.SizeNorthWestSouthEast, 32642 },
  26. {StandardCursorType.SizeWestEast, CursorType.SbHDoubleArrow},
  27. {StandardCursorType.UpArrow, CursorType.BasedArrowUp},
  28. {StandardCursorType.Wait, CursorType.Watch},
  29. {StandardCursorType.Help, "gtk-help"},
  30. {StandardCursorType.TopSide, CursorType.TopSide},
  31. {StandardCursorType.BottomSize, CursorType.BottomSide},
  32. {StandardCursorType.LeftSide, CursorType.LeftSide},
  33. {StandardCursorType.RightSide, CursorType.RightSide},
  34. {StandardCursorType.TopLeftCorner, CursorType.TopLeftCorner},
  35. {StandardCursorType.TopRightCorner, CursorType.TopRightCorner},
  36. {StandardCursorType.BottomLeftCorner, CursorType.BottomLeftCorner},
  37. {StandardCursorType.BottomRightCorner, CursorType.BottomRightCorner},
  38. {StandardCursorType.DragCopy, CursorType.CenterPtr},
  39. {StandardCursorType.DragMove, CursorType.Fleur},
  40. {StandardCursorType.DragLink, CursorType.Cross},
  41. };
  42. private static readonly Dictionary<StandardCursorType, IPlatformHandle> Cache =
  43. new Dictionary<StandardCursorType, IPlatformHandle>();
  44. private IntPtr GetCursor(object desc)
  45. {
  46. IntPtr rv;
  47. var name = desc as string;
  48. if (name != null)
  49. {
  50. var theme = Native.GtkIconThemeGetDefault();
  51. IntPtr icon, error;
  52. using (var u = new Utf8Buffer(name))
  53. icon = Native.GtkIconThemeLoadIcon(theme, u, 32, 0, out error);
  54. rv = icon == IntPtr.Zero
  55. ? Native.GdkCursorNew(GdkCursorType.XCursor)
  56. : Native.GdkCursorNewFromPixbuf(Native.GdkGetDefaultDisplay(), icon, 0, 0);
  57. }
  58. else
  59. {
  60. rv = Native.GdkCursorNew((CursorType)desc);
  61. }
  62. return rv;
  63. }
  64. public IPlatformHandle GetCursor(StandardCursorType cursorType)
  65. {
  66. IPlatformHandle rv;
  67. if (!Cache.TryGetValue(cursorType, out rv))
  68. {
  69. Cache[cursorType] =
  70. rv =
  71. new PlatformHandle(
  72. GetCursor(CursorTypeMapping[cursorType]),
  73. "GTKCURSOR");
  74. }
  75. return rv;
  76. }
  77. }
  78. }