1
+ #include < objc/NSObjCRuntime.h>
1
2
#define PY_SSIZE_T_CLEAN
2
3
#include < Cocoa/Cocoa.h>
3
4
#include < ApplicationServices/ApplicationServices.h>
@@ -629,6 +630,39 @@ static CGFloat _get_device_scale(CGContextRef cr)
629
630
return (PyObject*)self;
630
631
}
631
632
633
+ int set_icon (PyObject* icon_path) {
634
+ if (!icon_path) {
635
+ return 0 ;
636
+ }
637
+ const char * icon_path_ptr = PyUnicode_AsUTF8 (icon_path);
638
+ if (!icon_path_ptr) {
639
+ Py_DECREF (icon_path);
640
+ return -1 ;
641
+ }
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];
649
+ [ns_icon_path release ];
650
+ if (!image) {
651
+ PyErr_SetString (PyExc_RuntimeError, " Could not create NSImage*" );
652
+ [image release ];
653
+ return -1 ;
654
+ }
655
+ if (!image.valid ) {
656
+ PyErr_SetString (PyExc_RuntimeError, " Image is not valid" );
657
+ [image release ];
658
+ return -1 ;
659
+ }
660
+ NSApplication * app = [NSApplication sharedApplication ];
661
+ app.applicationIconImage = image;
662
+ [image release ];
663
+ return 0 ;
664
+ }
665
+
632
666
static int
633
667
FigureManager_init (FigureManager *self, PyObject *args, PyObject *kwds)
634
668
{
@@ -637,24 +671,32 @@ static CGFloat _get_device_scale(CGContextRef cr)
637
671
View* view;
638
672
PyObject* size;
639
673
int width, height;
640
- PyObject* obj;
641
674
FigureCanvas* canvas;
675
+ PyObject* icon_path;
642
676
643
677
if (!self->window ) {
644
678
PyErr_SetString (PyExc_RuntimeError, " NSWindow* is NULL" );
645
679
return -1 ;
646
680
}
681
+ static char * kwlist[3 ] = { " canvas" , " icon_path" , NULL };
682
+ icon_path = NULL ;
683
+ if (!PyArg_ParseTupleAndKeywords (args, kwds,
684
+ " O!|O&" , kwlist,
685
+ &FigureCanvasType, &canvas,
686
+ &PyUnicode_FSDecoder, &icon_path)) {
687
+ return -1 ;
688
+ }
689
+ if (set_icon (icon_path)) {
690
+ return -1 ;
691
+ }
647
692
648
- if (!PyArg_ParseTuple (args, " O" , &obj)) { return -1 ; }
649
-
650
- canvas = (FigureCanvas*)obj;
651
693
view = canvas->view ;
652
694
if (!view) { /* Something really weird going on */
653
695
PyErr_SetString (PyExc_RuntimeError, " NSView* is NULL" );
654
696
return -1 ;
655
697
}
656
698
657
- size = PyObject_CallMethod (obj , " get_width_height" , " " );
699
+ size = PyObject_CallMethod ((PyObject*)canvas , " get_width_height" , " " );
658
700
if (!size) { return -1 ; }
659
701
if (!PyArg_ParseTuple (size, " ii" , &width, &height)) {
660
702
Py_DECREF (size);
@@ -778,6 +820,23 @@ static CGFloat _get_device_scale(CGContextRef cr)
778
820
Py_RETURN_NONE;
779
821
}
780
822
823
+ static PyObject*
824
+ FigureManager__get_image_types (PyObject* no_self, PyObject* no_args)
825
+ {
826
+ NSArray <NSString *>* images = NSImage .imageUnfilteredTypes ;
827
+ const NSUInteger n_images = [images count ];
828
+ PyObject* out = PyList_New (n_images);
829
+ if (!out) {
830
+ [images release ];
831
+ return NULL ;
832
+ }
833
+ for (NSUInteger i=0 ; i < n_images; ++i) {
834
+ const char * string = images[i].UTF8String ;
835
+ PyList_SET_ITEM (out, i, PyUnicode_FromString (string));
836
+ }
837
+ return out;
838
+ }
839
+
781
840
static PyMethodDef FigureManager_methods[] = {
782
841
{" show" ,
783
842
(PyCFunction)FigureManager_show,
@@ -804,6 +863,11 @@ static CGFloat _get_device_scale(CGContextRef cr)
804
863
METH_VARARGS,
805
864
NULL , // docstring inherited.
806
865
},
866
+ {" _get_image_types" ,
867
+ FigureManager__get_image_types,
868
+ METH_STATIC | METH_NOARGS,
869
+ " Return image types supported by macOS"
870
+ },
807
871
{NULL } /* Sentinel */
808
872
};
809
873
0 commit comments