1 /*         ______   ___    ___
2  *        /\  _  \ /\_ \  /\_ \
3  *        \ \ \L\ \\//\ \ \//\ \      __     __   _ __   ___
4  *         \ \  __ \ \ \ \  \ \ \   /'__`\ /'_ `\/\`'__\/ __`\
5  *          \ \ \/\ \ \_\ \_ \_\ \_/\  __//\ \L\ \ \ \//\ \L\ \
6  *           \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
7  *            \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
8  *                                           /\____/
9  *                                           \_/__/
10  *
11  *      Grabber plugin for changing between absolute and relative
12  *      filenames.
13  *
14  *      By Eric Botcazou and Evert Glebbeek.
15  *
16  *      See readme.txt for copyright information.
17  */
18 
19 
20 #include <stdio.h>
21 
22 #include "allegro.h"
23 #include "../datedit.h"
24 
25 
26 
27 /* worker function for counting objects with origin */
do_origin_check(DATAFILE * dat,int * param,int param2)28 static int do_origin_check(DATAFILE *dat, int *param, int param2)
29 {
30    AL_CONST char *orig = get_datafile_property(dat, DAT_ORIG);
31 
32    if (orig[0])
33       (*param)++;
34 
35    return D_O_K;
36 }
37 
38 
39 
40 /* checks whether our change command is allowed at the moment */
change_query(int popup)41 static int change_query(int popup)
42 {
43    int n, p;
44 
45    if (popup) {
46       p = 0;
47       grabber_foreach_selection(do_origin_check, &n, &p, 0);
48       return (p > 0);
49    }
50 
51    return TRUE;
52 }
53 
54 
55 
56 /* worker function for changing to a relative filename */
do_change_relative(DATAFILE * dat,int * param,int param2)57 static int do_change_relative(DATAFILE *dat, int *param, int param2)
58 {
59    AL_CONST char *orig = get_datafile_property(dat, DAT_ORIG);
60    char relative[FILENAME_LENGTH];
61 
62    if (!orig[0] || is_relative_filename(orig)) {
63       (*param)++;
64       return D_O_K;
65    }
66 
67    make_relative_filename(relative, grabber_data_file, orig, FILENAME_LENGTH);
68    datedit_set_property(dat, DAT_ORIG, relative);
69 
70    return D_REDRAW;
71 }
72 
73 
74 
75 /* change the origin to a relative filename */
change_relative(void)76 static int change_relative(void)
77 {
78    char buf[80];
79    int ret, n;
80    int p = 0;
81 
82    grabber_busy_mouse(TRUE);
83 
84    ret = grabber_foreach_selection(do_change_relative, &n, &p, 0);
85 
86    grabber_busy_mouse(FALSE);
87 
88    if (n <= 0) {
89       alert ("Nothing to change!", NULL, NULL, "OK", NULL, 13, 0);
90    }
91    else if (p > 0) {
92       sprintf(buf, "%d object%s ignored", p, (p==1) ? " was" : "s were");
93       alert(buf, NULL, NULL, "OK", NULL, 13, 0);
94    }
95 
96    if (n > p)
97       grabber_modified(TRUE);
98 
99    return ret;
100 }
101 
102 
103 
104 /* worker function for changing to an absolute filename */
do_change_absolute(DATAFILE * dat,int * param,int param2)105 static int do_change_absolute(DATAFILE *dat, int *param, int param2)
106 {
107    AL_CONST char *orig = get_datafile_property(dat, DAT_ORIG);
108    char absolute[FILENAME_LENGTH];
109 
110    if (!orig[0] || !is_relative_filename(orig)) {
111       (*param)++;
112       return D_O_K;
113    }
114 
115    make_absolute_filename(absolute, grabber_data_file, orig, FILENAME_LENGTH);
116    datedit_set_property(dat, DAT_ORIG, absolute);
117 
118    return D_REDRAW;
119 }
120 
121 
122 
123 /* change the origin to an absolute filename */
change_absolute(void)124 static int change_absolute(void)
125 {
126    char buf[80];
127    int ret, n;
128    int p = 0;
129 
130    grabber_busy_mouse(TRUE);
131 
132    ret = grabber_foreach_selection(do_change_absolute, &n, &p, 0);
133 
134    grabber_busy_mouse(FALSE);
135 
136    if (n <= 0) {
137       alert ("Nothing to change!", NULL, NULL, "OK", NULL, 13, 0);
138    }
139    else if (p > 0) {
140       sprintf(buf, "%d object%s ignored", p, (p==1) ? " was" : "s were");
141       alert(buf, NULL, NULL, "OK", NULL, 13, 0);
142    }
143 
144    if (n > p)
145       grabber_modified(TRUE);
146 
147    return ret;
148 }
149 
150 
151 
152 static MENU filename_menu[] =
153 {
154    { "To &Relative",    change_relative,  NULL,       0, NULL  },
155    { "To &Absolute",    change_absolute,  NULL,       0, NULL  },
156    { NULL,              NULL,             NULL,       0, NULL  }
157 };
158 
159 
160 
161 /* hook ourselves into the grabber menu system */
162 static MENU change_filename_menu =
163 {
164    "Change Filename",
165    NULL,
166    filename_menu,
167    0,
168    NULL
169 };
170 
171 
172 
173 DATEDIT_MENU_INFO datfname_type_menu =
174 {
175    &change_filename_menu,
176    change_query,
177    DATEDIT_MENU_OBJECT | DATEDIT_MENU_POPUP,
178    0,
179    NULL
180 };
181 
182