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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
|
/** \file
* \brief iupmatrix control
* cell selection
*
* See Copyright Notice in "iup.h"
*/
/**************************************************************************/
/* Functions to cell selection (mark) */
/**************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <memory.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_mark.h"
#include "iupmat_getset.h"
#include "iupmat_draw.h"
static void iMatrixMarkLinSet(Ihandle* ih, int lin, int mark)
{
/* called when MARKMODE=LIN */
if (mark==-1)
mark = !(ih->data->lines.flags[lin] & IUPMAT_MARK);
if (mark)
ih->data->lines.flags[lin] |= IUPMAT_MARK;
else
ih->data->lines.flags[lin] &= ~IUPMAT_MARK;
}
static void iMatrixMarkColSet(Ihandle* ih, int col, int mark)
{
/* called when MARKMODE=COL */
if (mark==-1)
mark = !(ih->data->columns.flags[col] & IUPMAT_MARK);
if (mark)
ih->data->columns.flags[col] |= IUPMAT_MARK;
else
ih->data->columns.flags[col] &= ~IUPMAT_MARK;
}
static void iMatrixMarkCellSet(Ihandle* ih, int lin, int col, int mark, IFniii markedit_cb, IFnii mark_cb, char* str)
{
/* called only when MARKMODE=CELL */
if (mark == -1)
mark = !iupMatrixMarkCellGet(ih, lin, col, mark_cb, str);
if (ih->data->callback_mode)
{
if (markedit_cb)
markedit_cb(ih, lin, col, mark);
else
{
sprintf(str, "MARK%d:%d", lin, col);
if (mark)
iupAttribSetStr(ih, str, "1");
else
iupAttribSetStr(ih, str, NULL);
}
}
else
{
if (mark)
ih->data->cells[lin][col].flags |= IUPMAT_MARK;
else
ih->data->cells[lin][col].flags &= ~IUPMAT_MARK;
}
}
int iupMatrixMarkCellGet(Ihandle* ih, int lin, int col, IFnii mark_cb, char* str)
{
/* called independent from MARKMODE */
if (ih->data->mark_mode == IMAT_MARK_NO)
return 0;
if (ih->data->mark_mode == IMAT_MARK_CELL)
{
if (ih->data->callback_mode)
{
if (mark_cb)
return mark_cb(ih, lin, col);
else
{
int mark = 0;
char* value;
sprintf(str, "MARK%d:%d", lin, col);
value = iupAttribGet(ih, str);
iupStrToInt(value, &mark);
return mark;
}
}
else
return ih->data->cells[lin][col].flags & IUPMAT_MARK;
}
else
{
if (ih->data->lines.flags[lin] & IUPMAT_MARK || ih->data->columns.flags[col] & IUPMAT_MARK)
return 1;
else
return 0;
}
}
static void iMatrixMarkItem(Ihandle* ih, int lin1, int col1, int mark, IFniii markedit_cb, IFnii mark_cb, char* str)
{
int lin, col;
if (ih->data->mark_full1 == IMAT_PROCESS_LIN)
{
if (ih->data->mark_mode == IMAT_MARK_CELL)
{
for (col = 1; col < ih->data->columns.num; col++)
iMatrixMarkCellSet(ih, lin1, col, mark, markedit_cb, mark_cb, str);
}
else
{
iMatrixMarkLinSet(ih, lin1, mark);
iupMatrixDrawLineTitle(ih, lin1, lin1);
}
iupMatrixDrawCells(ih, lin1, 1, lin1, ih->data->columns.num-1);
}
else if (ih->data->mark_full1 == IMAT_PROCESS_COL)
{
if (ih->data->mark_mode == IMAT_MARK_CELL)
{
for(lin = 1; lin < ih->data->lines.num; lin++)
iMatrixMarkCellSet(ih, lin, col1, mark, markedit_cb, mark_cb, str);
}
else
{
iMatrixMarkColSet(ih, col1, mark);
iupMatrixDrawColumnTitle(ih, col1, col1);
}
iupMatrixDrawCells(ih, 1, col1, ih->data->lines.num-1, col1);
}
else if (ih->data->mark_mode == IMAT_MARK_CELL)
{
iMatrixMarkCellSet(ih, lin1, col1, mark, markedit_cb, mark_cb, str);
iupMatrixDrawCells(ih, lin1, col1, lin1, col1);
}
}
static void iMatrixMarkBlock(Ihandle* ih, int lin1, int col1, int lin2, int col2, int mark, IFniii markedit_cb, IFnii mark_cb, char* str)
{
int lin, col;
if (lin1 > lin2) {int t = lin1; lin1 = lin2; lin2 = t;}
if (col1 > col2) {int t = col1; col1 = col2; col2 = t;}
if (ih->data->mark_full1 == IMAT_PROCESS_LIN)
{
for(lin=lin1; lin<=lin2; lin++)
iMatrixMarkItem(ih, lin, 0, mark, markedit_cb, mark_cb, str);
}
else if(ih->data->mark_full1 == IMAT_PROCESS_COL)
{
for(col=col1; col<=col2; col++)
iMatrixMarkItem(ih, 0, col, mark, markedit_cb, mark_cb, str);
}
else if (ih->data->mark_mode == IMAT_MARK_CELL)
{
for(lin=lin1; lin<=lin2; lin++)
{
for(col=col1; col<=col2; col++)
iMatrixMarkItem(ih, lin, col, mark, markedit_cb, mark_cb, str);
}
}
}
void iupMatrixMarkMouseBlock(Ihandle* ih, int lin2, int col2)
{
/* called only when "shift" is pressed and MARKMULTIPLE=YES */
IFniii markedit_cb = NULL;
IFnii mark_cb = NULL;
char str[100];
iupMatrixPrepareDrawData(ih);
ih->data->mark_full2 = 0;
if (lin2 == 0 && col2 == 0)
return;
/* If it was pointing for a column title... */
else if (lin2 == 0)
{
if ((ih->data->mark_mode == IMAT_MARK_CELL && ih->data->mark_multiple) ||
ih->data->mark_mode & IMAT_MARK_COL)
ih->data->mark_full2 = IMAT_PROCESS_COL;
}
/* If it was pointing for a line title... */
else if (col2 == 0)
{
if ((ih->data->mark_mode == IMAT_MARK_CELL && ih->data->mark_multiple) ||
ih->data->mark_mode & IMAT_MARK_LIN)
ih->data->mark_full2 = IMAT_PROCESS_LIN;
}
/* if new block is not the same, does nothing */
if (ih->data->mark_full1 != ih->data->mark_full2)
return;
if (ih->data->mark_mode == IMAT_MARK_CELL && ih->data->callback_mode)
{
markedit_cb = (IFniii)IupGetCallback(ih, "MARKEDIT_CB");
mark_cb = (IFnii)IupGetCallback(ih, "MARK_CB");
}
/* Unmark previous block */
if (ih->data->mark_lin1 != -1 && ih->data->mark_lin2 != -1 &&
ih->data->mark_col1 != -1 && ih->data->mark_col2 != -1)
iMatrixMarkBlock(ih, ih->data->mark_lin1, ih->data->mark_col1, ih->data->mark_lin2, ih->data->mark_col2, 0, markedit_cb, mark_cb, str);
ih->data->mark_lin2 = lin2;
ih->data->mark_col2 = col2;
/* Unmark new block */
iMatrixMarkBlock(ih, ih->data->mark_lin1, ih->data->mark_col1, ih->data->mark_lin2, ih->data->mark_col2, 1, markedit_cb, mark_cb, str);
}
void iupMatrixMarkMouseReset(Ihandle* ih)
{
ih->data->mark_lin1 = -1;
ih->data->mark_col1 = -1;
ih->data->mark_lin2 = -1;
ih->data->mark_col2 = -1;
ih->data->mark_full1 = 0;
ih->data->mark_full2 = 0;
}
void iupMatrixMarkMouseItem(Ihandle* ih, int ctrl, int lin1, int col1)
{
int mark = 1, mark_full_all, lin, col;
IFniii markedit_cb = NULL;
IFnii mark_cb = NULL;
char str[100];
iupMatrixMarkMouseReset(ih);
iupMatrixPrepareDrawData(ih);
if (!ih->data->mark_multiple || ih->data->mark_continuous || !ctrl)
{
iupMatrixMarkClearAll(ih, 1);
iupMatrixDraw(ih, 0);
}
else
mark = -1; /* toggle mark state */
ih->data->mark_full1 = 0;
mark_full_all = 0;
if (lin1 == 0 && col1 == 0)
{
if ((ih->data->mark_mode == IMAT_MARK_CELL && ih->data->mark_multiple) ||
ih->data->mark_mode == IMAT_MARK_COL ||
ih->data->mark_mode == IMAT_MARK_LIN)
mark_full_all = 1;
}
/* If it was pointing for a column title... */
else if (lin1 == 0)
{
if ((ih->data->mark_mode == IMAT_MARK_CELL && ih->data->mark_multiple) ||
ih->data->mark_mode & IMAT_MARK_COL)
ih->data->mark_full1 = IMAT_PROCESS_COL;
}
/* If it was pointing for a line title... */
else if (col1 == 0)
{
if ((ih->data->mark_mode == IMAT_MARK_CELL && ih->data->mark_multiple) ||
ih->data->mark_mode & IMAT_MARK_LIN)
ih->data->mark_full1 = IMAT_PROCESS_LIN;
}
if (ih->data->mark_mode == IMAT_MARK_CELL && ih->data->callback_mode)
{
markedit_cb = (IFniii)IupGetCallback(ih, "MARKEDIT_CB");
mark_cb = (IFnii)IupGetCallback(ih, "MARK_CB");
}
if (mark_full_all)
{
if (ih->data->mark_mode == IMAT_MARK_CELL)
{
char str[100];
for (col = 1; col < ih->data->columns.num; col++)
{
for(lin = 1; lin < ih->data->lines.num; lin++)
iMatrixMarkCellSet(ih, lin, col, mark, markedit_cb, mark_cb, str);
}
}
else if (ih->data->mark_mode == IMAT_MARK_LIN)
{
for(lin = 1; lin < ih->data->lines.num; lin++)
iMatrixMarkLinSet(ih, lin, mark);
iupMatrixDrawLineTitle(ih, 1, ih->data->lines.num-1);
}
else if (ih->data->mark_mode == IMAT_MARK_COL)
{
for (col = 1; col < ih->data->columns.num; col++)
iMatrixMarkColSet(ih, col, mark);
iupMatrixDrawColumnTitle(ih, 1, ih->data->columns.num-1);
}
iupMatrixDrawCells(ih, 1, 1, ih->data->lines.num-1, ih->data->columns.num-1);
}
else
iMatrixMarkItem(ih, lin1, col1, mark, markedit_cb, mark_cb, str);
ih->data->mark_lin1 = lin1;
ih->data->mark_col1 = col1;
}
static void iMatrixMarkAllLinCol(ImatLinColData *p, int mark)
{
int i;
for(i = 1; i < p->num; i++)
{
if (mark)
p->flags[i] |= IUPMAT_MARK;
else
p->flags[i] &= ~IUPMAT_MARK;
}
}
void iupMatrixMarkClearAll(Ihandle* ih, int check)
{
/* "!check" is used to clear all marks independent from MARKMODE */
if (ih->data->mark_mode == IMAT_MARK_CELL || !check)
{
int lin, col;
IFniii markedit_cb = NULL;
char str[100];
if (check)
markedit_cb = (IFniii)IupGetCallback(ih, "MARKEDIT_CB");
for(lin = 1; lin < ih->data->lines.num; lin++)
{
for(col = 1; col < ih->data->columns.num; col++)
iMatrixMarkCellSet(ih, lin, col, 0, markedit_cb, NULL, str);
}
}
if (ih->data->mark_mode & IMAT_MARK_LIN || !check)
iMatrixMarkAllLinCol(&(ih->data->lines), 0);
if (ih->data->mark_mode & IMAT_MARK_COL || !check)
iMatrixMarkAllLinCol(&(ih->data->columns), 0);
}
int iupMatrixColumnIsMarked(Ihandle* ih, int col)
{
if (col == 0 || /* Line titles are never marked... */
!(ih->data->mark_mode & IMAT_MARK_COL))
return 0;
return ih->data->columns.flags[col] & IUPMAT_MARK;
}
int iupMatrixLineIsMarked(Ihandle* ih, int lin)
{
if (lin == 0 || /* Column titles are never marked... */
!(ih->data->mark_mode & IMAT_MARK_LIN))
return 0;
return ih->data->lines.flags[lin] & IUPMAT_MARK;
}
int iupMatrixSetMarkedAttrib(Ihandle* ih, const char* value)
{
int lin, col, mark;
char str[100];
IFniii markedit_cb;
if (ih->data->mark_mode == IMAT_MARK_NO)
return 0;
if (!value)
iupMatrixMarkClearAll(ih, 1);
else if (*value == 'C' || *value == 'c') /* columns */
{
if (ih->data->mark_mode == IMAT_MARK_LIN)
return 0;
value++; /* skip C mark */
if ((int)strlen(value) != ih->data->columns.num-1)
return 0;
markedit_cb = (IFniii)IupGetCallback(ih, "MARKEDIT_CB");
for(col = 1; col < ih->data->columns.num; col++)
{
if (*value++ == '1')
mark = 1;
else
mark = 0;
/* mark all the cells for that column */
if (ih->data->mark_mode == IMAT_MARK_CELL)
{
for(lin = 1; lin < ih->data->lines.num; lin++)
iMatrixMarkCellSet(ih, lin, col, mark, markedit_cb, NULL, str);
}
else
iMatrixMarkColSet(ih, col, mark);
}
if (ih->data->mark_mode & IMAT_MARK_LIN)
iMatrixMarkAllLinCol(&(ih->data->lines), 0);
}
else if (*value == 'L' || *value == 'l') /* lines */
{
if (ih->data->mark_mode == IMAT_MARK_COL)
return 0;
value++; /* skip L mark */
if ((int)strlen(value) != ih->data->lines.num-1)
return 0;
markedit_cb = (IFniii)IupGetCallback(ih, "MARKEDIT_CB");
for(lin = 1; lin < ih->data->lines.num; lin++)
{
if (*value++ == '1')
mark = 1;
else
mark = 0;
/* Mark all the cells for that line */
if (ih->data->mark_mode == IMAT_MARK_CELL)
{
for(col = 1; col < ih->data->columns.num; col++)
iMatrixMarkCellSet(ih, lin, col, mark, markedit_cb, NULL, str);
}
else
iMatrixMarkLinSet(ih, lin, mark);
}
if (ih->data->mark_mode & IMAT_MARK_COL)
iMatrixMarkAllLinCol(&(ih->data->columns), 0);
}
else if (ih->data->mark_mode == IMAT_MARK_CELL) /* cells */
{
if ((int)strlen(value) != (ih->data->lines.num-1)*(ih->data->columns.num-1))
return 0;
markedit_cb = (IFniii)IupGetCallback(ih, "MARKEDIT_CB");
for(lin = 1; lin < ih->data->lines.num; lin++)
{
for(col = 1; col < ih->data->columns.num; col++)
{
if (*value++ == '1')
mark = 1;
else
mark = 0;
iMatrixMarkCellSet(ih, lin, col, mark, markedit_cb, NULL, str);
}
}
}
if (ih->handle)
iupMatrixDraw(ih, 1);
return 0;
}
char* iupMatrixGetMarkedAttrib(Ihandle* ih)
{
int lin, col, size;
IFnii mark_cb;
char str[100];
char* p, *value = NULL;
int exist_mark = 0; /* Show if there is someone marked */
if (ih->data->mark_mode == IMAT_MARK_NO)
return NULL;
mark_cb = (IFnii)IupGetCallback(ih, "MARK_CB");
if (ih->data->mark_mode == IMAT_MARK_CELL)
{
size = (ih->data->lines.num-1) * (ih->data->columns.num-1) + 1;
value = iupStrGetMemory(size);
p = value;
for(lin = 1; lin < ih->data->lines.num; lin++)
{
for(col = 1; col < ih->data->columns.num; col++)
{
if (iupMatrixMarkCellGet(ih, lin, col, mark_cb, str))
{
exist_mark = 1;
*p++ = '1';
}
else
*p++ = '0';
}
}
*p = 0;
}
else
{
int marked_lines = 0, marked_cols = 0;
if (ih->data->mark_mode == IMAT_MARK_LINCOL) /* must find which format to return */
{
/* look for a marked column */
for(col = 1; col < ih->data->columns.num; col++)
{
if (ih->data->columns.flags[col] & IUPMAT_MARK)
{
marked_cols = 1; /* at least one column is marked */
break;
}
}
if (!marked_cols)
marked_lines = 1;
}
else if (ih->data->mark_mode == IMAT_MARK_LIN)
marked_lines = 1;
else if (ih->data->mark_mode == IMAT_MARK_COL)
marked_cols = 1;
if (marked_lines)
{
size = 1 + (ih->data->lines.num-1) + 1;
value = iupStrGetMemory(size);
p = value;
*p++ = 'L';
for(lin = 1; lin < ih->data->lines.num; lin++)
{
if (ih->data->lines.flags[lin] & IUPMAT_MARK)
{
exist_mark = 1;
*p++ = '1';
}
else
*p++ = '0';
}
*p = 0;
}
else if (marked_cols)
{
size = 1 + (ih->data->columns.num-1) + 1;
value = iupStrGetMemory(size);
p = value;
*p++ = 'C';
for(col = 1; col < ih->data->columns.num; col++)
{
if (ih->data->columns.flags[col] & IUPMAT_MARK)
{
exist_mark = 1;
*p++ = '1';
}
else
*p++ = '0';
}
*p = 0;
}
}
return exist_mark? value: NULL;
}
int iupMatrixSetMarkAttrib(Ihandle* ih, const char* name_id, const char* value)
{
int lin = 0, col = 0;
if (ih->data->mark_mode == IMAT_MARK_NO)
return 0;
if (iupStrToIntInt(name_id, &lin, &col, ':') == 2)
{
if (!iupMatrixCheckCellPos(ih, lin, col))
return 0;
if (ih->data->mark_mode == IMAT_MARK_CELL)
{
int mark;
if (lin == 0 || col == 0) /* title can NOT have a mark */
return 0;
mark = iupStrBoolean(value);
if (ih->data->callback_mode)
{
IFniii markedit_cb = (IFniii)IupGetCallback(ih, "MARKEDIT_CB");
if (markedit_cb)
markedit_cb(ih, lin, col, mark);
else if (mark)
return 1; /* store the attribute */
}
else
{
if (mark)
ih->data->cells[lin][col].flags |= IUPMAT_MARK;
else
ih->data->cells[lin][col].flags &= ~IUPMAT_MARK;
}
if (ih->handle)
{
/* This assumes that the matrix has been draw completely previously */
iupMatrixPrepareDrawData(ih);
iupMatrixDrawCells(ih, lin, col, lin, col);
}
}
else
{
int mark = iupStrBoolean(value);
if (ih->data->mark_mode & IMAT_MARK_LIN && lin!=0)
{
if (mark)
ih->data->lines.flags[lin] |= IUPMAT_MARK;
else
ih->data->lines.flags[lin] &= ~IUPMAT_MARK;
}
if (ih->data->mark_mode & IMAT_MARK_COL && col!=0)
{
if (mark)
ih->data->columns.flags[col] |= IUPMAT_MARK;
else
ih->data->columns.flags[col] &= ~IUPMAT_MARK;
}
if (ih->handle)
{
/* This assumes that the matrix has been draw completely previously */
iupMatrixPrepareDrawData(ih);
iupMatrixDrawCells(ih, lin, col, lin, col);
}
}
}
return 0;
}
char* iupMatrixGetMarkAttrib(Ihandle* ih, const char* name_id)
{
int lin = 0, col = 0;
if (ih->data->mark_mode == IMAT_MARK_NO)
return "0";
if (iupStrToIntInt(name_id, &lin, &col, ':') == 2)
{
if (!iupMatrixCheckCellPos(ih, lin, col))
return NULL;
if (ih->data->mark_mode == IMAT_MARK_CELL)
{
if (lin == 0 || col == 0) /* title can NOT have a mark */
return NULL;
if (ih->data->callback_mode)
{
IFnii mark_cb = (IFnii)IupGetCallback(ih, "MARK_CB");
if (mark_cb)
{
if (mark_cb(ih, lin, col))
return "1";
else
return "0";
}
else
return NULL; /* let check the hash table */
}
else
{
if (ih->data->cells[lin][col].flags & IUPMAT_MARK)
return "1";
else
return "0";
}
}
else
{
if (ih->data->mark_mode & IMAT_MARK_LIN && lin!=0 && ih->data->lines.flags[lin] & IUPMAT_MARK)
return "1";
if (ih->data->mark_mode & IMAT_MARK_COL && col!=0 && ih->data->columns.flags[col] & IUPMAT_MARK)
return "1";
return "0";
}
}
return NULL;
}
|