
import math
import Tkinter, Pmw
import tkSimpleDialog

#class LayoutDialog(tkSimpleDialog.Dialog):
class LayoutDialog:
    def __init__ (self, image_disp, parent, *args, **kwargs):
        self.image_disp = image_disp
        self.angle = 0
        self.dialog = Pmw.Dialog(parent,
                                  buttons = ('Done',),
                                  defaultbutton = None,
                               )
        
        context = self.image_disp.layout_context

        self.dialog.title("Layout Scaling")

        frame = Tkinter.Frame(self.dialog.interior())
        frame.pack()
        
        Tkinter.Label(frame, text='X translation').grid(row=0, sticky=Tkinter.W)
        Tkinter.Label(frame, text='Y translation').grid(row=1, sticky=Tkinter.W)
        Tkinter.Label(frame, text='X scale').grid(row=2, sticky=Tkinter.W)
        Tkinter.Label(frame, text='Y scale').grid(row=3, sticky=Tkinter.W)
        Tkinter.Label(frame, text='Rotation').grid(row=4, sticky=Tkinter.W)

        button = Tkinter.Button(frame, text='Center Here',
                       command = self.center_layout)
        button.grid(row=0, column=2, rowspan=2)

        self.x_trans = Tkinter.Entry(frame, width=10)
        self.y_trans = Tkinter.Entry(frame, width=10)
        self.x_multiplier = Tkinter.Scale(frame, from_ = 0.1, to = 10.0,
                                          orient=Tkinter.HORIZONTAL,
                                          resolution = 0.01)
        self.y_multiplier = Tkinter.Scale(frame, from_ = 0.1, to = 10.0,
                                          orient=Tkinter.HORIZONTAL,
                                          resolution = 0.01)
        self.rotation_scale = Tkinter.Scale(frame, from_ = 0, to = 360,
                                            resolution = 1,
                                            orient=Tkinter.HORIZONTAL)

        self.x_trans.grid(row=0, column=1, sticky=Tkinter.W)
        self.y_trans.grid(row=1, column=1, sticky=Tkinter.W)
        self.x_multiplier.grid(row=2, column=1, sticky=Tkinter.W)
        self.y_multiplier.grid(row=3, column=1, sticky=Tkinter.W)
        self.rotation_scale.grid(row=5, column=1, sticky=Tkinter.W)

        self.angle = math.acos(context.angle_cos) 
        self.rotation_scale.set(self.angle)
        self.x_multiplier.set(context.x_multiplier)
        self.y_multiplier.set(context.y_multiplier)
        self.x_trans.insert('0', str(context.x_offset))
        self.y_trans.insert('0', str(context.y_offset))

        self.x_trans.bind('<Return>', self.update_layout)
        self.y_trans.bind('<Return>', self.update_layout)
        self.x_multiplier.bind('<ButtonRelease-1>', self.update_layout)
        self.y_multiplier.bind('<ButtonRelease-1>', self.update_layout)
        self.rotation_scale.bind('<ButtonRelease-1>', self.update_layout)

        
    def update_layout (self, event):
        print 'updated'
        context = self.image_disp.layout_context

        x_offset = int(self.x_trans.get())
        y_offset = int(self.y_trans.get())
        x_multiplier = self.x_multiplier.get()
        y_multiplier = self.y_multiplier.get() 
        angle = self.rotation_scale.get()

        modified = 0

        if angle != self.angle:
            self.angle = angle
            context.set_angle(angle)
            modified = 1
            
        if (x_offset != context.x_offset or y_offset != context.y_offset):
            context.set_offset(x_offset, y_offset)
            modified = 1

        if (x_multiplier != context.x_multiplier or
            y_multiplier != context.y_multiplier):
            context.set_multiplier(x_multiplier, y_multiplier)
            modified = 1

        if modified:
            self.image_disp.redisplay_layout()

    def center_layout (self):
        "Set layout to current location"
        state = self.image_disp.client.state
        context = self.image_disp.layout_context

        context.set_offset(-state.x, -state.y)
        
if __name__ == '__main__':
    root = Tkinter.Tk()
    Pmw.initialise(root, fontScheme = 'pmw1')   

    f = Tkinter.Frame(root)
    d = LayoutDialog(None, f, buttons = ('OK', 'Apply', 'Cancel'),
                      title = 'Move Layout', defaultbutton = 'OK')
    root.mainloop()
    
        


