1 //************************************************************************************************** 2 // TextCtrl.cpp * 3 // -------------- * 4 // Started : 2004-06-21 * 5 // Last Update : 2015-04-03 * 6 // Copyright : (C) 2004 by MSWaters * 7 //************************************************************************************************** 8 9 //************************************************************************************************** 10 // * 11 // This program is free software; you can redistribute it and/or modify it under the * 12 // terms of the GNU General Public License as published by the Free Software Foundation; * 13 // either version 3 of the License, or (at your option) any later version. * 14 // * 15 //************************************************************************************************** 16 17 #include "TextCtrl.hpp" 18 19 //************************************************************************************************** 20 // Initialize static variables for class 21 22 int TextCtrl::m_iLinesMax = TXT_LNSDEF; 23 int TextCtrl::m_iLinesDsp = TXT_DISPDEF; 24 25 //************************************************************************************************** 26 // Constructor. 27 TextCtrl(void)28TextCtrl::TextCtrl( void ) : wxTextCtrl( ) 29 { 30 bSetInitMsg( TXT_INITMSG ); 31 32 bClear( ); 33 } 34 35 //************************************************************************************************** 36 // Destructor. 37 ~TextCtrl()38TextCtrl::~TextCtrl( ) 39 { 40 } 41 42 //************************************************************************************************** 43 // Create an instantance of this object. 44 // 45 // Argument List : 46 // poWin - The parent window 47 // oWinID - The window identifier 48 // 49 // Return Values : 50 // true - Success 51 // false - Failure 52 bCreate(wxWindow * poWin,wxWindowID oWinID)53bool TextCtrl::bCreate( wxWindow * poWin, wxWindowID oWinID ) 54 { 55 long lStyle; 56 57 // Check if the object has already been created 58 if( bIsCreated( ) ) return( true ); 59 60 lStyle = wxTE_MULTILINE | wxTE_READONLY | wxSUNKEN_BORDER | wxTE_DONTWRAP | 61 wxHSCROLL; 62 if( ! Create( poWin, oWinID, wxT(""), wxDefaultPosition, wxDefaultSize, lStyle )) return( false ); 63 64 SetFont( FONT_MONO ); 65 66 return( bClear( ) ); 67 } 68 69 //************************************************************************************************** 70 // Clear the text control. 71 // 72 // Return Values : 73 // true - Success 74 // false - Failure 75 bClear(void)76bool TextCtrl::bClear( void ) 77 { 78 long lStyle; 79 80 m_iLinesCnt = 0; 81 82 // Check if the object has already been created 83 if( ! bIsCreated( ) ) return( false ); 84 85 // Left-justify the text control contents 86 lStyle = GetWindowStyle( ); 87 lStyle &= ~( wxTE_LEFT | wxTE_CENTER | wxTE_RIGHT ); 88 lStyle |= wxTE_LEFT; 89 SetWindowStyle( lStyle ); 90 91 Clear( ); // Clear the text control contents 92 SetEditable( false ); // Make the text control non-editable 93 DiscardEdits( ); // Clear internal modified flag as if the current changes had been saved 94 95 return( true ); 96 } 97 98 //************************************************************************************************** 99 // Clear the text control and display a message to the user. 100 // 101 // Return Values : 102 // true - Success 103 // false - Failure 104 bInitialize(void)105bool TextCtrl::bInitialize( void ) 106 { 107 int i1; 108 109 // Check if the object has already been created 110 if( ! bIsCreated( ) ) return( false ); 111 112 bClear( ); // Clear the text control contents 113 114 if( ! m_osInitMsg.IsEmpty( ) ) 115 { 116 // Center the default message horizontally 117 SetWindowStyle( GetWindowStyle( ) | wxTE_CENTRE ); 118 119 // Display the requisite number of blank lines 120 for( i1=1; i1<TXT_INITLNS; i1++ ) 121 AppendText( wxT("\n") ); 122 123 // Display the default message 124 AppendText( m_osInitMsg ); 125 } 126 127 DiscardEdits( ); // Clear internal modified flag as if the current changes had been saved 128 129 return( true ); 130 } 131 132 //************************************************************************************************** 133 // Is the text control empty (ie. contains nothing or just the initialization message). 134 // 135 // Return Values : 136 // true - Success 137 // false - Failure 138 bIsEmpty(void)139bool TextCtrl::bIsEmpty( void ) 140 { 141 long li1; 142 143 if( wxTextCtrl::IsEmpty( ) ) return( true ); 144 145 for( li1=1; li1<TXT_INITLNS; li1++ ) 146 if( ! GetLineText( li1-1 ).IsEmpty( ) ) return( false ); 147 148 if( GetLineText( li1 ).Find( m_osInitMsg ) == wxNOT_FOUND ) return( false ); 149 150 return( true ); 151 } 152 153 //************************************************************************************************** 154 // Set the initialization message to be displayed. 155 // 156 // Argument List : 157 // rosMsg - The initialization message 158 // 159 // Return Values : 160 // true - Success 161 // false - Failure 162 bSetInitMsg(const wxString & rosMsg)163bool TextCtrl::bSetInitMsg( const wxString & rosMsg ) 164 { 165 if( rosMsg.Length( ) > 60 ) return( false ); 166 167 m_osInitMsg = rosMsg; 168 169 return( true ); 170 } 171 172 //************************************************************************************************** 173 // Set the maximum number of lines to be loaded into the control. 174 // 175 // Argument List : 176 // iLines - The maximum number of lines to be displayed 177 // 178 // Return Values : 179 // true - Success 180 // false - Failure 181 bSetLinesMax(int iLines)182bool TextCtrl::bSetLinesMax( int iLines ) 183 { 184 if( iLines<TXT_LNSMIN || iLines>TXT_LNSMAX ) return( false ); 185 186 m_iLinesMax = iLines; 187 188 return( true ); 189 } 190 191 //************************************************************************************************** 192 // Set the number of lines to be displayed in the control. 193 // 194 // Argument List : 195 // iLines - The number of lines to be displayed 196 // 197 // Return Values : 198 // true - Success 199 // false - Failure 200 bSetLinesDsp(int iLines)201bool TextCtrl::bSetLinesDsp( int iLines ) 202 { 203 if( iLines<TXT_DISPMIN || iLines>TXT_DISPMAX ) return( false ); 204 205 m_iLinesDsp = iLines; 206 207 return( true ); 208 } 209 210 211 //************************************************************************************************** 212 // Append a line of text to the text control. 213 // 214 // Argument List : 215 // rosLine - The line of text to append 216 // 217 // Return Values : 218 // true - Success 219 // false - Failure 220 bAppendLine(const wxString & rosLine)221bool TextCtrl::bAppendLine( const wxString & rosLine ) 222 { 223 // Check if the object has already been created 224 if( ! bIsCreated( ) ) return( false ); 225 226 // Check that the max. lines hasn't been reached 227 if( m_iLinesCnt >= m_iLinesMax ) return( false ); 228 229 // Append this text and a line terminator 230 *this << rosLine << wxT('\n'); 231 m_iLinesCnt++; 232 233 return( true ); 234 } 235 236 //************************************************************************************************** 237 // Append the contents of a file to the text control. 238 // 239 // Argument List : 240 // roFName - The file name to append 241 // 242 // Return Values : 243 // true - Success 244 // false - Failure 245 bAppendFile(const wxString & roFName)246bool TextCtrl::bAppendFile( const wxString & roFName ) 247 { 248 wxString os1; 249 size_t szt1; 250 251 // Check if the object has already been created 252 if( ! bIsCreated( ) ) return( false ); 253 254 // Open the file 255 wxTextFile oFile( roFName ); 256 if( ! oFile.Open( ) ) 257 { 258 *this << wxT("Couldn't load the file containing the process output :\n "); 259 if( roFName.IsEmpty( ) ) *this << wxT("The log file name is empty!"); 260 else *this << roFName; 261 return( false ); 262 } 263 264 // Check that the file isn't empty 265 if( oFile.GetLineCount( ) > 0 ) 266 { // Append the file contents to the text control 267 for( szt1=0; szt1<oFile.GetLineCount( ); szt1++ ) 268 { 269 os1 = oFile.GetLine( szt1 ); 270 if( ! bAppendLine( os1 ) ) 271 { 272 *this << wxT("\nText control maximum line count (ie. ") << m_iLinesMax 273 << wxT(") reached.\n"); 274 break; 275 } 276 } 277 } 278 else *this << wxT("There is no process output."); 279 280 oFile.Close( ); // Close the log file 281 282 SetInsertionPoint( 0 ); // Go to the top of the text control 283 284 return( true ); 285 } 286 287 //************************************************************************************************** 288 // Load the contents of a file into the text control. 289 // 290 // Argument List : 291 // roFName - The file name to load 292 // 293 // Return Values : 294 // true - Success 295 // false - Failure 296 bLoadFile(const wxString & roFName)297bool TextCtrl::bLoadFile( const wxString & roFName ) 298 { 299 // Check if the object has already been created 300 if( ! bIsCreated( ) ) return( false ); 301 302 bClear( ); // Clear the text control 303 304 return( bAppendFile( roFName ) ); 305 } 306 307 //************************************************************************************************** 308