diff --git a/progress.cpp b/progress.cpp index c1dbeec..7a821c1 100644 --- a/progress.cpp +++ b/progress.cpp @@ -1,6 +1,10 @@ #include "progress.hpp" + +#ifndef GUI + #include -#include + +#endif #ifdef _WIN32 @@ -9,35 +13,39 @@ #include #define cout std::wcout -using string = std::wstring; #else // UNIX +#ifdef GUI +#include "progresswindow.hpp" +#endif // GUI + #include #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 diff --git a/progress.hpp b/progress.hpp index f7f92ba..c8e4673 100644 --- a/progress.hpp +++ b/progress.hpp @@ -1,8 +1,38 @@ #ifndef PROGRESS_HPP #define PROGRESS_HPP -namespace ProgressBar { -void print(int perc); +#include + +#ifdef _WIN32 +#include +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