#
# ImageViewCanvasGtk.py -- A FITS image widget with canvas drawing in Gtk
#
# Eric Jeschke (eric@naoj.org)
#
# Copyright (c) Eric R. Jeschke.  All rights reserved.
# This is open-source software licensed under a BSD license.
# Please see the file LICENSE.txt for details.
#
from ginga import ImageView
from ginga import Mixins
from ginga.gtkw import ImageViewGtk
from ginga.canvas.mixins import DrawingMixin, CanvasMixin, CompoundMixin


class ImageViewCanvasError(ImageViewGtk.ImageViewGtkError):
    pass

class ImageViewCanvas(ImageViewGtk.ImageViewZoom,
                      DrawingMixin, CanvasMixin, CompoundMixin):

    def __init__(self, logger=None, rgbmap=None, settings=None,
                 bindmap=None, bindings=None):
        ImageViewGtk.ImageViewZoom.__init__(self, logger=logger,
                                            rgbmap=rgbmap,
                                            settings=settings,
                                            bindmap=bindmap,
                                            bindings=bindings)
        CompoundMixin.__init__(self)
        CanvasMixin.__init__(self)
        DrawingMixin.__init__(self)

        for name in ('modified', ):
            self.enable_callback(name)

        #self.canvas.add(self)
        self.set_canvas(self)

        self.setSurface(self)
        self.ui_setActive(True)

        # for displaying modal keyboard state
        self.mode_obj = None
        bm = self.get_bindmap()
        bm.add_callback('mode-set', self.mode_change_cb)
        self.add_callback('configure', self._configure_cb)


    def update_canvas(self, whence=3):
        self.logger.debug("updating canvas")
        self.redraw(whence=whence)

    def redraw_data(self, whence=0):
        super(ImageViewCanvas, self).redraw_data(whence=whence)

        if not self.surface:
            return
        self.draw(self)

    def mode_change_cb(self, bindmap, mode, modetype):
        # delete the old indicator
        obj = self.mode_obj
        self.mode_obj = None
        if obj:
            try:
                self.deleteObject(obj)
            except:
                pass

        # if not one of the standard modifiers, display the new one
        if not mode in (None, 'ctrl', 'shift'):
            Text = self.getDrawClass('text')
            Rect = self.getDrawClass('rectangle')
            Compound = self.getDrawClass('compoundobject')

            if modetype == 'locked':
                text = '%s [L]' % (mode)
            else:
                text = mode

            xsp, ysp = 4, 6
            wd, ht = self.get_window_size()
            x1, y1 = wd-12*len(text), ht-12
            o1 = Text(x1, y1, text,
                      fontsize=14, color='yellow', coord='canvas')
            wd, ht = self.renderer.get_dimensions(o1)

            # yellow text on a black filled rectangle
            o2 = Compound(Rect(x1-xsp, y1+ysp, x1+wd+xsp, y1-ht,
                               color='black',
                               fill=True, fillcolor='black', coord='canvas'),
                               o1)
            self.mode_obj = o2
            self.add(o2)

        return True

    def _configure_cb(self, view, width, height):
        # redraw the mode indicator since the window has been resized
        bm = view.get_bindmap()
        mode, modetype = bm.current_mode()
        self.mode_change_cb(bm, mode, modetype)

#END
