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

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

improvements/corrections of some *.pro headers + replace some message by some report

  • 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
[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
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
[242]41; activate to show the box on graphic 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  compile_opt idl2, strictarrsubs
57;
[2]58        ON_ERROR, 2
59;
60;  Check the number of parameters.
61;
62justify=-1
63if keyword_set(right) ne 0 then justify=1
64if keyword_set(center) ne 0 then justify=0
65if keyword_set(vert_space) eq 0 then vert_space= 1.5
66IF n_elements(text) eq 0 then message, 'must specify text'
67nnx=!x.window*!d.x_vsize
68nny=!y.window*!d.y_vsize
69nnx=[0., 1.]*!d.x_vsize
70nny=[0., 1.]*!d.y_vsize
71
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
[114]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])]
[2]80  posstring=string(form='(a,4(f5.2,a))',$
[114]81           ',pos=[',pos[0],',',pos[1],',',pos[2],',',pos[3],']')
[2]82  print,strcompress(posstring,/remove_all)
83
[224]84
[2]85endif else begin
[224]86
[114]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])
[2]91
92endelse
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))
[224]107  charsize=1
[2]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
[114]122      sum=sum+wlen[i]+blen
[2]123      if sum+3*blen gt xx2-xx1 then begin
124        ilines=ilines+1
[114]125        sum=wlen[i]+blen
[2]126      endif
[224]127      lpnt[i]=ilines
128
[114]129;      print,f='(f8.2,4i8,3f8.2)',charsize,i,ilines,n_lines,lpnt[i],$
130;                 wlen[i]+blen,sum+3*blen,xx2-xx1
[224]131    endfor
[2]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
[224]139endrep until nomore
[2]140
141lines=strarr(n_lines)
142maxlen=0
143
144for i=0,n_lines-1 do begin
145  ii=where(lpnt eq i,nc)
[114]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]
[2]150endfor
151
152;
153  align=.5*(1+justify)
[224]154
[2]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
[114]175;   print,xx,yy,lines[i_line],charsize
176    xyouts, xx, yy, lines[i_line], /device, charsize=charsize, $
[2]177      alignment=align, color=color, font=-1, _extra = ex
178  endfor
179  if keyword_set(box) then plots,xbox,ybox,color=color,/device
180;
181return
182end
Note: See TracBrowser for help on using the repository browser.