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