xref: /plan9-contrib/sys/include/control.h (revision d46c239f8612929b7dbade67d0d071633df3a15d)
1 #pragma src "/sys/src/libcontrol"
2 #pragma lib "libcontrol.a"
3 
4 #pragma	varargck	type	"q"	char*
5 #pragma	varargck	type	"Q"	Rune*
6 #pragma	varargck	argpos	ctlprint	2
7 
8 typedef struct Control Control;
9 typedef struct Controlset Controlset;
10 typedef struct CParse CParse;
11 typedef struct CCache CCache;
12 typedef struct CCache CImage;
13 typedef struct CCache CFont;
14 
15 enum	/* types */
16 {
17 	Ctlunknown,
18 	Ctlbox,
19 	Ctlbutton,
20 	Ctlentry,
21 	Ctlkeyboard,
22 	Ctllabel,
23 	Ctlmenu,
24 	Ctlradio,
25 	Ctlscribble,
26 	Ctlslider,
27 	Ctltabs,
28 	Ctltext,
29 	Ctltextbutton,
30 	Ctlgroup,		// divider between controls and metacontrols
31 	Ctlboxbox,
32 	Ctlcolumn,
33 	Ctlrow,
34 	Ctlstack,
35 	Ctltab,
36 	Ntypes,
37 };
38 
39 struct Controlset
40 {
41 	Control		*controls;
42 	Image		*screen;
43 	Control		*actives;
44 	Control		*focus;
45 	Channel		*ctl;
46 	Channel		*data;		/* currently only for sync */
47 	Channel		*kbdc;
48 	Channel		*mousec;
49 	Channel		*resizec;
50 	Channel		*resizeexitc;
51 	Channel		*csexitc;
52 	Keyboardctl	*keyboardctl;	/* will be nil if user supplied keyboard */
53 	Mousectl		*mousectl;	/* will be nil if user supplied mouse */
54 	int			clicktotype;	/* flag */
55 };
56 
57 struct Control
58 {
59 	/* known to client */
60 	char			*name;
61 	Rectangle		rect;
62 	Rectangle		size;				/* minimum/maximum Dx, Dy (not a rect) */
63 	Channel		*event;			/* chan(char*) to client */
64 	Channel		*data;			/* chan(char*) to client */
65 	/* internal to control set */
66 	int			type;
67 	int			hidden;			/* hide hides, show unhides (and redraws) */
68 	Controlset	*controlset;
69 	Image		*screen;			/* where Control appears */
70 	char			*format;			/* used to generate events */
71 	char			wevent;			/* event channel rewired */
72 	char			wdata;			/* data channel rewired */
73 	/* method table */
74 	void			(*ctl)(Control*, CParse*);
75 	void			(*mouse)(Control*, Mouse*);
76 	void			(*key)(Control*, Rune*);
77 	void			(*exit)(Control*);
78 	void			(*setsize)(Control*);
79 	void			(*activate)(Control*, int);
80 	Control		*nextactive;
81 	Control		*next;
82 };
83 
84 struct CCache
85 {
86 	union{
87 		Image	*image;
88 		Font		*font;
89 	};
90 	char		*name;
91 	int		index;			/* entry number in cache */
92 	int		ref;				/* one for client, plus one for each use */
93 };
94 
95 struct CParse
96 {
97 	char	str[256];
98 	char	*sender;
99 	char *receiver;
100 	int	cmd;
101 	char	*pargs[32];
102 	int	iargs[32];
103 	char	**args;
104 	int nargs;
105 };
106 
107 enum	/* alignments */
108 {
109 	Aupperleft = 0,
110 	Auppercenter,
111 	Aupperright,
112 	Acenterleft,
113 	Acenter,
114 	Acenterright,
115 	Alowerleft,
116 	Alowercenter,
117 	Alowerright,
118 	Nalignments
119 };
120 
121 enum
122 {
123 	_Ctlmaxsize = 10000,
124 };
125 
126 extern char *ctltypenames[];
127 
128 /* Functions used internally */
129 void		_ctladdgroup(Control*, Control*);
130 void		_ctlargcount(Control*, CParse*, int);
131 Control*	_createctl(Controlset*, char*, uint, char*);
132 Rune*	_ctlrunestr(char*);
133 char*	_ctlstrrune(Rune*);
134 void		_ctlputsnarf(Rune*);
135 Rune*	_ctlgetsnarf(void);
136 int		_ctlalignment(char*);
137 Point		_ctlalignpoint(Rectangle, int, int, int);
138 void		_ctlfocus(Control*, int);
139 void		_activategroup(Control*);
140 void		_deactivategroup(Control*);
141 int		_ctllookup(char *s, char *tab[], int ntab);
142 void		_ctlprint(Control *c, char *fmt, ...);
143 
144 /* images */
145 CImage*	_getctlimage(char*);
146 void		_setctlimage(Control*, CImage**, char*);
147 void		_putctlimage(CImage*);
148 CFont*	_getctlfont(char*);
149 void		_putctlfont(CFont*);
150 
151 /* fonts */
152 CImage*	_getctlfont(char*);
153 void		_setctlfont(Control*, CImage**, char*);
154 void		_putctlfont(CImage*);
155 CFont*	_getctlfont(char*);
156 void		_putctlfont(CFont*);
157 
158 /* Public functions */
159 
160 /* images */
161 int		namectlimage(Image*, char*);
162 int		freectlimage(char*);
163 /* fonts */
164 int		namectlfont(Font*, char*);
165 int		freectlfont(char*);
166 /* commands */
167 int		ctlprint(Control*, char*, ...);
168 
169 /* general */
170 void			initcontrols(void);
171 Controlset*	newcontrolset(Image*, Channel*, Channel*, Channel*);
172 void			closecontrolset(Controlset*);
173 void			closecontrol(Control*);
174 void			ctlerror(char*, ...);
175 Control*		controlcalled(char*);
176 
177 /* publicly visible error-checking allocation routines */
178 void*	ctlmalloc(uint);
179 void*	ctlrealloc(void*, uint);
180 char*	ctlstrdup(char*);
181 
182 /* creation */
183 void		controlwire(Control*, char*, Channel*);
184 void		activate(Control*);
185 void		deactivate(Control*);
186 Control*	createbox(Controlset*, char*);
187 Control*	createbutton(Controlset*, char*);
188 Control*	createcolumn(Controlset*, char*);
189 Control*	createboxbox(Controlset*, char*);
190 Control*	createentry(Controlset*, char*);
191 Control*	createkeyboard(Controlset*, char*);
192 Control*	createlabel(Controlset*, char*);
193 Control*	createmenu(Controlset*, char*);
194 Control*	createradiobutton(Controlset*, char*);
195 Control*	createrow(Controlset*, char*);
196 Control*	createscribble(Controlset*, char*);
197 Control*	createslider(Controlset*, char*);
198 Control*	createstack(Controlset*, char*);
199 Control*	createtab(Controlset*, char*);
200 Control*	createtext(Controlset*, char*);
201 Control*	createtextbutton(Controlset*, char*);
202 
203 /* user-supplied */
204 void		resizecontrolset(Controlset*);
205 
206 int		_ctlsnarffd;
207 char		*alignnames[];
208 int		ctldeletequits;
209