Merge branch 'palette' of https://github.com/swt2c/Phoenix into swt2c-palette

This commit is contained in:
Robin Dunn
2016-08-04 19:47:54 -07:00
6 changed files with 172 additions and 3 deletions

View File

@@ -131,7 +131,6 @@ Other Dev Stuff
* dialup ??
* docmdi ??
* docview ??
* palette ??
* persist ??
* Add a _msw module that will contain classes and such that are only

View File

@@ -661,7 +661,7 @@
"DirPickerWidgetNameStr":"wx.",
"DirSelector":"wx.",
"DirSelectorPromptStr":"wx.",
"Direction":"wx.DataObject.",
"Direction":"wx.",
"DisableAsserts":"wx.",
"Display":"wx.",
"DisplayChangedEvent":"wx.",
@@ -2205,6 +2205,7 @@
"NullIcon":"wx.",
"NullIconBundle":"wx.",
"NullImage":"wx.",
"NullPalette":"wx.",
"NullPen":"wx.",
"NullRegion":"wx.",
"ODCB_DCLICK_CYCLES":"wx.adv.",
@@ -2460,6 +2461,7 @@
"PageSetupDialogData":"wx.",
"PaintDC":"wx.",
"PaintEvent":"wx.",
"Palette":"wx.",
"PaletteChangedEvent":"wx.",
"Panel":"wx.",
"PanelNameStr":"wx.",

View File

@@ -94,6 +94,7 @@ INCLUDES = [ # base and core stuff
'graphics',
'imaglist',
'overlay',
'palette',
'renderer',
'rawbmp',

View File

@@ -84,7 +84,6 @@ def run():
# TODO: Add these classes for real :-)
module.insertItem(0, etgtools.WigCode("""\
// forward declarations
class wxPalette;
class wxExecuteEnv;
"""))

127
etg/palette.py Normal file
View File

@@ -0,0 +1,127 @@
#---------------------------------------------------------------------------
# Name: etg/palette.py
# Author: Scott Talbert
#
# Created: 31-Jul-2016
# Copyright: (c) 2016 by Total Control Software
# License: wxWindows License
#---------------------------------------------------------------------------
import etgtools
import etgtools.tweaker_tools as tools
PACKAGE = "wx"
MODULE = "_core"
NAME = "palette" # Base name of the file to generate to for this script
DOCSTRING = ""
# The classes and/or the basename of the Doxygen XML files to be processed by
# this script.
ITEMS = [ 'wxPalette' ]
#---------------------------------------------------------------------------
def run():
# Parse the XML file(s) building a collection of Extractor objects
module = etgtools.ModuleDef(PACKAGE, MODULE, NAME, DOCSTRING)
etgtools.parseDoxyXML(module, ITEMS)
#-----------------------------------------------------------------
# Tweak the parsed meta objects in the module object as needed for
# customizing the generated code and docstrings.
c = module.find('wxPalette')
c.find('GetPixel.red').pyInt = True
c.find('GetPixel.green').pyInt = True
c.find('GetPixel.blue').pyInt = True
c.find('GetRGB').ignore()
c.addCppMethod('PyObject*', 'GetRGB', '(int pixel)',
doc="Returns RGB values for a given palette index.",
body="""\
unsigned char red;
unsigned char green;
unsigned char blue;
wxPyThreadBlocker blocker;
if (!self->GetRGB(pixel, &red, &green, &blue)) {
PyErr_SetString(PyExc_IndexError, "Pixel out of range");
return NULL;
}
PyObject* rv = PyTuple_New(3);
PyTuple_SetItem(rv, 0, wxPyInt_FromLong(red));
PyTuple_SetItem(rv, 1, wxPyInt_FromLong(green));
PyTuple_SetItem(rv, 2, wxPyInt_FromLong(blue));
return rv;
""")
c.find('Create').ignore()
c.addCppMethod('PyObject*', 'Create', '(PyObject* red, PyObject* green, PyObject* blue)',
doc="Creates a palette from tuples of integers, one for each red, blue or green component.",
body="""\
wxPyThreadBlocker blocker;
char* errMsg = "Expected a tuple of integer objects";
if (!PyTuple_Check(red)) {
PyErr_SetString(PyExc_TypeError, errMsg);
return NULL;
}
if (!PyTuple_Check(green)) {
PyErr_SetString(PyExc_TypeError, errMsg);
return NULL;
}
if (!PyTuple_Check(blue)) {
PyErr_SetString(PyExc_TypeError, errMsg);
return NULL;
}
int count = PyTuple_Size(red);
if (PyTuple_Size(green) != count || PyTuple_Size(blue) != count) {
PyErr_SetString(PyExc_ValueError, "Tuple sizes must be equal");
return NULL;
}
unsigned char* redArray = new unsigned char[count];
unsigned char* greenArray = new unsigned char[count];
unsigned char* blueArray = new unsigned char[count];
for (int i = 0; i < count; i++) {
PyObject* redItem = PyTuple_GET_ITEM(red, i);
PyObject* greenItem = PyTuple_GET_ITEM(green, i);
PyObject* blueItem = PyTuple_GET_ITEM(blue, i);
if (!wxPyInt_Check(redItem) || !wxPyInt_Check(greenItem) || !wxPyInt_Check(blueItem)) {
delete[] redArray;
delete[] greenArray;
delete[] blueArray;
PyErr_SetString(PyExc_TypeError, errMsg);
return NULL;
}
long redLong = wxPyInt_AsLong(redItem);
long greenLong = wxPyInt_AsLong(greenItem);
long blueLong = wxPyInt_AsLong(blueItem);
if (redLong < 0 || redLong > 256 || greenLong < 0 || greenLong > 256 || blueLong < 0 || blueLong > 256) {
delete[] redArray;
delete[] greenArray;
delete[] blueArray;
PyErr_SetString(PyExc_ValueError, "Tuple values must be > 0 and < 256");
return NULL;
}
redArray[i] = redLong;
greenArray[i] = greenLong;
blueArray[i] = blueLong;
}
if (self->Create(count, redArray, greenArray, blueArray)) {
Py_RETURN_TRUE;
} else {
Py_RETURN_FALSE;
}
""")
#-----------------------------------------------------------------
tools.doCommonTweaks(module)
tools.runGenerators(module)
#---------------------------------------------------------------------------
if __name__ == '__main__':
run()

41
unittests/test_palette.py Normal file
View File

@@ -0,0 +1,41 @@
import unittest
from unittests import wtc
import wx
#---------------------------------------------------------------------------
class palette_Tests(wtc.WidgetTestCase):
def test_palette(self):
rgb = bytearray(range(256))
p = wx.Palette(256, rgb, rgb, rgb)
self.assertTrue(p.IsOk())
self.assertTrue(p.GetColoursCount() == 256)
self.assertTrue(p.GetPixel(42, 42, 42) == 42)
self.assertTrue(p.GetRGB(64) == (64, 64, 64))
with self.assertRaises(IndexError):
p.GetRGB(257)
def test_paletteCreate(self):
p = wx.Palette()
with self.assertRaises(TypeError):
p.Create(1, 2, 3)
with self.assertRaises(ValueError):
p.Create((1, 2, 3), (1, 2, 3), (1, 2))
with self.assertRaises(TypeError):
p.Create((1, 2, 3), ("1", "2", "3"), (1, 2, 3))
with self.assertRaises(ValueError):
p.Create((257, 2, 3), (1, 2, 3), (1, 2, 3))
with self.assertRaises(ValueError):
p.Create((1, 2, 3), (-1, 2, 3), (1, 2, 3))
self.assertTrue(p.Create((1, 2, 3), (4, 5, 6), (7, 8, 9)))
self.assertTrue(p.GetRGB(1) == (2, 5, 8))
def test_nullPalette(self):
wx.NullPalette
#---------------------------------------------------------------------------
if __name__ == '__main__':
unittest.main()