@@ -363,10 +363,8 @@ static CGFloat _get_device_scale(CGContextRef cr)
363363static void
364364FigureCanvas_dealloc (FigureCanvas* self)
365365{
366- if (self->view ) {
367- [self ->view setCanvas: NULL ];
368- [self ->view release ];
369- }
366+ [self ->view setCanvas: NULL ];
367+ [self ->view release ];
370368 Py_TYPE (self)->tp_free ((PyObject*)self);
371369}
372370
@@ -380,82 +378,50 @@ static CGFloat _get_device_scale(CGContextRef cr)
380378static PyObject*
381379FigureCanvas_draw (FigureCanvas* self)
382380{
383- View* view = self->view ;
384- if (view) { /* The figure may have been closed already */
385- [view display ];
386- }
381+ [self ->view display ];
387382 Py_RETURN_NONE;
388383}
389384
390385static PyObject*
391386FigureCanvas_draw_idle (FigureCanvas* self)
392387{
393- View* view = self->view ;
394- if (!view) {
395- PyErr_SetString (PyExc_RuntimeError, " NSView* is NULL" );
396- return NULL ;
397- }
398- [view setNeedsDisplay: YES ];
388+ [self ->view setNeedsDisplay: YES ];
399389 Py_RETURN_NONE;
400390}
401391
402392static PyObject*
403393FigureCanvas_flush_events (FigureCanvas* self)
404394{
405- View* view = self->view ;
406- if (!view) {
407- PyErr_SetString (PyExc_RuntimeError, " NSView* is NULL" );
408- return NULL ;
409- }
410- [view displayIfNeeded ];
395+ [self ->view displayIfNeeded ];
411396 Py_RETURN_NONE;
412397}
413398
414399static PyObject*
415400FigureCanvas_set_rubberband (FigureCanvas* self, PyObject *args)
416401{
417402 View* view = self->view ;
418- int x0, y0, x1, y1;
419- NSRect rubberband;
420403 if (!view) {
421404 PyErr_SetString (PyExc_RuntimeError, " NSView* is NULL" );
422405 return NULL ;
423406 }
424- if (!PyArg_ParseTuple (args, " iiii" , &x0, &y0, &x1, &y1)) { return NULL ; }
425-
407+ int x0, y0, x1, y1;
408+ if (!PyArg_ParseTuple (args, " iiii" , &x0, &y0, &x1, &y1)) {
409+ return NULL ;
410+ }
426411 x0 /= view->device_scale ;
427412 x1 /= view->device_scale ;
428413 y0 /= view->device_scale ;
429414 y1 /= view->device_scale ;
430-
431- if (x1 > x0) {
432- rubberband.origin .x = x0;
433- rubberband.size .width = x1 - x0;
434- } else {
435- rubberband.origin .x = x1;
436- rubberband.size .width = x0 - x1;
437- }
438- if (y1 > y0) {
439- rubberband.origin .y = y0;
440- rubberband.size .height = y1 - y0;
441- } else {
442- rubberband.origin .y = y1;
443- rubberband.size .height = y0 - y1;
444- }
445-
415+ NSRect rubberband = NSMakeRect (x0 < x1 ? x0 : x1, y0 < y1 ? y0 : y1,
416+ abs (x1 - x0), abs (y1 - y0));
446417 [view setRubberband: rubberband];
447418 Py_RETURN_NONE;
448419}
449420
450421static PyObject*
451422FigureCanvas_remove_rubberband (FigureCanvas* self)
452423{
453- View* view = self->view ;
454- if (!view) {
455- PyErr_SetString (PyExc_RuntimeError, " NSView* is NULL" );
456- return NULL ;
457- }
458- [view removeRubberband ];
424+ [self ->view removeRubberband ];
459425 Py_RETURN_NONE;
460426}
461427
@@ -606,40 +572,26 @@ static CGFloat _get_device_scale(CGContextRef cr)
606572static int
607573FigureManager_init (FigureManager *self, PyObject *args, PyObject *kwds)
608574{
609- NSRect rect;
610- Window* window;
611- View* view;
612- PyObject* size;
613- int width, height;
614- PyObject* obj;
615- FigureCanvas* canvas;
616-
617- if (!self->window ) {
618- PyErr_SetString (PyExc_RuntimeError, " NSWindow* is NULL" );
575+ PyObject* canvas;
576+ if (!PyArg_ParseTuple (args, " O" , &canvas)) {
619577 return -1 ;
620578 }
621579
622- if (!PyArg_ParseTuple (args, " O" , &obj)) { return -1 ; }
623-
624- canvas = (FigureCanvas*)obj;
625- view = canvas->view ;
626- if (!view) { /* Something really weird going on */
580+ View* view = ((FigureCanvas*)canvas)->view ;
581+ if (!view) { /* Something really weird going on */
627582 PyErr_SetString (PyExc_RuntimeError, " NSView* is NULL" );
628583 return -1 ;
629584 }
630585
631- size = PyObject_CallMethod (obj , " get_width_height" , " " );
632- if (!size) { return - 1 ; }
633- if (!PyArg_ParseTuple (size, " ii" , &width, &height)) {
634- Py_DECREF (size);
586+ PyObject* size = PyObject_CallMethod (canvas , " get_width_height" , " " );
587+ int width, height;
588+ if (!size || ! PyArg_ParseTuple (size, " ii" , &width, &height)) {
589+ Py_XDECREF (size);
635590 return -1 ;
636591 }
637592 Py_DECREF (size);
638593
639- rect.origin .x = 100 ;
640- rect.origin .y = 350 ;
641- rect.size .height = height;
642- rect.size .width = width;
594+ NSRect rect = NSMakeRect ( /* x */ 100 , /* y */ 350 , height, width);
643595
644596 self->window = [self ->window initWithContentRect: rect
645597 styleMask: NSWindowStyleMaskTitled
@@ -649,7 +601,7 @@ static CGFloat _get_device_scale(CGContextRef cr)
649601 backing: NSBackingStoreBuffered
650602 defer: YES
651603 withManager: (PyObject*)self ];
652- window = self->window ;
604+ Window* window = self->window ;
653605 [window setDelegate: view];
654606 [window makeFirstResponder: view];
655607 [[window contentView ] addSubview: view];
@@ -668,32 +620,23 @@ static CGFloat _get_device_scale(CGContextRef cr)
668620static void
669621FigureManager_dealloc (FigureManager* self)
670622{
671- Window* window = self->window ;
672- if (window) {
673- [window close ];
674- }
623+ [self ->window close ];
675624 Py_TYPE (self)->tp_free ((PyObject*)self);
676625}
677626
678627static PyObject*
679628FigureManager_show (FigureManager* self)
680629{
681- Window* window = self->window ;
682- if (window) {
683- [window makeKeyAndOrderFront: nil ];
684- [window orderFrontRegardless ];
685- }
630+ [self ->window makeKeyAndOrderFront: nil ];
631+ [self ->window orderFrontRegardless ];
686632 Py_RETURN_NONE;
687633}
688634
689635static PyObject*
690636FigureManager_destroy (FigureManager* self)
691637{
692- Window* window = self->window ;
693- if (window) {
694- [window close ];
695- self->window = NULL ;
696- }
638+ [self ->window close ];
639+ self->window = NULL ;
697640 Py_RETURN_NONE;
698641}
699642
@@ -744,30 +687,19 @@ static CGFloat _get_device_scale(CGContextRef cr)
744687 if (!PyArg_ParseTuple (args, " s" , &title)) {
745688 return NULL ;
746689 }
747- Window* window = self->window ;
748- if (window) {
749- NSString * ns_title = [[[NSString alloc ]
750- initWithCString: title
751- encoding: NSUTF8StringEncoding] autorelease ];
752- [window setTitle: ns_title];
753- }
690+ NSString * ns_title = [[[NSString alloc ]
691+ initWithCString: title
692+ encoding: NSUTF8StringEncoding] autorelease ];
693+ [self ->window setTitle: ns_title];
754694 Py_RETURN_NONE;
755695}
756696
757697static PyObject*
758698FigureManager_get_window_title (FigureManager* self)
759699{
760- Window* window = self->window ;
761- PyObject* result = NULL ;
762- if (window) {
763- NSString * title = [window title ];
764- if (title) {
765- const char * cTitle = [title UTF8String ];
766- result = PyUnicode_FromString (cTitle);
767- }
768- }
769- if (result) {
770- return result;
700+ NSString * title = [self ->window title ];
701+ if (title) {
702+ return PyUnicode_FromString ([title UTF8String ]);
771703 } else {
772704 Py_RETURN_NONE;
773705 }
@@ -1366,7 +1298,7 @@ -(void)drawRect:(NSRect)rect
13661298
13671299 CGContextRef cr = [[NSGraphicsContext currentContext ] CGContext ];
13681300
1369- if (!(renderer = PyObject_CallMethod (canvas, " _draw" , " " , NULL ))
1301+ if (!(renderer = PyObject_CallMethod (canvas, " _draw" , " " ))
13701302 || !(renderer_buffer = PyObject_GetAttrString (renderer, " _renderer" ))) {
13711303 PyErr_Print ();
13721304 goto exit;
@@ -1392,7 +1324,7 @@ - (void)updateDevicePixelRatio:(double)scale
13921324 PyGILState_STATE gstate = PyGILState_Ensure ();
13931325
13941326 device_scale = scale;
1395- if (!(change = PyObject_CallMethod (canvas, " _set_device_pixel_ratio" , " d" , device_scale, NULL ))) {
1327+ if (!(change = PyObject_CallMethod (canvas, " _set_device_pixel_ratio" , " d" , device_scale))) {
13961328 PyErr_Print ();
13971329 goto exit;
13981330 }
0 commit comments