Listicongadget multiple lines with textwrap

Share your advanced PureBasic knowledge/code with the community.
jesperbrannmark
Enthusiast
Enthusiast
Posts: 536
Joined: Mon Feb 16, 2009 10:42 am
Location: sweden
Contact:

Listicongadget multiple lines with textwrap

Post by jesperbrannmark »

Just a simple thing if you want listicongadget to not cut off your text but continue on a new line with a new listicongadget-item.
Just one column, uses startdrawing which might be awful in many peoples eyes... but it does its job.

Code: Select all

;Listicongadget multiline works so-so, especially if you decide to go cross platform
;This is a simple workaround.
;Use it any way you want to
;Jesper Brännmark, 2011
mytext.s="This is a line that is longer than the actual listicongadget and will therefor be cut off on several lines"

Procedure AddGadgetItem_wrap(gadget.l,text.s) 
  width=300-30
  StartDrawing(ImageOutput(0))
  DrawingFont(FontID(0))  
  If TextWidth(text)<=width 
    AddGadgetItem(gadget.l,-1,TEXT.S);
  Else 
    limit=0 : Repeat : limit+1 : Until TextWidth(Left(text,limit))>width : cut=limit 
    Repeat : cut-1 : Until Mid(text,cut,1)=" " Or Mid(text,cut,1)="-" Or cut=0 
    If cut=0 : cut=limit-1 : EndIf 
    AddGadgetItem(gadget.l,-1,Left(text,cut))
    StopDrawing()
    AddGadgetItem_wrap(gadget.l,Right(text,Len(text)-cut))
  EndIf 
  StopDrawing()
EndProcedure 
;
OpenWindow(0,0,0,400,400,"Listicongadget textwrap")
CreateImage(0,400,400)
LoadFont(0,"Arial",12)
ListIconGadget(0,0,0,400,400,"Question",300)
SetGadgetFont(0,FontID(0))
AddGadgetItem(0,-1,mytext) ;-gets cut off
AddGadgetItem(0,-1,"") ;empty line 
AddGadgetItem_wrap(0,mytext) ;-splits into several lines
Repeat
Until WaitWindowEvent()=#PB_Event_CloseWindow
WilliamL
Addict
Addict
Posts: 1252
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Listicongadget multiple lines with textwrap

Post by WilliamL »

jesperbrannmark,

So you are using the StartDrawing essentially to change the font and get text width? I made this same thing without using the drawing commands but had to estimate (or constant) the text width and I added word wrap. Wasn't too bad but I haven't needed to use it. Just an exercise. :)

(on vacation in Key West! :D )
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
Post Reply