Page 1 of 2
Colorizing chars in *Gadget
Posted: Mon Aug 17, 2015 3:00 am
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.
Re: Colorizing chars in TextGadget
Posted: Mon Aug 17, 2015 8:05 am
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
Re: Colorizing chars in TextGadget
Posted: Mon Aug 17, 2015 12:42 pm
by Fangbeast
Nice example except he asked for this to happen in a TextGadget and not an editorgadget:):)
Re: Colorizing chars in TextGadget
Posted: Mon Aug 17, 2015 12:57 pm
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
Re: Colorizing chars in TextGadget
Posted: Mon Aug 17, 2015 1:57 pm
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?
Re: Colorizing chars in TextGadget
Posted: Mon Aug 17, 2015 2:38 pm
by microdevweb
if it's for just display text, you can any use a ImageGadget and draw in this your text with most color.
Re: Colorizing chars in TextGadget
Posted: Mon Aug 17, 2015 3:33 pm
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.
Re: Colorizing chars in TextGadget
Posted: Mon Aug 17, 2015 4:30 pm
by RASHAD
You can change the style & the color at the same TextGadget()
But it is for windows only
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
Re: Colorizing chars in TextGadget
Posted: Thu Aug 20, 2015 10:46 am
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?
Re: Colorizing chars in *Gadget
Posted: Tue Aug 25, 2015 2:37 am
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
Re: Colorizing chars in *Gadget
Posted: Tue Aug 25, 2015 11:34 am
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.
Re: Colorizing chars in *Gadget
Posted: Tue Aug 25, 2015 1:41 pm
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!
Re: Colorizing chars in *Gadget
Posted: Tue Aug 25, 2015 3:03 pm
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?
Re: Colorizing chars in *Gadget
Posted: Tue Aug 25, 2015 8:44 pm
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.
Re: Colorizing chars in *Gadget
Posted: Tue Aug 25, 2015 8:59 pm
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