source: trunk/SRC/Utilities/text_box.pro @ 231

Last change on this file since 231 was 231, checked in by pinsard, 17 years ago

improvements/corrections of some *.pro headers

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.9 KB
RevLine 
[2]1;+
2;
[224]3; @file_comments
[133]4; This procedure writes a short text message within a box-shaped
5; area in a graphics window.  The message may be split at word
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;
9; @param TEXT {in}{required}
[136]10; ASCII text string containing the message.
[2]11;
[224]12; @keyword POS
[136]13; 4 element 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 use
[2]17;
[163]18; @keyword FG_COLOR {default=0}
19; color of box and legend titles
[2]20;
[224]21; @keyword BG_COLOR
22; background color. Setting BG_COLOR erases the area
[2]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
[226]26;               guarantee a readability.
[224]27;
28; @keyword RIGHT
[136]29; if set, right justify text
[2]30;
[224]31; @keyword CENTER
[136]32; if set, center the text
[2]33;
[163]34; @keyword VERT_SPACE {default=1.5}
[136]35; vertical spacing of lines in units of character height
[2]36;
[224]37; @keyword _EXTRA
[231]38; Used to pass keywords
[133]39;
[224]40; @keyword BOX
[136]41; activate to show the box on graphics window.
[133]42;
[224]43; @history
44; Paul Ricchiazzi                            7Jul93
[2]45;           Institute for Computational Earth System Science
46;           University of California, Santa Barbara
[133]47;
[224]48; @version
49; $Id$
[133]50;
[2]51;-
[231]52;
[97]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, _EXTRA = ex
[2]55;
[114]56;
57  compile_opt idl2, strictarrsubs
58;
[2]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
73
74if 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]86endif 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
93endelse
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]140endrep until nomore
[2]141
142lines=strarr(n_lines)
143maxlen=0
144
145for 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]151endfor
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;
182return
183end
184
Note: See TracBrowser for help on using the repository browser.