Cleanup: remove redundant doxygen \file argument
[blender.git] / source / blender / blenkernel / intern / workspace.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16
17 /** \file \ingroup bke
18  */
19
20 /* allow accessing private members of DNA_workspace_types.h */
21 #define DNA_PRIVATE_WORKSPACE_ALLOW
22
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "BLI_utildefines.h"
27 #include "BLI_string.h"
28 #include "BLI_string_utils.h"
29 #include "BLI_listbase.h"
30
31 #include "BKE_idprop.h"
32 #include "BKE_library.h"
33 #include "BKE_main.h"
34 #include "BKE_scene.h"
35 #include "BKE_object.h"
36 #include "BKE_workspace.h"
37
38 #include "DNA_object_types.h"
39 #include "DNA_scene_types.h"
40 #include "DNA_screen_types.h"
41 #include "DNA_workspace_types.h"
42
43 #include "DEG_depsgraph.h"
44
45 #include "MEM_guardedalloc.h"
46
47
48 /* -------------------------------------------------------------------- */
49 /* Internal utils */
50
51 static void workspace_layout_name_set(
52         WorkSpace *workspace, WorkSpaceLayout *layout, const char *new_name)
53 {
54         BLI_strncpy(layout->name, new_name, sizeof(layout->name));
55         BLI_uniquename(&workspace->layouts, layout, "Layout", '.', offsetof(WorkSpaceLayout, name), sizeof(layout->name));
56 }
57
58 /**
59  * This should only be used directly when it is to be expected that there isn't
60  * a layout within \a workspace that wraps \a screen. Usually - especially outside
61  * of BKE_workspace - #BKE_workspace_layout_find should be used!
62  */
63 static WorkSpaceLayout *workspace_layout_find_exec(
64         const WorkSpace *workspace, const bScreen *screen)
65 {
66         return BLI_findptr(&workspace->layouts, screen, offsetof(WorkSpaceLayout, screen));
67 }
68
69 static void workspace_relation_add(
70         ListBase *relation_list, void *parent, void *data)
71 {
72         WorkSpaceDataRelation *relation = MEM_callocN(sizeof(*relation), __func__);
73         relation->parent = parent;
74         relation->value = data;
75         /* add to head, if we switch back to it soon we find it faster. */
76         BLI_addhead(relation_list, relation);
77 }
78 static void workspace_relation_remove(
79         ListBase *relation_list, WorkSpaceDataRelation *relation)
80 {
81         BLI_remlink(relation_list, relation);
82         MEM_freeN(relation);
83 }
84
85 static void workspace_relation_ensure_updated(
86         ListBase *relation_list, void *parent, void *data)
87 {
88         WorkSpaceDataRelation *relation = BLI_findptr(relation_list, parent, offsetof(WorkSpaceDataRelation, parent));
89         if (relation != NULL) {
90                 relation->value = data;
91                 /* reinsert at the head of the list, so that more commonly used relations are found faster. */
92                 BLI_remlink(relation_list, relation);
93                 BLI_addhead(relation_list, relation);
94         }
95         else {
96                 /* no matching relation found, add new one */
97                 workspace_relation_add(relation_list, parent, data);
98         }
99 }
100
101 static void *workspace_relation_get_data_matching_parent(
102         const ListBase *relation_list, const void *parent)
103 {
104         WorkSpaceDataRelation *relation = BLI_findptr(relation_list, parent, offsetof(WorkSpaceDataRelation, parent));
105         if (relation != NULL) {
106                 return relation->value;
107         }
108         else {
109                 return NULL;
110         }
111 }
112
113 /**
114  * Checks if \a screen is already used within any workspace. A screen should never be assigned to multiple
115  * WorkSpaceLayouts, but that should be ensured outside of the BKE_workspace module and without such checks.
116  * Hence, this should only be used as assert check before assigining a screen to a workspace.
117  */
118 #ifndef NDEBUG
119 static bool workspaces_is_screen_used
120 #else
121 static bool UNUSED_FUNCTION(workspaces_is_screen_used)
122 #endif
123         (const Main *bmain, bScreen *screen)
124 {
125         for (WorkSpace *workspace = bmain->workspaces.first; workspace; workspace = workspace->id.next) {
126                 if (workspace_layout_find_exec(workspace, screen)) {
127                         return true;
128                 }
129         }
130
131         return false;
132 }
133
134 /* -------------------------------------------------------------------- */
135 /* Create, delete, init */
136
137 WorkSpace *BKE_workspace_add(Main *bmain, const char *name)
138 {
139         WorkSpace *new_workspace = BKE_libblock_alloc(bmain, ID_WS, name, 0);
140         return new_workspace;
141 }
142
143 /**
144  * The function that actually frees the workspace data (not workspace itself). It shouldn't be called
145  * directly, instead #BKE_workspace_remove should be, which calls this through #BKE_id_free then.
146  *
147  * Should something like a bke_internal.h be added, this should go there!
148  */
149 void BKE_workspace_free(WorkSpace *workspace)
150 {
151         BKE_workspace_relations_free(&workspace->hook_layout_relations);
152
153         BLI_freelistN(&workspace->owner_ids);
154         BLI_freelistN(&workspace->layouts);
155
156         while (!BLI_listbase_is_empty(&workspace->tools)) {
157                 BKE_workspace_tool_remove(workspace, workspace->tools.first);
158         }
159
160         if (workspace->status_text) {
161                 MEM_freeN(workspace->status_text);
162                 workspace->status_text = NULL;
163         }
164 }
165
166 /**
167  * Remove \a workspace by freeing itself and its data. This is a higher-level wrapper that
168  * calls #BKE_workspace_free (through #BKE_id_free) to free the workspace data, and frees
169  * other data-blocks owned by \a workspace and its layouts (currently that is screens only).
170  *
171  * Always use this to remove (and free) workspaces. Don't free non-ID workspace members here.
172  */
173 void BKE_workspace_remove(Main *bmain, WorkSpace *workspace)
174 {
175         for (WorkSpaceLayout *layout = workspace->layouts.first, *layout_next; layout; layout = layout_next) {
176                 layout_next = layout->next;
177                 BKE_workspace_layout_remove(bmain, workspace, layout);
178         }
179         BKE_id_free(bmain, workspace);
180 }
181
182 WorkSpaceInstanceHook *BKE_workspace_instance_hook_create(const Main *bmain)
183 {
184         WorkSpaceInstanceHook *hook = MEM_callocN(sizeof(WorkSpaceInstanceHook), __func__);
185
186         /* set an active screen-layout for each possible window/workspace combination */
187         for (WorkSpace *workspace = bmain->workspaces.first; workspace; workspace = workspace->id.next) {
188                 BKE_workspace_hook_layout_for_workspace_set(hook, workspace, workspace->layouts.first);
189         }
190
191         return hook;
192 }
193 void BKE_workspace_instance_hook_free(const Main *bmain, WorkSpaceInstanceHook *hook)
194 {
195         /* workspaces should never be freed before wm (during which we call this function) */
196         BLI_assert(!BLI_listbase_is_empty(&bmain->workspaces));
197
198         /* Free relations for this hook */
199         for (WorkSpace *workspace = bmain->workspaces.first; workspace; workspace = workspace->id.next) {
200                 for (WorkSpaceDataRelation *relation = workspace->hook_layout_relations.first, *relation_next;
201                      relation;
202                      relation = relation_next)
203                 {
204                         relation_next = relation->next;
205                         if (relation->parent == hook) {
206                                 workspace_relation_remove(&workspace->hook_layout_relations, relation);
207                         }
208                 }
209         }
210
211         MEM_freeN(hook);
212 }
213
214 /**
215  * Add a new layout to \a workspace for \a screen.
216  */
217 WorkSpaceLayout *BKE_workspace_layout_add(
218         Main *bmain,
219         WorkSpace *workspace,
220         bScreen *screen,
221         const char *name)
222 {
223         WorkSpaceLayout *layout = MEM_callocN(sizeof(*layout), __func__);
224
225         BLI_assert(!workspaces_is_screen_used(bmain, screen));
226 #ifndef DEBUG
227         UNUSED_VARS(bmain);
228 #endif
229         layout->screen = screen;
230         id_us_plus(&layout->screen->id);
231         workspace_layout_name_set(workspace, layout, name);
232         BLI_addtail(&workspace->layouts, layout);
233
234         return layout;
235 }
236
237 void BKE_workspace_layout_remove(
238         Main *bmain,
239         WorkSpace *workspace, WorkSpaceLayout *layout)
240 {
241         id_us_min(&layout->screen->id);
242         BKE_id_free(bmain, layout->screen);
243         BLI_freelinkN(&workspace->layouts, layout);
244 }
245
246 void BKE_workspace_relations_free(
247         ListBase *relation_list)
248 {
249         for (WorkSpaceDataRelation *relation = relation_list->first, *relation_next; relation; relation = relation_next) {
250                 relation_next = relation->next;
251                 workspace_relation_remove(relation_list, relation);
252         }
253 }
254
255 /* -------------------------------------------------------------------- */
256 /* General Utils */
257
258 WorkSpaceLayout *BKE_workspace_layout_find(
259         const WorkSpace *workspace, const bScreen *screen)
260 {
261         WorkSpaceLayout *layout = workspace_layout_find_exec(workspace, screen);
262         if (layout) {
263                 return layout;
264         }
265
266         printf("%s: Couldn't find layout in this workspace: '%s' screen: '%s'. "
267                "This should not happen!\n",
268                __func__, workspace->id.name + 2, screen->id.name + 2);
269
270         return NULL;
271 }
272
273 /**
274  * Find the layout for \a screen without knowing which workspace to look in.
275  * Can also be used to find the workspace that contains \a screen.
276  *
277  * \param r_workspace: Optionally return the workspace that contains the looked up layout (if found).
278  */
279 WorkSpaceLayout *BKE_workspace_layout_find_global(
280         const Main *bmain, const bScreen *screen,
281         WorkSpace **r_workspace)
282 {
283         WorkSpaceLayout *layout;
284
285         if (r_workspace) {
286                 *r_workspace = NULL;
287         }
288
289         for (WorkSpace *workspace = bmain->workspaces.first; workspace; workspace = workspace->id.next) {
290                 if ((layout = workspace_layout_find_exec(workspace, screen))) {
291                         if (r_workspace) {
292                                 *r_workspace = workspace;
293                         }
294
295                         return layout;
296                 }
297         }
298
299         return NULL;
300 }
301
302 /**
303  * Circular workspace layout iterator.
304  *
305  * \param callback: Custom function which gets executed for each layout. Can return false to stop iterating.
306  * \param arg: Custom data passed to each \a callback call.
307  *
308  * \return the layout at which \a callback returned false.
309  */
310 WorkSpaceLayout *BKE_workspace_layout_iter_circular(
311         const WorkSpace *workspace, WorkSpaceLayout *start,
312         bool (*callback)(const WorkSpaceLayout *layout, void *arg),
313         void *arg, const bool iter_backward)
314 {
315         WorkSpaceLayout *iter_layout;
316
317         if (iter_backward) {
318                 LISTBASE_CIRCULAR_BACKWARD_BEGIN(&workspace->layouts, iter_layout, start)
319                 {
320                         if (!callback(iter_layout, arg)) {
321                                 return iter_layout;
322                         }
323                 }
324                 LISTBASE_CIRCULAR_BACKWARD_END(&workspace->layouts, iter_layout, start);
325         }
326         else {
327                 LISTBASE_CIRCULAR_FORWARD_BEGIN(&workspace->layouts, iter_layout, start)
328                 {
329                         if (!callback(iter_layout, arg)) {
330                                 return iter_layout;
331                         }
332                 }
333                 LISTBASE_CIRCULAR_FORWARD_END(&workspace->layouts, iter_layout, start)
334         }
335
336         return NULL;
337 }
338
339 void BKE_workspace_tool_remove(
340         struct WorkSpace *workspace, struct bToolRef *tref)
341 {
342         if (tref->runtime) {
343                 MEM_freeN(tref->runtime);
344         }
345         if (tref->properties) {
346                 IDP_FreeProperty(tref->properties);
347                 MEM_freeN(tref->properties);
348         }
349         BLI_remlink(&workspace->tools, tref);
350         MEM_freeN(tref);
351 }
352
353 /* -------------------------------------------------------------------- */
354 /* Getters/Setters */
355
356 WorkSpace *BKE_workspace_active_get(WorkSpaceInstanceHook *hook)
357 {
358         return hook->active;
359 }
360 void BKE_workspace_active_set(WorkSpaceInstanceHook *hook, WorkSpace *workspace)
361 {
362         hook->active = workspace;
363         if (workspace) {
364                 WorkSpaceLayout *layout = workspace_relation_get_data_matching_parent(&workspace->hook_layout_relations, hook);
365                 if (layout) {
366                         hook->act_layout = layout;
367                 }
368         }
369 }
370
371 WorkSpaceLayout *BKE_workspace_active_layout_get(const WorkSpaceInstanceHook *hook)
372 {
373         return hook->act_layout;
374 }
375 void BKE_workspace_active_layout_set(WorkSpaceInstanceHook *hook, WorkSpaceLayout *layout)
376 {
377         hook->act_layout = layout;
378 }
379
380 bScreen *BKE_workspace_active_screen_get(const WorkSpaceInstanceHook *hook)
381 {
382         return hook->act_layout->screen;
383 }
384 void BKE_workspace_active_screen_set(WorkSpaceInstanceHook *hook, WorkSpace *workspace, bScreen *screen)
385 {
386         /* we need to find the WorkspaceLayout that wraps this screen */
387         WorkSpaceLayout *layout = BKE_workspace_layout_find(hook->active, screen);
388         BKE_workspace_hook_layout_for_workspace_set(hook, workspace, layout);
389 }
390
391 ListBase *BKE_workspace_layouts_get(WorkSpace *workspace)
392 {
393         return &workspace->layouts;
394 }
395
396 const char *BKE_workspace_layout_name_get(const WorkSpaceLayout *layout)
397 {
398         return layout->name;
399 }
400 void BKE_workspace_layout_name_set(WorkSpace *workspace, WorkSpaceLayout *layout, const char *new_name)
401 {
402         workspace_layout_name_set(workspace, layout, new_name);
403 }
404
405 bScreen *BKE_workspace_layout_screen_get(const WorkSpaceLayout *layout)
406 {
407         return layout->screen;
408 }
409 void BKE_workspace_layout_screen_set(WorkSpaceLayout *layout, bScreen *screen)
410 {
411         layout->screen = screen;
412 }
413
414 WorkSpaceLayout *BKE_workspace_hook_layout_for_workspace_get(
415         const WorkSpaceInstanceHook *hook, const WorkSpace *workspace)
416 {
417         return workspace_relation_get_data_matching_parent(&workspace->hook_layout_relations, hook);
418 }
419 void BKE_workspace_hook_layout_for_workspace_set(
420         WorkSpaceInstanceHook *hook, WorkSpace *workspace, WorkSpaceLayout *layout)
421 {
422         hook->act_layout = layout;
423         workspace_relation_ensure_updated(&workspace->hook_layout_relations, hook, layout);
424 }
425
426 bool BKE_workspace_owner_id_check(
427         const WorkSpace *workspace, const char *owner_id)
428 {
429         if ((*owner_id == '\0') ||
430             ((workspace->flags & WORKSPACE_USE_FILTER_BY_ORIGIN) == 0))
431         {
432                 return true;
433         }
434         else {
435                 /* we could use hash lookup, for now this list is highly under < ~16 items. */
436                 return BLI_findstring(&workspace->owner_ids, owner_id, offsetof(wmOwnerID, name)) != NULL;
437         }
438 }