Posted by
| Nick Gammon
Australia (23,042 posts) bio
Forum Administrator |
Message
| You can't do it like that as you are not calculating the width of the word in a variable-pitch font. You need to do along the lines of what I suggested. This works for me:
-- test text
pages = [[
I want pages and whispers to print to a miniwindow. I want to append new messages like you can do with the
notepad and possibly allow for scrolling to read previous messages (though having previous lines scroll up would
be fine as well). I tried the notepad route and it's just way too annoying and isn't flexible enough.
]]
-- window and font info
win = GetPluginID ()
local width = 700
local height = 200
local font = "f2"
-- make window
WindowCreate (win, 0, 0, width, height, 12, 0, ColourNameToRGB("darkslategray"))
-- show border so we can see what we are doing
WindowRectOp (win, 1, 1, 1, -1, -1, ColourNameToRGB("blue"))
-- set up our font
WindowFont (win, font, "Tahoma", 9, false, true, false, false)
-- how far in to draw
local left_margin = 5
local right_margin = 5
-- derive stuff from font
local space_width = WindowTextWidth (win, font, " ")
local line_height = WindowFontInfo (win, font, 1)
-- starting point
local x = left_margin
local y = line_height
-- do each word
for word in string.gmatch (pages, "%S+") do
local word_width = WindowTextWidth (win, font, word)
-- time to start a new line?
if (word_width + x) > (width - right_margin) and (x > left_margin) then
y = y + line_height
x = left_margin
end -- if
-- draw the text
WindowText (win, font, word, x, y, 0, 0, ColourNameToRGB("white"))
-- advance past the word and a single space
x = x + word_width + space_width
end -- for loop
WindowShow (win, true)
You can test that in an Immediate window. The code above breaks the text into words, and tries to fit each word on the line, starting a new line if necessary. It does not honour newlines (or multiple consecutive spaces).
For a newline (eg. a new chat line) simply start a new line as it does above, ie.
y = y + line_height
x = left_margin
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | top |
|