Skip to content

Commit 23deb00

Browse files
jkseppanmspacek
authored andcommitted
Add window title management to MacOSX backend
1 parent 3b19a15 commit 23deb00

File tree

2 files changed

+73
-3
lines changed

2 files changed

+73
-3
lines changed

lib/matplotlib/backends/backend_macosx.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,8 @@ def zoomy(self, direction):
444444
self.canvas.invalidate()
445445

446446
def save_figure(self, *args):
447-
filename = _macosx.choose_save_file('Save the figure')
447+
filename = _macosx.choose_save_file('Save the figure',
448+
self.canvas.get_default_filename())
448449
if filename is None: # Cancel
449450
return
450451
self.canvas.print_figure(filename)
@@ -469,7 +470,8 @@ def set_cursor(self, cursor):
469470
_macosx.set_cursor(cursor)
470471

471472
def save_figure(self, *args):
472-
filename = _macosx.choose_save_file('Save the figure')
473+
filename = _macosx.choose_save_file('Save the figure',
474+
self.canvas.get_default_filename())
473475
if filename is None: # Cancel
474476
return
475477
self.canvas.print_figure(filename)

src/_macosx.m

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3827,6 +3827,56 @@ static void _data_provider_release(void* info, const void* data, size_t size)
38273827
return Py_None;
38283828
}
38293829

3830+
static PyObject*
3831+
FigureManager_set_window_title(FigureManager* self,
3832+
PyObject *args, PyObject *kwds)
3833+
{
3834+
char* title;
3835+
if(!PyArg_ParseTuple(args, "es", "UTF-8", &title))
3836+
return NULL;
3837+
3838+
Window* window = self->window;
3839+
if(window)
3840+
{
3841+
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
3842+
NSString* ns_title = [[NSString alloc]
3843+
initWithCString: title
3844+
encoding: NSUTF8StringEncoding];
3845+
[window setTitle: ns_title];
3846+
[pool release];
3847+
}
3848+
PyMem_Free(title);
3849+
Py_INCREF(Py_None);
3850+
return Py_None;
3851+
}
3852+
3853+
static PyObject*
3854+
FigureManager_get_window_title(FigureManager* self)
3855+
{
3856+
Window* window = self->window;
3857+
PyObject* result = NULL;
3858+
if(window)
3859+
{
3860+
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
3861+
NSString* title = [window title];
3862+
if (title) {
3863+
const char* cTitle = [title UTF8String];
3864+
#if PY3K || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 6)
3865+
result = PyUnicode_FromString(cTitle);
3866+
#else
3867+
result = PyString_FromString(cTitle);
3868+
#endif
3869+
}
3870+
[pool release];
3871+
}
3872+
if (result) {
3873+
return result;
3874+
} else {
3875+
Py_INCREF(Py_None);
3876+
return Py_None;
3877+
}
3878+
}
3879+
38303880
static PyMethodDef FigureManager_methods[] = {
38313881
{"show",
38323882
(PyCFunction)FigureManager_show,
@@ -3838,6 +3888,16 @@ static void _data_provider_release(void* info, const void* data, size_t size)
38383888
METH_NOARGS,
38393889
"Closes the window associated with the figure manager."
38403890
},
3891+
{"set_window_title",
3892+
(PyCFunction)FigureManager_set_window_title,
3893+
METH_VARARGS,
3894+
"Sets the title of the window associated with the figure manager."
3895+
},
3896+
{"get_window_title",
3897+
(PyCFunction)FigureManager_get_window_title,
3898+
METH_NOARGS,
3899+
"Returns the title of the window associated with the figure manager."
3900+
},
38413901
{NULL} /* Sentinel */
38423902
};
38433903

@@ -4806,11 +4866,19 @@ -(void)save_figure:(id)sender
48064866
{
48074867
int result;
48084868
const char* title;
4809-
if(!PyArg_ParseTuple(args, "s", &title)) return NULL;
4869+
char* default_filename;
4870+
if(!PyArg_ParseTuple(args, "ses", &title, "UTF-8", &default_filename))
4871+
return NULL;
48104872

48114873
NSSavePanel* panel = [NSSavePanel savePanel];
48124874
[panel setTitle: [NSString stringWithCString: title
48134875
encoding: NSASCIIStringEncoding]];
4876+
NSString* ns_default_filename =
4877+
[[NSString alloc]
4878+
initWithCString: default_filename
4879+
encoding: NSUTF8StringEncoding];
4880+
PyMem_Free(default_filename);
4881+
[panel setNameFieldStringValue: ns_default_filename];
48144882
result = [panel runModal];
48154883
if (result == NSOKButton)
48164884
{

0 commit comments

Comments
 (0)