1 // =================================
  2 // Copyright (c) 2025 Seppo Laakko
  3 // Distributed under the MIT license
  4 // =================================
  5 
  6 using System;
  7 using System.Windows.API;
  8 
  9 namespace System.Windows
 10 {
 11     public enum StandardBitmapId : int
 12     {
 13         OBM_BTNCORNERS = 32758, 
 14         OBM_BTSIZE = 32761, 
 15         OBM_CHECK = 32760, 
 16         OBM_CHECKBOXES = 32759, 
 17         OBM_CLOSE = 32754, 
 18         OBM_COMBO = 32738, 
 19         OBM_DNARROW = 32752, 
 20         OBM_DNARROWD = 32742, 
 21         OBM_DNARROWI = 32736, 
 22         OBM_LFARROW = 32750, 
 23         OBM_LFARROWD = 32740, 
 24         OBM_LFARROWI = 32734, 
 25         OBM_MNARROW = 32739, 
 26         OBM_REDUCE = 32749, 
 27         OBM_REDUCED = 32746, 
 28         OBM_RESTORE = 32747, 
 29         OBM_RESTORED = 32744, 
 30         OBM_RGARROW = 32751, 
 31         OBM_RGARROWD = 32741, 
 32         OBM_RGARROWI = 32735, 
 33         OBM_SIZE = 32766, 
 34         OBM_UPARROW = 32753, 
 35         OBM_UPARROWD = 32743, 
 36         OBM_UPARROWI = 32737, 
 37         OBM_ZOOM = 32748, 
 38         OBM_ZOOMD = 32745
 39     }
 40 
 41     public class WinBitmap : Resource
 42     {
 43         public WinBitmap() : bitmapHandle(null), standard(false)
 44         {
 45         }
 46         public explicit WinBitmap(void* bitmapHandle_) : bitmapHandle(bitmapHandle_), standard(false)
 47         {
 48         }
 49         public WinBitmap(void* bitmapHandle_, bool standard_) : bitmapHandle(bitmapHandle_), standard(standard_)
 50         {
 51         }
 52         public WinBitmap(WinBitmap&& that) : bitmapHandle(that.bitmapHandle), standard(that.standard)
 53         {
 54             that.bitmapHandle = null;
 55             that.standard = false;
 56         }
 57         public ~WinBitmap()
 58         {
 59             if (bitmapHandle != null && !standard)
 60             {
 61                 WinDestroyBitmap(bitmapHandle);
 62             }
 63         }
 64         public default void operator=(WinBitmap&&);
 65         suppress WinBitmap(const WinBitmap&);
 66         suppress void operator=(const WinBitmap&);
 67         public void* Handle() const
 68         {
 69             return bitmapHandle;
 70         }
 71         public bool IsStandard() const
 72         {
 73             return standard;
 74         }
 75         public void SetStandard()
 76         {
 77             standard = true;
 78         }
 79         private void* bitmapHandle;
 80         private bool standard;
 81     }
 82 
 83     [nodiscard]
 84     public Result<WinBitmap> LoadStandardBitmap(StandardBitmapId bitmapId)
 85     {
 86         void* bitmapHandle = WinLoadStandardBitmap(cast<int>(bitmapId));
 87         if (bitmapHandle == null)
 88         {
 89             int errorId = WinAllocateWindowsError("could not load standard bitmap", WinGetLastError());
 90             return Result<WinBitmap>(ErrorId(errorId));
 91         }
 92         return WinBitmap(bitmapHandle, true);
 93     }
 94 
 95     [nodiscard]
 96     public Result<WinBitmap> LoadBitmap(const string& bitmapName)
 97     {
 98         auto result = LoadBitmap(bitmapName.Chars());
 99         if (result.Error())
100         {
101             return Result<WinBitmap>(ErrorId(result.GetErrorId()));
102         }
103         return Result<WinBitmap>(WinBitmap(result.Value()));
104     }