From 19c55f370ec3f51528e6d73818d351d4bacbde20 Mon Sep 17 00:00:00 2001 From: Markus Ewald Date: Mon, 16 Jul 2007 19:00:02 +0000 Subject: [PATCH] Fully implemented the tracking bar control which will now act as a specialized progress bar for tracking progressions git-svn-id: file:///srv/devel/repo-conversion/nuwi@7 d2e56fa2-650e-0410-a79f-9358c0239efd --- .../ContainerListView.Designer.cs | 17 +- Source/ContainerListView/ContainerListView.cs | 3 +- .../ListViewEmbeddedControl.cs | 1 + .../ListViewEmbeddedControlCollection.cs | 1 + Source/EmbeddedControlCollection.cs | 7 + Source/TrackingBar/TrackingBar.Designer.cs | 36 +++- Source/TrackingBar/TrackingBar.cs | 156 ++++++++++++++++++ 7 files changed, 202 insertions(+), 19 deletions(-) diff --git a/Source/ContainerListView/ContainerListView.Designer.cs b/Source/ContainerListView/ContainerListView.Designer.cs index 6ca26ce..0b360f2 100644 --- a/Source/ContainerListView/ContainerListView.Designer.cs +++ b/Source/ContainerListView/ContainerListView.Designer.cs @@ -21,15 +21,14 @@ License along with this library namespace Nuclex.Windows.Forms { partial class ContainerListView { - /// - /// Required designer variable. - /// + + /// Required designer variable. private System.ComponentModel.IContainer components = null; - /// - /// Clean up any resources being used. - /// - /// true if managed resources should be disposed; otherwise, false. + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + /// protected override void Dispose(bool disposing) { if(disposing && (components != null)) { components.Dispose(); @@ -40,8 +39,8 @@ namespace Nuclex.Windows.Forms { #region Component Designer generated code /// - /// Required method for Designer support - do not modify - /// the contents of this method with the code editor. + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. /// private void InitializeComponent() { components = new System.ComponentModel.Container(); diff --git a/Source/ContainerListView/ContainerListView.cs b/Source/ContainerListView/ContainerListView.cs index a89c3f8..8032439 100644 --- a/Source/ContainerListView/ContainerListView.cs +++ b/Source/ContainerListView/ContainerListView.cs @@ -17,6 +17,7 @@ You should have received a copy of the IBM Common Public License along with this library */ #endregion + using System; using System.Collections.Generic; using System.ComponentModel; @@ -32,7 +33,7 @@ namespace Nuclex.Windows.Forms { /// /// There basically were two possible design choices: Provide a specialized /// ListViewSubItem that carries a Control instead of a string or manage the - /// embedded controls in seperation of the ListView's items. The first option + /// embedded controls seperate of the ListView's items. The first option /// would require a complete rewrite of the ListViewItem class and its related /// support classes, all of which are surprisingly large and complex. Thus, /// the less clean but more doable latter option has been chosen. diff --git a/Source/ContainerListView/ListViewEmbeddedControl.cs b/Source/ContainerListView/ListViewEmbeddedControl.cs index 9e2175e..072d52e 100644 --- a/Source/ContainerListView/ListViewEmbeddedControl.cs +++ b/Source/ContainerListView/ListViewEmbeddedControl.cs @@ -17,6 +17,7 @@ You should have received a copy of the IBM Common Public License along with this library */ #endregion + using System; using System.Collections.Generic; using System.Windows.Forms; diff --git a/Source/ContainerListView/ListViewEmbeddedControlCollection.cs b/Source/ContainerListView/ListViewEmbeddedControlCollection.cs index 062a9a4..65ab13a 100644 --- a/Source/ContainerListView/ListViewEmbeddedControlCollection.cs +++ b/Source/ContainerListView/ListViewEmbeddedControlCollection.cs @@ -17,6 +17,7 @@ You should have received a copy of the IBM Common Public License along with this library */ #endregion + using System; using System.Collections.Generic; using System.Collections.ObjectModel; diff --git a/Source/EmbeddedControlCollection.cs b/Source/EmbeddedControlCollection.cs index 771f7c5..527d1e6 100644 --- a/Source/EmbeddedControlCollection.cs +++ b/Source/EmbeddedControlCollection.cs @@ -17,6 +17,7 @@ You should have received a copy of the IBM Common Public License along with this library */ #endregion + using System; using System.Collections.Generic; using System.Windows.Forms; @@ -79,6 +80,12 @@ namespace Nuclex.Windows.Forms { OnControlRemoved(value); } + // These three methods don't need to be override since their current implementations + // in the .NET Framework 2.0 all call the Remove() method on each individual control. + // The ControlAdded/ControlRemoved events weren't in the original class and there + // are other designs you could use to guarantee that removed controls get disposed + // properly, so this should be rechecked when the code is compiled on another + // version of the .NET framework /* /// Adds an array of control objects to the collection /// diff --git a/Source/TrackingBar/TrackingBar.Designer.cs b/Source/TrackingBar/TrackingBar.Designer.cs index 8ad9ccf..a2a146a 100644 --- a/Source/TrackingBar/TrackingBar.Designer.cs +++ b/Source/TrackingBar/TrackingBar.Designer.cs @@ -1,15 +1,33 @@ +#region CPL License +/* +Nuclex Framework +Copyright (C) 2002-2007 Nuclex Development Labs + +This library is free software; you can redistribute it and/or +modify it under the terms of the IBM Common Public License as +published by the IBM Corporation; either version 1.0 of the +License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +IBM Common Public License for more details. + +You should have received a copy of the IBM Common Public +License along with this library +*/ +#endregion + namespace Nuclex.Windows.Forms { partial class TrackingBar { - /// - /// Required designer variable. - /// + /// Required designer variable. private System.ComponentModel.IContainer components = null; - /// - /// Clean up any resources being used. - /// - /// true if managed resources should be disposed; otherwise, false. + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + /// protected override void Dispose(bool disposing) { if(disposing && (components != null)) { components.Dispose(); @@ -20,8 +38,8 @@ namespace Nuclex.Windows.Forms { #region Component Designer generated code /// - /// Required method for Designer support - do not modify - /// the contents of this method with the code editor. + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. /// private void InitializeComponent() { components = new System.ComponentModel.Container(); diff --git a/Source/TrackingBar/TrackingBar.cs b/Source/TrackingBar/TrackingBar.cs index 5ce7a98..21e4b33 100644 --- a/Source/TrackingBar/TrackingBar.cs +++ b/Source/TrackingBar/TrackingBar.cs @@ -1,3 +1,23 @@ +#region CPL License +/* +Nuclex Framework +Copyright (C) 2002-2007 Nuclex Development Labs + +This library is free software; you can redistribute it and/or +modify it under the terms of the IBM Common Public License as +published by the IBM Corporation; either version 1.0 of the +License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +IBM Common Public License for more details. + +You should have received a copy of the IBM Common Public +License along with this library +*/ +#endregion + using System; using System.Collections.Generic; using System.ComponentModel; @@ -5,6 +25,9 @@ using System.Drawing; using System.Data; using System.Text; using System.Windows.Forms; +using System.Threading; + +using Nuclex.Support.Tracking; namespace Nuclex.Windows.Forms { @@ -14,8 +37,141 @@ namespace Nuclex.Windows.Forms { /// Initializes a new tracking bar public TrackingBar() { InitializeComponent(); + + // We start off being in the idle state (and thus, being invisible) + this.isIdle = true; + base.Visible = false; + + // Initialize the delegates we use to update the control's state and those + // we use to register ourselfes to the tracker's events + this.updateIdleStateDelegate = new MethodInvoker(updateIdleState); + this.updateProgressDelegate = new MethodInvoker(updateProgress); + this.asyncIdleStateChangedDelegate = new EventHandler( + asyncIdleStateChanged + ); + this.asyncProgressUpdateDelegate = new EventHandler( + asyncProgressUpdated + ); + + // Create the tracker and attach ourselfes to its events + this.tracker = new ProgressionTracker(); + this.tracker.AsyncIdleStateChanged += this.asyncIdleStateChangedDelegate; + this.tracker.AsyncProgressUpdated += this.asyncProgressUpdateDelegate; } + /// Tracks the specified progression in the tracking bar + /// Progression to be tracked + public void Track(Progression progression) { + this.tracker.Track(progression); + } + + /// Tracks the specified progression in the tracking bar + /// Progression to be tracked + /// Weight of this progression in the total progress + public void Track(Progression progression, float weight) { + this.tracker.Track(progression, weight); + } + + /// Stops tracking the specified progression + /// Progression to stop tracking + public void Untrack(Progression progression) { + this.tracker.Untrack(progression); + } + + /// + /// Called when the summed progressed of the tracked progressions has changed + /// + /// Progression whose progress has changed + /// Contains the progress achieved by the progression + private void asyncProgressUpdated( + object sender, ProgressUpdateEventArgs arguments + ) { + + // Set the new progress without any synchronization + this.currentProgress = arguments.Progress; + + // Another use of the double-checked locking idiom, here we're trying to optimize + // away the lock in case some "trigger-happy" progressions send way more + // progress updates than the poor control can process :) + if(!this.progressUpdatePending) { + lock(this) { + if(!this.progressUpdatePending) { + this.progressUpdatePending = true; + this.progressUpdateAsyncResult = BeginInvoke(this.updateProgressDelegate); + } + } // lock + } + + } + + /// Called when the tracker becomes enters of leaves the idle state + /// Tracker that has entered or left the idle state + /// Contains the new idle state + private void asyncIdleStateChanged(object sender, IdleStateEventArgs arguments) { + + // Do a fully synchronous update of the idle state. This update must not be + // lost because otherwise, the progress bar might stay on-screen when in fact, + // the background operation has already finished and nothing is happening anymore. + this.isIdle = arguments.Idle; + Invoke(this.updateIdleStateDelegate); + + } + + /// Synchronously updates the value visualized in the progress bar + private void updateProgress() { + lock(this) { + + // Reset the update flag so incoming updates will cause the control to + // update itself another time. + this.progressUpdatePending = false; + EndInvoke(this.progressUpdateAsyncResult); + + // Transform the progress into an integer in the range of the progress bar's + // min and max values (these should normally be set to 0 and 100). + int min = base.Minimum; + int max = base.Maximum; + int progress = (int)(this.currentProgress * (max - min)) + min; + + // Update the control + base.Value = progress; + + // Assigning the value sends PBM_SETPOS to the control which, + // according to MSDN, already causes a redraw! + //base.Invalidate(); + + } // lock + } + + /// + /// Updates the idle state of the progress bar + /// (controls whether the progress bar is shown or invisible) + /// + private void updateIdleState() { + + base.Visible = !this.isIdle; + + } + + /// Whether an update of the control state is pending + private volatile bool progressUpdatePending; + /// Async result for the invoked control state update method + private volatile IAsyncResult progressUpdateAsyncResult; + /// Whether the progress bar is in the idle state + private volatile bool isIdle; + /// Most recently reported progress of the tracker + private volatile float currentProgress; + + /// Tracker used to sum and update the total progress + private ProgressionTracker tracker; + /// Delegate for the progress update method + private MethodInvoker updateProgressDelegate; + /// Delegate for the idle state update method + private MethodInvoker updateIdleStateDelegate; + /// Delegate for the OnAsyncProgressionEnded method + private EventHandler asyncIdleStateChangedDelegate; + /// Delegate for the OnAsyncProgressionProgressUpdated method + private EventHandler asyncProgressUpdateDelegate; + } } // namespace Nuclex.Windows.Forms