Page 1 of 1

SetTextGadgetAutosize(Gadget, State)

Posted: Mon Dec 26, 2005 11:17 pm
by Dr. Dri
Code updated For 5.20+

Those functions make a (PB native) TextGagdet resize automatically. It even works with multiline strings and custom fonts. In this example the window takes the size of the gadget.

Code: Select all

Procedure AutosizeCallback(WindowID, Message, wParam, lParam)
  Protected WndProc, Gadget, Size.Size
  Protected Text.s, Field.s, Count, i, w, h
  
  WndProc = GetProp_(WindowID, "AutosizeWndProc")
  
  If Message = #WM_CTLCOLORSTATIC And GetProp_(lParam, "AutosizeValue")
    Gadget = GetDlgCtrlID_(lParam)
    
    Text = GetGadgetText(Gadget)
    
    ;vire les retours charriot
    Text = ReplaceString(Text, #CRLF$, #LF$)
    Text = ReplaceString(Text, #CR$, #LF$)
    
    Count = CountString(Text, #LF$) + 1
    
    For i = 1 To Count
      Field = StringField(Text, i, #LF$)
      GetTextExtentPoint32_(wParam, Field, Len(Field), Size)
      
      If w < Size\cx
        w = Size\cx
        Size\cx = 0
      EndIf
    Next i
    
    h = Size\cy * Count
    ResizeGadget(Gadget, #PB_Default, #PB_Default, w, h)
  EndIf
  
  ProcedureReturn CallWindowProc_(WndProc, WindowID, Message, wParam, lParam)
EndProcedure

Procedure SetTextGadgetAutosize(TextGadget, Autosize)
  Protected GadgetID, WindowID, WndProc
  
  If IsGadget(TextGadget)
    GadgetID = GadgetID(TextGadget)
    SetProp_(GadgetID, "AutosizeValue",  Autosize)
    
    WindowID = GetParent_(GadgetID)
    
    If GetProp_(WindowID, "AutosizeWndProc") = #Null
      WndProc = SetWindowLong_( WindowID, #GWL_WNDPROC, @AutosizeCallback() )
      SetProp_(WindowID, "AutosizeWndProc", WndProc)
    EndIf
    
    InvalidateRect_(GadgetID, #Null, #True)
  EndIf
EndProcedure

If OpenWindow(0,0,0,270,160,"Autosize",#PB_Window_SystemMenu|#PB_Window_ScreenCentered) 
  label.s = "Auto size"
  label + #CRLF$ + "ceci est un exemple"
  label + #CRLF$ + "c'est simple"
  
  TextGadget(0, 0, 0, 250, 20, label)
  
  ;change de police pour le fun
  LoadFont(0, "Verdana", 28) ;<- change values ;)
  SetGadgetFont(0, FontID(0))
  
  ;autosize le gadget
  SetTextGadgetAutosize(0, #True)
  
  Repeat
    If WindowWidth(0) <> GadgetWidth(0) Or WindowHeight(0) <> GadgetHeight(0)
      ResizeWindow( 0,0,0,GadgetWidth(0), GadgetHeight(0) )
    EndIf
  Until WaitWindowEvent()=#PB_Event_CloseWindow
  
  DeleteObject_(hBrush)
EndIf
Dri ;)

Posted: Mon Dec 26, 2005 11:29 pm
by dobro
Another methode
no api , + short , and colour code :D


  ; PureBasic Visual Designer v3.92 build 1460

;- Window Constants
Declare Open_message(text$,taille)

Enumeration
     #message
     #Police
     #Button_0
     #Text_0
EndEnumeration

text$= "hello " + Chr (10)+ Chr (13)+ "comment vas tu ?" ; <----------------------------ICI ADD text
taille=35 ; <----------------------------ICI change
Open_message(text$,taille)



; ******************* boucle principale ****************
Repeat ; Start of the event loop
    Event = WaitWindowEvent () ; This line waits until an event is received from Windows
     WindowID = EventWindowID () ; The Window where the event is generated, can be used in the gadget procedures
     GadgetID = EventGadgetID () ; Is it a gadget event?
     EventType = EventType () ; The event type
     If Event = #PB_EventGadget
         If GadgetID = #Button_0
             End
         EndIf
     EndIf
Until Event = #PB_Event_CloseWindow ; End of the event loop
End
;**********************************



Procedure Open_message(text$,taille)
     FontID = LoadFont ( #Police , "Comic Sans MS" , taille, #PB_Font_Bold )
    long=( Len (text$)*taille)
     If OpenWindow ( #message , 10, 10,long, 50+long/8, #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_TitleBar , "message" )
         If CreateGadgetList ( WindowID ())
             ButtonGadget ( #Button_0 , 0, WindowHeight ()-30, 60, 30, "ok" )
             TextGadget ( #Text_0 , 30, (taille/8 ), 140+long, taille*200, text$)
             SetGadgetFont ( #Text_0 , FontID )
         EndIf
     EndIf
EndProcedure

Posted: Mon Dec 26, 2005 11:38 pm
by Bonne_den_kule
dobro:

Did you color your code yourself, or did you have a tool to do that?

Posted: Mon Dec 26, 2005 11:43 pm
by PB
The color looks nice, but what about all the extra bandwidth used?

Posted: Tue Dec 27, 2005 10:16 am
by gnozal
Bonne_den_kule wrote:dobro:
Did you color your code yourself, or did you have a tool to do that?
It's a tool from the french forum : http://purebasic.hmt-forum.com/viewtopic.php?t=3875