1"""
2Render to gtk from agg
3"""
4from __future__ import (absolute_import, division, print_function,
5                        unicode_literals)
6
7import six
8
9import matplotlib
10from matplotlib.cbook import warn_deprecated
11from matplotlib.backends.backend_agg import FigureCanvasAgg
12from matplotlib.backends.backend_gtk import (
13    gtk, _BackendGTK, FigureCanvasGTK, FigureManagerGTK, NavigationToolbar2GTK,
14    backend_version, error_msg_gtk, PIXELS_PER_INCH)
15from matplotlib.backends._gtkagg import agg_to_gtk_drawable
16
17
18class NavigationToolbar2GTKAgg(NavigationToolbar2GTK):
19    def _get_canvas(self, fig):
20        return FigureCanvasGTKAgg(fig)
21
22
23class FigureManagerGTKAgg(FigureManagerGTK):
24    def _get_toolbar(self, canvas):
25        # must be inited after the window, drawingArea and figure
26        # attrs are set
27        if matplotlib.rcParams['toolbar']=='toolbar2':
28            toolbar = NavigationToolbar2GTKAgg (canvas, self.window)
29        else:
30            toolbar = None
31        return toolbar
32
33
34class FigureCanvasGTKAgg(FigureCanvasGTK, FigureCanvasAgg):
35    filetypes = FigureCanvasGTK.filetypes.copy()
36    filetypes.update(FigureCanvasAgg.filetypes)
37
38    def __init__(self, *args, **kwargs):
39        warn_deprecated('2.2',
40                        message=('The GTKAgg backend is deprecated. It is '
41                                 'untested and will be removed in Matplotlib '
42                                 '3.0. Use the GTK3Agg backend instead. See '
43                                 'Matplotlib usage FAQ for more info on '
44                                 'backends.'),
45                        alternative='GTK3Agg')
46        super(FigureCanvasGTKAgg, self).__init__(*args, **kwargs)
47
48    def configure_event(self, widget, event=None):
49
50        if widget.window is None:
51            return
52        try:
53            del self.renderer
54        except AttributeError:
55            pass
56        w,h = widget.window.get_size()
57        if w==1 or h==1: return # empty fig
58
59        # compute desired figure size in inches
60        dpival = self.figure.dpi
61        winch = w/dpival
62        hinch = h/dpival
63        self.figure.set_size_inches(winch, hinch, forward=False)
64        self._need_redraw = True
65        self.resize_event()
66        return True
67
68    def _render_figure(self, pixmap, width, height):
69        FigureCanvasAgg.draw(self)
70
71        buf = self.buffer_rgba()
72        ren = self.get_renderer()
73        w = int(ren.width)
74        h = int(ren.height)
75
76        pixbuf = gtk.gdk.pixbuf_new_from_data(
77            buf, gtk.gdk.COLORSPACE_RGB,  True, 8, w, h, w*4)
78        pixmap.draw_pixbuf(pixmap.new_gc(), pixbuf, 0, 0, 0, 0, w, h,
79                           gtk.gdk.RGB_DITHER_NONE, 0, 0)
80
81    def blit(self, bbox=None):
82        agg_to_gtk_drawable(self._pixmap, self.renderer._renderer, bbox)
83        x, y, w, h = self.allocation
84        self.window.draw_drawable(self.style.fg_gc[self.state], self._pixmap,
85                                  0, 0, 0, 0, w, h)
86
87    def print_png(self, filename, *args, **kwargs):
88        # Do this so we can save the resolution of figure in the PNG file
89        agg = self.switch_backends(FigureCanvasAgg)
90        return agg.print_png(filename, *args, **kwargs)
91
92
93@_BackendGTK.export
94class _BackendGTKAgg(_BackendGTK):
95    FigureCanvas = FigureCanvasGTKAgg
96    FigureManager = FigureManagerGTKAgg
97