It is currently Tue Oct 22, 2019 8:52 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: [BUG] Dialog - wrong width-calculation for '&' (= '&')
PostPosted: Thu Jan 21, 2016 4:23 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Mon Apr 16, 2007 3:57 am
Posts: 409
Location: Germany, NRW
The width of a Gadget seems to be calculated wrong, if it includes a '&'.

Code:
EnableExplicit

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

#xmlText   = 0
#xmlDialog = 0

#SampleAMP$ = "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"

Define   XML$ = "<window id='0' name='WinMain' height='200' minwidth='auto' text='Wrong GadgetWidth'  flags='#PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_SizeGadget'>" +
                "      <vbox spacing='0'>" +
                "         <singlebox  expand='horizontal'>" +
                "            <text Name='Text1' text='Both Lines should display 30  x  &apos;&amp;&apos;    '/>" +
                "         </singlebox>" +
                "         <singlebox  expand='horizontal'>" +
                "            <text Name='Text2' text='"+#SampleAMP$+"' flags='#PB_Text_Border'/>" +
                "         </singlebox>" +
                "         <singlebox  expand='no'>" +
                "            <text Name='Text3' text='"+#SampleAMP$+"' flags='#PB_Text_Border'/>" +
                "         </singlebox>" +
                "      </vbox>" +
                "</window>"

If Not (CatchXML(#xmlText, @xml$, StringByteLength(xml$), 0, #XmlEncoding) And XMLStatus(#xmlText) = #PB_XML_Success)
   MessageRequester("Fehler in XML-Definition !!!", "XML-Definition '"+xml$+"' konnte nicht decodiert werden."+Chr(10)+Chr(10)+"XML error: " + XMLError(#xmlText) + " (Line: " + XMLErrorLine(#xmlText) + ")", #PB_MessageRequester_Ok)
EndIf

If CreateDialog(#xmlDialog) And OpenXMLDialog(#xmlDialog, #xmlText, "winMain")
    Debug "GadgetWith should be > "+Str(GadgetWidth(DialogGadget(#xmlDialog, "Text1")))
    Debug ""
    Debug "GadgetWith(Line1) = "+Str(GadgetWidth(DialogGadget(#xmlDialog, "Text2")))+"   -  expand = yes or horizontal"
    Debug "GadgetWith(Line2) = "+Str(GadgetWidth(DialogGadget(#xmlDialog, "Text3")))+"   -  expand = no, or vertical"
    
    Repeat
      Define Event = WaitWindowEvent()
   Until Event = #PB_Event_CloseWindow
   
Else 
   Debug "Dialog error: " + DialogError(#xmlDialog)
EndIf


Last edited by PureLust on Fri Jan 22, 2016 12:37 am, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Dialog - wrong width-calculation for '&amp;' (= '&')
PostPosted: Thu Jan 21, 2016 11:39 pm 
Offline
PureBasic Team
PureBasic Team
User avatar

Joined: Fri Apr 25, 2003 6:14 pm
Posts: 1704
Location: Germany (Saxony, Deutscheinsiedel)
Confirmed! On Win8.1 with PB5.41 x64

Just to proof with a "more realistic" text content I've extended your example with two additional strings 'Sample2$' and 'Sample3$'. They show that there is a problem (only) with the width of text including the '&', but not with another sign like the '"' (doublequote).

Code:

; The width of a Gadget seems To be calculated wrong, If it includes a '&'.

EnableExplicit

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

#xmlText   = 0
#xmlDialog = 0

#SampleAMP$ = "&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;"

#Sample2$ = "Text1 &amp; Text2 &amp; Text3"
#Sample3$ = "&quot;Text in Doublequotes&quot;"

Define   XML$ = "<window id='0' name='WinMain' height='200' minwidth='auto' text='Wrong GadgetWidth'  flags='#PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_SizeGadget'>" +
                "      <vbox spacing='0'>" +
                "         <singlebox  expand='horizontal'>" +
                "            <text Name='Text1' text='Both Lines should display 30  x  &apos;&amp;&apos;    '/>" +
                "         </singlebox>" +
                "         <singlebox  expand='horizontal'>" +
                "            <text Name='Text2' text='"+#SampleAMP$+"' flags='#PB_Text_Border'/>" +
                "         </singlebox>" +
                "         <singlebox  expand='no'>" +
                "            <text Name='Text3' text='"+#SampleAMP$+"' flags='#PB_Text_Border'/>" +
                "         </singlebox>" +
                "         <singlebox  expand='no'>" +
                "            <text Name='Text4' text='"+#Sample2$+"' flags='#PB_Text_Border'/>" +
                "         </singlebox>" +
                "         <singlebox  expand='no'>" +
                "            <text Name='Text5' text='"+#Sample3$+"' flags='#PB_Text_Border'/>" +
                "         </singlebox>" +
                "      </vbox>" +
                "</window>"

If Not (CatchXML(#xmlText, @xml$, StringByteLength(xml$), 0, #XmlEncoding) And XMLStatus(#xmlText) = #PB_XML_Success)
   MessageRequester("Fehler in XML-Definition !!!", "XML-Definition '"+xml$+"' konnte nicht decodiert werden."+Chr(10)+Chr(10)+"XML error: " + XMLError(#xmlText) + " (Line: " + XMLErrorLine(#xmlText) + ")", #PB_MessageRequester_Ok)
EndIf

If CreateDialog(#xmlDialog) And OpenXMLDialog(#xmlDialog, #xmlText, "winMain")
    Debug "GadgetWith should be > "+Str(GadgetWidth(DialogGadget(#xmlDialog, "Text1")))
    Debug ""
    Debug "GadgetWith(Line1) = "+Str(GadgetWidth(DialogGadget(#xmlDialog, "Text2")))+"   -  expand = yes or horizontal"
    Debug "GadgetWith(Line2) = "+Str(GadgetWidth(DialogGadget(#xmlDialog, "Text3")))+"   -  expand = no, or vertical"
   
    Repeat
      Define Event = WaitWindowEvent()
   Until Event = #PB_Event_CloseWindow
   
Else
   Debug "Dialog error: " + DialogError(#xmlDialog)
 EndIf
 

_________________
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)


Top
 Profile  
Reply with quote  
 Post subject: Re: [BUG] Dialog - wrong width-calculation for '&amp;' (= '&
PostPosted: Fri Jan 22, 2016 2:51 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Mon Apr 16, 2007 3:57 am
Posts: 409
Location: Germany, NRW
I had tested all other characters as well, and only '&' seems to be problematic.

The following Test-Program lists 2 other Characters with interesting sizes as well.
On these Characters the generated GadgetWidth is different to the width, given back from TextWidth().
Especially Chr(49) (= '1') amazes me, because I always thought, that all numeric Characters ("0"-"9") does have the same Font-Width (as they have with TextWidth()).
But, other that with '&', on these two Characters, all Chars are shown and fit into the calculated GadgetWidth - so this looks ok to me.

There seems to be a difference in how TextWidth() generates/calculates a Text, and how the OS-Routine does it for Gadgets.

But, nevertheless ... '&' generates an unusual result.

Request: Could anyone run this routine and test it on Linux or Mac?

Hint: Because of THIS Bug, I had to split the display into 3 different ScrollAreas, otherwise it will create an IMA because there will be more than 102 Entries in the ScrollArea.)
Code:
CompilerIf #PB_Compiler_Unicode
   #XmlEncoding = #PB_UTF8
CompilerElse
   #XmlEncoding = #PB_Ascii
CompilerEndIf

Procedure.s   Txt2XML(Text$)      ; converts all unsupported ASCii-Chars into HTML-Code
   Protected Pos=1
   Text$ = ReplaceString(Text$, "&", "&amp;")
   Text$ = ReplaceString(Text$, Chr(9), "&#9;")
   Text$ = ReplaceString(Text$, Chr(10), "&#10;")
   Text$ = ReplaceString(Text$, Chr(39), "&#39;")
   Text$ = ReplaceString(Text$, Chr(60), "&lt;")
   Text$ = ReplaceString(Text$, Chr(62), "&gt;")
   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

#Dialog = 0
#Xml = 0
Define myFont, n, m, o, Text$, GadgetTextWidth, RealTextWidth

Runtime Enumeration Gadgets
   #gadText
   #gadButton
EndEnumeration

Define myFont = LoadFont(#PB_Any, "Arial",8, #PB_Font_HighQuality)
SetGadgetFont(#PB_Default, FontID(myFont))

XML$ = "<window id='0' name='test' text='GadgetWidth() to TextWidth() comparsion' minwidth='auto' minheight='400' flags='#PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_SizeGadget'>" +
       "   <hbox>"

For o = 0 To 2
   XML$ + "      <scrollarea name='ScrollArea' scrolling='vertical'>"  +
          "         <vbox>"
   
   For n = 32 + o*75 To 32+74 + o*75
      Text$ = ""
      If n <= 255
         For m = 1 To 30 : Text$ + Chr(n) :    Next
         XML$ + "         <hbox align='left' expand='no'>" +
                "           <text name='ChrFeld_"+Str(n)+"' text='Chr("+Str(n)+")' width='20' expand='no'/>" +
                "           <text name='TextFeld_"+Str(n)+"' text='"+Txt2XML(Text$)+"' expand='no'/>" +
                "         </hbox>"
      EndIf
   Next
   
   XML$ + "      </vbox></scrollarea>"
Next
XML$ + "</hbox></window>"


If CatchXML(#Xml, @XML$, StringByteLength(XML$), 0, #XmlEncoding) And XMLStatus(#Xml) = #PB_XML_Success
   
   If CreateDialog(#Dialog) And OpenXMLDialog(#Dialog, #Xml, "test")
      
      
      For n = 32 To 255
         If StartDrawing(WindowOutput(0))
            DrawingFont(FontID(myFont))
            
            GadgetTextWidth = GadgetWidth(DialogGadget(#Dialog, "TextFeld_"+Str(n)))
            RealTextWidth   = TextWidth(GetGadgetText(DialogGadget(#Dialog, "TextFeld_"+Str(n))))
            
            If GadgetTextWidth-2 <> RealTextWidth
               Debug "Size Problem: Chr("+Str(n)+") - is Length:"+Str(GadgetTextWidth-2)+" - should be: "+Str(RealTextWidth)+"     -    " +
                     "Size of a single Character:  Gadget: "+StrF((GadgetWidth(DialogGadget(#Dialog, "TextFeld_"+Str(n)))-2)/30,2) +
                     "   -  TextWidth(): "+StrF(RealTextWidth/30)
               SetGadgetColor(DialogGadget(#Dialog, "ChrFeld_"+Str(n)), #PB_Gadget_FrontColor  , $0000ff)
               SetGadgetColor(DialogGadget(#Dialog, "TextFeld_"+Str(n)), #PB_Gadget_FrontColor  , $0000ff)
            EndIf
            StopDrawing()
         EndIf
      Next
      
      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


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye