Debian Codegraph @

fontforgeexe/gotodlg.c

raw symbols SCIP
1 /* Copyright (C) 2000-2012 by George Williams */
2 /*
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5
6 * Redistributions of source code must retain the above copyright notice, this
7 * list of conditions and the following disclaimer.
8
9 * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12
13 * The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <fontforge-config.h>
29
30 #include "fontforgeui.h"
31 #include "fvfonts.h"
32 #include "ustring.h"
33 #include "utype.h"
34
35 static int alpha(const void *_t1, const void *_t2) {
36 const GTextInfo *t1 = _t1, *t2 = _t2;
37
38 return( strcmp((char *) (t1->text),(char *) (t2->text)));
39 }
40
41 static GTextInfo *AvailableRanges(SplineFont *sf,EncMap *map) {
42 int i, cnt, pos, num_blocks;
43 const struct unicode_range* blocks = uniname_blocks(&num_blocks);
44 GTextInfo *ret = calloc(num_blocks+3,sizeof(GTextInfo));
45
46 for (i = cnt = 0; i < num_blocks; ++i) {
47 pos = SFFindSlot(sf, map, blocks[i].first_char, NULL);
48 if (pos != -1) {
49 ret[cnt].text = (unichar_t *) _(blocks[i].name);
50 ret[cnt].text_is_1byte = true;
51 ret[cnt++].userdata = (void *) (intptr_t) pos;
52 }
53 }
54
55 qsort(ret,cnt,sizeof(GTextInfo),alpha);
56 return( ret );
57 }
58
59 typedef struct gotodata {
60 SplineFont *sf;
61 EncMap *map;
62 GWindow gw;
63 int ret, done;
64 GTextInfo *ranges;
65 } GotoData;
66
67 #define CID_Name 1000
68 #define CID_MergeWithSelection 1001
69
70 static int Goto_Cancel(GGadget *g, GEvent *e) {
71 GWindow gw;
72 GotoData *d;
73
74 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) {
75 gw = GGadgetGetWindow(g);
76 d = GDrawGetUserData(gw);
77 d->done = true;
78 }
79 return( true );
80 }
81
82 static int Goto_OK(GGadget *g, GEvent *e) {
83 GWindow gw;
84 GotoData *d;
85 char *ret;
86 int i;
87
88 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) {
89 gw = GGadgetGetWindow(g);
90 d = GDrawGetUserData(gw);
91 ret = GGadgetGetTitle8(GWidgetGetControl(gw,CID_Name));
92 d->ret = NameToEncoding(d->sf,d->map,ret);
93 if ( d->ret<0 || (d->ret>=d->map->enccount && d->sf->cidmaster==NULL ))
94 d->ret = -1;
95 if ( d->ret==-1 && d->ranges!=NULL ) {
96 for ( i=0; d->ranges[i].text!=NULL; ++i ) {
97 if ( strcmp(ret,(char *) d->ranges[i].text)==0 ) {
98 d->ret = (intptr_t) d->ranges[i].userdata;
99 break;
100 }
101 }
102 }
103 if ( d->ret==-1 ) {
104 ff_post_notice(_("Goto"),_("Could not find the glyph: %.70s"),ret);
105 } else
106 d->done = true;
107 free(ret);
108 }
109 return( true );
110 }
111
112 static int goto_e_h(GWindow gw, GEvent *event) {
113 if ( event->type==et_close ) {
114 GotoData *d = GDrawGetUserData(gw);
115 d->done = true;
116 } else if ( event->type == et_char ) {
117 return( false );
118 }
119 return( true );
120 }
121
122 static unichar_t **GotoCompletion(GGadget *t,int from_tab) {
123 unichar_t *pt, *spt; unichar_t **ret;
124 int gid, cnt, doit, match_len;
125 SplineChar *sc;
126 GotoData *d = GDrawGetUserData(GGadgetGetWindow(t));
127 SplineFont *sf = d->sf;
128 int do_wildcards;
129
130 pt = spt = (unichar_t *) _GGadgetGetTitle(t);
131 if ( pt==NULL )
132 return( NULL );
133 while ( *pt && *pt!='*' && *pt!='?' && *pt!='[' && *pt!='{' )
134 ++pt;
135 do_wildcards = *pt!='\0';
136 if ( do_wildcards && !from_tab )
137 return( NULL );
138 if ( do_wildcards ) {
139 pt = spt;
140 spt = malloc((u_strlen(spt)+2)*sizeof(unichar_t));
141 u_strcpy(spt,pt);
142 uc_strcat(spt,"*");
143 }
144
145 match_len = u_strlen(spt);
146 ret = NULL;
147 for ( doit=0; doit<2; ++doit ) {
148 cnt=0;
149 for ( gid=0; gid<sf->glyphcnt; ++gid ) if ( (sc=sf->glyphs[gid])!=NULL ) {
150 int matched;
151 if ( do_wildcards ) {
152 unichar_t *temp = utf82u_copy(sc->name);
153 matched = GGadgetWildMatch((unichar_t *) spt,temp,false);
154 free(temp);
155 } else
156 matched = uc_strncmp(spt,sc->name,match_len)==0;
157 if ( matched ) {
158 if ( doit )
159 ret[cnt] = utf82u_copy(sc->name);
160 ++cnt;
161 }
162 }
163 if ( doit )
164 ret[cnt] = NULL;
165 else if ( cnt==0 )
166 break;
167 else
168 ret = malloc((cnt+1)*sizeof(unichar_t *));
169 }
170 if ( do_wildcards )
171 free(spt);
172 return( ret );
173 }
174
175 int GotoChar(SplineFont *sf,EncMap *map,int *merge_with_selection) {
176 GRect pos;
177 GWindow gw;
178 GWindowAttrs wattrs;
179 GGadgetCreateData gcd[9], boxes[3], *hvarray[6][2], *barray[10];
180 GTextInfo label[9];
181 static GotoData gd;
182 GTextInfo *ranges = NULL;
183 int k,j;
184
185 if ( !map->enc->only_1byte )
186 ranges = AvailableRanges(sf,map);
187 memset(&gd,0,sizeof(gd));
188 gd.sf = sf;
189 gd.map = map;
190 gd.ret = -1;
191 gd.ranges = ranges;
192
193 memset(&wattrs,0,sizeof(wattrs));
194 wattrs.mask = wam_events|wam_cursor|wam_utf8_wtitle|wam_undercursor|wam_isdlg|wam_restrict;
195 wattrs.event_masks = ~(1<<et_charup);
196 wattrs.restrict_input_to_me = 1;
197 wattrs.undercursor = 1;
198 wattrs.cursor = ct_pointer;
199 wattrs.utf8_window_title = _("Goto");
200 wattrs.is_dlg = true;
201 pos.x = pos.y = 0;
202 pos.width = GGadgetScale(GDrawPointsToPixels(NULL,170));
203 pos.height = GDrawPointsToPixels(NULL,90);
204 gd.gw = gw = GDrawCreateTopWindow(NULL,&pos,goto_e_h,&gd,&wattrs);
205
206 memset(&label,0,sizeof(label));
207 memset(&gcd,0,sizeof(gcd));
208 memset(&boxes,0,sizeof(boxes));
209
210 k=j=0;
211 label[k].text = (unichar_t *) _("Enter the name of a glyph in the font");
212 label[k].text_is_1byte = true;
213 gcd[k].gd.label = &label[k];
214 gcd[k].gd.flags = gg_enabled|gg_visible;
215 gcd[k].creator = GLabelCreate;
216 hvarray[j][0] = &gcd[k++]; hvarray[j++][1] = NULL;
217
218 gcd[k].gd.flags = gg_enabled|gg_visible|gg_text_xim;
219 gcd[k].gd.cid = CID_Name;
220 if ( ranges==NULL )
221 gcd[k].creator = GTextCompletionCreate;
222 else {
223 gcd[k].gd.u.list = ranges;
224 gcd[k].creator = GListFieldCreate;
225 }
226 hvarray[j][0] = &gcd[k++]; hvarray[j++][1] = NULL;
227
228 if ( merge_with_selection!=NULL ) {
229 label[k].text = (unichar_t *) _("Merge into selection");
230 label[k].text_is_1byte = true;
231 gcd[k].gd.label = &label[k];
232 gcd[k].gd.cid = CID_MergeWithSelection;
233 gcd[k].gd.flags = *merge_with_selection ?
234 gg_enabled|gg_visible|gg_cb_on :
235 gg_enabled|gg_visible;
236 gcd[k].creator = GCheckBoxCreate;
237 hvarray[j][0] = &gcd[k++]; hvarray[j++][1] = NULL;
238 }
239 hvarray[j][0] = GCD_Glue; hvarray[j++][1] = NULL;
240
241 gcd[k].gd.flags = gg_visible | gg_enabled | gg_but_default;
242 label[k].text = (unichar_t *) _("_OK");
243 label[k].text_is_1byte = true;
244 label[k].text_in_resource = true;
245 gcd[k].gd.label = &label[k];
246 gcd[k].gd.handle_controlevent = Goto_OK;
247 gcd[k].creator = GButtonCreate;
248 barray[0] = GCD_Glue; barray[1] = &gcd[k++]; barray[2] = GCD_Glue; barray[3] = GCD_Glue;
249
250 gcd[k].gd.flags = gg_visible | gg_enabled | gg_but_cancel;
251 label[k].text = (unichar_t *) _("_Cancel");
252 label[k].text_is_1byte = true;
253 label[k].text_in_resource = true;
254 gcd[k].gd.label = &label[k];
255 gcd[k].gd.handle_controlevent = Goto_Cancel;
256 gcd[k].creator = GButtonCreate;
257 barray[4] = GCD_Glue; barray[5] = &gcd[k++]; barray[6] = GCD_Glue; barray[7] = NULL;
258
259 boxes[2].gd.flags = gg_visible | gg_enabled;
260 boxes[2].gd.u.boxelements = barray;
261 boxes[2].creator = GHBoxCreate;
262 hvarray[j][0] = &boxes[2]; hvarray[j++][1] = NULL;
263 hvarray[j][0] = NULL;
264
265 boxes[0].gd.pos.x = boxes[0].gd.pos.y = 2;
266 boxes[0].gd.flags = gg_visible | gg_enabled;
267 boxes[0].gd.u.boxelements = hvarray[0];
268 boxes[0].creator = GHVGroupCreate;
269
270 GGadgetsCreate(gw,boxes);
271 GCompletionFieldSetCompletion(gcd[1].ret,GotoCompletion);
272 GCompletionFieldSetCompletionMode(gcd[1].ret,true);
273 GHVBoxSetExpandableRow(boxes[0].ret,gb_expandglue);
274 GHVBoxSetExpandableCol(boxes[2].ret,gb_expandgluesame);
275 GHVBoxFitWindow(boxes[0].ret);
276 GDrawSetVisible(gw,true);
277 while ( !gd.done )
278 GDrawProcessOneEvent(NULL);
279 if ( merge_with_selection!=NULL )
280 *merge_with_selection = GGadgetIsChecked(GWidgetGetControl(gw,CID_MergeWithSelection));
281 GDrawDestroyWindow(gw);
282 free(ranges);
283 return( gd.ret );
284 }