summaryrefslogtreecommitdiff
path: root/iup/srccontrols/matrix/iupmat_numlc.c
blob: e48720a981e88537040fd874c2c86eb05d7ca30e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/** \file
 * \brief iupmatrix control
 * change number of columns or lines
 *
 * See Copyright Notice in "iup.h"
 */

/**************************************************************************/
/*  Functions to change the number of lines and columns of the matrix,    */
/*  after it has been created.                                            */
/**************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "iup.h"
#include "iupcbs.h"

#include <cd.h>

#include "iup_object.h"
#include "iup_attrib.h"
#include "iup_str.h"
#include "iup_stdcontrols.h"

#include "iupmat_def.h"
#include "iupmat_edit.h"
#include "iupmat_mem.h"
#include "iupmat_numlc.h"
#include "iupmat_draw.h"


static void iMatrixUpdateLineAttributes(Ihandle* ih, int base, int count, int add)
{
#define IMAT_NUM_ATTRIB_LINE 6
#define IMAT_ATTRIB_LINE_ONLY 3
  char* attrib_format[IMAT_NUM_ATTRIB_LINE] = {
    "BGCOLOR%d:*",
    "FGCOLOR%d:*",
    "FONT%d:*",
    "BGCOLOR%d:%d",
    "FGCOLOR%d:%d",
    "FONT%d:%d"};
  char* attrib = iupStrGetMemory(100);
  int a, lin, col;
  char* value;

  if (add)  /* ADD */
  {
    /* copy the attributes of the moved cells, from base+count to num */
    /*   do it in reverse order to avoid overlapping */
    /* then clear the new space starting from base to base+count */

    for(a = 0; a < IMAT_NUM_ATTRIB_LINE; a++)
    {
      for(lin = ih->data->lines.num-1; lin >= base+count; lin--)
      {
        /* Update the line attributes */
        if (a < IMAT_ATTRIB_LINE_ONLY)
        {
          sprintf(attrib, attrib_format[a], lin-count);
          value = iupAttribGet(ih, attrib);
          sprintf(attrib, attrib_format[a], lin);
          iupAttribStoreStr(ih, attrib, value);
        }
        /* Update the cell attribute */
        else for(col = 0; col < ih->data->columns.num; col++)
        {
          sprintf(attrib, attrib_format[a], lin-count, col);
          value = iupAttribGet(ih, attrib);
          sprintf(attrib, attrib_format[a], lin, col);
          iupAttribStoreStr(ih, attrib, value);
        }
      }

      for(lin = base; lin < base+count; lin++)
      {
        if (a < IMAT_ATTRIB_LINE_ONLY)
        {
          sprintf(attrib, attrib_format[a], lin);
          iupAttribSetStr(ih, attrib, NULL);
        }
        else for(col = 0; col < ih->data->columns.num; col++)
        {
          sprintf(attrib, attrib_format[a], lin, col);
          iupAttribSetStr(ih, attrib, NULL);
        }
      }
    }
  }
  else  /* DEL */
  {
    /* copy the attributes of the moved cells from base+count to base */
    /* then clear the remaining space starting at num */

    for(a = 0; a < IMAT_NUM_ATTRIB_LINE; a++)
    {
      for(lin = base; lin < ih->data->lines.num; lin++)
      {
        /* Update the line attributes */     
        if (a < IMAT_ATTRIB_LINE_ONLY)      
        {
          sprintf(attrib, attrib_format[a], lin+count);
          value = iupAttribGet(ih, attrib);
          sprintf(attrib, attrib_format[a], lin);
          iupAttribStoreStr(ih, attrib, value);
        }
        /* Update each cell attribute */
        else for(col = 0; col < ih->data->columns.num; col++) 
        {
          sprintf(attrib, attrib_format[a], lin+count, col);
          value = iupAttribGet(ih, attrib);
          sprintf(attrib, attrib_format[a], lin, col);
          iupAttribStoreStr(ih, attrib, value);
        }
      }

      for(lin = ih->data->lines.num; lin < ih->data->lines.num+count; lin++)
      {
        if (a < IMAT_ATTRIB_LINE_ONLY)
        {
          sprintf(attrib, attrib_format[a], lin);
          iupAttribSetStr(ih, attrib, NULL);
        }
        else for(col = 0; col < ih->data->columns.num; col++)
        {
          sprintf(attrib, attrib_format[a], lin, col);
          iupAttribSetStr(ih, attrib, NULL);
        }
      }
    }
  }
}

static void iMatrixUpdateColumnAttributes(Ihandle* ih, int base, int count, int add)
{
#define IMAT_NUM_ATTRIB_COL 7
#define IMAT_ATTRIB_COL_ONLY 4
  char* attrib_format[IMAT_NUM_ATTRIB_COL] = { 
    "ALIGNMENT%d",
    "BGCOLOR*:%d",
    "FGCOLOR*:%d",
    "FONT*:%d",
    "BGCOLOR%d:%d",
    "FGCOLOR%d:%d",
    "FONT%d:%d"};
  char* attrib = iupStrGetMemory(100);
  int a, col, lin;
  char* value;

  if (add)  /* ADD */
  {
    /* update the attributes of the moved cells, from base+count to num */
    /*   do it in reverse order to avoid overlapping */
    /* then clear the new space starting from base to base+count */

    for(a = 0; a < IMAT_NUM_ATTRIB_COL; a++)
    {
      for(col = ih->data->columns.num-1; col >= base+count; col--)
      {
        /* Update the column attributes */
        if (a < IMAT_ATTRIB_COL_ONLY)
        {
          sprintf(attrib, attrib_format[a], col-count);
          value = iupAttribGet(ih, attrib);
          sprintf(attrib,attrib_format[a],col);
          iupAttribStoreStr(ih, attrib, value);
        }
        /* Update the cell attributes */
        else for(lin = 0; lin < ih->data->lines.num; lin++)
        {
          sprintf(attrib, attrib_format[a], lin, col-count);
          value = iupAttribGet(ih, attrib);
          sprintf(attrib, attrib_format[a], lin, col);
          iupAttribStoreStr(ih, attrib, value);
        }
      }

      for(col = base; col < base+count; col++)
      {
        if (a < IMAT_ATTRIB_COL_ONLY)
        {
          sprintf(attrib, attrib_format[a], col);
          iupAttribSetStr(ih, attrib, NULL);
        }
        else for(lin = 0; lin < ih->data->lines.num; lin++)
        {
          sprintf(attrib, attrib_format[a], lin, col);
          iupAttribSetStr(ih, attrib, NULL);
        }
      }
    }
  }
  else   /* DEL */
  {
    /* copy the attributes of the moved cells from base+count to base */
    /* then clear the remaining space starting at num */

    for(a = 0; a < IMAT_NUM_ATTRIB_COL; a++)
    {
      for(col = base; col < ih->data->columns.num; col++)
      {
        /* Update the column attributes */
        if (a < IMAT_ATTRIB_COL_ONLY)
        {
          sprintf(attrib, attrib_format[a], col+count);
          value = iupAttribGet(ih, attrib);
          sprintf(attrib, attrib_format[a], col);
          iupAttribStoreStr(ih, attrib, value);
        }
        /* Update the cell attributes */
        else for(lin = 0; lin < ih->data->lines.num; lin++)
        {
          sprintf(attrib, attrib_format[a], lin, col+count);
          value = iupAttribGet(ih, attrib);
          sprintf(attrib, attrib_format[a], lin, col);
          iupAttribStoreStr(ih, attrib, value);
        }
      }

      for(col = ih->data->columns.num; col < ih->data->columns.num+count; col++)
      {
        if (a < IMAT_ATTRIB_COL_ONLY)
        {
          sprintf(attrib, attrib_format[a], col);
          iupAttribSetStr(ih, attrib, NULL);
        }
        else for(lin = 0; lin < ih->data->lines.num; lin++)
        {
          sprintf(attrib, attrib_format[a], lin, col);
          iupAttribSetStr(ih, attrib, NULL);
        }
      }
    }
  }
}

static int iMatrixGetStartEnd(const char* value, int *base, int *count, int max, int del)
{
  int ret;

  if (!value)
    return 0;

  *base = 0; 
  *count = 1;

  ret = sscanf(value, "%d-%d", base, count);
  if (ret <= 0 || ret > 2)
    return 0;
  if (ret == 1)
    *count = 1;

  if (*count <= 0)
    return 0;

  if (del)
  {
    if (*base <= 0)  /* the first valid element is always 1 */
      *base = 1;

    /* when del, base can be at the last element */
    if (*base > max-1)
      *base = max-1;

    /* when del, count must be inside the existant range */
    if (*base + *count > max)
      *count = max - *base;
  }
  else
  {
    (*base)++; /* add after the given index, so increment to position the base */

    if (*base <= 0)  /* the first valid element is always 1 */
      *base = 1;

    /* when add, base can be just after the last element but not more */
    if (*base > max)
      *base = max;

    /* when add, count can be any positive value */
  }

  return 1;
}


/**************************************************************************/
/* Exported functions                                                     */
/**************************************************************************/

int iupMatrixSetAddLinAttrib(Ihandle* ih, const char* value)
{
  int base, count, lines_num = ih->data->lines.num;

  if (!ih->handle)  /* do not store the action before map */
    return 0;

  if (!iMatrixGetStartEnd(value, &base, &count, lines_num, 0))
    return 0;

  /* if the focus cell is after the inserted area */
  if (ih->data->lines.focus_cell >= base)
  {
    /* leave of the edition mode */
    iupMatrixEditForceHidden(ih);

    /* move it to the same cell */
    ih->data->lines.focus_cell += count;
  }

  iupMatrixMemReAllocLines(ih, lines_num, lines_num+count, base);

  ih->data->lines.num += count;
  ih->data->need_calcsize = 1;

  if (base < lines_num)  /* If before the last line. */
    iMatrixUpdateLineAttributes(ih, base, count, 1);

  iupMatrixDraw(ih, 1);
  return 0;
}

int iupMatrixSetDelLinAttrib(Ihandle* ih, const char* value)
{
  int base, count, lines_num = ih->data->lines.num;

  if (!ih->handle)  /* do not store the action before map */
    return 0;

  if (!iMatrixGetStartEnd(value, &base, &count, lines_num, 1))
    return 0;

  /* if the focus cell is after the removed area */
  if (ih->data->lines.focus_cell >= base)
  {
    /* leave of the edition mode */
    iupMatrixEditForceHidden(ih);

    /* if the focus cell is inside the removed area */
    if (ih->data->lines.focus_cell <= base+count-1)
      ih->data->lines.focus_cell = base;   /* move it to the first existant cell */
    else
      ih->data->lines.focus_cell -= count; /* move it to the same cell */
  }

  iupMatrixMemReAllocLines(ih, lines_num, lines_num-count, base);

  ih->data->lines.num -= count;
  ih->data->need_calcsize = 1;

  if (ih->data->lines.focus_cell >= ih->data->lines.num)
    ih->data->lines.focus_cell = ih->data->lines.num-1;
  if (ih->data->lines.focus_cell <= 0)
    ih->data->lines.focus_cell = 1;

  if (base < lines_num)  /* If before the last line. (always true when deleting) */
    iMatrixUpdateLineAttributes(ih, base, count, 0);

  iupMatrixDraw(ih, 1);
  return 0;
}

int iupMatrixSetAddColAttrib(Ihandle* ih, const char* value)
{
  int base, count, columns_num = ih->data->columns.num;

  if (!ih->handle)  /* do not store the action before map */
    return 0;

  if (!iMatrixGetStartEnd(value, &base, &count, columns_num, 0))
    return 0;

  /* if the focus cell is after the inserted area */
  if (ih->data->columns.focus_cell >= base)
  {
    /* leave the edition mode */
    iupMatrixEditForceHidden(ih);

    /* move it to the same cell */
    ih->data->columns.focus_cell += count;
  }

  iupMatrixMemReAllocColumns(ih, columns_num, columns_num+count, base);

  ih->data->columns.num += count;
  ih->data->need_calcsize = 1;

  if (base < columns_num)  /* If before the last column. */
    iMatrixUpdateColumnAttributes(ih, base, count, 1);

  iupMatrixDraw(ih, 1);
  return 0;
}

int iupMatrixSetDelColAttrib(Ihandle* ih, const char* value)
{
  int base, count, columns_num = ih->data->columns.num;

  if (!ih->handle)  /* do not store the action before map */
    return 0;

  if (!iMatrixGetStartEnd(value, &base, &count, columns_num, 1))
    return 0;

  /* if the focus cell is after the removed area */
  if (ih->data->columns.focus_cell >= base)
  {
    /* leave the edition mode */
    iupMatrixEditForceHidden(ih);

    /* if the focus cell is inside the removed area */
    if (ih->data->columns.focus_cell <= base+count-1)
      ih->data->columns.focus_cell = base;    /* move it to the first existant cell */
    else
      ih->data->columns.focus_cell -= count;  /* move it to the same cell */
  }

  iupMatrixMemReAllocColumns(ih, columns_num, columns_num-count, base);

  ih->data->columns.num -= count;
  ih->data->need_calcsize = 1;

  if (ih->data->columns.focus_cell >= ih->data->columns.num)
    ih->data->columns.focus_cell = ih->data->columns.num-1;
  if (ih->data->columns.focus_cell <= 0)
    ih->data->columns.focus_cell = 1;

  if (base < columns_num)  /* If before the last column. (always true when deleting) */
    iMatrixUpdateColumnAttributes(ih, base, count, 0);

  iupMatrixDraw(ih, 1);
  return 0;
}

int iupMatrixSetNumLinAttrib(Ihandle* ih, const char* value)
{
  int num = 0;
  if (iupStrToInt(value, &num))
  {
    if (num < 0) num = 0;

    num++; /* add room for title line */

    /* can be set before map */
    if (ih->handle)
    {
      int base;  /* base is after the end */
      if (num >= ih->data->lines.num) /* add or alloc */
        base = ih->data->lines.num;   
      else
        base = num;
      iupMatrixMemReAllocLines(ih, ih->data->lines.num, num, base);  
    }

    ih->data->lines.num = num;  
    ih->data->need_calcsize = 1;

    if (ih->handle)
      iupMatrixDraw(ih, 1);
  }

  return 0;
}

int iupMatrixSetNumColAttrib(Ihandle* ih, const char* value)
{
  int num = 0;
  if (iupStrToInt(value, &num))
  {
    if (num < 0) num = 0;

    num++; /* add room for title column */

    /* can be set before map */
    if (ih->handle)
    {
      int base;  /* base is after the end */
      if (num >= ih->data->columns.num) /* add or alloc */
        base = ih->data->columns.num;
      else
        base = num;
      iupMatrixMemReAllocColumns(ih, ih->data->columns.num, num, base);
    }

    ih->data->columns.num = num;
    ih->data->need_calcsize = 1;

    if (ih->handle)
      iupMatrixDraw(ih, 1);
  }

  return 0;
}