xref: /plan9/sys/src/cmd/aux/antiword/icons.c (revision 25b329d522281a8cdd35da0dcc08c3fc621059a9)
1 /*
2  * icons.c
3  * Copyright (C) 1998-2001 A.J. van Os; Released under GPL
4  *
5  * Description:
6  * Update window icons
7  */
8 
9 #include <string.h>
10 #include "DeskLib:Error.h"
11 #include "DeskLib:WimpSWIs.h"
12 #include "antiword.h"
13 
14 void
vUpdateIcon(window_handle tWindow,icon_block * pIcon)15 vUpdateIcon(window_handle tWindow, icon_block *pIcon)
16 {
17 	window_redrawblock	tRedraw;
18 	BOOL		bMore;
19 
20 	tRedraw.window = tWindow;
21 	tRedraw.rect = pIcon->workarearect;
22 	Error_CheckFatal(Wimp_UpdateWindow(&tRedraw, &bMore));
23 	while (bMore) {
24 		Error_CheckFatal(Wimp_PlotIcon(pIcon));
25 		Error_CheckFatal(Wimp_GetRectangle(&tRedraw, &bMore));
26 	}
27 } /* end of vUpdateIcon */
28 
29 void
vUpdateRadioButton(window_handle tWindow,icon_handle tIconNumber,BOOL bSelected)30 vUpdateRadioButton(window_handle tWindow, icon_handle tIconNumber,
31 	BOOL bSelected)
32 {
33 	icon_block	tIcon;
34 
35 	Error_CheckFatal(Wimp_GetIconState(tWindow, tIconNumber, &tIcon));
36 	DBG_DEC(tIconNumber);
37 	DBG_HEX(tIcon.flags.data.selected);
38 	if (bSelected == (tIcon.flags.data.selected == 1)) {
39 		/* No update needed */
40 		return;
41 	}
42 	Error_CheckFatal(Wimp_SetIconState(tWindow, tIconNumber,
43 			bSelected ? 0x00200000 : 0, 0x00200000));
44 	vUpdateIcon(tWindow, &tIcon);
45 } /* end of vUpdateRadioButton */
46 
47 /*
48  * vUpdateWriteable - update a writeable icon with a string
49  */
50 void
vUpdateWriteable(window_handle tWindow,icon_handle tIconNumber,const char * szString)51 vUpdateWriteable(window_handle tWindow, icon_handle tIconNumber,
52 	const char *szString)
53 {
54 	icon_block	tIcon;
55 	caret_block	tCaret;
56 	int		iLen;
57 
58 	fail(szString == NULL);
59 
60 	NO_DBG_DEC(tIconNumber);
61 	NO_DBG_MSG(szString);
62 
63 	Error_CheckFatal(Wimp_GetIconState(tWindow, tIconNumber, &tIcon));
64 	NO_DBG_HEX(tIcon.flags);
65 	if (!tIcon.flags.data.text || !tIcon.flags.data.indirected) {
66 		werr(1, "Icon %d must be indirected text", (int)tIconNumber);
67 		return;
68 	}
69 	strncpy(tIcon.data.indirecttext.buffer,
70 		szString,
71 		tIcon.data.indirecttext.bufflen - 1);
72 	/* Ensure the caret is behind the last character of the text */
73 	Error_CheckFatal(Wimp_GetCaretPosition(&tCaret));
74 	if (tCaret.window == tWindow && tCaret.icon == tIconNumber) {
75 		iLen = strlen(tIcon.data.indirecttext.buffer);
76 		if (tCaret.index != iLen) {
77 			tCaret.index = iLen;
78 			Error_CheckFatal(Wimp_SetCaretPosition(&tCaret));
79 		}
80 	}
81 	Error_CheckFatal(Wimp_SetIconState(tWindow, tIconNumber, 0, 0));
82 	vUpdateIcon(tWindow, &tIcon);
83 } /* end of vUpdateWriteable */
84 
85 /*
86  * vUpdateWriteableNumber - update a writeable icon with a number
87  */
88 void
vUpdateWriteableNumber(window_handle tWindow,icon_handle tIconNumber,int iNumber)89 vUpdateWriteableNumber(window_handle tWindow, icon_handle tIconNumber,
90 	int iNumber)
91 {
92 	char	szTmp[1+3*sizeof(int)+1];
93 
94 	(void)sprintf(szTmp, "%d", iNumber);
95 	vUpdateWriteable(tWindow, tIconNumber, szTmp);
96 } /* end of vUpdateWriteableNumber */
97