[USETUP] Progress-bar: Code formatting, add annotations, don't use floats.

This commit is contained in:
Hermès Bélusca-Maïto 2018-08-26 20:31:14 +02:00
parent 914769a14a
commit 3a33de0fb1
No known key found for this signature in database
GPG key ID: 3B2539C65E7B93D0
2 changed files with 73 additions and 67 deletions

View file

@ -11,7 +11,7 @@
static static
VOID VOID
DrawBorder( DrawBorder(
PPROGRESSBAR Bar) IN PPROGRESSBAR Bar)
{ {
COORD coPos; COORD coPos;
DWORD Written; DWORD Written;
@ -94,7 +94,7 @@ DrawBorder(
static static
VOID VOID
DrawThickBorder( DrawThickBorder(
PPROGRESSBAR Bar) IN PPROGRESSBAR Bar)
{ {
COORD coPos; COORD coPos;
DWORD Written; DWORD Written;
@ -177,7 +177,7 @@ DrawThickBorder(
static static
VOID VOID
DrawProgressBar( DrawProgressBar(
PPROGRESSBAR Bar) IN PPROGRESSBAR Bar)
{ {
CHAR TextBuffer[8]; CHAR TextBuffer[8];
COORD coPos; COORD coPos;
@ -185,7 +185,7 @@ DrawProgressBar(
PROGRESSBAR BarBorder = *Bar; PROGRESSBAR BarBorder = *Bar;
/* Draw the progress bar "border" border */ /* Draw the progress bar "border" border */
if (Bar->Double) if (Bar->DoubleEdge)
{ {
BarBorder.Top -= 5; BarBorder.Top -= 5;
BarBorder.Bottom += 2; BarBorder.Bottom += 2;
@ -197,8 +197,9 @@ DrawProgressBar(
/* Draw the progress bar border */ /* Draw the progress bar border */
DrawBorder(Bar); DrawBorder(Bar);
/* Write the text associated with the bar */ /* Display the description text */
CONSOLE_SetTextXY(Bar->TextTop, Bar->TextRight, Bar->Text); if (Bar->DescriptionText)
CONSOLE_SetTextXY(Bar->TextTop, Bar->TextRight, Bar->DescriptionText);
/* Print percentage */ /* Print percentage */
sprintf(TextBuffer, "%-3lu%%", Bar->Percent); sprintf(TextBuffer, "%-3lu%%", Bar->Percent);
@ -232,15 +233,15 @@ DrawProgressBar(
PPROGRESSBAR PPROGRESSBAR
CreateProgressBarEx( CreateProgressBarEx(
SHORT Left, IN SHORT Left,
SHORT Top, IN SHORT Top,
SHORT Right, IN SHORT Right,
SHORT Bottom, IN SHORT Bottom,
SHORT TextTop, IN SHORT TextTop,
SHORT TextRight, IN SHORT TextRight,
IN BOOLEAN DoubleEdge, IN BOOLEAN DoubleEdge,
SHORT ProgressColour, IN SHORT ProgressColour,
CHAR *Text) IN PCSTR DescriptionText OPTIONAL)
{ {
PPROGRESSBAR Bar; PPROGRESSBAR Bar;
@ -256,18 +257,19 @@ CreateProgressBarEx(
Bar->Bottom = Bottom; Bar->Bottom = Bottom;
Bar->TextTop = TextTop; Bar->TextTop = TextTop;
Bar->TextRight = TextRight; Bar->TextRight = TextRight;
Bar->Double = DoubleEdge;
Bar->ProgressColour = ProgressColour;
Bar->Text = Text;
Bar->Width = Bar->Right - Bar->Left + 1; Bar->Width = Bar->Right - Bar->Left + 1;
Bar->Percent = 0; Bar->DoubleEdge = DoubleEdge;
Bar->Pos = 0; Bar->ProgressColour = ProgressColour;
Bar->DescriptionText = DescriptionText;
Bar->StepCount = 0; Bar->StepCount = 0;
Bar->CurrentStep = 0; Bar->CurrentStep = 0;
Bar->Percent = 0;
Bar->Pos = 0;
DrawProgressBar(Bar); DrawProgressBar(Bar);
return Bar; return Bar;
@ -275,27 +277,26 @@ CreateProgressBarEx(
PPROGRESSBAR PPROGRESSBAR
CreateProgressBar( CreateProgressBar(
SHORT Left, IN SHORT Left,
SHORT Top, IN SHORT Top,
SHORT Right, IN SHORT Right,
SHORT Bottom, IN SHORT Bottom,
SHORT TextTop, IN SHORT TextTop,
SHORT TextRight, IN SHORT TextRight,
IN BOOLEAN DoubleEdge, IN BOOLEAN DoubleEdge,
CHAR *Text) IN PCSTR DescriptionText OPTIONAL)
{ {
/* Call the Ex variant of the function */ /* Call the Ex variant of the function */
return CreateProgressBarEx(Left, Top, Right, Bottom, return CreateProgressBarEx(Left, Top, Right, Bottom,
TextTop, TextRight, TextTop, TextRight,
DoubleEdge, DoubleEdge,
FOREGROUND_YELLOW | BACKGROUND_BLUE, FOREGROUND_YELLOW | BACKGROUND_BLUE,
Text); DescriptionText);
} }
VOID VOID
DestroyProgressBar( DestroyProgressBar(
PPROGRESSBAR Bar) IN OUT PPROGRESSBAR Bar)
{ {
RtlFreeHeap(ProcessHeap, 0, Bar); RtlFreeHeap(ProcessHeap, 0, Bar);
} }
@ -303,8 +304,8 @@ DestroyProgressBar(
VOID VOID
ProgressSetStepCount( ProgressSetStepCount(
PPROGRESSBAR Bar, IN PPROGRESSBAR Bar,
ULONG StepCount) IN ULONG StepCount)
{ {
Bar->CurrentStep = 0; Bar->CurrentStep = 0;
Bar->StepCount = StepCount; Bar->StepCount = StepCount;
@ -314,15 +315,15 @@ ProgressSetStepCount(
VOID VOID
ProgressNextStep( ProgressNextStep(
PPROGRESSBAR Bar) IN PPROGRESSBAR Bar)
{ {
ProgressSetStep(Bar, Bar->CurrentStep + 1); ProgressSetStep(Bar, Bar->CurrentStep + 1);
} }
VOID VOID
ProgressSetStep( ProgressSetStep(
PPROGRESSBAR Bar, IN PPROGRESSBAR Bar,
ULONG Step) IN ULONG Step)
{ {
CHAR TextBuffer[8]; CHAR TextBuffer[8];
COORD coPos; COORD coPos;
@ -336,7 +337,7 @@ ProgressSetStep(
Bar->CurrentStep = Step; Bar->CurrentStep = Step;
/* Calculate new percentage */ /* Calculate new percentage */
NewPercent = (ULONG)(((100.0 * (float)Bar->CurrentStep) / (float)Bar->StepCount) + 0.5); NewPercent = ((100 * Bar->CurrentStep + (Bar->StepCount / 2)) / Bar->StepCount);
/* Redraw percentage if changed */ /* Redraw percentage if changed */
if (Bar->Percent != NewPercent) if (Bar->Percent != NewPercent)
@ -354,10 +355,10 @@ ProgressSetStep(
&Written); &Written);
} }
/* Calculate bar position */ /* Calculate the bar position */
NewPos = (ULONG)((((float)(Bar->Width - 2) * 2.0 * (float)Bar->CurrentStep) / (float)Bar->StepCount) + 0.5); NewPos = (((Bar->Width - 2) * 2 * Bar->CurrentStep + (Bar->StepCount / 2)) / Bar->StepCount);
/* Redraw bar if changed */ /* Redraw the bar if it has changed */
if (Bar->Pos != NewPos) if (Bar->Pos != NewPos)
{ {
Bar->Pos = NewPos; Bar->Pos = NewPos;
@ -370,7 +371,7 @@ ProgressSetStep(
Bar->Pos / 2, Bar->Pos / 2,
coPos, coPos,
&Written); &Written);
coPos.X += Bar->Pos/2; coPos.X += Bar->Pos / 2;
if (NewPos & 1) if (NewPos & 1)
{ {

View file

@ -26,8 +26,9 @@
#pragma once #pragma once
typedef struct _PROGRESS typedef struct _PROGRESSBAR
{ {
/* Border and text positions */
SHORT Left; SHORT Left;
SHORT Top; SHORT Top;
SHORT Right; SHORT Right;
@ -37,58 +38,62 @@ typedef struct _PROGRESS
SHORT Width; SHORT Width;
ULONG Percent; /* Maximum and current step counts */
SHORT Pos;
ULONG StepCount; ULONG StepCount;
ULONG CurrentStep; ULONG CurrentStep;
BOOLEAN Double; /* User-specific displayed bar progress/position */
ULONG Percent;
SHORT Pos;
/* Static progress bar cues */
BOOLEAN DoubleEdge;
SHORT ProgressColour; SHORT ProgressColour;
CHAR *Text; PCSTR DescriptionText;
} PROGRESSBAR, *PPROGRESSBAR; } PROGRESSBAR, *PPROGRESSBAR;
/* FUNCTIONS ****************************************************************/ /* FUNCTIONS ****************************************************************/
PPROGRESSBAR PPROGRESSBAR
CreateProgressBarEx( CreateProgressBarEx(
SHORT Left, IN SHORT Left,
SHORT Top, IN SHORT Top,
SHORT Right, IN SHORT Right,
SHORT Bottom, IN SHORT Bottom,
SHORT TextTop, IN SHORT TextTop,
SHORT TextRight, IN SHORT TextRight,
IN BOOLEAN DoubleEdge, IN BOOLEAN DoubleEdge,
SHORT ProgressColour, IN SHORT ProgressColour,
CHAR *Text); IN PCSTR DescriptionText OPTIONAL);
PPROGRESSBAR PPROGRESSBAR
CreateProgressBar( CreateProgressBar(
SHORT Left, IN SHORT Left,
SHORT Top, IN SHORT Top,
SHORT Right, IN SHORT Right,
SHORT Bottom, IN SHORT Bottom,
SHORT TextTop, IN SHORT TextTop,
SHORT TextRight, IN SHORT TextRight,
BOOLEAN DoubleEdge, IN BOOLEAN DoubleEdge,
CHAR *Text); IN PCSTR DescriptionText OPTIONAL);
VOID VOID
DestroyProgressBar( DestroyProgressBar(
PPROGRESSBAR Bar); IN OUT PPROGRESSBAR Bar);
VOID VOID
ProgressSetStepCount( ProgressSetStepCount(
PPROGRESSBAR Bar, IN PPROGRESSBAR Bar,
ULONG StepCount); IN ULONG StepCount);
VOID VOID
ProgressNextStep( ProgressNextStep(
PPROGRESSBAR Bar); IN PPROGRESSBAR Bar);
VOID VOID
ProgressSetStep( ProgressSetStep(
PPROGRESSBAR Bar, IN PPROGRESSBAR Bar,
ULONG Step); IN ULONG Step);
/* EOF */ /* EOF */