1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
/**
* This file is a part of the Cairo-Dock project
*
* Copyright : (C) see the 'copyright' file.
* E-mail : see the 'copyright' file.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "cairo-dock-struct.h"
#include "cairo-dock-manager.h"
#include "cairo-dock-log.h"
#include "cairo-dock-object.h"
/* obj -> mgr0 -> mgr1 -> ... -> mgrN
* new_dialog -> new_object (mgr, attr) -> mgr->top_parent->init(attr) -> mgr->parent->init(attr) -> mgr->init(attr) --> notif
* unref_object -> ref-- -> notif -> mgr->destroy -> mgr->parent->destroy -> mgr->top_parent->destroy --> free
* GLDI_OBJECT_IS_xxx obj->mgr == pMgr || mgr->parent->mrg == pMgr || ...
* */
void gldi_object_set_manager (GldiObject *pObject, GldiManager *pMgr)
{
pObject->mgr = pMgr;
pObject->mgrs = g_list_copy (pMgr->object.mgrs);
pObject->mgrs = g_list_append (pObject->mgrs, pMgr);
gldi_object_install_notifications (pObject, pMgr->object.pNotificationsTab->len);
}
void gldi_object_init (GldiObject *obj, GldiManager *pMgr, gpointer attr)
{
obj->ref = 1;
// set the manager
gldi_object_set_manager (obj, pMgr);
// init the object
GList *m;
for (m = obj->mgrs; m != NULL; m = m->next)
{
pMgr = m->data;
if (pMgr->init_object)
pMgr->init_object (obj, attr);
}
// emit a notification
gldi_object_notify (obj, NOTIFICATION_NEW, obj);
}
GldiObject *gldi_object_new (GldiManager *pMgr, gpointer attr)
{
GldiObject *obj = g_malloc0 (pMgr->iObjectSize);
gldi_object_init (obj, pMgr, attr);
return obj;
}
void gldi_object_ref (GldiObject *pObject)
{
g_return_if_fail (pObject != NULL && pObject->ref > 0);
pObject->ref ++;
}
void gldi_object_unref (GldiObject *pObject)
{
if (pObject == NULL)
return;
pObject->ref --;
if (pObject->ref == 0) // so if it was already 0, don't do anything
{
// emit a notification
gldi_object_notify (pObject, NOTIFICATION_DESTROY, pObject);
// reset the object
GldiManager *pMgr = pObject->mgr;
while (pMgr)
{
if (pMgr->reset_object)
pMgr->reset_object (pObject);
pMgr = pMgr->object.mgr;
}
// clear notifications
GPtrArray *pNotificationsTab = pObject->pNotificationsTab;
guint i;
for (i = 0; i < pNotificationsTab->len; i ++)
{
GSList *pNotificationRecordList = g_ptr_array_index (pNotificationsTab, i);
g_slist_foreach (pNotificationRecordList, (GFunc)g_free, NULL);
g_slist_free (pNotificationRecordList);
}
g_ptr_array_free (pNotificationsTab, TRUE);
// free memory
g_free (pObject);
}
}
void gldi_object_delete (GldiObject *pObject)
{
if (pObject == NULL)
return;
//\_________________ delete the object from the current theme
gboolean r = TRUE;
GldiManager *pMgr = pObject->mgr;
while (pMgr)
{
if (pMgr->delete_object)
r = pMgr->delete_object (pObject);
if (!r)
return;
pMgr = pMgr->object.mgr;
}
//\_________________ destroy the object
gldi_object_unref (pObject);
}
gboolean gldi_object_is_manager_child (GldiObject *pObject, GldiManager *pMgr)
{
while (pObject)
{
if (pObject->mgr == pMgr)
return TRUE;
pObject = GLDI_OBJECT (pObject->mgr);
}
return FALSE;
}
#define GLDI_OBJECT_IS_DOCK(obj) gldi_object_is_manager_child (obj, &myDocksMgr)
void gldi_object_register_notification (gpointer pObject, GldiNotificationType iNotifType, GldiNotificationFunc pFunction, gboolean bRunFirst, gpointer pUserData)
{
g_return_if_fail (pObject != NULL);
// grab the notifications tab
GPtrArray *pNotificationsTab = GLDI_OBJECT(pObject)->pNotificationsTab;
if (!pNotificationsTab || pNotificationsTab->len < iNotifType)
{
cd_warning ("someone tried to register to an inexisting notification (%d) on an object of type '%s'", iNotifType, GLDI_OBJECT(pObject)->mgr?GLDI_OBJECT(pObject)->mgr->cModuleName:"manager");
return ; // don't try to create/resize the notifications tab, since noone will emit this notification.
}
// add a record
GldiNotificationRecord *pNotificationRecord = g_new (GldiNotificationRecord, 1);
pNotificationRecord->pFunction = pFunction;
pNotificationRecord->pUserData = pUserData;
GSList *pNotificationRecordList = g_ptr_array_index (pNotificationsTab, iNotifType);
pNotificationsTab->pdata[iNotifType] = (bRunFirst ? g_slist_prepend : g_slist_append) (pNotificationRecordList, pNotificationRecord);
}
void gldi_object_remove_notification (gpointer pObject, GldiNotificationType iNotifType, GldiNotificationFunc pFunction, gpointer pUserData)
{
g_return_if_fail (pObject != NULL);
// grab the notifications tab
GPtrArray *pNotificationsTab = GLDI_OBJECT(pObject)->pNotificationsTab;
// remove the record
GSList *pNotificationRecordList = g_ptr_array_index (pNotificationsTab, iNotifType);
GldiNotificationRecord *pNotificationRecord;
GSList *nr;
for (nr = pNotificationRecordList; nr != NULL; nr = nr->next)
{
pNotificationRecord = nr->data;
if (pNotificationRecord->pFunction == pFunction && pNotificationRecord->pUserData == pUserData)
{
pNotificationsTab->pdata[iNotifType] = g_slist_delete_link (pNotificationRecordList, nr);
g_free (pNotificationRecord);
break;
}
}
}
|