figure position set using [x y w h] now set the axes_size instead of figure size
[scilab.git] / scilab / modules / gui / src / cpp / SetUicontrolPosition.cpp
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2007 - INRIA - Vincent COUVERT
4  * Sets the position of an uicontrol object 
5  * 
6  * This file must be used under the terms of the CeCILL.
7  * This source file is licensed as described in the file COPYING, which
8  * you should have received as part of this distribution.  The terms
9  * are also available at    
10  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
11  *
12  */
13
14 #include "SetUicontrolPosition.hxx"
15
16 using namespace org_scilab_modules_gui_bridge;
17
18 int SetUicontrolPosition(sciPointObj* sciObj, size_t stackPointer, int valueType, int nbRow, int nbCol)
19 {
20   // Position can be [x, y, width, height]
21   // or "x|y|width|height"
22
23   int xInt = 0, yInt = 0, widthInt = 0, heightInt, nbvalues = 0;
24
25   double * allValues = NULL;
26   
27   float xDouble = 0.0, yDouble = 0.0, widthDouble = 0.0, heightDouble = 0.0;
28
29   int * returnValues = NULL;
30
31   sciPointObj *parent = NULL;
32
33   int status = 0;
34   
35   if (stackPointer == -1) /* Default values setting */
36     {
37       xInt = 20;
38       yInt = 40;
39       widthInt = 40;
40       heightInt = 20;
41     }
42   else
43     {
44       if (valueType == sci_strings)
45         {
46           if(nbCol != 1)
47             {
48               Scierror(999, _("Wrong size for '%s' property: A string or a 1 x %d real row vector expected.\n"), "Position", 4);
49               return SET_PROPERTY_ERROR;
50             }
51           
52           nbvalues = sscanf(getStringFromStack(stackPointer), "%e|%e|%e|%e", &xDouble, &yDouble, &widthDouble, &heightDouble);
53           
54           if (nbvalues != 4)
55             {
56               Scierror(999, _("Wrong value for '%s' property: A string or a 1 x %d real row vector expected.\n"), "Position", 4);
57               return SET_PROPERTY_ERROR;
58             }
59           
60           xInt = ConvertToPixel(xDouble, pUICONTROL_FEATURE(sciObj)->units, sciObj, TRUE);
61           yInt = ConvertToPixel(yDouble, pUICONTROL_FEATURE(sciObj)->units, sciObj, FALSE);
62           widthInt = ConvertToPixel(widthDouble, pUICONTROL_FEATURE(sciObj)->units, sciObj, TRUE);
63           heightInt = ConvertToPixel(heightDouble, pUICONTROL_FEATURE(sciObj)->units, sciObj, FALSE);
64           
65         }
66       else if (valueType == sci_matrix)
67         {
68           if(nbCol != 4 || nbRow != 1)
69             {
70               Scierror(999, _("Wrong size for '%s' property: A string or a 1 x %d real row vector expected.\n"), "Position", 4);
71               return SET_PROPERTY_ERROR;
72             }
73           
74           allValues = getDoubleMatrixFromStack(stackPointer);
75           xInt = ConvertToPixel(allValues[0], pUICONTROL_FEATURE(sciObj)->units, sciObj, TRUE);
76           yInt = ConvertToPixel(allValues[1], pUICONTROL_FEATURE(sciObj)->units, sciObj, FALSE);
77           widthInt = ConvertToPixel(allValues[2], pUICONTROL_FEATURE(sciObj)->units, sciObj, TRUE);
78           heightInt = ConvertToPixel(allValues[3], pUICONTROL_FEATURE(sciObj)->units, sciObj, FALSE);
79           
80         }
81       else
82         {
83           Scierror(999, _("Wrong type for '%s' property: A string or a 1 x %d real row vector expected.\n"), "Position", 4);
84           return SET_PROPERTY_ERROR;
85         }
86     }
87
88   if (pUICONTROL_FEATURE(sciObj)->style == SCI_UIFRAME) /* Frame style uicontrols */
89     {
90       parent = sciGetParent(sciObj);
91       if (parent != NULL && sciGetEntityType(parent)==SCI_UICONTROL)
92         {
93           /* Parent is a frame and position is relative to parent */
94            returnValues = CallScilabBridge::getFramePosition(getScilabJavaVM(),
95                                                              pUICONTROL_FEATURE(parent)->hashMapIndex);
96            xInt = returnValues[0] + xInt;
97            yInt = returnValues[1] + yInt;
98         }
99
100       CallScilabBridge::setFramePosition(getScilabJavaVM(), 
101                                          pUICONTROL_FEATURE(sciObj)->hashMapIndex, 
102                                          xInt, 
103                                          yInt, 
104                                          widthInt, 
105                                          heightInt);
106     }
107   else if( sciGetEntityType(sciObj) == SCI_FIGURE ) /* Uicontrol figure */
108   {
109     /* disable protection since this function will call Java */
110     disableFigureSynchronization(sciObj);
111     status = sciSetDimension(sciObj, widthInt, heightInt) ;
112     enableFigureSynchronization(sciObj);
113     return (int)(sciInitScreenPosition(sciObj, xInt, yInt) & status);
114     //return (int)(sciInitScreenPosition(sciObj, xInt, yInt) & sciSetWindowDim(sciObj, widthInt, heightInt));
115   }
116   else /* All other uicontrol styles */
117     {
118
119       parent = sciGetParent(sciObj);
120       if (parent != NULL && sciGetEntityType(parent)==SCI_UICONTROL)
121         {
122           /* Parent is a frame and position is relative to parent */
123            returnValues = CallScilabBridge::getFramePosition(getScilabJavaVM(),
124                                                              pUICONTROL_FEATURE(parent)->hashMapIndex);
125            xInt = returnValues[0] + xInt;
126            yInt = returnValues[1] + yInt;
127                    delete [] returnValues;
128         }
129
130       CallScilabBridge::setWidgetPosition(getScilabJavaVM(), 
131                                           pUICONTROL_FEATURE(sciObj)->hashMapIndex, 
132                                           xInt, 
133                                           yInt, 
134                                           widthInt, 
135                                           heightInt);
136       
137       /* Special case for Sliders: set orientation */
138       if (pUICONTROL_FEATURE(sciObj)->style == SCI_SLIDER)
139         {
140           if (widthInt > heightInt) /* Horizontal ScrollBar */
141             {
142               CallScilabBridge::setSliderHorizontal(getScilabJavaVM(), 
143                                                           pUICONTROL_FEATURE(sciObj)->hashMapIndex);
144             }
145           else /* Vertical ScrollBar */
146             {
147               CallScilabBridge::setSliderVertical(getScilabJavaVM(), 
148                                                           pUICONTROL_FEATURE(sciObj)->hashMapIndex);
149             }
150         }
151     }
152   return SET_PROPERTY_SUCCEED;
153 }
154