Graphics: return a null pointer in C when property does not exist
[scilab.git] / scilab / modules / graphics / src / c / getHandleProperty / get_children_property.c
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2004-2006 - INRIA - Fabrice Leray
4  * Copyright (C) 2006 - INRIA - Allan Cornet
5  * Copyright (C) 2006 - INRIA - Jean-Baptiste Silvy
6  * Copyright (C) 2011 - DIGITEO - Vincent Couvert
7  *
8  * This file must be used under the terms of the CeCILL.
9  * This source file is licensed as described in the file COPYING, which
10  * you should have received as part of this distribution.  The terms
11  * are also available at
12  * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
13  *
14  */
15
16 /*------------------------------------------------------------------------*/
17 /* file: get_children_property.c                                          */
18 /* desc : function to retrieve in Scilab the children field of a          */
19 /*        handle                                                          */
20 /*------------------------------------------------------------------------*/
21
22 #include "getHandleProperty.h"
23 #include "GetProperty.h"
24 #include "returnProperty.h"
25 #include "MALLOC.h"
26 #include "Scierror.h"
27 #include "localization.h"
28 #include "HandleManagement.h"
29 #include "getConsoleIdentifier.h"
30
31 #include "getGraphicObjectProperty.h"
32 #include "graphicObjectProperties.h"
33
34 /*--------------------------------------------------------------------------*/
35 int get_children_property(void* _pvCtx, char* pobjUID)
36 {
37     int i = 0;
38     int status = 0;
39     long *plChildren = NULL;
40     char **pstChildrenUID;
41     int iHidden = 0;
42     int *piHidden = &iHidden;
43     int iNotHiddenChildrenNumber = 0;
44     int iChildIndex = 0;
45     int iShowHiddenHandles = 0;
46     int *piShowHiddenHandles = &iShowHiddenHandles;
47
48     // All Graphic Objects have __GO_CHILDREN__ & __GO_CHILDREN_COUNT__ properties.
49     int iChildrenCount = 0;
50     int *piChildrenCount = &iChildrenCount;
51
52     getGraphicObjectProperty(pobjUID, __GO_CHILDREN_COUNT__, jni_int, (void **)&piChildrenCount);
53     if (piChildrenCount == NULL || piChildrenCount[0] == 0)
54     {
55         // No Child
56         return sciReturnEmptyMatrix(_pvCtx);
57     }
58
59     getGraphicObjectProperty(pobjUID, __GO_CHILDREN__, jni_string_vector, (void **)&pstChildrenUID);
60
61     getGraphicObjectProperty(getConsoleIdentifier(), __GO_SHOWHIDDENHANDLES__, jni_bool, (void **)&piShowHiddenHandles);
62
63     if (iShowHiddenHandles == 0)
64     {
65         // Find number of not hidden children
66         for (i = 0; i < piChildrenCount[0]; ++i)
67         {
68             getGraphicObjectProperty(pstChildrenUID[i], __GO_HIDDEN__, jni_bool, (void **)&piHidden);
69             if (iHidden == 0)
70             {
71                 iNotHiddenChildrenNumber++;
72             }
73         }
74
75         if (iNotHiddenChildrenNumber == 0)
76         {
77             // No Child
78             return sciReturnEmptyMatrix(_pvCtx);
79         }
80     }
81     else
82     {
83         iNotHiddenChildrenNumber = piChildrenCount[0];
84     }
85
86     plChildren = MALLOC(iNotHiddenChildrenNumber * sizeof(long));
87
88     for (i = 0; i < piChildrenCount[0]; ++i)
89     {
90         getGraphicObjectProperty(pstChildrenUID[i], __GO_HIDDEN__, jni_bool, (void **)&piHidden);
91         if (iHidden == 0 || iShowHiddenHandles == 1)
92         {
93             plChildren[iChildIndex++] = getHandle(pstChildrenUID[i]);
94         }
95     }
96
97     status = sciReturnColHandleVector(_pvCtx, plChildren, iNotHiddenChildrenNumber);
98     FREE(plChildren);
99
100     return status;
101 }
102
103 /*--------------------------------------------------------------------------*/