Dialog - TextGadget misses linebreak support

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2137
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Dialog - TextGadget misses linebreak support

Post by Andre »

I can imagine, that automatically linebreaks are difficult to support in resizable Dialogs (because PB will automatically calculate the maximum needed box/window width according to the text-length...), but unlike the regular TextGadget() it doesn't even support a linebreak forced by the programmer.

See this examples:

Example 1 - manually Gadget coding:

Code: Select all

  a$ = "Very very looooooong name + another loooooonnnnggggg term"    ; for this long text-line a linebreak will automatically be done, if the TextGadget is small...
  a$ + Chr(10) + "further text on a new line..."    ; the Chr(10) will force a linebreak
  
  If OpenWindow(0, 0, 0, 270, 160, "TextGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    TextGadget(0, 10,  10, 250, 70, a$)
    Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
  EndIf
Example 2 - the same text displayed in a dialog misses even the Chr(10) forced linebreaks:

Code: Select all

  CompilerIf #PB_Compiler_Unicode
    #XmlEncoding = #PB_UTF8
  CompilerElse 
    #XmlEncoding = #PB_Ascii
  CompilerEndIf

  #Dialog = 0
  #Xml = 0
  
  a$ = "Very very looooooong name + another loooooonnnnggggg term"
  a$ + Chr(10) + "further text on a new line..."
  
  
;   If OpenWindow(0, 0, 0, 270, 160, "TextGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
;     TextGadget(0, 10,  10, 250, 70, a$)
;     Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
;   EndIf
  
  
  XML$ = "<window id='#PB_Any' name='test' text='test' minwidth='auto' minheight='auto' flags='#PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_SizeGadget'>" +
         "      <vbox>" +
         "        <text text='" + a$ + "' />" +      
         "      </vbox>" +
         "</window>"
  
  If CatchXML(#Xml, @XML$, StringByteLength(XML$), 0, #XmlEncoding) And XMLStatus(#Xml) = #PB_XML_Success
    
    If CreateDialog(#Dialog) And OpenXMLDialog(#Dialog, #Xml, "test")
      
      Repeat
        Event = WaitWindowEvent()
      Until Event = #PB_Event_CloseWindow 
      
    Else  
      Debug "Dialog error: " + DialogError(#Dialog)
    EndIf
  Else
    Debug "XML error: " + XMLError(#Xml) + " (Line: " + XMLErrorLine(#Xml) + ")"
  EndIf  
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2137
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: Dialog - TextGadget misses linebreak support

Post by Andre »

As this problem occurs more often when I try to rewrite all my GUI stuff into new Dialog XML descriptions (causing Dialog windows to be much widther because of one-line textgadgets, which could easily be using a higher height value and display texts multi-line) I need a workaround as long Fred doesn't implement this wish natively.

Any ideas?
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
PureLust
Enthusiast
Enthusiast
Posts: 477
Joined: Mon Apr 16, 2007 3:57 am
Location: Germany, NRW

Re: Dialog - TextGadget misses linebreak support

Post by PureLust »

Andre wrote:Any ideas?
Hey André, ... if you create the Text-Element without Text and set the width manually, you can apply the Multiline-Text to the Textgadget afterwards (with SetGadgetText()).
Using this method, the Text-Element will do word-wrap automatically and also supports the CHR(10) as Linefeed.

Greetz, Albert.

Code: Select all

CompilerIf #PB_Compiler_Unicode
    #XmlEncoding = #PB_UTF8
  CompilerElse 
    #XmlEncoding = #PB_Ascii
  CompilerEndIf

  #Dialog = 0
  #Xml = 0
  
  a$ = "Very very looooooong name + another loooooonnnnggggg term"
  a$ + Chr(10) + Chr(10) + "further text on a new line..."
  
  
  XML$ = "<window id='#PB_Any' name='test' text='test' minwidth='auto' minheight='auto' flags='#PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_SizeGadget'>" +
         "      <vbox>" +
         "        <text name='TextFeld' width='80' height='55' />" +      
         "      </vbox>" +
         "</window>"
  
  If CatchXML(#Xml, @XML$, StringByteLength(XML$), 0, #XmlEncoding) And XMLStatus(#Xml) = #PB_XML_Success
    
    If CreateDialog(#Dialog) And OpenXMLDialog(#Dialog, #Xml, "test")
    	
    	ResizeWindow(DialogWindow(#Dialog), #PB_Ignore, #PB_Ignore, 220, #PB_Ignore)
    	
    	SetGadgetText(DialogGadget(#Dialog, "TextFeld"), a$)
    	
      Repeat
        Event = WaitWindowEvent()
      Until Event = #PB_Event_CloseWindow 
      
    Else  
      Debug "Dialog error: " + DialogError(#Dialog)
    EndIf
  Else
    Debug "XML error: " + XMLError(#Xml) + " (Line: " + XMLErrorLine(#Xml) + ")"
  EndIf 
[Dynamic-Dialogs] - create complex GUIs the easy way
[DeFlicker] - easily deflicker your resizeable Windows
[WinFX] - Window Effects (incl. 'click-through' Window)
PureLust
Enthusiast
Enthusiast
Posts: 477
Joined: Mon Apr 16, 2007 3:57 am
Location: Germany, NRW

Re: Dialog - TextGadget misses linebreak support

Post by PureLust »

Andre wrote:I can imagine, that automatically linebreaks are difficult to support in resizable Dialogs ...
I just recognized, that 'RefreshDialog()' resizes an MultiLined-Textgadget (including CHR(10)) the way, you would expect from the beginning.
So it seems, that there is already a routine which can calculate the right size.

It looks like, that PB just uses a different (simpler) algorithm when it creates the Text-Element, than it does with 'RefreshDialog()'.

So maybe it's not a big deal for Fred to use the 'ResfeshDialog()' routine for the creation process of a Text-Element as well ?!? :D

Code: Select all

CompilerIf #PB_Compiler_Unicode
	#XmlEncoding = #PB_UTF8
CompilerElse 
	#XmlEncoding = #PB_Ascii
CompilerEndIf

#Dialog = 0
#Xml = 0

Runtime Enumeration Gadgets
	#gadText
	#gadButton
EndEnumeration

a$ = "Try to resize the Windows to see the word-wrap in action."
a$ + Chr(10) + Chr(10) + "Yes ... I'm a working New-Line..."


XML$ = "<window id='#PB_Any' name='test' text='test' minwidth='auto' minheight='auto' flags='#PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_SizeGadget'>" +
       "      <hbox>" +
       "        <text name='TextFeld' width='100' height='5' expand='yes'/>" +
       "        <button id='#gadButton' text='RefreshDialog()'/>" +
       "      </hbox>" +
       "</window>"

If CatchXML(#Xml, @XML$, StringByteLength(XML$), 0, #XmlEncoding) And XMLStatus(#Xml) = #PB_XML_Success
	
	If CreateDialog(#Dialog) And OpenXMLDialog(#Dialog, #Xml, "test")
		
		ResizeWindow(DialogWindow(#Dialog), #PB_Ignore, #PB_Ignore, 280,48)
		SetGadgetText(DialogGadget(#Dialog, "TextFeld"), a$)
		
		Repeat
			Event = WaitWindowEvent()
			If Event = #PB_Event_Gadget And EventType() = #PB_EventType_LeftClick
				RefreshDialog(#Dialog)			; <---  you can use RefreshDialog, to resize the Text-Element to the width/height you would have expected during creation.
			EndIf
		Until Event = #PB_Event_CloseWindow 
		
	Else  
		Debug "Dialog error: " + DialogError(#Dialog)
	EndIf
Else
	Debug "XML error: " + XMLError(#Xml) + " (Line: " + XMLErrorLine(#Xml) + ")"
EndIf 
[Dynamic-Dialogs] - create complex GUIs the easy way
[DeFlicker] - easily deflicker your resizeable Windows
[WinFX] - Window Effects (incl. 'click-through' Window)
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2137
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: Dialog - TextGadget misses linebreak support

Post by Andre »

Good idea, thank you! :D

I successfully implemented the 'Save the multi-line text in strings, create the dialog with empty TextGadgets, and do SetGadgetText() and a RefreshDialog() after dialog creation' in my project.

Of course this "complicates" the dialog creation and should be considered for implementing the directly at dialog creation:
a) support for multi-line texts given at dialog creation
b) automatically linebreaks when there are sizes for the <TextGadget> given and/or parent objects like <SingleBox> etc. are set
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
PureLust
Enthusiast
Enthusiast
Posts: 477
Joined: Mon Apr 16, 2007 3:57 am
Location: Germany, NRW

Re: Dialog - TextGadget misses linebreak support

Post by PureLust »

Hi André, ... little update on this:

I just become aware, that Multiline-Support works fine, if you do it the right way. :wink:

Because CatchXML() needs a proper XML-Text, you just have to use the right XML-Code instead of 'CHR(10)', which is '&#10;' for a NewLine. :mrgreen:

Maybe THIS PAGE will be helpfull with other special Characters (like '&') as well.

Greetz and have fun,
Albert.

[EDIT:]

Here is a little Routine, that will do the Job for you:
(It converts all unsupported or problematic ASCii-Character into their equivelant HTML-Code.)

Code: Select all

Procedure.s	Text2HTML(Text$)		; converts all unsupported ASCii-Chars into HTML-Code
	Protected Pos=1
	Text$ = ReplaceString(Text$, "&", "&")
	Text$ = ReplaceString(Text$, Chr(9), "&#9;")
	Text$ = ReplaceString(Text$, Chr(10), "&#10;")
	Text$ = ReplaceString(Text$, Chr(39), "'")
	Text$ = ReplaceString(Text$, Chr(60), "<")
	Text$ = ReplaceString(Text$, Chr(62), ">")
	While pos < Len(Text$)
		If Asc(Mid(Text$,Pos,1)) < 32
			Text$ = Left(Text$,Pos-1)+Mid(Text$,pos+1)
		Else
			Pos + 1
		EndIf
	Wend
	ProcedureReturn Text$
EndProcedure
Last edited by PureLust on Fri Jan 22, 2016 3:45 pm, edited 1 time in total.
[Dynamic-Dialogs] - create complex GUIs the easy way
[DeFlicker] - easily deflicker your resizeable Windows
[WinFX] - Window Effects (incl. 'click-through' Window)
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2137
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: Dialog - TextGadget misses linebreak support

Post by Andre »

Hi Albert,

thank you for this tipp! I already tried it successfully in my project - this could avoid the additionally SetGadgetText() and allows directly including the needed text content in the XML definition, just like it should.... :)

I had already built me a similar conversion procedure, but until now mainly for characters which are breaking the XML definition or lead to a wrong display, like: " ' & etc.

So we have the needed workarounds now. Now it's up to Fred and would be very welcome, to implement a native linebreak / correct height calculation of longer texts, without making pre-calculations and manually inserting linebreaks...
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2137
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: Dialog - TextGadget misses linebreak support

Post by Andre »

@Albert: Do you have already checked, if all chars listet on the mentioned website (http://dev.w3.org/html5/html-author/charref) are problemativ for XML dialog definitions? If yes, I think it's worth adding a note about this (problematic chars + need for a conversion, link to the table) to the PB manual...
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
PureLust
Enthusiast
Enthusiast
Posts: 477
Joined: Mon Apr 16, 2007 3:57 am
Location: Germany, NRW

Re: Dialog - TextGadget misses linebreak support

Post by PureLust »

Andre wrote:@Albert: Do you have already checked, if all chars listet on the mentioned website (http://dev.w3.org/html5/html-author/charref) are problemativ for XML dialog definitions?...
Hi André,

yes, I've already checked that.
All characters which i found out were problematic, are covered by the Text2HTML() routine above.

So, as you can see in the code, only the Chars from 0-31, and & ' < > results in Problems and have to be converted.

As you can see in the result of the sample-code in THIS post, the conversion works quit well. :wink:
[Dynamic-Dialogs] - create complex GUIs the easy way
[DeFlicker] - easily deflicker your resizeable Windows
[WinFX] - Window Effects (incl. 'click-through' Window)
Post Reply