示例程序
HTML 包的示例源代码包含若干 HTML 代码生成与销毁页面对象的示例。
图 19:使用 HTML 包的示例程序
void HTML_Package_Sample_Frames (const char *output_path)
/****************************************************************************
As an example, lets create a typical frame structure.
Lets split the "top" frame into 2 sub-frames divided vertically into
a right frame and a left frame. Lets say that we want the left frame
to be 20% of the total page width and the right frame to be 80%. Let
us further say that we want the right frame to be divided vertically
and the top frames should be 60% of the right frame's height and the
bottom frame should be 40%. Lets create the need objects below:
*****************************************************************************/
{
FIN (HTML_Package_Sample_Frames (output_path));
/* First, let us set up some useful int values for our frame sizes */
/* All sizes are percentages of the “encapsulating” frame */
int max_frame_size = 100; /* the largest a frame can be */
int left_frame_size = 20; /* the size of our left frame */
int right_frame_size = max_frame_size - left_frame_size;
/* the size of the right frame, from above */
int upper_subframe_size = 60; /* the size of the upper */
int lower_subframe_size = max_frame_size - upper_subframe_size;
/* It is easiest to start with the pages and frames that are the */
/* lowest in the frame hierarchy: */
HtmlT_Page *top_subpage_page_ptr;
HtmlT_Page *bottom_subpage_page_ptr;
/* Now we create the page objects themselves: */
top_subpage_page_ptr = Html_Page_Element_Create ("A blank Html page",
"upper_subpage", 0,0);
bottom_subpage_page_ptr = Html_Page_Element_Create ("A blank Html
page", "bottom_subpage", 0,0);
/* next we need frames to put these frames in: */
HtmlT_Frame *right_upper_subframe_frame_ptr;
HtmlT_Frame *right_lower_subframe_frame_ptr;
/* now we create the frame objects: */
right_upper_subframe_frame_ptr = Html_Frame_Page_Create
(top_subpage_page_ptr, upper_subframe_size, 0);
right_lower_subframe_frame_ptr = Html_Frame_Page_Create
(bottom_subpage_page_ptr, lower_subframe_size, 0);
/* now we create the frameset */
HtmlT_Frame *right_frameset_frame_ptr;
right_frameset_frame_ptr = Html_Frame_Frameset_Create
(HtmlC_Frameset_Rows, right_frame_size, 0);
/* the next step is to add the frames to the frameset */
Html_Frameset_Frame_Add (right_frameset_frame_ptr,
right_upper_subframe_frame_ptr);
Html_Frameset_Frame_Add (right_frameset_frame_ptr,
right_lower_subframe_frame_ptr);
/* we now create the frameset page that will hold the newly */
/* created frameset */
HtmlT_Page *right_frameset_page_ptr;
right_frameset_page_ptr = Html_Page_Frameset_Create
(right_frameset_frame_ptr, "A Frameset Page",
"right_frameset_page", 0);
/* To complete the rest of the frame structure we continue as above */
/* create the next level of pages. (NOTE: one of the pages is done */
/* already, we just finished it.) */
HtmlT_Page *left_frame_page_ptr;
left_frame_page_ptr = Html_Page_Element_Create ("The Left Page",
"left_page", 0, 0);
/* create the frames for the current "level" of frame/page */
HtmlT_Frame *left_frame_ptr;
HtmlT_Frame *right_frame_ptr;
left_frame_ptr = Html_Frame_Page_Create (left_frame_page_ptr,
left_frame_size, 0);
right_frame_ptr = Html_Frame_Page_Create (right_frameset_page_ptr,
right_frame_size, 0);
/* The final steps of setting up this frame structure are to create */
/* the frameset for the left and right frames and then putting the */
/* frameset into a page. */
HtmlT_Frame *top_frameset_ptr;
top_frameset_ptr = Html_Frameset_Frame_Create (HtmlC_Frameset_Columns,
max_frame_size, 0);
Html_Frameset_Frame_Add (top_frameset_ptr, left_frame_ptr);
Html_Frameset_Frame_Add (top_frameset_ptr, right_frame_ptr);
HtmlT_Page *top_page_ptr;
top_page_ptr = Html_Page_Frameset_Create (top_frameset_ptr,
"The Top-most Page", "index", 0);
/* In order to view our handiwork, we need to generate the code and */
/* open a browser to it */
Html_Page_Code_Generate (top_page_ptr, output_path);
Html_Page_Code_Generate (left_page_ptr, output_path);
Html_Page_Code_Generate (right_frameset_page_ptr, output_path);
Html_Page_Code_Generate (top_subpage_page_ptr, output_path);
Html_Page_Code_Generate (bottom_subpage_page_ptr, output_path);
char * index_location_str;
sprintf (index_location_str, "%s%c%index.html", output_path,
PATH_SEPERATOR_CHAR);
Mmi_Browser_Open (index_location_str);
Html_Page_Destroy (top_page_ptr);
Html_Page_Destroy (left_page_ptr);
Html_Page_Destroy (right_frameset_page_ptr);
Html_Page_Destroy (top_subpage_page_ptr);
Html_Page_Destroy (bottom_subpage_page_ptr);
/* Last, we return control up the call stack. */
FOUT;
}
/*****************************************************************
This concludes the example on how to create the nested frame
structure.
NOTE: to create "deeper" frame structures, either put the top_page_ptr
into an HtmlT_Frame as seen above and keep going, or put frameset
pages into the "lowest" level pages.
*****************************************************************/
void
HTML_Package_Sample_List (const char * output_path)
/*****************************************************************************
Another example of the usage of the HTML package: creation
of a list of text elements.
*****************************************************************************/
{
FIN (HTML_Package_Sample_List (output_path);
/* As a preliminary step, let us declare some elements to insert */
/* into the list. */
HtmlT_Element *elem1_ptr;
HtmlT_Element *elem2_ptr;
HtmlT_Element *sub_elem1_ptr;
HtmlT_Element *sub_elem2_ptr;
/* let us also declare an int to make this more legible. */
int no_options = 0;
/* The first step in creating a list of elements is to create the */
/* lists. Let us have a primary list, with a sub-list of elements */
PrgT_List *element_list_lptr; /* the primary list of elements.*/
PrgT_List *subelement_list_lptr; /* the sub-list */
element_list_lptr = Prg_List_Create ();
subelement_list_lptr = Prg_List_Create ();
/* Now let us create the elements we wish to put into the list. */
elem1_ptr = Html_Element_Text_Line_Create ("The first element in the
list", no_options);
elem2_ptr = Html_Element_Text_Line_Create ("The second element",
no_options);
sub_elem1_ptr = Html_Element_Text_Line_Create ("An element in a sub-
list", no_options);
sub_elem2_ptr = Html_Element_Text_Line_Create ("Elements in a list
don't need to be text", no_options);
/* Next we populate the lists with elements. */
Prg_List_Insert (element_list_lptr, elem1_ptr, PRGC_LISTPOS_TAIL);
Prg_List_Insert (element_list_lptr, elem2_ptr, PRGC_LISTPOS_TAIL);
Prg_List_Insert (subelement_list_lptr, sub_elem1_ptr,
PRGC_LISTPOS_TAIL);
Prg_List_Insert (subelement_list_lptr, sub_elem2_ptr,
PRGC_LISTPOS_TAIL);
/* Since we are creating a list element with a list as an element, */
/* we need to create the sub-list element first. */
HtmlT_Element *sub_list_ptr;
sub_list_ptr = Html_Element_List_Create (subelement_list_lptr, "This
is the list's optional title", no_options);
/* Now we add the sub-list element to the main element list. */
Prg_List_Insert (element_list_lptr, sub_list_ptr, PRGC_LISTPOS_TAIL);
/* We now create the main list element from the list of */
/* sub-elements. This list will have now title. */
HtmlT_Element *list_ptr;
list_ptr = Html_Element_List_Create (element_list_lptr, ""/* title */,
no_options);
/* Now that we have constructed our list elements, we add the main */
/* list element to the page, and are done. */
HtmlT_Page *page_ptr = Html_Page_Element_Create ("A Page with a List",
"list_page", no_options);
Html_Page_Element_Add (page_ptr, list_ptr);
/* Having created the page and put the list element in it, we */
/* generate the code, open a browser on the page, and the destroy */
/* the page object. */
Html_Page_Code_Generate (page_ptr, output_path);
char * index_location_str;
sprintf (index_location_str, "%s%c%index.html", output_path,
PATH_SEPERATOR_CHAR);
Mmi_Browser_Open (index_location_str);
Html_Page_Destroy (page_ptr);
/* Lastly, we return control up the call stack. */
FOUT:
}
/****************************************************************************
This concludes the example on how to create lists in the HTML package
NOTE: The HTML package does not impose any limit on the "depth" of a
list, it is therefore possible to have any number of lists inside of
lists.
*****************************************************************************/
void
HTML_Package_Sample_Table (const char* output_path)
/****************************************************************************
Another useful example is creating a table.
We will create a table 4 cells across and 5 high. In each cell will
be nonsense data. Our data will for a mock network usage report. We
also want each row of data to be numbered.
****************************************************************************/
{
FIN (HTML_Package_Sample_Table (output_path));
/* The first step to displaying data in a table is to have data, */
/* and here we create some bogus data to display in our table. */
/* Also, at this time we will create an array of strings that will */
/* be the headers of each column, and a no options constant. */
int no_options = 0;
int num_rows = 5;
char *data [5][4] = {
{"Fast WAN", "16.35%", "10.2 msec", "OC3"},
{"External Employee Entry", "10.21%", "27.8 msec",
"ADSL"},
{"Client Entry", "72.89%", "52.5 msec", "T1"},
{"Maintenance (56K)", "0.15%", "92 msec", "Serial"},
{"Sales Engineer Entry", "20.10%", "15.1 msec", "ISDN"},
};
int num_titles = 4;
char *titles [4] = {"Name", "Link Utilization", "Link Delay", "Link
Type"};
/* Now that we have data that we want to display, the first thing */
/* to do is create a table element. */
HtmlT_Element *table_ptr;
table_elem_ptr = Html_Element_Table_Create ("This is Our Sample Usage
Report:", VOSC_NIL, no_options);
/* The next step is to create the row of column titles into the */
/* table. */
HtmlT_Table_Row *title_row_ptr;
title_row_ptr = Html_Table_Row_Create ();
/* Now we fill the title row with elements from the title array. */
/* We first add a "blank" cell so that the row numbers don't */
/* prevent the column labels from lining up properly. */
HtmlT_Table_Cell *cell_ptr; /* a pointer the cell we are working on */
/* create the blank cell and add it to the row. */
title_elem_ptr = Html_Element_Text_Line_Create ("", no_options);
cell_ptr = Html_Table_Cell_Create (title_elem_ptr, HTML_RGB(255, 206,
156), no_options);
Html_Table_Row_Cell_Add (title_row_ptr, cell_ptr);
/* create each column label and it to the row */
for (int title_index = 0; title_index < num_titles; title_index++)
{
/* First: create an element to represent the title */
/* on the page */
title_elem_ptr = Html_Element_Text_Line_Create (titles
[title_index], no_options);
/* Second: create a cell to contain the data element */
cell_ptr = Html_Table_Cell_Create (title_elem_ptr,
HTML_RGB(255, 206, 156),
HTMLC_TABLE_CELL_OPT_HEADER);
/* Third: put the cell into the title row */
Html_Table_Row_Cell_Add (title_row_ptr, cell_ptr);
}
/* Now add the row to the table. */
Html_Table_Row_Add (table_elem_ptr, title_row_ptr);
/* Now we go through data the way we did for the titles. */
HtmlT_Table_Row *data_row_ptr;
HtmlT_Element *data_elem_ptr;
char *row_lable_str;
for (int row_index = 0; row_index < num_rows; row_index++)
{
/* Create the current data row. */
data_row_ptr = Html_Table_Row_Create ();
/* create the row lable. - create the string */
sprintf (row_lable_str, "Row #%d", row_index +1);
/* create the element */
data_elem_ptr = Html_Element_Text_Line_Create
(row_lable_str, no_options);
/* create a cell around the element */
cell_ptr = Html_Table_Cell_Create (data_elem_ptr,
HTMLC_COLOR_WHITE,
HTMLC_TABLE_CELL_OPT_HEADER);
/* add the cell to the row */
Html_Table_Row_Cell_Add (data_row_ptr, cell_ptr);
/* create elements for the rest of the row and put the into the row */
for (int column_index = 0; column_index < num_columns;
column_index++)
/* this loop finishes the row */
{
/* create the element to hold the data */
data_elem_ptr = Html_Element_Text_Line_Create
(data [row_index][column_index],
no_options);
/* create a cell around the data element */
cell_ptr = Html_Table_Cell_Create
(data_elem_ptr, HTMLC_COLOR_WHITE,
no_options);
/* put the cell in the current row */
Html_Table_Row_Cell_Add (data_row_ptr,
cell_ptr);
}
/* add the row to the table */
Html_Table_Row_Add (table_elem_ptr, data_row_ptr);
}
/* Having completed filling the rows of the table with the data, */
/* we add the table to the page. */
HtmlT_Page *page_ptr = Html_Page_Element_Create ("A Page with a
Table", "table_page", no_options);
Html_Page_Element_Add (page_ptr, table_elem_ptr);
/* Having created the page and put the list element in it, we */
/* generate the code, open a browser on the page, and the destroy */
/* the page object. */
Html_Page_Code_Generate (page_ptr, output_path);
char * index_location_str;
sprintf (index_location_str, "%s%c%index.html", output_path,
PATH_SEPERATOR_CHAR);
Mmi_Browser_Open (index_location_str);
Html_Page_Destroy (page_ptr);
/* Lastly, we return control up the call stack. */
FOUT:
}
/****************************************************************************
That concludes the example on the constructing tables. NOTE: in the
example, only text line elements were used, but any element may be
placed in the cells of a table.
*****************************************************************************/
HTML_Package_Sample_Report (const char *output_path)
/****************************************************************************
The final example for the HTML package will be the construction of a
complete example report. This example will utilize lists, tables,
links, frames, and images. This is to give the user a more complete
sense of what the package was intended to be able to do.
*****************************************************************************/
{
FIN (HTML_Package_Sample_Report (output_path));
/* simple helper variables */
int no_options = 0;
int background_color = HTMLC_COLOR_WHITE;
int nav_bar_size = 30;
int data_frame_size = 70;
int data_subframe_size = 50;
char *page_tile;
strcpy (page_title, "HTML Package Sample Report");
char *path;
/* pages */
HtmlT_Page *index_page_ptr;
HtmlT_Page *navigation_page_ptr;
HtmlT_Page *welcome_page_ptr;
HtmlT_Page *data1_page_ptr;
HtmlT_Page *data2_frameset_page;
HtmlT_Page *data2_top_subpage_ptr;
HtmlT_Page *data2_bottom_subpage_a_ptr;
HtmlT_Page *data2_bottom_subpage_b_ptr;
HtmlT_Page *rvbd_page_ptr;
/* frames */
HtmlT_Frame *main_frameset_ptr;
HtmlT_Frame *navigation_frame_ptr;
HtmlT_Frame *data_frame_ptr;
HtmlT_Frame *data2_frameset_ptr;
HtmlT_Frame *data2_top_frame_ptr;
HtmlT_Frame *data2_bottom_frame_ptr;
/* navigation options */
Prg_List *navigation_links_list_lptr;
HtmlT_Element *navigation_list_elem_ptr;
HtmlT_Element *welcome_link_elem_ptr;
HtmlT_Element *data1_link_elem_ptr;
HtmlT_Element *data2_link_elem_ptr;
HtmlT_Element *data2a_internal_link_ptr;
HtmlT_Element *data2b_internal_link_ptr;
HtmlT_Element *rvbd_link_ptr;
/* on page titles */
HtmlT_Element *welcome_title_elem_ptr
HtmlT_Element *data1_title_elem_ptr;
HtmlT_Element *data2_title_elem_ptr;
HtmlT_Element *data2a_title_elem_ptr;
HtmlT_Element *data2b_title_elem_ptr;
/* data tables */
HtmlT_Element *data1_table_elem_ptr;
HtmlT_Element *data2a_table_elem_ptr;
HtmlT_Element *data2b_table_elem_ptr;
/* welcome blurb */
HtmlT_Element *welcome_statement_ptr;
/* fonts */
HtmlT_Font font_array [] =
{
/* fontname color size options*/
HTML_FONT (VOSC_NIL, HTMLC_COLOR_BLACK,
HTMLC_FONT_SIZE_UNSPEC, 0),
HTML_FONT (VOSC_NIL, HTMLC_COLOR_PALEGREEN,
HTMLC_FONT_SIZE_UNSPEC, 0),
HTML_FONT (VOSC_NIL, HTMLC_COLOR_UNSPEC,
HTMLC_FONT_SIZE_UNSPEC, 0)
};
/* create the data2 sub pages */
data2_bottom_subpage_a_ptr = Html_Page_Element_Create (page_title,
"data_2a", background_color, HTMLC_TEXT_LINE_OPT_HEADER2);
data2_bottom_subpage_b_ptr = Html_Page_Element_Create (page_title,
"data_2b", background_color, HTMLC_TEXT_LINE_OPT_HEADER2);
/* create the frame for the sub pages */
data2_bottom_frame_ptr = Html_Frame_Page_Create
(data2_bottom_subpage_a_ptr, data_subframe_size);
/* create the on page titles for the data2 sub pages */
data2a_title_elem_ptr = Html_Element_Text_Line_Create ("This is the
first item of interest for data2", no_options);
data2b_title_elem_ptr = Html_Element_Text_Line_Create ("This is the
second item of interest for data2", no_options);
/* add data2 titles to subpages */
Html_Page_Element_Add (data2_bottom_subpage_a_ptr,
data2a_title_elem_ptr);
Html_Page_Element_Add (data2_bottom_subpage_b_ptr,
data2b_title_elem_ptr);
/* create the main data2 page */
data2_top_subpage_ptr = Html_Page_Element_Create (page_title,
"data_2", background_color, no_options);
/* add data2 main title to top data2 page */
data2_table_elem_ptr = Html_Element_Text_Line_Create ("The second data
series", HTMLC_TEXT_LINE_OPT_HEADER);
Html_Page_Element_Add (data2_top_subpage_ptr, data2_table_elem_ptr);
/* create the frame for the data2 top page */
data2_top_frame_ptr = Html_Page_Frame_Create (data2_top_subpage_ptr,
data_subframe_size);
/* create the data2 frameset page */
data2_frameset_ptr = Html_Frame_Frameset_Create (HtmlC_Frameset_Rows,
data_frame_size, no_options);
/* add the frames to the frameset */
Html_Frameset_Frame_Add (data2_frameset_ptr, data2_top_frame_ptr);
Html_Frameset_Frame_Add (data2_frameset_ptr, data2_bottom_frame_ptr);
/* create the data2 frameset page */
data2_frameset_page_ptr = Html_Page_Frameset_Create
(data2_frameset_ptr, page_title, "data2_index",
no_options);
/* create the internal data2 link elements */
data2a_internal_link_ptr = Html_Element_Text_Line_Create ("view data2
subset a", no_options);
data2b_internal_link_ptr = Html_Element_Text_Line_Create ("view data2
subset b", no_options);
Html_Page_Element_Add (data2_top_subpage_ptr,
data2a_internal_link_ptr);
Html_Page_Element_Add (data2_top_subpage_ptr,
data2b_internal_link_ptr);
/* form the links */
Html_Element_Link_Page_Create (data2a_internal_link_ptr,
data2_bottom_subpage_a_ptr, data2_bottom_frame_ptr, 0);
Html_Element_Link_Page_Create (data2b_internal_link_ptr,
data2_bottom_subpage_b_ptr, data2_bottom_frame_ptr, 0);
/* create data1 page */
data1_page_ptr = Html_Page_Element_Create (page_title, "data1_page",
background_color, no_options);
/* put the onpage title onto data1's page */
data1_title_elem_ptr = Html_Element_Text_Line_Create ("Data series 1",
HTMLC_TEXT_LINE_OPT_HEADER);
Html_Page_Element_Add (data1_page_ptr, data1_title_elem_ptr);
/* create the welcome page */
welcome_page_ptr = Html_Page_Element_Create (page_title, "welcome",
background_color, no_options);
welcome_title_elem_ptr = Html_Element_Text_Line_Create ("Sample Web
Report", HTMLC_TEXT_LINE_OPT_HEADER);
Html_Page_Element_Add (welcome_page_ptr, welcome_title_elem_ptr);
welcome_statement_prt = Html_Element_Text_Create ("Welcome to the
Riverbed HTML PAckage. This is a sample
report. We hope you find it useful.", font_aray [1],
no_options);
Html_Page_Element_Add (welcome_page_ptr, welcome_statement_ptr);
/* create the frame for the data (right) side of the page */
data_frame_ptr = Html_Frame_Page_Create (welcome_page_ptr,
data_frame_size, no_options);
/* create the navigation page's list of links */
navigation_links_list_lptr = Prg_List_Create ();
welcome_link_elem_ptr = Html_Element_Text_Line_Create ("Home",
no_options);
Prg_List_Insert (navigation_links_list_lptr, welcome_link_elem_ptr,
PRGC_LISTPOS_TAIL);
data1_link_elem_ptr = Html_Element_Text_Line_Create ("Data 1",
no_options);
Prg_List_Insert (navigation_links_list_lptr, data1_link_elem_ptr,
PRGC_LISTPOS_TAIL);
data2_link_elem_ptr = Html_Element_Text_Line_Create ("Data 2",
no_options);
Prg_List_Insert (navigation_links_list_lptr, data2_link_elem_ptr,
PRGC_LISTPOS_TAIL);
rvbd_link_ptr = tml_Element_Text_Line_Create ("Riverbed Home",
no_options);
Prg_List_Insert (navigation_links_list_lptr, rvbd_link_ptr,
PRGC_LISTPOS_TAIL);
/* create the list element */
navigation_list_elem_ptr = Html_Element_List_Create
(navigation_links_list_lptr, "", no_options);
/* create the Navigation page and put it into its frame */
navigation_page_ptr = Html_Page_Element_Create (page_title,
"nav_page", background_color, no_options);
navigation_frame_ptr = Html_Frame_Page_Create (navigation_page_ptr,
nav_bar_size, no_options);
/* construct the main frameset */
main_frameset_ptr = Html_Frame_Frameset_Create
(HtmlC_Frameset_Columns, 100, no_options);
Html_Frameset_Frame_Add (main_frameset_ptr, navigation_frame_ptr);
Html_Frameset_Frame_Add (main_frameset_ptr, data_frame_ptr);
/* create the main page */
index_page_ptr = Html_Page_Frameset_Create (main_frameset_ptr,
page_title, "index", background_color, no_options);
/* create the Riverbed home page dummy page */
rvbd_page_ptr = Html_Page_Remote_Create ("www.riverbed.com",
no_options);
/* connect the navigation bar links to their targets */
Html_Element_Link_Page_Create (welcome_link_elem_ptr,
welcome_page_ptr, data_frame_ptr, 0);
Html_Element_Link_Page_Create (data1_link_elem_ptr, data1_page_ptr,
data_frame_ptr, 0);
Html_Element_Link_Page_Create (data2_link_elem_ptr,
data2_frameset_page_ptr, data_frame_ptr, 0);
Html_Element_Link_Page_Create (rvbd_link_ptr, rvbd_page_ptr,
data_frame_ptr, 1);
/* now we generate the code */
Html_Page_Code_Generate (index_page_ptr, output_path);
Html_Page_Code_Generate (navigation_page_ptr, output_path);
Html_Page_Code_Generate (welcome_page_ptr, output_path);
Html_Page_Code_Generate (data1_page_ptr, output_path);
Html_Page_Code_Generate (data2_frameset_page, output_path);
Html_Page_Code_Generate (data2_top_subpage_ptr, output_path);
Html_Page_Code_Generate (data2_bottom_subpage_a_ptr, output_path);
Html_Page_Code_Generate (data2_bottom_subpage_b_ptr, output_path);
Html_Page_Destroy (index_page_ptr);
Html_Page_Destroy (navigation_page_ptr);
Html_Page_Destroy (welcome_page_ptr);
Html_Page_Destroy (data1_page_ptr);
Html_Page_Destroy (data2_frameset_page);
Html_Page_Destroy (data2_top_subpage_ptr);
Html_Page_Destroy (data2_bottom_subpage_a_ptr);
Html_Page_Destroy (data2_bottom_subpage_b_ptr);
FOUT;
}