~ubuntu-branches/ubuntu/wily/grass/wily

« back to all changes in this revision

Viewing changes to imagery/i.points/ask.c

Tags: 7.0.0~rc1+ds1-1~exp1
* New upstream release candidate.
* Repack upstream tarball, remove precompiled Python objects.
* Add upstream metadata.
* Update gbp.conf and Vcs-Git URL to use the experimental branch.
* Update watch file for GRASS 7.0.
* Drop build dependencies for Tcl/Tk, add build dependencies:
  python-numpy, libnetcdf-dev, netcdf-bin, libblas-dev, liblapack-dev
* Update Vcs-Browser URL to use cgit instead of gitweb.
* Update paths to use grass70.
* Add configure options: --with-netcdf, --with-blas, --with-lapack,
  remove --with-tcltk-includes.
* Update patches for GRASS 7.
* Update copyright file, changes:
  - Update copyright years
  - Group files by license
  - Remove unused license sections
* Add patches for various typos.
* Fix desktop file with patch instead of d/rules.
* Use minimal dh rules.
* Bump Standards-Version to 3.9.6, no changes.
* Use dpkg-maintscript-helper to replace directories with symlinks.
  (closes: #776349)
* Update my email to use @debian.org address.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <string.h>
2
 
#include <stdlib.h>
3
 
#include <grass/gis.h>
4
 
#include <grass/raster.h>
5
 
#include "globals.h"
6
 
#include "local_proto.h"
7
 
#define NLINES 18
8
 
 
9
 
struct box
10
 
{
11
 
    int top, bottom, left, right;
12
 
};
13
 
 
14
 
static int pick(int, int, int);
15
 
static int downarrow(struct box *, int);
16
 
static int uparrow(struct box *, int);
17
 
static int dobox(struct box *, char *, int, int, int, int, int);
18
 
static int dotext(char *, int, int, int, int, int);
19
 
static int inbox(struct box *, int, int);
20
 
static int cancel_which(void);
21
 
 
22
 
static int text_size;
23
 
static int which;
24
 
static struct box cancel, more, less;
25
 
static int height, size, edge, count;
26
 
static int page, npages;
27
 
static struct
28
 
{
29
 
    char name[GNAME_MAX], mapset[GMAPSET_MAX];
30
 
    struct box box;
31
 
} list[NLINES * 2];
32
 
 
33
 
int
34
 
ask_gis_files(char *type, char *file, char *xname, char *xmapset,
35
 
              int position)
36
 
{
37
 
    static int use = 1;
38
 
    static Objects objects[] = {
39
 
        OTHER(pick, &use),
40
 
        {0}
41
 
    };
42
 
 
43
 
    char msg[100];
44
 
    FILE *fd;
45
 
    int width;
46
 
    int len1, len2, len;
47
 
    long offset;
48
 
    long *page_offset;
49
 
    int col, nlist;
50
 
    int line;
51
 
    int stat;
52
 
    char buf[100];
53
 
    int top, bottom, left, right, center;
54
 
    int topx, bottomx, leftx, rightx, widthx;
55
 
    char name[GNAME_MAX], mapset[GMAPSET_MAX], cur_mapset[GMAPSET_MAX];
56
 
    int new_mapset;
57
 
 
58
 
    Menu_msg("");
59
 
 
60
 
    fd = fopen(file, "r");
61
 
    if (fd == NULL)
62
 
        G_fatal_error("ask_gis_files: can't read tempfile");
63
 
    if (fread(&len1, sizeof(len1), 1, fd) != 1
64
 
        || fread(&len2, sizeof(len2), 1, fd) != 1 || len1 <= 0 || len2 <= 0) {
65
 
        fclose(fd);
66
 
        return 0;
67
 
    }
68
 
 
69
 
    sprintf(msg, "Double click on %s file to be plotted", type);
70
 
 
71
 
    /*
72
 
     * build a popup window at center of the screen.
73
 
     * 35% the height and wide enough to hold 2 columms of file names
74
 
     *
75
 
     * the window is for choosing file names and will be laid out in 2 columns
76
 
     *
77
 
     *             ------------------------------------------
78
 
     *             |     CANCEL           | (MORE) | (LESS) | 
79
 
     *             ------------------------------------------
80
 
     *             |             mapset                     |
81
 
     *             ------------------------------------------
82
 
     *             |      name1        |   name2            |
83
 
     *             ------------------------------------------
84
 
     *             |      name3        |   name4            |
85
 
     *             ------------------------------------------
86
 
     *             |      name5        |   name6            |
87
 
     *             |                   .                    |
88
 
     *             |                   .                    |
89
 
     *             |                   .                    |
90
 
     *             ------------------------------------------
91
 
     */
92
 
 
93
 
    /* height of 1 line, based on NLINES taking up 35% vertical space */
94
 
    height = (.35 * (SCREEN_BOTTOM - SCREEN_TOP)) / NLINES + 1;
95
 
 
96
 
    /* size of text, 80% of line height */
97
 
    text_size = .8 * height;
98
 
    size = text_size - 1;       /* fudge for computing pixels width of text */
99
 
 
100
 
    /* indent for the text */
101
 
    edge = .1 * height + 1;
102
 
 
103
 
    /* this is a fudge to determine the length of the largest text */
104
 
    len1 = 2 * len1;            /* name in 2 columns */
105
 
    len2 += strlen("mapset ");
106
 
    len = (len1 > len2 ? len1 : len2);
107
 
 
108
 
    /* width is for max chars plus sidecar for more/less */
109
 
    width = len * size + height;
110
 
    widthx = strlen(msg) * size;
111
 
    if (widthx < width)
112
 
        widthx = width;
113
 
 
114
 
    /* define the window */
115
 
    top = (SCREEN_TOP + SCREEN_BOTTOM - height * NLINES) / 2;
116
 
    bottom = top + height * NLINES;
117
 
 
118
 
    center = (SCREEN_LEFT + SCREEN_RIGHT) / 2;
119
 
    if (position > 0) {
120
 
        right = (center + SCREEN_RIGHT + width) / 2;
121
 
        if (right >= SCREEN_RIGHT)
122
 
            right = SCREEN_RIGHT - 1;
123
 
        left = right - width;
124
 
    }
125
 
    else if (position < 0) {
126
 
        left = (center + SCREEN_LEFT - width) / 2;
127
 
        if (left <= SCREEN_LEFT)
128
 
            left = SCREEN_LEFT + 1;
129
 
        right = left + width;
130
 
    }
131
 
    else {
132
 
        left = center + width / 2;
133
 
        right = left + width;
134
 
    }
135
 
 
136
 
    topx = top - 3 * height;
137
 
    bottomx = topx + 2 * height;
138
 
    leftx = (left + right - widthx) / 2;
139
 
    if (leftx < SCREEN_LEFT)
140
 
        leftx = SCREEN_LEFT;
141
 
    rightx = leftx + widthx;
142
 
 
143
 
    /* save what is under these areas, so they can be restored */
144
 
    R_panel_save(tempfile1, top, bottom + 1, left, right + 1);
145
 
    R_panel_save(tempfile2, topx, bottomx + 1, leftx, rightx + 1);
146
 
 
147
 
    /* fill it top with GREY, pick area with white */
148
 
    R_standard_color(WHITE);
149
 
    R_box_abs(left, top, right, bottom);
150
 
    R_standard_color(GREY);
151
 
    R_box_abs(leftx, topx, rightx, bottomx);
152
 
 
153
 
    R_standard_color(BLACK);
154
 
    Outline_box(top, bottom, left, right);
155
 
    right -= height;            /* reduce it to exclude sidecar */
156
 
    Outline_box(top, bottom, left, right);
157
 
 
158
 
    /* print messages above the files */
159
 
    dotext(msg, topx, topx + height, leftx, rightx, 1);
160
 
    dotext("Double click here to cancel", topx + height, bottomx, leftx,
161
 
           rightx, 1);
162
 
    cancel.top = topx;
163
 
    cancel.bottom = bottomx;
164
 
    cancel.left = leftx;
165
 
    cancel.right = rightx;
166
 
 
167
 
    /* start the mouse in the cancel box */
168
 
    Set_mouse_xy((leftx + rightx) / 2, (topx + bottomx) / 2);
169
 
 
170
 
    dobox(&less, "", WHITE, top, right, right + height, 0);
171
 
    dobox(&more, "", WHITE, bottom - height, right, right + height, 0);
172
 
 
173
 
    /* as we read the file of names, keep track of pages so we can
174
 
     * page backward
175
 
     */
176
 
    page = 0;
177
 
    page_offset = (long *)G_calloc(npages = 1, sizeof(long));
178
 
    *page_offset = ftell(fd);
179
 
 
180
 
    nlist = sizeof(list) / sizeof(list[0]);
181
 
    for (stat = -1; stat < 0;) {
182
 
        line = 0;
183
 
        count = 0;
184
 
        *cur_mapset = 0;
185
 
        col = 0;
186
 
        while (1) {
187
 
            offset = ftell(fd);
188
 
            if (fgets(buf, sizeof buf, fd) == NULL
189
 
                || sscanf(buf, "%s %s", name, mapset) != 2)
190
 
                break;
191
 
            if (new_mapset = (strcmp(cur_mapset, mapset) != 0)) {
192
 
                if (line)
193
 
                    line++;
194
 
                if (col)
195
 
                    line++;
196
 
                col = 0;
197
 
            }
198
 
            if (count >= nlist || line + new_mapset >= NLINES) {
199
 
                if (page + 1 == npages) {
200
 
                    npages++;
201
 
                    page_offset =
202
 
                        (long *)G_realloc(page_offset, npages * sizeof(long));
203
 
                    page_offset[npages - 1] = offset;
204
 
                }
205
 
                break;
206
 
            }
207
 
            if (new_mapset) {
208
 
                struct box dummy;
209
 
                char label[GMAPSET_MAX + 7];
210
 
 
211
 
                strcpy(cur_mapset, mapset);
212
 
                sprintf(label, "Mapset %s", mapset);
213
 
                dobox(&dummy, label, WHITE, top + line * height, left, right,
214
 
                      0);
215
 
                line++;
216
 
            }
217
 
            if (col) {
218
 
                dobox(&list[count].box, name, GREY, top + line * height,
219
 
                      left + width / 2, right, 0);
220
 
                line++;
221
 
                col = 0;
222
 
            }
223
 
            else {
224
 
                dobox(&list[count].box, name, GREY, top + line * height, left,
225
 
                      left + width / 2, 0);
226
 
                col = 1;
227
 
            }
228
 
            strcpy(list[count].name, name);
229
 
            strcpy(list[count].mapset, mapset);
230
 
            count++;
231
 
        }
232
 
        downarrow(&more, page + 1 < npages ? BLACK : WHITE);
233
 
        uparrow(&less, page > 0 ? BLACK : WHITE);
234
 
        which = -1;
235
 
        switch (Input_pointer(objects)) {
236
 
        case -1:                /* more or less */
237
 
            break;
238
 
        case -2:                /* cancel */
239
 
            stat = 0;
240
 
            continue;
241
 
        default:                /* file picked */
242
 
            strcpy(xname, list[which].name);
243
 
            strcpy(xmapset, list[which].mapset);
244
 
            stat = 1;
245
 
            continue;
246
 
        }
247
 
        fseek(fd, page_offset[page], 0);
248
 
        R_standard_color(WHITE);
249
 
        R_box_abs(left + 1, top + 1, right - 1, bottom - 1);
250
 
    }
251
 
 
252
 
    /* all done. restore what was under the window */
253
 
    right += height;            /* move it back over the sidecar */
254
 
    R_standard_color(WHITE);
255
 
    R_box_abs(left, top, right, bottom);
256
 
    R_panel_restore(tempfile1);
257
 
    R_panel_restore(tempfile2);
258
 
    R_panel_delete(tempfile1);
259
 
    R_panel_delete(tempfile2);
260
 
    R_flush();
261
 
 
262
 
    G_free(page_offset);
263
 
    return stat;
264
 
}
265
 
 
266
 
static int dobox(struct box *box, char *text, int color, int top, int left,
267
 
                 int right, int centered)
268
 
{
269
 
    int bottom;
270
 
 
271
 
    bottom = top + height;
272
 
    /* fill inside of box with color */
273
 
    R_standard_color(color);
274
 
    R_box_abs(left + 1, top + 1, right - 1, bottom - 1);
275
 
 
276
 
    /* draw box outline and text in black */
277
 
    R_standard_color(BLACK);
278
 
    Outline_box(top, bottom, left, right);
279
 
    dotext(text, top, bottom, left, right, centered);
280
 
    R_flush();
281
 
 
282
 
    box->top = top;
283
 
    box->bottom = bottom;
284
 
    box->left = left;
285
 
    box->right = right;
286
 
 
287
 
    return 0;
288
 
}
289
 
 
290
 
static int uparrow(struct box *box, int color)
291
 
{
292
 
    R_standard_color(color);
293
 
    Uparrow(box->top + edge, box->bottom - edge, box->left + edge,
294
 
            box->right - edge);
295
 
 
296
 
    return 0;
297
 
}
298
 
 
299
 
static int downarrow(struct box *box, int color)
300
 
{
301
 
    R_standard_color(color);
302
 
    Downarrow(box->top + edge, box->bottom - edge, box->left + edge,
303
 
              box->right - edge);
304
 
 
305
 
    return 0;
306
 
}
307
 
 
308
 
static int pick(int x, int y, int button)
309
 
{
310
 
    int n;
311
 
 
312
 
    if (inbox(&more, x, y)) {
313
 
        cancel_which();
314
 
        if (page + 1 >= npages)
315
 
            return 0;
316
 
        page++;
317
 
        return -1;
318
 
    }
319
 
    if (inbox(&less, x, y)) {
320
 
        cancel_which();
321
 
        if (page == 0)
322
 
            return 0;
323
 
        page--;
324
 
        return -1;
325
 
    }
326
 
    if (inbox(&cancel, x, y)) {
327
 
        if (which == -2)
328
 
            return -2;
329
 
        cancel_which();
330
 
        which = -2;
331
 
        R_standard_color(RED);
332
 
        Outline_box(cancel.top, cancel.bottom, cancel.left, cancel.right);
333
 
        R_flush();
334
 
        return 0;
335
 
    }
336
 
    /* search name list. handle double click */
337
 
    for (n = 0; n < count; n++)
338
 
        if (inbox(&list[n].box, x, y)) {
339
 
            if (n == which)     /* second click! */
340
 
                return 1;
341
 
            cancel_which();
342
 
            which = n;
343
 
            R_standard_color(RED);
344
 
            Outline_box(list[n].box.top, list[n].box.bottom,
345
 
                        list[n].box.left, list[n].box.right);
346
 
            R_flush();
347
 
            return 0;           /* ignore first click */
348
 
        }
349
 
 
350
 
    cancel_which();
351
 
    return 0;
352
 
}
353
 
 
354
 
static int cancel_which(void)
355
 
{
356
 
    if (which == -2) {
357
 
        R_standard_color(BLACK);
358
 
        Outline_box(cancel.top, cancel.bottom, cancel.left, cancel.right);
359
 
    }
360
 
    else if (which >= 0) {
361
 
        R_standard_color(BLACK);
362
 
        Outline_box(list[which].box.top, list[which].box.bottom,
363
 
                    list[which].box.left, list[which].box.right);
364
 
    }
365
 
    which = -1;
366
 
    R_flush();
367
 
 
368
 
    return 0;
369
 
}
370
 
 
371
 
static int inbox(struct box *box, int x, int y)
372
 
{
373
 
    return (x > box->left && x < box->right && y > box->top &&
374
 
            y < box->bottom);
375
 
}
376
 
 
377
 
static int dotext(char *text, int top, int bottom, int left, int right,
378
 
                  int centered)
379
 
{
380
 
    R_text_size(text_size, text_size);
381
 
    R_move_abs(left + 1 + edge, bottom - 1 - edge);
382
 
    if (centered)
383
 
        R_move_rel((right - left - strlen(text) * size) / 2, 0);
384
 
    R_set_window(top, bottom, left, right);     /* for text clipping */
385
 
    R_text(text);
386
 
    R_set_window(SCREEN_TOP, SCREEN_BOTTOM, SCREEN_LEFT, SCREEN_RIGHT);
387
 
 
388
 
    return 0;
389
 
}