import wx
import wx.xrc
import wx.richtext
import docx
list1=[]
list2=[]
list3=[]

dic1={}
dic2={'ridiculous':[0,0],'convinced':[0,0],'primary':[0,0,'primarry'],'secondary':[0,0,'secondarry'],'run':[0,0,'runs'],'walks':[0,0,'run']}
###########################################################################
## Class MyFrame1
###########################################################################

class MyApp(wx.App):
        def OnInit(self):
                self.frame = Frame1(None, )
                self.SetTopWindow(self.frame)
                self.frame.Show()
                return True




class Frame1 ( wx.Frame ):
	
	def __init__( self, parent ):
		wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 815,512 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
		
		self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
		
		sbSizer1 = wx.StaticBoxSizer( wx.StaticBox( self, wx.ID_ANY, u"label" ), wx.VERTICAL )
		
		gSizer1 = wx.GridSizer( 1, 3, 0, 0 )
		
		self.m_checkBox1 = wx.CheckBox( sbSizer1.GetStaticBox(), wx.ID_ANY, u"checkbox1", wx.DefaultPosition, wx.DefaultSize, 0 )
		self.m_checkBox1.SetForegroundColour(wx.BLACK)
		self.m_checkBox1.SetBackgroundColour(wx.BLUE)
		gSizer1.Add( self.m_checkBox1, 0, wx.ALL, 5 )
		
		self.m_checkBox2 = wx.CheckBox( sbSizer1.GetStaticBox(), wx.ID_ANY, u"checkbox2", wx.DefaultPosition, wx.DefaultSize, 0 )
		self.m_checkBox2.SetForegroundColour(wx.BLACK)
		self.m_checkBox2.SetBackgroundColour(wx.GREEN)
		gSizer1.Add( self.m_checkBox2, 0, wx.ALL, 5 )
		
		self.m_checkBox3 = wx.CheckBox( sbSizer1.GetStaticBox(), wx.ID_ANY, u"checkbox3", wx.DefaultPosition, wx.DefaultSize, 0 )
		self.m_checkBox3.SetForegroundColour(wx.BLACK)
		self.m_checkBox3.SetBackgroundColour(wx.RED)
		gSizer1.Add( self.m_checkBox3, 0, wx.ALL, 5 )
		
		
		sbSizer1.Add( gSizer1, 0, wx.EXPAND, 5 )
		
		self.m_richText3 = wx.richtext.RichTextCtrl( sbSizer1.GetStaticBox(), wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, wx.TE_READONLY|wx.VSCROLL|wx.HSCROLL|wx.NO_BORDER|wx.WANTS_CHARS )
		sbSizer1.Add( self.m_richText3, 1, wx.EXPAND |wx.ALL, 5 )
		
		self.m_richText4 = wx.richtext.RichTextCtrl( sbSizer1.GetStaticBox(), wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, wx.TE_READONLY|wx.VSCROLL|wx.HSCROLL|wx.NO_BORDER|wx.WANTS_CHARS )
		sbSizer1.Add( self.m_richText4, 1, wx.EXPAND |wx.ALL, 5 )
		
		
		self.m_richText3.Bind(wx.EVT_IDLE, self.Check1UnderlineWords)
		self.m_richText3.Bind(wx.EVT_IDLE, self.Check2UnderlineWords)
		self.m_richText3.Bind(wx.EVT_IDLE, self.Check3UnderlineWords)
		self.dc1 = wx.ClientDC(self.m_richText3)
		
		self.Check1underlining_on = False
		self.Check2underlining_on = False
		self.Check3underlining_on = False
		self.Hilight1_on= False
		self.Hilight2_on= False
		self.Hilight3_on= False
		self.SetSizer( sbSizer1 )
		self.Layout()
		
		
		self.Centre( wx.BOTH )
		doc1= "After reading the ridiculous offer letter, I am convinced that this proposal is a bad deal for our company.\n I started my schooling as the majority did in my area, at the local primarry school. I then went to the local secondarry school and recieved gradin English, Maths, Phisics, Biology, Geography, Art, Graphical Comunication and Philosophy of Religeon. I'll not bore you with the 'A' levels and above.\nSeeing the dogs runs toward him, he walk quickly to avoid them."
		doc2= "After reading the ridiculous offer letter, I am convinced that this proposal is a bad deal for our company.\n 2.	I started my schooling as the majority did in my area, at the local primary school. I then went to the local secondary school and received grades in English, Maths, Phisics, Biology, Geography, Art, Graphical Communication and Philosophy of Religeon. I'll not bore you with the 'A' levels and above.\nSeeing the dogs run toward him, he walks quickly to avoid them."
		
		
		
                doc2_wods=doc2.split()
                for word in doc2_wods:
                    start=self.m_richText3.GetCaretPosition()
                    self.m_richText3.WriteText(word)
                    pos2=self.m_richText3.GetCaret().GetPosition()
                    end=self.m_richText3.GetCaretPosition()
                    self.m_richText3.WriteText(' ')
                    if word in dic2.keys():
                            dic2[word][0]=start
                            dic2[word][1]=end
                        

		
                doc1_wods=doc1.split()
                for word in doc1_wods:
                    start=self.m_richText4.GetCaretPosition()
                    self.m_richText4.WriteText(word)
                    end=self.m_richText4.GetCaretPosition()
                    self.m_richText4.WriteText(' ')
                    dic1[word]=[start,end]
                        
                
		
		list1=['ridiculous','convinced']
		list2=['primary','secondary']
		list3=['run','walks']
		
		
		self.m_richText3.Bind(wx.EVT_MOTION, self.OnHover)
		self.m_checkBox1.Bind( wx.EVT_CHECKBOX, self.m_checkBox1OnCheckBox )
		
		self.m_checkBox2.Bind( wx.EVT_CHECKBOX, self.m_checkBox2OnCheckBox )
		
		self.m_checkBox3.Bind( wx.EVT_CHECKBOX, self.m_checkBox3OnCheckBox )
		
	
	def __del__( self ):
        
		pass
	
	def m_checkBox1OnCheckBox( self, event ):
                if self.Check1underlining_on:
                        self.Check1UnderlineWords(event)
                        self.Check1underlining_on = False
                else:
                        self.Check1underlining_on = True
                        
        def m_checkBox2OnCheckBox ( self, event ):
                if self.Check2underlining_on:
                        self.Check2UnderlineWords(event)
                        self.Check2underlining_on = False
                else:
                        self.Check2underlining_on = True

        def m_checkBox3OnCheckBox ( self, event ):
                if self.Check3underlining_on:
                        self.Check3UnderlineWords(event)
                        self.Check3underlining_on = False
                else:
                        self.Check3underlining_on = True 
               
                
                
        def Check1UnderlineWords(self,event):
                if self.Check1underlining_on == True:
                        
                        dc1 = self.dc1
                        w1, h1 = self.m_richText3.GetTextExtent('ridiculous')
                        w2, h2 = self.m_richText3.GetTextExtent('convinced')
                        if self.m_checkBox1.GetValue():
                               
                                dc1.SetPen(wx.Pen(wx.BLUE,3, wx.DOT))
                            #draw underline for word 'ridiculous'
                        
                                self.m_richText3.SetInsertionPoint(dic2['ridiculous'][0]+1)
                                p=self.m_richText3.GetCaret().GetPosition()
                            
                                x1=p[0]
                                x2=x1+w1
                                y=p[1]+h1-2
                        
                                dc1.DrawLine(x1,y,x2,y)
                            #draw underline for word 'convinced'
                        
                                self.m_richText3.SetInsertionPoint(dic2['convinced'][0]+1)
                                p1=self.m_richText3.GetCaret().GetPosition()
                            
                                x11=p1[0]
                                x22=x11+w2
                                y2=p1[1]+h2-2
                        
                                dc1.DrawLine(x11,y2,x22,y2)
                                self.Hilight1_on=True
                                

                                
                        else:
                                self.Hilight1_on=False
                                dc2 = wx.ClientDC(self.m_richText3)
                                dc2.SetPen(wx.Pen((255,255,255),3, wx.DOT))
                       
                            #erease the underline for word 'ridiculous'
                        
                                self.m_richText3.SetInsertionPoint(dic2['ridiculous'][0]+1)
                                p=self.m_richText3.GetCaret().GetPosition()
                                x1=p[0]
                                x2=x1+w1
                                y=p[1]+h1-2
                        
                                dc2.DrawLine(x1,y,x2,y)
                            #erease the underline for word 'convinced'
                                self.m_richText3.SetInsertionPoint(dic2['convinced'][0]+1)
                                p1=self.m_richText3.GetCaret().GetPosition()
                                x11=p1[0]
                                x22=x11+w2
                                y2=p1[1]+h2-2
                                dc2.DrawLine(x11,y2,x22,y2)
                event.Skip()
                
	
                        

       

        
        

                                        
                        
                        
        def Check2UnderlineWords(self,event):
                if self.Check2underlining_on == True:
                    dc1 = self.dc1
                    w1, h1 = self.m_richText3.GetTextExtent('primary')
                    w2, h2 = self.m_richText3.GetTextExtent('secondary')
                    if self.m_checkBox2.GetValue():
                            dc1.SetPen(wx.Pen(wx.GREEN,3, wx.DOT))
                          
                        
                            self.m_richText3.SetInsertionPoint(dic2['primary'][0]+2)
                            p=self.m_richText3.GetCaret().GetPosition()
                            
                            x1=p[0]
                            x2=x1+w1-9
                            y=p[1]+h1+1
                        
                            dc1.DrawLine(x1,y,x2,y)
                           
                        
                            self.m_richText3.SetInsertionPoint(dic2['secondary'][0]+2)
                            p1=self.m_richText3.GetCaret().GetPosition()
        
                            x11=p1[0]
                            x22=x11+w2-9
                            y2=p1[1]+h2+1
                        
                            dc1.DrawLine(x11,y2,x22,y2)

                            self.Hilight2_on =True
                    else:
                            self.Hilight2_on =False
                            dc2 = wx.ClientDC(self.m_richText3)
                            dc2.SetPen(wx.Pen((255,255,255),3, wx.DOT))
                       
                            #erease the underline 
                        
                            self.m_richText3.SetInsertionPoint(dic2['secondary'][0]+2)
                            p=self.m_richText3.GetCaret().GetPosition()
                            x1=p[0]
                            x2=x1+w1-9
                            y=p[1]+h1+1
                        
                            dc2.DrawLine(x1,y,x2,y)
                            #erease the underline 
                            self.m_richText3.SetInsertionPoint(dic2['primary'][0]+2)
                            p1=self.m_richText3.GetCaret().GetPosition()
                            x11=p1[0]
                            x22=x11+w2-9
                            y2=p1[1]+h2+1
                            dc2.DrawLine(x11,y2,x22,y2)
                event.Skip()
                
        def Check3UnderlineWords(self,event):
                if self.Check3underlining_on == True:
                    dc1 = self.dc1
                    w1, h1 = self.m_richText3.GetTextExtent('run')
                    w2, h2 = self.m_richText3.GetTextExtent('walks')
                    if self.m_checkBox3.GetValue():
                            dc1.SetPen(wx.Pen(wx.RED,3, wx.DOT))
                            #draw underline 
                        
                            self.m_richText3.SetInsertionPoint(dic2['run'][0]+2)
                            p=self.m_richText3.GetCaret().GetPosition()
                            
                            x1=p[0]
                            x2=x1+w1-9
                            y=p[1]+h1+1
                        
                            dc1.DrawLine(x1,y,x2,y)
                            #draw underline 
                        
                            self.m_richText3.SetInsertionPoint(dic2['walks'][0]+2)
                            p1=self.m_richText3.GetCaret().GetPosition()
        
                            x11=p1[0]
                            x22=x11+w2-9
                            y2=p1[1]+h2+1
                        
                            dc1.DrawLine(x11,y2,x22,y2)

                            self.Hilight3_on =True
                    else:
                            self.Hilight3_on =False
                            dc2 = wx.ClientDC(self.m_richText3)
                            dc2.SetPen(wx.Pen((255,255,255),3, wx.DOT))
                       
                            #erease the underline 
                        
                            self.m_richText3.SetInsertionPoint(dic2['run'][0]+2)
                            p=self.m_richText3.GetCaret().GetPosition()
                            x1=p[0]
                            x2=x1+w1-9
                            y=p[1]+h1+1
                        
                            dc2.DrawLine(x1,y,x2,y)
                            #erease the underline 
                            self.m_richText3.SetInsertionPoint(dic2['walks'][0]+2)
                            p1=self.m_richText3.GetCaret().GetPosition()
                            x11=p1[0]
                            x22=x11+w2-9
                            y2=p1[1]+h2+1
                            dc2.DrawLine(x11,y2,x22,y2)
                event.Skip()

                
        def  OnHover(self,event):
                if self.Hilight1_on == True:
                        pos= event.GetPosition()
                        line, col = self.m_richText3.HitTestPos(pos)
                        for word in list1:
                                print word
                                C1=dic2[word][0]
                                C2=dic2[word][1]
                                print C1,C2
                                if col>=C1 and col<=C2:
                                        print word
                                        attr = wx.richtext.RichTextAttr()
                                        attr.SetFlags(wx.TEXT_ATTR_BACKGROUND_COLOUR)
                                        attr.SetBackgroundColour((190,212,236))
                                        start=dic1[word][0]
                                        end=dic1[word][1]
                                        self.m_richText4.SetStyle([start+1,end+1],attr)
                                else:
                                        attr = wx.richtext.RichTextAttr()
                                        attr.SetFlags(wx.TEXT_ATTR_BACKGROUND_COLOUR)
                                        attr.SetBackgroundColour((255,255,255))
                                        start=dic1['After'][0]
                                        end=dic1['them.'][1]
                                        self.m_richText4.SetStyle([start+1,end+1],attr)
                        
                if self.Hilight2_on ==True:
                        pos= event.GetPosition()
                        line, col = self.m_richText3.HitTestPos(pos)
                       
                        for word in list2:
                                C1=dic2[word][0]
                                C2=dic2[word][1]
                                if (col<=C2 and col>=C1):
                                        print word
                                        attr = wx.richtext.RichTextAttr()
                                        attr.SetFlags(wx.TEXT_ATTR_BACKGROUND_COLOUR)
                                        attr.SetBackgroundColour((190,212,236))
                                        start=dic1[dic2[word][2]][0]
                                        end=dic1[dic2[word][2]][1]
                                        self.m_richText4.SetStyle([start+1,end+1],attr)
                                        break
                                else:
                                        attr = wx.richtext.RichTextAttr()
                                        attr.SetFlags(wx.TEXT_ATTR_BACKGROUND_COLOUR)
                                        attr.SetBackgroundColour((255,255,255))
                                        start=dic1['After'][0]
                                        end=dic1['them.'][1]
                                        self.m_richText4.SetStyle([start+1,end+1],attr)
                                
                       

                if self.Hilight3_on ==True:
                        pos= event.GetPosition()
                        line, col = self.m_richText3.HitTestPos(pos)
                        for word in list3:
                                C1=dic2[word][0]
                                C2=dic2[word][1]
                                if (col<=C2 and col>=C1):
                                        print word
                                        attr = wx.richtext.RichTextAttr()
                                        attr.SetFlags(wx.TEXT_ATTR_BACKGROUND_COLOUR)
                                        attr.SetBackgroundColour((190,212,236))
                                        start=dic1[dic2[word][2]][0]
                                        end=dic1[dic2[word][2]][1]
                                        self.m_richText4.SetStyle([start+1,end+1],attr)
                                        break
                                else:
                                        attr = wx.richtext.RichTextAttr()
                                        attr.SetFlags(wx.TEXT_ATTR_BACKGROUND_COLOUR)
                                        attr.SetBackgroundColour((255,255,255))
                                        start=dic1['After'][0]
                                        end=dic1['them.'][1]
                                        self.m_richText4.SetStyle([start+1,end+1],attr)
                                

 
                        
	
	

if __name__ == "__main__":
        app=MyApp(False)
        app.MainLoop()
