@@ -363,10 +363,8 @@ static CGFloat _get_device_scale(CGContextRef cr)
363
363
static void
364
364
FigureCanvas_dealloc (FigureCanvas* self)
365
365
{
366
- if (self->view ) {
367
- [self ->view setCanvas: NULL ];
368
- [self ->view release ];
369
- }
366
+ [self ->view setCanvas: NULL ];
367
+ [self ->view release ];
370
368
Py_TYPE (self)->tp_free ((PyObject*)self);
371
369
}
372
370
@@ -380,82 +378,50 @@ static CGFloat _get_device_scale(CGContextRef cr)
380
378
static PyObject*
381
379
FigureCanvas_draw (FigureCanvas* self)
382
380
{
383
- View* view = self->view ;
384
- if (view) { /* The figure may have been closed already */
385
- [view display ];
386
- }
381
+ [self ->view display ];
387
382
Py_RETURN_NONE;
388
383
}
389
384
390
385
static PyObject*
391
386
FigureCanvas_draw_idle (FigureCanvas* self)
392
387
{
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 ];
399
389
Py_RETURN_NONE;
400
390
}
401
391
402
392
static PyObject*
403
393
FigureCanvas_flush_events (FigureCanvas* self)
404
394
{
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 ];
411
396
Py_RETURN_NONE;
412
397
}
413
398
414
399
static PyObject*
415
400
FigureCanvas_set_rubberband (FigureCanvas* self, PyObject *args)
416
401
{
417
402
View* view = self->view ;
418
- int x0, y0 , x1, y1 ;
419
- NSRect rubberband;
420
403
if (!view) {
421
404
PyErr_SetString (PyExc_RuntimeError, " NSView* is NULL" );
422
405
return NULL ;
423
406
}
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
+ }
426
411
x0 /= view->device_scale ;
427
412
x1 /= view->device_scale ;
428
413
y0 /= view->device_scale ;
429
414
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 ));
446
417
[view setRubberband: rubberband];
447
418
Py_RETURN_NONE;
448
419
}
449
420
450
421
static PyObject*
451
422
FigureCanvas_remove_rubberband (FigureCanvas* self)
452
423
{
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 ];
459
425
Py_RETURN_NONE;
460
426
}
461
427
@@ -606,40 +572,26 @@ static CGFloat _get_device_scale(CGContextRef cr)
606
572
static int
607
573
FigureManager_init (FigureManager *self, PyObject *args, PyObject *kwds)
608
574
{
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)) {
619
577
return -1 ;
620
578
}
621
579
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 */
627
582
PyErr_SetString (PyExc_RuntimeError, " NSView* is NULL" );
628
583
return -1 ;
629
584
}
630
585
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);
635
590
return -1 ;
636
591
}
637
592
Py_DECREF (size);
638
593
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);
643
595
644
596
self->window = [self ->window initWithContentRect: rect
645
597
styleMask: NSWindowStyleMaskTitled
@@ -649,7 +601,7 @@ static CGFloat _get_device_scale(CGContextRef cr)
649
601
backing: NSBackingStoreBuffered
650
602
defer: YES
651
603
withManager: (PyObject*)self ];
652
- window = self->window ;
604
+ Window* window = self->window ;
653
605
[window setDelegate: view];
654
606
[window makeFirstResponder: view];
655
607
[[window contentView ] addSubview: view];
@@ -668,32 +620,23 @@ static CGFloat _get_device_scale(CGContextRef cr)
668
620
static void
669
621
FigureManager_dealloc (FigureManager* self)
670
622
{
671
- Window* window = self->window ;
672
- if (window) {
673
- [window close ];
674
- }
623
+ [self ->window close ];
675
624
Py_TYPE (self)->tp_free ((PyObject*)self);
676
625
}
677
626
678
627
static PyObject*
679
628
FigureManager_show (FigureManager* self)
680
629
{
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 ];
686
632
Py_RETURN_NONE;
687
633
}
688
634
689
635
static PyObject*
690
636
FigureManager_destroy (FigureManager* self)
691
637
{
692
- Window* window = self->window ;
693
- if (window) {
694
- [window close ];
695
- self->window = NULL ;
696
- }
638
+ [self ->window close ];
639
+ self->window = NULL ;
697
640
Py_RETURN_NONE;
698
641
}
699
642
@@ -744,30 +687,19 @@ static CGFloat _get_device_scale(CGContextRef cr)
744
687
if (!PyArg_ParseTuple (args, " s" , &title)) {
745
688
return NULL ;
746
689
}
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];
754
694
Py_RETURN_NONE;
755
695
}
756
696
757
697
static PyObject*
758
698
FigureManager_get_window_title (FigureManager* self)
759
699
{
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 ]);
771
703
} else {
772
704
Py_RETURN_NONE;
773
705
}
@@ -1366,7 +1298,7 @@ -(void)drawRect:(NSRect)rect
1366
1298
1367
1299
CGContextRef cr = [[NSGraphicsContext currentContext ] CGContext ];
1368
1300
1369
- if (!(renderer = PyObject_CallMethod (canvas, " _draw" , " " , NULL ))
1301
+ if (!(renderer = PyObject_CallMethod (canvas, " _draw" , " " ))
1370
1302
|| !(renderer_buffer = PyObject_GetAttrString (renderer, " _renderer" ))) {
1371
1303
PyErr_Print ();
1372
1304
goto exit ;
@@ -1392,7 +1324,7 @@ - (void)updateDevicePixelRatio:(double)scale
1392
1324
PyGILState_STATE gstate = PyGILState_Ensure ();
1393
1325
1394
1326
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))) {
1396
1328
PyErr_Print ();
1397
1329
goto exit ;
1398
1330
}
0 commit comments