2013-04-04 75 views
0

我使用的是Gtk2Hs,所有這些GTK的東西都是我的新東西。 我正在使用TextView。我想用 替換當前選中的 文本並添加一些新的文本,並選擇新的文本。最近 我已經能夠拿出的是:替換選定的文本並選擇新文本

-- Create marks so I can "remember" where the selection was 
(startIter, stopIter) <- textBufferGetSelectionBounds buffer 
startMark <- textBufferCreateMark buffer (Just "start") startIter True 
stopMark <- textBufferCreateMark buffer (Just "stop") stopIter True 

-- Delete the currently selected text 
textBufferDeleteSelection buffer True True 
-- now startIter and stopIter are no longer valid 

-- Insert the new text 
somehow convert startMark to startIter2 ??? 
textBufferInsert buffer startIter2 text 
-- now startIter2 is no longer valid 

-- Select the new text 
somehow convert startMark to startIter3 ??? 
somehow convert stopMark to stopIter3 ??? 
textBufferSelectRange buffer startIter3 stopIter3 

我發現設置選擇的唯一功能需要TextIter S, 不TextMark秒。但是我一直沒有找到任何功能來從TextMark中獲得一個 TextIter。這是正確的程序嗎?

回答

0

好的,我找到了一種使用TextMarks的方法。我正在尋找的功能是textBufferGetIterAtMark

textBufferReplaceSelection 
    ∷ TextBufferClass self ⇒ self → String → IO() 
textBufferReplaceSelection buffer text = do 
    -- Create marks so I can "remember" where the selection was 
    (startIter, stopIter) <- textBufferGetSelectionBounds buffer 
    startMark <- textBufferCreateMark buffer (Just "start") startIter False 
    stopMark <- textBufferCreateMark buffer (Just "stop") stopIter True 

    -- Delete the currently selected text 
    textBufferDeleteSelection buffer True True 
    -- now startIter and stopIter are no longer valid 

    -- Insert the new text 
    startIter2 <- textBufferGetIterAtMark buffer startMark 
    textBufferInsert buffer startIter2 text 
    -- now startIter2 is no longer valid 

    -- Select the new text 
    startIter3 <- textBufferGetIterAtMark buffer startMark 
    stopIter3 <- textBufferGetIterAtMark buffer stopMark 
    textBufferSelectRange buffer startIter3 stopIter3 
0

我有這個解決方案。即使使用我測試的非ASCII字符,它也能正常工作。但是,我仍然想知道是否有辦法使用TextMark s來完成。

textBufferReplaceSelection buffer text = do 
    textBufferDeleteSelection buffer True True 
    textBufferInsertAtCursor buffer text 
    (_, end) <- textBufferGetSelectionBounds buffer 
    start <- textIterCopy end 
    textIterBackwardChars start (length text) 
    textBufferMoveMarkByName buffer "selection_bound" start