Skip to content

Commit

Permalink
Add window title management to MacOSX backend
Browse files Browse the repository at this point in the history
  • Loading branch information
jkseppan committed Jun 9, 2012
1 parent ef2166d commit 08dd29f
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
6 changes: 4 additions & 2 deletions lib/matplotlib/backends/backend_macosx.py
Expand Up @@ -442,7 +442,8 @@ def zoomy(self, direction):
self.canvas.invalidate()

def save_figure(self, *args):
filename = _macosx.choose_save_file('Save the figure')
filename = _macosx.choose_save_file('Save the figure',
self.canvas.get_default_filename())
if filename is None: # Cancel
return
self.canvas.print_figure(filename)
Expand All @@ -466,7 +467,8 @@ def set_cursor(self, cursor):
_macosx.set_cursor(cursor)

def save_figure(self, *args):
filename = _macosx.choose_save_file('Save the figure')
filename = _macosx.choose_save_file('Save the figure',
self.canvas.get_default_filename())
if filename is None: # Cancel
return
self.canvas.print_figure(filename)
Expand Down
70 changes: 69 additions & 1 deletion src/_macosx.m
Expand Up @@ -3827,6 +3827,56 @@ static void _data_provider_release(void* info, const void* data, size_t size)
return Py_None;
}

static PyObject*
FigureManager_set_window_title(FigureManager* self,
PyObject *args, PyObject *kwds)
{
char* title;
if(!PyArg_ParseTuple(args, "es", "UTF-8", &title))
return NULL;

Window* window = self->window;
if(window)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSString* ns_title = [[NSString alloc]
initWithCString: title
encoding: NSUTF8StringEncoding];
[window setTitle: ns_title];
[pool release];
}
PyMem_Free(title);
Py_INCREF(Py_None);
return Py_None;
}

static PyObject*
FigureManager_get_window_title(FigureManager* self)
{
Window* window = self->window;
PyObject* result = NULL;
if(window)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSString* title = [window title];
if (title) {
const char* cTitle = [title UTF8String];
#if PY3K || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 6)
result = PyUnicode_FromString(cTitle);
#else
result = PyString_FromString(cTitle);
#endif
}
[pool release];
}
if (result) {
return result;
} else {
Py_INCREF(Py_None);
return Py_None;
}
}

static PyMethodDef FigureManager_methods[] = {
{"show",
(PyCFunction)FigureManager_show,
Expand All @@ -3838,6 +3888,16 @@ static void _data_provider_release(void* info, const void* data, size_t size)
METH_NOARGS,
"Closes the window associated with the figure manager."
},
{"set_window_title",
(PyCFunction)FigureManager_set_window_title,
METH_VARARGS,
"Sets the title of the window associated with the figure manager."
},
{"get_window_title",
(PyCFunction)FigureManager_get_window_title,
METH_NOARGS,
"Returns the title of the window associated with the figure manager."
},
{NULL} /* Sentinel */
};

Expand Down Expand Up @@ -4806,11 +4866,19 @@ -(void)save_figure:(id)sender
{
int result;
const char* title;
if(!PyArg_ParseTuple(args, "s", &title)) return NULL;
char* default_filename;
if(!PyArg_ParseTuple(args, "ses", &title, "UTF-8", &default_filename))
return NULL;

NSSavePanel* panel = [NSSavePanel savePanel];
[panel setTitle: [NSString stringWithCString: title
encoding: NSASCIIStringEncoding]];
NSString* ns_default_filename =
[[NSString alloc]
initWithCString: default_filename
encoding: NSUTF8StringEncoding];
PyMem_Free(default_filename);
[panel setNameFieldStringValue: ns_default_filename];
result = [panel runModal];
if (result == NSOKButton)
{
Expand Down

0 comments on commit 08dd29f

Please sign in to comment.