import ROOT as R

# just an example of an root object to be displayed. This object could also be a member of the class.
root_obj = R.TF1('power_func', '[0]*abs(x)**[1]', -1, 2)
root_obj.SetParameters( 1, 2 )

class pMainFrame( R.TGMainFrame ):
    def __init__( self, parent, width, height ):
        R.TGMainFrame.__init__( self, parent, width, height )

        self.fTitle = R.TGLabel(self)
        self.fTitle.SetBackgroundColor( 0x002200 )
        self.fTitle.SetForegroundColor( 0xFFFFFF )
        #self.fTitle.SetTextFont('helvetica') # or courier, or times. But it messes up the text size.
        self.AddFrame( self.fTitle, R.TGLayoutHints( R.kLHintsExpandX | R.kLHintsTop, 25, 25, 5, 5) )
        
        self.SetCleanup( R.kDeepCleanup )
        self.canvas = R.TRootEmbeddedCanvas( 'canvas', self, int(0.9*width), int(0.6*height) )
        self.AddFrame( self.canvas, R.TGLayoutHints( R.kLHintsTop | R.kLHintsExpandY | R.kLHintsCenterX, 5, 5, 5, 5 ) )
        canvas = self.canvas.GetCanvas()
        canvas.cd()
        root_obj.Draw()
        self.update_params()
        self.fTitle.ChangeText('Press the arrow keys...')
        
        self.fButton = R.TGTextButton( self, 'E&xit Application', 10 )
        self.AddFrame( self.fButton, R.TGLayoutHints( R.kLHintsExpandX | R.kLHintsBottom, 20, 20, 20, 20) )
        self.fQuitDispatch = R.TPyDispatcher( self.quit )
        self.fButton.Connect( 'Clicked()', 'TPyDispatcher', self.fQuitDispatch, 'Dispatch()' )

        self.fTextEntry = R.TGTextEntry( self, '' , 50)
        self.AddFrame( self.fTextEntry, R.TGLayoutHints( R.kLHintsExpandX, 20, 20, 20, 20) )

        self.AddInput( R.kKeyPressMask )
        R.gVirtualX.GrabKey(self.GetId(), R.gVirtualX.KeysymToKeycode(R.kKey_F3), R.kKeyControlMask, 1 )

        self.fEventDispatch = R.TPyDispatcher( self.EventSlot )
        self.Connect( 'ProcessedEvent(Event_t*)', 'TPyDispatcher', self.fEventDispatch, 'Dispatch(Event_t*)' )
        self.canvas.GetContainer().Connect( 'ProcessedEvent(Event_t*)', 'TPyDispatcher',
                                            self.fEventDispatch, 'Dispatch(Event_t*)' )
        self.MapSubwindows()
        self.Layout()
        self.MapWindow()
        #self.Resize(150,100)


    def __del__( self ):
        self.Cleanup()


    def HandleKey( self, event ):
        print "pMainFrame::HandleKey() : key pressed! But this method never gets called."


    def EventSlot( self, event ):
        if event.fType == R.kGKeyPress:
            if event.fCode in ( 24, 9 ):
                self.quit()
            elif event.fCode in ( 33, ):
                print 'Parameters are: {}, {}'.format( root_obj.GetParameter(0), root_obj.GetParameter(1) )
            elif event.fCode == 111:
                root_obj.SetParameter( 0, 1.25 * root_obj.GetParameter( 0 ) )
            elif event.fCode == 116:
                root_obj.SetParameter( 0, 0.8 * root_obj.GetParameter( 0 ) )
            elif event.fCode == 113:
                root_obj.SetParameter( 1, 1.25 * root_obj.GetParameter( 1 ) )
            elif event.fCode == 114:
                root_obj.SetParameter( 1, 0.8 * root_obj.GetParameter( 1 ) )
            else:
                print "pMainFrame::EventSlot() : the {} key was pressed.".format(event.fCode)
            self.update_params()


    def update_params(self):
        self.fTitle.ChangeText( 'Parameters are {:.4f} and {:.4f}'.format( root_obj.GetParameter(0),
                                                                           root_obj.GetParameter(1) ) )
        tcanvas = self.canvas.GetCanvas()
        tcanvas.Modified()
        tcanvas.Update()
        
    def quit(self):
        print 'Bye bye...'
        self.CloseWindow()
        # The following lines quit everything. But that is outside the scope of this object.
        # For example, when using this class from the iPython prompt, we want to keep the
        # both the R.gApplication and the terminal alive.
        #R.gApplication.Terminate()
        #exit()


if __name__ == '__main__':
    window = pMainFrame( R.gClient.GetRoot(), 300, 500 )
