#!/usr/bin/python
# -*- coding: UTF-8 -*-

'''
Simple Imageviewer by
Adrian Immler
http://code.uebergeek.de/

Version: 0.3
Date:    2006-06-13
'''

'''
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
'''

import gtk, os, sys

APP_TITLE = 'Image Viewer'
fileexts = ['.jpg','jpeg','.png','.gif','tiff','.tif','.svg', '.ico']

class box_image(gtk.VBox):
  def __init__(self, imgfile):
    gtk.VBox.__init__(self)
    scrolled = gtk.ScrolledWindow()
    scrolled.set_policy(gtk.POLICY_AUTOMATIC,gtk.POLICY_AUTOMATIC)
    self.image = gtk.Image()
    scrolled.add_with_viewport(self.image)
    self.add(scrolled)
    self.set_image(imgfile, 100) # 100 = no-zoom / original size

  def set_image(self, imgfile, scale):
    pixbuf = gtk.gdk.pixbuf_new_from_file(imgfile)
    width = pixbuf.get_width()
    height = pixbuf.get_height()
    scale = scale / 100.0
    scaled_buf = pixbuf.scale_simple( int(width*scale),int(height*scale),gtk.gdk.INTERP_BILINEAR)
    self.image.set_from_pixbuf(scaled_buf)



class imageviewer(gtk.VBox):
  def __init__(self, imgfile, window = None):
    gtk.VBox.__init__(self)
    self.img = box_image(imgfile)
    self.zoom = 100
    self.imgfile = imgfile
    if window != None: self.win = window

    toolbar = gtk.Toolbar()

    #.append_item(text, tooltip_text, tooltip_private_text, icon, callback, user_data=None)
    #.insert_stock(stock_id, tooltip_text, tooltip_private_text, callback, user_data, position)
    toolbar.insert_stock(gtk.STOCK_GOTO_FIRST, None, None, self.__file_first, None, 1)
    toolbar.insert_stock(gtk.STOCK_GO_BACK,    None, None, self.__file_prev, None, 2)
    toolbar.insert_stock(gtk.STOCK_GO_FORWARD, None, None, self.__file_next, None, 3)
    toolbar.insert_stock(gtk.STOCK_GOTO_LAST,  None, None, self.__file_last, None, 4)
    toolbar.insert_space(5)
    toolbar.insert_stock(gtk.STOCK_ZOOM_IN,    None, None, self.__zoom_in, None, 5)
    toolbar.insert_stock(gtk.STOCK_ZOOM_100,   None, None, self.__zoom_100, None, 6)
    toolbar.insert_stock(gtk.STOCK_ZOOM_OUT,   None, None, self.__zoom_out, None, 7)

    toolbar.set_style(gtk.TOOLBAR_ICONS) # TEXT | ICONS | BOTH
    self.pack_start(toolbar, False, False)
    self.pack_start(self.img, True, True)
    #print os.path.dirname(imgfile)
    #print os.path.basename(imgfile)

    self.files = []
    for file in os.listdir(os.path.dirname(imgfile)):
      if file[-4:].lower() in fileexts:
        self.files.append(os.path.dirname(imgfile) + '/' + file)

    self.win.set_title(APP_TITLE + ': ' + imgfile)

  def __file_first(self, emitter):
    self.set_image(self.files[0])

  def __file_prev(self, emitter):
    imgfile = self.files[self.files.index(self.imgfile)-1]
    self.set_image(imgfile)

  def __file_next(self, emitter):
    size = len(self.files)
    if size == self.files.index(self.imgfile)+1:
      imgfile = self.files[0]
    else:
      imgfile = self.files[self.files.index(self.imgfile)+1]
    self.set_image(imgfile)

  def __file_last(self, emitter):
    self.set_image(self.files[-1])


  def set_image(self, imgfile, zoom = 100):
    self.imgfile = imgfile
    self.zoom = zoom
    self.img.set_image(imgfile, zoom)
    imgof = '[%s/%s]' % ( self.files.index(self.imgfile)+1,len(self.files) )
    title = '%s: %s %s' % ( APP_TITLE, imgof, imgfile )
    self.win.set_title(title)


  def set_zoom(self, value):
    self.set_image(self.imgfile, value)

  def __zoom_in(self, emitter):
    if self.zoom < 300:
      self.zoom = self.zoom + 10
      self.set_zoom(self.zoom)

  def __zoom_100(self, emitter):
    self.zoom = 100
    self.set_zoom(self.zoom)

  def __zoom_out(self, emitter):
    if self.zoom > 10:
      self.zoom = self.zoom - 10
      self.set_zoom(self.zoom)



if __name__ == '__main__':
  if len(sys.argv) < 2:
    print 'Filename Missing'
  else:
    imgfile = sys.argv[1]

    window = gtk.Window()
    window.set_position(1)
    window.set_title('Image Viewer')
    window.set_resizable( True )
    window.resize(640,510)
    viewer = imageviewer(imgfile, window)
    window.add(viewer)
    window.connect('destroy', lambda win: gtk.main_quit())
    window.show_all()
    gtk.main()