File: fd_rubber.c

package info (click to toggle)
libforms 1.0.93sp1-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 11,548 kB
  • sloc: ansic: 97,227; sh: 9,236; makefile: 858
file content (435 lines) | stat: -rw-r--r-- 8,824 bytes parent folder | download | duplicates (2)
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
/*
 * This file is part of XForms.
 *
 *  XForms is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU Lesser General Public License as
 *  published by the Free Software Foundation; either version 2.1, or
 *  (at your option) any later version.
 *
 *  XForms is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with XForms.  If not, see <http://www.gnu.org/licenses/>.
 */


/**
 * \file fd_rubber.c
 *
 *  This file is part of XForms package
 *  Copyright (c) 1996-2002  T.C. Zhao and Mark Overmars
 *  All rights reserved.
 *
 * This file is part of the Form Designer.
 *
 *  Someday, we'll have to re-write all of these so it fits with
 *  X event handling better.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include "include/forms.h"
#include "fd_main.h"

extern void color( unsigned long );

extern GC fd_gc,
          fd_xorgc,
           fd_copygc;
extern Colormap fd_colormap;
extern Display *fd_display;

/****
  ROUNDING
*****/

static float xmin   = 0.0,
             ymin   = 0.0,
             width  = 1280.0,
             height = 1024.0;
static float stepsize = 10.0;   /* Step size (for alignment) */


/***************************************
 * Sets the step size
 ***************************************/

void
set_step_size( float size )
{
    stepsize = size < 1.0 ? 1.0 : size;
}


/***************************************
 * Returns the step size
 ***************************************/

float
get_step_size( void )
{
    return stepsize;
}


/***************************************
 * Sets the bounding box.
 ***************************************/

void
set_bounding_box( float x,
                  float y,
                  float w,
                  float h )
{
    xmin   = x;
    ymin   = y;
    width  = w;
    height = h;
}


/***************************************
 * Rounds the box size to the stepsize and inside the bounding box
 ***************************************/

static void
round_size( float * x,
            float * y,
            float * w,
            float * h )
{
    int t;

    if ( *w > width )
        *w = width;
    if ( *h > height )
        *h = height;
    if ( stepsize > 0.0 )
    {
        t = *w / stepsize + 0.5;
        if ( *w >= 0.0 )
            *w = stepsize * t;
        else
            *w = stepsize * ( t - 1 );
        t = *h / stepsize + 0.5;
        *h = stepsize * t;
        if ( *h >= 0.0 )
            *h = stepsize * t;
        else
            *h = stepsize * ( t - 1 );
    }

    if ( *x + *w > xmin + width )
        *w = xmin + width - *x;
    if ( *y + *h > ymin + height )
        *h = ymin + height - *y;
    if ( *x + *w < xmin )
        *w = xmin - *x;
    if ( *y + *h < ymin )
        *h = ymin - *y;
    if ( *w >= 0.0f && *w < stepsize )
        *w = stepsize;
    if ( *h >= 0.0 && *h < stepsize )
        *h = stepsize;
    if ( *w < 0.0 && *w > -stepsize )
        *w = -stepsize;
    if ( *h < 0.0 && *h > -stepsize )
        *h = -stepsize;
}


/***************************************
 * Rounds the position to the stepsize and inside the bounding box
 ***************************************/

static void
round_position( float * x,
                float * y,
                float * w,
                float * h )
{
    int t;

    if ( *w > width )
        *w = width;
    if ( *h > height )
        *h = height;
    if ( stepsize > 0.0 )
    {
        t = *x / stepsize + 0.5;
        *x = stepsize * t;
        t = *y / stepsize + 0.5;
        *y = stepsize * t;
    }

    if ( *x < xmin )
        *x = xmin;
    if ( *y < ymin )
        *y = ymin;
    if ( *x + *w > xmin + width )
        *x = xmin + width - *w;
    if ( *y + *h > ymin + height )
        *y = ymin + height - *h;
}


/****
  DRAWING
****/

/***************************************
 * Draws a box
 ***************************************/

static void
show_box( float x,
          float y,
          float w,
          float h )
{
    XSetFunction( fd_display, fd_gc, GXxor );
    color( fd_col ^ fd_black );
    rect( x, y, x + w - 1, y + h - 1 );
    XSetFunction( fd_display, fd_gc, GXcopy );
}

#define hide_box show_box

/****
  THE ACTUAL ROUTINES
****/


/***************************************
 * Whether we should stop
 ***************************************/

static int
ready( void )
{
    XEvent xev;

    if ( fl_check_forms( ) == FL_EVENT )
    {
        fl_XNextEvent( &xev );
        fli_xevent_name( "ready:", &xev );
        return (   xev.type == ButtonRelease
                || ( is_pasting && xev.type == KeyPress ) );
    }

    fl_msleep( 10 );

    return 0;
}


/***************************************
 * Returns the position of the mouse in world coordinates
 ***************************************/

void
get_mouse_pos( float * xx,
               float * yy )
{
    FL_Coord x,
             y;
    unsigned int kmask;

    fl_get_win_mouse( main_window, &x, &y, &kmask );
    *xx = x;
    *yy = y;
}


/***************************************
 * Drag a box around until the user releases a mouse button
 ***************************************/

void
move_box( float * x,
          float * y,
          float * w,
          float * h,
          int     offset )
{
    float oldx = *x, oldy = *y, oldw = *w, oldh = *h;
    float xoff, yoff;

    if ( offset )
    {
        get_mouse_pos( &xoff, &yoff );
        xoff -= *x;
        yoff -= *y;
    }
    else
    {
        xoff = *w / 2.0;
        yoff = *h / 2.0;
    }

    show_box( *x, *y, *w, *h );

    while ( ! ready( ) )
    {
        get_mouse_pos( x, y );
        *x -= xoff;
        *y -= yoff;
        round_position( x, y, w, h );
        if ( *x != oldx || *y != oldy )
        {
            hide_box( oldx, oldy, oldw, oldh );
            show_box( *x, *y, *w, *h );

            if ( fd_trackgeometry )
                show_geometry( *x, *y, *w, *h );
            oldx = *x;
            oldy = *y;
            oldw = *w;
            oldh = *h;
        }
    }

    hide_box( oldx, oldy, *w, *h );
}


/***************************************
 * Scales a box until the user releases a mouse button
 ***************************************/

void
scale_box( float * x,
           float * y,
           float * w,
           float * h )
{
    float oldw = 1.0,
          oldh = 1.0;

    round_position( x, y, w, h );

    while ( ! ready( ) )
    {
        get_mouse_pos( w, h );
        *w -= *x;
        *h -= *y;
        round_size( x, y, w, h );

        if ( *w != oldw || *h != oldh )
        {
            if ( fd_trackgeometry )
                show_geometry( *x, *y, *w, *h );
            hide_box( *x - 1, *y - 1, oldw, oldh );
            show_box( *x - 1, *y - 1, *w, *h );
            oldw = *w;
            oldh = *h;
        }
    }

    hide_box( *x - 1, *y - 1, oldw, oldh );

    if ( *w < 0.0 )
    {
        *x += *w;
        *w = - *w;
    }

    if ( *h < 0.0 )
    {
        *y += *h;
        *h = - *h;
    }
}


/***************************************
 ***************************************/

void
color( unsigned long n )
{
    static unsigned long oldcol;

    if ( fd_gc && oldcol != n )
    {
        XSetForeground( fd_display, fd_gc, n );
        oldcol = n;
    }
}


/***************************************
 ***************************************/

void
rectf( FL_Coord x1,
       FL_Coord y1,
       FL_Coord x2,
       FL_Coord y2 )
{
    int x = x1,
        y = y1,
        w = x2 - x1,
        h = y2 - y1;

    fli_canonicalize_rect( &x, &y, &w, &h );
    XFillRectangle( fd_display, main_window, fd_gc, x, y, w, h );
}


/***************************************
 ***************************************/

void
rect( FL_Coord x1,
      FL_Coord y1,
      FL_Coord x2,
      FL_Coord y2 )
{
    FL_Coord x = x1,
             y = y1,
             w = x2 - x1,
             h = y2 - y1;

    fli_canonicalize_rect( &x, &y, &w, &h );
    XDrawRectangle( fd_display, main_window, fd_gc, x, y, w, h );
}


/***************************************
 ***************************************/

void
fd_clear( int x,
          int y,
          int w,
          int h )
{
    static GC blk_gc;

    if ( main_window && w > 0 && h > 0 )
    {
        if ( ! blk_gc )
        {
            blk_gc = XCreateGC( fd_display, main_window, 0, 0 );
            XSetForeground( fd_display, blk_gc, fd_black );
        }

        XFillRectangle( fd_display, main_window, blk_gc, x, y, w, h );
    }
}


/*
 * Local variables:
 * tab-width: 4
 * indent-tabs-mode: nil
 * End:
 */