Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 417aea6

Browse files
committed
Change to static method for setting icon
1 parent bcdfab5 commit 417aea6

File tree

2 files changed

+45
-42
lines changed

2 files changed

+45
-42
lines changed

lib/matplotlib/backends/backend_macosx.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ class FigureManagerMac(_macosx.FigureManager, FigureManagerBase):
6666
Wrap everything up into a window for the pylab interface
6767
"""
6868
def __init__(self, canvas, num):
69+
_macosx.FigureManager.__init__(self, canvas)
6970
icon_path = str(cbook._get_data_path('images/matplotlib.pdf'))
70-
_macosx.FigureManager.__init__(self, canvas, icon_path)
71+
_macosx.FigureManager.set_icon(icon_path)
7172
FigureManagerBase.__init__(self, canvas, num)
7273
if mpl.rcParams['toolbar'] == 'toolbar2':
7374
self.toolbar = NavigationToolbar2Mac(canvas)

src/_macosx.m

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -629,37 +629,6 @@ static CGFloat _get_device_scale(CGContextRef cr)
629629
return (PyObject*)self;
630630
}
631631

632-
int set_icon(PyObject* icon_path) {
633-
if (!icon_path) {
634-
return 0;
635-
}
636-
const char* icon_path_ptr = PyUnicode_AsUTF8(icon_path);
637-
if (!icon_path_ptr) {
638-
Py_DECREF(icon_path);
639-
return -1;
640-
}
641-
@autoreleasepool {
642-
NSString* ns_icon_path = [NSString stringWithUTF8String: icon_path_ptr];
643-
if (!ns_icon_path) {
644-
Py_DECREF(icon_path);
645-
PyErr_SetString(PyExc_RuntimeError, "Could not convert to NSString*");
646-
return -1;
647-
}
648-
NSImage* image = [[[NSImage alloc] initByReferencingFile: ns_icon_path] autorelease];
649-
if (!image) {
650-
PyErr_SetString(PyExc_RuntimeError, "Could not create NSImage*");
651-
return -1;
652-
}
653-
if (!image.valid) {
654-
PyErr_SetString(PyExc_RuntimeError, "Image is not valid");
655-
return -1;
656-
}
657-
NSApplication* app = [NSApplication sharedApplication];
658-
app.applicationIconImage = image;
659-
}
660-
return 0;
661-
}
662-
663632
static int
664633
FigureManager_init(FigureManager *self, PyObject *args, PyObject *kwds)
665634
{
@@ -669,21 +638,14 @@ int set_icon(PyObject* icon_path) {
669638
PyObject* size;
670639
int width, height;
671640
FigureCanvas* canvas;
672-
PyObject* icon_path;
673641

674642
if (!self->window) {
675643
PyErr_SetString(PyExc_RuntimeError, "NSWindow* is NULL");
676644
return -1;
677645
}
678-
static char* kwlist[3] = { "canvas", "icon_path", NULL };
679-
icon_path = NULL;
680-
if (!PyArg_ParseTupleAndKeywords(args, kwds,
681-
"O!|O&", kwlist,
682-
&FigureCanvasType, &canvas,
683-
&PyUnicode_FSDecoder, &icon_path)) {
684-
return -1;
685-
}
686-
if (set_icon(icon_path)) {
646+
static char* kwlist[3] = { "canvas", NULL };
647+
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!", kwlist,
648+
&FigureCanvasType, &canvas)) {
687649
return -1;
688650
}
689651

@@ -762,6 +724,41 @@ int set_icon(PyObject* icon_path) {
762724
Py_RETURN_NONE;
763725
}
764726

727+
static PyObject*
728+
FigureManager_set_icon(PyObject* null, PyObject* args, PyObject* kwds) {
729+
PyObject* icon_path;
730+
static char* kwlist[3] = { "icon_path", NULL };
731+
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&", kwlist,
732+
&PyUnicode_FSDecoder, &icon_path)) {
733+
return NULL;
734+
}
735+
const char* icon_path_ptr = PyUnicode_AsUTF8(icon_path);
736+
if (!icon_path_ptr) {
737+
Py_DECREF(icon_path);
738+
return NULL;
739+
}
740+
@autoreleasepool {
741+
NSString* ns_icon_path = [NSString stringWithUTF8String: icon_path_ptr];
742+
Py_DECREF(icon_path);
743+
if (!ns_icon_path) {
744+
PyErr_SetString(PyExc_RuntimeError, "Could not convert to NSString*");
745+
return NULL;
746+
}
747+
NSImage* image = [[[NSImage alloc] initByReferencingFile: ns_icon_path] autorelease];
748+
if (!image) {
749+
PyErr_SetString(PyExc_RuntimeError, "Could not create NSImage*");
750+
return NULL;
751+
}
752+
if (!image.valid) {
753+
PyErr_SetString(PyExc_RuntimeError, "Image is not valid");
754+
return NULL;
755+
}
756+
NSApplication* app = [NSApplication sharedApplication];
757+
app.applicationIconImage = image;
758+
}
759+
Py_RETURN_NONE;
760+
}
761+
765762
static PyObject*
766763
FigureManager_set_window_title(FigureManager* self,
767764
PyObject *args, PyObject *kwds)
@@ -828,6 +825,11 @@ int set_icon(PyObject* icon_path) {
828825
METH_NOARGS,
829826
NULL, // docstring inherited.
830827
},
828+
{"set_icon",
829+
(PyCFunction)FigureManager_set_icon,
830+
METH_STATIC | METH_VARARGS | METH_KEYWORDS,
831+
NULL, // Set application icon
832+
},
831833
{"set_window_title",
832834
(PyCFunction)FigureManager_set_window_title,
833835
METH_VARARGS,

0 commit comments

Comments
 (0)