summaryrefslogtreecommitdiff
path: root/iup/srccontrols/matrix/iupmat_colres.c
blob: 79f389e30ef84fbe1f95fc68bdf5898f0c233f8b (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
/** \file
 * \brief iupmatrix column resize
 *
 * See Copyright Notice in "iup.h"
 */

/**************************************************************************/
/* Interactive Column Resize Functions and WIDTH/HEIGHT change            */
/**************************************************************************/

#include <stdio.h>
#include <stdlib.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_colres.h"
#include "iupmat_draw.h"


#define IMAT_COLRES_TOL       3
#define IMAT_RESIZE_COLOR  0x666666L


/* Verify if the mouse is in the intersection between two of column titles,
   if so the resize is started */
int iupMatrixColResStart(Ihandle* ih, int x, int y)
{
  if (ih->data->lines.sizes[0] && 
      y < ih->data->lines.sizes[0] && 
      iupAttribGetBoolean(ih, "RESIZEMATRIX"))
  {
    int x_col, col;
   
    /* Check if is the column of titles */
    x_col = ih->data->columns.sizes[0];
    if (abs(x_col-x) < IMAT_COLRES_TOL)
    {
      ih->data->colres_drag_col_start_x = x;
      ih->data->colres_dragging =  1;
      ih->data->colres_drag_col_last_x = -1;
      ih->data->colres_drag_col = 0;
      return 1;
    }
    else
    {
      /* find the column */
      x_col -= ih->data->columns.first_offset;
      for(col = ih->data->columns.first; col <= ih->data->columns.last; col++)
      {
        x_col += ih->data->columns.sizes[col];
        if (abs(x_col-x) < IMAT_COLRES_TOL)
        {
          ih->data->colres_drag_col_start_x = x;
          ih->data->colres_dragging =  1;
          ih->data->colres_drag_col_last_x = -1;
          ih->data->colres_drag_col = col;
          return 1;
        }
      }
    }
  }
  return 0;
}

void iupMatrixColResFinish(Ihandle* ih, int x)
{
  char str[100];
  int delta = x - ih->data->colres_drag_col_start_x;
  int width = ih->data->columns.sizes[ih->data->colres_drag_col] + delta;
  if (width < 0)
    width = 0;

  /* delete feedback */
  if (ih->data->colres_drag_col_last_x != -1)
  {
    int y1 = ih->data->lines.sizes[0];  /* from the bottom of the line of titles */
    int y2 = ih->data->h-1;             /* to the bottom of the matrix */

    cdCanvasWriteMode(ih->data->cdcanvas, CD_XOR);
    cdCanvasForeground(ih->data->cdcanvas, IMAT_RESIZE_COLOR);               
    cdCanvasLine(ih->data->cdcanvas, ih->data->colres_drag_col_last_x, iupMATRIX_INVERTYAXIS(ih, y1), 
                                     ih->data->colres_drag_col_last_x, iupMATRIX_INVERTYAXIS(ih, y2));
    cdCanvasWriteMode(ih->data->cdcanvas, CD_REPLACE);
  }

  ih->data->colres_dragging = 0;

  sprintf(str, "RASTERWIDTH%d", ih->data->colres_drag_col);
  iupAttribSetInt(ih, str, width-IMAT_PADDING_W-IMAT_FRAME_W);
  sprintf(str, "WIDTH%d", ih->data->colres_drag_col);
  iupAttribSetStr(ih, str, NULL);

  ih->data->need_calcsize = 1;
  iupMatrixDraw(ih, 0);
}

/* Change the column width interactively, just change the line in the screen.
   When the user finishes the drag, the iupMatrixColResFinish function is called
   to truly change the column width. */
void iupMatrixColResMove(Ihandle* ih, int x)
{
  int y1, y2;

  int delta = x - ih->data->colres_drag_col_start_x;
  int width = ih->data->columns.sizes[ih->data->colres_drag_col] + delta;
  if (width < 0)
    return;

  y1 = ih->data->lines.sizes[0];  /* from the bottom of the line of titles */
  y2 = ih->data->h-1;             /* to the bottom of the matrix */

  cdCanvasWriteMode(ih->data->cdcanvas, CD_XOR);
  cdCanvasForeground(ih->data->cdcanvas, IMAT_RESIZE_COLOR);

  /* If it is not the first time, move old line */
  if (ih->data->colres_drag_col_last_x != -1)
  {
    cdCanvasLine(ih->data->cdcanvas, ih->data->colres_drag_col_last_x, iupMATRIX_INVERTYAXIS(ih, y1), 
                                     ih->data->colres_drag_col_last_x, iupMATRIX_INVERTYAXIS(ih, y2));
  }

  cdCanvasLine(ih->data->cdcanvas, x, iupMATRIX_INVERTYAXIS(ih, y1), 
                                   x, iupMATRIX_INVERTYAXIS(ih, y2));

  ih->data->colres_drag_col_last_x = x;
  cdCanvasWriteMode(ih->data->cdcanvas, CD_REPLACE);
}


static void iMatrixColResResetMatrixCursor(Ihandle* ih)
{
  char *cursor = iupAttribGet(ih, "_IUPMAT_CURSOR");
  if (cursor)
  {
    IupStoreAttribute(ih, "CURSOR", cursor);
    iupAttribSetStr(ih, "_IUPMAT_CURSOR", NULL);
  }
}

/* Change the cursor when it passes over a group of the column titles. */
void iupMatrixColResCheckChangeCursor(Ihandle* ih, int x, int y)
{
  if(ih->data->lines.sizes[0] && 
     y < ih->data->lines.sizes[0] && 
     iupAttribGetBoolean(ih, "RESIZEMATRIX"))
  {
    /* It is in the column titles area and the resize mode is on */
    int found = 0, x_col, col;

    x_col = ih->data->columns.sizes[0];
    if (abs(x_col - x) < IMAT_COLRES_TOL)
      found = 1;    /* line titles */
    else
    {
      x_col -= ih->data->columns.first_offset;
      for(col = ih->data->columns.first; col <= ih->data->columns.last && !found; col++)
      {
        x_col += ih->data->columns.sizes[col];
        if(abs(x_col - x) < IMAT_COLRES_TOL)
          found = 1;
      }
    }

    if (found)
    {
      if (!iupAttribGet(ih, "_IUPMAT_CURSOR"))
        iupAttribStoreStr(ih, "_IUPMAT_CURSOR", IupGetAttribute(ih, "CURSOR"));
      IupSetAttribute(ih, "CURSOR", "RESIZE_W");
    }
    else /* It is in the empty area after the last column, or inside a cell */
      iMatrixColResResetMatrixCursor(ih); 
  }
  else
    iMatrixColResResetMatrixCursor(ih);
}

int iupMatrixColResIsResizing(Ihandle* ih)
{
  return ih->data->colres_dragging;
}