%% options copyright owner = Dirk Krause copyright year = 2011-xxxx license = bsd %% header #ifndef DK4CONF_H_INCLUDED #include "dk4conf.h" #endif #ifndef WX_WXPREC_H_INCLUDED #include #define WX_WXPREC_H_INCLUDED 1 #endif #ifdef __BORLANDC__ #pragma hdrstop #endif #ifndef WX_PRECOMP #ifndef WX_WX_H_INCLUDED #include #define WX_WX_H_INCLUDED 1 #endif #endif /** Synchronized communication between a worker thread and a progress dialog (ie DkWxProgressDialog). */ class DkWxCommunicator { protected: /** Grant access to only one thread at a time to other members. */ wxCriticalSection csProtect; /** Flag: Thread is still running. */ int iRunning; /** Autostart state. */ int iAutostart; /** Flag: Worker thread can continue. */ int iCanContinue; /** String containing all the messages produced by worker thread. */ wxString *sMessages; /** Current file name processed. */ wxChar *sFilename; /** Current gauge value (0-1000). */ int iGauge; /** Flag: Update required. */ int iUpdated; /** Encoding for wxChar. */ int wxe; /** Encoding for dkChar. */ int dke; /** Most significant log level. */ int iLogLevelMax; /** Change file name, set iUpdated if new file name differs from previous one. @param newFileName New file name. @return 1 on success (new file name saved), 0 on error. */ int internalSetFilename(wxChar const *newFileName); /** Change gauge value, set iUpdated if new value differs from previous one. @param newValue New gauge value. */ void internalSetGauge(int newValue); public: /** Constructor. @param we Encoding used for wxChar strings. @param de Encoding used for dkChar strings. */ DkWxCommunicator(int we, int de); /** Destructor. */ ~DkWxCommunicator(); /** Check whether the object has set up correctly and is ready to use. @return 1 if setup is ok, 0 for error. */ int checkSetup() const; /** Update GUI elements. @param wDialog Progress dialog. @param tFilename Static text to show file name. @param gProgress Progress bar. @return Or-combination of DK_WX_COMMUNICATOR_STATUS_RUNNING and DK_WX_COMMUNICATOR_STATUS_UPDATE. If the thread is no longer running, you can close the progress dialog. If there was an update to file name or progress bar value you need to call Refresh() and Update(). */ int getUpdates( wxWindow *wDialog, wxStaticText *tFilename, wxGauge *gProgress ); /** Set new file name and gauge value, may be used by a worker thread. @param fn New file name. @param nv New gauge value. @return 1 on success, 0 on error. */ int setUpdates(wxChar const *fn, int nv); /** Update gauge value only, leave file name untouched. @param nv New gauge value. */ void updateGauge(int nv); /** Set text to text control. @param tc Text control to use for log messages from worker thread. */ void getText( wxTextCtrl *tc ); /** Prepare to run a worker thread. */ void prepareRun(); /** Set can-continue flag. @param nv New flag value (1=can continue, 0=must stop). Set to 0 if a "Cancel" button was pressed in the progress dialog. */ void setCanContinue(int nv); /** Get can-continue flag, used by the worker thread to react on "Cancel" button. @return 1 on success (can continue), 0 on error (cancel button pressed). */ int getCanContinue(); /** Set iRunning. A worker thread uses setRunning(0) to inidicate it is finished. @param nv New iRunning value. */ void setRunning(int nv); /** Check whether the thread is still running. @return 1 for thread still running, 0 for thread finished. */ int getRunning(); /** Enable/disable autostart job. @param nv New autostart flag. */ void autostartEnable(bool nv = true); /** Check whether we can run an autostart job. On success we change the internal autostart variable to indicate that an autostart job is currently running. @return 1 to run autostart job, 0 otherwise. */ int autostartCanRun(); /** Mark autostart job as finished. This is typically invoked at the end of the autostart worker thread. */ void autostartSetFinished(); /** Check whether the autostart job is completed so we can close the window. @return 1 to close the window, 0 otherwise. */ int autostartIsFinished(); /** Add text to log messages string. @param t Text to add. */ void addWxText(wxChar const *t); /** Add text to log messages string. @param t Text to add. */ void addDkText(dkChar const * t); /** Add text to log messages string. @param t Text to add. */ void addCharText(char const *t); /** Add newline to log messages string. */ void nl(); /** Set most significant log level. @param nv Log level occured. */ void setLogLevel(int nv); /** Retrieve most significant log level. @return Log level. */ int getLogLevel(); /** Retrieve autostart information. @return Current iAutostart value. */ int autostartGet(); /** Get current messages text. @return Current text stored. */ wxString getText(); }; /** Status: Thread is still running. */ #define DK_WX_COMMUNICATOR_STATUS_RUNNING 1 /** Status: Thread updated data since last request. */ #define DK_WX_COMMUNICATOR_STATUS_UPDATE 2 %% module #include "dk3wxs.h" #include #include "DkWxCommunicator.h" $!trace-include /** Empty text to use if no file name is specified. */ static wxChar const dkwx_communicator_empty_text[] = { wxT("") } ; /** Newline. */ static wxChar const dkwx_communicator_newline[] = { wxT("\n") } ; DkWxCommunicator::DkWxCommunicator(int we, int de) { $? "+ DkWxCommunicator::DkWxCommunicator" wxe = we; dke = de; iRunning = 0; iAutostart = DK3_WX_AUTOSTART_NORMAL; $? ". normal start %d", iAutostart sMessages = NULL; sFilename = NULL; iGauge = 0; iUpdated = 0; iCanContinue = 1; iLogLevelMax = DK3_LL_IGNORE; sMessages = new wxString(dkwx_communicator_empty_text); $? "- DkWxCommunicator::DkWxCommunicator" } DkWxCommunicator::~DkWxCommunicator() { { wxCriticalSectionLocker lock(csProtect); } if(sMessages) { delete sMessages; } if(sFilename) { dk3_release(sFilename); } iRunning = 0; iGauge = 0; iUpdated = 0; iCanContinue = 0; } int DkWxCommunicator::checkSetup() const { int back = 0; if(sMessages) { back = 1; } return back; } int DkWxCommunicator::getUpdates( wxWindow *wDialog, wxStaticText *tFilename, wxGauge *gProgress ) { int back = 0; wxCriticalSectionLocker lock(csProtect); if(iUpdated) { back |= DK_WX_COMMUNICATOR_STATUS_UPDATE; if(tFilename) { if(sFilename) { tFilename->SetLabel(sFilename); } else { tFilename->SetLabel(dkwx_communicator_empty_text); } } if(gProgress) { gProgress->SetValue(iGauge); } iUpdated = 0; } if(iRunning) { back |= DK_WX_COMMUNICATOR_STATUS_RUNNING; } return back; } void DkWxCommunicator::internalSetGauge(int newValue) { int nv; nv = newValue; if(nv < 0) nv = 0; if(nv > 1000) nv = 1000; if(iRunning) { if(nv == 1000) nv = 999; } if(nv != iGauge) { iGauge = nv; iUpdated = 1; } } int DkWxCommunicator::internalSetFilename(wxChar const *newFileName) { int back = 0; if(newFileName) { if(sFilename) { if(dk3wxs_cmp(newFileName, sFilename)) { dk3_release(sFilename); sFilename = dk3wxs_dup(newFileName); if(sFilename) { back = 1; } iUpdated = 1; } else { back = 1; } } else { sFilename = dk3wxs_dup(newFileName); if(sFilename) { iUpdated = 1; back = 1; } } } else { if(sFilename) { dk3_release(sFilename); sFilename = NULL; iUpdated = 1; } back = 1; } return back; } int DkWxCommunicator::setUpdates(wxChar const *fn, int nv) { int back = 0; wxCriticalSectionLocker lock(csProtect); if(internalSetFilename(fn)) { back = 1; } internalSetGauge(nv); return back; } void DkWxCommunicator::updateGauge(int nv) { wxCriticalSectionLocker lock(csProtect); internalSetGauge(nv); } void DkWxCommunicator::prepareRun() { wxCriticalSectionLocker lock(csProtect); internalSetFilename(NULL); internalSetGauge(0); iRunning = 1; iCanContinue = 1; sMessages->Empty(); iUpdated = 1; iLogLevelMax = DK3_LL_IGNORE; } void DkWxCommunicator::setCanContinue(int nv) { wxCriticalSectionLocker lock(csProtect); iCanContinue = nv; } int DkWxCommunicator::getCanContinue() { int back = 0; wxCriticalSectionLocker lock(csProtect); back = iCanContinue; return back; } void DkWxCommunicator::setRunning(int nv) { $? "+ DkWxCommunicator::setRunning %d", nv wxCriticalSectionLocker lock(csProtect); /* Set new value. */ iRunning = nv; /* If we are no longer running we have finished the autostart job. */ if(nv == 0) { $? ". no longer running" if(iAutostart == DK3_WX_AUTOSTART_RUNNING) { $? ". was autostart" iAutostart = DK3_WX_AUTOSTART_FINISHED; $? ". autostart finished %d", iAutostart } } $? "- DkWxCommunicator::setRunning" } int DkWxCommunicator::getRunning() { int back = 1; { wxCriticalSectionLocker lock(csProtect); back = iRunning; } return back; } void DkWxCommunicator::getText( wxTextCtrl *tc) { wxCriticalSectionLocker lock(csProtect); if(tc) { if(sMessages) { tc->ChangeValue(*sMessages); } } } void DkWxCommunicator::addWxText(wxChar const *t) { $? "+ addWxText \"%ls\"", t if(sMessages) { $? ". sMessages ok" if(t) { $? ". t ok" wxCriticalSectionLocker lock(csProtect); $? ". before Append" sMessages->Append(t); $? ". after Append" } } $? "- addWxText" } void DkWxCommunicator::addDkText(dkChar const *t) { wxChar bu[2 * DK3_MAX_PATH]; if(sMessages) { if(t) { if(dk3wxs_from_dkstr(bu, DK3_SIZEOF(bu,wxChar), wxe, t, dke)) { wxCriticalSectionLocker lock(csProtect); sMessages->Append(bu); } } } } void DkWxCommunicator::addCharText(char const *t) { wxChar bu[2 * DK3_MAX_PATH]; if(sMessages) { if(t) { if(dk3wxs_from_plain(bu, DK3_SIZEOF(bu,wxChar), t)) { wxCriticalSectionLocker lock(csProtect); sMessages->Append(bu); } } } } void DkWxCommunicator::nl() { if(sMessages) { wxCriticalSectionLocker lock(csProtect); sMessages->Append(dkwx_communicator_newline); } } int DkWxCommunicator::autostartCanRun() { int back = 0; $? "+ DkWxCommunicator::autostartCanRun" { wxCriticalSectionLocker lock(csProtect); if(iAutostart == DK3_WX_AUTOSTART_START) { $? ". autostart wanted" iAutostart = DK3_WX_AUTOSTART_RUNNING; $? ". autostart running %d", iAutostart back = 1; } } $? "- DkWxCommunicator::autostartCanRun %d", back return back; } void DkWxCommunicator::autostartSetFinished() { $? "+ DkWxCommunicator::autostartSetFinished" wxCriticalSectionLocker lock(csProtect); if(iAutostart == DK3_WX_AUTOSTART_RUNNING) { iAutostart = DK3_WX_AUTOSTART_FINISHED; $? ". autostart finished %d", iAutostart } $? "- DkWxCommunicator::autostartSetFinished" } int DkWxCommunicator::autostartIsFinished() { int back = 0; $? "+ DkWxCommunicator::autostartIsFinished" { wxCriticalSectionLocker lock(csProtect); if(iAutostart == DK3_WX_AUTOSTART_FINISHED) { iAutostart = DK3_WX_AUTOSTART_WAIT_FOR_EXIT; $? ". autostart waiting for exit %d", iAutostart back = 1; } } $? "- DkWxCommunicator::autostartIsFinished %d", back return back; } int DkWxCommunicator::autostartGet() { int back; $? "+ DkWxCommunicator::autostartGet" { wxCriticalSectionLocker lock(csProtect); back = iAutostart; } $? "- DkWxCommunicator::autostartGet %d", back return back; } void DkWxCommunicator::autostartEnable(bool nv) { $? "+ DkWxCommunicator::autostartEnable %d", ((nv) ? 1 : 0) wxCriticalSectionLocker lock(csProtect); if(nv) { iAutostart = DK3_WX_AUTOSTART_START; $? ". autostart enabled %d", iAutostart } else { iAutostart = DK3_WX_AUTOSTART_NORMAL; $? ". autostart disabled %d", iAutostart } $? "- DkWxCommunicator::autostartEnable" } void DkWxCommunicator::setLogLevel(int nv) { wxCriticalSectionLocker lock(csProtect); if(nv < iLogLevelMax) { iLogLevelMax = nv; } } int DkWxCommunicator::getLogLevel() { int back = DK3_LL_IGNORE; { wxCriticalSectionLocker lock(csProtect); back = iLogLevelMax; } return back; } wxString DkWxCommunicator::getText() { wxString back(wxT("")); if(sMessages) { back = *sMessages; } return back; }