1 /*******************************************************************************
2  * Copyright (c) 2013-2021, Andrés Martinelli <andmarti@gmail.com>             *
3  * All rights reserved.                                                        *
4  *                                                                             *
5  * This file is a part of SC-IM                                                *
6  *                                                                             *
7  * SC-IM is a spreadsheet program that is based on SC. The original authors    *
8  * of SC are James Gosling and Mark Weiser, and mods were later added by       *
9  * Chuck Martin.                                                               *
10  *                                                                             *
11  * Redistribution and use in source and binary forms, with or without          *
12  * modification, are permitted provided that the following conditions are met: *
13  * 1. Redistributions of source code must retain the above copyright           *
14  *    notice, this list of conditions and the following disclaimer.            *
15  * 2. Redistributions in binary form must reproduce the above copyright        *
16  *    notice, this list of conditions and the following disclaimer in the      *
17  *    documentation and/or other materials provided with the distribution.     *
18  * 3. All advertising materials mentioning features or use of this software    *
19  *    must display the following acknowledgement:                              *
20  *    This product includes software developed by Andrés Martinelli            *
21  *    <andmarti@gmail.com>.                                                    *
22  * 4. Neither the name of the Andrés Martinelli nor the                        *
23  *   names of other contributors may be used to endorse or promote products    *
24  *   derived from this software without specific prior written permission.     *
25  *                                                                             *
26  * THIS SOFTWARE IS PROVIDED BY ANDRES MARTINELLI ''AS IS'' AND ANY            *
27  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED   *
28  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE      *
29  * DISCLAIMED. IN NO EVENT SHALL ANDRES MARTINELLI BE LIABLE FOR ANY           *
30  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES  *
31  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;*
32  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND *
33  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT  *
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE       *
35  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.           *
36  *******************************************************************************/
37 
38 /**
39  * \file hide_show.c
40  * \author Andrés Martinelli <andmarti@gmail.com>
41  * \date 2017-07-18
42  * \brief TODO Write a tbrief file description.
43  */
44 
45 #include <stdlib.h>
46 
47 #include "sc.h"
48 #include "macros.h"
49 #include "tui.h"
50 #include "hide_show.h"
51 #include "conf.h"
52 #include "vmtbl.h"   // for growtbl
53 
54 #ifdef UNDO
55 #include "undo.h"
56 extern struct undo undo_item;
57 #endif
58 
59 /**
60  * \brief Mark a row as hidden
61  *
62  * \param[in] from_row
63  * \param[in] arg
64  *
65  * \return none
66  */
67 
hide_row(int from_row,int arg)68 void hide_row(int from_row, int arg) {
69     register int r2;
70 
71     r2 = from_row + arg - 1;
72     if (from_row < 0 || from_row > r2) {
73         sc_error("Cannot hide row: Invalid range.");
74         return;
75     }
76     if (r2 >= maxrows - 1) {
77         // error: tried to hide a row higher than maxrow.
78         lookat(from_row + arg + 1, curcol); //FIXME this HACK
79         if (! growtbl(GROWROW, arg + 1, 0)) {
80             sc_error("You can't hide the last row");
81             return;
82         }
83     }
84 
85     if (! loading) {
86         modflg++;
87         #ifdef UNDO
88         create_undo_action();
89         undo_hide_show(from_row, -1, 'h', arg);
90         end_undo_action();
91         #endif
92     }
93     while ( from_row <= r2)
94         row_hidden[ from_row++ ] = TRUE;
95     return;
96 }
97 
98 /**
99  * \brief Mark a column as hidden
100  *
101  * \param[in] from_col
102  * \param[in] arg
103  *
104  * \return none
105  */
106 
hide_col(int from_col,int arg)107 void hide_col(int from_col, int arg) {
108     int c2 = from_col + arg - 1;
109     if (from_col < 0 || from_col > c2) {
110         sc_error ("Cannot hide col: Invalid range.");
111         return;
112     }
113     if (c2 >= maxcols - 1) {
114         // sc_error: tried to hide a column higher than maxcol.
115         lookat(currow, from_col + arg + 1); //FIXME this HACK
116         if ((arg >= ABSMAXCOLS - 1) || ! growtbl(GROWCOL, 0, arg + 1)) {
117             sc_error("You can't hide the last col");
118             return;
119         }
120     }
121 
122     if (! loading) {
123         modflg++;
124         #ifdef UNDO
125         create_undo_action();
126         create_undo_action();
127         create_undo_action();
128         undo_hide_show(-1, from_col, 'h', arg);
129         end_undo_action();
130         #endif
131     }
132     while (from_col <= c2)
133         col_hidden[ from_col++ ] = TRUE;
134     return;
135 }
136 
137 /**
138  * \brief Mark a row as not-hidden
139  *
140  * \param[in] from_row
141  * \param[in] arg
142  *
143  * \return none
144  */
145 
show_row(int from_row,int arg)146 void show_row(int from_row, int arg) {
147     int r2 = from_row + arg - 1;
148     if (from_row < 0 || from_row > r2) {
149         sc_error ("Cannot show row: Invalid range.");
150         return;
151     }
152     if (r2 > maxrows - 1) {
153         r2 = maxrows - 1;
154     }
155 
156     modflg++;
157     #ifdef UNDO
158     create_undo_action();
159     #endif
160     while (from_row <= r2) {
161         #ifdef UNDO
162         if ( row_hidden[from_row] ) undo_hide_show(from_row, -1, 's', 1);
163         #endif
164         row_hidden[ from_row++ ] = FALSE;
165     }
166     #ifdef UNDO
167     end_undo_action();
168     #endif
169     return;
170 }
171 
172 /**
173  * \brief Mark a column as not-hidden
174  *
175  * \param[in] from_col
176  * \param[in] arg
177  *
178  * \return none
179  */
180 
show_col(int from_col,int arg)181 void show_col(int from_col, int arg) {
182     int c2 = from_col + arg - 1;
183     if (from_col < 0 || from_col > c2) {
184         sc_error ("Cannot show col: Invalid range.");
185         return;
186     }
187     if (c2 > maxcols - 1) {
188         c2 = maxcols - 1;
189     }
190 
191     modflg++;
192     #ifdef UNDO
193     create_undo_action();
194     #endif
195     while (from_col <= c2) {
196         #ifdef UNDO
197         if ( col_hidden[from_col] ) undo_hide_show(-1, from_col, 's', 1);
198         #endif
199         col_hidden[ from_col++ ] = FALSE;
200     }
201     #ifdef UNDO
202     end_undo_action();
203     #endif
204     return;
205 }
206 
207 /**
208  * \brief TODO Document show_hiddenrows
209  *
210  * \return none
211  */
212 
show_hiddenrows()213 void show_hiddenrows() {
214     int r, c = 0;
215     for (r = 0; r < maxrow; r++) {
216         if (row_hidden[r]) c++;
217     }
218     char valores[12 * c + 20];
219     valores[0]='\0';
220     strcpy(valores, "Hidden rows:\n"); // 20
221     for (r = 0; r < maxrow; r++) {
222        if (row_hidden[r]) sprintf(valores + strlen(valores), "- %d\n", r); // 12
223     }
224     ui_show_text(valores);
225 
226     return;
227 }
228 
229 /**
230  * \brief TODO Document show_hiddencols
231  *
232  * \return none
233  */
234 
show_hiddencols()235 void show_hiddencols() {
236     int c, count = 0;
237     for (c = 0; c < maxcol; c++) {
238         if (col_hidden[c]) count++;
239     }
240     char valores[8 * c + 20];
241     valores[0]='\0';
242     strcpy(valores, "Hidden cols:\n"); // 20
243     for (c = 0; c < maxcol; c++) {
244        if (col_hidden[c]) sprintf(valores + strlen(valores), "- %s\n", coltoa(c)); // 8
245     }
246     ui_show_text(valores);
247 
248     return;
249 }
250