AutoSize() for text gadgets - Cross Platform

Share your advanced PureBasic knowledge/code with the community.
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

AutoSize() for text gadgets - Cross Platform

Post by TI-994A »

A function to auto-size text gadgets using native PureBasic functions. The text gadget is either shrunk to the closest longest word or phrase in the caption, or is expanded to the longest word in the caption. Works with left, right and centre alignments. Tested and working on Windows and OSX, and should also work on Linux.

Includes a ready-to-use demo to test with customisable captions, and selectable fonts, sizes and text alignments:

Code: Select all

;========================================================
;  AutoSize() for automatically sizing text gadgets -
;  expands the width of an undersized text gadget to
;  fit the longest word in the caption, or shrinks an
;  oversized text gadget to the width of the longest
;  possible phrase in the caption - height corresponds
;
;  for Windows, OSX (and Linux?)
;
;  tested with PureBasic v5.31 on Windows 8.1 Pro (x64)
;  and PureBasic v5.2 on on OSX 10.7.5 (x64)
;
;  by TI-994A - free to use, improve, share...
;
;  18th December 2014
;========================================================

;==============================================================================================
; fully cross-platform, self-contained text gadget auto-sizing function - just pop-in and call
;==============================================================================================
Procedure AutoSize(gadgetNo)
  Shared alignFlag
  gadgetWt = GadgetWidth(gadgetNo)
  gadgetHt = GadgetHeight(gadgetNo, #PB_Gadget_RequiredSize)
  gadgetText.s = (ReplaceString(Trim(GetGadgetText(gadgetNo)), "  ", " ")) + " "
  tempGadget = TextGadget(#PB_Any, -1000, -1000, gadgetWt, gadgetHt, "", alignFlag)
  SetGadgetFont(tempGadget, GetGadgetFont(gadgetNo))
  HideGadget(tempGadget, 1)
  For textIndex = 1 To CountString(gadgetText, " ")
    If alignFlag = #PB_Text_Right
      SetGadgetText(tempGadget, " " + StringField(gadgetText, textIndex, " "))
    Else
      SetGadgetText(tempGadget, StringField(gadgetText, textIndex, " "))
    EndIf
    textIndexWidth = GadgetWidth(tempGadget, #PB_Gadget_RequiredSize)
    If textIndexWidth > gadgetWt
      gadgetWt = textIndexWidth
      expanded = 1
    EndIf
  Next textIndex
  
  If Not expanded
    textIndexWidth = 0
    For textIndex = 1 To CountString(gadgetText, " ")
      SetGadgetText(tempGadget, spacer$ + StringField(gadgetText, textIndex, " "))
      currentStringWidth = GadgetWidth(tempGadget, #PB_Gadget_RequiredSize) - 2
      textIndexWidth + currentStringWidth
      If textIndexWidth => gadgetWt
        If textIndexWidth - currentStringWidth > maxWidth
          maxWidth = textIndexWidth - currentStringWidth
        EndIf
        If textIndexWidth > gadgetWt
          spacer$ = " " 
        Else 
          spacer$ = ""
        EndIf        
        If alignFlag = #PB_Text_Right
          SetGadgetText(tempGadget, " " + StringField(gadgetText, textIndex, " "))
        Else
          SetGadgetText(tempGadget, StringField(gadgetText, textIndex, " "))
        EndIf      
        textIndexWidth = GadgetWidth(tempGadget, #PB_Gadget_RequiredSize) - 2
      Else 
        spacer$ = " "
      EndIf  
    Next textIndex
    
    If textIndexWidth > maxWidth
      maxWidth = textIndexWidth
    EndIf    
    
    If maxWidth
      gadgetWt = maxWidth + 2
    EndIf
  EndIf
  
  spacer$ = ""
  textIndexWidth = 0
  
  For textIndex = 1 To CountString(gadgetText, " ")
    SetGadgetText(tempGadget, spacer$ + StringField(gadgetText, textIndex, " "))
    textIndexWidth + GadgetWidth(tempGadget, #PB_Gadget_RequiredSize) - 2
    If textIndexWidth => gadgetWt
      If textIndexWidth > gadgetWt
        spacer$ = " " 
      Else 
        spacer$ = ""
      EndIf
      If textIndex = CountString(gadgetText, " ")
        lines + 2
      Else
        lines + 1
      EndIf
      If alignFlag = #PB_Text_Right
        spacer$ = " " 
        SetGadgetText(tempGadget, " " + StringField(gadgetText, textIndex, " "))
      Else
        SetGadgetText(tempGadget, StringField(gadgetText, textIndex, " "))
      EndIf
      If textIndexWidth > gadgetWt
        textIndexWidth = GadgetWidth(tempGadget, #PB_Gadget_RequiredSize) - 2
      Else
        textIndexWidth = 0
      EndIf
    Else 
      spacer$ = " "
      If textIndex = CountString(gadgetText, " ")
        lines + 1
      EndIf
    EndIf
  Next textIndex
  
  gadgetHt * lines
  
  ResizeGadget(gadgetNo, #PB_Ignore, #PB_Ignore, gadgetWt, gadgetHt)
  SetGadgetText(gadgetNo, Trim(gadgetText))
  FreeGadget(tempGadget) 
  
EndProcedure
;==============================================================================================

;===========
; demo code
;===========
Enumeration
  #window1
  #label1
  #label2
  #label3
  #label4
  #textDemo
  #left
  #centre
  #right
  #fontName
  #fontSize
  #maxWidth
  #results
  #sample
  #set
  #font1
  #enterKey
EndEnumeration

Declare updateTextGadget()

wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
OpenWindow(#window1, #PB_Any, #PB_Any, 700, 600, "AutoSize TextGadget", wFlags)
AddKeyboardShortcut(#window1, #PB_Shortcut_Return, #enterKey)

TextGadget(#label1, 30, 20, 150, 20, "Select Font:")
TextGadget(#label2, 190, 20, 50, 20, "Font Size:")
TextGadget(#label3, 250, 20, 40, 20, "Width:")
TextGadget(#label4, 30, 70, 200, 20, "Sample Caption:")

ComboBoxGadget(#fontName, 30, 40, 150, 20)
AddGadgetItem(#fontName, -1, "Arial")
AddGadgetItem(#fontName, -1, "Book Antiqua")
AddGadgetItem(#fontName, -1, "Century Gothic")
AddGadgetItem(#fontName, -1, "Lucida Console")
AddGadgetItem(#fontName, -1, "Tahoma")
AddGadgetItem(#fontName, -1, "Trebuchet MS")
AddGadgetItem(#fontName, -1, "Times New Roman")
SetGadgetState(#fontName, 6)

ComboBoxGadget(#fontSize, 190, 40, 50, 20)
AddGadgetItem(#fontSize, -1, "10")
AddGadgetItem(#fontSize, -1, "20")
AddGadgetItem(#fontSize, -1, "30")
AddGadgetItem(#fontSize, -1, "40")
AddGadgetItem(#fontSize, -1, "50")
AddGadgetItem(#fontSize, -1, "60")
SetGadgetState(#fontSize, 4)

StringGadget(#maxWidth, 250, 40, 40, 23, "300")

OptionGadget(#left, 30, 120, 80, 25, " left align")
OptionGadget(#centre, 120, 120, 80, 25, " centre text")
OptionGadget(#right, 222, 120, 80, 25, " right align")
SetGadgetState(#left, 1)

StringGadget(#sample, 30, 90, 200, 23, "A rose by any other name would smell as sweet.")
ButtonGadget(#set, 240, 90, 50, 23, "SET")

ListViewGadget(#results, 30, 170, 260, 400)

LoadFont(#font1, GetGadgetText(#fontName), Val(GetGadgetText(#fontSize)))
TextGadget(#textDemo, 350, 20, Val(GetGadgetText(#maxWidth)), 10, GetGadgetText(#sample))
SetGadgetFont(#textDemo, FontID(#font1))
SetGadgetColor(#textDemo, #PB_Gadget_BackColor, RGB(120, 0, 0))
SetGadgetColor(#textDemo, #PB_Gadget_FrontColor, RGB(220, 220, 0))

updateTextGadget()

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      appQuit = 1
    Case #PB_Event_Menu
      updateTextGadget()
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #set, #fontName, #fontSize
          updateTextGadget()
        Case #left, #centre, #right
          Select EventGadget()
            Case #left
              alignFlag = 0
            Case #centre
              alignFlag = #PB_Text_Center
            Case #right
              alignFlag = #PB_Text_Right
          EndSelect
          updateTextGadget()
      EndSelect
  EndSelect
Until appQuit = 1 

Procedure updateTextGadget()
  Shared alignFlag
  Static currentAlignment
  
  If currentAlignment <> alignFlag
    TextGadget(#textDemo, 350, 20, Val(GetGadgetText(#maxWidth)), 10, GetGadgetText(#sample), alignFlag)
    SetGadgetColor(#textDemo, #PB_Gadget_BackColor, RGB(120, 0, 0))
    SetGadgetColor(#textDemo, #PB_Gadget_FrontColor, RGB(220, 220, 0))
    currentAlignment = alignFlag
  EndIf
  
  init = 1
  fontName.s = GetGadgetText(#fontName)
  fontSize = Val(GetGadgetText(#fontSize))
  LoadFont(#font1, fontName, fontSize)
  SetGadgetFont(#textDemo, FontID(#font1))
  SetGadgetText(#textDemo, Trim(GetGadgetText(#sample)))
  ResizeGadget(#textDemo, #PB_Ignore, #PB_Ignore, Val(GetGadgetText(#maxWidth)), #PB_Ignore)
  AddGadgetItem(#results, 0, "initialised size: " + Str(GadgetWidth(#textDemo)) + " x " + Str(GadgetHeight(#textDemo)))
  AddGadgetItem(#results, 1, "font: " + GetGadgetText(#fontName) + " @ " + GetGadgetText(#fontSize) + " points")
  AutoSize(#textDemo)
  AddGadgetItem(#results, 2, "--> AutoSized to: " + Str(GadgetWidth(#textDemo)) + " x " + Str(GadgetHeight(#textDemo)))
  AddGadgetItem(#results, 3, " ")
  SetGadgetState(#results, 2)
  
  ExamineDesktops()
  
  If GadgetWidth(#textDemo) > 320
    wWidth = GadgetWidth(#textDemo) + 380
    If wWidth > DesktopWidth(0)
      wWidth = DesktopWidth(0) - 100
    EndIf
  Else
    wWidth = 700
  EndIf
  
  If GadgetHeight(#textDemo) > 520
    wHeight = GadgetHeight(#textDemo) + 40
    If wHeight > DesktopHeight(0)
      wHeight = DesktopHeight(0) - 100
    EndIf
    lHeight = wHeight - 200
  Else
    wHeight = 600
    lHeight = 400
  EndIf
  
  winX = (DesktopWidth(0) - wWidth) / 2
  winY = (DesktopHeight(0) - wHeight) / 2
  ResizeWindow(#window1, winX, winY, wWidth, wHeight)
  ResizeGadget(#results, #PB_Ignore, #PB_Ignore, #PB_Ignore, lHeight)
  
EndProcedure
There is a minor inaccuracy when the gadget is expanded - if the initial gadget size is smaller than any word in the caption, and has to expand, it sometimes adds an additional blank line. If anyone knows what's causing this, please update the code. No known issues otherwise.

Thank you. :D
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
electrochrisso
Addict
Addict
Posts: 980
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

Re: AutoSize() for text gadgets - Cross Platform

Post by electrochrisso »

Useful, thanks for sharing. :)
PureBasic! Purely one of the best 8)
User avatar
Kukulkan
Addict
Addict
Posts: 1352
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: AutoSize() for text gadgets - Cross Platform

Post by Kukulkan »

Nice, thank you!

Still would prefer some #PB_Text_AutoSize flag (http://www.purebasic.fr/english/viewtop ... =3&t=61139).
But helpful for the meantime :-)

Kukulkan
User avatar
Shardik
Addict
Addict
Posts: 1991
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: AutoSize() for text gadgets - Cross Platform

Post by Shardik »

Thank you TI-994A for your nice example which you claim in the title to be cross-platform and for which you cautiously state:
TI-994A wrote:Tested and working on Windows and OSX, and should also work on Linux.
While I can confirm that it works fine on Windows XP, Windows 7 and MacOS X "Snow Leopard", it unfortunately doesn't work correctly on Linux...

That's in part due to an error in the Linux version of PB, where the line wrap currently only works in TextGadgets with left-aligned text but not with centered or right-aligned text. I have therefore already posted this bug report together with a workaround.

Another problem only occurring on Linux is the wrong vertical autosizing for font sizes of 30 or bigger. These are the values displayed in the ListIconGadget #results (the values emphasized in red are too big!):
initialised size: 300 x 1659
Font: Times New Roman @ 10 points
--> AutoSized to: 290 x 17

initialised size: 300 x 17
Font: Times New Roman @ 20 points
--> AutoSized to: 283 x 96

initialised size: 300 x 96
Font: Times New Roman @ 30 points
--> AutoSized to: 293 x 576

initialised size: 300 x 576
Font: Times New Roman @ 40 points
--> AutoSized to: 254 x 1134

initialised size: 300 x 1134
Font: Times New Roman @ 50 points
--> AutoSized to: 274 x 1659

initialised size: 300 x 1659
Font: Times New Roman @ 60 points
--> AutoSized to: 268 x 2256
The reason for this took me quite some time for debugging: PB seems to calculate the height of a gadget differently in Linux compared to Windows and MacOS when using flag #PB_Gadget_RequiredSize. In Linux PB calculates the required gadget height for all lines taking into account line wrapping. In MacOS and Windows only the required height for a single line is calculated!

I have therefore taken TI-994A's example and added CompilerIf blocks for Linux to compensate for the above mentioned bug and the different behaviour in calculating the required gadget height:

Code: Select all

;========================================================
;  AutoSize() for automatically sizing text gadgets -
;  expands the width of an undersized text gadget to
;  fit the longest word in the caption, or shrinks an
;  oversized text gadget to the width of the longest
;  possible phrase in the caption - height corresponds
;
;  for Windows, OSX (and Linux?)
;
;  tested with PureBasic v5.31 on Windows 8.1 Pro (x64)
;  and PureBasic v5.2 on on OSX 10.7.5 (x64)
;
;  by TI-994A - free to use, improve, share...
;
;  18th December 2014
;
;  Modifications by Shardik to also work correctly on Linux
;
;  26th December 2014
;========================================================

;==============================================================================================
; fully cross-platform, self-contained text gadget auto-sizing function - just pop-in and call
;==============================================================================================
Procedure AutoSize(gadgetNo)
  Shared alignFlag
  gadgetWt = GadgetWidth(gadgetNo)
  CompilerIf #PB_Compiler_OS = #PB_OS_Linux
    gtk_label_set_line_wrap_(GadgetID(gadgetNo), #False)
    gadgetHt = GadgetHeight(gadgetNo, #PB_Gadget_RequiredSize)
    gtk_label_set_line_wrap_(GadgetID(gadgetNo), #True)
  CompilerEndIf
  gadgetText.s = (ReplaceString(Trim(GetGadgetText(gadgetNo)), "  ", " ")) + " "
  tempGadget = TextGadget(#PB_Any, -1000, -1000, gadgetWt, gadgetHt, "", alignFlag)
  SetGadgetFont(tempGadget, GetGadgetFont(gadgetNo))
  HideGadget(tempGadget, 1)
  For textIndex = 1 To CountString(gadgetText, " ")
    If alignFlag = #PB_Text_Right
      SetGadgetText(tempGadget, " " + StringField(gadgetText, textIndex, " "))
    Else
      SetGadgetText(tempGadget, StringField(gadgetText, textIndex, " "))
    EndIf
    textIndexWidth = GadgetWidth(tempGadget, #PB_Gadget_RequiredSize)
    If textIndexWidth > gadgetWt
      gadgetWt = textIndexWidth
      expanded = 1
    EndIf
  Next textIndex
 
  If Not expanded
    textIndexWidth = 0
    For textIndex = 1 To CountString(gadgetText, " ")
      SetGadgetText(tempGadget, spacer$ + StringField(gadgetText, textIndex, " "))
      currentStringWidth = GadgetWidth(tempGadget, #PB_Gadget_RequiredSize) - 2
      textIndexWidth + currentStringWidth
      If textIndexWidth => gadgetWt
        If textIndexWidth - currentStringWidth > maxWidth
          maxWidth = textIndexWidth - currentStringWidth
        EndIf
        If textIndexWidth > gadgetWt
          spacer$ = " "
        Else
          spacer$ = ""
        EndIf       
        If alignFlag = #PB_Text_Right
          SetGadgetText(tempGadget, " " + StringField(gadgetText, textIndex, " "))
        Else
          SetGadgetText(tempGadget, StringField(gadgetText, textIndex, " "))
        EndIf     
        textIndexWidth = GadgetWidth(tempGadget, #PB_Gadget_RequiredSize) - 2
      Else
        spacer$ = " "
      EndIf 
    Next textIndex
   
    If textIndexWidth > maxWidth
      maxWidth = textIndexWidth
    EndIf   
   
    If maxWidth
      gadgetWt = maxWidth + 2
    EndIf
  EndIf
 
  spacer$ = ""
  textIndexWidth = 0
 
  For textIndex = 1 To CountString(gadgetText, " ")
    SetGadgetText(tempGadget, spacer$ + StringField(gadgetText, textIndex, " "))
    textIndexWidth + GadgetWidth(tempGadget, #PB_Gadget_RequiredSize) - 2
    If textIndexWidth => gadgetWt
      If textIndexWidth > gadgetWt
        spacer$ = " "
      Else
        spacer$ = ""
      EndIf
      If textIndex = CountString(gadgetText, " ")
        lines + 2
      Else
        lines + 1
      EndIf
      If alignFlag = #PB_Text_Right
        spacer$ = " "
        SetGadgetText(tempGadget, " " + StringField(gadgetText, textIndex, " "))
      Else
        SetGadgetText(tempGadget, StringField(gadgetText, textIndex, " "))
      EndIf
      If textIndexWidth > gadgetWt
        textIndexWidth = GadgetWidth(tempGadget, #PB_Gadget_RequiredSize) - 2
      Else
        textIndexWidth = 0
      EndIf
    Else
      spacer$ = " "
      If textIndex = CountString(gadgetText, " ")
        lines + 1
      EndIf
    EndIf
  Next textIndex

  Debug "gadgetHt = " + Str(gadgetHt) + ", lines = " + Str(lines)
  gadgetHt * lines
 
  ResizeGadget(gadgetNo, #PB_Ignore, #PB_Ignore, gadgetWt, gadgetHt)
  SetGadgetText(gadgetNo, Trim(gadgetText))
  FreeGadget(tempGadget)
 
EndProcedure
;==============================================================================================

;===========
; demo code
;===========
Enumeration
  #window1
  #label1
  #label2
  #label3
  #label4
  #textDemo
  #left
  #centre
  #right
  #fontName
  #fontSize
  #maxWidth
  #results
  #sample
  #set
  #font1
  #enterKey
EndEnumeration

Declare updateTextGadget()

wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
OpenWindow(#window1, #PB_Any, #PB_Any, 700, 600, "AutoSize TextGadget", wFlags)
AddKeyboardShortcut(#window1, #PB_Shortcut_Return, #enterKey)

TextGadget(#label1, 30, 20, 150, 20, "Select Font:")
TextGadget(#label2, 190, 20, 50, 20, "Font Size:")
TextGadget(#label3, 250, 20, 40, 20, "Width:")
TextGadget(#label4, 30, 70, 200, 20, "Sample Caption:")

ComboBoxGadget(#fontName, 30, 40, 150, 20)
AddGadgetItem(#fontName, -1, "Arial")
AddGadgetItem(#fontName, -1, "Book Antiqua")
AddGadgetItem(#fontName, -1, "Century Gothic")
AddGadgetItem(#fontName, -1, "Lucida Console")
AddGadgetItem(#fontName, -1, "Tahoma")
AddGadgetItem(#fontName, -1, "Trebuchet MS")
AddGadgetItem(#fontName, -1, "Times New Roman")
SetGadgetState(#fontName, 6)

ComboBoxGadget(#fontSize, 190, 40, 50, 20)
AddGadgetItem(#fontSize, -1, "10")
AddGadgetItem(#fontSize, -1, "20")
AddGadgetItem(#fontSize, -1, "30")
AddGadgetItem(#fontSize, -1, "40")
AddGadgetItem(#fontSize, -1, "50")
AddGadgetItem(#fontSize, -1, "60")
SetGadgetState(#fontSize, 4)

StringGadget(#maxWidth, 250, 40, 40, 23, "300")

OptionGadget(#left, 30, 120, 80, 25, " left align")
OptionGadget(#centre, 120, 120, 80, 25, " centre text")
OptionGadget(#right, 222, 120, 80, 25, " right align")
SetGadgetState(#left, 1)

StringGadget(#sample, 30, 90, 200, 23, "A rose by any other name would smell as sweet.")
ButtonGadget(#set, 240, 90, 50, 23, "SET")

ListViewGadget(#results, 30, 170, 260, 400)

LoadFont(#font1, GetGadgetText(#fontName), Val(GetGadgetText(#fontSize)))
; TextGadget(#textDemo, 350, 20, Val(GetGadgetText(#maxWidth)), 10, GetGadgetText(#sample))
TextGadget(#textDemo, 350, 20, Val(GetGadgetText(#maxWidth)), 10, "Test")
Debug "Required height = " + GadgetHeight(#textDemo, #PB_Gadget_RequiredSize)
SetGadgetFont(#textDemo, FontID(#font1))
Debug "Required height = " + GadgetHeight(#textDemo, #PB_Gadget_RequiredSize)
SetGadgetColor(#textDemo, #PB_Gadget_BackColor, RGB(120, 0, 0))
SetGadgetColor(#textDemo, #PB_Gadget_FrontColor, RGB(220, 220, 0))

updateTextGadget()

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      appQuit = 1
    Case #PB_Event_Menu
      updateTextGadget()
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #set, #fontName, #fontSize
          updateTextGadget()
        Case #left, #centre, #right
          Select EventGadget()
            Case #left
              alignFlag = 0
            Case #centre
              alignFlag = #PB_Text_Center
            Case #right
              alignFlag = #PB_Text_Right
          EndSelect
          updateTextGadget()
      EndSelect
  EndSelect
Until appQuit = 1

Procedure updateTextGadget()
  Shared alignFlag
  Static currentAlignment
 
  If currentAlignment <> alignFlag
    TextGadget(#textDemo, 350, 20, Val(GetGadgetText(#maxWidth)), 10, GetGadgetText(#sample), alignFlag)
    CompilerIf #PB_Compiler_OS = #PB_OS_Linux
      gtk_label_set_line_wrap_(GadgetID(#textDemo), #True)
      If alignFlag & #PB_Text_Center
        gtk_label_set_justify_(GadgetID(#textDemo), #GTK_JUSTIFY_CENTER)
      ElseIf alignFlag & #PB_Text_Right
        gtk_label_set_justify_(GadgetID(#textDemo), #GTK_JUSTIFY_RIGHT)
      EndIf
    CompilerEndIf
    SetGadgetColor(#textDemo, #PB_Gadget_BackColor, RGB(120, 0, 0))
    SetGadgetColor(#textDemo, #PB_Gadget_FrontColor, RGB(220, 220, 0))
    currentAlignment = alignFlag
  EndIf
 
  init = 1
  fontName.s = GetGadgetText(#fontName)
  fontSize = Val(GetGadgetText(#fontSize))
  LoadFont(#font1, fontName, fontSize)
  SetGadgetFont(#textDemo, FontID(#font1))
  SetGadgetText(#textDemo, Trim(GetGadgetText(#sample)))
  ResizeGadget(#textDemo, #PB_Ignore, #PB_Ignore, Val(GetGadgetText(#maxWidth)), #PB_Ignore)
  AddGadgetItem(#results, 0, "initialised size: " + Str(GadgetWidth(#textDemo)) + " x " + Str(GadgetHeight(#textDemo)))
  AddGadgetItem(#results, 1, "font: " + GetGadgetText(#fontName) + " @ " + GetGadgetText(#fontSize) + " points")
  AutoSize(#textDemo)
  AddGadgetItem(#results, 2, "--> AutoSized to: " + Str(GadgetWidth(#textDemo)) + " x " + Str(GadgetHeight(#textDemo)))
  AddGadgetItem(#results, 3, " ")
  SetGadgetState(#results, 2)
 
  ExamineDesktops()
 
  If GadgetWidth(#textDemo) > 320
    wWidth = GadgetWidth(#textDemo) + 380
    If wWidth > DesktopWidth(0)
      wWidth = DesktopWidth(0) - 100
    EndIf
  Else
    wWidth = 700
  EndIf

  Debug "GadgetHeight(#textDemo) = " + GadgetHeight(#textDemo)

  If GadgetHeight(#textDemo) > 520
    wHeight = GadgetHeight(#textDemo) + 40
    If wHeight > DesktopHeight(0)
      wHeight = DesktopHeight(0) - 100
    EndIf
    lHeight = wHeight - 200
  Else
    wHeight = 600
    lHeight = 400
  EndIf
 
  winX = (DesktopWidth(0) - wWidth) / 2
  winY = (DesktopHeight(0) - wHeight) / 2
  ResizeWindow(#window1, winX, winY, wWidth, wHeight)
  ResizeGadget(#results, #PB_Ignore, #PB_Ignore, #PB_Ignore, lHeight)
 
EndProcedure
Post Reply