1 | ;+ |
---|
2 | ; |
---|
3 | ; @file_comments |
---|
4 | ; This procedure writes a short text message within a box-shaped |
---|
5 | ; area in a graphic window. The message may be split at word |
---|
6 | ; boundaries into several lines, and the character size and |
---|
7 | ; orientation may be adjusted for the text to fit within the box. |
---|
8 | ; |
---|
9 | ; @param TEXT {in}{required} {type=string} |
---|
10 | ; message in ASCII. |
---|
11 | ; |
---|
12 | ; @keyword POS |
---|
13 | ; 4 elements vector specifying the box position and size |
---|
14 | ; pos[0],pos[1] specify the lower left corner coordinate, |
---|
15 | ; pos[2],pos[3] specify the upper right corner coordinate, |
---|
16 | ; data window normalized coordinates are used. |
---|
17 | ; |
---|
18 | ; @keyword FG_COLOR {default=0} |
---|
19 | ; color of box and legend titles |
---|
20 | ; |
---|
21 | ; @keyword BG_COLOR |
---|
22 | ; background color. Setting BG_COLOR erases the area |
---|
23 | ; covered by the text box (filling it with color BG_COLOR) |
---|
24 | ; prior to writing the text. If both BG_COLOR and !p.color |
---|
25 | ; are zero then the background color is reset to 255 to |
---|
26 | ; guarantee a readability. |
---|
27 | ; |
---|
28 | ; @keyword RIGHT |
---|
29 | ; if set, right justify text |
---|
30 | ; |
---|
31 | ; @keyword CENTER |
---|
32 | ; if set, center the text |
---|
33 | ; |
---|
34 | ; @keyword VERT_SPACE {default=1.5} |
---|
35 | ; vertical spacing of lines in units of character height |
---|
36 | ; |
---|
37 | ; @keyword _EXTRA |
---|
38 | ; Used to pass keywords |
---|
39 | ; |
---|
40 | ; @keyword BOX |
---|
41 | ; activate to show the box on graphic window. |
---|
42 | ; |
---|
43 | ; @history |
---|
44 | ; Paul Ricchiazzi 7Jul93 |
---|
45 | ; Institute for Computational Earth System Science |
---|
46 | ; University of California, Santa Barbara |
---|
47 | ; |
---|
48 | ; @version |
---|
49 | ; $Id$ |
---|
50 | ; |
---|
51 | ;- |
---|
52 | PRO text_box, text, POS=pos, FG_COLOR=fg_color, BG_COLOR=bg_color $ |
---|
53 | , CENTER=center, RIGHT=right, BOX=box, VERT_SPACE=vert_space $ |
---|
54 | , _EXTRA=ex |
---|
55 | ; |
---|
56 | compile_opt idl2, strictarrsubs |
---|
57 | ; |
---|
58 | ON_ERROR, 2 |
---|
59 | ; |
---|
60 | ; Check the number of parameters. |
---|
61 | ; |
---|
62 | justify=-1 |
---|
63 | if keyword_set(right) ne 0 then justify=1 |
---|
64 | if keyword_set(center) ne 0 then justify=0 |
---|
65 | if keyword_set(vert_space) eq 0 then vert_space= 1.5 |
---|
66 | IF n_elements(text) eq 0 then message, 'must specify text' |
---|
67 | nnx=!x.window*!d.x_vsize |
---|
68 | nny=!y.window*!d.y_vsize |
---|
69 | nnx=[0., 1.]*!d.x_vsize |
---|
70 | nny=[0., 1.]*!d.y_vsize |
---|
71 | |
---|
72 | |
---|
73 | if n_elements(pos) eq 0 then begin |
---|
74 | |
---|
75 | box_cursor,xx1,yy1,nx,ny |
---|
76 | xx2=xx1+nx |
---|
77 | yy2=yy1+ny |
---|
78 | pos=[(xx1-nnx[0])/(nnx[1]-nnx[0]),(yy1-nny[0])/(nny[1]-nny[0]),$ |
---|
79 | (xx2-nnx[0])/(nnx[1]-nnx[0]),(yy2-nny[0])/(nny[1]-nny[0])] |
---|
80 | posstring=string(form='(a,4(f5.2,a))',$ |
---|
81 | ',pos=[',pos[0],',',pos[1],',',pos[2],',',pos[3],']') |
---|
82 | print,strcompress(posstring,/remove_all) |
---|
83 | |
---|
84 | |
---|
85 | endif else begin |
---|
86 | |
---|
87 | xx1 = nnx[0]+pos[0]*(nnx[1]-nnx[0]) |
---|
88 | xx2 = nnx[0]+pos[2]*(nnx[1]-nnx[0]) |
---|
89 | yy1 = nny[0]+pos[1]*(nny[1]-nnx[0]) |
---|
90 | yy2 = nny[0]+pos[3]*(nny[1]-nnx[0]) |
---|
91 | |
---|
92 | endelse |
---|
93 | ; |
---|
94 | ; calculate the height and width of the box in characters. |
---|
95 | ; |
---|
96 | width = (xx2 - xx1) / !d.x_ch_size |
---|
97 | height = (yy2 - yy1) / !d.y_ch_size |
---|
98 | ; |
---|
99 | ; decompose the message into words. |
---|
100 | ; |
---|
101 | words = str_sep(text,' ') |
---|
102 | ; print,f='(20a)',words |
---|
103 | nwords=n_elements(words) |
---|
104 | wordlen=lenstr(words)*!d.x_vsize |
---|
105 | blanklen=lenstr(' ')*!d.x_vsize |
---|
106 | maxcharsize=(xx2-xx1)/(4*blanklen+max(wordlen)) |
---|
107 | charsize=1 |
---|
108 | lpnt=intarr(nwords) |
---|
109 | nomore=0 |
---|
110 | ntries=0 |
---|
111 | repeat begin |
---|
112 | ntries=ntries+1 |
---|
113 | if ntries gt 20 then message,'Can not fit message into box' |
---|
114 | ychsiz=vert_space*!d.y_ch_size*charsize |
---|
115 | wlen=wordlen*charsize |
---|
116 | blen=blanklen*charsize |
---|
117 | n_lines=fix((yy2-yy1)/ychsiz)-1 |
---|
118 | sum=0 |
---|
119 | ilines=0 |
---|
120 | ; print,f='(8a8)','charsz','i','ilines','n_lines','lpnt','wlen','sum','xwdth' |
---|
121 | for i=0,nwords-1 do begin |
---|
122 | sum=sum+wlen[i]+blen |
---|
123 | if sum+3*blen gt xx2-xx1 then begin |
---|
124 | ilines=ilines+1 |
---|
125 | sum=wlen[i]+blen |
---|
126 | endif |
---|
127 | lpnt[i]=ilines |
---|
128 | |
---|
129 | ; print,f='(f8.2,4i8,3f8.2)',charsize,i,ilines,n_lines,lpnt[i],$ |
---|
130 | ; wlen[i]+blen,sum+3*blen,xx2-xx1 |
---|
131 | endfor |
---|
132 | case 1 of |
---|
133 | ilines+1 lt n_lines: if charsize*1.1 gt maxcharsize then $ |
---|
134 | vert_space=(yy2-yy1)/((n_lines-1)*!d.y_ch_size*charsize) $ |
---|
135 | else charsize=charsize*1.1 |
---|
136 | ilines+1 eq n_lines: nomore=1 |
---|
137 | ilines+1 gt n_lines: charsize=charsize*.9 |
---|
138 | endcase |
---|
139 | endrep until nomore |
---|
140 | |
---|
141 | lines=strarr(n_lines) |
---|
142 | maxlen=0 |
---|
143 | |
---|
144 | for i=0,n_lines-1 do begin |
---|
145 | ii=where(lpnt eq i,nc) |
---|
146 | maxlen=(total(wlen[ii])+nc*blen)>maxlen |
---|
147 | lines[i]=string(f='(200a)',words[ii]+' ') |
---|
148 | ; print,i,words[ii] |
---|
149 | ; print,i,lines[i] |
---|
150 | endfor |
---|
151 | |
---|
152 | ; |
---|
153 | align=.5*(1+justify) |
---|
154 | |
---|
155 | case justify of |
---|
156 | -1:xx = xx1+.5*((xx2-xx1)-maxlen) |
---|
157 | 0:xx = 0.5*(xx1 + xx2) |
---|
158 | 1:xx = xx2-.5*((xx2-xx1)-maxlen) |
---|
159 | endcase |
---|
160 | |
---|
161 | dy=!d.y_ch_size*charsize*vert_space |
---|
162 | yy=yy2-0.5*dy |
---|
163 | |
---|
164 | xbox=[xx1,xx2,xx2,xx1,xx1] |
---|
165 | ybox=[yy1,yy1,yy2,yy2,yy1] |
---|
166 | if n_elements(bg_color) ne 0 then begin |
---|
167 | if !p.color eq 0 and bg_color eq 0 then bgc=255 else bgc=bg_color |
---|
168 | polyfill,xbox,ybox,color=bgc,/device |
---|
169 | endif |
---|
170 | |
---|
171 | if n_elements(fg_color) eq 0 then color = 0 else color=fg_color |
---|
172 | |
---|
173 | for i_line = 0,n_lines-1 do begin |
---|
174 | yy = yy-dy |
---|
175 | ; print,xx,yy,lines[i_line],charsize |
---|
176 | xyouts, xx, yy, lines[i_line], /device, charsize=charsize, $ |
---|
177 | alignment=align, color=color, font=-1, _extra = ex |
---|
178 | endfor |
---|
179 | if keyword_set(box) then plots,xbox,ybox,color=color,/device |
---|
180 | ; |
---|
181 | return |
---|
182 | end |
---|