Progress: add gui progress capability

This commit is contained in:
zvon 2020-01-17 14:05:30 +01:00
parent 875541b67d
commit af1355ca93
2 changed files with 87 additions and 11 deletions

View File

@ -1,6 +1,10 @@
#include "progress.hpp"
#ifndef GUI
#include <iostream>
#include <stdio.h>
#endif
#ifdef _WIN32
@ -9,35 +13,39 @@
#include <winuser.h>
#define cout std::wcout
using string = std::wstring;
#else // UNIX
#ifdef GUI
#include "progresswindow.hpp"
#endif // GUI
#include <sys/ioctl.h>
#define cout std::cout
#define TEXT( a ) a
using string = std::string;
#endif // UNIX
#ifdef WIN32
#ifndef GUI // Don't need terminal specific functions in GUI
#ifdef _WIN32
size_t getTermWidth() {
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo( GetStdHandle( STD_OUTPUT_HANDLE ), &csbi );
return csbi.srWindow.Right - csbi.srWindow.Left + 1;
}
#else
#else // UNIX
size_t getTermWidth() {
struct winsize w;
ioctl( 0, TIOCGWINSZ, &w );
return w.ws_col;
}
#endif
#endif // UNIX
#ifdef WIN32
#ifdef _WIN32
void home( size_t /*UNUSED*/ ) {
static HANDLE h = NULL;
while ( !h )
@ -47,16 +55,19 @@ void home( size_t /*UNUSED*/ ) {
COORD c = { 0, info.dwCursorPosition.Y - 1 };
SetConsoleCursorPosition( h, c );
}
#else
#else // UNIX
void home( size_t width ) {
cout << "\x1b[" << width << "D";
}
#endif
#endif // UNIX
#endif // not GUI
size_t getNum( size_t width, int perc ) {
return ( width * perc ) / 100;
}
#ifndef GUI
void ProgressBar::print( int perc ) {
auto width = getTermWidth();
home( width );
@ -75,3 +86,38 @@ void ProgressBar::print( int perc ) {
cout << " ";
cout << perc << "%" << std::flush;
}
void ProgressBar::print( const string &t ) {
cout << t << std::endl;
#ifdef _WIN32
cout << std::endl;
#endif
}
#else // GUI
void ProgressBar::print( int perc ) {
#ifndef _WIN32
static_cast< ProgressWindow * >( ptr )->setPerc( perc );
#else
PostMessage( ptr, WM_APP, 0, perc );
#endif
}
void ProgressBar::print( const string &t ) {
#ifndef _WIN32
static_cast< ProgressWindow * >( ptr )->setLabel( t );
#else
PostMessage( ptr, WM_APP, 1, ( LPARAM )t.c_str() );
#endif
}
ProgressBar::~ProgressBar() {
#ifndef _WIN32
static_cast< ProgressWindow * >( ptr )->~ProgressWindow();
#else
PostMessageW( ptr, WM_APP, 2, 0 );
#endif
}
#endif // GUI

View File

@ -1,8 +1,38 @@
#ifndef PROGRESS_HPP
#define PROGRESS_HPP
namespace ProgressBar {
void print(int perc);
#include <string>
#ifdef _WIN32
#include <Windows.h>
using string = std::wstring;
#else
using string = std::string;
#endif
class ProgressBar {
public:
#ifndef GUI
ProgressBar() = default;
~ProgressBar() = default;
#else
#ifndef _WIN32
ProgressBar( void *pw ) : ptr( pw ){};
#else
ProgressBasr( void *hwnd_in ) {
while ( *hwnd_in == nullptr )
sleep( 50 );
ptr = *hwnd_in;
};
#endif // _WIN32
~ProgressBar();
#endif // GUI
void print( int perc );
void print( const string &t );
#ifdef GUI
private:
void *ptr;
#endif
};
#endif