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

我发现设置选择的唯一功能需要TextIters,而不是TextMarks。但是我还没有找到任何函数来从 TextMark 中获取 TextIter。这是正确的程序吗?

4

2 回答 2

0

我有这个解决方案。即使使用我测试的非 ASCII 字符,它也可以工作。但是,我仍然想知道是否有办法使用TextMarks.

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  
于 2013-04-04T14:30:41.087 回答
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
于 2013-04-04T17:43:40.567 回答