📄 rttable.cpp
字号:
//===========================================================================
//
// Module: RTTABLE.CPP
//
// Description:
// Sample C++ API program that creates and modifies
// two rich text tables using the LNTable class.
//
// Syntax: RTTABLE <dbname> [options]
//
// Where dbname = file path name for the database to which you
// want to add the table. If you do not supply this parameter,
// you will be prompted for arguments at run time.
// Supplying options without this parameter will
// generate an error. If you supply this parameter alone,
// all of the options will be set to their defaults.
//
// "options" are one or more of the following in any order:
//
// R - Number of rows in the table. There cannot be a space
// between the option letter and the number
// (i.e., "RTTABLE rtxtsamp.nsf r10"). Default = 3.
//
// C - Number of columns in the table. There cannot be a
// space between the option letter and the number
// (i.e., "RTTABLE rtxtsamp.nsf r10c15"). Default = 5.
//
//===========================================================================
#include <stdlib.h>
#include "rttable.hpp"
// Input parameter strings.
IOParameter CommandBuf;
IOParameter PromptString1;
IOParameter ParamString1;
// Input parameter numbers.
IOParameter PromptNumber1;
LNINT ParamNumber1;
IOParameter PromptNumber2;
LNINT ParamNumber2;
//---------------------------------------------------------------------------
//
// MAIN
//
//---------------------------------------------------------------------------
int main(int argc, char *argv[])
{
int ProgramStatus = 0;
LNNotesSession Session;
// Throw all errors encountered during command execution.
LNSetThrowAllErrors (TRUE);
try
{
LNString DatabasePath;
LNDatabase DocDB;
LNDocument NewDoc;
LNINT Rows;
LNINT Cols;
LNRichText RT;
LNText Subject, Author;
// Initialize the API.
Session.Init( argc, argv );
// Parse the argument list.
ProcessArguments ( argc, argv, &DatabasePath, &Rows, &Cols );
// Get the specified Notes database and open it.
Session.GetDatabase( DatabasePath, &DocDB );
DocDB.Open();
// Create a new document in the database and open it.
DocDB.CreateDocument(&NewDoc, "MainTopic");
// Create the "Subject" and "From" fields.
// NOTE: Make sure you pass in the LNITEMFLAGS_SUMMARY
// when creating those items. Otherwise, the subject and the author
// will not appear in the document listing.
Subject.SetValue("Rttable");
Author.SetValue("Sam Sample");
NewDoc.CreateItem("From", Author, LNITEMFLAGS_SUMMARY);
NewDoc.CreateItem("Subject", Subject, LNITEMFLAGS_SUMMARY);
// Create the rich text "Body" field in the note.
NewDoc.CreateItem("Body", &RT);
// Add the new table to the current doc.
AppendRTTable( &NewDoc, Rows, Cols );
// Save and close the document
NewDoc.Save();
NewDoc.Close();
// Close the database and set execution status to successful.
DocDB.Close();
ProgramStatus = 0;
} // END try
catch (LNSTATUS lnerror)
{
char ErrorBuf[LNERROR_MESSAGE_LENGTH];
LNGetErrorMessage(lnerror, ErrorBuf, LNERROR_MESSAGE_LENGTH);
cout << "\nError: " << ErrorBuf << endl;
ProgramStatus = 2;
}
catch (const char *pErrorMessage)
{
cout << "\nError: " << pErrorMessage << endl << endl;
ProgramStatus = 1;
}
// Terminate the API.
Session.Term();
return (ProgramStatus);
} // END MAIN
//---------------------------------------------------------------------------
//
// Name:
// AppendRTTable
//
// Description:
// Creates a rich text table and appends it to the body field of
// the LNDocument passed in.
//---------------------------------------------------------------------------
void AppendRTTable( LNDocument *NewDoc, LNINT Rows, LNINT Cols )
{
LNSTATUS lnstatus;
LNRichText RT;
LNRTCursor Cursor1, Cursor2, SectionBeginCursor, SectionEndCursor;
LNRTCursor TableCursor;
LNTable Table1, Table2;
LNTableCell ThisCell1, ThisCell2;
LNString CellText;
char Buffer[80];
LNINT Row, Col;
LNString SectionTitle, ButtonTitle;
LNFormula Formula("@Prompt([Ok];\"Greeting\";\"Hi, how are you?!\")");
LNButton Button1, Button2;
// Get the rich text "Body" field from the document.
// We will append the table to it later.
NewDoc->GetItem("Body", &RT);
// Create a section that contains this table.
// So we need to get a cursor before the table.
RT.GetCursor(&SectionBeginCursor);
// Append a string to the end of the field where the table will follow.
RT << "\nThe New TABLE Goes Between Here:\n\n";
RT << "\n\nAnd Here:\n";
// Get a cursor at the end of this RT object and move it back to the
// point between the two strings above.
RT.GetEndCursor(&Cursor1);
Cursor1-=12;
// Create a new table just after the end cursor position.
// This is more efficient than constructing a table on the stack and
// then inserting it into the rich text because we can avoid an extra copy.
RT.CreateTable(Rows, Cols, &Cursor1, &Table1);
// Get an end cursor again and create a string section between the two
// cursor positions.
RT.GetEndCursor(&SectionEndCursor);
SectionTitle = "Here is the table:";
RT.CreateStringSection(SectionTitle, &SectionBeginCursor, &SectionEndCursor);
// Get a cursor for the table positioned at the first cell: [R0:C0].
Table1.GetCursor(&TableCursor);
// Iterate through each row.
for (Row=0; Row<(int)Rows; Row++)
{
// Iterate through each column.
for (Col=0; Col<(int)Cols; Col++)
{
// Add text in each cell that identifies its
// row/column position. (i.e., "Cell: [R3:C5]")
sprintf(Buffer, "Cell: [R%d:C%d]", Row, Col );
// Insert the text in the current cell
Table1.Insert( Buffer, &TableCursor );
// Get the cell object for the current cursor position.
// We can determine if the cursor is in a table cell,
// and get a smart pointer to the table cell.
lnstatus = TableCursor.GetParentContainer( LNRTTYPE_TABLE_CELL, &ThisCell1 );
// Since we know the cursor is inside a table, it must be inside a
// table cell.
if (lnstatus == LNWARN_NOT_FOUND)
throw ("Object is not in table cell");
// Confirm that the table cell's row and columns match what we think
// the cursor's position is.
if ((ThisCell1.GetRowIndex() != Row) ||
(ThisCell1.GetColumnIndex() != Col))
throw ("The cursor is not in the correct table cell");
// Find out which row and column we are on and, if on
// an outside cell, remove the outer borders to make
// a tic-tac-toe-like structure.
if (Row==0) // first row.
{
if (Col==0) // Left top corner.
{
// Remove left border.
ThisCell1.SetLeftBorderWidth( LNTABLEBORDERTYPE_NONE );
// Remove top border.
ThisCell1.SetTopBorderWidth( LNTABLEBORDERTYPE_NONE );
}
else if (Col==(Cols-1)) // Right top corner.
{
// Remove top border.
ThisCell1.SetTopBorderWidth( LNTABLEBORDERTYPE_NONE );
// Remove right border.
ThisCell1.SetRightBorderWidth( LNTABLEBORDERTYPE_NONE );
}
else // Somewhere in middle of top row.
{
// Remove top border only.
ThisCell1.SetTopBorderWidth( LNTABLEBORDERTYPE_NONE );
}
}
else if (Row==(Rows-1)) // Last row.
{
if ( Col==0) // Left bottom corner.
{
// Remove left border.
ThisCell1.SetLeftBorderWidth( LNTABLEBORDERTYPE_NONE );
// Remove bottom border.
ThisCell1.SetBottomBorderWidth( LNTABLEBORDERTYPE_NONE );
}
else if (Col==(Cols-1)) // Right bottom corner border.
{
// Remove right border.
ThisCell1.SetRightBorderWidth( LNTABLEBORDERTYPE_NONE );
// Remove bottom border.
ThisCell1.SetBottomBorderWidth( LNTABLEBORDERTYPE_NONE );
}
else // Somewhere in middle of bottom row.
{
// Remove bottom border only.
ThisCell1.SetBottomBorderWidth( LNTABLEBORDERTYPE_NONE );
}
}
else // Any row in between.
{
if (Col==0) // Somewhere in middle of first left column.
{
// Remove left border only.
ThisCell1.SetLeftBorderWidth( LNTABLEBORDERTYPE_NONE );
}
if (Col==(Cols-1)) // Somewhere in middle of last right column.
{
// Remove right border only.
ThisCell1.SetRightBorderWidth( LNTABLEBORDERTYPE_NONE );
}
} // END if(Row)
TableCursor++; // Curser increments through table like reading a book.
} // END (iterate Columns loop)
} // END (iterate Rows loop)
// Now let's create another table and demonstrate a few other methods of
// LNTable class.
RT.Append("\n\nAnother table goes here:\n");
RT.GetEndCursor(&Cursor1);
Rows = 1;
Cols = 2;
RT.CreateTable(Rows, Cols, &Cursor1, &Table2);
// Now let's append another row to this table.
Table2.AppendRows(1);
// Change the border style.
// For some reason, this erases the "inside" borders of the table.
// Filed an SPR about it.
// Table2.SetBorderStyle(LNTABLEBORDERSTYLE_EMBOSSED);
// Change the background color of the table
Table2.SetBackgroundColor(LNCOLOR_CYAN);
// Now we'll show how to insert and delete rich text items for a table cell
// Let's create two buttons and delete one of them:
// First choose the cells where to place the buttons
ThisCell1 = Table2(1,1);
ThisCell2 = Table2(0, 0);
// Append a comment
ThisCell1.Append("The button goes here: ");
ThisCell2.Append("The button goes here: ");
// Get the end cursors to place the buttons
ThisCell1.GetEndCursor(&Cursor1);
ThisCell2.GetEndCursor(&Cursor2);
// Create the buttons
ButtonTitle = "Click Me!";
ThisCell1.CreateButton(ButtonTitle, Formula, &Cursor1, &Button1);
ThisCell2.CreateButton(ButtonTitle, Formula, &Cursor2, &Button2);
// Now let's delete one of the buttons and replace the comment in that cell
ThisCell2.Delete(&Button2);
ThisCell2.SetValue("The button used to be here");
// Save the note to disk.
NewDoc->Save();
} // END AppendRTTable()
//---------------------------------------------------------------------------
//
// 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,
LNINT *Rows,
LNINT *Cols )
{
LNBOOL Prompt = FALSE;
if (argc < 2)
Prompt = TRUE;
// Set up the default parameters first.
InitDefaultParams();
*DatabasePath = ParamString1;
*Rows = ParamNumber1;
*Cols = ParamNumber2;
// Check the command line.
if (Prompt)
{
// Get user input data by prompting.
GetArguments();
*DatabasePath = ParamString1;
*Rows = ParamNumber1;
*Cols = ParamNumber2;
}
else
{
int I, J, Number;
// Parse the command line strings.
*DatabasePath = argv[1];
// Parse the command line options.
for(I=2; I<argc; I++)
{
char *argstring = argv[I];
int Count = strlen(argstring);
for (J=0; J<Count; J++)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -