📄 rtbutton.cpp
字号:
// Note:
// In order to delete the button using CD records, we need to delete the HotspotBegin
// records that precedes the button record, the Button record itself, and the
// HotspotEnd record that follows the button record.
// We should delete each of those records separately as opposed to passing the cursors
// to the RichText::Delete() method and expecting it to delete everything between the cursors.
// The reason for this requirement lies in the fact that there might be other CD records in
// the rich text that we are not aware of (e.g. SectionBegin record). If, by passing
// the cursors, we delete SectionBegin record but leave the SectionEnd record, this
// may mess up the document.
RT->GetCursor(&Cursor);
// Since we know that we inserted 3 buttons into the document,
// we can search for the 3rd HotspotBegin structure in order to locate the
// records to be deleted.
while (HotspotBeginCounter < 3)
{
if (HotspotBeginCounter == 0)
Lnstatus = Cursor.GotoFirst(LNRTTYPE_COMPOSITE_DATA, &CDHotspotBegin);
else
Lnstatus = Cursor.GotoNext(LNRTTYPE_COMPOSITE_DATA, &CDHotspotBegin);
if (Lnstatus == LNWARN_NOT_FOUND)
{
cout << "Error. Button cannot be found. Unable to delete it." << endl;
NotFound = TRUE;
break;
}
CDSig =CDHotspotBegin.GetCDSignature();
if (CDSig == SIG_CD_HOTSPOTBEGIN)
HotspotBeginCounter++;
}
// OK, found the third HotspotBegin CD record.
// Now we need to get a hold of the Button CD record that follows the HotspotBegin
// and of the HotspotEnd CD record that goes after the Button CD record.
// Once we got a hold of those objects, we can delete them.
// Note!!!
// Do not delete each record as soon as you get a hold of it.
// If you delete the record right away, the pointer that was originally pointing to it
// will now point to nothing, and will throw an error when you will try to use it
// to locate the next CD record that you want to get a hold of.
Cursor.GotoNext(LNRTTYPE_COMPOSITE_DATA, &CDButton);
CDSig = CDButton.GetCDSignature();
if (CDSig != SIG_CD_BUTTON)
NotFound = TRUE;
Cursor.GotoNext (LNRTTYPE_COMPOSITE_DATA, &CDHotspotEnd);
CDSig = CDHotspotEnd.GetCDSignature();
if (CDSig != SIG_CD_HOTSPOTEND)
NotFound = TRUE;
if (NotFound)
cout << "Error. Cannot locate the button." << endl;
else
{
RT->Delete(&CDHotspotBegin);
RT->Delete(&CDButton);
RT->Delete(&CDHotspotEnd);
}
} // END DeleteCDButton()
//---------------------------------------------------------------------------
//
// Name:
// ProcessArguments
//
// Description:
// Scan the input command line and sort out the input strings.
// If no arguments were supplied, prompt for them.
//
// Throws exceptions:
// char * Argument error description
//
//---------------------------------------------------------------------------
void ProcessArguments ( int argc,
char *argv[],
LNString *DatabasePath,
LNString *ServerPath,
LNString *ButtonTitle )
{
LNBOOL prompt = FALSE;
if (argc < 2)
prompt = TRUE;
// Set up the default parameters first.
InitDefaultParams();
*ButtonTitle = ParamString1;
*DatabasePath = ParamString2;
*ServerPath = ParamString3;
if (prompt)
{
// Get user input data.
GetArguments();
*ButtonTitle = ParamString1;
*DatabasePath = ParamString2;
*ServerPath = ParamString3;
}
else
{
// Get info from command line.
if(argc > 1)
*ButtonTitle = argv[1];
if(argc > 2)
*DatabasePath = argv[2];
if(argc > 3)
*ServerPath = argv[3];
}
} // END ProcessArguments()
//---------------------------------------------------------------------------
//
// Name:
// InitDefaultParams
//
// Description:
// Set up all the default parameter strings and numbers for user
// input. Called by ProcessArguments() if no user input params
// were provided.
//
// NOTE:
// You only need to edit this function and ProcessArguments()
// when creating a new sample. The rest of the user interface
// functions below are called by ProcessArguments() and are
// generic, which means that they can stay the same unless
// you modify the number of parameters to use.
// Then you only need to cut or paste in your changes to
// the switch statements in the functions below.
//---------------------------------------------------------------------------
void InitDefaultParams()
{
// Initialize default parameter strings.
PromptString1 = "Button Title : [";
ParamString1 = "My New Button";
PromptString2 = "NSF File Path Name : [";
ParamString2 = "rtxtsamp.nsf";
PromptString3 = "Server Path Name : [";
ParamString3 = "";
} // END InitDefaultParams()
//---------------------------------------------------------------------------
//
// Name:
// GetArguments
//
// Description:
// Allows the user to change any or all of the input parameter
// IOParameter. The inputs can also be left as they are if desired.
//---------------------------------------------------------------------------
void GetArguments()
{
BOOL IsCorrectData = FALSE;
QueryArguments();
while(! IsCorrectData)
{
PrintArguments();
cout << "Are these current data settings correct? [Y] ";
cin >> CommandBuf;
switch( CommandBuf[0] )
{
case 'Y':
case 'y':
IsCorrectData = TRUE; // All done; get out now!
break;
default:
// Prompt again for anything other than a "Y" or carriage return.
if( (CommandBuf) != (const char *)"" )
QueryArguments();
else
IsCorrectData = TRUE; // All done; get out now!
} // END switch
} // END while
} // END GetArguments()
//---------------------------------------------------------------------------
//
// Name:
// PrintArguments
//
// Description:
// Prints out all of the current input parameters to use.
//---------------------------------------------------------------------------
void PrintArguments()
{
cout << endl;
cout << "The current default data settings are: " << endl;
cout << endl;
cout << " " << PromptString1 << ParamString1 << "]" << endl;
cout << " " << PromptString2 << ParamString2 << "]" << endl;
cout << " " << PromptString3 << ParamString3 << "]" << endl;
cout << endl;
} // END PrintArguments()
//---------------------------------------------------------------------------
//
// Name:
// QueryArguments
//
// Description:
// Queries the user to change any of the input parameters.
// A carriage return on any of the prompts leaves it alone.
//---------------------------------------------------------------------------
void QueryArguments()
{
cout << endl;
cout << "Enter " << PromptString1 << ParamString1 << "]> ";
cin >> CommandBuf;
if( (CommandBuf) != (const char *)"" )
ParamString1 = CommandBuf;
cout << "Enter " << PromptString2 << ParamString2 << "]> ";
cin >> CommandBuf;
if( (CommandBuf) != (const char *)"" )
ParamString2 = CommandBuf;
cout << "Enter " << PromptString3 << ParamString3 << "]> ";
cin >> CommandBuf;
if( (CommandBuf) != (const char *)"" )
ParamString3 = CommandBuf;
} // END QueryArguments()
//===========================================================================
//
// IOPARAMETER Class implementation
//
// Description:
// Implementation for a simple generic string buffer class
// to hold user input and output prompts.
//===========================================================================
//===========================================================================
// Constructors.
//===========================================================================
IOParameter::IOParameter() // Default constructor.
{
Size =1; // null terminator.
if(! (BufferPointer = new char[Size]) )
{
cout << "IOParameter: Allocation Error!!!" << endl;
exit(1);
}
BufferPointer[0] = '\0';
}
//===========================================================================
IOParameter::IOParameter( const char *Str ) // Init with const string.
{
Size = strlen(Str) + 1; // Size of string + null term.
if(! (BufferPointer = new char[Size]) )
{
cout << "IOParameter: Allocation Error!!!" << endl;
exit(1);
}
strcpy(BufferPointer, Str);
}
//===========================================================================
// Constructs a decimal radix string representation of an integer.
IOParameter::IOParameter( const int Number )
{
char Buffer[16]; // Temporary buffer for characters.
sprintf(Buffer, "%d", Number);
Size = strlen(Buffer) + 1; // Size of string + null term.
if(! (BufferPointer = new char[Size]) )
{
cout << "IOParameter: Allocation Error!!!" << endl;
exit(1);
}
strcpy(BufferPointer, Buffer);
}
//===========================================================================
IOParameter::IOParameter( const IOParameter &Other ) // Copy constructor.
{
Size = Other.Size;
if(! (BufferPointer = new char[Size]) )
{
cout << "IOParameter: Allocation Error!!!" << endl;
exit(1);
}
strcpy(BufferPointer, Other.BufferPointer);
}
//===========================================================================
// Constructor using 2 IOParameter objects as input.
IOParameter::IOParameter( const IOParameter& Prefix, const IOParameter& Postfix)
{
Size = Prefix.Size + Postfix.Size - 1;
if(! (BufferPointer = new char[Size]) )
{
cout << "IOParameter: Allocation Error!!!" << endl;
exit(1);
}
strcpy(BufferPointer, Prefix.BufferPointer);
strcat(BufferPointer, Postfix.BufferPointer);
}
//===========================================================================
// Subscript, etc.
//===========================================================================
char IOParameter::operator [] (int Index)
{
char Character;
if (Index > Size)
Character = '\0';
else
Character = BufferPointer[Index];
return Character;
}
//===========================================================================
// Assignment.
//===========================================================================
IOParameter IOParameter::operator = ( const IOParameter &Other )
{
if(BufferPointer)
delete [] BufferPointer;
Size = Other.Size;
if(! (BufferPointer = new char[Size]) )
{
cout << "IOParameter: Allocation Error!!!" << endl;
exit(1);
}
strcpy(BufferPointer, Other.BufferPointer);
return *this;
}
//===========================================================================
IOParameter IOParameter::operator = ( const char *Str )
{
Size = strlen(Str) +1;
if (BufferPointer)
delete [] BufferPointer;
if(! (BufferPointer = new char[Size]) )
{
cout << "IOParameter: Allocation Error!!!" << endl;
exit(1);
}
strcpy(BufferPointer, Str);
return *this;
}
//===========================================================================
// I/O operators.
//===========================================================================
ostream &operator << ( ostream &Stream, const IOParameter &Other )
{
Stream << Other.BufferPointer;
return Stream;
}
//===========================================================================
istream &operator >> ( istream &Stream, IOParameter &Other )
{
const int BufferLength = 255; // Arbitrary size; change if needed.
char T[BufferLength]; // temp string...
int Length;
for (Length=0; Length<BufferLength; Length++)
{
Stream.get(T[Length]);
if (T[Length] == '\n') // New line character.
break;
if (T[Length] == '\b') // Backspace character.
{
if(Length)
{
Length--;
// cout << "'\b'"; // For debug only.
}
}
}
T[Length] = '\0';
Length++;
if(Other.BufferPointer)
delete [] Other.BufferPointer;
if (! (Other.BufferPointer = new char[Length]) )
{
cout << "IOParameter: Allocation Error!!!" << endl;
exit(1);
}
Other.Size = Length;
strcpy(Other.BufferPointer, T);
return Stream;
} // END istream &operator >>
//===========================================================================
// Concatenate.
//===========================================================================
IOParameter IOParameter::operator + ( const IOParameter &Other ) // Cat 2 IOParameter objects.
{
return IOParameter( *this, Other );
}
//===========================================================================
IOParameter IOParameter::operator + ( const char *Str ) // Cat IOParameter and string.
{
return IOParameter( *this, IOParameter(Str) );
}
//===========================================================================
IOParameter operator + ( char *Str, const IOParameter &Other ) // Cat string with IOParameter.
{
return IOParameter( IOParameter(Str), Other );
}
//===========================================================================
IOParameter& IOParameter::operator << ( const IOParameter &Other ) // Cat 2 IOParameter objects.
{
IOParameter TempString(*this);
Size = TempString.Size + Other.Size - 1;
if (BufferPointer)
delete [] BufferPointer;
if(! (BufferPointer = new char[Size]) )
{
cout << "IOParameter: Allocation Error!!!" << endl;
exit(1);
}
strcpy(BufferPointer, TempString.BufferPointer);
strcat(BufferPointer, Other.BufferPointer);
return *this;
}
//===========================================================================
IOParameter& IOParameter::operator << ( const char *Str ) // Cat IOParameter and string.
{
IOParameter TempString(*this);
IOParameter Other(Str);
Size = TempString.Size + Other.Size - 1;
if (BufferPointer)
delete [] BufferPointer;
if(! (BufferPointer = new char[Size]) )
{
cout << "IOParameter: Allocation Error!!!" << endl;
exit(1);
}
strcpy(BufferPointer, TempString.BufferPointer);
strcat(BufferPointer, Other.BufferPointer);
return *this;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -