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