summaryrefslogtreecommitdiff
path: root/iup/srccontrols/matrix/iupmat_scroll.c
blob: 24bbef21fc8d2c0fc4908709d206e7d384af6478 (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
 * scrolling
 *
 * See Copyright Notice in "iup.h"
 */

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

#include <cd.h>

#include "iup_object.h"
#include "iup_stdcontrols.h"

#include "iupmat_def.h"
#include "iupmat_scroll.h"
#include "iupmat_focus.h"
#include "iupmat_aux.h"
#include "iupmat_edit.h"
#include "iupmat_draw.h"


/* Macros used by "dir" parameter of the iMatrixScrollLine and iMatrixScrollColumn functions */
#define IMAT_SCROLL_LEFT    0
#define IMAT_SCROLL_RIGHT   1
#define IMAT_SCROLL_UP      2
#define IMAT_SCROLL_DOWN    3


/**************************************************************************/
/* Private functions                                                      */
/**************************************************************************/


static int iMatrixScrollIsFullVisibleLast(ImatLinColData *p)
{
  int i, sum = 0;

  for(i = p->first; i <= p->last; i++)
    sum  += p->sizes[i];

  if (sum > p->visible_size)
    return 0;
  else
    return 1;
}

/* Scroll columns/lines in the left/top side of the matriz until the last column/line is FULLY visible.
   -> m : Define the mode of operation: lines or columns [IMAT_PROCESS_LIN|IMAT_PROCESS_COL] */
static void iMatrixScrollToVisible(Ihandle* ih, int m, int index)
{
  ImatLinColData* p;

  if (m == IMAT_PROCESS_LIN)
    p = &(ih->data->lines);
  else
    p = &(ih->data->columns);

  if (index < p->first)
  {
    p->first = index;
    return;
  }
  else if (index > p->last)
  {
    /* Increment the first column/line until the index is visible */
    while(index > p->last && p->last != (p->num - 1))
    {
      p->first++;
      iupMatrixAuxUpdateLast(p);
    } 
  }

  if (index == p->last)
  {
    /* must increment util the last is fully visible */
    while(index == p->last && p->last != (p->num - 1) && !iMatrixScrollIsFullVisibleLast(p))
    {
      p->first++;
      iupMatrixAuxUpdateLast(p);
    } 
  }
}

/* Callback to report to the user which visualization area of
   the matrix changed. */
static void iMatrixScrollCallScrollTopCb(Ihandle* ih)
{
  IFnii cb = (IFnii)IupGetCallback(ih, "SCROLLTOP_CB");
  if (cb) 
    cb(ih, ih->data->lines.first, ih->data->columns.first);
}

static int iMatrixScrollGetNextNonEmpty(Ihandle* ih, int m, int index)
{
  ImatLinColData* p;

  if (m == IMAT_PROCESS_LIN)
    p = &(ih->data->lines);
  else
    p = &(ih->data->columns);

  while(index < p->num && p->sizes[index] == 0)
    index++;

  if (index > p->num-1)
  {
    if (p->num == 1)
      return 1;
    else
      return p->num-1;
  }
  else
    return index;
}

static int iMatrixScrollGetPrevNonEmpty(Ihandle* ih, int m, int index)
{
  ImatLinColData* p;

  if (m == IMAT_PROCESS_LIN)
    p = &(ih->data->lines);
  else
    p = &(ih->data->columns);

  while(index > 0 && p->sizes[index] == 0)
    index--;

  if (index < 1)
    return 1;
  else
    return index;
}

static void iMatrixScrollSetFocusScrollToVisible(Ihandle* ih, int m, int index)
{
  if (m == IMAT_PROCESS_COL)
    iupMatrixFocusSet(ih, ih->data->lines.focus_cell, index);
  else
    iupMatrixFocusSet(ih, index, ih->data->columns.focus_cell);

  /* set for both because current focus maybe hidden */
  iMatrixScrollToVisible(ih, IMAT_PROCESS_COL, ih->data->columns.focus_cell);
  iMatrixScrollToVisible(ih, IMAT_PROCESS_LIN, ih->data->lines.focus_cell);
}

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


/* Move using the cells of matrix.
   Receive as a parameter a pointer to a function that will make the work,
   in fact. This is done to avoid a test to each of the manipulation cursor
   functions, verifying if it is necessary to call or not the scroll
   callback. This is only done here.
   -> func - pointer to the function that will make the movement
   -> mode - parameter passed to func, specify if the movement request is of
             the scrollbar or the keyboard
   -> pos  - parameter passed to func, that will be the handle position function
             of the scrollbar, returning the scrollbar thumb position...
             if func is other function, this parameter will be ignored
   -> m    - parameter passed to func, specify which is the mode of operation:
             lines or columns [IMAT_PROCESS_LIN|IMAT_PROCESS_COL]
*/
void iupMatrixScrollMoveCursor(iupMatrixScrollMoveF func, Ihandle* ih, int mode, float pos, int m)
{
  int old_lines_first = ih->data->lines.first;
  int old_columns_first = ih->data->columns.first;

  iupMatrixEditForceHidden(ih);

  func(ih, mode, pos, m);

  if (ih->data->lines.first != old_lines_first || ih->data->columns.first != old_columns_first)
  {
    if (ih->data->columns.first != old_columns_first)
      iupMatrixAuxUpdateVisiblePos(ih, IMAT_PROCESS_COL);

    if (ih->data->lines.first != old_lines_first)
      iupMatrixAuxUpdateVisiblePos(ih, IMAT_PROCESS_LIN);

    iMatrixScrollCallScrollTopCb(ih);

    iupMatrixDraw(ih, 0);
  }
}

void iupMatrixScrollToVisible(Ihandle* ih, int lin, int col)
{
  int old_lines_first = ih->data->lines.first;
  int old_columns_first = ih->data->columns.first;

  iMatrixScrollToVisible(ih, IMAT_PROCESS_COL, col);
  iMatrixScrollToVisible(ih, IMAT_PROCESS_LIN, lin);

  if (ih->data->lines.first != old_lines_first || ih->data->columns.first != old_columns_first)
  {
    if (ih->data->columns.first != old_columns_first)
      iupMatrixAuxUpdateVisiblePos(ih, IMAT_PROCESS_COL);

    if (ih->data->lines.first != old_lines_first)
      iupMatrixAuxUpdateVisiblePos(ih, IMAT_PROCESS_LIN);

    iMatrixScrollCallScrollTopCb(ih);

    iupMatrixDraw(ih, 1);
  }
}

/* This function is called when the "home" key is pressed.
   In the first time, go to the beginning of the line.
   In the second time, go to the beginning of the page.
   In the third time, go to the beginning of the matrix.
   -> mode and pos : DO NOT USED.
*/
void iupMatrixScrollHome(Ihandle* ih, int unused_mode, float unused_pos, int unused_m)
{
  (void)unused_m;
  (void)unused_mode;
  (void)unused_pos;

  /* called only for mode==IMAT_SCROLLKEY */

  if(ih->data->homekeycount == 0)  /* go to the beginning of the line */
  {
    ih->data->columns.first = iMatrixScrollGetNextNonEmpty(ih, IMAT_PROCESS_COL, 1);
    iMatrixScrollSetFocusScrollToVisible(ih, IMAT_PROCESS_COL, ih->data->columns.first);
  }
  else if(ih->data->homekeycount == 1)   /* go to the beginning of the visible page */
  {
    iupMatrixFocusSet(ih, ih->data->lines.first, ih->data->columns.first);

    /* set for both because current focus maybe hidden */
    iMatrixScrollToVisible(ih, IMAT_PROCESS_COL, ih->data->columns.focus_cell);
    iMatrixScrollToVisible(ih, IMAT_PROCESS_LIN, ih->data->lines.focus_cell);
  }
  else if(ih->data->homekeycount == 2)   /* go to the beginning of the matrix 1:1 */
  {
    ih->data->columns.first = iMatrixScrollGetNextNonEmpty(ih, IMAT_PROCESS_COL, 1);
    ih->data->lines.first = iMatrixScrollGetNextNonEmpty(ih, IMAT_PROCESS_LIN, 1);

    iupMatrixFocusSet(ih, ih->data->lines.first, ih->data->columns.first);

    /* set for both because current focus maybe hidden */
    iMatrixScrollToVisible(ih, IMAT_PROCESS_COL, ih->data->columns.focus_cell);
    iMatrixScrollToVisible(ih, IMAT_PROCESS_LIN, ih->data->lines.focus_cell);
  }
}

/* This function is called when the "end" key is pressed.
   In the first time, go to the end of the line.
   In the second time, go to the end of the page.
   In the third time, go to the end of the matrix.
   -> mode and pos : DO NOT USED.
*/
void iupMatrixScrollEnd(Ihandle* ih, int unused_mode, float unused_pos, int unused_m)
{
  (void)unused_m;
  (void)unused_mode;
  (void)unused_pos;

  /* called only for mode==IMAT_SCROLLKEY */

  if(ih->data->endkeycount == 0)  /* go to the end of the line */
  {
    int last_col = iMatrixScrollGetPrevNonEmpty(ih, IMAT_PROCESS_COL, ih->data->columns.num-1);
    iMatrixScrollSetFocusScrollToVisible(ih, IMAT_PROCESS_COL, last_col);
  }
  else if(ih->data->endkeycount == 1)   /* go to the end of the visible page */
  {
    iupMatrixFocusSet(ih, ih->data->lines.last, ih->data->columns.last);

    /* set for both because current focus maybe hidden */
    iMatrixScrollToVisible(ih, IMAT_PROCESS_COL, ih->data->columns.focus_cell);
    iMatrixScrollToVisible(ih, IMAT_PROCESS_LIN, ih->data->lines.focus_cell);
  }
  else if(ih->data->endkeycount == 2)   /* go to the end of the matrix */
  {
    int last_col = iMatrixScrollGetPrevNonEmpty(ih, IMAT_PROCESS_COL, ih->data->columns.num-1);
    int last_lin = iMatrixScrollGetPrevNonEmpty(ih, IMAT_PROCESS_LIN, ih->data->lines.num-1);

    iupMatrixFocusSet(ih, last_lin, last_col);

    /* set for both because current focus maybe hidden */
    iMatrixScrollToVisible(ih, IMAT_PROCESS_COL, ih->data->columns.focus_cell);
    iMatrixScrollToVisible(ih, IMAT_PROCESS_LIN, ih->data->lines.focus_cell);
  }
}

/* This function is called to move a cell to the left or up.
   -> mode : indicate if the command was from the keyboard or the scrollbar. If scrollbar,
             do not change the focus.
   -> pos  : DO NOT USED
   -> m    : define the mode of operation: lines or columns [IMAT_PROCESS_LIN|IMAT_PROCESS_COL]
*/
void iupMatrixScrollLeftUp(Ihandle* ih, int mode, float pos, int m)
{
  ImatLinColData* p;
  (void)pos;

  if(m == IMAT_PROCESS_LIN)
    p = &(ih->data->lines);
  else
    p = &(ih->data->columns);

  if (mode == IMAT_SCROLLKEY)
  {
    int next = iMatrixScrollGetPrevNonEmpty(ih, m, p->focus_cell-1);
    iMatrixScrollSetFocusScrollToVisible(ih, m, next);
  }
  else  /* IMAT_SCROLLBAR */
  {
    p->first = iMatrixScrollGetPrevNonEmpty(ih, m, p->first-1);
  }
}

/* This function is called to move a cell to the right or down.
   -> mode : indicate if the command from the keyboard or the scrollbar. If scrollbar,
             do not change the focus.
   -> pos  : DO NOT USED
   -> m    : define the mode of operation: lines or columns [IMAT_PROCESS_LIN|IMAT_PROCESS_COL]
*/
void iupMatrixScrollRightDown(Ihandle* ih, int mode, float pos, int m)
{
  ImatLinColData* p;
  (void)pos;

  if(m == IMAT_PROCESS_LIN)
    p = &(ih->data->lines);
  else
    p = &(ih->data->columns);

  if (mode == IMAT_SCROLLKEY)
  {
    int next = iMatrixScrollGetNextNonEmpty(ih, m, p->focus_cell+1);
    iMatrixScrollSetFocusScrollToVisible(ih, m, next);
  }
  else  /* IMAT_SCROLLBAR */
  {
    p->first = iMatrixScrollGetNextNonEmpty(ih, m, p->first+1);
  }
}

/* This function is called to move a page to the left or up.
   -> mode : indicate if the command was from the keyboard or the scrollbar. If scrollbar,
             do not change the focus.
   -> pos  : DO NOT USED
   -> m    : define the mode of operation: lines (PgLeft) or columns (PgUp) [IMAT_PROCESS_LIN|IMAT_PROCESS_COL]
*/
void iupMatrixScrollPgLeftUp(Ihandle* ih, int mode, float pos, int m)
{
  ImatLinColData* p;
  (void)pos;

  if(m == IMAT_PROCESS_LIN)
    p = &(ih->data->lines);
  else
    p = &(ih->data->columns);

  if (mode == IMAT_SCROLLKEY)
  {
    int next = iMatrixScrollGetPrevNonEmpty(ih, m, p->focus_cell - (p->last - p->first));
    iMatrixScrollSetFocusScrollToVisible(ih, m, next);
  }
  else  /* IMAT_SCROLLBAR */
  {
    p->first = iMatrixScrollGetPrevNonEmpty(ih, m, p->first - (p->last - p->first));
  }
}

/* This function is called to move a page to the right or down.
   -> mode : indicate if the command was from the keyboard or the scrollbar. If scrollbar,
             do not change the focus.
   -> pos  : DO NOT USED
   -> m    : define the mode of operation: lines (PgDown) or columns (PgRight) [IMAT_PROCESS_LIN|IMAT_PROCESS_COL]
*/
void iupMatrixScrollPgRightDown(Ihandle* ih, int mode, float pos, int m)
{
  ImatLinColData* p;
  (void)pos;

  if(m == IMAT_PROCESS_LIN)
    p = &(ih->data->lines);
  else
    p = &(ih->data->columns);

  if (mode == IMAT_SCROLLKEY)
  {
    int next = iMatrixScrollGetNextNonEmpty(ih, IMAT_PROCESS_COL, p->focus_cell + (p->last - p->first));
    iMatrixScrollSetFocusScrollToVisible(ih, m, next);
  }
  else  /* IMAT_SCROLLBAR */
  {
    p->first = iMatrixScrollGetPrevNonEmpty(ih, m, p->first + (p->last - p->first));
  }
}

void iupMatrixScrollCr(Ihandle* ih, int unused_mode, float unused_pos, int unused_m)
{
  int oldlin = ih->data->lines.focus_cell;
  int oldcol = ih->data->columns.focus_cell;
  (void)unused_m;
  (void)unused_mode;
  (void)unused_pos;

  /* called only for mode==IMAT_SCROLLKEY */

  /* try the normal processing of next cell down */
  iupMatrixScrollRightDown(ih, IMAT_SCROLLKEY, 0, IMAT_PROCESS_LIN);

  if(ih->data->lines.focus_cell == oldlin && ih->data->columns.focus_cell == oldcol)
  {
    /* If focus was not changed, it was because it is in the last line of the column.
       Go to the next column of the same line. */
    iupMatrixScrollRightDown(ih, IMAT_SCROLLKEY, 0, IMAT_PROCESS_COL);
  }
}

/* This function is called when a drag is performed in the scrollbar.
   -> x    : scrollbar thumb position, value between 0 and 1
   -> mode : DO NOT USED
   -> m    : define the mode of operation: lines or columns [IMAT_PROCESS_LIN|IMAT_PROCESS_COL]
*/
void iupMatrixScrollPos(Ihandle* ih, int mode, float pos, int m)
{
  int scroll_pos, index, vp;
  float d;
  ImatLinColData* p;
  (void)mode;

  if (m == IMAT_PROCESS_LIN)
  {
    p = &(ih->data->lines);
    d = IupGetFloat(ih, "DY");
  }
  else
  {
    p = &(ih->data->columns);
    d = IupGetFloat(ih, "DX");
  }

  if (p->num == 1)
  {
    p->first = 1;
    return;
  }

  scroll_pos = (int)(pos * p->total_size + 0.5);

  vp = 0;
  for(index = 1; index < p->num; index++)
  {
    vp += p->sizes[index];
    if (vp > scroll_pos)
     break;
  }

  if (index == p->num)
  {
    if (p->num == 1)
      index = 1;
    else
      index = p->num-1;
  }

  p->first = index;
}

int iupMatrixScroll_CB(Ihandle* ih, int action, float x, float y)
{
  if (!iupMatrixIsValid(ih, 0))
    return IUP_DEFAULT;

  switch(action)
  {
    case IUP_SBUP      : iupMatrixScrollUp(ih);       break;
    case IUP_SBDN      : iupMatrixScrollDown(ih);     break;
    case IUP_SBPGUP    : iupMatrixScrollPgUp(ih);     break;
    case IUP_SBPGDN    : iupMatrixScrollPgDown(ih);   break;
    case IUP_SBRIGHT   : iupMatrixScrollRight(ih);    break;
    case IUP_SBLEFT    : iupMatrixScrollLeft(ih);     break;
    case IUP_SBPGRIGHT : iupMatrixScrollPgRight(ih);  break;
    case IUP_SBPGLEFT  : iupMatrixScrollPgLeft(ih);   break;
    case IUP_SBPOSV    : iupMatrixScrollPosVer(ih,y); break;
    case IUP_SBPOSH    : iupMatrixScrollPosHor(ih,x); break;
    case IUP_SBDRAGV   : iupMatrixScrollPosVer(ih,y); break;
    case IUP_SBDRAGH   : iupMatrixScrollPosHor(ih,x); break;
  }

  iupMatrixDrawUpdate(ih);

  return IUP_DEFAULT;
}