xref: /petsc/src/sys/classes/draw/interface/dtext.c (revision 5c6c1daec53e1d9ab0bec9db5309fd8fc7645b8d)
1 
2 /*
3        Provides the calling sequences for all the basic PetscDraw routines.
4 */
5 #include <petsc-private/drawimpl.h>  /*I "petscdraw.h" I*/
6 
7 #undef __FUNCT__
8 #define __FUNCT__ "PetscDrawString"
9 /*@C
10    PetscDrawString - PetscDraws text onto a drawable.
11 
12    Not Collective
13 
14    Input Parameters:
15 +  draw - the drawing context
16 .  xl - the coordinates of lower left corner of text
17 .  yl - the coordinates of lower left corner of text
18 .  cl - the color of the text
19 -  text - the text to draw
20 
21    Level: beginner
22 
23    Concepts: drawing^string
24    Concepts: string^drawing
25 
26 .seealso: PetscDrawStringVertical()
27 
28 @*/
29 PetscErrorCode  PetscDrawString(PetscDraw draw,PetscReal xl,PetscReal yl,int cl,const char text[])
30 {
31   PetscErrorCode ierr;
32   PetscBool      isnull;
33 
34   PetscFunctionBegin;
35   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
36   PetscValidCharPointer(text,5);
37   ierr = PetscObjectTypeCompare((PetscObject)draw,PETSC_DRAW_NULL,&isnull);CHKERRQ(ierr);
38   if (isnull) PetscFunctionReturn(0);
39   ierr = (*draw->ops->string)(draw,xl,yl,cl,text);CHKERRQ(ierr);
40   PetscFunctionReturn(0);
41 }
42 
43 #undef __FUNCT__
44 #define __FUNCT__ "PetscDrawBoxedString"
45 /*@C
46    PetscDrawBoxedString - Draws a string with a box around it
47 
48    Not Collective
49 
50    Input Parameters:
51 +  draw - the drawing context
52 .  sxl - the coordinates of center of the box
53 .  syl - the coordinates of top line of box
54 .  sc - the color of the text
55 .  bc - the color of the bounding box
56 -  text - the text to draw
57 
58    Output Parameter:
59 .   w,h - width and height of resulting box (optional)
60 
61    Level: beginner
62 
63    Concepts: drawing^string
64    Concepts: string^drawing
65 
66 .seealso: PetscDrawStringVertical(), PetscDrawBoxedStringSize()
67 
68 @*/
69 PetscErrorCode  PetscDrawBoxedString(PetscDraw draw,PetscReal sxl,PetscReal syl,int sc,int bc,const char text[],PetscReal *w,PetscReal *h)
70 {
71   PetscErrorCode ierr ;
72   PetscBool      isnull;
73   PetscReal      top,left,right,bottom,tw,th;
74   size_t         len,mlen = 0;
75   char           **array;
76   int            cnt,i;
77 
78   PetscFunctionBegin;
79   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
80   PetscValidCharPointer(text,5);
81   ierr = PetscObjectTypeCompare((PetscObject)draw,PETSC_DRAW_NULL,&isnull);CHKERRQ(ierr);
82   if (isnull) PetscFunctionReturn(0);
83 
84   ierr = PetscStrToArray(text,'\n',&cnt,&array);CHKERRQ(ierr);
85   for (i=0; i<cnt; i++) {
86     ierr = PetscStrlen(array[i],&len);CHKERRQ(ierr);
87     mlen = PetscMax(mlen,len);
88   }
89 
90   ierr = PetscDrawStringGetSize(draw,&tw,&th);CHKERRQ(ierr);
91 
92   top    = syl;
93   left   = sxl - .5*(mlen + 2)*tw;
94   right  = sxl + .5*(mlen + 2)*tw;
95   bottom = syl - (1.0 + cnt)*th;
96   if (w) *w = right - left;
97   if (h) *h = top - bottom;
98 
99   /* compute new bounding box */
100   draw->boundbox_xl = PetscMin(draw->boundbox_xl,left);
101   draw->boundbox_xr = PetscMax(draw->boundbox_xr,right);
102   draw->boundbox_yl = PetscMin(draw->boundbox_yl,bottom);
103   draw->boundbox_yr = PetscMax(draw->boundbox_yr,top);
104 
105   /* top, left, bottom, right lines */
106   ierr = PetscDrawLine(draw,left,top,right,top,bc);CHKERRQ(ierr);
107   ierr = PetscDrawLine(draw,left,bottom,left,top,bc);CHKERRQ(ierr);
108   ierr = PetscDrawLine(draw,right,bottom,right,top,bc);CHKERRQ(ierr);
109   ierr = PetscDrawLine(draw,left,bottom,right,bottom,bc);CHKERRQ(ierr);
110 
111   for  (i=0; i<cnt; i++) {
112     ierr = PetscDrawString(draw,left + tw,top - (1.5 + i)*th,sc,array[i]);CHKERRQ(ierr);
113   }
114   ierr = PetscStrToArrayDestroy(cnt,array);CHKERRQ(ierr);
115 
116   PetscFunctionReturn(0);
117 }
118