Page 1 of 1

StatusBar - Resize

Posted: Sun Sep 07, 2008 5:01 pm
by Progi1984
It could be cool if we can resize a field of a statusbar.

Posted: Sun Sep 07, 2008 11:14 pm
by akj
Here's a way to do it in Windows:

Code: Select all

; Demo of Changing Status Bar Field Widths  AKJ  07-Sep-08
EnableExplicit
RandomSeed(ElapsedMilliseconds())

Enumeration
  #winMain
  #staBar
  #butAllChange
EndEnumeration

; Create GUI
Define winw, winh, flags, hwin, hsta, field, stah, butw, buth
winw = 500: winh = 200
butw = 160: buth = 30
flags = #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_TitleBar|#PB_Window_ScreenCentered
hwin = OpenWindow(#winMain, 0, 0, winw, winh, " Adjust Status Bar Field Widths", flags)
CreateGadgetList(hwin)
hsta = CreateStatusBar(#staBar, hwin) ; Returns status bar handle (not documented!)
For field = 0 To 2 ; Three status bar fields
  AddStatusBarField(winw/3)
  StatusBarText(#staBar, field, "Field "+Str(field))
Next field
stah = StatusBarHeight(#staBar)
ButtonGadget(#butAllChange, (winw-butw)/2, (winh-buth-stah)/2, butw, buth, "Change Status Bar Fields")

; Event loop  AKJ  02-Sep-08
Define done.b=#False, ev
Dim fieldX(2)
Repeat
  ev = WaitWindowEvent()
  Select ev
  Case #PB_Event_Gadget
    Select EventGadget()
    Case #butAllChange
      ; Randomly choose status bar field widths (as x co-ordinates of field ends)
      Repeat
        fieldX(0) = Random(winw-200)+100
        fieldX(1) = fieldX(0) + Random(winw-200)+100
      Until fieldX(1)<=winw-100
      fieldX(2) = -1 ; Fill remaining width
      ; See  http://msdn.microsoft.com/en-us/library/bb760757(VS.85).aspx
      SendMessage_(hsta,  #SB_SETPARTS,  3,  fieldX())
      StatusBarText(#staBar, 0, "Width = "+Str(fieldX(0)))
      StatusBarText(#staBar, 1, "Width = "+Str(fieldX(1)-fieldX(0)))
      StatusBarText(#staBar, 2, "Width = "+Str(winw-fieldX(1)))
    EndSelect
  Case #PB_Event_CloseWindow
    done = #True
  EndSelect
Until done
End

Posted: Sun Sep 07, 2008 11:25 pm
by Progi1984
But the problem is the portability : How can you do this under Linux & MacOS ?

Posted: Sun Sep 07, 2008 11:29 pm
by ts-soft

Posted: Tue Sep 09, 2008 2:02 am
by Demivec
Progi1984 wrote:But the problem is the portability : How can you do this under Linux & MacOS ?
I modified akj's code so that it doesn't use API. It seems to work fine, though I can't confirm this for Linux or MacOS.

The code simply recreates the StatusBar with the desired widths. The only method that is questionable is that it does not manually free the old status bar before creating the new one. I was thinking that if the same ID# was used it should free it before recreating it.

Code: Select all

; Demo of Changing Status Bar Field Widths  AKJ  07-Sep-08
;modified by Demivec to use non - API
EnableExplicit
RandomSeed(ElapsedMilliseconds())

Enumeration
  #winMain
  #staBar
  #butAllChange
EndEnumeration

Structure statusBar
  width.l
  text.s
EndStructure


Procedure setupStatusBar(hwin.l,fields.statusBar(1),numFields.l)
  Protected field.l
  
  If CreateStatusBar(#staBar, hwin)
    For field = 0 To numFields - 1
      AddStatusBarField(fields(field)\width)
      StatusBarText(#staBar, field, fields(field)\text)
    Next field
  EndIf 
EndProcedure

; Create GUI
Define winw, winh, flags, hwin, hsta, field, stah, butw, buth
winw = 500: winh = 200
butw = 160: buth = 30
flags = #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_TitleBar|#PB_Window_ScreenCentered
hwin = OpenWindow(#winMain, 0, 0, winw, winh, " Adjust Status Bar Field Widths", flags)

Dim fieldX.statusBar(2)
For field = 0 To 2
  fieldX(field)\width = winw/3
  fieldX(field)\text = "Field "+Str(field)
Next 
setupStatusBar(hwin,fieldX(),3)
stah = StatusBarHeight(#staBar)

CreateGadgetList(hwin)
ButtonGadget(#butAllChange, (winw-butw) / 2, (winh - buth - stah) / 2, butw, buth, "Change Status Bar Fields")

; Event loop  AKJ  02-Sep-08
Define done.b = #False, ev
Repeat
  ev = WaitWindowEvent()
  Select ev
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #butAllChange
          ; Randomly choose status bar field widths
          Repeat
            fieldX(0)\width = Random(winw - 200) + 100
            fieldX(1)\width = Random(winw - 200) + 100
            fieldX(2)\width = winw - fieldX(0)\width - fieldX(1)\width ; Fill remaining width
          Until fieldX(2)\width>=100
          fieldX(0)\text = "Width = " + Str(fieldX(0)\width)
          fieldX(1)\text = "Width = " + Str(fieldX(1)\width)
          fieldX(2)\text = "Width = " + Str(fieldX(2)\width)
          SetupStatusBar(hwin,fieldX(),3)
      EndSelect
    Case #PB_Event_CloseWindow
      done = #True
  EndSelect
Until done
End
@Edit: updated code with corrections suggested by Akj.

Posted: Tue Sep 09, 2008 8:06 am
by akj
@Demivec
Your version is not reporting the status bar field widths correctly.
Here is revised code for the event loop to overcome that:

Code: Select all

; Event loop  AKJ  02-Sep-08
Define done.b = #False, ev
Repeat
  ev = WaitWindowEvent()
  Select ev
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #butAllChange
          ; Randomly choose status bar field widths
          Repeat
            fieldX(0)\width = Random(winw - 200) + 100
            fieldX(1)\width = Random(winw - 200) + 100
            fieldX(2)\width = winw - fieldX(0)\width - fieldX(1)\width ; Fill remaining width
          Until fieldX(2)\width>=100
          fieldX(0)\text = "Width = " + Str(fieldX(0)\width)
          fieldX(1)\text = "Width = " + Str(fieldX(1)\width)
          fieldX(2)\text = "Width = " + Str(fieldX(2)\width)
          SetupStatusBar(hwin,fieldX(),3)
      EndSelect
    Case #PB_Event_CloseWindow
      done = #True
  EndSelect
Until done
End

Posted: Tue Sep 09, 2008 8:36 am
by Demivec
akj wrote:@Demivec
Your version is not reporting the status bar field widths correctly.
Here is revised code for the event loop to overcome that:
Thanks, I had apparently overlooked some details in my hasty rewrite. I've edited the code in my post to reflect your revision.

Posted: Sun Sep 14, 2008 7:39 pm
by whertz
Is there a way to resize the statusbar when resizing the window? I tried both ways using the #WM_SIZE umsg on a window callback, resizing with my regular gadgets but the API method just makes the statusbar disappear.