home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / layout / laygrid.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  88.6 KB  |  4,294 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #include "xp.h"
  20. #include "shist.h"
  21. #include "pa_parse.h"
  22. #include "layout.h"
  23. #include "libmocha.h"
  24.  
  25.  
  26. #define TOP_EDGE    0
  27. #define BOTTOM_EDGE    1
  28. #define LEFT_EDGE    2
  29. #define RIGHT_EDGE    3
  30.  
  31.  
  32. static lo_GridPercent *
  33. lo_parse_percent_list(MWContext *context, char *str, int32 *rows)
  34. {
  35.     char *tptr;
  36.     int32 i, cnt;
  37.     lo_GridPercent *percent_list;
  38.  
  39.     if ((str == NULL)||(*str == '\0'))
  40.     {
  41.         return((lo_GridPercent *)NULL);
  42.     }
  43.  
  44.     cnt = 0;
  45.     tptr = strchr(str, ',');
  46.     while (tptr != NULL)
  47.     {
  48.         cnt++;
  49.         tptr++;
  50.         tptr = strchr(tptr, ',');
  51.     }
  52.     cnt++;
  53.     *rows = cnt;
  54.  
  55.     percent_list = (lo_GridPercent *)XP_ALLOC(cnt * sizeof(lo_GridPercent));
  56.     if (percent_list == NULL)
  57.     {
  58.         return((lo_GridPercent *)NULL);
  59.     }
  60.  
  61.     tptr = str;
  62.     for (i=0; i<cnt; i++)
  63.     {
  64.         char *ptr;
  65.         char *end_ptr;
  66.  
  67.         ptr = strchr(tptr, ',');
  68.         if (ptr != NULL)
  69.         {
  70.             *ptr = '\0';
  71.         }
  72.  
  73.         /*
  74.          * Need the end of the string to check if the number
  75.          * ends in '%', '*', or 'p'.
  76.          */
  77.         if (ptr != NULL)
  78.         {
  79.             end_ptr = (char *)(ptr - 1);
  80.             if (end_ptr < tptr)
  81.             {
  82.                 end_ptr = tptr;
  83.             }
  84.         }
  85.         else
  86.         {
  87.             end_ptr = (char *)(str + XP_STRLEN(str) - 1);
  88.         }
  89.  
  90.         if (*end_ptr == '*')
  91.         {
  92.             percent_list[i].type = LO_PERCENT_FREE;
  93.             percent_list[i].original_value = (int32)XP_ATOI(tptr);
  94.             if (percent_list[i].original_value < 1)
  95.             {
  96.                 percent_list[i].original_value = 1;
  97.             }
  98.         }
  99.         else if (*end_ptr == '%')
  100.         {
  101.             percent_list[i].type = LO_PERCENT_NORMAL;
  102.             percent_list[i].original_value = (int32)XP_ATOI(tptr);
  103.             if (percent_list[i].original_value <= 0)
  104.             {
  105.                 percent_list[i].original_value = 100 / cnt;
  106.                 if (percent_list[i].original_value == 0)
  107.                 {
  108.                     percent_list[i].original_value = 1;
  109.                 }
  110.             }
  111.         }
  112.         /*
  113.          * Default is in "pixels"
  114.          */
  115.         else
  116.         {
  117.             int32 val;
  118.  
  119.             percent_list[i].type = LO_PERCENT_FIXED;
  120.             val = (int32)XP_ATOI(tptr);
  121.             val = FEUNITS_X(val, context);
  122.             if (val < 1)
  123.             {
  124.                 val = 1;
  125.             }
  126.             percent_list[i].original_value = val;
  127.         }
  128.  
  129.         if (ptr != NULL)
  130.         {
  131.             *ptr = ',';
  132.             tptr = (char *)(ptr + 1);
  133.         }
  134.     }
  135.     return(percent_list);
  136. }
  137.  
  138.  
  139. void
  140. lo_FreeGridEdge(lo_GridEdge *edge)
  141. {
  142.     if (edge->cell_array != NULL)
  143.     {
  144.         XP_FREE(edge->cell_array);
  145.     }
  146.     XP_DELETE(edge);
  147. }
  148.  
  149.  
  150. void
  151. lo_FreeGridRec(lo_GridRec *grid)
  152. {
  153.     if (grid == NULL)
  154.     {
  155.         return;
  156.     }
  157.  
  158.     if (grid->row_percents != NULL)
  159.     {
  160.         XP_FREE(grid->row_percents);
  161.     }
  162.     if (grid->col_percents != NULL)
  163.     {
  164.         XP_FREE(grid->col_percents);
  165.     }
  166.  
  167.     if (grid->edge_list != NULL)
  168.     {
  169.         lo_GridEdge *tmp_edge;
  170.         lo_GridEdge *edge_list;
  171.  
  172.         edge_list = grid->edge_list;
  173.         while (edge_list != NULL)
  174.         {
  175.             tmp_edge = edge_list;
  176.             edge_list = edge_list->next;
  177.             lo_FreeGridEdge(tmp_edge);
  178.         }
  179.     }
  180.  
  181.     XP_DELETE(grid);
  182. }
  183.  
  184.  
  185. void
  186. lo_FreeGridCellRec(MWContext *context, lo_GridRec *grid, lo_GridCellRec *cell)
  187. {
  188.     History_entry *hist;
  189.  
  190.     if (cell == NULL)
  191.     {
  192.         return;
  193.     }
  194.  
  195.     if (cell->url != NULL)
  196.     {
  197.         XP_FREE(cell->url);
  198.     }
  199.     if (cell->name != NULL)
  200.     {
  201.         XP_FREE(cell->name);
  202.     }
  203.     if (cell->context != NULL)
  204.     {
  205.         /*
  206.          * If the cell has a context, use it instead of the
  207.          * parent's context.  Otherwise use the parent context.
  208.          * This is bogus but needed to keep the winfe from
  209.          * dumping because it blindly dereferences the context
  210.          * even though it doesn't use it.
  211.          */
  212.         context = cell->context;
  213.     }
  214.     if (cell->hist_list != NULL)
  215.     {
  216.         XP_List *list_ptr;
  217.  
  218.         list_ptr = (XP_List *)cell->hist_list;
  219.         hist = (History_entry *)XP_ListRemoveTopObject(list_ptr);
  220.         while (hist != NULL)
  221.         {
  222.             SHIST_DropEntry(context, hist);
  223.             hist =(History_entry *)XP_ListRemoveTopObject(list_ptr);
  224.         }
  225.         XP_ListDestroy(list_ptr);
  226.     }
  227.     if (cell->hist_array != NULL)
  228.     {
  229.         int32 i;
  230.  
  231.         for (i = 0; i < grid->hist_size; i++)
  232.         {
  233.             hist = (History_entry *)cell->hist_array[i].hist;
  234.             SHIST_DropEntry(context, hist);
  235.         }
  236.         XP_FREE(cell->hist_array);
  237.     }
  238.     XP_DELETE(cell);
  239. }
  240.  
  241.  
  242. static lo_GridEdge *
  243. lo_new_horizontal_edge(MWContext *context, lo_DocState *state,
  244.             int32 cols, int32 x, int32 y, int32 width, int32 height)
  245. {
  246.     lo_GridEdge *edge;
  247.     LO_EdgeStruct *fe_edge;
  248.  
  249.     edge = XP_NEW(lo_GridEdge);
  250.     if (edge == NULL)
  251.     {
  252.         return(NULL);
  253.     }
  254.  
  255.     fe_edge = (LO_EdgeStruct *)lo_NewElement(context, state, LO_EDGE, NULL, 0);
  256.     if (fe_edge == NULL)
  257.     {
  258.         XP_DELETE(edge);
  259.         return(NULL);
  260.     }
  261.  
  262.     edge->x = x;
  263.     edge->y = y;
  264.     edge->width = width;
  265.     edge->height = height;
  266.     edge->is_vertical = FALSE;
  267.     edge->left_top_bound = 0;
  268.     edge->right_bottom_bound = 0;
  269.     edge->cell_cnt = 0;
  270.     edge->cell_max = 0;
  271.     edge->cell_array = NULL;
  272.     edge->next = NULL;
  273.  
  274.     if (cols > 0)
  275.     {
  276.         int32 cells;
  277.  
  278.         cells = 2 * cols;
  279.         edge->cell_array = (lo_GridCellRec **)XP_ALLOC(cells *
  280.             sizeof(lo_GridCellRec *));
  281.         if (edge->cell_array != NULL)
  282.         {
  283.             edge->cell_max = cells;
  284.         }
  285.     }
  286.  
  287.     fe_edge->type = LO_EDGE;
  288.     fe_edge->ele_id = NEXT_ELEMENT;
  289.     fe_edge->x = x;
  290.     fe_edge->x_offset = 0;
  291.     fe_edge->y = y;
  292.     fe_edge->y_offset = 0;
  293.     fe_edge->width = edge->width;
  294.     fe_edge->height = edge->height;
  295.     fe_edge->line_height = 0;
  296.     fe_edge->next = NULL;
  297.     fe_edge->prev = NULL;
  298.  
  299.     fe_edge->FE_Data = NULL;
  300.     fe_edge->lo_edge_info = (void *)edge;
  301.     fe_edge->is_vertical = edge->is_vertical;
  302.     fe_edge->visible = TRUE;
  303.     fe_edge->movable = TRUE;
  304.     fe_edge->left_top_bound = 0;
  305.     fe_edge->right_bottom_bound = 0;
  306.  
  307.     edge->fe_edge = fe_edge;
  308.  
  309.     return(edge);
  310. }
  311.  
  312.  
  313. static lo_GridEdge *
  314. lo_new_vertical_edge(MWContext *context, lo_DocState *state,
  315.             int32 rows, int32 x, int32 y, int32 width, int32 height)
  316. {
  317.     lo_GridEdge *edge;
  318.     LO_EdgeStruct *fe_edge;
  319.  
  320.     edge = XP_NEW(lo_GridEdge);
  321.     if (edge == NULL)
  322.     {
  323.         return(NULL);
  324.     }
  325.  
  326.     fe_edge = (LO_EdgeStruct *)lo_NewElement(context, state, LO_EDGE, NULL, 0);
  327.     if (fe_edge == NULL)
  328.     {
  329.         XP_DELETE(edge);
  330.         return(NULL);
  331.     }
  332.  
  333.     edge->x = x;
  334.     edge->y = y;
  335.     edge->width = width;
  336.     edge->height = height;
  337.     edge->is_vertical = TRUE;
  338.     edge->left_top_bound = 0;
  339.     edge->right_bottom_bound = 0;
  340.     edge->cell_cnt = 0;
  341.     edge->cell_max = 0;
  342.     edge->cell_array = NULL;
  343.     edge->next = NULL;
  344.  
  345.     if (rows > 0)
  346.     {
  347.         int32 cells;
  348.  
  349.         cells = 2 * rows;
  350.         edge->cell_array = (lo_GridCellRec **)XP_ALLOC(cells *
  351.             sizeof(lo_GridCellRec *));
  352.         if (edge->cell_array != NULL)
  353.         {
  354.             edge->cell_max = cells;
  355.         }
  356.     }
  357.  
  358.     fe_edge->type = LO_EDGE;
  359.     fe_edge->ele_id = NEXT_ELEMENT;
  360.     fe_edge->x = x;
  361.     fe_edge->x_offset = 0;
  362.     fe_edge->y = y;
  363.     fe_edge->y_offset = 0;
  364.     fe_edge->width = edge->width;
  365.     fe_edge->height = edge->height;
  366.     fe_edge->line_height = 0;
  367.     fe_edge->next = NULL;
  368.     fe_edge->prev = NULL;
  369.  
  370.     fe_edge->FE_Data = NULL;
  371.     fe_edge->lo_edge_info = (void *)edge;
  372.     fe_edge->is_vertical = edge->is_vertical;
  373.     fe_edge->visible = TRUE;
  374.     fe_edge->movable = TRUE;
  375.     fe_edge->left_top_bound = 0;
  376.     fe_edge->right_bottom_bound = 0;
  377.  
  378.     edge->fe_edge = fe_edge;
  379.  
  380.     return(edge);
  381. }
  382.  
  383.  
  384. static void
  385. lo_grow_edge_cell_array(lo_GridEdge *edge, int32 more)
  386. {
  387.     lo_GridCellRec **old_array;
  388.     lo_GridCellRec **new_array;
  389.     int32 new_size;
  390.  
  391.     /*
  392.      * Error condition, punt.
  393.      */
  394.     if (more <= 0)
  395.     {
  396.         return;
  397.     }
  398.  
  399.     /*
  400.      * If we are not growing the array, but instead allocating a new
  401.      * array.
  402.      */
  403.     if (edge->cell_max == 0)
  404.     {
  405.         edge->cell_array = (lo_GridCellRec **)XP_ALLOC(more *
  406.             sizeof(lo_GridCellRec *));
  407.         if (edge->cell_array != NULL)
  408.         {
  409.             edge->cell_max = more;
  410.         }
  411.         return;
  412.     }
  413.  
  414.     /*
  415.      * Attempt to grow the old array.
  416.      */
  417.     new_size = edge->cell_max + more;
  418.     old_array = edge->cell_array;
  419.     new_array = (lo_GridCellRec **)XP_REALLOC(old_array, (new_size *
  420.             sizeof(lo_GridCellRec *)));
  421.     /*
  422.      * If realloc fails, punt.
  423.      */
  424.     if (new_array == NULL)
  425.     {
  426.         return;
  427.     }
  428.  
  429.     edge->cell_array = new_array;
  430.     edge->cell_max = new_size;
  431. }
  432.  
  433.  
  434. static void
  435. lo_add_cell_to_edge(lo_GridEdge *edge, lo_GridCellRec *cell)
  436. {
  437.     /*
  438.      * Error.
  439.      */
  440.     if ((cell == NULL)||(edge == NULL))
  441.     {
  442.         return;
  443.     }
  444.  
  445.     /*
  446.      * If I did my math right earlier, this will never happen.
  447.      */
  448.     if (edge->cell_cnt >= edge->cell_max)
  449.     {
  450.         lo_grow_edge_cell_array(edge, 1);
  451.         /*
  452.          * This may fail
  453.          */
  454.         if (edge->cell_cnt >= edge->cell_max)
  455.         {
  456.             return;
  457.         }
  458.     }
  459.  
  460.     edge->cell_array[edge->cell_cnt] = cell;
  461.     edge->cell_cnt++;
  462. }
  463.  
  464.  
  465. static void
  466. lo_adjust_percents(int32 count, lo_GridPercent *percents, int32 max)
  467. {
  468.     int32 i;
  469.     int32 fixed_percent;
  470.     int32 free_percent;
  471.     int32 total;
  472.  
  473.     if (max == 0)
  474.         max = 1;
  475.  
  476.     /*
  477.      * Total up the three different type of grid cell percentages.
  478.      */
  479.     fixed_percent = 0;
  480.     free_percent = 0;
  481.     total = 0;
  482.     for (i=0; i < count; i++)
  483.     {
  484.         if (percents[i].type == LO_PERCENT_FIXED)
  485.         {
  486.             /*
  487.              * Now that we know the max dimension turn
  488.              * fixed pixel dimensions into percents.
  489.              */
  490.             percents[i].value = 100 * percents[i].original_value / max;
  491.             if (percents[i].value < 1)
  492.             {
  493.                 percents[i].value = 1;
  494.             }
  495.             fixed_percent += percents[i].value;
  496.         }
  497.         else if (percents[i].type == LO_PERCENT_FREE)
  498.         {
  499.             percents[i].value = percents[i].original_value;
  500.             free_percent += percents[i].original_value;
  501.         }
  502.         else
  503.         {
  504.             percents[i].value = percents[i].original_value;
  505.             total += percents[i].original_value;
  506.         }
  507.     }
  508.  
  509.     /*
  510.      * If the user didn't explicitly use up all the space.
  511.      */
  512.     if ((total + fixed_percent) < 100)
  513.     {
  514.         int32 val;
  515.  
  516.         /*
  517.          * We have some free percentage cells that want to
  518.          * soak up the excess space.
  519.          */
  520.         if (free_percent > 0)
  521.         {
  522.             int32 used;
  523.             int32 last_i;
  524.  
  525.             last_i = -1;
  526.             used = 0;
  527.             val = 100 - (total + fixed_percent);
  528.             for (i=0; i < count; i++)
  529.             {
  530.                 if (percents[i].type == LO_PERCENT_FREE)
  531.                 {
  532.                     percents[i].value *= val;
  533.                     percents[i].value /= free_percent;
  534.                     if (percents[i].value < 1)
  535.                     {
  536.                         percents[i].value = 1;
  537.                     }
  538.                     used += percents[i].value;
  539.                     last_i = i;
  540.                 }
  541.             }
  542.             /*
  543.              * Slop the extra into the last qualifying cell.
  544.              */
  545.             if (((used + total + fixed_percent) < 100)&&
  546.                 (last_i >= 0))
  547.             {
  548.                 percents[last_i].value +=
  549.                     (100 - total - fixed_percent - used);
  550.             }
  551.         }
  552.         /*
  553.          * Else we have no free cells, but have some normal
  554.          * percentage cells, distribute the extra space among them.
  555.          */
  556.         else if (total > 0)
  557.         {
  558.             int32 used;
  559.             int32 last_i;
  560.  
  561.             last_i = -1;
  562.             used = 0;
  563.             val = (100 - fixed_percent);
  564.             for (i=0; i < count; i++)
  565.             {
  566.                 if (percents[i].type == LO_PERCENT_NORMAL)
  567.                 {
  568.                     percents[i].value *= val;
  569.                     percents[i].value /= total;
  570.                     used += percents[i].value;
  571.                     last_i = i;
  572.                 }
  573.             }
  574.             /*
  575.              * Slop the extra into the last qualifying cell.
  576.              */
  577.             if ((used < val)&&(last_i >= 0))
  578.             {
  579.                 percents[last_i].value += (val - used);
  580.             }
  581.         }
  582.         /*
  583.          * Else all we have are fixed percentage cells, we will
  584.          * have to grow them.
  585.          */
  586.         else
  587.         {
  588.             int32 used;
  589.  
  590.             used = 0;
  591.             for (i=0; i < count; i++)
  592.             {
  593.                 percents[i].value *= 100;
  594.                 percents[i].value /= (total + fixed_percent);
  595.                 used += percents[i].value;
  596.             }
  597.             /*
  598.              * Slop the extra into the last cell.
  599.              */
  600.             if ((used < 100)&&(count > 0))
  601.             {
  602.                 percents[count - 1].value += (100 - used);
  603.             }
  604.         }
  605.     }
  606.     /*
  607.      * Else if the user allocated too much space, we need to shrink
  608.      * something.
  609.      */
  610.     else if ((total + fixed_percent) > 100)
  611.     {
  612.         int32 val;
  613.  
  614.         /*
  615.          * If there is not too much fixed percentage
  616.          * added, we can just shrink the normal percentage
  617.          * cells to make things fit.
  618.          */
  619.         if (fixed_percent <= 100)
  620.         {
  621.             int32 used;
  622.             int32 last_i;
  623.  
  624.             last_i = -1;
  625.             used = 0;
  626.             val = (100 - fixed_percent);
  627.             for (i=0; i < count; i++)
  628.             {
  629.                 if (percents[i].type == LO_PERCENT_NORMAL)
  630.                 {
  631.                     percents[i].value *= val;
  632.                     percents[i].value /= total;
  633.                     used += percents[i].value;
  634.                 }
  635.             }
  636.             /*
  637.              * Since integer division always truncates, we either
  638.              * made it fit exactly, or overcompensated and made
  639.              * it too small.
  640.              */
  641.             if ((used < val)&&(last_i >= 0))
  642.             {
  643.                 percents[last_i].value += (val - used);
  644.             }
  645.         }
  646.         /*
  647.          * Else there is too much fixed percentage as well, we will
  648.          * just shrink all the cells.
  649.          */
  650.         else
  651.         {
  652.             int32 used;
  653.  
  654.             used = 0;
  655.             for (i=0; i < count; i++)
  656.             {
  657.                 if (percents[i].type == LO_PERCENT_FREE)
  658.                 {
  659.                     percents[i].value = 0;
  660.                 }
  661.                 else
  662.                 {
  663.                     percents[i].value *= 100;
  664.                     percents[i].value /=
  665.                         (total + fixed_percent);
  666.                 }
  667.                 used += percents[i].value;
  668.             }
  669.             /*
  670.              * Since integer division always truncates, we either
  671.              * made it fit exactly, or overcompensated and made
  672.              * it too small.
  673.              */
  674.             if (used < 100)
  675.             {
  676.                 percents[count - 1].value += (100 - used);
  677.             }
  678.         }
  679.     }
  680. }
  681.  
  682.  
  683. PRIVATE
  684. void
  685. lo_LayoutGridCells(MWContext *context, lo_DocState *state,
  686.     int32 x, int32 y, int32 width, int32 height,
  687.     lo_GridRec *grid, lo_GridEdge **edges)
  688. {
  689.     int32 cols, rows;
  690.     int32 col_cnt, row_cnt;
  691.     int32 orig_x, orig_y;
  692.     lo_GridCellRec *cell_parent;
  693.     lo_GridCellRec *cell_list;
  694.     lo_GridEdge **col_edges;
  695.     lo_GridEdge *top_edge;
  696.     lo_GridEdge *new_edges[4];    /* top, bottom, left, right */
  697.  
  698.     new_edges[TOP_EDGE] = NULL;
  699.     new_edges[BOTTOM_EDGE] = NULL;
  700.     new_edges[LEFT_EDGE] = NULL;
  701.     new_edges[RIGHT_EDGE] = NULL;
  702.  
  703.     if (grid == NULL)
  704.     {
  705.         return;
  706.     }
  707.  
  708.     cols = grid->cols;
  709.     rows = grid->rows;
  710.     cell_list = grid->cell_list;
  711.  
  712.     /*
  713.      * Allocate space for the temporary array of column
  714.      * edge pointer we will need here.
  715.      */
  716.     col_edges = (lo_GridEdge **)XP_ALLOC(sizeof(lo_GridEdge *) * cols);
  717.     if (col_edges == NULL)
  718.     {
  719.         return;
  720.     }
  721.     top_edge = NULL;
  722.  
  723.     /*
  724.      * If we came in with bounding edges, we are a sub-grid, and we need
  725.      * to increase the size of the cell arrays in those edges to reflect
  726.      * the extra cells now bordering them.
  727.      */
  728.     if (edges[TOP_EDGE] != NULL)
  729.     {
  730.         int32 more;
  731.  
  732.         more = (cols * 2) - 1;
  733.         lo_grow_edge_cell_array(edges[TOP_EDGE], more);
  734.     }
  735.     if (edges[BOTTOM_EDGE] != NULL)
  736.     {
  737.         int32 more;
  738.  
  739.         more = (cols * 2) - 1;
  740.         lo_grow_edge_cell_array(edges[BOTTOM_EDGE], more);
  741.     }
  742.     if (edges[LEFT_EDGE] != NULL)
  743.     {
  744.         int32 more;
  745.  
  746.         more = (rows * 2) - 1;
  747.         lo_grow_edge_cell_array(edges[LEFT_EDGE], more);
  748.     }
  749.     if (edges[RIGHT_EDGE] != NULL)
  750.     {
  751.         int32 more;
  752.  
  753.         more = (rows * 2) - 1;
  754.         lo_grow_edge_cell_array(edges[RIGHT_EDGE], more);
  755.     }
  756.  
  757.     orig_x = x;
  758.     orig_y = y;
  759.     col_cnt = 0;
  760.     row_cnt = 0;
  761.     cell_parent = NULL;
  762.     while (cell_list != NULL)
  763.     {
  764.         lo_GridEdge *tmp_edge;
  765.  
  766.         tmp_edge = NULL; /* make gcc happy */
  767.  
  768.         cell_list->width_percent =
  769.             (intn)grid->col_percents[col_cnt].value;
  770.         cell_list->height_percent =
  771.             (intn)grid->row_percents[row_cnt].value;
  772.  
  773.         cell_list->x = x;
  774.         if (col_cnt != 0)
  775.         {
  776.             cell_list->x += grid->grid_cell_border;
  777.         }
  778.         cell_list->y = y;
  779.         if (row_cnt != 0)
  780.         {
  781.             cell_list->y += grid->grid_cell_border;
  782.         }
  783.  
  784.         cell_list->width = width * cell_list->width_percent / 100;
  785.         if (cell_list->width < grid->grid_cell_min_dim)
  786.         {
  787.             cell_list->width = grid->grid_cell_min_dim;
  788.         }
  789.         if (col_cnt > 0)
  790.         {
  791.             cell_list->width -= grid->grid_cell_border;
  792.         }
  793.         if (col_cnt < (cols - 1))
  794.         {
  795.             cell_list->width -= grid->grid_cell_border;
  796.         }
  797.  
  798.         /*
  799.          * Make sure the last column uses all the remaining space.
  800.          * We subtract orig_x to get cell_list->x into the same coord
  801.          * space as width.
  802.          */
  803.         if (col_cnt == (cols - 1))
  804.         {
  805.             cell_list->width = width - (cell_list->x - orig_x);
  806.         }
  807.  
  808.         cell_list->height = height * cell_list->height_percent / 100;
  809.         if (cell_list->height < grid->grid_cell_min_dim)
  810.         {
  811.             cell_list->height = grid->grid_cell_min_dim;
  812.         }
  813.         if (row_cnt > 0)
  814.         {
  815.             cell_list->height -= grid->grid_cell_border;
  816.         }
  817.         if (row_cnt < (rows - 1))
  818.         {
  819.             cell_list->height -= grid->grid_cell_border;
  820.         }
  821.  
  822.         /*
  823.          * Make sure the last row uses all the remaining space.
  824.          * We subtract orig_y to get cell_list->y into the same coord
  825.          * space as height.
  826.          */
  827.         if (row_cnt == (rows - 1))
  828.         {
  829.             cell_list->height = height - (cell_list->y - orig_y);
  830.         }
  831.  
  832.         /*
  833.          * If this is the first column, for any row but the
  834.          * last row, create a new edge to be the bottom of this
  835.          * row.
  836.          */
  837.         if ((col_cnt == 0)&&(row_cnt < (rows - 1)))
  838.         {
  839.             tmp_edge = lo_new_horizontal_edge(context, state,
  840.                 cols, x, (cell_list->y + cell_list->height),
  841.                 width, (2 * grid->grid_cell_border));
  842.             if (tmp_edge != NULL)
  843.             {
  844.                 tmp_edge->next = grid->edge_list;
  845.                 grid->edge_list = tmp_edge;
  846.             }
  847.         }
  848.  
  849.         /*
  850.          * When in the first row, create a new side edge on
  851.          * each column but the last one.
  852.          */
  853.         if ((row_cnt == 0)&&(col_cnt < (cols - 1)))
  854.         {
  855.             lo_GridEdge *v_edge;
  856.  
  857.             v_edge = lo_new_vertical_edge(context, state,
  858.                 rows, (cell_list->x + cell_list->width), y,
  859.                 (2 * grid->grid_cell_border), height);
  860.             if (v_edge != NULL)
  861.             {
  862.                 v_edge->next = grid->edge_list;
  863.                 grid->edge_list = v_edge;
  864.             }
  865.             col_edges[col_cnt] = v_edge;
  866.         }
  867.  
  868.         /*
  869.          * If this is the first column in the row, we need to
  870.          * set top and bottom edges, for later columns in
  871.          * the same row, they remain unchanged.
  872.          */
  873.         if (col_cnt == 0)
  874.         {
  875.             new_edges[TOP_EDGE] = top_edge;
  876.             if (row_cnt == 0)
  877.             {
  878.                 new_edges[TOP_EDGE] = edges[TOP_EDGE];
  879.             }
  880.             new_edges[BOTTOM_EDGE] = tmp_edge;
  881.             if (row_cnt == (rows - 1))
  882.             {
  883.                 new_edges[BOTTOM_EDGE] = edges[BOTTOM_EDGE];
  884.             }
  885.             /*
  886.              * Set top_edge for next time around.
  887.              */
  888.             top_edge = tmp_edge;
  889.         }
  890.  
  891.         /*
  892.          * Set the left and right edges for this column.
  893.          */
  894.         if (col_cnt == 0)
  895.         {
  896.             new_edges[LEFT_EDGE] = edges[LEFT_EDGE];
  897.         }
  898.         else
  899.         {
  900.             new_edges[LEFT_EDGE] = col_edges[(col_cnt - 1)];
  901.         }
  902.         if (col_cnt == (cols - 1))
  903.         {
  904.             new_edges[RIGHT_EDGE] = edges[RIGHT_EDGE];
  905.         }
  906.         else
  907.         {
  908.             new_edges[RIGHT_EDGE] = col_edges[col_cnt];
  909.         }
  910.  
  911.         x += cell_list->width;
  912.         if (col_cnt > 0)
  913.         {
  914.             x += grid->grid_cell_border;
  915.         }
  916.         if (col_cnt < (cols - 1))
  917.         {
  918.             x += grid->grid_cell_border;
  919.         }
  920.         col_cnt++;
  921.         if (col_cnt >= cols)
  922.         {
  923.             x = orig_x;
  924.             y += cell_list->height;
  925.             if (row_cnt > 0)
  926.             {
  927.                 y += grid->grid_cell_border;
  928.             }
  929.             if (row_cnt < (rows - 1))
  930.             {
  931.                 y += grid->grid_cell_border;
  932.             }
  933.             col_cnt = 0;
  934.             row_cnt++;
  935.         }
  936.  
  937.         if (cell_list->subgrid != NULL)
  938.         {
  939.             lo_GridRec *subgrid;
  940.             lo_GridCellRec *dummy_cell;
  941.  
  942.             subgrid = cell_list->subgrid;
  943.             dummy_cell = cell_list;
  944.  
  945.             subgrid->grid_cell_border = grid->grid_cell_border;
  946.             subgrid->grid_cell_min_dim = grid->grid_cell_min_dim;
  947.  
  948.             /*
  949.              * Make sure all the row percentages total up
  950.              * to 100%
  951.              */
  952.             lo_adjust_percents(subgrid->rows, subgrid->row_percents,
  953.                 cell_list->height);
  954.  
  955.             /*
  956.              * Make sure all the col percentages total up
  957.              * to 100%
  958.              */
  959.             lo_adjust_percents(subgrid->cols, subgrid->col_percents,
  960.                 cell_list->width);
  961.  
  962.             lo_LayoutGridCells(context, state,
  963.                 cell_list->x, cell_list->y,
  964.                 cell_list->width, cell_list->height,
  965.                 subgrid, new_edges);
  966.  
  967.             /*
  968.              * Merge the edge list of the sub-grid with that of
  969.              * the parent grid.
  970.              */
  971.             if (subgrid->edge_list != NULL)
  972.             {
  973.                 lo_GridEdge *a_edge;
  974.  
  975.                 a_edge = subgrid->edge_list;
  976.                 while (a_edge->next != NULL)
  977.                 {
  978.                     a_edge = a_edge->next;
  979.                 }
  980.                 a_edge->next = grid->edge_list;
  981.                 grid->edge_list = subgrid->edge_list;
  982.                 subgrid->edge_list = NULL;
  983.             }
  984.  
  985.             /*
  986.              * Replace the subgrid cell in the list with its
  987.              * own list of member cells.
  988.              */
  989.             /*
  990.              * If the cell to be replace is not the head of the
  991.              * list.
  992.              */
  993.             if (cell_parent != NULL)
  994.             {
  995.                 /*
  996.                  * If the cell is being replaced with a list
  997.                  * of one or more cells.
  998.                  */
  999.                 if (subgrid->cell_list != NULL)
  1000.                 {
  1001.                 cell_parent->next = subgrid->cell_list;
  1002.                 subgrid->cell_list_last->next = cell_list->next;
  1003.                 /*
  1004.                  * If the cell being replaced was the end of
  1005.                  * the list, set the new end properly.
  1006.                  */
  1007.                 if (grid->cell_list_last == cell_list)
  1008.                 {
  1009.                     grid->cell_list_last =
  1010.                     subgrid->cell_list_last;
  1011.                 }
  1012.                 cell_parent = subgrid->cell_list_last;
  1013.                 cell_list = cell_list->next;
  1014.                 }
  1015.                 /*
  1016.                  * If the cell is being replaced with
  1017.                  * no cells (just remove it.)
  1018.                  */
  1019.                 else
  1020.                 {
  1021.                 cell_parent->next = cell_list->next;
  1022.                 /*
  1023.                  * If the cell being replaced was the end of
  1024.                  * the list, set the new end properly.
  1025.                  */
  1026.                 if (grid->cell_list_last == cell_list)
  1027.                 {
  1028.                     grid->cell_list_last = cell_parent;
  1029.                 }
  1030.                 cell_parent = cell_parent;
  1031.                 cell_list = cell_list->next;
  1032.                 }
  1033.             }
  1034.             /*
  1035.              * Else we are replacing the head of the list.
  1036.              */
  1037.             else
  1038.             {
  1039.                 /*
  1040.                  * If the cell is being replaced with a list
  1041.                  * of one or more cells.
  1042.                  */
  1043.                 if (subgrid->cell_list != NULL)
  1044.                 {
  1045.                 grid->cell_list = subgrid->cell_list;
  1046.                 subgrid->cell_list_last->next = cell_list->next;
  1047.                 /*
  1048.                  * If the cell being replaced was the end of
  1049.                  * the list, set the new end properly.
  1050.                  */
  1051.                 if (grid->cell_list_last == cell_list)
  1052.                 {
  1053.                     grid->cell_list_last =
  1054.                     subgrid->cell_list_last;
  1055.                 }
  1056.                 cell_parent = subgrid->cell_list_last;
  1057.                 cell_list = cell_list->next;
  1058.                 }
  1059.                 /*
  1060.                  * If the cell is being replaced with
  1061.                  * no cells (just remove it.)
  1062.                  */
  1063.                 else
  1064.                 {
  1065.                 grid->cell_list = cell_list->next;
  1066.                 /*
  1067.                  * If the cell being replaced was the end of
  1068.                  * the list, set the new end properly.
  1069.                  */
  1070.                 if (grid->cell_list_last == cell_list)
  1071.                 {
  1072.                     grid->cell_list_last = cell_list->next;
  1073.                 }
  1074.                 cell_parent = cell_parent;
  1075.                 cell_list = cell_list->next;
  1076.                 }
  1077.             }
  1078.  
  1079.             lo_FreeGridRec(subgrid);
  1080.             lo_FreeGridCellRec(context, grid, dummy_cell);
  1081.         }
  1082.         /*
  1083.          * Else this was a normal cell.
  1084.          */
  1085.         else
  1086.         {
  1087.             /*
  1088.              * Add this cell to the cell array for each edge
  1089.              * it borders.
  1090.              */
  1091.             if (new_edges[TOP_EDGE] != NULL)
  1092.             {
  1093.                 lo_add_cell_to_edge(new_edges[TOP_EDGE],
  1094.                     cell_list);
  1095.             }
  1096.             if (new_edges[BOTTOM_EDGE] != NULL)
  1097.             {
  1098.                 lo_add_cell_to_edge(new_edges[BOTTOM_EDGE],
  1099.                     cell_list);
  1100.             }
  1101.             if (new_edges[LEFT_EDGE] != NULL)
  1102.             {
  1103.                 lo_add_cell_to_edge(new_edges[LEFT_EDGE],
  1104.                     cell_list);
  1105.             }
  1106.             if (new_edges[RIGHT_EDGE] != NULL)
  1107.             {
  1108.                 lo_add_cell_to_edge(new_edges[RIGHT_EDGE],
  1109.                     cell_list);
  1110.             }
  1111.  
  1112.             cell_list->side_edges[TOP_EDGE] =
  1113.                 new_edges[TOP_EDGE];
  1114.             cell_list->side_edges[BOTTOM_EDGE] =
  1115.                 new_edges[BOTTOM_EDGE];
  1116.             cell_list->side_edges[LEFT_EDGE] =
  1117.                 new_edges[LEFT_EDGE];
  1118.             cell_list->side_edges[RIGHT_EDGE] =
  1119.                 new_edges[RIGHT_EDGE];
  1120.  
  1121.             cell_parent = cell_list;
  1122.             cell_list = cell_list->next;
  1123.         }
  1124.  
  1125.     }
  1126.  
  1127.     if (col_edges != NULL)
  1128.     {
  1129.         XP_FREE(col_edges);
  1130.     }
  1131. }
  1132.  
  1133. static void
  1134. lo_set_edge_bounds(lo_GridEdge *edge)
  1135. {
  1136.     int32 i;
  1137.     int32 min, max;
  1138.     Bool visible;
  1139.     Bool movable;
  1140.     int16 size;
  1141.     int8 color_priority;
  1142.     LO_Color *bg_color;
  1143.  
  1144.     if (edge == NULL)
  1145.     {
  1146.         return;
  1147.     }
  1148.  
  1149.     visible = FALSE;
  1150.     movable = TRUE;
  1151.     size = -1;
  1152.     color_priority = 0;
  1153.     bg_color = NULL;
  1154.  
  1155.     min = -1;
  1156.     max = -1;
  1157.     /*
  1158.      * For horizontal edges
  1159.      */
  1160.     if (edge->is_vertical == FALSE)
  1161.     {
  1162.         for (i=0; i < edge->cell_cnt; i++)
  1163.         {
  1164.             lo_GridCellRec *cell;
  1165.  
  1166.             cell = edge->cell_array[i];
  1167.             if (cell == NULL)
  1168.             {
  1169.                 continue;
  1170.             }
  1171.  
  1172.             /*
  1173.              * If a cell on this edge is not resizable,
  1174.              * this edge is not movable.
  1175.              */
  1176.             if (cell->resizable == FALSE)
  1177.             {
  1178.                 movable = FALSE;
  1179.             }
  1180.  
  1181.             /*
  1182.              * If a cell on this edge has visible edges,
  1183.              * this edge is visible.
  1184.              */
  1185.             if (cell->no_edges == FALSE)
  1186.             {
  1187.                 visible = TRUE;
  1188.             }
  1189.  
  1190.             /*
  1191.              * A cell's size is the max of the edge_size
  1192.              * of all cells next to it.
  1193.              */
  1194.             if (cell->edge_size > size)
  1195.             {
  1196.                 size = cell->edge_size;
  1197.             }
  1198.  
  1199.             /*
  1200.              * A cell sets a color on an edge that doesn't
  1201.              * already have a color, or has a lower
  1202.              * priority, it wins.
  1203.              */
  1204.             if ((cell->border_color != NULL)&&
  1205.                 ((bg_color == NULL)||
  1206.                  (cell->color_priority > color_priority)))
  1207.             {
  1208.                 bg_color = XP_NEW(LO_Color);
  1209.                 if (bg_color != NULL)
  1210.                 {
  1211.                     bg_color->red = cell->border_color->red;
  1212.                     bg_color->green = cell->border_color->green;
  1213.                     bg_color->blue = cell->border_color->blue;
  1214.                     color_priority = cell->color_priority;
  1215.                 }
  1216.             }
  1217.  
  1218.             /*
  1219.              * If the cell is above the edge.
  1220.              */
  1221.             if (cell->y < edge->y)
  1222.             {
  1223.                 if ((cell->y > min)||(min == -1))
  1224.                 {
  1225.                     min = cell->y;
  1226.                 }
  1227.             }
  1228.             /*
  1229.              * Else the cell is below the edge.
  1230.              */
  1231.             else
  1232.             {
  1233.                 int32 bottom;
  1234.  
  1235.                 bottom = cell->y + cell->height - 1;
  1236.                 if ((bottom < max)||(max == -1))
  1237.                 {
  1238.                     max = bottom;
  1239.                 }
  1240.             }
  1241.         }
  1242.  
  1243.         /*
  1244.          * Make min and max be the inclusive bounds through
  1245.          * which edge->y can move.
  1246.          * We should never move an edge so that cell->y
  1247.          * becomes equal to edge->y or else in the future we
  1248.          * will thing that cell is on the wrong side of the
  1249.          * edge.  To prevent this, it min is set by a cell's
  1250.          * bounds, we increment it by one.
  1251.          */
  1252.         if (min == -1)
  1253.         {
  1254.             min = edge->y;
  1255.         }
  1256.         else
  1257.         {
  1258.             min = min + 1;
  1259.         }
  1260.         if (max == -1)
  1261.         {
  1262.             max = edge->y + edge->height - 1;
  1263.         }
  1264.         else
  1265.         {
  1266.             max = max - edge->height + 1;
  1267.         }
  1268.         /*
  1269.          * Sanity check bounds
  1270.          */
  1271.         if (max < min)
  1272.         {
  1273.             max = min;
  1274.         }
  1275.         edge->left_top_bound = min;
  1276.         edge->right_bottom_bound = max;
  1277.     }
  1278.     /*
  1279.      * Else for vertical edges.
  1280.      */
  1281.     else
  1282.     {
  1283.         for (i=0; i < edge->cell_cnt; i++)
  1284.         {
  1285.             lo_GridCellRec *cell;
  1286.  
  1287.             cell = edge->cell_array[i];
  1288.             if (cell == NULL)
  1289.             {
  1290.                 continue;
  1291.             }
  1292.  
  1293.             /*
  1294.              * If a cell on this edge is not resizable,
  1295.              * this edge is not movable.
  1296.              */
  1297.             if (cell->resizable == FALSE)
  1298.             {
  1299.                 movable = FALSE;
  1300.             }
  1301.  
  1302.             /*
  1303.              * If a cell on this edge has visible edges,
  1304.              * this edge is visible.
  1305.              */
  1306.             if (cell->no_edges == FALSE)
  1307.             {
  1308.                 visible = TRUE;
  1309.             }
  1310.  
  1311.             /*
  1312.              * A cells size is the max of the edge_size
  1313.              * of all cells next to it.
  1314.              */
  1315.             if (cell->edge_size > size)
  1316.             {
  1317.                 size = cell->edge_size;
  1318.             }
  1319.  
  1320.             /*
  1321.              * A cell sets a color on an edge that doesn't
  1322.              * already have a color, or has a lower
  1323.              * priority, it wins.
  1324.              */
  1325.             if ((cell->border_color != NULL)&&
  1326.                 ((bg_color == NULL)||
  1327.                  (cell->color_priority > color_priority)))
  1328.             {
  1329.                 bg_color = XP_NEW(LO_Color);
  1330.                 if (bg_color != NULL)
  1331.                 {
  1332.                     bg_color->red = cell->border_color->red;
  1333.                     bg_color->green = cell->border_color->green;
  1334.                     bg_color->blue = cell->border_color->blue;
  1335.                     color_priority = cell->color_priority;
  1336.                 }
  1337.             }
  1338.  
  1339.             /*
  1340.              * If the cell is left of the edge.
  1341.              */
  1342.             if (cell->x < edge->x)
  1343.             {
  1344.                 if ((cell->x > min)||(min == -1))
  1345.                 {
  1346.                     min = cell->x;
  1347.                 }
  1348.             }
  1349.             /*
  1350.              * Else the cell is right of the edge.
  1351.              */
  1352.             else
  1353.             {
  1354.                 int32 right;
  1355.  
  1356.                 right = cell->x + cell->width - 1;
  1357.                 if ((right < max)||(max == -1))
  1358.                 {
  1359.                     max = right;
  1360.                 }
  1361.             }
  1362.         }
  1363.  
  1364.         /*
  1365.          * Make min and max be the inclusive bounds through
  1366.          * which edge->x can move.
  1367.          * We should never move an edge so that cell->x
  1368.          * becomes equal to edge->x or else in the future we
  1369.          * will thing that cell is on the wrong side of the
  1370.          * edge.  To prevent this, it min is set by a cell's
  1371.          * bounds, we increment it by one.
  1372.          */
  1373.         if (min == -1)
  1374.         {
  1375.             min = edge->x;
  1376.         }
  1377.         else
  1378.         {
  1379.             min = min + 1;
  1380.         }
  1381.         if (max == -1)
  1382.         {
  1383.             max = edge->x + edge->width - 1;
  1384.         }
  1385.         else
  1386.         {
  1387.             max = max - edge->width + 1;
  1388.         }
  1389.         /*
  1390.          * Sanity check bounds
  1391.          */
  1392.         if (max < min)
  1393.         {
  1394.             max = min;
  1395.         }
  1396.         edge->left_top_bound = min;
  1397.         edge->right_bottom_bound = max;
  1398.     }
  1399.  
  1400.     edge->fe_edge->left_top_bound = edge->left_top_bound;
  1401.     edge->fe_edge->right_bottom_bound = edge->right_bottom_bound;
  1402.     edge->fe_edge->visible = visible;
  1403.     edge->fe_edge->movable = movable;
  1404.     edge->fe_edge->size = size;
  1405.     edge->fe_edge->bg_color = bg_color;
  1406. }
  1407.  
  1408.  
  1409. static void
  1410. lo_set_all_edge_bounds(MWContext *context, lo_DocState *state, lo_GridRec *grid)
  1411. {
  1412.     lo_GridEdge *edge_list;
  1413.  
  1414.     if ((grid == NULL)||(grid->edge_list == NULL))
  1415.     {
  1416.         return;
  1417.     }
  1418.  
  1419.     edge_list = grid->edge_list;
  1420.     while (edge_list != NULL)
  1421.     {
  1422.         lo_set_edge_bounds(edge_list);
  1423.  
  1424.         /*
  1425.          * Insert this edge into the float list.
  1426.          */
  1427.         edge_list->fe_edge->next = state->float_list;
  1428.         state->float_list = (LO_Element *)edge_list->fe_edge;
  1429.  
  1430.         lo_DisplayEdge(context, edge_list->fe_edge);
  1431.         edge_list = edge_list->next;
  1432.     }
  1433. }
  1434.  
  1435.  
  1436. /*
  1437.  * Use the cell's history index to reload the document
  1438.  * it should now be currently displaying.
  1439.  * (Note, history index starts at 1, and array starts at 0).
  1440.  */
  1441. static void
  1442. lo_reload_cell_from_history(lo_GridCellRec *cell_ptr,
  1443.     NET_ReloadMethod force_reload)
  1444. {
  1445.     void *hist;
  1446.  
  1447.     hist = cell_ptr->hist_array[cell_ptr->hist_indx - 1].hist;
  1448.  
  1449.     /*
  1450.      * If we have a history entry, and a context to load
  1451.      * it into, have the FE load it now.
  1452.      */
  1453.     if ((hist != NULL)&&(cell_ptr->context != NULL))
  1454.     {
  1455.         FE_LoadGridCellFromHistory(cell_ptr->context, hist,
  1456.             force_reload);
  1457.     }
  1458. }
  1459.  
  1460.  
  1461. static lo_GridCellRec *reconnecting_cell = NULL;
  1462.  
  1463.  
  1464. PRIVATE
  1465. void
  1466. lo_MakeGrid(MWContext *context, lo_DocState *state, lo_GridRec *grid)
  1467. {
  1468.     int32 x, y;
  1469.     int32 edge_size;
  1470.     int32 width, height;
  1471.     lo_GridCellRec *cell_ptr;
  1472.     lo_GridEdge *edges[4];    /* top, bottom, left, right */
  1473.  
  1474.     width = state->win_width;
  1475.     height = state->win_height;
  1476.     FE_GetFullWindowSize(context, &width, &height);
  1477.  
  1478.     if (width <= 0)
  1479.         width = 1;
  1480.  
  1481.     if (height <= 0)
  1482.         height = 1;
  1483.  
  1484.     grid->main_width = width;
  1485.     grid->main_height = height;
  1486.  
  1487. #ifdef NO_FRAMESET_BORDER
  1488.     edge_size = 1;
  1489.     FE_GetEdgeMinSize(context, &edge_size);
  1490.     if (edge_size < 1)
  1491.     {
  1492.         edge_size = 1;
  1493.     }
  1494. #else
  1495.     if (grid->edge_size < 0)
  1496.     {
  1497.         edge_size = 1;
  1498.         FE_GetEdgeMinSize(context, &edge_size
  1499. #if defined(XP_WIN) || defined(XP_OS2)
  1500.         /* need this information here under windows */
  1501.         , grid->no_edges
  1502. #endif
  1503.             );
  1504.     }
  1505.     else
  1506.     {
  1507.         edge_size = grid->edge_size;
  1508.     }
  1509.     if (edge_size < 0)
  1510.     {
  1511.         edge_size = 0;
  1512.     }
  1513. #endif /* NO_FRAMESET_BORDER */
  1514.     grid->grid_cell_border = (edge_size + 1) / 2;
  1515.     grid->grid_cell_min_dim = (2 * grid->grid_cell_border) + 1;
  1516.  
  1517.     x = 0;
  1518.     y = 0;
  1519.  
  1520.     /*
  1521.      * Make sure all the row percentages total up
  1522.      * to 100%
  1523.      */
  1524.     lo_adjust_percents(grid->rows, grid->row_percents, height);
  1525.  
  1526.     /*
  1527.      * Make sure all the col percentages total up
  1528.      * to 100%
  1529.      */
  1530.     lo_adjust_percents(grid->cols, grid->col_percents, width);
  1531.  
  1532.     edges[TOP_EDGE] = NULL;
  1533.     edges[BOTTOM_EDGE] = NULL;
  1534.     edges[LEFT_EDGE] = NULL;
  1535.     edges[RIGHT_EDGE] = NULL;
  1536.     lo_LayoutGridCells(context, state, x, y, width, height, grid, edges);
  1537.     lo_set_all_edge_bounds(context, state, grid);
  1538.  
  1539.     /*
  1540.      * If there is an old_grid in the top_state, that means we restored
  1541.      * from history with the sizes changed.  Go through that old_grid
  1542.      * now, and put those urls back into the cells, then free the old_grid.
  1543.      */
  1544.     if (state->top_state->old_grid != NULL)
  1545.     {
  1546.         lo_GridRec *old_grid;
  1547.         lo_GridCellRec *old_cell_ptr;
  1548.         lo_SavedGridData tmp_saved_grid;
  1549.  
  1550.         old_grid = state->top_state->old_grid;
  1551.         grid->current_hist = old_grid->current_hist;
  1552.         grid->max_hist = old_grid->max_hist;
  1553.         grid->hist_size = old_grid->hist_size;
  1554.         old_cell_ptr = old_grid->cell_list;
  1555.         cell_ptr = grid->cell_list;
  1556.         while ((cell_ptr != NULL)&&(old_cell_ptr != NULL))
  1557.         {
  1558.             /*
  1559.              * Free any initial URL loaded, and move the old
  1560.              * url in place.  Make sure to clear the old URL
  1561.              * so we don't accidentally free it later.
  1562.              */
  1563.             if (cell_ptr->url != NULL)
  1564.             {
  1565.                 XP_FREE(cell_ptr->url);
  1566.                 cell_ptr->url = NULL;
  1567.             }
  1568.             cell_ptr->url = old_cell_ptr->url;
  1569.             old_cell_ptr->url = NULL;
  1570.  
  1571.             /*
  1572.              * Also copy over the old history.
  1573.              */
  1574.             cell_ptr->hist_indx = old_cell_ptr->hist_indx;
  1575.             cell_ptr->hist_array = old_cell_ptr->hist_array;
  1576.             old_cell_ptr->hist_array = NULL;
  1577.             cell_ptr->hist_list = old_cell_ptr->hist_list;
  1578.             old_cell_ptr->hist_list = NULL;
  1579.  
  1580.             cell_ptr = cell_ptr->next;
  1581.             old_cell_ptr = old_cell_ptr->next;
  1582.         }
  1583.  
  1584.         /*
  1585.          * Free up the old saved grid.
  1586.          */
  1587.         tmp_saved_grid.main_width = 0;
  1588.         tmp_saved_grid.main_height = 0;
  1589.         tmp_saved_grid.the_grid = state->top_state->old_grid;
  1590.         state->top_state->old_grid = NULL;
  1591.         lo_FreeDocumentGridData(context, &tmp_saved_grid);
  1592.     }
  1593.  
  1594.     /*
  1595.      * Go through all the cells creating them where specified.
  1596.      */
  1597.     cell_ptr = grid->cell_list;
  1598.     while (cell_ptr != NULL)
  1599.     {
  1600.         /*
  1601.          * Have the front end create the grid cell.
  1602.          */
  1603.         if (cell_ptr->url != NULL)
  1604.         {
  1605.             void *history;
  1606.             void *hist_list;
  1607.  
  1608.             history = NULL;
  1609.             hist_list = NULL;
  1610.             if (cell_ptr->hist_list != NULL)
  1611.             {
  1612.                 hist_list = cell_ptr->hist_list;
  1613.                 cell_ptr->hist_indx = grid->current_hist;
  1614.                 history = cell_ptr->hist_array[cell_ptr->hist_indx - 1].hist;
  1615.                 if (history == NULL)
  1616.                 {
  1617.                     hist_list = NULL;
  1618.                 }
  1619.             }
  1620.             reconnecting_cell = cell_ptr;
  1621.             cell_ptr->context = FE_MakeGridWindow (context,
  1622.                 hist_list,
  1623.                 history,
  1624.                 cell_ptr->x, cell_ptr->y,
  1625.                 cell_ptr->width, cell_ptr->height,
  1626.                 (char *)cell_ptr->url, (char *)cell_ptr->name,
  1627.                 cell_ptr->scrolling,
  1628.                 FORCE_RELOAD_FLAG(state->top_state),
  1629.                 cell_ptr->no_edges);
  1630.             reconnecting_cell = NULL;
  1631.             if (cell_ptr->context)
  1632.             {
  1633.                 XP_ASSERT(cell_ptr->context->forceJSEnabled ==
  1634.                 context->forceJSEnabled);
  1635.                 if (history)
  1636.                 {
  1637.                 cell_ptr->context->hist.cur_doc_ptr = history;
  1638.                 }
  1639.             }
  1640.         }
  1641.         else
  1642.         {
  1643.             cell_ptr->context = NULL;
  1644.         }
  1645.         cell_ptr = cell_ptr->next;
  1646.     }
  1647. }
  1648.  
  1649.  
  1650.  
  1651. void
  1652. lo_BeginGrid(MWContext *context, lo_DocState *state, PA_Tag *tag)
  1653. {
  1654.     PA_Block buff;
  1655.     char *str;
  1656.     int32 rows, cols;
  1657.     lo_GridPercent *row_percents;
  1658.     lo_GridPercent *col_percents;
  1659.     lo_GridRec *grid;
  1660.  
  1661.     rows = 1;
  1662.     cols = 1;
  1663.  
  1664.     grid = XP_NEW(lo_GridRec);
  1665.     if (grid == NULL)
  1666.     {
  1667.         state->top_state->out_of_memory = TRUE;
  1668.         return;
  1669.     }
  1670.  
  1671.     grid->no_edges = FALSE;
  1672.     grid->edge_size = -1;
  1673.     grid->color_priority = 0;
  1674.     grid->border_color = NULL;
  1675.     grid->edge_list = NULL;
  1676.     grid->cell_list = NULL;
  1677.     grid->cell_list_last = NULL;
  1678.     grid->subgrid = NULL;
  1679.  
  1680.     buff = lo_FetchParamValue(context, tag, PARAM_FRAMEBORDER);
  1681.     if (buff != NULL)
  1682.     {
  1683.         Bool no_edges;
  1684.  
  1685.         no_edges = FALSE;
  1686.         PA_LOCK(str, char *, buff);
  1687.         if (pa_TagEqual("no", str))
  1688.         {
  1689.             no_edges = TRUE;
  1690.         }
  1691.         else if (pa_TagEqual("0", str))
  1692.         {
  1693.             no_edges = TRUE;
  1694.         }
  1695.         PA_UNLOCK(buff);
  1696.         PA_FREE(buff);
  1697.         grid->no_edges = no_edges;
  1698.     }
  1699.  
  1700.     buff = lo_FetchParamValue(context, tag, PARAM_BORDER);
  1701.     if (buff != NULL)
  1702.     {
  1703.         int32 border;
  1704.  
  1705.         PA_LOCK(str, char *, buff);
  1706.         border = XP_ATOI(str);
  1707.         PA_UNLOCK(buff);
  1708.         PA_FREE(buff);
  1709.         if (border < 0)
  1710.         {
  1711.             border = 0;
  1712.         }
  1713.         if (border > 100)
  1714.         {
  1715.             border = 100;
  1716.         }
  1717.         grid->edge_size = (int16)border;
  1718.     }
  1719.  
  1720.     /*
  1721.      * A borderwidth of zero automatically turns off edges.
  1722.      */
  1723.     if (grid->edge_size == 0)
  1724.     {
  1725.         grid->no_edges = TRUE;
  1726.     }
  1727.  
  1728.     /*
  1729.      * Check for a border color attribute
  1730.      */
  1731.     buff = lo_FetchParamValue(context, tag, PARAM_BORDERCOLOR);
  1732.     if (buff != NULL)
  1733.     {
  1734.         uint8 red, green, blue;
  1735.  
  1736.         PA_LOCK(str, char *, buff);
  1737.         LO_ParseRGB(str, &red, &green, &blue);
  1738.         PA_UNLOCK(buff);
  1739.         PA_FREE(buff);
  1740.         grid->border_color = XP_NEW(LO_Color);
  1741.         if (grid->border_color != NULL)
  1742.         {
  1743.             grid->border_color->red = red;
  1744.             grid->border_color->green = green;
  1745.             grid->border_color->blue = blue;
  1746.             grid->color_priority = 1;
  1747.         }
  1748.     }
  1749.  
  1750.     buff = lo_FetchParamValue(context, tag, PARAM_ROWS);
  1751.     if (buff != NULL)
  1752.     {
  1753.         int32 len;
  1754.  
  1755.         PA_LOCK(str, char *, buff);
  1756.         len = lo_StripTextWhitespace(str, XP_STRLEN(str));
  1757.         row_percents = lo_parse_percent_list(context, str, &rows);
  1758.         if ((row_percents == NULL)&&(rows >= 1))
  1759.         {
  1760.             PA_UNLOCK(buff);
  1761.             PA_FREE(buff);
  1762.             XP_DELETE(grid);
  1763.             state->top_state->out_of_memory = TRUE;
  1764.             return;
  1765.         }
  1766.         PA_UNLOCK(buff);
  1767.         PA_FREE(buff);
  1768.         if (rows <= 0)
  1769.         {
  1770.             row_percents = (lo_GridPercent *)
  1771.                  XP_ALLOC(sizeof(lo_GridPercent));
  1772.             if (row_percents == NULL)
  1773.             {
  1774.                 XP_DELETE(grid);
  1775.                 state->top_state->out_of_memory = TRUE;
  1776.                 return;
  1777.             }
  1778.             row_percents[0].type = LO_PERCENT_NORMAL;
  1779.             row_percents[0].original_value = 100;
  1780.             rows = 1;
  1781.         }
  1782.     }
  1783.     else
  1784.     {
  1785.         row_percents = (lo_GridPercent *)
  1786.             XP_ALLOC(sizeof(lo_GridPercent));
  1787.         if (row_percents == NULL)
  1788.         {
  1789.             XP_DELETE(grid);
  1790.             state->top_state->out_of_memory = TRUE;
  1791.             return;
  1792.         }
  1793.         row_percents[0].type = LO_PERCENT_NORMAL;
  1794.         row_percents[0].original_value = 100;
  1795.         rows = 1;
  1796.     }
  1797.  
  1798.     buff = lo_FetchParamValue(context, tag, PARAM_COLS);
  1799.     if (buff != NULL)
  1800.     {
  1801.         int32 len;
  1802.  
  1803.         PA_LOCK(str, char *, buff);
  1804.         len = lo_StripTextWhitespace(str, XP_STRLEN(str));
  1805.         col_percents = lo_parse_percent_list(context, str, &cols);
  1806.         if ((col_percents == NULL)&&(cols >= 1))
  1807.         {
  1808.             PA_UNLOCK(buff);
  1809.             PA_FREE(buff);
  1810.             if (row_percents != NULL)
  1811.             {
  1812.                 XP_FREE(row_percents);
  1813.             }
  1814.             XP_DELETE(grid);
  1815.             state->top_state->out_of_memory = TRUE;
  1816.             return;
  1817.         }
  1818.         PA_UNLOCK(buff);
  1819.         PA_FREE(buff);
  1820.         if (cols <= 0)
  1821.         {
  1822.             col_percents = (lo_GridPercent *)
  1823.                 XP_ALLOC(sizeof(lo_GridPercent));
  1824.             if (col_percents == NULL)
  1825.             {
  1826.                 if (row_percents != NULL)
  1827.                 {
  1828.                     XP_FREE(row_percents);
  1829.                 }
  1830.                 XP_DELETE(grid);
  1831.                 state->top_state->out_of_memory = TRUE;
  1832.                 return;
  1833.             }
  1834.             col_percents[0].type = LO_PERCENT_NORMAL;
  1835.             col_percents[0].original_value = 100;
  1836.             cols = 1;
  1837.         }
  1838.     }
  1839.     else
  1840.     {
  1841.         col_percents = (lo_GridPercent *)
  1842.             XP_ALLOC(sizeof(lo_GridPercent));
  1843.         if (col_percents == NULL)
  1844.         {
  1845.             if (row_percents != NULL)
  1846.             {
  1847.                 XP_FREE(row_percents);
  1848.             }
  1849.             XP_DELETE(grid);
  1850.             state->top_state->out_of_memory = TRUE;
  1851.             return;
  1852.         }
  1853.         col_percents[0].type = LO_PERCENT_NORMAL;
  1854.         col_percents[0].original_value = 100;
  1855.         cols = 1;
  1856.     }
  1857.  
  1858.     if ((rows == 1)&&(cols == 1))
  1859.     {
  1860.         if (row_percents != NULL)
  1861.         {
  1862.             XP_FREE(row_percents);
  1863.         }
  1864.         if (col_percents != NULL)
  1865.         {
  1866.             XP_FREE(col_percents);
  1867.         }
  1868.         XP_DELETE(grid);
  1869.         return;
  1870.     }
  1871.  
  1872. #ifdef MOCHA
  1873.     (void)lo_ProcessContextEventHandlers(context, state, tag);
  1874. #endif
  1875.  
  1876.     grid->rows = rows;
  1877.     grid->row_percents = row_percents;
  1878.     grid->cols = cols;
  1879.     grid->col_percents = col_percents;
  1880.     grid->cell_count = 0;
  1881.  
  1882.     grid->current_hist = 0;
  1883.     grid->max_hist = 0;
  1884.     grid->hist_size = 10;
  1885.  
  1886.     grid->main_width = state->win_width;
  1887.     grid->main_height = state->win_height;
  1888.     grid->current_cell_margin_width = 0;
  1889.     grid->current_cell_margin_height = 0;
  1890.  
  1891.     state->current_grid = grid;
  1892. }
  1893.  
  1894.  
  1895. void
  1896. lo_EndGrid(MWContext *context, lo_DocState *state, lo_GridRec *grid)
  1897. {
  1898.     int32 cell_cnt;
  1899.  
  1900.     if (grid == NULL)
  1901.     {
  1902.         return;
  1903.     }
  1904.  
  1905.     cell_cnt = grid->rows * grid->cols;
  1906.  
  1907.     state->top_state->the_grid = grid;
  1908.  
  1909.     lo_MakeGrid(context, state, grid);
  1910. }
  1911.  
  1912.  
  1913. static Bool
  1914. lo_is_grid_recursion(MWContext *context, lo_DocState *state, char *url)
  1915. {
  1916.     MWContext *cptr;
  1917.  
  1918.     /*
  1919.      * If this cell has its parent's URL, it is recursion.
  1920.      */
  1921.     if ((state->top_state->url != NULL)&&
  1922.         (XP_STRCMP(url, state->top_state->url) == 0))
  1923.     {
  1924.         return(TRUE);
  1925.     }
  1926.  
  1927.     cptr = context->grid_parent;
  1928.     while (cptr != NULL)
  1929.     {
  1930.         int32 doc_id;
  1931.         lo_TopState *top_state;
  1932.         char *anc_url;
  1933.  
  1934.         /*
  1935.          * Get the unique document ID, and retreive this
  1936.          * documents layout state.
  1937.          */
  1938.         doc_id = XP_DOCID(cptr);
  1939.         top_state = lo_FetchTopState(doc_id);
  1940.         /*
  1941.          * We have to get the ancenstor URL from the state because
  1942.          * context->url doesn't seem to be reliable.
  1943.          */
  1944.         if (top_state == NULL)
  1945.         {
  1946.             anc_url = NULL;
  1947.         }
  1948.         else
  1949.         {
  1950.             anc_url = top_state->url;
  1951.         }
  1952.  
  1953.         /*
  1954.          * if this cell has the same URL as any of its ancestors
  1955.          * this is recursion.
  1956.          */
  1957.         if ((anc_url != NULL)&&(XP_STRCMP(url, anc_url) == 0))
  1958.         {
  1959.             return(TRUE);
  1960.         }
  1961.         cptr = cptr->grid_parent;
  1962.     }
  1963.     return(FALSE);
  1964. }
  1965.  
  1966.  
  1967. void
  1968. lo_BeginGridCell(MWContext *context, lo_DocState *state, PA_Tag *tag)
  1969. {
  1970.     PA_Block buff;
  1971.     char *str;
  1972.     lo_GridRec *grid;
  1973.     int32 col_cnt, row_cnt;
  1974.     lo_GridCellRec *cell;
  1975.  
  1976.     /*
  1977.      * Get the current grid, if there is none, error out.
  1978.      */
  1979.     grid = state->current_grid;
  1980.     if (grid == NULL)
  1981.     {
  1982.         return;
  1983.     }
  1984.  
  1985.     /*
  1986.      * If we have subgrids, the deepest one is at the TOP of the
  1987.      * subgrid stack.
  1988.      */
  1989.     if (grid->subgrid != NULL)
  1990.     {
  1991.         grid = grid->subgrid;
  1992.     }
  1993.  
  1994.     /*
  1995.      * If this grid has its full complement of cells, ignore
  1996.      * this cell.
  1997.      */
  1998.     if (grid->cell_count >= (grid->rows * grid->cols))
  1999.     {
  2000.         return;
  2001.     }
  2002.  
  2003.     cell = XP_NEW(lo_GridCellRec);
  2004.     if (cell == NULL)
  2005.     {
  2006.         state->top_state->out_of_memory = TRUE;
  2007.         return;
  2008.     }
  2009.  
  2010.     row_cnt = grid->cell_count / grid->cols;
  2011.     col_cnt = grid->cell_count - (row_cnt *  grid->cols);
  2012.  
  2013.     cell->x = 0;
  2014.     cell->y = 0;
  2015.     cell->width = 0;
  2016.     cell->height = 0;
  2017.     cell->margin_width = 0;
  2018.     cell->margin_height = 0;
  2019.     cell->width_percent = 0;
  2020.     cell->height_percent = 0;
  2021.     cell->url = NULL;
  2022.     cell->name = NULL;
  2023.     cell->context = NULL;
  2024.     cell->hist_list = NULL;
  2025.     cell->hist_array = (lo_GridHistory *)XP_ALLOC(grid->hist_size *
  2026.                 sizeof(lo_GridHistory));
  2027.     if (cell->hist_array == NULL)
  2028.     {
  2029.         XP_DELETE(cell);
  2030.         state->top_state->out_of_memory = TRUE;
  2031.         return;
  2032.     }
  2033.     XP_MEMSET(cell->hist_array, 0, grid->hist_size *
  2034.                 sizeof(lo_GridHistory));
  2035.     cell->hist_indx = 0;
  2036.     cell->next = NULL;
  2037.     cell->subgrid = NULL;
  2038.     cell->side_edges[TOP_EDGE] = NULL;
  2039.     cell->side_edges[BOTTOM_EDGE] = NULL;
  2040.     cell->side_edges[LEFT_EDGE] = NULL;
  2041.     cell->side_edges[RIGHT_EDGE] = NULL;
  2042.     cell->scrolling = LO_SCROLL_AUTO;
  2043.     cell->no_edges = grid->no_edges;
  2044.     cell->resizable = TRUE;
  2045.     cell->edge_size = -1;
  2046.     cell->color_priority = 0;
  2047.     cell->border_color = NULL;
  2048.  
  2049.     buff = lo_FetchParamValue(context, tag, PARAM_FRAMEBORDER);
  2050.     if (buff != NULL)
  2051.     {
  2052.         Bool no_edges;
  2053.  
  2054.         no_edges = FALSE;
  2055.         PA_LOCK(str, char *, buff);
  2056.         if (pa_TagEqual("no", str))
  2057.         {
  2058.             no_edges = TRUE;
  2059.         }
  2060.         else if (pa_TagEqual("0", str))
  2061.         {
  2062.             no_edges = TRUE;
  2063.         }
  2064.         PA_UNLOCK(buff);
  2065.         PA_FREE(buff);
  2066.         cell->no_edges = no_edges;
  2067.     }
  2068.  
  2069.     buff = lo_FetchParamValue(context, tag, PARAM_BORDER);
  2070.     if (buff != NULL)
  2071.     {
  2072.         int32 border;
  2073.  
  2074.         PA_LOCK(str, char *, buff);
  2075.         border = XP_ATOI(str);
  2076.         PA_UNLOCK(buff);
  2077.         PA_FREE(buff);
  2078.         if (border < 0)
  2079.         {
  2080.             border = 0;
  2081.         }
  2082.         if (border > 100)
  2083.         {
  2084.             border = 100;
  2085.         }
  2086.         cell->edge_size = (int16)border;
  2087.     }
  2088.  
  2089.     /*
  2090.      * A borderwidth of zero automatically turns off edges.
  2091.      */
  2092.     if (cell->edge_size == 0)
  2093.     {
  2094.         cell->no_edges = TRUE;
  2095.     }
  2096.  
  2097.     /*
  2098.      * Check for a border color attribute
  2099.      */
  2100.     buff = lo_FetchParamValue(context, tag, PARAM_BORDERCOLOR);
  2101.     if (buff != NULL)
  2102.     {
  2103.         uint8 red, green, blue;
  2104.  
  2105.         PA_LOCK(str, char *, buff);
  2106.         LO_ParseRGB(str, &red, &green, &blue);
  2107.         PA_UNLOCK(buff);
  2108.         PA_FREE(buff);
  2109.         cell->border_color = XP_NEW(LO_Color);
  2110.         if (cell->border_color != NULL)
  2111.         {
  2112.             cell->border_color->red = red;
  2113.             cell->border_color->green = green;
  2114.             cell->border_color->blue = blue;
  2115.             cell->color_priority = 3;
  2116.         }
  2117.     }
  2118.     /*
  2119.      * Else we might inherit a border color from parent.
  2120.      */
  2121.     else if (grid->border_color != NULL)
  2122.     {
  2123.         cell->border_color = XP_NEW(LO_Color);
  2124.         if (cell->border_color != NULL)
  2125.         {
  2126.             cell->border_color->red = grid->border_color->red;
  2127.             cell->border_color->green = grid->border_color->green;
  2128.             cell->border_color->blue = grid->border_color->blue;
  2129.             cell->color_priority = grid->color_priority;
  2130.         }
  2131.     }
  2132.  
  2133.     buff = lo_FetchParamValue(context, tag, PARAM_NORESIZE);
  2134.     if (buff != NULL)
  2135.     {
  2136.         PA_FREE(buff);
  2137.         cell->resizable = FALSE;
  2138.     }
  2139.  
  2140.     buff = lo_FetchParamValue(context, tag, PARAM_SCROLLING);
  2141.     if (buff != NULL)
  2142.     {
  2143.         int8 scrolling;
  2144.  
  2145.         scrolling = LO_SCROLL_AUTO;
  2146.         PA_LOCK(str, char *, buff);
  2147.         if (pa_TagEqual("yes", str))
  2148.         {
  2149.             scrolling = LO_SCROLL_YES;
  2150.         }
  2151.         else if (pa_TagEqual("scroll", str))
  2152.         {
  2153.             scrolling = LO_SCROLL_YES;
  2154.         }
  2155.         else if (pa_TagEqual("no", str))
  2156.         {
  2157.             scrolling = LO_SCROLL_NO;
  2158.         }
  2159.         else if (pa_TagEqual("noscroll", str))
  2160.         {
  2161.             scrolling = LO_SCROLL_NO;
  2162.         }
  2163.         else if (pa_TagEqual("on", str))
  2164.         {
  2165.             scrolling = LO_SCROLL_YES;
  2166.         }
  2167.         else if (pa_TagEqual("off", str))
  2168.         {
  2169.             scrolling = LO_SCROLL_NO;
  2170.         }
  2171.         PA_UNLOCK(buff);
  2172.         PA_FREE(buff);
  2173.         cell->scrolling = scrolling;
  2174.     }
  2175.  
  2176.     buff = lo_FetchParamValue(context, tag, PARAM_SRC);
  2177.     if (buff != NULL)
  2178.     {
  2179.         char *new_str;
  2180.  
  2181.         PA_LOCK(str, char *, buff);
  2182.         if (str != NULL)
  2183.         {
  2184.             int32 len;
  2185.  
  2186.             len = lo_StripTextWhitespace(str, XP_STRLEN(str));
  2187.         }
  2188.         new_str = NET_MakeAbsoluteURL(state->top_state->base_url, str);
  2189.  
  2190.         PA_UNLOCK(buff);
  2191.         PA_FREE(buff);
  2192.         cell->url = new_str;
  2193.         /*
  2194.          * Don't allow cells to have the same URL as their parent
  2195.          * or any of their ancestors as that would cause infinite
  2196.          * recursion.
  2197.          */
  2198.         if (lo_is_grid_recursion(context, state, cell->url) != FALSE)
  2199.         {
  2200.             XP_FREE(new_str);
  2201.             cell->url = NULL;
  2202.         }
  2203.     }
  2204.     else
  2205.     {
  2206.         cell->url = NULL;
  2207.     }
  2208.  
  2209.     buff = lo_FetchParamValue(context, tag, PARAM_NAME);
  2210.     if (buff != NULL)
  2211.     {
  2212.         char *name;
  2213.  
  2214.         PA_LOCK(str, char *, buff);
  2215.         if (str != NULL)
  2216.         {
  2217.             int32 len;
  2218.  
  2219.             len = lo_StripTextWhitespace(str, XP_STRLEN(str));
  2220.         }
  2221.         name = (char *)XP_ALLOC(XP_STRLEN(str) + 1);
  2222.         if (name == NULL)
  2223.         {
  2224.             cell->name = NULL;
  2225.         }
  2226.         else
  2227.         {
  2228.             XP_STRCPY(name, str);
  2229.             cell->name = name;
  2230.         }
  2231.         PA_UNLOCK(buff);
  2232.         PA_FREE(buff);
  2233.     }
  2234.     else
  2235.     {
  2236.         cell->name = NULL;
  2237.     }
  2238.  
  2239.     /*
  2240.      * Get the margin width.
  2241.      */
  2242.     buff = lo_FetchParamValue(context, tag, PARAM_MARGINWIDTH);
  2243.     if (buff != NULL)
  2244.     {
  2245.         int32 val;
  2246.  
  2247.         PA_LOCK(str, char *, buff);
  2248.         val = XP_ATOI(str);
  2249.         if (val < 1)
  2250.         {
  2251.             val = 1;
  2252.         }
  2253.         cell->margin_width = val;
  2254.         PA_UNLOCK(buff);
  2255.         PA_FREE(buff);
  2256.     }
  2257.     cell->margin_width = FEUNITS_X(cell->margin_width, context);
  2258.  
  2259.     /*
  2260.      * Get the margin height.
  2261.      */
  2262.     buff = lo_FetchParamValue(context, tag, PARAM_MARGINHEIGHT);
  2263.     if (buff != NULL)
  2264.     {
  2265.         int32 val;
  2266.  
  2267.         PA_LOCK(str, char *, buff);
  2268.         val = XP_ATOI(str);
  2269.         if (val < 1)
  2270.         {
  2271.             val = 1;
  2272.         }
  2273.         cell->margin_height = val;
  2274.         PA_UNLOCK(buff);
  2275.         PA_FREE(buff);
  2276.     }
  2277.     cell->margin_height = FEUNITS_Y(cell->margin_height, context);
  2278.  
  2279.     grid->cell_count++;
  2280.  
  2281.     if (grid->cell_list_last == NULL)
  2282.     {
  2283.         grid->cell_list = cell;
  2284.         grid->cell_list_last = cell;
  2285.     }
  2286.     else
  2287.     {
  2288.         grid->cell_list_last->next = cell;
  2289.         grid->cell_list_last = cell;
  2290.     }
  2291. }
  2292.  
  2293.  
  2294. void
  2295. lo_BeginSubgrid(MWContext *context, lo_DocState *state, PA_Tag *tag)
  2296. {
  2297.     PA_Block buff;
  2298.     char *str;
  2299.     lo_GridRec *grid;
  2300.     int32 col_cnt, row_cnt;
  2301.     lo_GridRec *subgrid;
  2302.     int32 rows, cols;
  2303.     lo_GridPercent *row_percents;
  2304.     lo_GridPercent *col_percents;
  2305.     lo_GridCellRec *cell;
  2306.  
  2307.     /*
  2308.      * Get the current grid, if there is none, error out.
  2309.      */
  2310.     grid = state->current_grid;
  2311.     if (grid == NULL)
  2312.     {
  2313.         return;
  2314.     }
  2315.  
  2316.     /*
  2317.      * If we have subgrids, the deepest one is at the TOP of the
  2318.      * subgrid stack.
  2319.      */
  2320.     if (grid->subgrid != NULL)
  2321.     {
  2322.         grid = grid->subgrid;
  2323.     }
  2324.  
  2325.     /*
  2326.      * If this grid has its full complement of cells, ignore
  2327.      * this subgrid cell.
  2328.      */
  2329.     if (grid->cell_count >= (grid->rows * grid->cols))
  2330.     {
  2331.         return;
  2332.     }
  2333.  
  2334.  
  2335.     /*
  2336.      ********************************
  2337.      * Create and parse the subgrid *
  2338.      ********************************
  2339.      */
  2340.     rows = 1;
  2341.     cols = 1;
  2342.  
  2343.     subgrid = XP_NEW(lo_GridRec);
  2344.     if (subgrid == NULL)
  2345.     {
  2346.         state->top_state->out_of_memory = TRUE;
  2347.         return;
  2348.     }
  2349.  
  2350.     subgrid->no_edges = grid->no_edges;
  2351.     subgrid->edge_size = grid->edge_size;
  2352.     subgrid->color_priority = 0;
  2353.     subgrid->border_color = NULL;
  2354.     subgrid->edge_list = NULL;
  2355.     subgrid->cell_list = NULL;
  2356.     subgrid->cell_list_last = NULL;
  2357.     subgrid->subgrid = NULL;
  2358.  
  2359.     /*
  2360.      * Check for a border color attribute
  2361.      */
  2362.     buff = lo_FetchParamValue(context, tag, PARAM_BORDERCOLOR);
  2363.     if (buff != NULL)
  2364.     {
  2365.         uint8 red, green, blue;
  2366.  
  2367.         PA_LOCK(str, char *, buff);
  2368.         LO_ParseRGB(str, &red, &green, &blue);
  2369.         PA_UNLOCK(buff);
  2370.         PA_FREE(buff);
  2371.         subgrid->border_color = XP_NEW(LO_Color);
  2372.         if (subgrid->border_color != NULL)
  2373.         {
  2374.             subgrid->border_color->red = red;
  2375.             subgrid->border_color->green = green;
  2376.             subgrid->border_color->blue = blue;
  2377.             subgrid->color_priority = 2;
  2378.         }
  2379.     }
  2380.     /*
  2381.      * Else we might inherit a border color from parent.
  2382.      */
  2383.     else if (grid->border_color != NULL)
  2384.     {
  2385.         subgrid->border_color = XP_NEW(LO_Color);
  2386.         if (subgrid->border_color != NULL)
  2387.         {
  2388.             subgrid->border_color->red = grid->border_color->red;
  2389.             subgrid->border_color->green = grid->border_color->green;
  2390.             subgrid->border_color->blue = grid->border_color->blue;
  2391.             subgrid->color_priority = grid->color_priority;
  2392.         }
  2393.     }
  2394.  
  2395.     buff = lo_FetchParamValue(context, tag, PARAM_FRAMEBORDER);
  2396.     if (buff != NULL)
  2397.     {
  2398.         Bool no_edges;
  2399.  
  2400.         no_edges = FALSE;
  2401.         PA_LOCK(str, char *, buff);
  2402.         if (pa_TagEqual("no", str))
  2403.         {
  2404.             no_edges = TRUE;
  2405.         }
  2406.         else if (pa_TagEqual("0", str))
  2407.         {
  2408.             no_edges = TRUE;
  2409.         }
  2410.         PA_UNLOCK(buff);
  2411.         PA_FREE(buff);
  2412.         subgrid->no_edges = no_edges;
  2413.     }
  2414.  
  2415.     buff = lo_FetchParamValue(context, tag, PARAM_ROWS);
  2416.     if (buff != NULL)
  2417.     {
  2418.         int32 len;
  2419.  
  2420.         PA_LOCK(str, char *, buff);
  2421.         len = lo_StripTextWhitespace(str, XP_STRLEN(str));
  2422.         row_percents = lo_parse_percent_list(context, str, &rows);
  2423.         if ((row_percents == NULL)&&(rows >= 1))
  2424.         {
  2425.             PA_UNLOCK(buff);
  2426.             PA_FREE(buff);
  2427.             XP_DELETE(subgrid);
  2428.             state->top_state->out_of_memory = TRUE;
  2429.             return;
  2430.         }
  2431.         PA_UNLOCK(buff);
  2432.         PA_FREE(buff);
  2433.         if (rows <= 0)
  2434.         {
  2435.             row_percents = (lo_GridPercent *)
  2436.                 XP_ALLOC(sizeof(lo_GridPercent));
  2437.             if (row_percents == NULL)
  2438.             {
  2439.                 XP_DELETE(subgrid);
  2440.                 state->top_state->out_of_memory = TRUE;
  2441.                 return;
  2442.             }
  2443.             row_percents[0].type = LO_PERCENT_NORMAL;
  2444.             row_percents[0].original_value = 100;
  2445.             rows = 1;
  2446.         }
  2447.     }
  2448.     else
  2449.     {
  2450.         row_percents = (lo_GridPercent *)
  2451.             XP_ALLOC(sizeof(lo_GridPercent));
  2452.         if (row_percents == NULL)
  2453.         {
  2454.             XP_DELETE(subgrid);
  2455.             state->top_state->out_of_memory = TRUE;
  2456.             return;
  2457.         }
  2458.         row_percents[0].type = LO_PERCENT_NORMAL;
  2459.         row_percents[0].original_value = 100;
  2460.         rows = 1;
  2461.     }
  2462.  
  2463.     buff = lo_FetchParamValue(context, tag, PARAM_COLS);
  2464.     if (buff != NULL)
  2465.     {
  2466.         int32 len;
  2467.  
  2468.         PA_LOCK(str, char *, buff);
  2469.         len = lo_StripTextWhitespace(str, XP_STRLEN(str));
  2470.         col_percents = lo_parse_percent_list(context, str, &cols);
  2471.         if ((col_percents == NULL)&&(cols >= 1))
  2472.         {
  2473.             PA_UNLOCK(buff);
  2474.             PA_FREE(buff);
  2475.             if (row_percents != NULL)
  2476.             {
  2477.                 XP_FREE(row_percents);
  2478.             }
  2479.             XP_DELETE(subgrid);
  2480.             state->top_state->out_of_memory = TRUE;
  2481.             return;
  2482.         }
  2483.         PA_UNLOCK(buff);
  2484.         PA_FREE(buff);
  2485.         if (cols <= 0)
  2486.         {
  2487.             col_percents = (lo_GridPercent *)
  2488.                 XP_ALLOC(sizeof(lo_GridPercent));
  2489.             if (col_percents == NULL)
  2490.             {
  2491.                 if (row_percents != NULL)
  2492.                 {
  2493.                     XP_FREE(row_percents);
  2494.                 }
  2495.                 XP_DELETE(subgrid);
  2496.                 state->top_state->out_of_memory = TRUE;
  2497.                 return;
  2498.             }
  2499.             col_percents[0].type = LO_PERCENT_NORMAL;
  2500.             col_percents[0].original_value = 100;
  2501.             cols = 1;
  2502.         }
  2503.     }
  2504.     else
  2505.     {
  2506.         col_percents = (lo_GridPercent *)
  2507.             XP_ALLOC(sizeof(lo_GridPercent));
  2508.         if (col_percents == NULL)
  2509.         {
  2510.             if (row_percents != NULL)
  2511.             {
  2512.                 XP_FREE(row_percents);
  2513.             }
  2514.             XP_DELETE(subgrid);
  2515.             state->top_state->out_of_memory = TRUE;
  2516.             return;
  2517.         }
  2518.         col_percents[0].type = LO_PERCENT_NORMAL;
  2519.         col_percents[0].original_value = 100;
  2520.         cols = 1;
  2521.     }
  2522.  
  2523.     if ((rows == 1)&&(cols == 1))
  2524.     {
  2525.         if (row_percents != NULL)
  2526.         {
  2527.             XP_FREE(row_percents);
  2528.         }
  2529.         if (col_percents != NULL)
  2530.         {
  2531.             XP_FREE(col_percents);
  2532.         }
  2533.         XP_DELETE(subgrid);
  2534.         return;
  2535.     }
  2536.  
  2537. #ifdef MOCHA
  2538.     (void)lo_ProcessContextEventHandlers(context, state, tag);
  2539. #endif
  2540.  
  2541.     subgrid->rows = rows;
  2542.     subgrid->row_percents = row_percents;
  2543.     subgrid->cols = cols;
  2544.     subgrid->col_percents = col_percents;
  2545.     subgrid->cell_count = 0;
  2546.  
  2547.     subgrid->current_hist = 0;
  2548.     subgrid->max_hist = 0;
  2549.     subgrid->hist_size = 10;
  2550.  
  2551.     subgrid->main_width = state->win_width;
  2552.     subgrid->main_height = state->win_height;
  2553.     subgrid->current_cell_margin_width = 0;
  2554.     subgrid->current_cell_margin_height = 0;
  2555.  
  2556.     subgrid->subgrid = state->current_grid->subgrid;
  2557.     state->current_grid->subgrid = subgrid;
  2558.  
  2559.     /*
  2560.      ******************************************
  2561.      * Subgrid parsing done.                  *
  2562.      * Create a dummy cell to hang it off of. *
  2563.      ******************************************
  2564.      */
  2565.  
  2566.     cell = XP_NEW(lo_GridCellRec);
  2567.     if (cell == NULL)
  2568.     {
  2569.         state->top_state->out_of_memory = TRUE;
  2570.         return;
  2571.     }
  2572.  
  2573.     row_cnt = grid->cell_count / grid->cols;
  2574.     col_cnt = grid->cell_count - (row_cnt *  grid->cols);
  2575.  
  2576.     cell->x = 0;
  2577.     cell->y = 0;
  2578.     cell->width = 0;
  2579.     cell->height = 0;
  2580.     cell->margin_width = 0;
  2581.     cell->margin_height = 0;
  2582.     cell->width_percent = 0;
  2583.     cell->height_percent = 0;
  2584.     cell->url = NULL;
  2585.     cell->name = NULL;
  2586.     cell->context = NULL;
  2587.     cell->hist_list = NULL;
  2588.     cell->hist_array = (lo_GridHistory *)XP_ALLOC(grid->hist_size *
  2589.                 sizeof(lo_GridHistory));
  2590.     if (cell->hist_array == NULL)
  2591.     {
  2592.         XP_DELETE(cell);
  2593.         state->top_state->out_of_memory = TRUE;
  2594.         return;
  2595.     }
  2596.     XP_MEMSET(cell->hist_array, 0, grid->hist_size *
  2597.                 sizeof(lo_GridHistory));
  2598.     cell->hist_indx = 0;
  2599.     cell->next = NULL;
  2600.     cell->subgrid = subgrid;
  2601.     cell->side_edges[TOP_EDGE] = NULL;
  2602.     cell->side_edges[BOTTOM_EDGE] = NULL;
  2603.     cell->side_edges[LEFT_EDGE] = NULL;
  2604.     cell->side_edges[RIGHT_EDGE] = NULL;
  2605.     cell->scrolling = LO_SCROLL_AUTO;
  2606.     cell->no_edges = subgrid->no_edges;
  2607.     cell->resizable = TRUE;
  2608.     cell->edge_size = -1;
  2609.     cell->color_priority = 0;
  2610.     cell->border_color = NULL;
  2611.  
  2612.     grid->cell_count++;
  2613.  
  2614.     if (grid->cell_list_last == NULL)
  2615.     {
  2616.         grid->cell_list = cell;
  2617.         grid->cell_list_last = cell;
  2618.     }
  2619.     else
  2620.     {
  2621.         grid->cell_list_last->next = cell;
  2622.         grid->cell_list_last = cell;
  2623.     }
  2624. }
  2625.  
  2626.  
  2627. void
  2628. lo_EndSubgrid(MWContext *context, lo_DocState *state, lo_GridRec *grid)
  2629. {
  2630.     lo_GridRec *subgrid;
  2631.  
  2632.     if ((grid == NULL)||(grid->subgrid == NULL))
  2633.     {
  2634.         return;
  2635.     }
  2636.  
  2637.     subgrid = grid->subgrid;
  2638.     grid->subgrid = grid->subgrid->subgrid;
  2639.     subgrid->subgrid = NULL;
  2640. }
  2641.  
  2642.  
  2643. #ifdef DEBUG_EDGES
  2644. static void
  2645. lo_print_edge_info(lo_GridEdge *edge)
  2646. {
  2647.     int32 i;
  2648.  
  2649.     fprintf(stderr, "\nEdge %ld,%ld %ldx%ld; Range is %ld to %ld\n",
  2650.         (long) edge->x, (long) edge->y, (long) edge->width,
  2651.         (long) edge->height, (long) edge->left_top_bound,
  2652.         (long) edge->right_bottom_bound);
  2653.     fprintf(stderr, "\tBounds %ld cells\n", (long) edge->cell_cnt);
  2654.     for (i=0; i < edge->cell_cnt; i++)
  2655.     {
  2656.         lo_GridCellRec *cell;
  2657.  
  2658.         cell = edge->cell_array[i];
  2659.         fprintf(stderr, "\t(%ld) %ld,%ld %ldx%ld\n",
  2660.             (long) i, (long) cell->x, (long) cell->y,
  2661.             (long) cell->width, (long) cell->height);
  2662.     }
  2663.     fprintf(stderr, "\n");
  2664. }
  2665. #endif /* DEBUG_EDGES */
  2666.  
  2667.  
  2668. LO_Element *
  2669. lo_XYToGridEdge(MWContext *context, lo_DocState *state, int32 x, int32 y)
  2670. {
  2671.     lo_GridRec *grid;
  2672.     lo_GridEdge *edge_list;
  2673.  
  2674.     if ((state->top_state->is_grid == FALSE)||
  2675.         (state->top_state->the_grid == NULL))
  2676.     {
  2677.         return(NULL);
  2678.     }
  2679.  
  2680.     grid = state->top_state->the_grid;
  2681.     edge_list = grid->edge_list;
  2682.     while (edge_list != NULL)
  2683.     {
  2684.         /*
  2685.          * You cannot grab invisible edges.
  2686.          */
  2687.         if (edge_list->fe_edge->visible == FALSE)
  2688.         {
  2689.             edge_list = edge_list->next;
  2690.             continue;
  2691.         }
  2692.  
  2693.         if (edge_list->is_vertical == FALSE)
  2694.         {
  2695.             if ((y >= edge_list->y)&&
  2696.                 (y < (edge_list->y + edge_list->height))&&
  2697.                 (x >= edge_list->x)&&
  2698.                 (x < (edge_list->x + edge_list->width)))
  2699.             {
  2700.                 break;
  2701.             }
  2702.         }
  2703.         else
  2704.         {
  2705.             if ((x >= edge_list->x)&&
  2706.                 (x < (edge_list->x + edge_list->width))&&
  2707.                 (y >= edge_list->y)&&
  2708.                 (y < (edge_list->y + edge_list->height)))
  2709.             {
  2710.                 break;
  2711.             }
  2712.         }
  2713.         edge_list = edge_list->next;
  2714.     }
  2715.  
  2716.     if (edge_list != NULL)
  2717.     {
  2718. #ifdef DEBUG_EDGES
  2719.         lo_print_edge_info(edge_list);
  2720. #endif /* DEBUG_EDGES */
  2721.         return((LO_Element *)edge_list->fe_edge);
  2722.     }
  2723.     else
  2724.     {
  2725.         return(NULL);
  2726.     }
  2727. }
  2728.  
  2729.  
  2730. /*
  2731.  * Edges left of a moved vertical edge may need to be shrunk
  2732.  * or grown with the edge.
  2733.  */
  2734. static void
  2735. lo_change_left_edges(MWContext *context, lo_GridEdge *edge,
  2736.              int32 bound, int32 diff, Bool call_display_edge)
  2737. {
  2738.     if (edge != NULL)
  2739.     {
  2740.         /*
  2741.          * If this edge abuts the edge moved,
  2742.          * change this edges width just like
  2743.          * the cell.
  2744.          */
  2745.         if ((edge->x + edge->width) == bound)
  2746.         {
  2747.             edge->width += diff;
  2748.             edge->fe_edge->width += diff;
  2749.  
  2750.             if (call_display_edge)
  2751.               /*
  2752.                * Display the edge that was just changed.
  2753.                */
  2754.               lo_DisplayEdge(context, edge->fe_edge);
  2755.         }
  2756.     }
  2757. }
  2758.  
  2759.  
  2760. /*
  2761.  * Edges right of a moved vertical edge may need to be moved and shrunk
  2762.  * or grown with the edge.
  2763.  */
  2764. static void
  2765. lo_change_right_edges(MWContext *context, lo_GridEdge *edge,
  2766.               int32 bound, int32 diff, Bool call_display_edge)
  2767. {
  2768.     if (edge != NULL)
  2769.     {
  2770.         /*
  2771.          * If this edge abuts the edge moved,
  2772.          * change this edges position and width just like
  2773.          * the cell.
  2774.          */
  2775.         if ((edge->x - 1) == bound)
  2776.         {
  2777.             edge->x += diff;
  2778.             edge->width -= diff;
  2779.             edge->fe_edge->x += diff;
  2780.             edge->fe_edge->width -= diff;
  2781.             if (call_display_edge)
  2782.               /*
  2783.                * Display the edge that was just changed.
  2784.                */
  2785.               lo_DisplayEdge(context, edge->fe_edge);
  2786.         }
  2787.     }
  2788. }
  2789.  
  2790.  
  2791. /*
  2792.  * Edges above a moved vertical edge may need to be shrunk
  2793.  * or grown with the edge.
  2794.  */
  2795. static void
  2796. lo_change_top_edges(MWContext *context, lo_GridEdge *edge,
  2797.             int32 bound, int32 diff, Bool call_display_edge)
  2798. {
  2799.     if (edge != NULL)
  2800.     {
  2801.         /*
  2802.          * If this edge abuts the edge moved,
  2803.          * change this edges width just like
  2804.          * the cell.
  2805.          */
  2806.         if ((edge->y + edge->height) == bound)
  2807.         {
  2808.             edge->height += diff;
  2809.             edge->fe_edge->height += diff;
  2810.             if (call_display_edge)
  2811.               /*
  2812.                * Display the edge that was just changed.
  2813.                */
  2814.               lo_DisplayEdge(context, edge->fe_edge);
  2815.         }
  2816.     }
  2817. }
  2818.  
  2819.  
  2820. /*
  2821.  * Edges below a moved horizontal edge may need to be moved and shrunk
  2822.  * or grown with the edge.
  2823.  */
  2824. static void
  2825. lo_change_bottom_edges(MWContext *context, lo_GridEdge *edge,
  2826.                int32 bound, int32 diff, Bool call_display_edge)
  2827. {
  2828.     if (edge != NULL)
  2829.     {
  2830.         /*
  2831.          * If this edge abuts the edge moved,
  2832.          * change this edges position and width just like
  2833.          * the cell.
  2834.          */
  2835.         if ((edge->y - 1) == bound)
  2836.         {
  2837.             edge->y += diff;
  2838.             edge->height -= diff;
  2839.             edge->fe_edge->y += diff;
  2840.             edge->fe_edge->height -= diff;
  2841.             if (call_display_edge)
  2842.               /*
  2843.                * Display the edge that was just changed.
  2844.                */
  2845.               lo_DisplayEdge(context, edge->fe_edge);
  2846.         }
  2847.     }
  2848. }
  2849.  
  2850.  
  2851. void
  2852. LO_MoveGridEdge(MWContext *context, LO_EdgeStruct *fe_edge, int32 x, int32 y)
  2853. {
  2854.     int32 doc_id;
  2855.     lo_TopState *top_state;
  2856.     lo_DocState *state;
  2857.     int32 i;
  2858.     lo_GridEdge *edge;
  2859.  
  2860.     /*
  2861.      * Get the unique document ID, and retreive this
  2862.      * documents layout state.
  2863.      */
  2864.     doc_id = XP_DOCID(context);
  2865.     top_state = lo_FetchTopState(doc_id);
  2866.     if ((top_state == NULL)||(top_state->doc_state == NULL))
  2867.     {
  2868.         return;
  2869.     }
  2870.     state = top_state->doc_state;
  2871.  
  2872.     edge = (lo_GridEdge *)fe_edge->lo_edge_info;
  2873.     if (edge == NULL)
  2874.     {
  2875.         return;
  2876.     }
  2877.  
  2878.     /*
  2879.      * For horizontal edges
  2880.      */
  2881.     if (edge->is_vertical == FALSE)
  2882.     {
  2883.         int32 diff;
  2884.  
  2885.         diff = y - edge->y;
  2886.  
  2887.         for (i=0; i < edge->cell_cnt; i++)
  2888.         {
  2889.             lo_GridCellRec *cell;
  2890.  
  2891.             cell = edge->cell_array[i];
  2892.             if (cell == NULL)
  2893.             {
  2894.                 continue;
  2895.             }
  2896.             /*
  2897.              * If the cell is above the edge.
  2898.              * Grow the cell and any abutting
  2899.              * edges.
  2900.              */
  2901.             if (cell->y < edge->y)
  2902.             {
  2903.                 cell->height += diff;
  2904.                 lo_change_top_edges(context,
  2905.                     cell->side_edges[LEFT_EDGE],
  2906.                     edge->y, diff, TRUE);
  2907.                 lo_change_top_edges(context,
  2908.                     cell->side_edges[RIGHT_EDGE],
  2909.                     edge->y, diff, TRUE);
  2910.                 /*
  2911.                  * The bounds of this edge are effected
  2912.                  */
  2913.                 lo_set_edge_bounds(cell->side_edges[TOP_EDGE]);
  2914.             }
  2915.             /*
  2916.              * Else the cell is below the edge.
  2917.              * Move and shrink the cell and any abutting
  2918.              * edges.
  2919.              */
  2920.             else
  2921.             {
  2922.                 cell->y += diff;
  2923.                 cell->height -= diff;
  2924.                 lo_change_bottom_edges(context,
  2925.                     cell->side_edges[LEFT_EDGE],
  2926.                     (edge->y + edge->height - 1), diff, TRUE);
  2927.                 lo_change_bottom_edges(context,
  2928.                     cell->side_edges[RIGHT_EDGE],
  2929.                     (edge->y + edge->height - 1), diff, TRUE);
  2930.                 /*
  2931.                  * The bounds of this edge are effected
  2932.                  */
  2933.                 lo_set_edge_bounds(cell->side_edges[BOTTOM_EDGE]);
  2934.             }
  2935.             /*
  2936.              * Only restructure cells that have windows(contexts)
  2937.              */
  2938.             if (cell->context != NULL)
  2939.             {
  2940.                 FE_RestructureGridWindow (cell->context,
  2941.                     cell->x, cell->y,
  2942.                     cell->width, cell->height);
  2943.             }
  2944.         }
  2945.         edge->y = y;
  2946.         edge->fe_edge->y = y;
  2947.     }
  2948.     /*
  2949.      * Else for vertical edges.
  2950.      */
  2951.     else
  2952.     {
  2953.         int32 diff;
  2954.  
  2955.         diff = x - edge->x;
  2956.  
  2957.         for (i=0; i < edge->cell_cnt; i++)
  2958.         {
  2959.             lo_GridCellRec *cell;
  2960.  
  2961.             cell = edge->cell_array[i];
  2962.             if (cell == NULL)
  2963.             {
  2964.                 continue;
  2965.             }
  2966.             /*
  2967.              * If the cell is left of the edge.
  2968.              * Grow the cell and any abutting
  2969.              * edges.
  2970.              */
  2971.             if (cell->x < edge->x)
  2972.             {
  2973.                 cell->width += diff;
  2974.                 lo_change_left_edges(context,
  2975.                     cell->side_edges[TOP_EDGE],
  2976.                     edge->x, diff, TRUE);
  2977.                 lo_change_left_edges(context,
  2978.                     cell->side_edges[BOTTOM_EDGE],
  2979.                     edge->x, diff, TRUE);
  2980.                 /*
  2981.                  * The bounds of this edge are effected
  2982.                  */
  2983.                 lo_set_edge_bounds(cell->side_edges[LEFT_EDGE]);
  2984.             }
  2985.             /*
  2986.              * Else the cell is right of the edge.
  2987.              * Move and shrink the cell and any abutting
  2988.              * edges.
  2989.              */
  2990.             else
  2991.             {
  2992.                 cell->x += diff;
  2993.                 cell->width -= diff;
  2994.                 lo_change_right_edges(context,
  2995.                     cell->side_edges[TOP_EDGE],
  2996.                     (edge->x + edge->width - 1), diff, TRUE);
  2997.                 lo_change_right_edges(context,
  2998.                     cell->side_edges[BOTTOM_EDGE],
  2999.                     (edge->x + edge->width - 1), diff, TRUE);
  3000.                 /*
  3001.                  * The bounds of this edge are effected
  3002.                  */
  3003.                 lo_set_edge_bounds(cell->side_edges[RIGHT_EDGE]);
  3004.             }
  3005.             /*
  3006.              * Only restructure cells that have windows(contexts)
  3007.              */
  3008.             if (cell->context != NULL)
  3009.             {
  3010.                 FE_RestructureGridWindow (cell->context,
  3011.                     cell->x, cell->y,
  3012.                     cell->width, cell->height);
  3013.             }
  3014.         }
  3015.         edge->x = x;
  3016.         edge->fe_edge->x = x;
  3017.     }
  3018.     /*
  3019.      * Display the edge that was just moved.
  3020.      */
  3021.     lo_DisplayEdge(context, edge->fe_edge);
  3022. }
  3023.  
  3024. intn
  3025. lo_GetCurrentGridCellStatus(lo_GridCellRec *cell)
  3026. {
  3027.     int32 doc_id;
  3028.     lo_TopState *top_state;
  3029.  
  3030.     /*
  3031.      * This is a blank cell, always considered complete.
  3032.      */
  3033.     if ((cell == NULL)||(cell->context == NULL))
  3034.     {
  3035.         return(PA_COMPLETE);
  3036.     }
  3037.  
  3038.     /*
  3039.      * Get the unique document ID, and retreive this
  3040.      * documents layout state.
  3041.      */
  3042.     doc_id = XP_DOCID(cell->context);
  3043.     top_state = lo_FetchTopState(doc_id);
  3044.     if (top_state == NULL)
  3045.     {
  3046.         return(-1);
  3047.     }
  3048.     return(top_state->layout_status);
  3049. }
  3050.  
  3051.  
  3052. char *
  3053. lo_GetCurrentGridCellUrl(lo_GridCellRec *cell)
  3054. {
  3055.     int32 doc_id;
  3056.     lo_TopState *top_state;
  3057.     char *url;
  3058.  
  3059.     if ((cell == NULL)||(cell->context == NULL))
  3060.     {
  3061.         return(NULL);
  3062.     }
  3063.  
  3064.     /*
  3065.      * Get the unique document ID, and retreive this
  3066.      * documents layout state.
  3067.      */
  3068.     doc_id = XP_DOCID(cell->context);
  3069.     top_state = lo_FetchTopState(doc_id);
  3070.     if (top_state == NULL)
  3071.     {
  3072.         return(NULL);
  3073.     }
  3074.  
  3075.     if (top_state->url == NULL)
  3076.     {
  3077.         url = NULL;
  3078.     }
  3079.     else
  3080.     {
  3081.         url = XP_STRDUP(top_state->url);
  3082.     }
  3083.     return(url);
  3084. }
  3085.  
  3086.  
  3087. void
  3088. lo_GetGridCellMargins(MWContext *context,
  3089.     int32 *margin_width, int32 *margin_height)
  3090. {
  3091.     int32 doc_id;
  3092.     lo_TopState *top_state;
  3093.     MWContext *parent;
  3094.     lo_GridCellRec *cell_ptr;
  3095.  
  3096.     if ((context == NULL)||(context->grid_parent == NULL))
  3097.     {
  3098.         return;
  3099.     }
  3100.  
  3101.     parent = context->grid_parent;
  3102.     /*
  3103.      * Get the unique document ID, and retreive this
  3104.      * documents layout state.
  3105.      */
  3106.     doc_id = XP_DOCID(parent);
  3107.     top_state = lo_FetchTopState(doc_id);
  3108.     if ((top_state == NULL)||(top_state->the_grid == NULL))
  3109.     {
  3110.         return;
  3111.     }
  3112.  
  3113.     /*
  3114.      * Find the cell whose context matches the passed in context.
  3115.      */
  3116.     cell_ptr = top_state->the_grid->cell_list;
  3117.     while (cell_ptr != NULL)
  3118.     {
  3119.         if (cell_ptr->context == context)
  3120.         {
  3121.             break;
  3122.         }
  3123.         cell_ptr = cell_ptr->next;
  3124.     }
  3125.  
  3126.     /*
  3127.      * If we found the cell, set our margins.
  3128.      */
  3129.     if (cell_ptr != NULL)
  3130.     {
  3131.         *margin_width = cell_ptr->margin_width;
  3132.         *margin_height = cell_ptr->margin_height;
  3133.     }
  3134.     /*
  3135.      * If we didn't find the cell, it is
  3136.      * in the middle of creation, get the
  3137.      * margins from the parent grid.
  3138.      */
  3139.     else
  3140.     {
  3141.         *margin_width = top_state->the_grid->current_cell_margin_width;
  3142.         *margin_height = top_state->the_grid->current_cell_margin_height;
  3143.     }
  3144. }
  3145.  
  3146.  
  3147. static void
  3148. lo_reset_all_edge_bounds(MWContext *context, lo_DocState *state,
  3149.                 lo_GridRec *grid)
  3150. {
  3151.     lo_GridEdge *edge_list;
  3152.  
  3153.     if ((grid == NULL)||(grid->edge_list == NULL))
  3154.     {
  3155.         return;
  3156.     }
  3157.  
  3158.     edge_list = grid->edge_list;
  3159.     while (edge_list != NULL)
  3160.     {
  3161.         LO_EdgeStruct *fe_edge;
  3162.  
  3163.         fe_edge = (LO_EdgeStruct *)lo_NewElement(context, state,
  3164.                 LO_EDGE, NULL, 0);
  3165.         if (fe_edge == NULL)
  3166.         {
  3167.         }
  3168.  
  3169.         fe_edge->type = LO_EDGE;
  3170.         fe_edge->ele_id = NEXT_ELEMENT;
  3171.         fe_edge->x = edge_list->x;
  3172.         fe_edge->x_offset = 0;
  3173.         fe_edge->y = edge_list->y;
  3174.         fe_edge->y_offset = 0;
  3175.         fe_edge->width = edge_list->width;
  3176.         fe_edge->height = edge_list->height;
  3177.         fe_edge->line_height = 0;
  3178.         fe_edge->next = NULL;
  3179.         fe_edge->prev = NULL;
  3180.  
  3181.         fe_edge->FE_Data = NULL;
  3182.         fe_edge->lo_edge_info = (void *)edge_list;
  3183.         fe_edge->is_vertical = edge_list->is_vertical;
  3184.         fe_edge->movable = TRUE;
  3185.         fe_edge->left_top_bound = 0;
  3186.         fe_edge->right_bottom_bound = 0;
  3187.  
  3188.         edge_list->fe_edge = fe_edge;
  3189.  
  3190.         lo_set_edge_bounds(edge_list);
  3191.  
  3192.         /*
  3193.          * Insert this edge into the float list.
  3194.          */
  3195.         edge_list->fe_edge->next = state->float_list;
  3196.         state->float_list = (LO_Element *)edge_list->fe_edge;
  3197.  
  3198.         lo_DisplayEdge(context, edge_list->fe_edge);
  3199.         edge_list = edge_list->next;
  3200.     }
  3201. }
  3202.  
  3203.  
  3204. void
  3205. lo_RecreateGrid(MWContext *context, lo_DocState *state, lo_GridRec *grid)
  3206. {
  3207.     lo_GridCellRec *cell_ptr;
  3208.     int32 width, height;
  3209.  
  3210.     width = state->win_width;
  3211.     height = state->win_height;
  3212.     FE_GetFullWindowSize(context, &width, &height);
  3213.  
  3214.     if (width <= 0)
  3215.         width = 1;
  3216.  
  3217.     if (height <= 0)
  3218.         height = 1;
  3219.  
  3220. /* already set.
  3221.     grid->main_width = width;
  3222.     grid->main_height = height;
  3223. */
  3224.  
  3225.     lo_reset_all_edge_bounds(context, state, grid);
  3226.  
  3227.     state->top_state->the_grid = grid;
  3228.  
  3229.     /*
  3230.      * Go through all the cells creating them where specified.
  3231.      */
  3232.     cell_ptr = grid->cell_list;
  3233.     while (cell_ptr != NULL)
  3234.     {
  3235.         /*
  3236.          * Have the front end create the grid cell.
  3237.          */
  3238.         if (cell_ptr->url != NULL)
  3239.         {
  3240.             void *history;
  3241.             void *hist_list;
  3242.  
  3243.             history = NULL;
  3244.             hist_list = NULL;
  3245.             if (cell_ptr->hist_list != NULL)
  3246.             {
  3247.                 hist_list = cell_ptr->hist_list;
  3248.                 cell_ptr->hist_indx = grid->current_hist;
  3249.                 history = cell_ptr->hist_array[cell_ptr->hist_indx - 1].hist;
  3250.                 if (history == NULL)
  3251.                 {
  3252.                     hist_list = NULL;
  3253.                 }
  3254.             }
  3255.             grid->current_cell_margin_width = cell_ptr->margin_width;
  3256.             grid->current_cell_margin_height = cell_ptr->margin_height;
  3257.             reconnecting_cell = cell_ptr;
  3258.             cell_ptr->context = FE_MakeGridWindow (context,
  3259.                 hist_list,
  3260.                 history,
  3261.                 cell_ptr->x, cell_ptr->y,
  3262.                 cell_ptr->width, cell_ptr->height,
  3263.                 (char *)cell_ptr->url, (char *)cell_ptr->name,
  3264.                 cell_ptr->scrolling,
  3265.                 FORCE_RELOAD_FLAG(state->top_state),
  3266.                 cell_ptr->no_edges);
  3267.             reconnecting_cell = NULL;
  3268.             grid->current_cell_margin_width = 0;
  3269.             grid->current_cell_margin_height = 0;
  3270.             if (cell_ptr->context)
  3271.             {
  3272.                 XP_ASSERT(cell_ptr->context->forceJSEnabled ==
  3273.                 context->forceJSEnabled);
  3274.                 if (history)
  3275.                 {
  3276.                 cell_ptr->context->hist.cur_doc_ptr = history;
  3277.                 }
  3278.             }
  3279.         }
  3280.         else
  3281.         {
  3282.             cell_ptr->context = NULL;
  3283.         }
  3284.         cell_ptr = cell_ptr->next;
  3285.     }
  3286. }
  3287.  
  3288.  
  3289.  
  3290. lo_GridCellRec *
  3291. lo_ContextToCell(MWContext *context, Bool reconnect, lo_GridRec **grid_ptr)
  3292. {
  3293.     int32 doc_id;
  3294.     lo_TopState *top_state;
  3295.     MWContext *parent;
  3296.     lo_GridCellRec *cell_ptr;
  3297.  
  3298.     *grid_ptr = NULL;
  3299.     if ((context == NULL)||(context->grid_parent == NULL))
  3300.     {
  3301.         return((lo_GridCellRec *)NULL);
  3302.     }
  3303.  
  3304.     parent = context->grid_parent;
  3305.     /*
  3306.      * Get the unique document ID, and retreive this
  3307.      * documents layout state.
  3308.      */
  3309.     doc_id = XP_DOCID(parent);
  3310.     top_state = lo_FetchTopState(doc_id);
  3311.     if ((top_state == NULL)||(top_state->the_grid == NULL))
  3312.     {
  3313.         return((lo_GridCellRec *)NULL);
  3314.     }
  3315.  
  3316.     *grid_ptr = top_state->the_grid;
  3317.  
  3318.     /*
  3319.      * Find the cell whose context matches the passed in context.
  3320.      */
  3321.     cell_ptr = top_state->the_grid->cell_list;
  3322.     while (cell_ptr != NULL)
  3323.     {
  3324.         if (cell_ptr->context == context)
  3325.         {
  3326.             return(cell_ptr);
  3327.         }
  3328.         cell_ptr = cell_ptr->next;
  3329.     }
  3330.  
  3331.     if (reconnect && (cell_ptr = reconnecting_cell) != NULL)
  3332.     {
  3333.         cell_ptr->context = context;
  3334.         if (context->grid_parent)
  3335.         {
  3336.             context->forceJSEnabled =
  3337.                 context->grid_parent->forceJSEnabled;
  3338.         }
  3339.  
  3340.         return(cell_ptr);
  3341.     }
  3342.     return(NULL);
  3343. }
  3344.  
  3345.  
  3346. static void
  3347. lo_set_hist(MWContext *context, void **hep, History_entry *hist)
  3348. {
  3349.     History_entry *oldhist = *hep;
  3350.  
  3351.     if (oldhist == hist)
  3352.         return;
  3353.     *hep = SHIST_HoldEntry(hist);
  3354.     SHIST_DropEntry(context, oldhist);
  3355. }
  3356.  
  3357.  
  3358. PRIVATE
  3359. void
  3360. lo_UpdateOtherCells(lo_GridRec *grid, lo_GridCellRec *the_cell)
  3361. {
  3362.     lo_GridCellRec *cell_ptr;
  3363.  
  3364.     cell_ptr = grid->cell_list;
  3365.     while (cell_ptr != NULL)
  3366.     {
  3367.         if ((cell_ptr != the_cell)&&
  3368.             (cell_ptr->hist_indx < grid->current_hist))
  3369.         {
  3370.             int32 i;
  3371.             int32 old;
  3372.  
  3373.             /*
  3374.              * Make sure se don't go backwards off the beginning
  3375.              * of the array.
  3376.              */
  3377.             old = cell_ptr->hist_indx - 1;
  3378.             if (old < 0)
  3379.             {
  3380.             old = 0;
  3381.             }
  3382.  
  3383.             for (i = cell_ptr->hist_indx; i < grid->current_hist; i++)
  3384.             {
  3385.             lo_set_hist(cell_ptr->context,
  3386.                     &cell_ptr->hist_array[i].hist,
  3387.                     cell_ptr->hist_array[old].hist);
  3388.             cell_ptr->hist_array[i].position =
  3389.                 cell_ptr->hist_array[old].position;
  3390.             }
  3391.  
  3392.             /*
  3393.              * If we got here and hist_indx is zero, that means
  3394.              * we are trying to update this cell, before it ever
  3395.              * had its initializing load.  In that case, we
  3396.              * want to leave hist_indx zero so we will be able to
  3397.              * detect that initializing load when it occurs.
  3398.              */
  3399.             if (cell_ptr->hist_indx > 0)
  3400.             {
  3401.             cell_ptr->hist_indx = grid->current_hist;
  3402.             }
  3403.         }
  3404.         cell_ptr = cell_ptr->next;
  3405.     }
  3406. }
  3407.  
  3408.  
  3409. static void
  3410. lo_grow_grid_hist_arrays(lo_GridRec *grid)
  3411. {
  3412.     int32 new_size;
  3413.     lo_GridCellRec *cell_ptr;
  3414.  
  3415.     if (grid == NULL)
  3416.     {
  3417.         return;
  3418.     }
  3419.  
  3420.     new_size = grid->hist_size + 10;
  3421.     cell_ptr = grid->cell_list;
  3422.     while (cell_ptr != NULL)
  3423.     {
  3424.         lo_GridHistory *new_array;
  3425.  
  3426.         new_array = (lo_GridHistory *)XP_REALLOC(cell_ptr->hist_array,
  3427.                 (new_size * sizeof(lo_GridHistory)));
  3428.         if (new_array == NULL)
  3429.         {
  3430.             /* XXX should set top_state->out_of_memory somehow */
  3431.             return;
  3432.         }
  3433.         XP_MEMSET(new_array + grid->hist_size, 0,
  3434.               (10 * sizeof(lo_GridHistory)));
  3435.         cell_ptr->hist_array = new_array;
  3436.         cell_ptr = cell_ptr->next;
  3437.     }
  3438.     grid->hist_size  = new_size;
  3439. }
  3440.  
  3441.  
  3442. void
  3443. LO_UpdateGridHistory(MWContext *context)
  3444. {
  3445.     lo_GridRec *grid;
  3446.     lo_GridCellRec *cell_ptr;
  3447.     History_entry *hist;
  3448.  
  3449.     cell_ptr = lo_ContextToCell(context, TRUE, &grid);
  3450.     if (cell_ptr == NULL)
  3451.     {
  3452.         return;
  3453.     }
  3454.  
  3455.     hist = SHIST_GetCurrent(&context->hist);
  3456.     if (hist == NULL)
  3457.     {
  3458.         return;
  3459.     }
  3460.  
  3461.     if (cell_ptr->hist_indx >= grid->current_hist)
  3462.     {
  3463.         if (grid->current_hist >= grid->hist_size)
  3464.         {
  3465.             lo_grow_grid_hist_arrays(grid);
  3466.         }
  3467.         grid->current_hist = cell_ptr->hist_indx + 1;
  3468.         if (grid->current_hist > grid->max_hist)
  3469.         {
  3470.             grid->max_hist = grid->current_hist;
  3471.         }
  3472.         else if (grid->current_hist < grid->max_hist)
  3473.         {
  3474.             grid->max_hist = grid->current_hist;
  3475.         }
  3476.         if (grid->current_hist > 1)
  3477.         {
  3478.             lo_UpdateOtherCells(grid, cell_ptr);
  3479.             if ((context->grid_parent != NULL)&&
  3480.                 (context->grid_parent->is_grid_cell != FALSE))
  3481.             {
  3482.                 LO_UpdateGridHistory(context->grid_parent);
  3483.             }
  3484.         }
  3485.     }
  3486.     lo_set_hist(context,
  3487.             &cell_ptr->hist_array[cell_ptr->hist_indx].hist,
  3488.             hist);
  3489.     if (cell_ptr->hist_indx == 0)
  3490.     {
  3491.         cell_ptr->hist_array[cell_ptr->hist_indx].position = 1;
  3492.         /*
  3493.          * This means some other grid cell already progressed before
  3494.          * we got our first load, now that we are loaded, catch up.
  3495.          */
  3496.         if (grid->current_hist > 1)
  3497.         {
  3498.             int32 i;
  3499.  
  3500.             for (i = 1; i < grid->current_hist; i++)
  3501.             {
  3502.                 lo_set_hist(context,
  3503.                         &cell_ptr->hist_array[i].hist,
  3504.                         cell_ptr->hist_array[0].hist);
  3505.                 cell_ptr->hist_array[i].position =
  3506.                     cell_ptr->hist_array[0].position;
  3507.             }
  3508.             cell_ptr->hist_indx = grid->current_hist - 1;
  3509.         }
  3510.     }
  3511.     else
  3512.     {
  3513.         /*
  3514.          * Drop held MochaDecoder if any, we needed it only during
  3515.          * resize-reload and don't now that we've moved forward in
  3516.          * this frame context's history.
  3517.          */
  3518.         hist = cell_ptr->hist_array[cell_ptr->hist_indx - 1].hist;
  3519.         if (hist && hist->savedData.Window)
  3520.         {
  3521.             LM_DropSavedWindow(context, hist->savedData.Window);
  3522.             hist->savedData.Window = NULL;
  3523.         }
  3524.  
  3525.         cell_ptr->hist_array[cell_ptr->hist_indx].position =
  3526.             cell_ptr->hist_array[cell_ptr->hist_indx - 1].position + 1;
  3527.     }
  3528.     cell_ptr->hist_indx++;
  3529. }
  3530.  
  3531.  
  3532. PRIVATE
  3533. void
  3534. lo_BackupGridHistory(lo_GridRec *grid)
  3535. {
  3536.     lo_GridCellRec *cell_ptr;
  3537.  
  3538.     cell_ptr = grid->cell_list;
  3539.     while (cell_ptr != NULL)
  3540.     {
  3541.         if (cell_ptr->hist_indx > 1)
  3542.         {
  3543.             int32 indx;
  3544.  
  3545.             cell_ptr->hist_indx--;
  3546.             indx = cell_ptr->hist_indx - 1;
  3547.  
  3548.             if (cell_ptr->hist_array[indx].hist !=
  3549.                 cell_ptr->hist_array[cell_ptr->hist_indx].hist)
  3550.             {
  3551.                 lo_reload_cell_from_history(cell_ptr,
  3552.                         NET_DONT_RELOAD);
  3553.             }
  3554.             else if ((cell_ptr->hist_array[indx].position !=
  3555.                cell_ptr->hist_array[cell_ptr->hist_indx].position)&&
  3556.                (cell_ptr->context != NULL)&&
  3557.                (cell_ptr->context->grid_children != NULL))
  3558.             {
  3559.                 (void)LO_BackInGrid(cell_ptr->context);
  3560.             }
  3561.         }
  3562.         cell_ptr = cell_ptr->next;
  3563.     }
  3564. }
  3565.  
  3566.  
  3567. PRIVATE
  3568. void
  3569. lo_ForwardGridHistory(lo_GridRec *grid)
  3570. {
  3571.     lo_GridCellRec *cell_ptr;
  3572.  
  3573.     cell_ptr = grid->cell_list;
  3574.     while (cell_ptr != NULL)
  3575.     {
  3576.         if (cell_ptr->hist_indx < grid->max_hist)
  3577.         {
  3578.             int32 indx;
  3579.  
  3580.             cell_ptr->hist_indx++;
  3581.             indx = cell_ptr->hist_indx - 1;
  3582.  
  3583.             if (cell_ptr->hist_array[indx].hist !=
  3584.                 cell_ptr->hist_array[indx - 1].hist)
  3585.             {
  3586.                 lo_reload_cell_from_history(cell_ptr,
  3587.                         NET_DONT_RELOAD);
  3588.             }
  3589.             else if ((cell_ptr->hist_array[indx].position !=
  3590.                cell_ptr->hist_array[indx - 1].position)&&
  3591.                (cell_ptr->context != NULL)&&
  3592.                (cell_ptr->context->grid_children != NULL))
  3593.             {
  3594.                 (void)LO_ForwardInGrid(cell_ptr->context);
  3595.             }
  3596.         }
  3597.         cell_ptr = cell_ptr->next;
  3598.     }
  3599. }
  3600.  
  3601.  
  3602. Bool
  3603. LO_BackInGrid(MWContext *context)
  3604. {
  3605.     int32 doc_id;
  3606.     lo_TopState *top_state;
  3607.     lo_GridRec *grid;
  3608.  
  3609.     if ((context == NULL)||(context->grid_children == NULL))
  3610.     {
  3611.         return(FALSE);
  3612.     }
  3613.  
  3614.     /*
  3615.      * Get the unique document ID, and retreive this
  3616.      * documents layout state.
  3617.      */
  3618.     doc_id = XP_DOCID(context);
  3619.     top_state = lo_FetchTopState(doc_id);
  3620.     if ((top_state == NULL)||(top_state->the_grid == NULL))
  3621.     {
  3622.         return(FALSE);
  3623.     }
  3624.  
  3625.     grid = top_state->the_grid;
  3626.  
  3627.     if (grid->current_hist <= 1)
  3628.     {
  3629.         return(FALSE);
  3630.     }
  3631.  
  3632.     lo_BackupGridHistory(grid);
  3633.     grid->current_hist--;
  3634.     return(TRUE);
  3635. }
  3636.  
  3637.  
  3638. Bool
  3639. LO_ForwardInGrid(MWContext *context)
  3640. {
  3641.     int32 doc_id;
  3642.     lo_TopState *top_state;
  3643.     lo_GridRec *grid;
  3644.  
  3645.     if ((context == NULL)||(context->grid_children == NULL))
  3646.     {
  3647.         return(FALSE);
  3648.     }
  3649.  
  3650.     /*
  3651.      * Get the unique document ID, and retreive this
  3652.      * documents layout state.
  3653.      */
  3654.     doc_id = XP_DOCID(context);
  3655.     top_state = lo_FetchTopState(doc_id);
  3656.     if ((top_state == NULL)||(top_state->the_grid == NULL))
  3657.     {
  3658.         return(FALSE);
  3659.     }
  3660.  
  3661.     grid = top_state->the_grid;
  3662.  
  3663.     if (grid->current_hist >= grid->max_hist)
  3664.     {
  3665.         return(FALSE);
  3666.     }
  3667.  
  3668.     lo_ForwardGridHistory(grid);
  3669.     grid->current_hist++;
  3670.     return(TRUE);
  3671. }
  3672.  
  3673.  
  3674. Bool
  3675. LO_GridCanGoForward(MWContext *context)
  3676. {
  3677.     int32 doc_id;
  3678.     lo_TopState *top_state;
  3679.     lo_GridRec *grid;
  3680.  
  3681.     if ((context == NULL)||(context->grid_children == NULL))
  3682.     {
  3683.         return(FALSE);
  3684.     }
  3685.  
  3686.     /*
  3687.      * Get the unique document ID, and retreive this
  3688.      * documents layout state.
  3689.      */
  3690.     doc_id = XP_DOCID(context);
  3691.     top_state = lo_FetchTopState(doc_id);
  3692.     if ((top_state == NULL)||(top_state->the_grid == NULL))
  3693.     {
  3694.         return(FALSE);
  3695.     }
  3696.  
  3697.     grid = top_state->the_grid;
  3698.  
  3699.     if (grid->current_hist < grid->max_hist)
  3700.     {
  3701.         return(TRUE);
  3702.     }
  3703.     return(FALSE);
  3704. }
  3705.  
  3706.  
  3707. Bool
  3708. LO_GridCanGoBackward(MWContext *context)
  3709. {
  3710.     int32 doc_id;
  3711.     lo_TopState *top_state;
  3712.     lo_GridRec *grid;
  3713.  
  3714.     if ((context == NULL)||(context->grid_children == NULL))
  3715.     {
  3716.         return(FALSE);
  3717.     }
  3718.  
  3719.     /*
  3720.      * Get the unique document ID, and retreive this
  3721.      * documents layout state.
  3722.      */
  3723.     doc_id = XP_DOCID(context);
  3724.     top_state = lo_FetchTopState(doc_id);
  3725.     if ((top_state == NULL)||(top_state->the_grid == NULL))
  3726.     {
  3727.         return(FALSE);
  3728.     }
  3729.  
  3730.     grid = top_state->the_grid;
  3731.  
  3732.     if (grid->current_hist > 1)
  3733.     {
  3734.         return(TRUE);
  3735.     }
  3736.     return(FALSE);
  3737. }
  3738.  
  3739.  
  3740. static void
  3741. lo_cleanup_grid_history(MWContext *context, lo_GridRec *grid)
  3742. {
  3743.     lo_GridCellRec *cell_ptr;
  3744.  
  3745.     if (grid == NULL)
  3746.     {
  3747.         return;
  3748.     }
  3749.  
  3750.     /*
  3751.      * Set the current history to be the new maximum.
  3752.      */
  3753.     grid->max_hist = grid->current_hist;
  3754.  
  3755.     /*
  3756.      * Go through all the cells, truncating their history lists
  3757.      */
  3758.     cell_ptr = grid->cell_list;
  3759.     while (cell_ptr != NULL)
  3760.     {
  3761.         int32 indx, count;
  3762.         XP_List *list_ptr;
  3763.         History_entry *cell_hist;
  3764.  
  3765.         list_ptr = (XP_List *)cell_ptr->hist_list;
  3766.         cell_hist = cell_ptr->hist_array[grid->current_hist - 1].hist;
  3767.         if ((list_ptr != NULL)&&(cell_hist != NULL))
  3768.         {
  3769.             indx = XP_ListGetNumFromObject(list_ptr, cell_hist);
  3770.             for (count = XP_ListCount(list_ptr); count > indx; count--)
  3771.             {
  3772.             History_entry *old_entry;
  3773.  
  3774.             old_entry =
  3775.                 (History_entry *)XP_ListRemoveEndObject(list_ptr);
  3776.             SHIST_DropEntry(context, old_entry);
  3777.             }
  3778.         }
  3779.  
  3780.         /*
  3781.          * If the last history entry in the cells chain
  3782.          * is itself a grid, it may need to be cleaned
  3783.          * up recursively.
  3784.          */
  3785.         if (cell_hist != NULL)
  3786.         {
  3787.             lo_SavedGridData *saved_grid;
  3788.  
  3789.             saved_grid =(lo_SavedGridData *)(cell_hist->savedData.Grid);
  3790.             if ((saved_grid != NULL)&&(saved_grid->the_grid != NULL))
  3791.             {
  3792.             lo_cleanup_grid_history(context, saved_grid->the_grid);
  3793.             }
  3794.         }
  3795.  
  3796.         cell_ptr = cell_ptr->next;
  3797.     }
  3798. }
  3799.  
  3800.  
  3801. /*
  3802.  * When we have just added a session history after
  3803.  * this one, clean up the grid's history chains if
  3804.  * it is the parent we just left.
  3805.  */
  3806. void
  3807. LO_CleanupGridHistory(MWContext *context)
  3808. {
  3809.     History_entry *hist;
  3810.     lo_SavedGridData *saved_grid;
  3811.     lo_GridRec *grid;
  3812.  
  3813.     /*
  3814.      * Look back one, if the is no back, return.
  3815.      */
  3816.     hist = SHIST_GetPrevious(context);
  3817.     if (hist == NULL)
  3818.     {
  3819.         return;
  3820.     }
  3821.  
  3822.     /*
  3823.      * Is the document back one in history a valid grid,
  3824.      * if not, return.
  3825.      */
  3826.     saved_grid = (lo_SavedGridData *)(hist->savedData.Grid);
  3827.     if ((saved_grid == NULL)||(saved_grid->the_grid == NULL))
  3828.     {
  3829.         return;
  3830.     }
  3831.  
  3832.     /*
  3833.      * Since we are appending after this history stage,
  3834.      * the current history chains must be
  3835.      * clipped so this is the new max.
  3836.      */
  3837.     grid = saved_grid->the_grid;
  3838.     lo_cleanup_grid_history(context, grid);
  3839. }
  3840.  
  3841. static void
  3842. lo_MoveGridEdgeForRelayout(MWContext *context, LO_EdgeStruct *fe_edge, int32 x, int32 y)
  3843. {
  3844.     int32 doc_id;
  3845.     lo_TopState *top_state;
  3846.     lo_DocState *state;
  3847.     int32 i;
  3848.     lo_GridEdge *edge;
  3849.  
  3850.     /*
  3851.      * Get the unique document ID, and retreive this
  3852.      * documents layout state.
  3853.      */
  3854.     doc_id = XP_DOCID(context);
  3855.     top_state = lo_FetchTopState(doc_id);
  3856.     if ((top_state == NULL)||(top_state->doc_state == NULL))
  3857.     {
  3858.         return;
  3859.     }
  3860.     state = top_state->doc_state;
  3861.  
  3862.     edge = (lo_GridEdge *)fe_edge->lo_edge_info;
  3863.     if (edge == NULL)
  3864.     {
  3865.         return;
  3866.     }
  3867.  
  3868.     /*
  3869.      * For horizontal edges
  3870.      */
  3871.     if (edge->is_vertical == FALSE)
  3872.     {
  3873.         int32 diff;
  3874.  
  3875.         diff = y - edge->y;
  3876.  
  3877.         for (i=0; i < edge->cell_cnt; i++)
  3878.         {
  3879.             lo_GridCellRec *cell;
  3880.  
  3881.             cell = edge->cell_array[i];
  3882.             if (cell == NULL)
  3883.             {
  3884.                 continue;
  3885.             }
  3886.             /*
  3887.              * If the cell is above the edge.
  3888.              * Grow the cell and any abutting
  3889.              * edges.
  3890.              */
  3891.             if (cell->y < edge->y)
  3892.             {
  3893. #if 0
  3894.                 cell->height += diff;
  3895. #endif
  3896.                 lo_change_top_edges(context,
  3897.                     cell->side_edges[LEFT_EDGE],
  3898.                     edge->y, diff, FALSE);
  3899.                 lo_change_top_edges(context,
  3900.                     cell->side_edges[RIGHT_EDGE],
  3901.                     edge->y, diff, FALSE);
  3902.                 /*
  3903.                  * The bounds of this edge are effected
  3904.                  */
  3905.                 lo_set_edge_bounds(cell->side_edges[TOP_EDGE]);
  3906.             }
  3907.             /*
  3908.              * Else the cell is below the edge.
  3909.              * Move and shrink the cell and any abutting
  3910.              * edges.
  3911.              */
  3912.             else
  3913.             {
  3914. #if 0
  3915.                 cell->y += diff;
  3916.                 cell->height -= diff;
  3917. #endif
  3918.                 lo_change_bottom_edges(context,
  3919.                     cell->side_edges[LEFT_EDGE],
  3920.                     (edge->y + edge->height - 1), diff, FALSE);
  3921.                 lo_change_bottom_edges(context,
  3922.                     cell->side_edges[RIGHT_EDGE],
  3923.                     (edge->y + edge->height - 1), diff, FALSE);
  3924.                 /*
  3925.                  * The bounds of this edge are effected
  3926.                  */
  3927.                 lo_set_edge_bounds(cell->side_edges[BOTTOM_EDGE]);
  3928.             }
  3929.             /*
  3930.              * Only restructure cells that have windows(contexts)
  3931.              */
  3932.             if (cell->context != NULL)
  3933.             {
  3934.               cell->needs_restructuring = TRUE;
  3935.             }
  3936.         }
  3937.         edge->y = y;
  3938.         edge->fe_edge->y = y;
  3939.     }
  3940.     /*
  3941.      * Else for vertical edges.
  3942.      */
  3943.     else
  3944.     {
  3945.         int32 diff;
  3946.  
  3947.         diff = x - edge->x;
  3948.  
  3949.         for (i=0; i < edge->cell_cnt; i++)
  3950.         {
  3951.             lo_GridCellRec *cell;
  3952.  
  3953.             cell = edge->cell_array[i];
  3954.             if (cell == NULL)
  3955.             {
  3956.                 continue;
  3957.             }
  3958.             /*
  3959.              * If the cell is left of the edge.
  3960.              * Grow the cell and any abutting
  3961.              * edges.
  3962.              */
  3963.             if (cell->x < edge->x)
  3964.             {
  3965. #if 0
  3966.                 cell->width += diff;
  3967. #endif
  3968.                 lo_change_left_edges(context,
  3969.                     cell->side_edges[TOP_EDGE],
  3970.                     edge->x, diff, FALSE);
  3971.                 lo_change_left_edges(context,
  3972.                     cell->side_edges[BOTTOM_EDGE],
  3973.                     edge->x, diff, FALSE);
  3974.                 /*
  3975.                  * The bounds of this edge are effected
  3976.                  */
  3977.                 lo_set_edge_bounds(cell->side_edges[LEFT_EDGE]);
  3978.             }
  3979.             /*
  3980.              * Else the cell is right of the edge.
  3981.              * Move and shrink the cell and any abutting
  3982.              * edges.
  3983.              */
  3984.             else
  3985.             {
  3986. #if 0
  3987.                 cell->x += diff;
  3988.                 cell->width -= diff;
  3989. #endif
  3990.                 lo_change_right_edges(context,
  3991.                     cell->side_edges[TOP_EDGE],
  3992.                     (edge->x + edge->width - 1), diff, FALSE);
  3993.                 lo_change_right_edges(context,
  3994.                     cell->side_edges[BOTTOM_EDGE],
  3995.                     (edge->x + edge->width - 1), diff, FALSE);
  3996.                 /*
  3997.                  * The bounds of this edge are effected
  3998.                  */
  3999.                 lo_set_edge_bounds(cell->side_edges[RIGHT_EDGE]);
  4000.             }
  4001.             /*
  4002.              * Only restructure cells that have windows(contexts)
  4003.              */
  4004.             if (cell->context != NULL)
  4005.             {
  4006.               cell->needs_restructuring = TRUE;
  4007.             }
  4008.         }
  4009.         edge->x = x;
  4010.         edge->fe_edge->x = x;
  4011.     }
  4012. }
  4013.  
  4014. PRIVATE
  4015. void
  4016. lo_ReLayoutGridCells(MWContext *context,
  4017.              int32 x, int32 y, int32 width, int32 height,
  4018.              lo_GridRec *grid)
  4019. {
  4020.     int32 cols, rows;
  4021.     int32 col_cnt, row_cnt;
  4022.     int32 orig_x, orig_y;
  4023.     lo_GridCellRec *cell_list;
  4024.     int32 new_cell_width, new_cell_height;
  4025.  
  4026.     if (grid == NULL)
  4027.     {
  4028.         return;
  4029.     }
  4030.  
  4031.     /*
  4032.      * Make sure all the row percentages total up
  4033.      * to 100%
  4034.      */
  4035.     lo_adjust_percents(grid->rows, grid->row_percents, height);
  4036.     
  4037.     /*
  4038.      * Make sure all the col percentages total up
  4039.      * to 100%
  4040.      */
  4041.     lo_adjust_percents(grid->cols, grid->col_percents, width);
  4042.  
  4043.     cols = grid->cols;
  4044.     rows = grid->rows;
  4045.     cell_list = grid->cell_list;
  4046.  
  4047.     orig_x = x;
  4048.     orig_y = y;
  4049.     col_cnt = 0;
  4050.     row_cnt = 0;
  4051.     while (cell_list != NULL)
  4052.     {
  4053.         cell_list->width_percent =
  4054.             (intn)grid->col_percents[col_cnt].value;
  4055.         cell_list->height_percent =
  4056.             (intn)grid->row_percents[row_cnt].value;
  4057.  
  4058.         cell_list->x = x;
  4059.         if (col_cnt != 0)
  4060.         {
  4061.             cell_list->x += grid->grid_cell_border;
  4062.         }
  4063.         cell_list->y = y;
  4064.         if (row_cnt != 0)
  4065.         {
  4066.             cell_list->y += grid->grid_cell_border;
  4067.         }
  4068.  
  4069.         new_cell_width = width * cell_list->width_percent / 100;
  4070.         if (new_cell_width < grid->grid_cell_min_dim)
  4071.         {
  4072.             new_cell_width = grid->grid_cell_min_dim;
  4073.         }
  4074.         if (col_cnt > 0)
  4075.         {
  4076.             new_cell_width -= grid->grid_cell_border;
  4077.         }
  4078.         if (col_cnt < (cols - 1))
  4079.         {
  4080.             new_cell_width -= grid->grid_cell_border;
  4081.         }
  4082.  
  4083.         /*
  4084.          * Make sure the last column uses all the remaining space.
  4085.          * We subtract orig_x to get cell_list->x into the same coord
  4086.          * space as width.
  4087.          */
  4088.         if (col_cnt == (cols - 1))
  4089.         {
  4090.             new_cell_width = width - (cell_list->x - orig_x);
  4091.         }
  4092.  
  4093.         new_cell_height = height * cell_list->height_percent / 100;
  4094.         if (new_cell_height < grid->grid_cell_min_dim)
  4095.         {
  4096.             new_cell_height = grid->grid_cell_min_dim;
  4097.         }
  4098.         if (row_cnt > 0)
  4099.         {
  4100.             new_cell_height -= grid->grid_cell_border;
  4101.         }
  4102.         if (row_cnt < (rows - 1))
  4103.         {
  4104.             new_cell_height -= grid->grid_cell_border;
  4105.         }
  4106.  
  4107.         /*
  4108.          * Make sure the last row uses all the remaining space.
  4109.          * We subtract orig_y to get cell_list->y into the same coord
  4110.          * space as height.
  4111.          */
  4112.         if (row_cnt == (rows - 1))
  4113.         {
  4114.             new_cell_height = height - (cell_list->y - orig_y);
  4115.         }
  4116.  
  4117.         cell_list->width = new_cell_width;
  4118.         cell_list->height = new_cell_height;
  4119.  
  4120.         if (cell_list->side_edges[RIGHT_EDGE])
  4121.           {
  4122.             if (cell_list->side_edges[RIGHT_EDGE]->dealt_with_in_relayout == FALSE)
  4123.               {
  4124.             lo_MoveGridEdgeForRelayout(context,
  4125.                            cell_list->side_edges[RIGHT_EDGE]->fe_edge,
  4126.                            cell_list->x + cell_list->width,
  4127.                            cell_list->side_edges[RIGHT_EDGE]->y);
  4128.             cell_list->side_edges[RIGHT_EDGE]->dealt_with_in_relayout = TRUE;
  4129.               }
  4130.           }
  4131.  
  4132.         if (cell_list->side_edges[BOTTOM_EDGE])
  4133.           {
  4134.             if (cell_list->side_edges[BOTTOM_EDGE]->dealt_with_in_relayout == FALSE)
  4135.               {
  4136.             lo_MoveGridEdgeForRelayout(context,
  4137.                            cell_list->side_edges[BOTTOM_EDGE]->fe_edge,
  4138.                            cell_list->side_edges[BOTTOM_EDGE]->x,
  4139.                            cell_list->y + cell_list->height);
  4140.             cell_list->side_edges[BOTTOM_EDGE]->dealt_with_in_relayout = TRUE;
  4141.               }
  4142.           }
  4143.  
  4144.         x += new_cell_width;
  4145.         if (col_cnt > 0)
  4146.         {
  4147.             x += grid->grid_cell_border;
  4148.         }
  4149.         if (col_cnt < (cols - 1))
  4150.         {
  4151.             x += grid->grid_cell_border;
  4152.         }
  4153.         col_cnt++;
  4154.         if (col_cnt >= cols)
  4155.         {
  4156.             x = orig_x;
  4157.             y += new_cell_height;
  4158.             if (row_cnt > 0)
  4159.             {
  4160.                 y += grid->grid_cell_border;
  4161.             }
  4162.             if (row_cnt < (rows - 1))
  4163.             {
  4164.                 y += grid->grid_cell_border;
  4165.             }
  4166.             col_cnt = 0;
  4167.             row_cnt++;
  4168.         }
  4169.  
  4170.         cell_list = cell_list->next;
  4171.     }
  4172. }
  4173.  
  4174. static void
  4175. lo_PrepareGridForRelayout(MWContext *context,
  4176.               lo_GridRec *grid)
  4177. {
  4178.   lo_GridEdge *edge_list;
  4179.   lo_GridCellRec *cell_list;
  4180.  
  4181.   cell_list = grid->cell_list;
  4182.   while(cell_list)
  4183.     {
  4184.       cell_list->needs_restructuring = FALSE;
  4185.       cell_list = cell_list->next;
  4186.     }
  4187.  
  4188.   edge_list = grid->edge_list;
  4189.   while (edge_list != NULL)
  4190.     {
  4191.       /*      FE_HideEdge(context, FE_VIEW, edge_list->fe_edge);*/
  4192.       edge_list->dealt_with_in_relayout = FALSE;
  4193.       edge_list = edge_list->next;
  4194.     }
  4195.  
  4196. }
  4197.  
  4198. static void
  4199. lo_ResyncEdgeSizes(MWContext *context,
  4200.            lo_GridRec *grid)
  4201. {
  4202.   lo_GridEdge *edge_list;
  4203.  
  4204.   edge_list = grid->edge_list;
  4205.   while (edge_list != NULL)
  4206.     {
  4207.       int i;
  4208.  
  4209.       lo_set_edge_bounds(edge_list);
  4210.  
  4211.       if (edge_list->is_vertical == FALSE) /* horizontal edge */
  4212.     {
  4213.       int32 width = 0;
  4214.       for (i = 0; i < edge_list->cell_cnt; i ++)
  4215.         {
  4216.           lo_GridCellRec *cell;
  4217.           
  4218.           cell = edge_list->cell_array[i];
  4219.           if (cell == NULL)
  4220.         {
  4221.           continue;
  4222.         }
  4223.  
  4224.           if (cell->x + cell->width > edge_list->x + width)
  4225.         width = cell->x + cell->width - edge_list->x;
  4226.         }
  4227.  
  4228.       edge_list->width = width;
  4229.       edge_list->fe_edge->width = width;
  4230.     }
  4231.       else /* vertical edge */
  4232.     {
  4233.       int32 height = 0;
  4234.       for (i = 0; i < edge_list->cell_cnt; i ++)
  4235.         {
  4236.           lo_GridCellRec *cell;
  4237.           
  4238.           cell = edge_list->cell_array[i];
  4239.           if (cell == NULL)
  4240.         {
  4241.           continue;
  4242.         }
  4243.  
  4244.           if (cell->y + cell->height > edge_list->y + height)
  4245.         height = cell->y + cell->height - edge_list->y;
  4246.         }
  4247.  
  4248.       edge_list->height = height;
  4249.       edge_list->fe_edge->height = height;
  4250.     }
  4251.  
  4252.       lo_DisplayEdge(context, edge_list->fe_edge);
  4253.  
  4254.       edge_list = edge_list->next;
  4255.     }
  4256. }
  4257.  
  4258. static void
  4259. lo_RestructureCells(MWContext *context,
  4260.             lo_GridRec *grid)
  4261. {
  4262.   lo_GridCellRec *cell_list;
  4263.   cell_list = grid->cell_list;
  4264.   while(cell_list)
  4265.     {
  4266.       if (cell_list->needs_restructuring &&
  4267.       cell_list->context != NULL)
  4268.     {
  4269.       FE_RestructureGridWindow (cell_list->context,
  4270.                     cell_list->x, cell_list->y,
  4271.                     cell_list->width, cell_list->height);
  4272.     }
  4273.  
  4274.       cell_list = cell_list->next;
  4275.     }
  4276. }
  4277.  
  4278. void
  4279. lo_RelayoutGridDocumentOnResize(MWContext *context,
  4280.                 lo_TopState *top_state,
  4281.                 int32 width, int32 height)
  4282. {
  4283.   lo_GridRec *grid = top_state->the_grid;
  4284.  
  4285.   lo_PrepareGridForRelayout(context, grid);
  4286.  
  4287.   lo_ReLayoutGridCells(context, 0, 0, width, height, grid);
  4288.  
  4289.   lo_RestructureCells(context, grid);
  4290.  
  4291.   lo_ResyncEdgeSizes(context, grid);
  4292. }
  4293.  
  4294.