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