Page 1 of 1

TaskBarProgress Module OOP Win7+

Posted: Sat Jun 29, 2013 10:13 pm
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

Re: TaskBarProgress Module OOP Win7+

Posted: Sat Jun 29, 2013 10:32 pm
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)

Re: TaskBarProgress Module OOP Win7+

Posted: Sat Jun 29, 2013 10:38 pm
by ts-soft
you are right, changed :wink:

Re: TaskBarProgress Module OOP Win7+

Posted: Sat Jun 29, 2013 11:17 pm
by rsts
Good one!

Very professional looking :D

Re: TaskBarProgress Module OOP Win7+

Posted: Sun Jun 30, 2013 12:42 pm
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"

Re: TaskBarProgress Module OOP Win7+

Posted: Sun Jun 30, 2013 12:48 pm
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)

Re: TaskBarProgress Module OOP Win7+

Posted: Sun Jun 30, 2013 12:49 pm
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

Re: TaskBarProgress Module OOP Win7+

Posted: Sun Jun 30, 2013 12:57 pm
by infratec
:mrgreen: :mrgreen: :mrgreen:

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

Bernd

Re: TaskBarProgress Module OOP Win7+

Posted: Sun Jun 30, 2013 1:01 pm
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.

Re: TaskBarProgress Module OOP Win7+

Posted: Sun Jun 30, 2013 4:55 pm
by ts-soft
Update:
History wrote:; Version 1.3
; added optional setting for range of progress
; (min = 0 To max) (max = min To 9223372036854775807)

Re: TaskBarProgress Module OOP Win7+

Posted: Fri Jul 25, 2014 8:27 pm
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?

Re: TaskBarProgress Module OOP Win7+

Posted: Sat Jun 27, 2015 8:28 pm
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?

Re: TaskBarProgress Module OOP Win7+

Posted: Fri Sep 04, 2015 3:34 pm
by ts-soft
Update for PB5.40