Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

dlgResProg.cpp

Go to the documentation of this file.
00001 /* 00002 * wxChecksums 00003 * Copyright (C) 2003-2004 Julien Couot 00004 * 00005 * This program is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU General Public License 00007 * as published by the Free Software Foundation; either version 2 00008 * of the License, or (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00018 */ 00019 00020 /** 00021 * \file dlgResProg.cpp 00022 * Template for progress dialogs which display textual informations. 00023 */ 00024 00025 00026 //--------------------------------------------------------------------------- 00027 // For compilers that support precompilation, includes "wx.h". 00028 #include <wx/wxprec.h> 00029 00030 #ifdef __BORLANDC__ 00031 #pragma hdrstop 00032 #endif 00033 00034 #ifndef WX_PRECOMP 00035 // Include your minimal set of headers here, or wx.h 00036 #include <wx/wx.h> 00037 #endif 00038 00039 #include "dlgResProg.hpp" 00040 #include "comdefs.hpp" 00041 #include "utils.hpp" 00042 00043 #include "compat.hpp" 00044 //--------------------------------------------------------------------------- 00045 00046 00047 /// The C++ standard namespace. 00048 using namespace std; 00049 00050 00051 IMPLEMENT_DYNAMIC_CLASS(dlgResultsProgress, wxDialog) 00052 00053 00054 /** 00055 * Creates a new dialog. 00056 */ 00057 dlgResultsProgress::dlgResultsProgress() : wxDialog() 00058 { 00059 winDisabler = NULL; 00060 createControls(); 00061 } 00062 //--------------------------------------------------------------------------- 00063 00064 00065 /** 00066 * Creates a new dialog. 00067 * 00068 * @param parent Parent of the dialog. 00069 * @param id An identifier for the dialog. A value of <CODE>-1</CODE> is 00070 * taken to mean a default. 00071 * @param title The title of the dialog. 00072 * @param pos The dialog position. A value of <CODE>(-1, -1)</CODE> 00073 * indicates a default position, chosen by either the windowing 00074 * system or wxWidgets, depending on platform. 00075 * @param size The dialog size. A value of <CODE>(-1, -1)</CODE> indicates a 00076 * default size, chosen by either the windowing system or 00077 * wxWidgets, depending on platform. 00078 * @param style The window style. See wxDialog's documentation. 00079 * @param name Used to associate a name with the window, allowing the 00080 * application user to set Motif resource values for individual 00081 * dialog boxes. 00082 */ 00083 dlgResultsProgress::dlgResultsProgress(wxWindow* parent, wxWindowID id, 00084 const wxString& title, const wxPoint& pos, 00085 const wxSize& size, 00086 long style, 00087 const wxString& name) : 00088 wxDialog(parent, id, title, pos, size, style, name) 00089 { 00090 // Dialog may disappear at any moment, let the others know about it. 00091 SetExtraStyle(GetExtraStyle() | wxWS_EX_TRANSIENT); 00092 00093 // Disable all others windows. 00094 winDisabler = new wxWindowDisabler(this); 00095 00096 // Set the dialog state. 00097 setState(running); 00098 00099 // Set the last inserted item. 00100 lastInsertedPos = 0L; 00101 lastInsertedWidth = 0L; 00102 00103 createControls(); 00104 Fit(); 00105 } 00106 //--------------------------------------------------------------------------- 00107 00108 00109 /** 00110 * Creates and initializes the controls of the dialog. 00111 */ 00112 void dlgResultsProgress::createControls() 00113 { 00114 // Creates the controls 00115 txtResults = new wxTextCtrl(this, TXT_RESULTS, wxEmptyString, wxDefaultPosition, wxDefaultSize, 00116 wxTE_DONTWRAP | wxTE_MULTILINE | wxTE_READONLY | wxTE_RICH2); 00117 00118 wxClientDC dc(txtResults); 00119 wxSize labelSize(dc.GetCharWidth() * 20, -1); 00120 00121 btnPause = new wxButton(this, BTN_PAUSE, _("&Pause")); 00122 btnPause->SetDefault(); 00123 btnCancel = new wxButton(this, wxID_CANCEL, _("&Cancel")); 00124 00125 //------------------------------------------------------------------------- 00126 // Creates the dialog sizers 00127 00128 // Dialog sizer 00129 wxBoxSizer* dlgResultsProgressSizer2 = new wxBoxSizer(wxVERTICAL); 00130 this->SetSizer(dlgResultsProgressSizer2); 00131 wxBoxSizer* dlgResultsProgressSizer = new wxBoxSizer(wxVERTICAL); 00132 dlgResultsProgressSizer2->Add(dlgResultsProgressSizer, 1, wxALL | wxGROW, CONTROL_SPACE); 00133 00134 // List of results 00135 dlgResultsProgressSizer->Add(txtResults, 1, wxGROW); 00136 00137 // Information section 00138 infoSizer = new wxBoxSizer(wxVERTICAL); 00139 dlgResultsProgressSizer->Add(infoSizer, 0, wxGROW); 00140 00141 // Validation buttons sizer 00142 wxGridSizer* buttonsSizer = new wxGridSizer(2, 0, 2 * CONTROL_SPACE); 00143 buttonsSizer->Add(btnPause); 00144 buttonsSizer->Add(btnCancel); 00145 dlgResultsProgressSizer->Add(buttonsSizer, 0, wxTOP | wxALIGN_RIGHT, (3 * CONTROL_SPACE) / 2); 00146 } 00147 //--------------------------------------------------------------------------- 00148 00149 00150 /** 00151 * The class descructor. 00152 */ 00153 dlgResultsProgress::~dlgResultsProgress() 00154 { 00155 reenableOtherWindows(); 00156 } 00157 //--------------------------------------------------------------------------- 00158 00159 00160 /** 00161 * Hides or shows the dialog. 00162 * 00163 * @param show If <CODE>TRUE</CODE>, the dialog box is shown and brought to 00164 * the front; otherwise the box is hidden. 00165 */ 00166 bool dlgResultsProgress::Show(bool show) 00167 { 00168 // Reenable other windows before hiding this one because otherwise 00169 // Windows wouldn't give the focus back to the window which had 00170 // been previously focused because it would still be disabled 00171 if (!show) 00172 reenableOtherWindows(); 00173 00174 return wxDialog::Show(show); 00175 } 00176 //--------------------------------------------------------------------------- 00177 00178 00179 /** 00180 * Processes button Cancel. 00181 * 00182 * @param event The event's parameters 00183 */ 00184 void dlgResultsProgress::btnCancelClick(wxCommandEvent& event) 00185 { 00186 if (getState() == finished) 00187 { 00188 // this means that the count down is already finished and we're being 00189 // shown as a modal dialog - so just let the default handler do the job 00190 event.Skip(); 00191 } 00192 else 00193 { 00194 // request to cancel was received 00195 setState(canceled); 00196 00197 // update the button state immediately so that the user knows that the 00198 // request has been noticed 00199 btnCancel->Disable(); 00200 btnPause->Disable(); 00201 } 00202 } 00203 //--------------------------------------------------------------------------- 00204 00205 00206 /** 00207 * Processes button Pause. 00208 * 00209 * @param event The event's parameters 00210 */ 00211 void dlgResultsProgress::btnPauseClick(wxCommandEvent& event) 00212 { 00213 switch (getState()) 00214 { 00215 case running : 00216 btnPause->SetLabel(_("C&ontinue")); 00217 setState(paused); 00218 break; 00219 case paused : 00220 btnPause->SetLabel(_("&Pause")); 00221 setState(running); 00222 break; 00223 } 00224 wxYield(); 00225 #ifdef __WXMAC__ 00226 MacUpdateImmediately(); 00227 #endif 00228 } 00229 //--------------------------------------------------------------------------- 00230 00231 00232 /** 00233 * Event handler to respond to system close events. 00234 * 00235 * @param event event parameters. 00236 */ 00237 void dlgResultsProgress::FrameClose(wxCloseEvent& event) 00238 { 00239 if (getState() == finished) 00240 // let the default handler close the window as we already terminated 00241 event.Skip(); 00242 else 00243 setState(canceled); 00244 } 00245 //--------------------------------------------------------------------------- 00246 00247 00248 /** 00249 * Reenables the others windows. 00250 * 00251 * Must be called to reenable the other windows temporarily disabled while 00252 * the dialog was shown. 00253 */ 00254 void dlgResultsProgress::reenableOtherWindows() 00255 { 00256 if (winDisabler != NULL) 00257 { 00258 delete winDisabler; 00259 winDisabler = NULL; 00260 } 00261 } 00262 //--------------------------------------------------------------------------- 00263 00264 00265 /** 00266 * Says to the dialog that the process is finished. 00267 * 00268 * Sets the state to "finished" and change the caption of the <I>Cancel</I> 00269 * button to "Close". 00270 */ 00271 void dlgResultsProgress::Finished() 00272 { 00273 setState(finished); 00274 00275 btnCancel->SetLabel(_("&Close")); 00276 btnPause->Disable(); 00277 wxYield(); 00278 #ifdef __WXMAC__ 00279 MacUpdateImmediately(); 00280 #endif 00281 ShowModal(); 00282 } 00283 //--------------------------------------------------------------------------- 00284 00285 00286 /** 00287 * Gets the dialog state. 00288 * 00289 * @return The dialog state. 00290 */ 00291 dlgResultsProgress::State dlgResultsProgress::getState() const 00292 { 00293 return state; 00294 } 00295 //--------------------------------------------------------------------------- 00296 00297 00298 /** 00299 * Sets the dialog state. 00300 * 00301 * @param newState The new dialog state. 00302 */ 00303 void dlgResultsProgress::setState(State newState) 00304 { 00305 switch (newState) 00306 { 00307 case running : 00308 case paused : 00309 case canceled : 00310 state = newState; 00311 break; 00312 default : 00313 state = finished; 00314 } 00315 } 00316 //--------------------------------------------------------------------------- 00317 00318 00319 /** 00320 * Refreshes the dialog. 00321 * 00322 * Should be called on each update of a control on the dialog. 00323 */ 00324 void dlgResultsProgress::refreshDialog() 00325 { 00326 // Update the display (especially on X, GTK) 00327 wxYield(); 00328 00329 #ifdef __WXMAC__ 00330 MacUpdateImmediately(); 00331 #endif 00332 } 00333 //--------------------------------------------------------------------------- 00334 00335 00336 /** 00337 * Adds a line to the results. 00338 * 00339 * @param text Line of text to add. 00340 * @param indentation Indentation of the text. 00341 * @param colour Colour of the text. 00342 */ 00343 void dlgResultsProgress::addResultLine(const wxString& text, const unsigned int indentation, const wxColour& colour) 00344 { 00345 wxString indent; 00346 for (unsigned int i = 0; i < indentation; i++) 00347 indent += wxT("\t"); 00348 00349 lastInsertedPos += lastInsertedWidth; 00350 lastInsertedWidth = indent.size() + text.size(); 00351 00352 txtResults->Freeze(); 00353 txtResults->SetDefaultStyle(colour); 00354 txtResults->AppendText(indent + text); 00355 txtResults->Thaw(); 00356 00357 // Update the display (especially on X, GTK) 00358 wxYield(); 00359 00360 #ifdef __WXMAC__ 00361 MacUpdateImmediately(); 00362 #endif 00363 } 00364 //--------------------------------------------------------------------------- 00365 00366 00367 /** 00368 * Replace the last inserted result line. 00369 * 00370 * @param text Line of text to add. 00371 * @param indentation Indentation of the text. 00372 * @param colour Colour of the text. 00373 */ 00374 void dlgResultsProgress::replaceLastResultLine(const wxString& text, const unsigned int indentation, const wxColour& colour) 00375 { 00376 if (lastInsertedWidth > 0) 00377 { 00378 wxString indent; 00379 for (unsigned int i = 0; i < indentation; i++) 00380 indent += wxT("\t"); 00381 00382 // Don't use wxTextCtrl::Replace because it don't use the style on Windows. 00383 txtResults->Freeze(); 00384 txtResults->Remove(lastInsertedPos, lastInsertedPos + lastInsertedWidth); 00385 txtResults->SetDefaultStyle(colour); 00386 txtResults->AppendText(indent + text); 00387 txtResults->Thaw(); 00388 00389 lastInsertedWidth = indent.size() + text.size(); 00390 00391 // Update the display (especially on X, GTK) 00392 wxYield(); 00393 00394 #ifdef __WXMAC__ 00395 MacUpdateImmediately(); 00396 #endif 00397 } 00398 } 00399 //--------------------------------------------------------------------------- 00400 00401 00402 /** 00403 * Remove the last inserted result line. 00404 */ 00405 void dlgResultsProgress::removeLastResultLine() 00406 { 00407 if (lastInsertedWidth > 0) 00408 { 00409 txtResults->Freeze(); 00410 txtResults->Remove(lastInsertedPos, lastInsertedPos + lastInsertedWidth); 00411 txtResults->Thaw(); 00412 00413 lastInsertedWidth = 0L; 00414 00415 // Update the display (especially on X, GTK) 00416 wxYield(); 00417 00418 #ifdef __WXMAC__ 00419 MacUpdateImmediately(); 00420 #endif 00421 } 00422 } 00423 //--------------------------------------------------------------------------- 00424 00425 00426 /** 00427 * Says if the process can continue. 00428 * 00429 * If the state of the dialog is <I>canceled</I> or <I>finished</I> the 00430 * checking can't continue. 00431 * 00432 * @return <CODE>true</CODE> if the state of the dialog is <I>running</I> or 00433 * <I>paused</I>, <CODE>false</CODE> otherwise. 00434 */ 00435 bool dlgResultsProgress::canContinue() const 00436 { 00437 bool res; 00438 00439 switch (getState()) 00440 { 00441 case running : 00442 case paused : 00443 res = true; 00444 break; 00445 default : 00446 res = false; 00447 } 00448 00449 return res; 00450 } 00451 //--------------------------------------------------------------------------- 00452 00453 00454 /** 00455 * Gets a colour from the preferences. 00456 * 00457 * @param pk Preference key from which the colour is get. 00458 * @return A colour from the preferences. 00459 */ 00460 wxColour dlgResultsProgress::getColour(PreferencesKeys pk) 00461 { 00462 long r = AppPrefs::get()->readLong(pk, -1); 00463 00464 if (r == -1) 00465 return wxColour(); 00466 else 00467 return ::longTowxColour(r); 00468 } 00469 //--------------------------------------------------------------------------- 00470 00471 00472 00473 BEGIN_EVENT_TABLE(dlgResultsProgress, wxDialog) 00474 EVT_BUTTON(wxID_CANCEL, dlgResultsProgress::btnCancelClick) 00475 EVT_BUTTON(BTN_PAUSE, dlgResultsProgress::btnPauseClick) 00476 EVT_CLOSE(dlgResultsProgress::FrameClose) 00477 END_EVENT_TABLE() 00478 //---------------------------------------------------------------------------

Generated on Sun May 30 13:37:44 2004 for wxChecksums by doxygen 1.3.7