StatusBar - Resize

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Progi1984
Addict
Addict
Posts: 806
Joined: Fri Feb 25, 2005 1:01 am
Location: France > Rennes
Contact:

StatusBar - Resize

Post by Progi1984 »

It could be cool if we can resize a field of a statusbar.
akj
Enthusiast
Enthusiast
Posts: 668
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Post 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
Anthony Jordan
User avatar
Progi1984
Addict
Addict
Posts: 806
Joined: Fri Feb 25, 2005 1:01 am
Location: France > Rennes
Contact:

Post by Progi1984 »

But the problem is the portability : How can you do this under Linux & MacOS ?
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Post 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.
Last edited by Demivec on Tue Sep 09, 2008 8:33 am, edited 1 time in total.
akj
Enthusiast
Enthusiast
Posts: 668
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Post 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
Anthony Jordan
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Post 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.
whertz
Enthusiast
Enthusiast
Posts: 124
Joined: Sat Jun 25, 2005 2:16 pm
Location: United Kingdom

Post 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.
Post Reply