我正在使用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 :(得分:0)
我有这个解决方案。即使使用我测试的非ASCII字符,它也能正常工作。但是,我仍然想知道是否有办法使用TextMark
来实现。
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
答案 1 :(得分: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