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

Skip to content

Commit 94ac7a7

Browse files
authored
Merge pull request #21717 from anntzer/macosxtb2
Simplify macosx toolbar init.
2 parents d114546 + faabfd9 commit 94ac7a7

File tree

2 files changed

+20
-38
lines changed

2 files changed

+20
-38
lines changed

lib/matplotlib/backends/backend_macosx.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,10 @@ def close(self):
8686
class NavigationToolbar2Mac(_macosx.NavigationToolbar2, NavigationToolbar2):
8787

8888
def __init__(self, canvas):
89-
self.canvas = canvas # Needed by the _macosx __init__.
9089
data_path = cbook._get_data_path('images')
9190
_, tooltips, image_names, _ = zip(*NavigationToolbar2.toolitems)
9291
_macosx.NavigationToolbar2.__init__(
93-
self,
92+
self, canvas,
9493
tuple(str(data_path / image_name) + ".pdf"
9594
for image_name in image_names if image_name is not None),
9695
tuple(tooltip for tooltip in tooltips if tooltip is not None))

src/_macosx.m

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,38 +1060,31 @@ -(void)save_figure:(id)sender
10601060
static int
10611061
NavigationToolbar2_init(NavigationToolbar2 *self, PyObject *args, PyObject *kwds)
10621062
{
1063-
PyObject* obj;
10641063
FigureCanvas* canvas;
1065-
View* view;
1066-
1067-
int i;
1068-
NSRect rect;
1069-
NSSize size;
1070-
NSSize scale;
1064+
const char* images[7];
1065+
const char* tooltips[7];
10711066

10721067
const float gap = 2;
10731068
const int height = 36;
10741069
const int imagesize = 24;
10751070

1076-
self->height = height;
1077-
1078-
obj = PyObject_GetAttrString((PyObject*)self, "canvas");
1079-
if (!obj) {
1080-
PyErr_SetString(PyExc_AttributeError, "Attempt to install toolbar for NULL canvas");
1081-
return -1;
1082-
}
1083-
Py_DECREF(obj); /* Don't increase the reference count */
1084-
if (!PyObject_IsInstance(obj, (PyObject*) &FigureCanvasType)) {
1085-
PyErr_SetString(PyExc_TypeError, "Attempt to install toolbar for object that is not a FigureCanvas");
1071+
if (!PyArg_ParseTuple(args, "O!(sssssss)(sssssss)",
1072+
&FigureCanvasType, &canvas,
1073+
&images[0], &images[1], &images[2], &images[3],
1074+
&images[4], &images[5], &images[6],
1075+
&tooltips[0], &tooltips[1], &tooltips[2], &tooltips[3],
1076+
&tooltips[4], &tooltips[5], &tooltips[6])) {
10861077
return -1;
10871078
}
1088-
canvas = (FigureCanvas*)obj;
1089-
view = canvas->view;
1079+
1080+
View* view = canvas->view;
10901081
if (!view) {
10911082
PyErr_SetString(PyExc_RuntimeError, "NSView* is NULL");
10921083
return -1;
10931084
}
10941085

1086+
self->height = height;
1087+
10951088
NSRect bounds = [view bounds];
10961089
NSWindow* window = [view window];
10971090

@@ -1101,16 +1094,6 @@ -(void)save_figure:(id)sender
11011094
bounds.size.height += height;
11021095
[window setContentSize: bounds.size];
11031096

1104-
const char* images[7];
1105-
const char* tooltips[7];
1106-
if (!PyArg_ParseTuple(args, "(sssssss)(sssssss)",
1107-
&images[0], &images[1], &images[2], &images[3],
1108-
&images[4], &images[5], &images[6],
1109-
&tooltips[0], &tooltips[1], &tooltips[2], &tooltips[3],
1110-
&tooltips[4], &tooltips[5], &tooltips[6])) {
1111-
return -1;
1112-
}
1113-
11141097
NSButton* buttons[7];
11151098
SEL actions[7] = {@selector(home:),
11161099
@selector(back:),
@@ -1127,23 +1110,23 @@ -(void)save_figure:(id)sender
11271110
NSButtonTypeMomentaryLight,
11281111
NSButtonTypeMomentaryLight};
11291112

1130-
rect.origin.x = 0;
1131-
rect.origin.y = 0;
1132-
rect.size.width = imagesize;
1133-
rect.size.height = imagesize;
1113+
NSRect rect;
1114+
NSSize size;
1115+
NSSize scale;
1116+
1117+
rect = NSMakeRect(0, 0, imagesize, imagesize);
11341118
#ifdef COMPILING_FOR_10_7
11351119
rect = [window convertRectToBacking: rect];
11361120
#endif
11371121
size = rect.size;
1138-
scale.width = imagesize / size.width;
1139-
scale.height = imagesize / size.height;
1122+
scale = NSMakeSize(imagesize / size.width, imagesize / size.height);
11401123

11411124
rect.size.width = 32;
11421125
rect.size.height = 32;
11431126
rect.origin.x = gap;
11441127
rect.origin.y = 0.5*(height - rect.size.height);
11451128

1146-
for (i = 0; i < 7; i++) {
1129+
for (int i = 0; i < 7; i++) {
11471130
NSString* filename = [NSString stringWithCString: images[i]
11481131
encoding: NSUTF8StringEncoding];
11491132
NSString* tooltip = [NSString stringWithCString: tooltips[i]

0 commit comments

Comments
 (0)