Debian Codegraph @

fontforgeexe/searchview.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 "cvundoes.h"
31 #include "fontforgeui.h"
32 #include "fvfonts.h"
33 #include "gkeysym.h"
34 #include "gresedit.h"
35 #include "search.h"
36 #include "splineutil.h"
37 #include "ustring.h"
38 #include "utype.h"
39
40 #include <math.h>
41
42 static SearchView *searcher=NULL;
43
44 #define CID_Allow 1000
45 #define CID_Flipping 1001
46 #define CID_Scaling 1002
47 #define CID_Rotating 1003
48 #define CID_Selected 1004
49 #define CID_Find 1005
50 #define CID_FindAll 1006
51 #define CID_Replace 1007
52 #define CID_ReplaceAll 1008
53 #define CID_Cancel 1009
54 #define CID_TopBox 1010
55 #define CID_Fuzzy 1011
56 #define CID_Endpoints 1012
57
58 GResFont searchview_font = GRESFONT_INIT("400 12pt " SANS_UI_FAMILIES);
59 GResFont searchview_boldfont = GRESFONT_INIT("700 12pt " SANS_UI_FAMILIES);
60 extern GBox _ggadget_Default_Box;
61 #define MAIN_FOREGROUND (_ggadget_Default_Box.main_foreground)
62
63 static double old_fudge = .001;
64
65 static void SVSelectSC(SearchView *sv) {
66 SplineChar *sc = sv->sd.curchar;
67 SplinePointList *spl;
68 SplinePoint *sp;
69 RefChar *rf;
70 int i;
71 int layer = sv->sd.fv->active_layer;
72
73 /* Deselect all */;
74 for ( spl = sc->layers[layer].splines; spl!=NULL; spl = spl->next ) {
75 for ( sp=spl->first ;; ) {
76 sp->selected = false;
77 if ( sp->next == NULL )
78 break;
79 sp = sp->next->to;
80 if ( sp==spl->first )
81 break;
82 }
83 }
84 for ( rf=sc->layers[layer].refs; rf!=NULL; rf = rf->next )
85 if ( rf->selected ) rf->selected = false;
86
87 if ( sv->sd.subpatternsearch ) {
88 spl = sv->sd.matched_spl;
89 for ( sp = sv->sd.matched_sp; ; ) {
90 sp->selected = true;
91 if ( sp->next == NULL || sv->sd.last_sp==NULL || sp==sv->sd.last_sp )
92 break;
93 sp = sp->next->to;
94 /* Ok to wrap back to first */
95 }
96 } else {
97 for ( rf=sc->layers[layer].refs, i=0; rf!=NULL; rf=rf->next, ++i )
98 if ( sv->sd.matched_refs&(1<<i) )
99 rf->selected = true;
100 for ( spl = sc->layers[layer].splines,i=0; spl!=NULL; spl = spl->next, ++i ) {
101 if ( sv->sd.matched_ss&(1<<i) ) {
102 for ( sp=spl->first ;; ) {
103 sp->selected = true;
104 if ( sp->next == NULL )
105 break;
106 sp = sp->next->to;
107 if ( sp==spl->first )
108 break;
109 }
110 }
111 }
112 }
113 SCUpdateAll(sc);
114 sc->changed_since_search = false;
115 }
116
117 static int DoFindOne(SearchView *sv,int startafter) {
118 int i, gid;
119 SplineChar *startcur = sv->sd.curchar;
120
121 /* It is possible that some idiot deleted the current character since */
122 /* the last search... do some mild checks */
123 if ( sv->sd.curchar!=NULL &&
124 sv->sd.curchar->parent == sv->sd.fv->sf &&
125 sv->sd.curchar->orig_pos>=0 && sv->sd.curchar->orig_pos<sv->sd.fv->sf->glyphcnt &&
126 sv->sd.curchar==sv->sd.fv->sf->glyphs[sv->sd.curchar->orig_pos] )
127 /* Looks ok */;
128 else
129 sv->sd.curchar=startcur=NULL;
130
131 if ( !sv->sd.subpatternsearch ) startafter = false;
132
133 if ( sv->showsfindnext && sv->sd.curchar!=NULL )
134 i = sv->sd.fv->map->backmap[sv->sd.curchar->orig_pos]+1-startafter;
135 else {
136 startafter = false;
137 if ( !sv->sd.onlyselected )
138 i = 0;
139 else {
140 for ( i=0; i<sv->sd.fv->map->enccount; ++i )
141 if ( sv->sd.fv->selected[i] && (gid=sv->sd.fv->map->map[i])!=-1 &&
142 sv->sd.fv->sf->glyphs[gid]!=NULL )
143 break;
144 }
145 }
146
147 for ( ; i<sv->sd.fv->map->enccount; ++i ) {
148 if (( !sv->sd.onlyselected || sv->sd.fv->selected[i]) && (gid=sv->sd.fv->map->map[i])!=-1 &&
149 sv->sd.fv->sf->glyphs[gid]!=NULL ) {
150 SCSplinePointsUntick(sv->sd.fv->sf->glyphs[gid],sv->sd.fv->active_layer);
151 if ( SearchChar(&sv->sd,gid,startafter) )
152 break;
153 }
154 startafter = false;
155 }
156 if ( i>=sv->sd.fv->map->enccount ) {
157 ff_post_notice(_("Not Found"),sv->showsfindnext?_("The search pattern was not found again in the font %.100s"):_("The search pattern was not found in the font %.100s"),sv->sd.fv->sf->fontname);
158 sv->sd.curchar = startcur;
159 GGadgetSetTitle8(GWidgetGetControl(sv->gw,CID_Find),_("Find"));
160 sv->showsfindnext = false;
161 return( false );
162 }
163 SVSelectSC(sv);
164 if ( sv->lastcv!=NULL && sv->lastcv->b.sc==startcur && sv->lastcv->b.fv== sv->sd.fv ) {
165 CVChangeSC(sv->lastcv,sv->sd.curchar);
166 GDrawSetVisible(sv->lastcv->gw,true);
167 GDrawRaise(sv->lastcv->gw);
168 } else
169 sv->lastcv = CharViewCreate(sv->sd.curchar,(FontView *) sv->sd.fv,-1);
170 GGadgetSetTitle8(GWidgetGetControl(sv->gw,CID_Find),_("Find Next"));
171 sv->showsfindnext = true;
172 return( true );
173 }
174
175 static void DoFindAll(SearchView *sv) {
176 int any;
177
178 any = _DoFindAll(&sv->sd);
179 GDrawRequestExpose(((FontView *) (sv->sd.fv))->v,NULL,false);
180 if ( !any )
181 ff_post_notice(_("Not Found"),_("The search pattern was not found in the font %.100s"),sv->sd.fv->sf->fontname);
182 }
183
184 static int pathpointcnt(SplineSet *ss) {
185 SplinePoint *sp;
186 int cnt;
187
188 if ( ss==NULL ) /* No paths */
189 return( 0 );
190 if ( ss->next!=NULL ) /* more than one path */
191 return( -1 );
192 if ( ss->first->prev!=NULL )/* single path, but it is closed */
193 return( -2 );
194 for ( sp=ss->first, cnt=1; sp->next!=NULL; sp=sp->next->to, ++cnt );
195 return( cnt );
196 }
197
198 static int SVParseDlg(SearchView *sv, int check_replace ) {
199 int err=false;
200 double fudge;
201
202 fudge = GetReal8(sv->gw,CID_Fuzzy,_("Match Fuzziness:"),&err);
203 if ( err )
204 return( false );
205 old_fudge = fudge;
206
207 sv->sd.tryreverse = true;
208 sv->sd.tryflips = GGadgetIsChecked(GWidgetGetControl(sv->gw,CID_Flipping));
209 sv->sd.tryscale = GGadgetIsChecked(GWidgetGetControl(sv->gw,CID_Scaling));
210 sv->sd.tryrotate = GGadgetIsChecked(GWidgetGetControl(sv->gw,CID_Rotating));
211 sv->sd.endpoints = GGadgetIsChecked(GWidgetGetControl(sv->gw,CID_Endpoints));
212 sv->sd.onlyselected = GGadgetIsChecked(GWidgetGetControl(sv->gw,CID_Selected));
213
214 SVResetPaths(&sv->sd);
215 if ( pathpointcnt(sv->sd.path)==0 )
216 ff_post_error(_("Bad search pattern"),_("Nothing to match."));
217 else if ( sv->sd.endpoints ) {
218 if ( pathpointcnt(sv->sd.path)<3 ||
219 (check_replace && pathpointcnt(sv->sd.replacepath)<3 && pathpointcnt(sv->sd.replacepath)!=0 )) {
220 if ( pathpointcnt(sv->sd.path)<0 )
221 ff_post_error(_("Bad search pattern"),_("When \"Endpoints specify minimum length and direction only\" is checked, the search pattern must be a single open contour."));
222 else if ( pathpointcnt(sv->sd.path)<3 )
223 ff_post_error(_("Bad search pattern"),_("When \"Endpoints specify minimum length and direction only\" is checked, the search pattern must be a single open contour with at least 3 points on it (otherwise there is nothing to match)."));
224 else
225 ff_post_error(_("Bad replace pattern"),_("When \"Endpoints specify minimum length and direction only\" is checked, the replace pattern must be a single open contour with at least 3 points on it."));
226 return( false );
227 }
228 } else if ( pathpointcnt(sv->sd.path)>0 ) {
229 /* It might make sense not to do a sub-pattern search if the */
230 /* replace pattern is closed... but that's kind of weird */
231 if ( check_replace && pathpointcnt(sv->sd.replacepath)<0 ) {
232 ff_post_error(_("Bad replace pattern"),_("When the search path is a single open contour, the replace pattern must also be."));
233 return( false );
234 }
235 }
236
237 sv->sd.fudge = fudge;
238 sv->sd.fudge_percent = sv->sd.tryrotate ? .01 : .001;
239 return( true );
240 }
241
242 static int SV_Find(GGadget *g, GEvent *e) {
243 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) {
244 SearchView *sv = (SearchView *) ((CharViewBase *) GDrawGetUserData(GGadgetGetWindow(g)))->container;
245 if ( !SVParseDlg(sv,false))
246 return( true );
247 sv->sd.findall = sv->sd.replaceall = false;
248 DoFindOne(sv,false);
249 }
250 return( true );
251 }
252
253 static int SV_FindAll(GGadget *g, GEvent *e) {
254 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) {
255 SearchView *sv = (SearchView *) ((CharViewBase *) GDrawGetUserData(GGadgetGetWindow(g)))->container;
256 if ( !SVParseDlg(sv,false))
257 return( true );
258 sv->sd.findall = true;
259 sv->sd.replaceall = false;
260 DoFindAll(sv);
261 }
262 return( true );
263 }
264
265 static int SV_RplFind(GGadget *g, GEvent *e) {
266 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) {
267 SearchView *sv = (SearchView *) ((CharViewBase *) GDrawGetUserData(GGadgetGetWindow(g)))->container;
268 RefChar *rf;
269 if ( !SVParseDlg(sv,true))
270 return( true );
271 sv->sd.findall = sv->sd.replaceall = false;
272 for ( rf=sv->sd.sc_rpl.layers[ly_fore].refs; rf!=NULL; rf = rf->next ) {
273 if ( SCDependsOnSC(rf->sc,sv->sd.curchar)) {
274 ff_post_error(_("Self-referential glyph"),_("Attempt to make a glyph that refers to itself"));
275 return( true );
276 }
277 }
278 DoRpl(&sv->sd);
279 DoFindOne(sv,sv->sd.subpatternsearch);
280 }
281 return( true );
282 }
283
284 static int SV_RplAll(GGadget *g, GEvent *e) {
285 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) {
286 SearchView *sv = (SearchView *) ((CharViewBase *) GDrawGetUserData(GGadgetGetWindow(g)))->container;
287 if ( !SVParseDlg(sv,true))
288 return( true );
289 sv->sd.findall = false;
290 sv->sd.replaceall = true;
291 DoFindAll(sv);
292 }
293 return( true );
294 }
295
296 /* ************************************************************************** */
297
298 void SV_DoClose(struct cvcontainer *cvc) {
299 GDrawSetVisible(((SearchView *) cvc)->gw,false);
300 }
301
302 static int SV_Cancel(GGadget *g, GEvent *e) {
303 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate )
304 SV_DoClose(((CharViewBase *) GDrawGetUserData(GGadgetGetWindow(g)))->container);
305 return( true );
306 }
307
308 static void SVResize(SearchView *sv, GEvent *event) {
309 int width, height;
310
311 if ( !event->u.resize.sized )
312 return;
313
314 GGadgetMove(GWidgetGetControl(sv->gw,CID_TopBox),4,4);
315 GGadgetResize(GWidgetGetControl(sv->gw,CID_TopBox),
316 event->u.resize.size.width-8,
317 event->u.resize.size.height-12);
318
319 width = (event->u.resize.size.width-40)/2;
320 height = (event->u.resize.size.height-sv->cv_y-sv->button_height-8);
321 if ( width<70 || height<80 ) {
322 if ( width<70 ) width = 70;
323 width = 2*width+40;
324 if ( height<80 ) height = 80;
325 height += sv->cv_y+sv->button_height+8;
326 GDrawResize(sv->gw,width,height);
327 return;
328 }
329 if ( width!=sv->cv_width || height!=sv->cv_height ) {
330 GDrawResize(sv->cv_srch.gw,width,height);
331 GDrawResize(sv->cv_rpl.gw,width,height);
332 sv->cv_width = width; sv->cv_height = height;
333 sv->rpl_x = 30+width;
334 GDrawMove(sv->cv_rpl.gw,sv->rpl_x,sv->cv_y);
335 }
336
337 GDrawSync(NULL);
338 GDrawProcessPendingEvents(NULL);
339 GDrawRequestExpose(sv->gw,NULL,false);
340 }
341
342 void SVMakeActive(SearchView *sv,CharView *cv) {
343 GRect r;
344 if ( sv==NULL )
345 return;
346 sv->cv_srch.inactive = sv->cv_rpl.inactive = true;
347 cv->inactive = false;
348 GDrawSetUserData(sv->gw,cv);
349 GDrawRequestExpose(sv->cv_srch.v,NULL,false);
350 GDrawRequestExpose(sv->cv_rpl.v,NULL,false);
351 GDrawGetSize(sv->gw,&r);
352 r.x = 0;
353 r.y = sv->mbh;
354 r.height = sv->fh+10;
355 GDrawRequestExpose(sv->gw,&r,false);
356 }
357
358 static void SVChar(SearchView *sv, GEvent *event) {
359 if ( event->u.chr.keysym==GK_Tab || event->u.chr.keysym==GK_BackTab )
360 SVMakeActive(sv,sv->cv_srch.inactive ? &sv->cv_srch : &sv->cv_rpl);
361 else
362 CVChar(sv->cv_srch.inactive ? &sv->cv_rpl : &sv->cv_srch,event);
363 }
364
365 static void SVDraw(SearchView *sv, GWindow pixmap, GEvent *event) {
366 GRect r;
367
368 GDrawSetLineWidth(pixmap,0);
369 if ( sv->cv_srch.inactive )
370 GDrawSetFont(pixmap,sv->plain);
371 else
372 GDrawSetFont(pixmap,sv->bold);
373 GDrawDrawText8(pixmap,10,sv->mbh+5+sv->as,
374 _("Search Pattern:"),-1,MAIN_FOREGROUND);
375 if ( sv->cv_rpl.inactive )
376 GDrawSetFont(pixmap,sv->plain);
377 else
378 GDrawSetFont(pixmap,sv->bold);
379 GDrawDrawText8(pixmap,sv->rpl_x,sv->mbh+5+sv->as,
380 _("Replace Pattern:"),-1,MAIN_FOREGROUND);
381 r.x = 10-1; r.y=sv->cv_y-1;
382 r.width = sv->cv_width+1; r.height = sv->cv_height+1;
383 GDrawDrawRect(pixmap,&r,MAIN_FOREGROUND);
384 r.x = sv->rpl_x-1;
385 GDrawDrawRect(pixmap,&r,MAIN_FOREGROUND);
386 }
387
388 static void SVCheck(SearchView *sv) {
389 int show = ( sv->sd.sc_srch.layers[ly_fore].splines!=NULL || sv->sd.sc_srch.layers[ly_fore].refs!=NULL );
390 int showrplall=show, showrpl;
391
392 if ( sv->sd.sc_srch.changed_since_autosave && sv->showsfindnext ) {
393 GGadgetSetTitle8(GWidgetGetControl(sv->gw,CID_Find),_("Find"));
394 sv->showsfindnext = false;
395 }
396 if ( showrplall ) {
397 if ( sv->sd.sc_srch.layers[ly_fore].splines!=NULL && sv->sd.sc_srch.layers[ly_fore].splines->next==NULL &&
398 sv->sd.sc_srch.layers[ly_fore].splines->first->prev==NULL &&
399 sv->sd.sc_rpl.layers[ly_fore].splines==NULL && sv->sd.sc_rpl.layers[ly_fore].refs==NULL )
400 showrplall = false;
401 }
402 showrpl = showrplall;
403 if ( !sv->showsfindnext || sv->sd.curchar==NULL || sv->sd.curchar->parent!=sv->sd.fv->sf ||
404 sv->sd.curchar->orig_pos<0 || sv->sd.curchar->orig_pos>=sv->sd.fv->sf->glyphcnt ||
405 sv->sd.curchar!=sv->sd.fv->sf->glyphs[sv->sd.curchar->orig_pos] ||
406 sv->sd.curchar->changed_since_search )
407 showrpl = false;
408
409 if ( sv->findenabled != show ) {
410 GGadgetSetEnabled(GWidgetGetControl(sv->gw,CID_Find),show);
411 GGadgetSetEnabled(GWidgetGetControl(sv->gw,CID_FindAll),show);
412 sv->findenabled = show;
413 }
414 if ( sv->rplallenabled != showrplall ) {
415 GGadgetSetEnabled(GWidgetGetControl(sv->gw,CID_ReplaceAll),showrplall);
416 sv->rplallenabled = showrplall;
417 }
418 if ( sv->rplenabled != showrpl ) {
419 GGadgetSetEnabled(GWidgetGetControl(sv->gw,CID_Replace),showrpl);
420 sv->rplenabled = showrpl;
421 }
422 }
423
424 static void SearchViewFree(SearchView *sv) {
425 SplinePointListsFree(sv->sd.sc_srch.layers[ly_fore].splines);
426 SplinePointListsFree(sv->sd.sc_rpl.layers[ly_fore].splines);
427 RefCharsFree(sv->sd.sc_srch.layers[ly_fore].refs);
428 RefCharsFree(sv->sd.sc_rpl.layers[ly_fore].refs);
429 free(sv);
430 }
431
432 static int sv_e_h(GWindow gw, GEvent *event) {
433 SearchView *sv = (SearchView *) ((CharViewBase *) GDrawGetUserData(gw))->container;
434
435 switch ( event->type ) {
436 case et_expose:
437 SVDraw(sv,gw,event);
438 break;
439 case et_resize:
440 if ( event->u.resize.sized )
441 SVResize(sv,event);
442 break;
443 case et_char:
444 SVChar(sv,event);
445 break;
446 case et_timer:
447 SVCheck(sv);
448 break;
449 case et_close:
450 SV_DoClose((struct cvcontainer *) sv);
451 break;
452 case et_create:
453 break;
454 case et_destroy:
455 SearchViewFree(sv);
456 break;
457 case et_map:
458 if ( event->u.map.is_visible )
459 CVPaletteActivate(sv->cv_srch.inactive?&sv->cv_rpl:&sv->cv_srch);
460 else
461 CVPalettesHideIfMine(sv->cv_srch.inactive?&sv->cv_rpl:&sv->cv_srch);
462 sv->isvisible = event->u.map.is_visible;
463 break;
464 default: break;
465 }
466 return( true );
467 }
468
469 static void SVSetTitle(SearchView *sv) {
470 char ubuf[150];
471 sprintf(ubuf,_("Find in %.100s"),sv->sd.fv->sf->fontname);
472 GDrawSetWindowTitles8(sv->gw,ubuf,_("Find"));
473 }
474
475 int SVAttachFV(FontView *fv,int ask_if_difficult) {
476 int i, doit, pos, any=0, gid;
477 RefChar *r, *rnext, *rprev;
478
479 if ( searcher==NULL )
480 return( false );
481
482 if ( searcher->sd.fv==(FontViewBase *) fv )
483 return( true );
484 if ( searcher->sd.fv!=NULL && searcher->sd.fv->sf==fv->b.sf ) {
485 ((FontView *) searcher->sd.fv)->sv = NULL;
486 fv->sv = searcher;
487 searcher->sd.fv = (FontViewBase *) fv;
488 SVSetTitle(searcher);
489 searcher->sd.curchar = NULL;
490 return( true );
491 }
492
493 if ( searcher->dummy_sf.layers[ly_fore].order2 != fv->b.sf->layers[ly_fore].order2 ) {
494 SCClearContents(&searcher->sd.sc_srch,ly_fore);
495 SCClearContents(&searcher->sd.sc_rpl,ly_fore);
496 for ( i=0; i<searcher->sd.sc_srch.layer_cnt; ++i )
497 UndoesFree(searcher->sd.sc_srch.layers[i].undoes);
498 for ( i=0; i<searcher->sd.sc_rpl.layer_cnt; ++i )
499 UndoesFree(searcher->sd.sc_rpl.layers[i].undoes);
500 }
501
502 for ( doit=!ask_if_difficult; doit<=1; ++doit ) {
503 for ( i=0; i<2; ++i ) {
504 rprev = NULL;
505 for ( r = searcher->chars[i]->layers[ly_fore].refs; r!=NULL; r=rnext ) {
506 rnext = r->next;
507 pos = SFFindSlot(fv->b.sf,fv->b.map,r->sc->unicodeenc,r->sc->name);
508 gid = -1;
509 if ( pos!=-1 )
510 gid = fv->b.map->map[pos];
511 if ( (gid==-1 || fv->b.sf->glyphs[gid]!=NULL) && !doit ) {
512 char *buttons[3];
513 buttons[0] = _("Yes");
514 buttons[1] = _("Cancel");
515 buttons[2] = NULL;
516 if ( ask_if_difficult==2 && !searcher->isvisible )
517 return( false );
518 if ( gwwv_ask(_("Bad Reference"),(const char **) buttons,1,1,
519 _("The %1$s in the search dialog contains a reference to %2$.20hs which does not exist in the new font.\nShould I remove the reference?"),
520 i==0?_("Search Pattern"):_("Replace Pattern"),
521 r->sc->name)==1 )
522 return( false );
523 } else if ( !doit )
524 /* Do Nothing */;
525 else if ( gid==-1 || fv->b.sf->glyphs[gid]!=NULL ) {
526 if ( rprev==NULL )
527 searcher->chars[i]->layers[ly_fore].refs = rnext;
528 else
529 rprev->next = rnext;
530 RefCharFree(r);
531 any = true;
532 } else {
533 /*SplinePointListsFree(r->layers[0].splines); r->layers[0].splines = NULL;*/
534 r->sc = fv->b.sf->glyphs[gid];
535 r->orig_pos = gid;
536 SCReinstanciateRefChar(searcher->chars[i],r,fv->b.active_layer);
537 any = true;
538 rprev = r;
539 }
540 }
541 }
542 }
543 fv->sv = searcher;
544 searcher->sd.fv = (FontViewBase *) fv;
545 searcher->sd.curchar = NULL;
546 if ( any ) {
547 GDrawRequestExpose(searcher->cv_srch.v,NULL,false);
548 GDrawRequestExpose(searcher->cv_rpl.v,NULL,false);
549 }
550 SVSetTitle(searcher);
551 return( true );
552 }
553
554 void SVDetachFV(FontView *fv) {
555 FontView *other;
556
557 fv->sv = NULL;
558 if ( searcher==NULL || searcher->sd.fv!=(FontViewBase *) fv )
559 return;
560 SV_DoClose((struct cvcontainer *) searcher);
561 for ( other=fv_list; other!=NULL; other=(FontView *) other->b.next ) {
562 if ( other!=fv ) {
563 SVAttachFV(fv,false);
564 return;
565 }
566 }
567 }
568
569 static int SV_Can_Navigate(struct cvcontainer *cvc, enum nav_type type) {
570 return( false );
571 }
572
573 static int SV_Can_Open(struct cvcontainer *cvc) {
574 return( true );
575 }
576
577 static void SV_Do_Navigate(struct cvcontainer *cvc, enum nav_type type) {
578 }
579
580 static SplineFont *SF_Of_SV(struct cvcontainer *cvc) {
581 return( ((SearchView *) cvc)->sd.fv->sf );
582 }
583
584 struct cvcontainer_funcs searcher_funcs = {
585 cvc_searcher,
586 (void (*) (struct cvcontainer *cvc,CharViewBase *cv)) SVMakeActive,
587 (void (*) (struct cvcontainer *cvc,void *)) SVChar,
588 SV_Can_Navigate,
589 SV_Do_Navigate,
590 SV_Can_Open,
591 SV_DoClose,
592 SF_Of_SV
593 };
594
595 static SearchView *SVFillup(SearchView *sv, FontView *fv) {
596
597 SDFillup(&sv->sd,(FontViewBase *) fv);
598 sv->base.funcs = &searcher_funcs;
599
600 sv->chars[0] = &sv->sd.sc_srch;
601 sv->chars[1] = &sv->sd.sc_rpl;
602 sv->dummy_sf.glyphs = sv->chars;
603 sv->dummy_sf.glyphcnt = sv->dummy_sf.glyphmax = 2;
604 sv->dummy_sf.pfminfo.fstype = -1;
605 sv->dummy_sf.pfminfo.stylemap = -1;
606 sv->dummy_sf.fontname = sv->dummy_sf.fullname = sv->dummy_sf.familyname = "dummy";
607 sv->dummy_sf.weight = "Medium";
608 sv->dummy_sf.origname = "dummy";
609 sv->dummy_sf.ascent = fv->b.sf->ascent;
610 sv->dummy_sf.descent = fv->b.sf->descent;
611 sv->dummy_sf.layers = sv->layerinfo;
612 sv->dummy_sf.layer_cnt = 2;
613 sv->layerinfo[ly_back].order2 = fv->b.sf->layers[ly_back].order2;
614 sv->layerinfo[ly_back].name = _("Back");
615 sv->layerinfo[ly_fore].order2 = fv->b.sf->layers[ly_fore].order2;
616 sv->layerinfo[ly_fore].name = _("Fore");
617 sv->dummy_sf.grid.order2 = fv->b.sf->grid.order2;
618 sv->dummy_sf.anchor = fv->b.sf->anchor;
619 sv->sd.sc_srch.width = sv->sd.sc_srch.vwidth = sv->sd.sc_rpl.vwidth = sv->sd.sc_rpl.width =
620 sv->dummy_sf.ascent + sv->dummy_sf.descent;
621 sv->sd.sc_srch.parent = sv->sd.sc_rpl.parent = &sv->dummy_sf;
622
623 sv->dummy_sf.fv = (FontViewBase *) &sv->dummy_fv;
624 sv->dummy_fv.b.active_layer = ly_fore;
625 sv->dummy_fv.b.sf = &sv->dummy_sf;
626 sv->dummy_fv.b.selected = sv->sel;
627 sv->dummy_fv.cbw = sv->dummy_fv.cbh = default_fv_font_size+1;
628 sv->dummy_fv.magnify = 1;
629
630 sv->cv_srch.b.container = (struct cvcontainer *) sv;
631 sv->cv_rpl.inactive = true;
632 sv->cv_rpl.b.container = (struct cvcontainer *) sv;
633
634 sv->dummy_fv.b.map = &sv->dummy_map;
635 sv->dummy_map.map = sv->map;
636 sv->dummy_map.backmap = sv->backmap;
637 sv->dummy_map.enccount = sv->dummy_map.encmax = sv->dummy_map.backmax = 2;
638 sv->dummy_map.enc = &custom;
639
640 sv->sd.fv = (FontViewBase *) fv;
641 if ( fv!=NULL )
642 fv->sv = sv;
643 return( sv );
644 }
645
646 SearchView *SVCreate(FontView *fv) {
647 SearchView *sv;
648 GRect pos;
649 GWindow gw;
650 GWindowAttrs wattrs;
651 GGadgetCreateData gcd[14], boxes[6], *butarray[14], *allowarray[6],
652 *fudgearray[4], *halfarray[3], *varray[14];
653 GTextInfo label[14];
654 int as, ds, ld;
655 char fudgebuf[20];
656 int k, sel_pos, efdo_pos;
657
658 if ( searcher!=NULL ) {
659 if ( SVAttachFV(fv,true)) {
660 GDrawSetVisible(fv->sv->gw,true);
661 GDrawRaise(fv->sv->gw);
662 return( searcher );
663 } else
664 return( NULL );
665 }
666
667 searcher = sv = SVFillup( calloc(1,sizeof(SearchView)), fv );
668
669 memset(&wattrs,0,sizeof(wattrs));
670 wattrs.mask = wam_events|wam_cursor|wam_isdlg/*|wam_icon*/;
671 wattrs.is_dlg = true;
672 wattrs.event_masks = -1;
673 wattrs.event_masks = -1;
674 wattrs.cursor = ct_pointer;
675 /*wattrs.icon = icon;*/
676 pos.width = 600;
677 pos.height = 400;
678 pos.x = GGadgetScale(104)+6;
679 DefaultY(&pos);
680 sv->gw = gw = GDrawCreateTopWindow(NULL,&pos,sv_e_h,&sv->cv_srch,&wattrs);
681 SVSetTitle(sv);
682
683 sv->plain = searchview_font.fi;
684 sv->bold = searchview_boldfont.fi;
685 GDrawWindowFontMetrics(sv->gw,sv->plain,&as,&ds,&ld);
686 sv->fh = as+ds; sv->as = as;
687
688 SVCharViewInits(sv);
689
690 memset(&label,0,sizeof(label));
691 memset(&gcd,0,sizeof(gcd));
692 memset(&boxes,0,sizeof(boxes));
693
694 k=0;
695
696 label[k].text = (unichar_t *) _("Allow:");
697 label[k].text_is_1byte = true;
698 gcd[k].gd.label = &label[k];
699 gcd[k].gd.pos.x = 5; gcd[k].gd.pos.y = GDrawPixelsToPoints(NULL,sv->cv_y+sv->cv_height+8);
700 gcd[k].gd.flags = gg_enabled|gg_visible;
701 gcd[k].gd.cid = CID_Allow;
702 gcd[k].gd.popup_msg = _("Allow a match even if the search pattern has\nto be transformed by a combination of the\nfollowing transformations.");
703 gcd[k].creator = GLabelCreate;
704 allowarray[k] = &gcd[k]; ++k;
705
706 label[k].text = (unichar_t *) _("Flipping");
707 label[k].text_is_1byte = true;
708 gcd[k].gd.label = &label[k];
709 gcd[k].gd.pos.x = 35; gcd[k].gd.pos.y = gcd[0].gd.pos.y-3;
710 gcd[k].gd.flags = gg_enabled|gg_visible|gg_cb_on;
711 gcd[k].gd.cid = CID_Flipping;
712 gcd[k].gd.popup_msg = _("Allow a match even if the search pattern has\nto be transformed by a combination of the\nfollowing transformations.");
713 gcd[k].creator = GCheckBoxCreate;
714 allowarray[k] = &gcd[k]; ++k;
715
716 label[k].text = (unichar_t *) _("Scaling");
717 label[k].text_is_1byte = true;
718 gcd[k].gd.label = &label[k];
719 gcd[k].gd.pos.x = 100; gcd[k].gd.pos.y = gcd[1].gd.pos.y;
720 gcd[k].gd.flags = gg_enabled|gg_visible;
721 gcd[k].gd.cid = CID_Scaling;
722 gcd[k].gd.popup_msg = _("Allow a match even if the search pattern has\nto be transformed by a combination of the\nfollowing transformations.");
723 gcd[k].creator = GCheckBoxCreate;
724 allowarray[k] = &gcd[k]; ++k;
725
726 label[k].text = (unichar_t *) _("Rotating");
727 label[k].text_is_1byte = true;
728 gcd[k].gd.label = &label[k];
729 gcd[k].gd.pos.x = 170; gcd[k].gd.pos.y = gcd[1].gd.pos.y;
730 gcd[k].gd.flags = gg_enabled|gg_visible;
731 gcd[k].gd.cid = CID_Rotating;
732 gcd[k].gd.popup_msg = _("Allow a match even if the search pattern has\nto be transformed by a combination of the\nfollowing transformations.");
733 gcd[k].creator = GCheckBoxCreate;
734 allowarray[k] = &gcd[k]; allowarray[++k] = GCD_Glue; allowarray[5] = NULL;
735
736 label[k].text = (unichar_t *) _("_Match Fuzziness:");
737 label[k].text_is_1byte = true;
738 label[k].text_in_resource = true;
739 gcd[k].gd.label = &label[k];
740 gcd[k].gd.flags = gg_enabled|gg_visible;
741 gcd[k].creator = GLabelCreate;
742 fudgearray[0] = &gcd[k++];
743
744 sprintf(fudgebuf,"%g",old_fudge);
745 label[k].text = (unichar_t *) fudgebuf;
746 label[k].text_is_1byte = true;
747 label[k].text_in_resource = true;
748 gcd[k].gd.label = &label[k];
749 gcd[k].gd.flags = gg_enabled|gg_visible;
750 gcd[k].gd.cid = CID_Fuzzy;
751 gcd[k].creator = GTextFieldCreate;
752 fudgearray[1] = &gcd[k++]; fudgearray[2] = GCD_Glue; fudgearray[3] = NULL;
753
754 efdo_pos = k;
755 label[k].text = (unichar_t *) _("Endpoints specify minimum length and direction only");
756 label[k].text_is_1byte = true;
757 gcd[k].gd.label = &label[k];
758 gcd[k].gd.pos.x = 5; gcd[k].gd.pos.y = gcd[1].gd.pos.y+18;
759 gcd[k].gd.flags = gg_enabled|gg_visible;
760 gcd[k].gd.cid = CID_Endpoints;
761 gcd[k].gd.popup_msg = _(
762 "If the search pattern is a single open contour\n"
763 "then do not match the end points. They merely\n"
764 "specify the direction from which the curve should\n"
765 "move toward the next point (which will be matched),\n"
766 "and the minimum distance between the first matched\n"
767 "point and the one before it. The endpoints of the\n"
768 "replace contour will also only be used for positioning.\n"
769 "\n"
770 "This allows you to match a right angle corner\n"
771 "without needed to specify exactly how long the edges\n"
772 "are which form the right angle.");
773 gcd[k++].creator = GCheckBoxCreate;
774
775 sel_pos = k;
776 label[k].text = (unichar_t *) _("Search Selected Chars Only");
777 label[k].text_is_1byte = true;
778 gcd[k].gd.label = &label[k];
779 gcd[k].gd.pos.x = 5; gcd[k].gd.pos.y = gcd[1].gd.pos.y+18;
780 gcd[k].gd.flags = gg_enabled|gg_visible;
781 gcd[k].gd.cid = CID_Selected;
782 gcd[k].gd.popup_msg = _("Only search characters selected in the fontview.\nNormally we search all characters in the font.");
783 gcd[k++].creator = GCheckBoxCreate;
784
785 label[k].text = (unichar_t *) _("Find Next"); /* Start with this to allow sufficient space */
786 label[k].text_is_1byte = true;
787 gcd[k].gd.label = &label[k];
788 gcd[k].gd.pos.x = 5; gcd[k].gd.pos.y = gcd[sel_pos].gd.pos.y+24;
789 gcd[k].gd.flags = gg_visible|gg_but_default;
790 gcd[k].gd.cid = CID_Find;
791 gcd[k].gd.handle_controlevent = SV_Find;
792 gcd[k].creator = GButtonCreate;
793 butarray[0] = GCD_Glue; butarray[1] = GCD_Glue; butarray[2] = &gcd[k++];
794
795 label[k].text = (unichar_t *) _("Find All");
796 label[k].text_is_1byte = true;
797 gcd[k].gd.label = &label[k];
798 gcd[k].gd.pos.x = 0; gcd[k].gd.pos.y = gcd[k-1].gd.pos.y+3;
799 gcd[k].gd.flags = gg_visible;
800 gcd[k].gd.cid = CID_FindAll;
801 gcd[k].gd.handle_controlevent = SV_FindAll;
802 gcd[k].creator = GButtonCreate;
803 butarray[3] = GCD_Glue; butarray[4] = &gcd[k++];
804
805 label[k].text = (unichar_t *) _("Replace");
806 label[k].text_is_1byte = true;
807 gcd[k].gd.label = &label[k];
808 gcd[k].gd.pos.x = 0; gcd[k].gd.pos.y = gcd[k-1].gd.pos.y;
809 gcd[k].gd.flags = gg_visible;
810 gcd[k].gd.cid = CID_Replace;
811 gcd[k].gd.handle_controlevent = SV_RplFind;
812 gcd[k].creator = GButtonCreate;
813 butarray[5] = GCD_Glue; butarray[6] = &gcd[k++];
814
815 label[k].text = (unichar_t *) _("Replace All");
816 label[k].text_is_1byte = true;
817 gcd[k].gd.label = &label[k];
818 gcd[k].gd.pos.x = 0; gcd[k].gd.pos.y = gcd[k-2].gd.pos.y;
819 gcd[k].gd.flags = gg_visible;
820 gcd[k].gd.cid = CID_ReplaceAll;
821 gcd[k].gd.handle_controlevent = SV_RplAll;
822 gcd[k].creator = GButtonCreate;
823 butarray[7] = GCD_Glue; butarray[8] = &gcd[k++];
824
825 label[k].text = (unichar_t *) _("_Cancel");
826 label[k].text_is_1byte = true;
827 label[k].text_in_resource = true;
828 gcd[k].gd.label = &label[k];
829 gcd[k].gd.pos.x = 0; gcd[k].gd.pos.y = gcd[k-3].gd.pos.y;
830 gcd[k].gd.flags = gg_enabled|gg_visible|gg_but_cancel;
831 gcd[k].gd.cid = CID_Cancel;
832 gcd[k].gd.handle_controlevent = SV_Cancel;
833 gcd[k].creator = GButtonCreate;
834 butarray[9] = GCD_Glue; butarray[10] = &gcd[k++];
835 butarray[11] = GCD_Glue; butarray[12] = GCD_Glue; butarray[13] = NULL;
836
837 boxes[2].gd.flags = gg_enabled|gg_visible;
838 boxes[2].gd.u.boxelements = allowarray;
839 boxes[2].creator = GHBoxCreate;
840
841 boxes[3].gd.flags = gg_enabled|gg_visible;
842 boxes[3].gd.u.boxelements = fudgearray;
843 boxes[3].creator = GHBoxCreate;
844 halfarray[0] = &boxes[2]; halfarray[1] = &boxes[3]; halfarray[2] = NULL;
845
846 boxes[4].gd.flags = gg_enabled|gg_visible;
847 boxes[4].gd.u.boxelements = halfarray;
848 boxes[4].creator = GHBoxCreate;
849
850 boxes[5].gd.flags = gg_enabled|gg_visible;
851 boxes[5].gd.u.boxelements = butarray;
852 boxes[5].creator = GHBoxCreate;
853
854 varray[0] = GCD_Glue; varray[1] = NULL;
855 varray[2] = &boxes[4]; varray[3] = NULL;
856 varray[4] = &gcd[efdo_pos]; varray[5] = NULL;
857 varray[6] = GCD_Glue; varray[7] = NULL;
858 varray[8] = &gcd[sel_pos]; varray[9] = NULL;
859 varray[10] = &boxes[5]; varray[11]= NULL;
860 varray[12] = NULL;
861
862 boxes[0].gd.pos.x = boxes[0].gd.pos.y = 2;
863 boxes[0].gd.flags = gg_enabled|gg_visible;
864 boxes[0].gd.u.boxelements = varray;
865 boxes[0].gd.cid = CID_TopBox;
866 boxes[0].creator = GHVGroupCreate;
867
868 GGadgetsCreate(gw,boxes);
869
870 GHVBoxSetExpandableRow(boxes[0].ret,0);
871 GHVBoxSetPadding(boxes[2].ret,6,3);
872 GHVBoxSetExpandableCol(boxes[2].ret,gb_expandglue);
873 GHVBoxSetExpandableCol(boxes[3].ret,gb_expandglue);
874 GHVBoxSetExpandableCol(boxes[5].ret,gb_expandglue);
875 GGadgetResize(boxes[0].ret,pos.width,pos.height);
876
877 GGadgetSetTitle8(GWidgetGetControl(gw,CID_Find),_("Find"));
878 sv->showsfindnext = false;
879 GDrawRequestTimer(gw,1000,1000,NULL);
880 sv->button_height = GDrawPointsToPixels(gw,100);
881 GDrawResize(gw,650,400); /* Force a resize event */
882
883 GDrawSetVisible(sv->gw,true);
884 return( sv );
885 }
886
887 void SVDestroy(SearchView *sv) {
888
889 if ( sv==NULL )
890 return;
891
892 SDDestroy(&sv->sd);
893 free(sv);
894 }
895
896 void FVReplaceOutlineWithReference( FontView *fv, double fudge ) {
897
898 if ( fv->v!=NULL )
899 GDrawSetCursor(fv->v,ct_watch);
900
901 FVBReplaceOutlineWithReference((FontViewBase *) fv, fudge);
902
903 if ( fv->v!=NULL ) {
904 GDrawRequestExpose(fv->v,NULL,false);
905 GDrawSetCursor(fv->v,ct_pointer);
906 }
907 }