Page 1 of 2

Formatting a multi-line Tooltip to align like columns ?

Posted: 28 Mar 2019, 13:26
by DRocks
Hello guys,


I have read a few times about the Format function in AHK but I can't practically make it to use and here's a case where I need to understand it better.
I'm trying to display a clean multi-line tooltip that shows some calculations.
The alignement of a invisible column is really important to make it consistent and look good.
Now all I have to solve the problem is guessing how many spaces it needs to display aligned.

Would someone please provide me with an example of how to properly format this kind of multi-line tooltip please ?
I'd really appreciate it.

Here's my example code that should run and display what I currently got:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance, Force ; Prevent double execution of this script
#Persistent

; Faking what are normally dynamic variables (changing with different calculations)
OverRowNumber := 1
, Qt_1 := 2
, Mt_1 := 100
, RawTs_1 := 4.35
, RawTq_1 := 8.68

; Prepare the values to display in the tooltip 
, Ligne_Quantite	 := Qt_%OverRowNumber%
, Ligne_PrixUnitaire := Mt_%OverRowNumber%
, Ligne_TPSUnitaire  := RawTs_%OverRowNumber%
, Ligne_TVQUnitaire  := RawTq_%OverRowNumber%
, Ligne_TotalHorsTx  := (Ligne_PrixUnitaire - (Ligne_TPSUnitaire + Ligne_TVQUnitaire)) * Ligne_Quantite
, Ligne_TotalTPS     := Ligne_TPSUnitaire	* Ligne_Quantite
, Ligne_TotalTVQ 	 := Ligne_TVQUnitaire	* Ligne_Quantite
, Ligne_TotalAvecTx  := Ligne_PrixUnitaire	* Ligne_Quantite


; Tooltip to format :
, TT_Tip := "Calcul ligne #"OverRowNumber " :"
		 . "`n`n   Sous-total hors taxes  :      " Ligne_TotalHorsTx "  $                    ( $ " Ligne_PrixUnitaire " * Quantité  = $ " Ligne_TotalAvecTx " - Taxes )"
		 . "`n    + TPS                           :      " Ligne_TotalTPS "  $                    ( $ " Ligne_TPSUnitaire " * Quantité )"
		 . "`n    + TVQ                          :      " Ligne_TotalTVQ "  $                    ( $ " Ligne_TVQUnitaire " * Quantité )"
		 . "`n   ________________________________________________________________________________________________________________________"
		 . "`n   Sous-total avec taxes  :      " Ligne_TotalAvecTx "  $"



ToolTip, % TT_Tip, % A_ScreenWidth//3, % A_ScreenHeight // 2.5									; Show the ToolTip
SetTimer, ToolTipOff, -4000						; Set the Timer to automatically turn off tooltip after number of ms ;*[Untitled1]
Return

ToolTipOff:
Tooltip ; Off
ExitApp
Thanks in advance

Re: Formatting a multi-line Tooltip to align like columns ?

Posted: 28 Mar 2019, 13:35
by wolf_II
Look for Name: String Things - Common String & Array Functions

here: https://www.autohotkey.com/boards/viewtopic.php?f=6&t=53

I hope that helps.

Re: Formatting a multi-line Tooltip to align like columns ?

Posted: 28 Mar 2019, 13:45
by DRocks
wolf_II wrote:
28 Mar 2019, 13:35
Look for Name: String Things - Common String & Array Functions

here: https://www.autohotkey.com/boards/viewtopic.php?f=6&t=53

I hope that helps.
Thank you wolf I will look at it :)

Re: Formatting a multi-line Tooltip to align like columns ?

Posted: 28 Mar 2019, 14:41
by teadrinker
wolf_II wrote: I hope that helps.
I'm not sure that it works as one may expect:

Image

I suppose, it could work correctly only for monospace fonts.

Re: Formatting a multi-line Tooltip to align like columns ?

Posted: 28 Mar 2019, 14:51
by DRocks
I had no success either with this. I tried 3 appropriate functions but no luck.


I am trying to make a listview at the moment rather than a tooltip

Re: Formatting a multi-line Tooltip to align like columns ?

Posted: 28 Mar 2019, 14:54
by wolf_II
@teadrinker
I would use my function LV_Box, but String things is written by professionals, so I recommend them first.
Also, LV_Box is a gui, rather than a tooltip. Lexikos says a tooltip may contain any Gui, so it is possible.
I have not tried to do that yet. You would not by chance have a gui in a tooltip example, would you?

Re: Formatting a multi-line Tooltip to align like columns ?

Posted: 28 Mar 2019, 14:57
by DRocks
wolf_II wrote:
28 Mar 2019, 14:54
@teadrinker
I would use my function LV_Box, but String things is written by professionals, so I recommend them first.
Also, LV_Box is a gui, rather then a tooltip. Lexikos reports, a tooltip may contain any Gui, so it is possible.
I have not tried to do that yet. You would not by chance have a gui in a tooltip example, would you?
hmm lv box might be interesting

Re: Formatting a multi-line Tooltip to align like columns ?

Posted: 28 Mar 2019, 14:59
by wolf_II
@DRocks: LV_box is a pain to set up first time, lemme know if I can help

Edit: I cleaned it so it is easier to use. And I renamed it on the way to LV_Show()
And I put it in my signature

Re: Formatting a multi-line Tooltip to align like columns ?  Topic is solved

Posted: 28 Mar 2019, 15:04
by teadrinker
As option you could using something like this to change a tooltip font:

Code: Select all

ToolTipFont(Options := "", Name := "", hwnd := "") {
    static hfont := 0
    if (hwnd = "")
        hfont := Options="Default" ? 0 : _TTG("Font", Options, Name), _TTHook()
    else
        DllCall("SendMessage", "ptr", hwnd, "uint", 0x30, "ptr", hfont, "ptr", 0)
}
 
ToolTipColor(Background := "", Text := "", hwnd := "") {
    static bc := "", tc := ""
    if (hwnd = "") {
        if (Background != "")
            bc := Background="Default" ? "" : _TTG("Color", Background)
        if (Text != "")
            tc := Text="Default" ? "" : _TTG("Color", Text)
        _TTHook()
    }
    else {
        VarSetCapacity(empty, 2, 0)
        DllCall("UxTheme.dll\SetWindowTheme", "ptr", hwnd, "ptr", 0
            , "ptr", (bc != "" || tc != "") ? &empty : 0)
        if (bc != "")
            DllCall("SendMessage", "ptr", hwnd, "uint", 1043, "ptr", bc, "ptr", 0)
        if (tc != "")
            DllCall("SendMessage", "ptr", hwnd, "uint", 1044, "ptr", tc, "ptr", 0)
    }
}
 
_TTHook() {
    static hook := 0
    if !hook
        hook := DllCall("SetWindowsHookExW", "int", 4
            , "ptr", RegisterCallback("_TTWndProc"), "ptr", 0
            , "uint", DllCall("GetCurrentThreadId"), "ptr")
}
 
_TTWndProc(nCode, _wp, _lp) {
    Critical 999
   ;lParam  := NumGet(_lp+0*A_PtrSize)
   ;wParam  := NumGet(_lp+1*A_PtrSize)
    uMsg    := NumGet(_lp+2*A_PtrSize, "uint")
    hwnd    := NumGet(_lp+3*A_PtrSize)
    if (nCode >= 0 && (uMsg = 1081 || uMsg = 1036)) {
        _hack_ = ahk_id %hwnd%
        WinGetClass wclass, %_hack_%
        if (wclass = "tooltips_class32") {
            ToolTipColor(,, hwnd)
            ToolTipFont(,, hwnd)
        }
    }
    return DllCall("CallNextHookEx", "ptr", 0, "int", nCode, "ptr", _wp, "ptr", _lp, "ptr")
}
 
_TTG(Cmd, Arg1, Arg2 := "") {
    static htext := 0, hgui := 0, WM_CTLCOLORSTATIC := 0x138, WM_GETFONT := 0x31, WS_CHILD := 0x40000000
    
    if !htext {
        Gui _TTG: +hwndhgui +%WS_CHILD%
        Gui _TTG: Add, Text, hwndhtext
    }
    Gui _TTG: %Cmd%, %Arg1%, %Arg2%
    if (Cmd = "Font") {
        GuiControl _TTG: Font, %htext%
        SendMessage, WM_GETFONT, 0, 0,, ahk_id %htext%
        return ErrorLevel
    }
    if (Cmd = "Color") {
        hdc := DllCall("GetDC", "ptr", htext, "ptr")
        SendMessage, WM_CTLCOLORSTATIC, hdc, htext,, ahk_id %hgui%
        clr := DllCall("GetBkColor", "ptr", hdc, "uint")
        DllCall("ReleaseDC", "ptr", htext, "ptr", hdc)
        return clr
    }
}

Code: Select all

#Persistent
data=
(
"Date","Pupil","Grade"
----,-----,-----
"25 May","Bloggs, Fred","C"
"25 May","Doe, Jane","B"
"15 July","Bloggs, Fred","A"
"15 April","Muniz, Alvin ""Hank""","A"
)
ToolTipFont("s10 q5", "Droid Sans Mono")
ToolTip % st_columnize(data, "csv", 2)
Image

Re: Formatting a multi-line Tooltip to align like columns ?

Posted: 28 Mar 2019, 15:07
by ahk7

Code: Select all

Data=
(join`n
Date,Pupil,Grade
25 May,Bloggs,Fred,C
25 May,Doe,Jane,B
15 July,Bloggs,Fred,A
15 April,Muniz Alvin Hank,A
)

Loop, parse, data, `n, `r
	{
	Loop, parse, A_LoopField, CSV
		{
		 show .= Format("{:-30}",A_LoopField) A_Tab
		}
	show .= "`n"
	}
ToolTip % Show
Sleep 2000
ExitApp

Re: Formatting a multi-line Tooltip to align like columns ?

Posted: 28 Mar 2019, 15:11
by DRocks
teadrinker wrote:
28 Mar 2019, 15:04
Nice you have it correctly on your setup.
Sad thing is I copied all your example with functions and st_ library and it doesnt order proeprly on my win10 machine. Wierd!


Image

Re: Formatting a multi-line Tooltip to align like columns ?

Posted: 28 Mar 2019, 15:12
by DRocks
ahk7 wrote:
28 Mar 2019, 15:07

Code: Select all

Data=
(join`n
Date,Pupil,Grade
25 May,Bloggs,Fred,C
25 May,Doe,Jane,B
15 July,Bloggs,Fred,A
15 April,Muniz Alvin Hank,A
)

Loop, parse, data, `n, `r
	{
	Loop, parse, A_LoopField, CSV
		{
		 show .= Format("{:-30}",A_LoopField) A_Tab
		}
	show .= "`n"
	}
ToolTip % Show
Sleep 2000
ExitApp
WTF this works let me analyze it. ty!

Man, I tried using format with individual values and I had no success at all and you just shown me a example of it inside a loop. very neat

Re: Formatting a multi-line Tooltip to align like columns ?

Posted: 28 Mar 2019, 15:14
by teadrinker
@ahk7:

Code: Select all

Data=
(join`n
Date,Pupil,Grade
25 May,Bloggs,Fred,C
25 May,Here is a very long text: 6666666666666666666666666666666666,Jane,B
15 July,Bloggs,Fred,A
15 April,Muniz Alvin Hank,Here is a very long text: 6666666666666666666666666666666666
)

Loop, parse, data, `n, `r
   {
   Loop, parse, A_LoopField, CSV
      {
       show .= Format("{:-30}",A_LoopField) A_Tab
      }
   show .= "`n"
   }
ToolTip % Show
Sleep 2000
ExitApp

Re: Formatting a multi-line Tooltip to align like columns ?

Posted: 28 Mar 2019, 15:19
by teadrinker
DRocks wrote: it doesnt order proeprly on my win10 machine
Perhaps you just have not Droid Sans Mono font installed in your machine. Try using a different monospaced font, such as Consolas.

Re: Formatting a multi-line Tooltip to align like columns ?

Posted: 28 Mar 2019, 15:20
by DRocks
teadrinker wrote:
28 Mar 2019, 15:14
Ah yeah, right it messes up now with this example

Re: Formatting a multi-line Tooltip to align like columns ?

Posted: 28 Mar 2019, 15:22
by DRocks
teadrinker wrote:
28 Mar 2019, 15:19
DRocks wrote: t doesnt order proeprly on my win10 machine
Perhaps you just have not Droid Sans Mono font installed in your machine. Try using a different monospaced font, such as Consolas.
Confirmed. With Consolas it works like on your screenshot :D
Thank you guys, this is great !

Re: Formatting a multi-line Tooltip to align like columns ?

Posted: 28 Mar 2019, 15:26
by jeeswg
Re. fonts, there's also Courier New.

Re: Formatting a multi-line Tooltip to align like columns ?

Posted: 28 Mar 2019, 15:37
by ahk7
You can/must tinker with the value (-30 in the example), depending on the data it might not be the best solution of course, but by that time you probably want something else to present data like a listview or perhaps html (using a table)

Re: Formatting a multi-line Tooltip to align like columns ?

Posted: 28 Mar 2019, 15:46
by DRocks
Thank you. I am testing all options. Might end-up with a Gui ListView acting like a tooltip

Re: Formatting a multi-line Tooltip to align like columns ?

Posted: 28 Mar 2019, 15:58
by wolf_II
If you get one going inside a tooltip, I would love to see it :D