File: tablist.pike

package info (click to toggle)
roxen 1.2beta2-3
  • links: PTS
  • area: contrib
  • in suites: slink
  • size: 16,920 kB
  • ctags: 8,589
  • sloc: ansic: 89,632; asm: 8,431; sh: 2,915; makefile: 1,784; cpp: 377
file content (285 lines) | stat: -rw-r--r-- 7,793 bytes parent folder | download | duplicates (4)
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
// The Tab lists tag module.
string cvs_version = "$Id: tablist.pike,v 1.11 1998/03/07 18:42:34 noring Exp $";
#include <module.h>

inherit "module";
inherit "roxenlib";

import Array;
import Image;

//constant Image = image;
constant Font = font;

#define DEFAULT_FONT "32/urw_itc_avant_garde-demi-r"
#define DEFAULT_PATH "fonts/"

// #define DEBUG_TABLIST

string *from=map(indices(allocate(256)),lambda(int l) { return sprintf("%c",l); });
string *to=map(indices(allocate(256)),lambda(int l) {
  switch(l)
  {
    case 0: return "-";
    case 'a'..'z':
    case '.':
    case ':':
    case 'A'+16..'Z':
    case '0'..'9': return sprintf("%c",l);
    default: return sprintf("%c%c",'A'+(l>>4),'A'+(l&15));
  }
});

string make_filename(mapping arguments)
{
  string s = encode_value(arguments);
  return replace(s,from,to);
}

mapping make_arguments(string filename)
{
  filename=replace(filename,to,from);
  
  return decode_value(filename);
}

array (int) make_color(string s)
{
  int c = (int) ("0x"+(s-" ")[1..]);
  return ({ ((c >> 16) & 0xff),
	    ((c >>  8) & 0xff),
	     (c        & 0xff) });
}

void draw_bg(object img, array (int) bg, array (int) tc)
{
  img->tuned_box(0, 0, img->xsize()-1, 7, ({
		 ({ @bg }),
		 ({ @bg }),
		 ({ @map(tc, `/, 7) }),
		 ({ @map(tc, `/, 7) }) }) );
  img->line(0, 8, img->xsize()-1, 8, 0,0,0);
  img->box(0, 9, img->xsize()-1, img->ysize()-12, @tc);
  img->tuned_box(0, img->ysize()-11, img->xsize()-1, img->ysize()-1, ({
                 ({ @tc }),
		 ({ @tc }),
		 ({ @map(bg, `/, 3) }),
		 ({ @map(bg, `/, 3) }) }) );
  img->line(0, img->ysize()-1, img->xsize()-1, img->ysize()-1, 0,0,0);
}

void right_shadow(object img, array (int) tc)
{
  int i;

  float dr = (6*((float) tc[0])/7)/10;
  float dg = (6*((float) tc[1])/7)/10;
  float db = (6*((float) tc[2])/7)/10;
  float tr = (float) tc[0]/7;
  float tg = (float) tc[1]/7;
  float tb = (float) tc[2]/7;
  for (i = 0; i < 10; i++) {
    tr += dr; tg += dg; tb += db;
    img->line(img->xsize()-15+i, 9, img->xsize()-5-1+i/2, img->ysize()-1-i,
	      (int)tr,(int)tg,(int)tb);
    img->line(img->xsize()-14+i, 9, img->xsize()-5-1+i/2, img->ysize()-1-i,
	      (int)tr,(int)tg,(int)tb);
  }
}

void right_selected(object img, array (int) bg)
{
  int y;
  float x = img->xsize()-1;
  float dx = ((float) 13)/((float) img->ysize());
  for (y = 9; y < img->ysize(); y++) {
    img->line((int) x, y, img->xsize()-1, y, @bg);
    x -= dx;
  }
  img->line(img->xsize()-1, 9, img->xsize()-13, img->ysize()-1, 0,0,0);
  img->line(img->xsize()-2, 9, img->xsize()-13, img->ysize()-1, 0,0,0);
}

void selected(object img, array (int) bg)
{
  int y;
  float x = ((float) img->xsize()) - 14.0;
  float dx = ((float) 10)/((float) img->ysize());
  for (y = 9; y < img->ysize(); y++) {
    img->line(0, y, (int) x, y, @bg);
    x += dx;
  }
}

void left_end(object img, array (int) bg)
{
  int y;
  float x = (float) 15;
  float dx = ((float) x)/((float) img->ysize());
  for (y = 0; y < img->ysize()-1; y++) {
    img->line(0, y, (int) x, y, @bg);
    x -= dx;
  }
  img->line(15, 9, 0, img->ysize()-1, 0,0,0);
  img->line(14, 9, 0, img->ysize()-1, 0,0,0);
}

void right_end(object img, array (int) bg)
{
  int y;
  float x = 0.0;
  float dx = ((float) 13)/((float) img->ysize());
  for (y = 0; y < img->ysize()-1; y++) {
    img->line(img->xsize()-13 + (int) x, y, img->xsize()-1, y, @bg);
    x += dx;
  }
  right_shadow(img, bg);
}

object tab(string name, int select, int n, int last, string font,
	   array (int) bg, array (int) tc, array (int) fc)
{
  int w_spacing = 40+20;
  int h_spacing = 20+5;
  object fnt, txt, img, tmp;
  int width, height;

#ifdef DEBUG_TABLIST
  perror("Creating tab \"" + name + (select==n?"\" (selected)\n":"\"\n"));
#endif

  fnt = Font();
  if (!fnt->load(font)) {
     perror("Could not load font \"" + font + "\"\n");
     fnt->load(DEFAULT_PATH DEFAULT_FONT);
  }
  txt = fnt->write(name);
#ifdef DEBUG_TABLIST
  perror((sprintf("Font image size: %d  %d\n",txt->xsize(),txt->ysize())));
#endif
  width = txt->xsize() + w_spacing;
  height = txt->ysize() + h_spacing;

  img = image(width,height);
  draw_bg(img, bg, tc);
  if (n == select)
    selected(img, bg);
  if (n+1 == select)
    right_selected(img, bg);
  if (n == last)
    right_end(img, bg);
  else if (n+1 != select)
    right_shadow(img, tc);

  if ((txt->xsize()) && (txt->ysize())) {
    tmp=image(txt->xsize(), txt->ysize());
    tmp->box(0, 0, tmp->xsize()-1, tmp->ysize()-1, @fc);
    img->paste_mask(tmp, txt, w_spacing/3, h_spacing/2);
  }

  if (!n)
    left_end(img, bg);
  
  return img;
}

array register_module()
{
  return ({
    MODULE_PARSER | MODULE_LOCATION,
      "Tab lists",
      ("This module makes graphical tablists.<p>"
       "<b>NOTE:</b> This module is not supported and is only here "
       "for compatibility reasons. Please use ``<b>Config tab-list</b>'' "
       "instead.<p>"
#if 0
       "<p> It defines the tag "
       "&lt;tablist&gt;.<p>"
       "Arguments:\n<br>"
       "font=string: Select font<br>\n"
       "1= 2= 3= ...: Set the destination for one of the tags<br>\n"
       "bg=#rrggbb: Set background color<br>\n"
       "tc=#rrggbb: Set tab color<br>\n"
       "fc=#rrggbb: Set font color<br>\n"
       "scale=<float>: Scale the result<br>\n"
       "names=foo;bar;gazonk: Set the text on the tabs, separated by ';'<br>\n"
       "selected=<int>: Select which tab to select<br>\n"
#endif
       ""),
    0, 1,
      });
}

void create()
{
  defvar("foo", "/tablists/", "mount point", TYPE_LOCATION|VAR_MORE, "");
  defvar("fontpath", DEFAULT_PATH, "font path", TYPE_DIR|VAR_MORE, "");
  defvar("defaultfont", DEFAULT_FONT, "default font", TYPE_FILE|VAR_MORE, "");
}

string query_location()
{
  return query("foo");
}

mapping find_file(string filename, object request_id)
{
  string s;
  if(s = cache_lookup("tabs", filename))
    return http_string_answer(s, "image/gif");

  mapping arguments = make_arguments(filename);
  int n = (int) arguments->n;
  int last = (int) arguments->last;
  string name = (string) (arguments->name || "");
  int selected = ((int) arguments->selected) || 1;
  selected--;

  float scale = 0.5;
  if ((float) arguments->scale > 0)
    scale *= (float) arguments->scale;

  string font = (string) (arguments->font || query("defaultfont"));
  if (font[0] != '/') font = query("fontpath") + font;
  array (int) bg = make_color(arguments->bg||"#c0c0c0");  // Background color
  array (int) tc = make_color(arguments->tc||"#d6c69c");  // Tab color
  array (int) fc = make_color(arguments->fc||"#000000");  // Font color

  s = tab(name, selected, n, last, font, bg, tc, fc)->scale(scale)->togif(@bg);
  cache_set("tabs", filename, s);
  return http_string_answer(s, "image/gif");
}

string tag_tablist(string tag_name, mapping arguments, object request_id)
{
  int n = 0;
  string s, name;

  array (string) names = ((string) arguments->names)/";";
  arguments[ "last" ] = sizeof(names)-1;
  s = "<table cellspacing=0 cellpadding=0 border=0><tr>";
  foreach(names, name) {
    arguments[ "name" ] = name;
    arguments[ "n" ] = (string) n++;
    s += "<td>";
    if (arguments[(string) n])
      s += "<a href=\""+(arguments[(string) n]?arguments[(string) n]:"")+"\">"+
	   "<img border=0 "
	   "alt=\""+((n==1||(n==(int)arguments->selected))?"/":"")+name+
 	    ((n+1==(int)arguments->selected)?"":"\\")+"\" "+
	   "src="+query_location()+make_filename(arguments)+"></a>";
    else
      s += "<img border=0 "
	   "alt=\""+((n==1||(n==(int)arguments->selected))?"/":"")+name+
	    ((n+1==(int)arguments->selected)?"":"\\")+"\" "+
	   "src="+query_location()+make_filename(arguments)+">";
    s += "</td>";
  }
  s += "</tr></table>";
  return s+"\n";
}

mapping query_tag_callers()
{
  return ([ "tablist":tag_tablist, ]);
}