1 /************************************************************************/
2 /*									*/
3 /*  Ted: Link page on format tool.					*/
4 /*									*/
5 /************************************************************************/
6 
7 #   include	"tedConfig.h"
8 
9 #   include	<stddef.h>
10 #   include	<stdio.h>
11 
12 #   include	"tedAppFront.h"
13 #   include	"tedLinkTool.h"
14 #   include	"tedToolUtil.h"
15 #   include	<appInspector.h>
16 #   include	<docField.h>
17 #   include	<docHyperlinkField.h>
18 #   include	<docBookmarkField.h>
19 #   include	<guiToolUtil.h>
20 #   include	<guiTextUtil.h>
21 #   include	<docEditCommand.h>
22 
23 #   include	<appDebugon.h>
24 
25 /************************************************************************/
26 /*									*/
27 /*  A)  A Bookmark was chosen in the list.				*/
28 /*  B)  Something was typed in the 'Destination' text widgets.		*/
29 /*									*/
30 /************************************************************************/
31 
tedLinkAdaptToText(LinkTool * lt)32 static void tedLinkAdaptToText(	LinkTool *		lt )
33     {
34     int				gotSomething;
35     int				changed;
36     int				canSetLink;
37 
38     BookmarkList *		bl= &(lt->ltBookmarkList);
39 
40     const HyperlinkField *	hfChosen= &(lt->ltLinkChosen);
41     const HyperlinkField *	hfSet= &(lt->ltLinkSet);
42 
43     lt->ltInProgrammaticChange++;
44 
45     bl->blIsLocal= ! docHyperlinkFieldIsExtern( hfChosen );
46     gotSomething= docHyperlinkFieldIsLink( hfChosen );
47 
48     changed= ! docEqualHyperlinkFields( hfChosen, hfSet );
49     canSetLink= lt->ltCanChange && changed && gotSomething;
50 
51     guiEnableWidget( lt->ltSetLinkButton, canSetLink );
52     guiEnableWidget( lt->ltRevertButton, changed );
53 
54     guiEnableWidget( lt->ltRemoveLinkButton,
55 				lt->ltCanChange && gotSomething && ! changed );
56 
57     guiEnableWidget( lt->ltFollowLinkButton, gotSomething );
58 
59     guiEnableWidget( bl->blMarkListWidget,
60 				    lt->ltCanChange && bl->blIsLocal );
61 
62     guiEnableText( lt->ltFileTextWidget, lt->ltCanChange );
63     guiEnableText( bl->blMarkTextWidget, lt->ltCanChange );
64 
65     lt->ltInProgrammaticChange--;
66 
67     return;
68     }
69 
tedLinkAdaptToValues(LinkTool * lt,int adaptFile,int adaptMark,int search,int select)70 static void tedLinkAdaptToValues(	LinkTool *		lt,
71 					int			adaptFile,
72 					int			adaptMark,
73 					int			search,
74 					int			select )
75     {
76     BookmarkList *		bl= &(lt->ltBookmarkList);
77     const HyperlinkField *	hfChosen= &(lt->ltLinkChosen);
78 
79     bl->blIsLocal= ! docHyperlinkFieldIsExtern( hfChosen );
80 
81     if  ( search )
82 	{
83 	tedBookmarkFindChosen( bl, lt->ltApplication, &(hfChosen->hfBookmark) );
84 	}
85 
86     lt->ltInProgrammaticChange++;
87 
88     if  ( adaptFile)
89 	{
90 	guiBufferToText( lt->ltFileTextWidget, &(hfChosen->hfFile) );
91 	}
92 
93     if  ( adaptMark )
94 	{
95 	guiBufferToText( bl->blMarkTextWidget, &(hfChosen->hfBookmark) );
96 	}
97 
98     if  ( select )
99 	{
100 	tedBookmarkUpdateSelectionInList( bl, &(hfChosen->hfBookmark) );
101 	}
102 
103     tedLinkAdaptToText( lt );
104 
105     lt->ltInProgrammaticChange--;
106 
107     return;
108     }
109 
110 /*  A  */
APP_LIST_CALLBACK_H(tedLinkBookmarkChosen,w,voidlt,voidlcs)111 static APP_LIST_CALLBACK_H( tedLinkBookmarkChosen, w, voidlt, voidlcs )
112     {
113     LinkTool *			lt= (LinkTool *)voidlt;
114     BookmarkList *		bl= &(lt->ltBookmarkList);
115     HyperlinkField *		hfChosen= &(lt->ltLinkChosen);
116 
117     lt->ltInProgrammaticChange++;
118 
119     tedBookmarkListToText( bl, &(hfChosen->hfBookmark), voidlcs, w );
120     tedLinkAdaptToText( lt );
121 
122     lt->ltInProgrammaticChange--;
123 
124     return;
125     }
126 
127 /*  B  */
APP_TXTYPING_CALLBACK_H(tedLinkFileChanged,w,voidlt)128 static APP_TXTYPING_CALLBACK_H( tedLinkFileChanged, w, voidlt )
129     {
130     LinkTool *			lt= (LinkTool *)voidlt;
131 
132     if  ( ! lt->ltInProgrammaticChange )
133 	{
134 	HyperlinkField *	hfChosen= &(lt->ltLinkChosen);
135 
136 	const int		adaptToFile= 0;
137 	const int		adaptToMark= 0;
138 	const int		search= 1;
139 	const int		select= 1;
140 
141 	if  ( appBufferFromTextWidget( &(hfChosen->hfFile),
142 						    lt->ltFileTextWidget ) )
143 	    { LDEB(1); return;	}
144 
145 	tedLinkAdaptToValues( lt, adaptToFile, adaptToMark, search, select );
146 	}
147 
148     return;
149     }
150 
APP_TXTYPING_CALLBACK_H(tedLinkMarkChanged,w,voidlt)151 static APP_TXTYPING_CALLBACK_H( tedLinkMarkChanged, w, voidlt )
152     {
153     LinkTool *			lt= (LinkTool *)voidlt;
154     BookmarkList *		bl= &(lt->ltBookmarkList);
155 
156     if  ( ! lt->ltInProgrammaticChange )
157 	{
158 	HyperlinkField *	hfChosen= &(lt->ltLinkChosen);
159 
160 	const int		adaptToFile= 0;
161 	const int		adaptToMark= 0;
162 	const int		search= 1;
163 	const int		select= 1;
164 
165 	if  ( appBufferFromTextWidget( &(hfChosen->hfBookmark),
166 						    bl->blMarkTextWidget ) )
167 	    { LDEB(1); return;	}
168 
169 	tedLinkAdaptToValues( lt, adaptToFile, adaptToMark, search, select );
170 	}
171 
172     return;
173     }
174 
175 /************************************************************************/
176 /*									*/
177 /*  'Revert' was pushed.						*/
178 /*									*/
179 /************************************************************************/
180 
APP_BUTTON_CALLBACK_H(tedLinkRevertPushed,w,voidlt)181 static APP_BUTTON_CALLBACK_H( tedLinkRevertPushed, w, voidlt )
182     {
183     LinkTool *		lt= (LinkTool *)voidlt;
184 
185     const int		adaptToFile= 1;
186     const int		adaptToMark= 1;
187     const int		search= 1;
188     const int		select= 1;
189 
190     if  ( docCopyHyperlinkField( &(lt->ltLinkChosen), &(lt->ltLinkSet) ) )
191 	{ LDEB(1); return;	}
192 
193     tedLinkAdaptToValues( lt, adaptToFile, adaptToMark, search, select );
194 
195     return;
196     }
197 
198 /************************************************************************/
199 /*									*/
200 /*  'Change' was pushed.						*/
201 /*									*/
202 /************************************************************************/
203 
APP_BUTTON_CALLBACK_H(tedLinkChangePushed,w,voidlt)204 static APP_BUTTON_CALLBACK_H( tedLinkChangePushed, w, voidlt )
205     {
206     LinkTool *			lt= (LinkTool *)voidlt;
207     BookmarkList *		bl= &(lt->ltBookmarkList);
208     HyperlinkField *		hfChosen= &(lt->ltLinkChosen);
209     EditApplication *		ea= lt->ltApplication;
210 
211     if  ( appBufferFromTextWidget( &(hfChosen->hfFile),
212 						    lt->ltFileTextWidget ) )
213 	{ LDEB(1); return;	}
214     if  ( appBufferFromTextWidget( &(hfChosen->hfBookmark),
215 						    bl->blMarkTextWidget ) )
216 	{ LDEB(1); return;	}
217 
218     if  ( docAdaptBookmarkName( &(hfChosen->hfBookmark) ) )
219 	{
220 	lt->ltInProgrammaticChange++;
221 
222 	guiBufferToText( bl->blMarkTextWidget,
223 					    &(hfChosen->hfBookmark) );
224 	appRefuseTextValue( bl->blMarkTextWidget );
225 
226 	lt->ltInProgrammaticChange--;
227 	return;
228 	}
229 
230     if  ( tedAppSetHyperlink( ea, hfChosen ) )
231 	{ LDEB(1);	}
232 
233     return;
234     }
235 
236 /************************************************************************/
237 /*									*/
238 /*  'Follow Link' was pushed.						*/
239 /*									*/
240 /************************************************************************/
241 
APP_BUTTON_CALLBACK_H(tedLinkFollowPushed,w,voidlt)242 static APP_BUTTON_CALLBACK_H( tedLinkFollowPushed, w, voidlt )
243     {
244     LinkTool *			lt= (LinkTool *)voidlt;
245     BookmarkList *		bl= &(lt->ltBookmarkList);
246     HyperlinkField *		hfChosen= &(lt->ltLinkChosen);
247     EditApplication *		ea= lt->ltApplication;
248 
249     if  ( appBufferFromTextWidget( &(hfChosen->hfFile),
250 						    lt->ltFileTextWidget ) )
251 	{ LDEB(1); return;	}
252     if  ( appBufferFromTextWidget( &(hfChosen->hfBookmark),
253 						    bl->blMarkTextWidget ) )
254 	{ LDEB(1); return;	}
255 
256     if  ( tedAppFollowLink( w, ea,
257 			    &(hfChosen->hfFile), &(hfChosen->hfBookmark) ) )
258 	{ LDEB(1);	}
259 
260     return;
261     }
262 
263 /************************************************************************/
264 /*									*/
265 /*  'Remove Link' was pushed.						*/
266 /*									*/
267 /************************************************************************/
268 
APP_BUTTON_CALLBACK_H(tedLinkRemovePushed,w,voidlt)269 static APP_BUTTON_CALLBACK_H( tedLinkRemovePushed, w, voidlt )
270     {
271     LinkTool *		lt= (LinkTool *)voidlt;
272     EditApplication *	ea= lt->ltApplication;
273 
274     if  ( tedAppRemoveHyperlink( ea ) )
275 	{ LDEB(1);	}
276 
277     return;
278     }
279 
280 /************************************************************************/
281 /*									*/
282 /*  Make a link tool. I.E. the 'Link' page on the format tool to manage	*/
283 /*  Hyperlinks.								*/
284 /*									*/
285 /************************************************************************/
286 
tedFillLinkTool(LinkTool * lt,const LinkToolResources * lpr,InspectorSubject * is,APP_WIDGET pageWidget,const InspectorSubjectResources * isr)287 void tedFillLinkTool(		LinkTool *			lt,
288 				const LinkToolResources *	lpr,
289 				InspectorSubject *		is,
290 				APP_WIDGET			pageWidget,
291 				const InspectorSubjectResources * isr )
292     {
293     BookmarkList *	bl= &(lt->ltBookmarkList);
294     APP_WIDGET		label;
295     APP_WIDGET		row;
296 
297     const int		listVisibleItems= 5;
298 
299     lt->ltPageResources= lpr;
300 
301     /* FULL WIDTH */
302     appMakeLabelInColumn( &label, pageWidget, lpr->lprFileText );
303     appMakeTextInColumn( &(lt->ltFileTextWidget), pageWidget, 0, 1 );
304     appMakeLabelInColumn( &(bl->blMarkLabelWidget),
305 				pageWidget, lpr->lprMarkText );
306     appMakeTextInColumn( &(bl->blMarkTextWidget), pageWidget, 0, 1 );
307 
308     appGuiSetTypingCallbackForText( lt->ltFileTextWidget,
309 					    tedLinkFileChanged, (void *)lt );
310     appGuiSetTypingCallbackForText( bl->blMarkTextWidget,
311 					    tedLinkMarkChanged, (void *)lt );
312 
313     appGuiMakeListInColumn( &(bl->blMarkListWidget),
314 		pageWidget, listVisibleItems,
315 		tedLinkBookmarkChosen, (APP_BUTTON_CALLBACK_T)0, (void *)lt );
316 
317     guiToolMake2BottonRow( &row, pageWidget,
318 	    &(lt->ltFollowLinkButton), &(lt->ltRemoveLinkButton),
319 	    isr->isrSelectButtonText, isr->isrDeleteButtonText,
320 	    tedLinkFollowPushed, tedLinkRemovePushed, (void *)lt );
321 
322     guiToolMake2BottonRow( &(is->isApplyRow), pageWidget,
323 	    &(is->isRevertButton), &(is->isApplyButton),
324 	    isr->isrRevert, isr->isrApplyToSubject,
325 	    tedLinkRevertPushed, tedLinkChangePushed, (void *)lt );
326 
327     lt->ltSetLinkButton= is->isApplyButton;
328     lt->ltRevertButton= is->isRevertButton;
329 
330     return;
331     }
332 
333 /************************************************************************/
334 /*									*/
335 /*  Initialize/Clean a link tool					*/
336 /*									*/
337 /************************************************************************/
338 
tedInitLinkTool(LinkTool * lt)339 void tedInitLinkTool(		LinkTool *		lt )
340     {
341     lt->ltCurrentDocumentId= 0;
342 
343     tedInitBookmarkList( &(lt->ltBookmarkList) );
344 
345     lt->ltSetLinkButton= (APP_WIDGET)0;
346     lt->ltRevertButton= (APP_WIDGET)0;
347     lt->ltFollowLinkButton= (APP_WIDGET)0;
348     lt->ltRemoveLinkButton= (APP_WIDGET)0;
349 
350     docInitHyperlinkField( &(lt->ltLinkChosen) );
351     docInitHyperlinkField( &(lt->ltLinkSet) );
352 
353     lt->ltInProgrammaticChange= 0;
354 
355     return;
356     }
357 
tedFormatCleanLinkTool(LinkTool * lt)358 void tedFormatCleanLinkTool(		LinkTool *		lt )
359     {
360     docCleanHyperlinkField( &(lt->ltLinkChosen) );
361     docCleanHyperlinkField( &(lt->ltLinkSet) );
362 
363     return;
364     }
365 
366 /************************************************************************/
367 /*									*/
368 /*  Finish the tabs page.						*/
369 /*									*/
370 /************************************************************************/
371 
tedLinkToolFillChoosers(LinkTool * lt)372 void tedLinkToolFillChoosers(	LinkTool *			lt )
373     {
374     return;
375     }
376 
tedFinishLinkTool(LinkTool * lt)377 void tedFinishLinkTool(		LinkTool *			lt )
378     {
379     return;
380     }
381 
382 /************************************************************************/
383 /*									*/
384 /*  Refresh the link tool.						*/
385 /*									*/
386 /************************************************************************/
387 
tedRefreshLinkTool(LinkTool * lt,int * pEnabled,int * pPref,InspectorSubject * is,const DocumentSelection * ds,const SelectionDescription * sd,BufferDocument * bd,const unsigned char * cmdEnabled)388 void tedRefreshLinkTool(	LinkTool *			lt,
389 				int *				pEnabled,
390 				int *				pPref,
391 				InspectorSubject *		is,
392 				const DocumentSelection *	ds,
393 				const SelectionDescription *	sd,
394 				BufferDocument *		bd,
395 				const unsigned char *		cmdEnabled )
396     {
397     DocumentField *		dfHyperlink;
398 
399     lt->ltCanChange= cmdEnabled[EDITcmdSET_HYPERLINK];
400 
401     if  ( lt->ltCurrentDocumentId != sd->sdDocumentId )
402 	{
403 	const int	includeTocMarks= 0;
404 
405 	lt->ltInProgrammaticChange++;
406 	tedFillBookmarkList( &(lt->ltBookmarkList), includeTocMarks, bd );
407 	lt->ltInProgrammaticChange--;
408 
409 	lt->ltCurrentDocumentId= sd->sdDocumentId;
410 	}
411 
412     docCleanHyperlinkField( &(lt->ltLinkChosen) );
413     docInitHyperlinkField( &(lt->ltLinkChosen) );
414 
415     docCleanHyperlinkField( &(lt->ltLinkSet) );
416     docInitHyperlinkField( &(lt->ltLinkSet) );
417 
418     dfHyperlink= docFindTypedFieldForPosition( bd, &(ds->dsHead),
419 							DOCfkHYPERLINK, 0 );
420     if  ( dfHyperlink )
421 	{
422 	const int		adaptToFile= 1;
423 	const int		adaptToMark= 1;
424 	const int		search= 1;
425 	const int		select= 1;
426 
427 	if  ( docGetHyperlinkField( &(lt->ltLinkSet), dfHyperlink ) )
428 	    { LDEB(1); return;	}
429 	if  ( docCopyHyperlinkField( &(lt->ltLinkChosen), &(lt->ltLinkSet) ) )
430 	    { LDEB(1); return;	}
431 
432 	tedLinkAdaptToValues( lt, adaptToFile, adaptToMark, search, select );
433 
434 	*pPref += 3;
435 	}
436     else{
437 	const int		adaptToFile= 1;
438 	const int		adaptToMark= 1;
439 	const int		search= 0;
440 	const int		select= 1;
441 
442 	lt->ltBookmarkList.blMarkChosenExists= 0;
443 	tedLinkAdaptToValues( lt, adaptToFile, adaptToMark, search, select );
444 	}
445 
446     guiEnableWidget( lt->ltSetLinkButton, 0 && lt->ltCanChange );
447     guiEnableWidget( lt->ltRevertButton, 0 );
448 
449     return;
450     }
451 
452 /************************************************************************/
453 /*									*/
454 /*  Get link tool resources.						*/
455 /*  Resource table for various texts.					*/
456 /*									*/
457 /************************************************************************/
458 
459 static AppConfigurableResource TED_TedLinkSubjectResourceTable[]=
460     {
461     APP_RESOURCE( "tableToolLink",
462 		offsetof(InspectorSubjectResources,isrSubjectName),
463 		"Hyperlink" ),
464 
465     APP_RESOURCE( "hyperlinkSetLink",
466 		offsetof(InspectorSubjectResources,isrApplyToSubject),
467 		"Set Link" ),
468     APP_RESOURCE( "hyperlinkRevert",
469 		offsetof(InspectorSubjectResources,isrRevert),
470 		"Revert" ),
471 
472     APP_RESOURCE( "hyperlinkFollowLink",
473 		offsetof(InspectorSubjectResources,isrSelectButtonText),
474 		"Follow Link" ),
475     APP_RESOURCE( "hyperlinkRemoveLink",
476 		offsetof(InspectorSubjectResources,isrDeleteButtonText),
477 		"Remove Link" ),
478     };
479 
480 static AppConfigurableResource TED_TedLinkToolResourceTable[]=
481 {
482     APP_RESOURCE( "hyperlinkDocument",
483 		offsetof(LinkToolResources,lprFileText),
484 		"Document" ),
485     APP_RESOURCE( "hyperlinkBookmark",
486 		offsetof(LinkToolResources,lprMarkText),
487 		"Bookmark" ),
488 };
489 
tedLinkToolGetResourceTable(EditApplication * ea,LinkToolResources * lpr,InspectorSubjectResources * isr)490 void tedLinkToolGetResourceTable(	EditApplication *		ea,
491 					LinkToolResources *		lpr,
492 					InspectorSubjectResources *	isr )
493     {
494     static int	gotToolResources= 0;
495     static int	gotSubjectResources= 0;
496 
497     if  ( ! gotToolResources )
498 	{
499 	appGuiGetResourceValues( &gotToolResources, ea, (void *)lpr,
500 				TED_TedLinkToolResourceTable,
501 				sizeof(TED_TedLinkToolResourceTable)/
502 				sizeof(AppConfigurableResource) );
503 	}
504 
505     if  ( ! gotSubjectResources )
506 	{
507 	appGuiGetResourceValues( &gotSubjectResources, ea, (void *)isr,
508 				TED_TedLinkSubjectResourceTable,
509 				sizeof(TED_TedLinkSubjectResourceTable)/
510 				sizeof(AppConfigurableResource) );
511 	}
512 
513     return;
514     }
515