libtt/process.c: coverity CID 87051; use after free
[oweals/cde.git] / cde / lib / DtHelp / Resize.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: Resize.c /main/5 1995/10/26 12:31:25 rswiston $ */
24 /************************************<+>*************************************
25  ****************************************************************************
26  **
27  **   File:        Resize.c
28  **
29  **   Project:     Display Area Library
30  **
31  **   Description: This body of code handles direct resize requests from
32  **                the GUI.
33  **
34  **  (c) Copyright 1987, 1988, 1989, 1990, 1991, 1992 Hewlett-Packard Company
35  **
36  **  (c) Copyright 1993, 1994 Hewlett-Packard Company
37  **  (c) Copyright 1993, 1994 International Business Machines Corp.
38  **  (c) Copyright 1993, 1994 Sun Microsystems, Inc.
39  **  (c) Copyright 1993, 1994 Novell, Inc.
40  **
41  **
42  ****************************************************************************
43  ************************************<+>*************************************/
44
45 /*
46  * system includes
47  */
48
49 /*
50  * private includes
51  */
52 #include "Access.h"
53 #include "DisplayAreaP.h"
54 #include "CallbacksI.h"
55 #include "ResizeI.h"
56 #include "SetListI.h"
57 #include "XUICreateI.h"
58
59 #ifdef NLS16
60 #endif
61
62 /********    Private Function Declarations    ********/
63 /********    End Private Function Declarations    ********/
64
65 /********    Private Variables    ********/
66 /********    End Private Variables  ********/
67
68 /*********************************************************************
69  *              Private Functions
70  *********************************************************************/
71
72 /*********************************************************************
73  *              Public Functions
74  *********************************************************************/
75 /*********************************************************************
76  * Function: ResizeDisplayArea
77  *
78  *    ResizeDisplayArea resizes the Display Area and re-formats the
79  *      text based on a row and column values given.
80  *
81  *********************************************************************/
82 void
83 _DtHelpResizeDisplayArea (
84         Widget                   parent,
85         XtPointer       client_data,
86         int                      rows,
87         int                      columns )
88 {
89     int            count = 0;
90     register int   n;
91     unsigned long  char_width;
92     Arg          args[5];
93     Dimension    newWidth;
94     Dimension    newHeight;
95     Dimension    vertWidth  = 0;
96     Dimension    horzHeight = 0;
97     Dimension    tstWidth;
98     Dimension    tstHeight;
99     Dimension    oldWidth;
100     Dimension    oldHeight;
101     DtHelpDispAreaStruct        *pDAS = (DtHelpDispAreaStruct *) client_data;
102
103     /*
104      * check the values.
105      */
106     if (rows <= 0)
107         rows = 1;
108     if (columns <= 0)
109         columns = 1;
110
111    /* Set the size of the text view area to the requested number of columns */
112    /* and lines. */
113     char_width = pDAS->charWidth * columns;
114
115     newWidth = char_width / 10 + (char_width % 10 ? 1 : 0) +
116                         2 * (pDAS->decorThickness + pDAS->marginWidth);
117     newHeight = pDAS->lineHeight * rows + 2 * pDAS->decorThickness;
118
119     /*
120      * get the width and height of the scroll bars.
121      */
122
123     if (pDAS->vertScrollWid)
124       {
125         XtSetArg (args[0], XmNwidth , &vertWidth);
126         XtGetValues (pDAS->vertScrollWid, args, 1);
127       }
128
129     if (pDAS->horzScrollWid)
130       {
131         XtSetArg (args[0], XmNheight , &horzHeight);
132         XtGetValues (pDAS->horzScrollWid, args, 1);
133       }
134
135     /*
136      * loop a couple of times at most to get the sizing correct.
137      * This is caused by the fact we can't do a set values on
138      * the drawn button itself, someone above it is rejecting
139      * the resize request. So, resize the great-grandparent
140      * (a couple of times if necessary).
141      */
142     do {
143         /*
144          * take into consideration the scrollbars.
145          */
146         tstWidth  = newWidth;
147         tstHeight = newHeight;
148         if (pDAS->vertScrollWid &&
149             _DtHelpIS_AS_NEEDED(pDAS->neededFlags, _DtHelpVERTICAL_SCROLLBAR)
150                                             && pDAS->vertIsMapped)
151           {
152             if (tstWidth > vertWidth)
153                 tstWidth -= vertWidth;
154             else
155                 tstWidth = 0;
156           }
157         if (pDAS->horzScrollWid &&
158             _DtHelpIS_AS_NEEDED(pDAS->neededFlags, _DtHelpHORIZONTAL_SCROLLBAR)
159                                             && pDAS->horzIsMapped)
160           {
161             if (tstHeight > horzHeight)
162                 tstHeight -= horzHeight;
163             else
164                 tstHeight = 0;
165           }
166       
167       
168         if (tstWidth == pDAS->dispWidth && tstHeight == pDAS->dispHeight)
169             return;
170     
171         /*
172          * kludge around the fact that some parent is not letting
173          * me resize the drawn button. Get the parent's size and
174          * resize that!
175          */
176         n = 0;
177         XtSetArg(args[n], XmNwidth,  &oldWidth);                ++n;
178         XtSetArg(args[n], XmNheight, &oldHeight);                ++n;
179         XtGetValues(parent, args, n);
180     
181         /*
182          * calculate its new size
183          */
184         oldWidth  = oldWidth - pDAS->dispWidth + tstWidth;
185         oldHeight = oldHeight - pDAS->dispHeight + tstHeight;
186     
187         /*
188          * set my parent's size to affect me.
189          */
190         n = 0;
191         XtSetArg(args[n], XmNwidth,  oldWidth);                ++n;
192         XtSetArg(args[n], XmNheight, oldHeight);                ++n;
193         XtSetValues(parent, args, n);
194
195     } while (count++ < 2);
196     
197     /*
198      * check to see if the Set Values on my parent took care of everything.
199      */
200     if (pDAS->vertScrollWid &&
201         _DtHelpIS_AS_NEEDED(pDAS->neededFlags, _DtHelpVERTICAL_SCROLLBAR)
202                                         && pDAS->vertIsMapped)
203       {
204         if (newWidth > vertWidth)
205             newWidth -= vertWidth;
206         else
207             newWidth = 0;
208       }
209
210     if (pDAS->horzScrollWid &&
211         _DtHelpIS_AS_NEEDED(pDAS->neededFlags, _DtHelpHORIZONTAL_SCROLLBAR)
212                                         && pDAS->horzIsMapped)
213       {
214         if (newHeight > horzHeight)
215             newHeight -= horzHeight;
216         else
217             newHeight = 0;
218       }
219
220     if (newWidth == pDAS->dispWidth && newHeight == pDAS->dispHeight)
221         return;
222
223     /*
224      * clean up
225     _DtHelpClearSelection (pDAS);
226      */
227
228     /*
229      * reset the scroll bars and possibly reformat the text.
230      */
231     _DtHelpSetScrollBars (client_data, newWidth, newHeight);
232     if (XtIsRealized (pDAS->dispWid))
233         _DtHelpCleanAndDrawWholeCanvas(client_data);
234
235 }  /* End _DtHelpResizeDisplayArea */