Page 1 of 1

GetStatusBarText

Posted: Mon Mar 10, 2025 3:20 am
by BarryG
Would love to see this added, please. So we can get the text from any given StatusBar field. Thanks for considering.

Re: GetStatusBarText

Posted: Mon Mar 10, 2025 4:12 am
by Ventural
Retrieves text from Statusbar fields.

Code: Select all

Procedure.s GetStatusbarText(StatusBarNum.q, FieldNum.l)
  Define buffer.s = Space(256)
  Define result.l = SendMessage_(StatusBarID(StatusBarNum), #SB_GETTEXT, FieldNum, @buffer)
  Define length.l = result & $FFFF
  ProcedureReturn Left(buffer, length)
  ;To remove extra spacing, change to: Trim(Left(buffer, length), Chr(9))
EndProcedure

If OpenWindow(0, 0, 0, 440, 50, "StatusBar", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget)
  
  If CreateStatusBar(0, WindowID(0))
    AddStatusBarField(90)
    AddStatusBarField(100)
    AddStatusBarField(#PB_Ignore)
    AddStatusBarField(100)
  EndIf

  StatusBarText(0, 0, "Area normal")
  StatusBarText(0, 1, "Area borderless", #PB_StatusBar_BorderLess)
  StatusBarText(0, 2, "Area right", #PB_StatusBar_Right)
  StatusBarText(0, 3, "Area centered", #PB_StatusBar_Center)
  
  ; Debug output for all fields
  Debug "Field 0: " + GetStatusbarText(0, 0)  ; "Area normal"
  Debug "Field 1: " + GetStatusbarText(0, 1)  ; "Area borderless"
  Debug "Field 2: " + GetStatusbarText(0, 2)  ; "Area right"
  Debug "Field 3: " + GetStatusbarText(0, 3)  ; "Area centered"
    
  Repeat
  Until WaitWindowEvent() = #PB_Event_CloseWindow
  
EndIf

Re: GetStatusBarText

Posted: Mon Mar 10, 2025 9:04 am
by BarryG
Thank you, Ventural. A nice method until it becomes a native command. :)

Re: GetStatusBarText

Posted: Mon Mar 10, 2025 9:29 am
by mk-soft
Why should you read the Statusbar text. Makes no sense to me as there is no user input here and it is only written.

Re: GetStatusBarText

Posted: Mon Mar 10, 2025 12:37 pm
by jacdelad
It does, especially from foreign windows. I needed this on my previous workplace.

Re: GetStatusBarText

Posted: Mon Mar 10, 2025 1:32 pm
by BarryG
mk-soft wrote: Mon Mar 10, 2025 9:29 amWhy should you read the Statusbar text
Because my app puts dynamic text there from multiple threads that changes every few seconds; so there's no point in me keeping track of the constant changes when a quick and simple "GetStatusBarText" command could just be used on demand instead. It's less resources and app maintenance for me if I had it (no need to set a Global variable from 20+ threads, plus any new threads that I might add later).

The other reason is because for every "Set" command, there really should be a corresponding "Get" command. Gadgets have them, so why not the StatusBar (rhetorical).
mk-soft wrote: Mon Mar 10, 2025 9:29 amMakes no sense to me as there is no user input here and it is only written
By that logic, SetWindowTitle() shouldn't need a corresponding GetWindowTitle() either, since it also has no user input. ;)

Re: GetStatusBarText

Posted: Mon Mar 10, 2025 1:55 pm
by skywalk
I love the statusbar for user updating and interaction. It is valuable real estate, so why not fully utilize?
That said, I prefer owner drawn statusbar as a containergadget of stringgadgets. Then, you control full behavior; color, font, resizing, mouse clock events, etc. And it becomes cross platform without custom API calls.

Re: GetStatusBarText

Posted: Mon Mar 10, 2025 2:31 pm
by Quin
+1, I've needed this in my app before, which is quite similar to Barry's in that the status bar is updated every few seconds from one of multiple threads, even sometimes more frequently if the user moves around a lot, and being able to programatically get the text would be super handy.

Re: GetStatusBarText

Posted: Mon Mar 10, 2025 11:24 pm
by BarryG
You can programmatically use a StatusBar as interactive with user input anyway. Example: in my app, clicking one of its fields toggles what it shows.

Re: GetStatusBarText

Posted: Thu Mar 13, 2025 4:43 pm
by infratec
StatusBar stuff is GUI stuff.
So it should be only modified in the main EventLoop.

PostEvents() should be used to do this.

Re: GetStatusBarText

Posted: Thu Mar 13, 2025 5:48 pm
by Quin
infratec wrote: Thu Mar 13, 2025 4:43 pm StatusBar stuff is GUI stuff.
So it should be only modified in the main EventLoop.

PostEvents() should be used to do this.
Sure, I agree.
Now, would you mind telling me what this has to do with adding a GetStatusBarText function? Regardless of how the info is set, we should be able to get it. To use Barry's earlier example once again, window titles can be gotten programmatically ;)

Re: GetStatusBarText

Posted: Thu Mar 13, 2025 9:14 pm
by infratec
Why I need a function to get a text when I wrote it?
If it is done at one place, you always know what is in the field.
Simply store it in a variable before you apply it like:
StatusBarField1$, StatusBarField2$, ...

But sure you can request every functionality you want.

And I never use GetWindowTitle(), it is faster to have it in a variable.

Re: GetStatusBarText

Posted: Thu Mar 13, 2025 9:37 pm
by jacdelad
What if you use a foreign module which changes/sets the titjebar if a certain window and you need to read it? In my opinion, if possible (!), every set function should have a get function.

Re: GetStatusBarText

Posted: Thu Mar 13, 2025 10:00 pm
by BarryG
infratec wrote: Thu Mar 13, 2025 9:14 pmWhy I need a function to get a text when I wrote it?
By that logic, PureBasic should remove GetWindowTitle(), GetGadgetText(), GetGadgetItemText(), GetGadgetData()... after all, we wrote them all.

Re: GetStatusBarText

Posted: Thu Mar 13, 2025 11:21 pm
by skywalk
It depends if your user entered the data or you displayed read only status messages.