TextSplit Demo - EditorGadget Text Wrapping

Share your advanced PureBasic knowledge/code with the community.
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

TextSplit Demo - EditorGadget Text Wrapping

Post by Thunder93 »

With the various fonts styles & sizes, we need to adapt to, and not force upon our users fixed gadget widths and fixed font styles & sizes. Not to favour common practices, have larger strings exceed into the abyss beyond visual areas. A few third-party programs that I really like, they suffer from visual issues in different scenarios.

This procedure was designed more so to show strings to the fullest on the line. Have where the last character would exceed beyond the width of the gadget, be brought down to the next line and continuing on and automatically handling the gadget height increment. This is ideal for showing file paths where the length ... on Windows specifically, can be up-to #MAX_PATH = 260, for instances.

Word wrapping support that you would want to use normally when making word processing products, and fields for people to type out words. However; for showing paths, that first part of the line with a space, rest of the line with no spaces, and because the line exceeds beyond, everything after the space in near beginning of the line is now pushed onto the next line. This I hated, made what I wanted to show, really ugly and unprofessional.

This code does support in-line wrapping, even though it wasn't initially my intentions.


This code I provide, I'm not saying it's perfect. It's one of the better ones compared to what I've seen thus far floating around the different forums. For what I was interested in having it do. I've went through a great many, I have. ~ Oh! As of late, PB user RASHAD offered a good solution too.

I also want to note, don't forget to make your application DPI-Aware. PB user Rescator few years back made available some code for this task. DPI Aware Application - http://forums.purebasic.com/english/vie ... 12&t=40507

It's not everyday I post something under ' Tricks 'n' Tips ' section. I hope you folks find it interesting and useful. I would like to make this completely cross-platform capable, but I need the equivalent code that I'm using to handle the hidden state of EditorGadget() scroll bars. I only program 'some' on Windows, and so I'm at a loss. :P

Code: Select all

;  TextSplit Demo - EditorGadget Text Wrapping
;  Author:  Thunder93
;  Filename:  TextSplit_DEMO.pb
;  FileVersion:  1
;  Created:  Thursday March 26, 2015
;  Updated last: Friday March 28, 2015
;  Supported Platforms:  Windows  
;;;   - (Note: the two WinAPIs used, just to keep EditorGadget scroll bars hidden. It should be easily enhanced for other platforms.)


EnableExplicit

#Font_Georgia6 = 1
#Font_Georgia8 = 2
#Font_Georgia11 = 3
#Font_Georgia14 = 4
#Font_Arial = 5

#SplitIt = 1 ; Toggle Text Splitting Mode feature. Relies on the use of  ' \n~ ' in-line,
                   ; for line splitting.

Declare TextSplit(String.s, FontID = 0, Mode.b = 0, WindowID = 0, GadgetID = 0)
Define String.s, UsedFont.i, EditorFlags.i, Event.i

String = "MAJ:\DEVICE\HARDdiskVOLUME2\USERS\123456789``\APPdata\LOCAL\TEMP\PUREbasic_COMPILATION1.EXE\XXXXXXXXXEE\eweCDCXCSSSSS\EEEEE.EXE"

If OpenWindow(0, 0, 0, 470, 160, "TextGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  LoadFont(#Font_Georgia6, "Georgia", 6)
  LoadFont(#Font_Georgia8, "Georgia", 8)
  LoadFont(#Font_Georgia11, "Georgia", 11)
  LoadFont(#Font_Georgia14, "Georgia", 14)
  LoadFont(#Font_Arial, "Arial", 14)
  
  UsedFont = FontID(#Font_Georgia14)
  
  EditorFlags = #PB_Editor_ReadOnly | #ES_CENTER ; #ES_CENTER, #ES_LEFT, #ES_RIGHT - Text Alignments. 
  
  EditorGadget(0, 10, 10, 450, 0, EditorFlags)  
 
  SetGadgetColor(0, #PB_Gadget_BackColor, $E0DFE3)
  SetGadgetFont(0, UsedFont)
  
  If #SplitIt
    String = "First Line\n~\n~Next Lineup."
    TextSplit(String, UsedFont, #SplitIt)  ;;; Splitting Mode. If need to-do in-line splits.
  Else
    TextSplit(String, UsedFont)  ;;; Normal. No need for in-line splits.
  EndIf  
  
  
  Repeat
    Event = WaitWindowEvent()
    Select Event
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 0
            Select EventType()
              Case #NIN_SELECT
                ShowScrollBar_(GadgetID(0), #SB_BOTH, #False)
            EndSelect              
        EndSelect        
    EndSelect    
  Until Event = #PB_Event_CloseWindow
EndIf

Procedure TextSplit(String.s, FontID = 0, Mode.b = 0, WindowID = 0, GadgetID = 0)
  Protected.l i, Lines, TextWidth, TextHeight
  Protected.s Str, Char
  
  If StartDrawing(WindowOutput(WindowID))
    DrawingFont(FontID)
    TextHeight = TextHeight("W")+1
       
    For i = 1 To Len(String)
      Char = Mid(String,i,1)
      
      If Mode
        If Char = "\" And Mid(String, i+1,2) = "n~"
          AddGadgetItem(GadgetID, -1, Str)
          Str = "" : TextWidth = 0 : Lines+1 : i+2 : TextHeight + 1
          Continue
        EndIf
      EndIf      
      
      Str + Char : TextWidth + TextWidth(Mid(String,i,1))
      
      If TextWidth >= GadgetWidth(GadgetID)-14 : AddGadgetItem(GadgetID, -1, Str) : Str = "" : TextWidth = 0 : Lines+1 : TextHeight + 1 : EndIf                      
    Next
    
    If Str
      If Lines = 0
        TextHeight + 3
      EndIf
      AddGadgetItem(GadgetID, -1, Str) : Lines + 1
    EndIf
    
    StopDrawing()
    ResizeGadget(GadgetID, #PB_Ignore, #PB_Ignore, #PB_Ignore, TextHeight*Lines)
    ShowScrollBar_(GadgetID(0), #SB_BOTH, #False)
    
    ProcedureReturn TextHeight*Lines
  EndIf
EndProcedure

Updated #2 - [ 28-03-2015 ]: Tad alteration for better ' height ' appearance in different scenarios.

Updated #1: Only using just two WinAPI calls to handle the hidden EditorGadget() scroll bars.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
tryforsure
New User
New User
Posts: 6
Joined: Tue Nov 27, 2007 12:19 am
Location: Scotland

Re: TextSplit Demo - EditorGadget Text Wrapping

Post by tryforsure »

Thank you for posting this example code, you have cured part of my coding-headache. :)
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: TextSplit Demo - EditorGadget Text Wrapping

Post by Thunder93 »

Your welcome tryforsure. :D
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
Post Reply