import wx
import wx.xrc
import wx.richtext
dic1={}
print wx.version()

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 |wx.FULL_REPAINT_ON_RESIZE)
		
		self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
        
		bSizer1 = wx.BoxSizer( wx.VERTICAL )
		
		self.m_checkBox1 = wx.CheckBox( self, wx.ID_ANY, u"checkbox1", wx.DefaultPosition, wx.DefaultSize, 0 )
		self.m_checkBox1.SetForegroundColour(wx.BLACK)
		self.m_checkBox1.SetBackgroundColour(wx.BLUE)
		bSizer1.Add( self.m_checkBox1, 0, wx.ALL, 5 )

		self.m_richText3 = wx.richtext.RichTextCtrl(self,wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, wx.TE_READONLY|wx.VSCROLL|wx.NO_BORDER|wx.WANTS_CHARS )
		self.m_richText3.Bind(wx.EVT_IDLE, self.UnderlineWords)
		self.dc1 = wx.ClientDC(self.m_richText3)
		self.underlining_on = False
        
		bSizer1.Add( self.m_richText3, 1, wx.EXPAND |wx.ALL, 5 )

		self.SetSizer( bSizer1 )


		text="After reading the ridiculous offer letter, I am convinced that this proposal is a bad deal for our company."
		wods=text.split()
                for word in 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=='ridiculous' or word=='convinced':
                        dic1[word]=[start,end]
                self.m_checkBox1.Bind( wx.EVT_CHECKBOX, self.m_checkBox1OnCheckBox )
        
        def m_checkBox1OnCheckBox( self, event ):
                if self.underlining_on:
                    self.UnderlineWords(event)
                    self.underlining_on = False
                else:
                    self.underlining_on = True

                
        def UnderlineWords(self,event):
                if self.underlining_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(dic1['ridiculous'][0]+1)
                            p=self.m_richText3.GetCaret().GetPosition()
                            print p
                            x1=p[0]
                            x2=x1+w1
                            y=p[1]+h1
                        
                            dc1.DrawLine(x1,y,x2,y)
                            #draw underline for word 'convinced'
                        
                            self.m_richText3.SetInsertionPoint(dic1['convinced'][0]+1)
                            p1=self.m_richText3.GetCaret().GetPosition()
                            print p1
                            x11=p1[0]
                            x22=x11+w2
                            y2=p1[1]+h2
                        
                            dc1.DrawLine(x11,y2,x22,y2)

                            self.Bind( wx.EVT_PAINT, self.OnPaint)
                    else:

                            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(dic1['ridiculous'][0]+1)
                            p=self.m_richText3.GetCaret().GetPosition()
                            x1=p[0]
                            x2=x1+w1
                            y=p[1]+h1
                        
                            dc2.DrawLine(x1,y,x2,y)
                            #erease the underline for word 'convinced'
                            self.m_richText3.SetInsertionPoint(dic1['convinced'][0]+1)
                            p1=self.m_richText3.GetCaret().GetPosition()
                            x11=p1[0]
                            x22=x11+w2
                            y2=p1[1]+h2
                            dc2.DrawLine(x11,y2,x22,y2)
                        

if __name__ == "__main__":
        app=MyApp(False)
        app.MainLoop()
                
                                  
