Word Wrap in a StringGadget?

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by merendo.

Code: Select all

If OpenWindow(0,0,0,640,480,#PB_Window_SystemMenu,"") And CreateGadgetList(WindowID(0))StringGadget(0,8,8,626,466,"Does anybody know how to set this stringgadget to auto-word wrap? When the text is too long, it is cut out and this is my problem. Do you know understand what I mean? I want the gadget to automatically add a line return when the text is too long and continued in the next line. Can anybody help me? Please! Thank you!",#ES_MULTILINE|#ES_AUTOVSCROLL|#WS_VSCROLL|#PB_String_ReadOnly)
Repeat
Until WaitWindowEvent()=#PB_Event_CloseWindow
EndIf
This code should tell everything :wink:

Can you help me? Thanx!

Cu @ll, merendo
--
Auf Regen folgt stets Sonnenschein!

Rain is always followed by sunshine!
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by ricardo.

This brilliant code from EL_CHONI i found in my snippets, it should be here in the forums somewhere, enjoy it!!

Code: Select all

Declare.l ActivateGadgetEx(Gadget)
Declare.l DisableGadgetEx(Gadget, State)
Declare.l FreeGadgetEx(Gadget)
Declare.l GadgetHeightEx(Gadget)
Declare.l GadgetIDEx(Gadget)
Declare.l GadgetToolTipEx(Gadget, Text$)
Declare.l GadgetWidthEx(Gadget)
Declare.l GadgetXEx(Gadget)
Declare.l GadgetYEx(Gadget)
Declare.s GetGadgetTextEx(Gadget)
Declare.l HideGadgetEx(Gadget, State)
Declare.l SetGadgetFontEx(FontID)
Declare.l SetGadgetTextEx(Gadget, Text$)
Declare.l WrapStringGadget(Gadget, x, y, width, height, text.s, flags)
Procedure Error(message$, fatal.b)
  FormatMessage_(#FORMAT_MESSAGE_FROM_SYSTEM, 0, GetLastError_(), 0, AllocateMemory(0, 256, 0), 256, 0)
  MessageRequester("Error", message$+Chr(10)+Chr(10)+PeekS(MemoryID()), 0)
  FreeMemory(0)
  If fatal
    End
  EndIf
EndProcedure
; To process events we don't want WaitEventWindow() to catch
Procedure WndProc(hWnd, uMsg, wParam, lParam)
  result = 0
  Select uMsg
    Case #WM_COMMAND
      EditClass.s = Space(4)
      If GetClassName_(hWnd, EditClass, 5)=4
        If GetWindowLong_(hWnd, #GWL_STYLE)=0 And EditClass="Edit"
          Select wParam>>16
            Case #EN_CHANGE
              result = #PB_ProcessPureBasicEvents
            Case #EN_KILLFOCUS
              result = #PB_ProcessPureBasicEvents
            Case #EN_SETFOCUS
              result = #PB_ProcessPureBasicEvents
          EndSelect
        Else
          result = #PB_ProcessPureBasicEvents
        EndIf
      Else
        result = #PB_ProcessPureBasicEvents
      EndIf
    Default
      result = #PB_ProcessPureBasicEvents
  EndSelect
  ProcedureReturn result
EndProcedure
If OpenWindow(0, 0, 0, 220, 120, #PB_Window_SystemMenu, "Wrap string gadget example")
  WindowID = WindowID()
  If CreateGadgetList(WindowID)
    SetGadgetFontEx(LoadFont(0, "Courier New", 10))
    If WrapStringGadget(0, 10, 10, 200, 100, "", #PB_String_Multiline|#ES_AUTOVSCROLL|#WS_VSCROLL)
      SetGadgetTextEx(0, "Wrap string gadget example: let's get wrapped until we scroll down and down!")
      Debug "GadgetIDEx: "+Str(GadgetIDEx(0))
      Debug "GadgetXEx: "+Str(GadgetXEx(0))
      Debug "GadgetYEx: "+Str(GadgetYEx(0))
      Debug "GadgetWidthEx: "+Str(GadgetWidthEx(0))
      Debug "GadgetHeightEx: "+Str(GadgetHeightEx(0))
      Debug "GadgetIDEx: "+GetGadgetTextEx(0)
      SetWindowCallback(@WndProc())
      If GadgetToolTipEx(0, "WrapEditGadget tooltip")=0
        Error("GadgetToolTipEx() failed", 0)
      EndIf
      Repeat
        EventID = WaitWindowEvent()
        Select EventID
          Case #PB_Event_Gadget
            Select EventGadgetID()
              Case 0
                Debug "WrapStringGadget event type processed:"+Str(EventType())
            EndSelect
        EndSelect
      Until EventID=#PB_Event_CloseWindow
      DeleteObject_(hFont)
    Else
      Error("WrapStringGadget() failed:", 1)
    EndIf
  Else
    Error("CreateGadgetList() failed:", 1)
  EndIf
Else
  Error("OpenWindow() failed:", 1)
EndIf
End
Global hFont
Procedure WrapStringGadget(Gadget, x, y, width, height, text$, flags)
  WrapStringGadget = CreateWindowEx_(#WS_EX_CLIENTEDGE, "EDIT", text$, flags|#WS_CHILDWINDOW|#WS_MAXIMIZEBOX|#WS_MINIMIZEBOX|#WS_VISIBLE, x, y, width, height, WindowID(), Gadget, GetModuleHandle_(0), 0)
  If WrapStringGadget
    If hFont
      SendMessage_(WrapStringGadget, #WM_SETFONT, hFont, 1)
    Else
      SendMessage_(WrapStringGadget, #WM_SETFONT, GetStockObject_(#DEFAULT_GUI_FONT), 1)
    EndIf
    UpdateWindow_(WrapStringGadget)
    ShowWindow_(WrapStringGadget, #SW_SHOWNORMAL)
  Else
    Error("CreateWindowEx_() failed:", 1)
  EndIf
  ProcedureReturn WrapStringGadget
EndProcedure
Procedure.s GetGadgetTextEx(Gadget)
  TextLength = SendMessage_(GadgetIDEx(Gadget), #WM_GETTEXTLENGTH, 0, 0)+1
  Buffer = AllocateMemory(0, TextLength, 0)
  SendMessage_(GadgetIDEx(Gadget), #WM_GETTEXT, TextLength, Buffer)
  result.s = PeekS(Buffer)
  FreeMemory(0)
  ProcedureReturn result
EndProcedure
Global GadgetIDEx
Procedure EnumChildProc(hWnd, lParam)
  ID = GetWindowLong_(hWnd, #GWL_ID)
  If ID=lParam
    GadgetIDEx = hWnd
    result = #FALSE
  Else
    result = #TRUE
  EndIf
  ProcedureReturn result
EndProcedure
Procedure GadgetIDEx(Gadget)
  EnumChildWindows_(WindowID(), @EnumChildProc(), Gadget)
  ProcedureReturn GadgetIDEx
EndProcedure
Procedure DisableGadgetEx(Gadget, State)
  result = EnableWindow_(GadgetIDEx(Gadget), State)
  ProcedureReturn result
EndProcedure
Procedure FreeGadgetEx(Gadget)
  result = DestroyWindow_(GadgetIDEx(Gadget))
  ProcedureReturn result
EndProcedure
Procedure HideGadgetEx(Gadget, State) 
  If State
    result = ShowWindow_(GadgetIDEx(Gadget), #SW_HIDE)
  Else
    result = ShowWindow_(GadgetIDEx(Gadget), #SW_SHOW)
  EndIf
  ProcedureReturn result
EndProcedure
Procedure GadgetWidthEx(Gadget) 
  GetWindowRect_(GadgetIDEx(Gadget), rc.RECT)
  result = rc\right-rc\left
  ProcedureReturn result
EndProcedure
Procedure GadgetHeightEx(Gadget) 
  GetWindowRect_(GadgetIDEx(Gadget), rc.RECT)
  result = rc\bottom-rc\top
  ProcedureReturn result
EndProcedure
Procedure GadgetXEx(Gadget) 
  GetWindowRect_(GadgetIDEx(Gadget), rc.RECT)
  result = rc\left
  ProcedureReturn result
EndProcedure
Procedure GadgetYEx(Gadget) 
  GetWindowRect_(GadgetIDEx(Gadget), rc.RECT)
  result = rc\top
  ProcedureReturn result
EndProcedure
Procedure ActivateGadgetEx(Gadget) 
  result = SetFocus_(GadgetIDEx(Gadget))
  ProcedureReturn result
EndProcedure
Procedure SetGadgetTextEx(Gadget, Text$)
  result = SendMessage_(GadgetIDEx(Gadget), #WM_SETTEXT, 0, Text$)
  ProcedureReturn result
EndProcedure
Procedure SetGadgetFontEx(FontID)
  If FontID=#PB_Font_Default
    hFont = GetStockObject_(#DEFAULT_GUI_FONT)
  Else
    hFont = FontID
  EndIf
  ProcedureReturn hFont
EndProcedure
Procedure GadgetToolTipEx(Gadget, Text$)
#TTF_DI_SETITEM = $8000
  hToolTip = CreateWindowEx_(0, "Tooltips_class32", 0, 0, 0, 0, 0, 0, WindowID(), 0, GetModuleHandle_(0), 0)
  If hToolTip
    ti.TOOLINFO
    ti\cbSize = SizeOf(TOOLINFO)
    ti\uFlags = #TTF_SUBCLASS|#TTF_IDISHWND
    ti\hWnd = GadgetIDEx(Gadget)
    ti\uId = GadgetIDEx(Gadget)
    ti\hinst = 0
    ti\lpszText = @Text$
    result = SendMessage_(hToolTip, #TTM_ADDTOOL, 0, ti)
  Else
    result = 0
  EndIf
  ProcedureReturn result
EndProcedure
Best Regards

Ricardo

Dont cry for me Argentina...
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

Courtesy of Fangbeast in another post:

Code: Select all

If OpenWindow(0,0,0,640,480,#PB_Window_SystemMenu,"") And CreateGadgetList(WindowID(0))
StringGadget(0,8,8,626,466,"Does anybody know how to set this stringgadget to auto-word wrap? When the text is too long, it is cut out and this is my problem. Do you know understand what I mean? I want the gadget to automatically add a line return when the text is too long and continued in the next line. Can anybody help me? Please! Thank you!",#ES_MULTILINE | #ESB_DISABLE_LEFT | #ESB_DISABLE_RIGHT | #ES_AUTOVSCROLL | #WS_VSCROLL)
Repeat
Until WaitWindowEvent()=#PB_Event_CloseWindow
EndIf
Post Reply