Colorizing chars in *Gadget

Just starting out? Need help? Post your questions and find answers here.
destiny
User
User
Posts: 29
Joined: Wed Jul 15, 2015 12:58 pm
Location: CCCP

Colorizing chars in *Gadget

Post by destiny »

Is it possible to colorize text in TextGadget without creating few TextGadgets with different text colors?
For the moment i have only one idea, bad idea - create each gadget for char group with identical color. This leads to calculations of char(s) width, where to place next gadget with next textcolor and other things that making this difficult enough.
Any ideas?

Why i need this?
For example, to make not so important chars/words little lighter than default black. If i have a table with numbers, some of them are round, some of them with decimals, and i need to visually identify this moments, ~ like this:
  • 123456.000
    000121.100
    321123.000
    000055.555
UPD 2015/08/21: for that moment i'm thinking about using CanvasGadget (?) because i need click control over it, TextGadget can't do anything with that.
Last edited by destiny on Fri Aug 21, 2015 3:13 am, edited 1 time in total.
User avatar
microdevweb
Enthusiast
Enthusiast
Posts: 179
Joined: Fri Jun 13, 2014 9:38 am
Location: Belgique

Re: Colorizing chars in TextGadget

Post by microdevweb »

Yes with windows only
This code it's not from me but it's from RsBasic
Look ther post
http://www.purebasic.fr/english/viewtop ... 27&t=62741

Code: Select all

;Autor: Eckhard.S

EnableExplicit

Define EventID

Procedure SetColor(id,s,e,color)
  Protected cf.charformat2\cbSize = SizeOf(charformat2)
  Protected sel.CHARRANGE
  
  SendMessage_(GadgetID(id),#EM_EXGETSEL,0,sel)
  cf\dwMask       = #CFM_COLOR
  cf\crTextColor  = color             
  SendMessage_(GadgetID(id),#EM_SETSEL,s,e)   
  SendMessage_(GadgetID(id),#EM_SETCHARFORMAT,#SCF_SELECTION,@cf)
  SendMessage_(GadgetID(id),#EM_SETSEL,sel\cpMin,sel\cpMax)
EndProcedure

If OpenWindow(0,0,0,500,400,"Window",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  EditorGadget(1,10,10,WindowWidth(0)-20,WindowHeight(0)-20,0)
  SetGadgetText(1,"PureBasicPureBasicPureBasicPureBasicPureBasicPureBasic")
  
  SetColor(1,12,17,$0000FF)
  SetColor(1,22,28,$408000)
  SetColor(1,0,12,$00FF00) 
  
  Repeat
    EventID=WaitWindowEvent()
    If EventID = #PB_Event_CloseWindow
      End
    EndIf
  ForEver
EndIf
Use Pb 5.73 lst and Windows 10

my mother-language isn't english, in advance excuse my mistakes.
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Colorizing chars in TextGadget

Post by Fangbeast »

Nice example except he asked for this to happen in a TextGadget and not an editorgadget:):)
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
RSBasic
Moderator
Moderator
Posts: 1228
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: Colorizing chars in TextGadget

Post by RSBasic »

You can use "TextGadget" based on EditorGadget:

Code: Select all

EnableExplicit

Define EventID

Procedure SetColor(id,s,e,color)
  Protected cf.charformat2\cbSize = SizeOf(charformat2)
  Protected sel.CHARRANGE
  
  SendMessage_(GadgetID(id), #EM_EXGETSEL, 0, sel)
  cf\dwMask       = #CFM_COLOR
  cf\crTextColor  = color             
  SendMessage_(GadgetID(id), #EM_SETSEL, s, e)   
  SendMessage_(GadgetID(id), #EM_SETCHARFORMAT, #SCF_SELECTION, @cf)
  SendMessage_(GadgetID(id), #EM_SETSEL, sel\cpMin, sel\cpMax)
EndProcedure

Procedure TextGadgetEx(Gadget, x, y, Width, Height, Text$, Flags = 0)
  EditorGadget(Gadget, x, y, Width, Height, Flags)
  DisableGadget(Gadget, 1)
  SetWindowTheme_(GadgetID(Gadget), @"", @"")
  SetWindowLongPtr_(GadgetID(Gadget), #GWL_EXSTYLE, 0)
  SendMessage_(GadgetID(Gadget), #EM_SHOWSCROLLBAR, #SB_HORZ, #False)
  SendMessage_(GadgetID(Gadget), #EM_SHOWSCROLLBAR, #SB_VERT, #False)
  SetWindowPos_(GadgetID(Gadget), 0, 0, 0, 0, 0, #SWP_NOMOVE | #SWP_NOSIZE | #SWP_FRAMECHANGED)
  SetGadgetColor(Gadget, #PB_Gadget_FrontColor, RGB(0, 0, 0))
  SetGadgetText(Gadget, Text$)
  
EndProcedure

If OpenWindow(0, 0, 0, 500, 400, "Window", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  TextGadgetEx(1, 10, 10, WindowWidth(0)-20, 50, "PureBasicPureBasicPureBasicPureBasicPureBasicPureBasic")
  
  SetColor(1, 12, 17, $0000FF)
  SetColor(1, 22, 28, $408000)
  SetColor(1, 0, 12, $00FF00)
  
  Repeat
    EventID = WaitWindowEvent()
    If EventID = #PB_Event_CloseWindow
      End
    EndIf
  ForEver
EndIf
Image
Image
destiny
User
User
Posts: 29
Joined: Wed Jul 15, 2015 12:58 pm
Location: CCCP

Re: Colorizing chars in TextGadget

Post by destiny »

Thanxs for this answers. I've found EditorGadget coloring code in CodeArchive yesterday when searching for coloring chars in TextGadget yesterday. Ray Siegl's (RSBasic) code extends what i've seen yesterday, but...

there is no chance to make TextGadget colored, only by "converting" EditorGadget to semiTextGadget by disabling it and changing background color + disabling scrollers?
In future i think i can use it in linux, so WinAPI calls will make impossible to use such code. Because of this i need to think about procedures that make composed TextGadgets?
User avatar
microdevweb
Enthusiast
Enthusiast
Posts: 179
Joined: Fri Jun 13, 2014 9:38 am
Location: Belgique

Re: Colorizing chars in TextGadget

Post by microdevweb »

if it's for just display text, you can any use a ImageGadget and draw in this your text with most color.
Use Pb 5.73 lst and Windows 10

my mother-language isn't english, in advance excuse my mistakes.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Colorizing chars in TextGadget

Post by wilbert »

destiny wrote:In future i think i can use it in linux, so WinAPI calls will make impossible to use such code.
API is not that bad if you can create the same behavior on different platforms.
Colorizing chars in a TextGadget is possible on OSX with some OSX specific code.
Maybe there's a Linux solution as well.
Windows (x64)
Raspberry Pi OS (Arm64)
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Colorizing chars in TextGadget

Post by RASHAD »

You can change the style & the color at the same TextGadget()
But it is for windows only :wink:

Code: Select all

LoadFont(0,"Arial",10)
LoadFont(1,"Arial",14,#PB_Font_Bold)
LoadFont(2,"Arial",10,#PB_Font_Underline)

Procedure WindowCB(hWnd,uMsg,wParam,lParam)
  Result = #PB_ProcessPureBasicEvents
  Select uMsg
   Case #WM_CTLCOLORSTATIC
      Select lParam
        Case GadgetID(1)
           SetBkMode_(wParam,#TRANSPARENT)
           SelectObject_(wParam,FontID(0))
           SetTextColor_(wParam,$0000FF)           
           TextOut_(wParam,0,4,@"Text",4)
           GetTextExtentPoint32_(wParam,@"Text",4,p.SIZE)
           x = p\cx + 4
           SelectObject_(wParam,FontID(1))
           SetTextColor_(wParam,$FF0000)
           TextOut_(wParam,x,0,@"Bold",4)
           GetTextExtentPoint32_(wParam,@"Bold",4,p.SIZE)
           x = x + p\cx + 4
           SelectObject_(wParam,FontID(2))
           SetTextColor_(wParam,$00FF00)
           TextOut_(wParam,x,4,@"UnderLine",9)
           ProcedureReturn GetStockObject_(#HOLLOW_BRUSH)
        EndSelect
  EndSelect
  ProcedureReturn Result
EndProcedure

If OpenWindow(0,0,0,250,250,"Window",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  TextGadget(1,10,10,200,40,"",0) 
  InvalidateRect_(WindowID(0),0,1)
  SetWindowCallback(@WindowCB())
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf

Egypt my love
destiny
User
User
Posts: 29
Joined: Wed Jul 15, 2015 12:58 pm
Location: CCCP

Re: Colorizing chars in TextGadget

Post by destiny »

Weird... i am very new to PB, so may not understand too many things...

Code: Select all

;EnableExplicit

Enumeration
	#Str_1
	#Info_1
	#Str_2
	#Info_2
EndEnumeration

; WinAPI size detection:
Procedure.u GetTextPixData_1(id.l, size.a)
;	Protected hDC.l, hFont.l, result.u, lpSize.???
	hDC = GetDC_(GadgetID(id))
	hFont = SendMessage_(GadgetID(id), #WM_GETFONT, 0, 0)
	If hFont And hDC
		SelectObject_(hDC, hFont)
	EndIf 
	GetTextExtentPoint32_(hDC, GetGadgetText(id), Len(GetGadgetText(id)), lpSize.SIZE)
	If size = 0
		result = lpSize\cx
	ElseIf size = 1
		result = lpSize\cy
	EndIf
	ProcedureReturn result
EndProcedure

; PB size detection:
Procedure.u GetTextPixData_2(id.l, size.a)
	Protected str$
	If StartDrawing(WindowOutput(0))
		str$ = GetGadgetText(id)
		If size = 0
			result = TextWidth(str$)
		ElseIf size = 1
			result = TextHeight(str$)
		EndIf
		StopDrawing()
	EndIf
	ProcedureReturn result
EndProcedure

If OpenWindow(0, 0, 0, 400, 400, "Text pixel size detection",  #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_TitleBar )
	TextGadget(#Str_1, 8, 8, 192, 16, "0")
	str$ = "Info 1: w = " + Str(GetTextPixData_1(#Str_1, 0)) + ", h = " + Str(GetTextPixData_1(#Str_1, 1)) + " px"
	TextGadget(#Info_1, 208, 8, 192, 16, str$)

	TextGadget(#Str_2, 8, 30, 192, 20, "0")
	str$ = "Info 2: w = " + Str(GetTextPixData_2(#Str_2, 0)) + ", h = " + Str(GetTextPixData_2(#Str_2, 1)) + " px"
	TextGadget(#Info_2, 208, 30, 192, 16, str$)
	Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
First - when using WinAPI (Procedure #1) - size of "0" in my system and my windows default font (i'm not using aero/glass etc, and as close as possible to classic theme) takes 6x13 pixels. When using PB TextWidth/TextHeight - 8x16px.
So when i try to compose textgadgets i have big gaps between them and i need manually subtract some from width and height from what calculated. But it's crazy - if there will be another font or size.
What i'm doind wrong?

And another question - when i use EnableExplicit i can't compile because lpSize is undeclared and i don't know how to declare it because it's WinAPI parameter (no experience in WinAPI, but maybe it will be needed sometime somewhere)

UPD: i've found info about ProGUI extension/library, there are TextControlEx() procedure (compiled in dll...) and author make possible to use escape codes in text to colorise (make bold/italic/underline/strike through/link effects) it. Anyone know something about it? So there is a chance to make only one TextGadget, not 2-3 to "hide" not important part of text? And as i understand - text is at the transparent bg. Maybe he uses ImageGadget?..
And harder question - if i wish to do the same in ListViewGadget? At the end i need some sort of list with few columns, where line/cell can be colored in any color, partially colored (for example, 20% of the cell in one color, 40% in another, ant last 40% in another one), with or without mini-icon/pict, with possibility to click on line/column and do some work with this. It's impossible?
destiny
User
User
Posts: 29
Joined: Wed Jul 15, 2015 12:58 pm
Location: CCCP

Re: Colorizing chars in *Gadget

Post by destiny »

EnableExplicit/declaration fixed. lpSize.??? = lpSize.SIZE - this structure declared in PB by default.
Detecting size of the chars fixed. Problem in default fonts. When using TextGadget - PB uses one font. When using StartDrawing - another. So we need to get default font for window and set it as drawing font. Undocumented "feature".

Code: Select all

	Protected hDC.l, hFont.l, result.u, lpSize.SIZE

Code: Select all

; PB size detection:
Procedure.u GetTextPixData_2(id.l, size.a)
  Protected str$
  If StartDrawing(WindowOutput(0))
    str$ = GetGadgetText(id)
    DrawingFont(GetGadgetFont(id))
    If size = 0
      result = TextWidth(str$)
    ElseIf size = 1
      result = TextHeight(str$)
    EndIf
    StopDrawing()
  EndIf
  ProcedureReturn result
EndProcedure
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Colorizing chars in *Gadget

Post by IdeasVacuum »

Not a 100% sure but I think ProGUI has been abandoned by it's developer.

If you are going to do cross-platform then it is much easier to use a CanvasGadget as a one-for-one TextGadget replacement.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 544
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Re: Colorizing chars in *Gadget

Post by bbanelli »

IdeasVacuum wrote:Not a 100% sure but I think ProGUI has been abandoned by it's developer.
I have purchased it recently, got most recent version (newer than one advertised on official page); Gold has full source code, so it works like a charm!
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
User avatar
blueb
Addict
Addict
Posts: 1111
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: Colorizing chars in *Gadget

Post by blueb »

bbanelli wrote:I have purchased it recently, got most recent version (newer than one advertised on official page); Gold has full source code, so it works like a charm!
Good to know bbanelli.

What version number did you purchase?
- It was too lonely at the top.

System : PB 6.21(x64) and Win 11 Pro (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 544
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Re: Colorizing chars in *Gadget

Post by bbanelli »

blueb wrote:
bbanelli wrote:I have purchased it recently, got most recent version (newer than one advertised on official page); Gold has full source code, so it works like a charm!
Good to know bbanelli.

What version number did you purchase?

Code: Select all

#AppName = "ProGUI"
#Version = 1.40
Works like a charm from W2k to W10; AFAIR, though, I had to make some (very) small changes to source code in order to make ti work with PB 5.3, but those are really few minutes.
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
destiny
User
User
Posts: 29
Joined: Wed Jul 15, 2015 12:58 pm
Location: CCCP

Re: Colorizing chars in *Gadget

Post by destiny »

bbanelli, did you saw how did escape-sequences colorising in TextGadgetEx? And which gadget is used there?
Very interested how it is done, especially because it has only one #id for this extenвed text gadget to compose/colorise/multiline
Last edited by destiny on Wed Jan 06, 2016 11:13 am, edited 1 time in total.
Post Reply