source: trunk/SRC/Utilities/text_box.pro

Last change on this file was 495, checked in by pinsard, 10 years ago

fix thanks to coding rules; typo; dupe empty lines; trailing blanks

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.8 KB
Line 
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.
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.
28;
29; @keyword RIGHT
30; if set, right justify text
31;
32; @keyword CENTER
33; if set, center the text
34;
35; @keyword VERT_SPACE {default=1.5}
36; vertical spacing of lines in units of character height
37;
38; @keyword _EXTRA
39; Used to pass keywords
40;
41; @keyword BOX
42; activate to show the box on graphic window.
43;
44; @history
45; Paul Ricchiazzi                            7Jul93
46;           Institute for Computational Earth System Science
47;           University of California, Santa Barbara
48;
49; @version
50; $Id$
51;
52;-
53PRO 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
56;
57  compile_opt idl2, strictarrsubs
58;
59        ON_ERROR, 2
60;
61;  Check the number of parameters.
62;
63justify=-1
64if keyword_set(right) ne 0 then justify=1
65if keyword_set(center) ne 0 then justify=0
66if keyword_set(vert_space) eq 0 then vert_space= 1.5
67IF n_elements(text) eq 0 then message, 'must specify text'
68nnx=!x.window*!d.x_vsize
69nny=!y.window*!d.y_vsize
70nnx=[0., 1.]*!d.x_vsize
71nny=[0., 1.]*!d.y_vsize
72
73if 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
84endif else begin
85
86  xx1 = nnx[0]+pos[0]*(nnx[1]-nnx[0])
87  xx2 = nnx[0]+pos[2]*(nnx[1]-nnx[0])
88  yy1 = nny[0]+pos[1]*(nny[1]-nnx[0])
89  yy2 = nny[0]+pos[3]*(nny[1]-nnx[0])
90
91endelse
92;
93;  calculate the height and width of the box in characters.
94;
95  width  = (xx2 - xx1) / !d.x_ch_size
96  height = (yy2 - yy1) / !d.y_ch_size
97;
98;  decompose the message into words.
99;
100  words = str_sep(text,' ')
101; print,f='(20a)',words
102  nwords=n_elements(words)
103  wordlen=lenstr(words)*!d.x_vsize
104  blanklen=lenstr(' ')*!d.x_vsize
105  maxcharsize=(xx2-xx1)/(4*blanklen+max(wordlen))
106  charsize=1
107  lpnt=intarr(nwords)
108  nomore=0
109  ntries=0
110  repeat begin
111    ntries=ntries+1
112    if ntries gt 20 then message,'Can not fit message into box'
113    ychsiz=vert_space*!d.y_ch_size*charsize
114    wlen=wordlen*charsize
115    blen=blanklen*charsize
116    n_lines=fix((yy2-yy1)/ychsiz)-1
117    sum=0
118    ilines=0
119;   print,f='(8a8)','charsz','i','ilines','n_lines','lpnt','wlen','sum','xwdth'
120    for i=0,nwords-1 do begin
121      sum=sum+wlen[i]+blen
122      if sum+3*blen gt xx2-xx1 then begin
123        ilines=ilines+1
124        sum=wlen[i]+blen
125      endif
126      lpnt[i]=ilines
127
128;      print,f='(f8.2,4i8,3f8.2)',charsize,i,ilines,n_lines,lpnt[i],$
129;                 wlen[i]+blen,sum+3*blen,xx2-xx1
130    endfor
131    case 1 of
132      ilines+1 lt n_lines: if charsize*1.1 gt maxcharsize then $
133          vert_space=(yy2-yy1)/((n_lines-1)*!d.y_ch_size*charsize) $
134          else charsize=charsize*1.1
135      ilines+1 eq n_lines: nomore=1
136      ilines+1 gt n_lines: charsize=charsize*.9
137    endcase
138endrep until nomore
139
140lines=strarr(n_lines)
141maxlen=0
142
143for i=0,n_lines-1 do begin
144  ii=where(lpnt eq i,nc)
145  maxlen=(total(wlen[ii])+nc*blen)>maxlen
146  lines[i]=string(f='(200a)',words[ii]+' ')
147; print,i,words[ii]
148; print,i,lines[i]
149endfor
150
151;
152  align=.5*(1+justify)
153
154  case justify of
155    -1:xx = xx1+.5*((xx2-xx1)-maxlen)
156     0:xx = 0.5*(xx1 + xx2)
157     1:xx = xx2-.5*((xx2-xx1)-maxlen)
158  endcase
159
160  dy=!d.y_ch_size*charsize*vert_space
161  yy=yy2-0.5*dy
162
163  xbox=[xx1,xx2,xx2,xx1,xx1]
164  ybox=[yy1,yy1,yy2,yy2,yy1]
165  if n_elements(bg_color) ne 0 then begin
166    if !p.color eq 0 and bg_color eq 0 then bgc=255 else bgc=bg_color
167    polyfill,xbox,ybox,color=bgc,/device
168  endif
169
170  if n_elements(fg_color) eq 0 then color = 0 else color=fg_color
171
172  for i_line = 0,n_lines-1 do begin
173    yy = yy-dy
174;   print,xx,yy,lines[i_line],charsize
175    xyouts, xx, yy, lines[i_line], /device, charsize=charsize, $
176      alignment=align, color=color, font=-1, _extra = ex
177  endfor
178  if keyword_set(box) then plots,xbox,ybox,color=color,/device
179;
180return
181end
Note: See TracBrowser for help on using the repository browser.