From 31e6c8ed34dc71b6690c71107d2693c40448335f Mon Sep 17 00:00:00 2001
From: Jeremy Drake <github@jdrake.com>
Date: Mon, 14 Jun 2021 11:43:29 -0700
Subject: [PATCH] WXMSW: use HandleToLong/LongToHandle functions.

On Clang++, casting from a pointer to an integer of a smaller size is
considered an error.  In cases where Windows HANDLEs are converted
to/from longs, use the Windows-provided conversion functions
HandleToLong and LongToHandle.

In a couple of cases, a pointer is being cast to long in a __hash__
function.  These don't seem Windows-specific so it is not safe to assume
the Windows conversion functions are present.  In those cases, fall back
to the (ugly) double-casting that the Windows functions contain.
---
 etg/bitmap.py   | 4 ++--
 etg/cursor.py   | 4 ++--
 etg/dataview.py | 2 +-
 etg/dc.py       | 2 +-
 etg/icon.py     | 6 +++---
 etg/treelist.py | 2 +-
 6 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/etg/bitmap.py b/etg/bitmap.py
index cb349fc9..7b901921 100644
--- a/etg/bitmap.py
+++ b/etg/bitmap.py
@@ -106,7 +106,7 @@ def run():
         doc='MSW-only method to fetch the windows handle for the bitmap.',
         body="""\
             #ifdef __WXMSW__
-                return (long)self->GetHandle();
+                return HandleToLong(self->GetHandle());
             #else
                 return 0;
             #endif
@@ -116,7 +116,7 @@ def run():
         doc='MSW-only method to set the windows handle for the bitmap.',
         body="""\
             #ifdef __WXMSW__
-                self->SetHandle((WXHANDLE)handle);
+                self->SetHandle((WXHANDLE)LongToHandle(handle));
             #endif
             """)
 
diff --git a/etg/cursor.py b/etg/cursor.py
index 67338561..5e221fe9 100644
--- a/etg/cursor.py
+++ b/etg/cursor.py
@@ -51,7 +51,7 @@ def run():
 
     c.addCppMethod('long', 'GetHandle', '()', """\
     #ifdef __WXMSW__
-        return (long)self->GetHandle();
+        return HandleToLong(self->GetHandle());
     #else
         return 0;
     #endif""",
@@ -59,7 +59,7 @@ def run():
 
     c.addCppMethod('void', 'SetHandle', '(long handle)', """\
     #ifdef __WXMSW__
-        self->SetHandle((WXHANDLE)handle);
+        self->SetHandle((WXHANDLE)LongToHandle(handle));
     #endif""",
     briefDoc="Set the handle to use for this Cursor.  Windows only.")
 
diff --git a/etg/dataview.py b/etg/dataview.py
index c00f736d..80eade26 100644
--- a/etg/dataview.py
+++ b/etg/dataview.py
@@ -82,7 +82,7 @@ def run():
 
     c.addCppMethod('int', '__nonzero__', '()', "return self->IsOk();")
     c.addCppMethod('int', '__bool__', '()', "return self->IsOk();")
-    c.addCppMethod('long', '__hash__', '()', "return (long)self->GetID();")
+    c.addCppMethod('long', '__hash__', '()', "return (long)(intptr_t)self->GetID();")
 
     c.addCppMethod('bool', '__eq__', '(wxDataViewItem* other)',
                    "return other ? (self->GetID() == other->GetID()) : false;")
diff --git a/etg/dc.py b/etg/dc.py
index 94e3ab44..0fd9514f 100644
--- a/etg/dc.py
+++ b/etg/dc.py
@@ -257,7 +257,7 @@ def run():
 
     c.addCppMethod('long', 'GetHDC', '()', """\
         #ifdef __WXMSW__
-            return (long)self->GetHandle();
+            return HandleToLong(self->GetHandle());
         #else
             wxPyRaiseNotImplemented();
             return 0;
diff --git a/etg/icon.py b/etg/icon.py
index 9bd18bca..2733ddd6 100644
--- a/etg/icon.py
+++ b/etg/icon.py
@@ -56,7 +56,7 @@ def run():
 
     c.addCppMethod('long', 'GetHandle', '()', """\
         #ifdef __WXMSW__
-            return (long)self->GetHandle();
+            return HandleToLong(self->GetHandle());
         #else
             return 0;
         #endif
@@ -64,7 +64,7 @@ def run():
 
     c.addCppMethod('void', 'SetHandle', '(long handle)', """\
         #ifdef __WXMSW__
-            self->SetHandle((WXHANDLE)handle);
+            self->SetHandle((WXHANDLE)LongToHandle(handle));
         #endif
         """)
 
@@ -73,7 +73,7 @@ def run():
         doc='MSW-only method to create a wx.Icon from a native icon handle.',
         body="""\
             #ifdef __WXMSW__
-                return self->CreateFromHICON((WXHICON)hicon);
+                return self->CreateFromHICON((WXHICON)LongToHandle(hicon));
             #else
                 return false;
             #endif
diff --git a/etg/treelist.py b/etg/treelist.py
index 34f22017..ca57ad65 100644
--- a/etg/treelist.py
+++ b/etg/treelist.py
@@ -47,7 +47,7 @@ def run():
     c.addCppMethod('int', '__bool__', '()', "return self->IsOk();")
 
     c.addCppMethod('long', '__hash__', '()', """\
-        return (long)self->GetID();
+        return (long)(intptr_t)self->GetID();
         """)
 
     c.addCppMethod('bool', '__eq__', '(wxTreeListItem* other)',
-- 
2.32.0.windows.1

