~rhialto/ctwm/smallbeer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
 * Window decoration routines -- initializtion time
 *
 * These are funcs that are called during ctwm initialization to setup
 * bits based on general X stuff and/or config file bits.
 */


#include "ctwm.h"

#include <stdio.h>
#include <stdlib.h>

#include "add_window.h"
#include "functions_defs.h"
#include "image.h"
#include "screen.h"

#include "win_decorations_init.h"


/*
 * Global marker used in config file loading to track "which one we're
 * currently messing with"
 */
static TitleButton *cur_tb = NULL;


/* Internal func[s] */
static void ComputeCommonTitleOffsets(void);



/*
 * InitTitlebarButtons - Do all the necessary stuff to load in a titlebar
 * button.  If we can't find the button, then put in a question; if we can't
 * find the question mark, something is wrong and we are probably going to be
 * in trouble later on.
 */
void
InitTitlebarButtons(void)
{
	TitleButton *tb;
	int h;

	/*
	 * initialize dimensions
	 */
	Scr->TBInfo.width = (Scr->TitleHeight -
	                     2 * (Scr->FramePadding + Scr->ButtonIndent));
	if(Scr->use3Dtitles)
		Scr->TBInfo.pad = ((Scr->TitlePadding > 1)
		                   ? ((Scr->TitlePadding + 1) / 2) : 0);
	else
		Scr->TBInfo.pad = ((Scr->TitlePadding > 1)
		                   ? ((Scr->TitlePadding + 1) / 2) : 1);
	h = Scr->TBInfo.width - 2 * Scr->TBInfo.border;

	/*
	 * add in some useful buttons and bindings so that novices can still
	 * use the system.
	 */
	if(!Scr->NoDefaults) {
		/* insert extra buttons */
#define MKBTN(bmap, func, isrt) \
                        CreateTitleButton(TBPM_##bmap, F_##func, "", NULL, \
                                          isrt, isrt)

		/* Iconify on the left, resize on the right */
		if(Scr->use3Dtitles) {
			MKBTN(3DDOT, ICONIFY, false);
			MKBTN(3DRESIZE, RESIZE, true);
		}
		else {
			MKBTN(ICONIFY, ICONIFY, false);
			MKBTN(RESIZE, RESIZE, true);
		}

#undef MKBTN

		/* Default mouse bindings in titlebar/icon/iconmgr as fallback */
		AddDefaultFuncButtons();
	}

	/* Init screen-wide dimensions for common titlebar bits */
	ComputeCommonTitleOffsets();


	/*
	 * load in images and do appropriate centering
	 */
	for(tb = Scr->TBInfo.head; tb; tb = tb->next) {
		tb->image = GetImage(tb->name, Scr->TitleC);
		if(!tb->image) {
			/* Couldn't find it, make a question mark */
			tb->image = GetImage(TBPM_QUESTION, Scr->TitleC);
			if(!tb->image) {
				/*
				 * (sorta) Can't Happen.  Calls a static function that
				 * builds from static data, so could only possibly fail
				 * if XCreateBitmapFromData() failed (which should be
				 * vanishingly rare; memory allocation failures etc).
				 */
				fprintf(stderr, "%s:  unable to add titlebar button \"%s\"\n",
				        ProgramName, tb->name);
				continue;
			}
		}
		tb->width  = tb->image->width;
		tb->height = tb->image->height;

		/* Figure centering.  Horizontally... */
		tb->dstx = (h - tb->width + 1) / 2;
		if(tb->dstx < 0) {              /* clip to minimize copying */
			tb->srcx = -(tb->dstx);
			tb->width = h;
			tb->dstx = 0;
		}
		else {
			tb->srcx = 0;
		}

		/* ... and vertically */
		tb->dsty = (h - tb->height + 1) / 2;
		if(tb->dsty < 0) {
			tb->srcy = -(tb->dsty);
			tb->height = h;
			tb->dsty = 0;
		}
		else {
			tb->srcy = 0;
		}
	}
}


/*
 * Figure general sizing/locations for titlebar bits.
 *
 * For the session; called during ctwm startup.  main() ->
 * InitTitleBarButtons() -> ComputeCommonTitleOffsets()
 */
static void
ComputeCommonTitleOffsets(void)
{
	int buttonwidth = (Scr->TBInfo.width + Scr->TBInfo.pad);

	/* Start "+left" and "-right" with our padding */
	Scr->TBInfo.leftx = Scr->TBInfo.rightoff = Scr->FramePadding;

	/*
	 * If there are buttons on the left, add a space to clear the right
	 * edge of the last one.
	 */
	if(Scr->TBInfo.nleft > 0) {
		Scr->TBInfo.leftx += Scr->ButtonIndent;
	}

	/*
	 * Similar on the right, except we need to know how many there are
	 * and account for all of that to leave enough space open for them.
	 * We didn't need to do that above because leftx is already relative
	 * to the end of the window holding them (and so means something like
	 * "move over this much further"), whereas rightoff is relative to
	 * the right side of the titlebar (and so means something like "we
	 * have to leave this much space")?
	 */
	if(Scr->TBInfo.nright > 0) {
		Scr->TBInfo.rightoff += (Scr->ButtonIndent
		                         + (Scr->TBInfo.nright * buttonwidth)
		                         - Scr->TBInfo.pad);
	}

	/*
	 * titlex does however go from the far-left of the titlebar, so it
	 * needs to account for the space the left-side buttons use.
	 */
	Scr->TBInfo.titlex = (Scr->TBInfo.leftx
	                      + (Scr->TBInfo.nleft * buttonwidth)
	                      - Scr->TBInfo.pad
	                      + Scr->TitlePadding);
}



/*
 * Sets the action for a given {mouse button,set of modifier keys} on the
 * "current" button.  This happens during initialization, in a few
 * different ways.
 *
 * CreateTitleButton() winds up creating a new button, and setting the
 * cur_tb global we rely on.  It calls us then to initialize our action
 * to what it was told (hardcoded for the !NoDefaults case in
 * InitTitlebarButtons() for fallback config, from the config file when
 * it's called via GotTitleButton() for the one-line string form of
 * *TitleButton spec).
 *
 * It's also called directly from the config parsing for the block-form
 * *TitleButton specs, when the cur_tb was previously set by
 * CreateTitleButton() at the opening of the block.
 */
void
SetCurrentTBAction(int button, int nmods, int func, char *action,
                   MenuRoot *menuroot)
{
	TitleButtonFunc *tbf;

	if(!cur_tb) {
		fprintf(stderr, "%s: can't find titlebutton\n", ProgramName);
		return;
	}
	for(tbf = cur_tb->funs; tbf; tbf = tbf->next) {
		if(tbf->num == button && tbf->mods == nmods) {
			break;
		}
	}
	if(!tbf) {
		tbf = malloc(sizeof(TitleButtonFunc));
		if(!tbf) {
			fprintf(stderr, "%s: out of memory\n", ProgramName);
			return;
		}
		tbf->next = cur_tb->funs;
		cur_tb->funs = tbf;
	}
	tbf->num = button;
	tbf->mods = nmods;
	tbf->func = func;
	tbf->action = action;
	tbf->menuroot = menuroot;
}



/*
 * XXX This return value is a little pointless.  The only failure it
 * acknowledges is from malloc(), and that Never Fails On Real
 * Systems(tm).  And if it does, we're pretty screwed anyway.
 */
bool
CreateTitleButton(char *name, int func, char *action, MenuRoot *menuroot,
                  bool rightside, bool append)
{
	int button;
	cur_tb = calloc(1, sizeof(TitleButton));

	if(!cur_tb) {
		fprintf(stderr,
		        "%s:  unable to allocate %lu bytes for title button\n",
		        ProgramName, (unsigned long) sizeof(TitleButton));
		return false;
	}

	cur_tb->name = name;           /* note that we are not copying */
	cur_tb->rightside = rightside;
	if(rightside) {
		Scr->TBInfo.nright++;
	}
	else {
		Scr->TBInfo.nleft++;
	}

	for(button = 0; button < MAX_BUTTONS; button++) {
		SetCurrentTBAction(button + 1, 0, func, action, menuroot);
	}

	/*
	 * Cases for list:
	 *
	 *     1.  empty list, prepend left       put at head of list
	 *     2.  append left, prepend right     put in between left and right
	 *     3.  append right                   put at tail of list
	 *
	 * Do not refer to widths and heights yet since buttons not created
	 * (since fonts not loaded and heights not known).
	 */
	if((!Scr->TBInfo.head) || ((!append) && (!rightside))) {    /* 1 */
		cur_tb->next = Scr->TBInfo.head;
		Scr->TBInfo.head = cur_tb;
	}
	else if(append && rightside) {      /* 3 */
		TitleButton *t;
		for(t = Scr->TBInfo.head; t->next; t = t->next) {
			/* just walking to tail */;
		}
		t->next = cur_tb;
		cur_tb->next = NULL;
	}
	else {                              /* 2 */
		TitleButton *t, *prev = NULL;
		for(t = Scr->TBInfo.head; t && !t->rightside; t = t->next) {
			prev = t;
		}
		if(prev) {
			cur_tb->next = prev->next;
			prev->next = cur_tb;
		}
		else {
			cur_tb->next = Scr->TBInfo.head;
			Scr->TBInfo.head = cur_tb;
		}
	}

	return true;
}