TaskBarProgress Module OOP Win7+

Share your advanced PureBasic knowledge/code with the community.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

TaskBarProgress Module OOP Win7+

Post by ts-soft »

A small Module with some OOP :wink:

Code: Select all

;======================================================================
; Module:          TaskBarProgress.pbi
;
; Author:          Thomas (ts-soft) Schulz
; Date:            Sep 04, 2015
; Version:         1.4
; Target Compiler: PureBasic 5.2+
; Target OS:       Windows 7 +
; License:         Free, unrestricted, no warranty whatsoever
;                  Use at your own risk
;======================================================================

; History:
; Version 1.4
; API-Update for PB5.40

; Version 1.3
; added optional setting for range of progress
; (min = 0 To max) (max = min To 9223372036854775807)

DeclareModule TaskBarProgress
  #TBPF_NOPROGRESS    = $0
  #TBPF_INDETERMINATE = $1
  #TBPF_NORMAL        = $2
  #TBPF_ERROR         = $4
  #TBPF_PAUSED        = $8

  Interface TaskBarProgress
    GetState.i()
    GetValue.i()
    SetState(state.i)
    SetValue(value.i)
    Destroy()
  EndInterface
 
  Declare TBP_New(hWnd.i, min.q = 0, max.q = 100)
EndDeclareModule

Module TaskBarProgress
  EnableExplicit
 
  Interface ITaskbarList3 Extends ITaskbarList2
    SetProgressValue.i(hWnd.i, ullCompleted.q, ullTotal.q)
    SetProgressState.i(hWnd.i, tbpFlags.l)
    RegisterTab.i(hWndTab.i, hWndMDI.i)
    UnregisterTab.i(hWndTab.i)
    SetTabOrder.i(hWndTab.i, hWndInsertBefore.i)
    SetTabActive.i(hWndTab.i, hWndMDI.i, tbatFlags.l)
    ThumbBarAddButtons.i(hWnd.i, cButtons.l, *pButton)
    ThumbBarUpdateButtons.i(hWnd.i, cButtons.l, *pButton)
    ThumbBarSetImageList.i(hWnd.i, himl.i)
    SetOverlayIcon.i(hWnd.i, hIcon.i, pszDescription.s)
    SetThumbnailTooltip.i(hWnd.i, pszTip.s)
    SetThumbnailClip.i(hWnd.i, *prcClip)
  EndInterface
 
  Structure TBPClass
    *vTable
    hWnd.i
    ptl.ITaskbarList3
    state.i
    value.i
    min.q
    max.q
  EndStructure
 
  Procedure TBP_New(hWnd.i, min.q = 0, max.q = 100)
    Protected *obj.TBPClass
    Protected ptl.ITaskbarList3
   
    If OSVersion() < #PB_OS_Windows_7
      Debug "You're OS is to old ;-)"
      ProcedureReturn #False
    EndIf
   
    If min < 0 : min = 0 : EndIf
    If max < min : max = min : EndIf
   
    *obj = AllocateMemory(SizeOf(TBPClass))
    If *obj
      *obj\vTable = ?vTable_TBPClass
      *obj\hWnd = hWnd
     
      CoCreateInstance_(?CLSID_TaskBarList, 0, 1, ?IID_ITaskBarList3, @ptl)
      If ptl
        ptl\HrInit()
        ptl\SetProgressValue(hWnd, min, max)
        ptl\SetProgressState(hWnd, #TBPF_NOPROGRESS)
       
        *obj\ptl = ptl
        *obj\state = #TBPF_NOPROGRESS
        *obj\value = 0
        *obj\min = min
        *obj\max = max
      EndIf
     
    EndIf
   
    ProcedureReturn *obj
  EndProcedure
 
  ; Methodes
  Procedure GetState(*this.TBPClass)
    If *this
      ProcedureReturn *this\state
    EndIf
  EndProcedure

  Procedure GetValue(*this.TBPClass)
    If *this
      ProcedureReturn *this\value
    EndIf
  EndProcedure
   
  Procedure SetState(*this.TBPClass, state)
    If *this
      With *this
        If \ptl
          Select state
            Case #TBPF_NOPROGRESS, #TBPF_INDETERMINATE, #TBPF_NORMAL, #TBPF_ERROR, #TBPF_PAUSED
              \ptl\SetProgressState(\hWnd, state)
              \state = state
          EndSelect       
        EndIf
      EndWith
    EndIf
  EndProcedure

  Procedure SetValue(*this.TBPClass, value)
    If *this
      With *this
        If \ptl
          If value < \min : value = \min : EndIf
          If value > \max : value = \max : EndIf
          \ptl\SetProgressValue(\hWnd, value, \max)
          \value = value
        EndIf
      EndWith
    EndIf
  EndProcedure
 
  Procedure Destroy(*this.TBPClass)
    If *this
      If *this\ptl
        *this\ptl\Release()
      EndIf
      FreeMemory(*this)
    EndIf
  EndProcedure
 
  DataSection
    CLSID_TaskBarList:
    Data.l $56FDF344
    Data.w $FD6D, $11D0
    Data.b $95, $8A, $00, $60, $97, $C9, $A0, $90
   
    IID_ITaskBarList3:
    Data.l $ea1afb91
    Data.w $9e28,$4b86
    Data.b $90,$E9,$9e,$9f,$8a,$5e,$ef,$af
   
    vTable_TBPClass:
      Data.i @GetState()
      Data.i @GetValue()
      Data.i @SetState()
      Data.i @SetValue()
      Data.i @Destroy()
  EndDataSection
EndModule

; example
CompilerIf #PB_Compiler_IsMainFile
  EnableExplicit
  UseModule TaskBarProgress
  Define TBP.TaskBarProgress
  Define progress
  Define hIcon = ExtractIcon_(GetModuleHandle_(#Null$), #PB_Compiler_Home + "PureBasic.exe", 0)
 
  OpenWindow(0, #PB_Ignore, #PB_Ignore, 265, 30, "Example", #PB_Window_SystemMenu)
  SendMessage_(WindowID(0), #WM_SETICON, 0, hIcon)
  ButtonGadget(0, 5, 5, 80, 20, "start")
  ButtonGadget(1, 90, 5, 80, 20, "stop")
  ButtonGadget(2, 175, 5, 80, 20, "pause")
 

  TBP = TBP_New(WindowID(0))
  If Not TBP
    MessageRequester("TaskBarProgress", "You're OS is to old ;-)")
    End
  EndIf
 
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        TBP\Destroy()
        DestroyIcon_(hIcon)
        Break
       
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 0 ; start
            AddWindowTimer(0, 1, 100)
          Case 1 ; stop
            RemoveWindowTimer(0, 1)
            TBP\SetState(#TBPF_NOPROGRESS)
            progress = 0
          Case 2 ; pause
            Select TBP\GetState()
              Case #TBPF_INDETERMINATE
                AddWindowTimer(0, 1, 100)
                TBP\SetState(#TBPF_NORMAL)
                SetGadgetText(2, "pause")
              Default
                RemoveWindowTimer(0, 1)
                TBP\SetState(#TBPF_INDETERMINATE)
                SetGadgetText(2, "continue")
            EndSelect
        EndSelect
       
      Case #PB_Event_Timer
        progress + 1
        If progress > 100 : progress = 0 : EndIf
        TBP\SetState(#TBPF_NORMAL)
        TBP\SetValue(progress)
    EndSelect
  ForEver
 
CompilerEndIf
Have fun
Last edited by ts-soft on Fri Sep 04, 2015 3:33 pm, edited 4 times in total.
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
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: TaskBarProgress Module OOP Win7+

Post by Josh »

Nice, but why du you use TBP in the interface. Looks not very smart:

Code: Select all

  Interface TaskBarProgress
    TBP_GetState.i()
    TBP_GetValue.i()
    TBP_SetState(state.i)
    TBP_SetValue(value.i)
    TBP_Destroy()
  EndInterface

  ....

  TBP\TBP_SetState(#TBPF_NORMAL)
  TBP\TBP_SetValue(progress)
I think this looks better and do the same:

Code: Select all

  Interface TaskBarProgress
    GetState.i()
    GetValue.i()
    SetState(state.i)
    SetValue(value.i)
    Destroy()
  EndInterface

  ....

  TBP\SetState(#TBPF_NORMAL)
  TBP\SetValue(progress)
sorry for my bad english
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: TaskBarProgress Module OOP Win7+

Post by ts-soft »

you are right, changed :wink:
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
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: TaskBarProgress Module OOP Win7+

Post by rsts »

Good one!

Very professional looking :D
jassing
Addict
Addict
Posts: 1768
Joined: Wed Feb 17, 2010 12:00 am

Re: TaskBarProgress Module OOP Win7+

Post by jassing »

Should have some minimum OS checks to avoid errors.
ie: Win2003 "Read Error at Address 0" on line 201: TBP\SetState(#TBPF_NORMAL)

--edit: On my win7 laptop, running as admin or normal user, 32 or 64bit 5.20b4: It did nothing other than show the window with buttons; clicking them didn't yield anything; so there must be some other requirement other than just "windows 7"
Last edited by jassing on Sun Jun 30, 2013 1:50 pm, edited 1 time in total.
infratec
Always Here
Always Here
Posts: 6866
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: TaskBarProgress Module OOP Win7+

Post by infratec »

Since I don't read the subject good enough :cry:
I got an IMA.

To avoid this for other users:

Code: Select all

TBP = TBP_New(WindowID(0))
  If Not TBP
    MessageRequester("Error", "Not possible To create TaskBarProgress maybe wrong OS (< Win7)")
    End
  EndIf
Bernd (who has to wait till monday to test)
Last edited by infratec on Sun Jun 30, 2013 12:56 pm, edited 1 time in total.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: TaskBarProgress Module OOP Win7+

Post by ts-soft »

There is a OS check! You have to check the result ob TBP_New(hWnd) :wink:
It should 0 on none supported OS.

Only the example doesn't check this. The example is not bulled proof and should only
illustrate the functions.

Greetings - Thomas
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
infratec
Always Here
Always Here
Posts: 6866
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: TaskBarProgress Module OOP Win7+

Post by infratec »

:mrgreen: :mrgreen: :mrgreen:

so why you don't use it in your demo code :?:

Bernd
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: TaskBarProgress Module OOP Win7+

Post by ts-soft »

infratec wrote:so why you don't use it in your demo code :?:
to lazy :mrgreen:

//edit
added Debug-Message to Module
changed the example.
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
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: TaskBarProgress Module OOP Win7+

Post by ts-soft »

Update:
History wrote:; Version 1.3
; added optional setting for range of progress
; (min = 0 To max) (max = min To 9223372036854775807)
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
Suirad
User
User
Posts: 42
Joined: Sun Sep 20, 2009 7:37 pm
Location: Melbourne, Florida, USA

Re: TaskBarProgress Module OOP Win7+

Post by Suirad »

ts-soft wrote:Update:
History wrote:; Version 1.3
; added optional setting for range of progress
; (min = 0 To max) (max = min To 9223372036854775807)
Great example. Modules are the perfect fit to fill really the only gap people could ask for when using oop in purebasic, as far as having private methods and vars. Hmm, any chance of swapping out the data sections vtable utilizing the new runtime objects?
juror
Enthusiast
Enthusiast
Posts: 228
Joined: Mon Jul 09, 2007 4:47 pm
Location: Courthouse

Re: TaskBarProgress Module OOP Win7+

Post by juror »

In reading the descriptions for the TBPFLAG parameter it indicates a left to right progression e.g. "The progress indicator grows in size from left to right" however on my system this and any other of the examples I've tried (luis, dark dragon, rescator) cycle from the bottom to the top of the taskbar icon, thus the effect is not as nice as if it were left to right.

Try as I might, I've been unable to determine a parameter which governs this behavior. Is it perhaps some system-wide setting for my system? Or am I missing something obvious?
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: TaskBarProgress Module OOP Win7+

Post by ts-soft »

Update for PB5.40
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
Post Reply