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