Cleanup: use typed unsigned ints
[blender.git] / source / blender / editors / interface / resources.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  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL/BL DUAL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/interface/resources.c
29  *  \ingroup edinterface
30  */
31
32 #include <math.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "MEM_guardedalloc.h"
37
38 #include "DNA_screen_types.h"
39 #include "DNA_space_types.h"
40 #include "DNA_userdef_types.h"
41
42 #include "BLI_blenlib.h"
43 #include "BLI_utildefines.h"
44 #include "BLI_math.h"
45
46 #include "BKE_addon.h"
47 #include "BKE_appdir.h"
48 #include "BKE_main.h"
49 #include "BKE_mesh_runtime.h"
50
51 #include "BLO_readfile.h"  /* for UserDef version patching. */
52
53 #include "BLF_api.h"
54
55 #include "ED_screen.h"
56
57 #include "UI_interface.h"
58 #include "UI_interface_icons.h"
59
60 #include "interface_intern.h"
61 #include "GPU_framebuffer.h"
62
63 /* global for themes */
64 typedef void (*VectorDrawFunc)(int x, int y, int w, int h, float alpha);
65
66 /* be sure to keep 'bThemeState' in sync */
67 static struct bThemeState g_theme_state = {
68     NULL,
69     SPACE_VIEW3D,
70     RGN_TYPE_WINDOW,
71 };
72
73 #define theme_active g_theme_state.theme
74 #define theme_spacetype g_theme_state.spacetype
75 #define theme_regionid g_theme_state.regionid
76
77 void ui_resources_init(void)
78 {
79         UI_icons_init();
80 }
81
82 void ui_resources_free(void)
83 {
84         UI_icons_free();
85 }
86
87
88 /* ******************************************************** */
89 /*    THEMES */
90 /* ******************************************************** */
91
92 const uchar *UI_ThemeGetColorPtr(bTheme *btheme, int spacetype, int colorid)
93 {
94         ThemeSpace *ts = NULL;
95         static char error[4] = {240, 0, 240, 255};
96         static char alert[4] = {240, 60, 60, 255};
97         static char headerdesel[4] = {0, 0, 0, 255};
98         static char back[4] = {0, 0, 0, 255};
99         static char setting = 0;
100         const char *cp = error;
101
102         /* ensure we're not getting a color after running BKE_blender_userdef_free */
103         BLI_assert(BLI_findindex(&U.themes, theme_active) != -1);
104         BLI_assert(colorid != TH_UNDEFINED);
105
106         if (btheme) {
107
108                 /* first check for ui buttons theme */
109                 if (colorid < TH_THEMEUI) {
110
111                         switch (colorid) {
112
113                                 case TH_REDALERT:
114                                         cp = alert; break;
115                         }
116                 }
117                 else {
118
119                         switch (spacetype) {
120                                 case SPACE_BUTS:
121                                         ts = &btheme->tbuts;
122                                         break;
123                                 case SPACE_VIEW3D:
124                                         ts = &btheme->tv3d;
125                                         break;
126                                 case SPACE_IPO:
127                                         ts = &btheme->tipo;
128                                         break;
129                                 case SPACE_FILE:
130                                         ts = &btheme->tfile;
131                                         break;
132                                 case SPACE_NLA:
133                                         ts = &btheme->tnla;
134                                         break;
135                                 case SPACE_ACTION:
136                                         ts = &btheme->tact;
137                                         break;
138                                 case SPACE_SEQ:
139                                         ts = &btheme->tseq;
140                                         break;
141                                 case SPACE_IMAGE:
142                                         ts = &btheme->tima;
143                                         break;
144                                 case SPACE_TEXT:
145                                         ts = &btheme->text;
146                                         break;
147                                 case SPACE_OUTLINER:
148                                         ts = &btheme->toops;
149                                         break;
150                                 case SPACE_INFO:
151                                         ts = &btheme->tinfo;
152                                         break;
153                                 case SPACE_USERPREF:
154                                         ts = &btheme->tuserpref;
155                                         break;
156                                 case SPACE_CONSOLE:
157                                         ts = &btheme->tconsole;
158                                         break;
159                                 case SPACE_NODE:
160                                         ts = &btheme->tnode;
161                                         break;
162                                 case SPACE_CLIP:
163                                         ts = &btheme->tclip;
164                                         break;
165                                 case SPACE_TOPBAR:
166                                         ts = &btheme->ttopbar;
167                                         break;
168                                 case SPACE_STATUSBAR:
169                                         ts = &btheme->tstatusbar;
170                                         break;
171                                 default:
172                                         ts = &btheme->tv3d;
173                                         break;
174                         }
175
176                         switch (colorid) {
177                                 case TH_BACK:
178                                         if (ELEM(theme_regionid, RGN_TYPE_WINDOW, RGN_TYPE_PREVIEW))
179                                                 cp = ts->back;
180                                         else if (theme_regionid == RGN_TYPE_CHANNELS)
181                                                 cp = ts->list;
182                                         else if (theme_regionid == RGN_TYPE_HEADER)
183                                                 cp = ts->header;
184                                         else if (theme_regionid == RGN_TYPE_NAV_BAR)
185                                                 cp = ts->navigation_bar;
186                                         else
187                                                 cp = ts->button;
188
189                                         copy_v4_v4_char(back, cp);
190                                         if (!ED_region_is_overlap(spacetype, theme_regionid)) {
191                                                 back[3] = 255;
192                                         }
193                                         cp = back;
194                                         break;
195                                 case TH_LOW_GRAD:
196                                         cp = ts->gradients.gradient;
197                                         break;
198                                 case TH_HIGH_GRAD:
199                                         cp = ts->gradients.high_gradient;
200                                         break;
201                                 case TH_SHOW_BACK_GRAD:
202                                         cp = &setting;
203                                         setting = ts->gradients.show_grad;
204                                         break;
205                                 case TH_TEXT:
206                                         if (theme_regionid == RGN_TYPE_WINDOW)
207                                                 cp = ts->text;
208                                         else if (theme_regionid == RGN_TYPE_CHANNELS)
209                                                 cp = ts->list_text;
210                                         else if (theme_regionid == RGN_TYPE_HEADER)
211                                                 cp = ts->header_text;
212                                         else
213                                                 cp = ts->button_text;
214                                         break;
215                                 case TH_TEXT_HI:
216                                         if (theme_regionid == RGN_TYPE_WINDOW)
217                                                 cp = ts->text_hi;
218                                         else if (theme_regionid == RGN_TYPE_CHANNELS)
219                                                 cp = ts->list_text_hi;
220                                         else if (theme_regionid == RGN_TYPE_HEADER)
221                                                 cp = ts->header_text_hi;
222                                         else
223                                                 cp = ts->button_text_hi;
224                                         break;
225                                 case TH_TITLE:
226                                         if (theme_regionid == RGN_TYPE_WINDOW)
227                                                 cp = ts->title;
228                                         else if (theme_regionid == RGN_TYPE_CHANNELS)
229                                                 cp = ts->list_title;
230                                         else if (theme_regionid == RGN_TYPE_HEADER)
231                                                 cp = ts->header_title;
232                                         else
233                                                 cp = ts->button_title;
234                                         break;
235
236                                 case TH_HEADER:
237                                         cp = ts->header; break;
238                                 case TH_HEADERDESEL:
239                                         /* we calculate a dynamic builtin header deselect color, also for pulldowns... */
240                                         cp = ts->header;
241                                         headerdesel[0] = cp[0] > 10 ? cp[0] - 10 : 0;
242                                         headerdesel[1] = cp[1] > 10 ? cp[1] - 10 : 0;
243                                         headerdesel[2] = cp[2] > 10 ? cp[2] - 10 : 0;
244                                         headerdesel[3] = cp[3];
245                                         cp = headerdesel;
246                                         break;
247                                 case TH_HEADER_TEXT:
248                                         cp = ts->header_text; break;
249                                 case TH_HEADER_TEXT_HI:
250                                         cp = ts->header_text_hi; break;
251
252                                 case TH_PANEL_HEADER:
253                                         cp = ts->panelcolors.header; break;
254                                 case TH_PANEL_BACK:
255                                         cp = ts->panelcolors.back; break;
256                                 case TH_PANEL_SUB_BACK:
257                                         cp = ts->panelcolors.sub_back; break;
258
259                                 case TH_BUTBACK:
260                                         cp = ts->button; break;
261                                 case TH_BUTBACK_TEXT:
262                                         cp = ts->button_text; break;
263                                 case TH_BUTBACK_TEXT_HI:
264                                         cp = ts->button_text_hi; break;
265
266                                 case TH_TAB_ACTIVE:
267                                         cp = ts->tab_active; break;
268                                 case TH_TAB_INACTIVE:
269                                         cp = ts->tab_inactive; break;
270                                 case TH_TAB_BACK:
271                                         cp = ts->tab_back; break;
272                                 case TH_TAB_OUTLINE:
273                                         cp = ts->tab_outline; break;
274
275                                 case TH_SHADE1:
276                                         cp = ts->shade1; break;
277                                 case TH_SHADE2:
278                                         cp = ts->shade2; break;
279                                 case TH_HILITE:
280                                         cp = ts->hilite; break;
281
282                                 case TH_GRID:
283                                         cp = ts->grid; break;
284                                 case TH_VIEW_OVERLAY:
285                                         cp = ts->view_overlay; break;
286                                 case TH_WIRE:
287                                         cp = ts->wire; break;
288                                 case TH_WIRE_INNER:
289                                         cp = ts->syntaxr; break;
290                                 case TH_WIRE_EDIT:
291                                         cp = ts->wire_edit; break;
292                                 case TH_LAMP:
293                                         cp = ts->lamp; break;
294                                 case TH_SPEAKER:
295                                         cp = ts->speaker; break;
296                                 case TH_CAMERA:
297                                         cp = ts->camera; break;
298                                 case TH_EMPTY:
299                                         cp = ts->empty; break;
300                                 case TH_SELECT:
301                                         cp = ts->select; break;
302                                 case TH_ACTIVE:
303                                         cp = ts->active; break;
304                                 case TH_GROUP:
305                                         cp = ts->group; break;
306                                 case TH_GROUP_ACTIVE:
307                                         cp = ts->group_active; break;
308                                 case TH_TRANSFORM:
309                                         cp = ts->transform; break;
310                                 case TH_VERTEX:
311                                         cp = ts->vertex; break;
312                                 case TH_VERTEX_SELECT:
313                                         cp = ts->vertex_select; break;
314                                 case TH_VERTEX_BEVEL:
315                                         cp = ts->vertex_bevel; break;
316                                 case TH_VERTEX_UNREFERENCED:
317                                         cp = ts->vertex_unreferenced; break;
318                                 case TH_VERTEX_SIZE:
319                                         cp = &ts->vertex_size; break;
320                                 case TH_OUTLINE_WIDTH:
321                                         cp = &ts->outline_width; break;
322                                 case TH_EDGE:
323                                         cp = ts->edge; break;
324                                 case TH_EDGE_SELECT:
325                                         cp = ts->edge_select; break;
326                                 case TH_EDGE_SEAM:
327                                         cp = ts->edge_seam; break;
328                                 case TH_EDGE_SHARP:
329                                         cp = ts->edge_sharp; break;
330                                 case TH_EDGE_CREASE:
331                                         cp = ts->edge_crease; break;
332                                 case TH_EDGE_BEVEL:
333                                         cp = ts->edge_bevel; break;
334                                 case TH_EDITMESH_ACTIVE:
335                                         cp = ts->editmesh_active; break;
336                                 case TH_EDGE_FACESEL:
337                                         cp = ts->edge_facesel; break;
338                                 case TH_FACE:
339                                         cp = ts->face; break;
340                                 case TH_FACE_SELECT:
341                                         cp = ts->face_select; break;
342                                 case TH_FACE_DOT:
343                                         cp = ts->face_dot; break;
344                                 case TH_FACEDOT_SIZE:
345                                         cp = &ts->facedot_size; break;
346                                 case TH_DRAWEXTRA_EDGELEN:
347                                         cp = ts->extra_edge_len; break;
348                                 case TH_DRAWEXTRA_EDGEANG:
349                                         cp = ts->extra_edge_angle; break;
350                                 case TH_DRAWEXTRA_FACEAREA:
351                                         cp = ts->extra_face_area; break;
352                                 case TH_DRAWEXTRA_FACEANG:
353                                         cp = ts->extra_face_angle; break;
354                                 case TH_NORMAL:
355                                         cp = ts->normal; break;
356                                 case TH_VNORMAL:
357                                         cp = ts->vertex_normal; break;
358                                 case TH_LNORMAL:
359                                         cp = ts->loop_normal; break;
360                                 case TH_BONE_SOLID:
361                                         cp = ts->bone_solid; break;
362                                 case TH_BONE_POSE:
363                                         cp = ts->bone_pose; break;
364                                 case TH_BONE_POSE_ACTIVE:
365                                         cp = ts->bone_pose_active; break;
366                                 case TH_STRIP:
367                                         cp = ts->strip; break;
368                                 case TH_STRIP_SELECT:
369                                         cp = ts->strip_select; break;
370                                 case TH_KEYTYPE_KEYFRAME:
371                                         cp = ts->keytype_keyframe; break;
372                                 case TH_KEYTYPE_KEYFRAME_SELECT:
373                                         cp = ts->keytype_keyframe_select; break;
374                                 case TH_KEYTYPE_EXTREME:
375                                         cp = ts->keytype_extreme; break;
376                                 case TH_KEYTYPE_EXTREME_SELECT:
377                                         cp = ts->keytype_extreme_select; break;
378                                 case TH_KEYTYPE_BREAKDOWN:
379                                         cp = ts->keytype_breakdown; break;
380                                 case TH_KEYTYPE_BREAKDOWN_SELECT:
381                                         cp = ts->keytype_breakdown_select; break;
382                                 case TH_KEYTYPE_JITTER:
383                                         cp = ts->keytype_jitter; break;
384                                 case TH_KEYTYPE_JITTER_SELECT:
385                                         cp = ts->keytype_jitter_select; break;
386                                 case TH_KEYTYPE_MOVEHOLD:
387                                         cp = ts->keytype_movehold; break;
388                                 case TH_KEYTYPE_MOVEHOLD_SELECT:
389                                         cp = ts->keytype_movehold_select; break;
390                                 case TH_KEYBORDER:
391                                         cp = ts->keyborder; break;
392                                 case TH_KEYBORDER_SELECT:
393                                         cp = ts->keyborder_select; break;
394                                 case TH_CFRAME:
395                                         cp = ts->cframe; break;
396                                 case TH_TIME_KEYFRAME:
397                                         cp = ts->time_keyframe; break;
398                                 case TH_TIME_GP_KEYFRAME:
399                                         cp = ts->time_gp_keyframe; break;
400                                 case TH_NURB_ULINE:
401                                         cp = ts->nurb_uline; break;
402                                 case TH_NURB_VLINE:
403                                         cp = ts->nurb_vline; break;
404                                 case TH_NURB_SEL_ULINE:
405                                         cp = ts->nurb_sel_uline; break;
406                                 case TH_NURB_SEL_VLINE:
407                                         cp = ts->nurb_sel_vline; break;
408                                 case TH_ACTIVE_SPLINE:
409                                         cp = ts->act_spline; break;
410                                 case TH_ACTIVE_VERT:
411                                         cp = ts->lastsel_point; break;
412                                 case TH_HANDLE_FREE:
413                                         cp = ts->handle_free; break;
414                                 case TH_HANDLE_AUTO:
415                                         cp = ts->handle_auto; break;
416                                 case TH_HANDLE_AUTOCLAMP:
417                                         cp = ts->handle_auto_clamped; break;
418                                 case TH_HANDLE_VECT:
419                                         cp = ts->handle_vect; break;
420                                 case TH_HANDLE_ALIGN:
421                                         cp = ts->handle_align; break;
422                                 case TH_HANDLE_SEL_FREE:
423                                         cp = ts->handle_sel_free; break;
424                                 case TH_HANDLE_SEL_AUTO:
425                                         cp = ts->handle_sel_auto; break;
426                                 case TH_HANDLE_SEL_AUTOCLAMP:
427                                         cp = ts->handle_sel_auto_clamped; break;
428                                 case TH_HANDLE_SEL_VECT:
429                                         cp = ts->handle_sel_vect; break;
430                                 case TH_HANDLE_SEL_ALIGN:
431                                         cp = ts->handle_sel_align; break;
432                                 case TH_FREESTYLE_EDGE_MARK:
433                                         cp = ts->freestyle_edge_mark; break;
434                                 case TH_FREESTYLE_FACE_MARK:
435                                         cp = ts->freestyle_face_mark; break;
436
437                                 case TH_SYNTAX_B:
438                                         cp = ts->syntaxb; break;
439                                 case TH_SYNTAX_V:
440                                         cp = ts->syntaxv; break;
441                                 case TH_SYNTAX_C:
442                                         cp = ts->syntaxc; break;
443                                 case TH_SYNTAX_L:
444                                         cp = ts->syntaxl; break;
445                                 case TH_SYNTAX_D:
446                                         cp = ts->syntaxd; break;
447                                 case TH_SYNTAX_R:
448                                         cp = ts->syntaxr; break;
449                                 case TH_SYNTAX_N:
450                                         cp = ts->syntaxn; break;
451                                 case TH_SYNTAX_S:
452                                         cp = ts->syntaxs; break;
453
454                                 case TH_NODE:
455                                         cp = ts->syntaxl; break;
456                                 case TH_NODE_INPUT:
457                                         cp = ts->syntaxn; break;
458                                 case TH_NODE_OUTPUT:
459                                         cp = ts->nodeclass_output; break;
460                                 case TH_NODE_COLOR:
461                                         cp = ts->syntaxb; break;
462                                 case TH_NODE_FILTER:
463                                         cp = ts->nodeclass_filter; break;
464                                 case TH_NODE_VECTOR:
465                                         cp = ts->nodeclass_vector; break;
466                                 case TH_NODE_TEXTURE:
467                                         cp = ts->nodeclass_texture; break;
468                                 case TH_NODE_PATTERN:
469                                         cp = ts->nodeclass_pattern; break;
470                                 case TH_NODE_SCRIPT:
471                                         cp = ts->nodeclass_script; break;
472                                 case TH_NODE_LAYOUT:
473                                         cp = ts->nodeclass_layout; break;
474                                 case TH_NODE_SHADER:
475                                         cp = ts->nodeclass_shader; break;
476                                 case TH_NODE_CONVERTOR:
477                                         cp = ts->syntaxv; break;
478                                 case TH_NODE_GROUP:
479                                         cp = ts->syntaxc; break;
480                                 case TH_NODE_INTERFACE:
481                                         cp = ts->console_output; break;
482                                 case TH_NODE_FRAME:
483                                         cp = ts->movie; break;
484                                 case TH_NODE_MATTE:
485                                         cp = ts->syntaxs; break;
486                                 case TH_NODE_DISTORT:
487                                         cp = ts->syntaxd; break;
488                                 case TH_NODE_CURVING:
489                                         cp = &ts->noodle_curving; break;
490
491                                 case TH_SEQ_MOVIE:
492                                         cp = ts->movie; break;
493                                 case TH_SEQ_MOVIECLIP:
494                                         cp = ts->movieclip; break;
495                                 case TH_SEQ_MASK:
496                                         cp = ts->mask; break;
497                                 case TH_SEQ_IMAGE:
498                                         cp = ts->image; break;
499                                 case TH_SEQ_SCENE:
500                                         cp = ts->scene; break;
501                                 case TH_SEQ_AUDIO:
502                                         cp = ts->audio; break;
503                                 case TH_SEQ_EFFECT:
504                                         cp = ts->effect; break;
505                                 case TH_SEQ_TRANSITION:
506                                         cp = ts->transition; break;
507                                 case TH_SEQ_META:
508                                         cp = ts->meta; break;
509                                 case TH_SEQ_TEXT:
510                                         cp = ts->text_strip; break;
511                                 case TH_SEQ_PREVIEW:
512                                         cp = ts->preview_back; break;
513
514                                 case TH_CONSOLE_OUTPUT:
515                                         cp = ts->console_output; break;
516                                 case TH_CONSOLE_INPUT:
517                                         cp = ts->console_input; break;
518                                 case TH_CONSOLE_INFO:
519                                         cp = ts->console_info; break;
520                                 case TH_CONSOLE_ERROR:
521                                         cp = ts->console_error; break;
522                                 case TH_CONSOLE_CURSOR:
523                                         cp = ts->console_cursor; break;
524                                 case TH_CONSOLE_SELECT:
525                                         cp = ts->console_select; break;
526
527                                 case TH_HANDLE_VERTEX:
528                                         cp = ts->handle_vertex;
529                                         break;
530                                 case TH_HANDLE_VERTEX_SELECT:
531                                         cp = ts->handle_vertex_select;
532                                         break;
533                                 case TH_HANDLE_VERTEX_SIZE:
534                                         cp = &ts->handle_vertex_size;
535                                         break;
536
537                                 case TH_GP_VERTEX:
538                                         cp = ts->gp_vertex;
539                                         break;
540                                 case TH_GP_VERTEX_SELECT:
541                                         cp = ts->gp_vertex_select;
542                                         break;
543                                 case TH_GP_VERTEX_SIZE:
544                                         cp = &ts->gp_vertex_size;
545                                         break;
546
547                                 case TH_DOPESHEET_CHANNELOB:
548                                         cp = ts->ds_channel;
549                                         break;
550                                 case TH_DOPESHEET_CHANNELSUBOB:
551                                         cp = ts->ds_subchannel;
552                                         break;
553                                 case TH_DOPESHEET_IPOLINE:
554                                         cp = ts->ds_ipoline;
555                                         break;
556
557                                 case TH_PREVIEW_BACK:
558                                         cp = ts->preview_back;
559                                         break;
560
561                                 case TH_STITCH_PREVIEW_FACE:
562                                         cp = ts->preview_stitch_face;
563                                         break;
564
565                                 case TH_STITCH_PREVIEW_EDGE:
566                                         cp = ts->preview_stitch_edge;
567                                         break;
568
569                                 case TH_STITCH_PREVIEW_VERT:
570                                         cp = ts->preview_stitch_vert;
571                                         break;
572
573                                 case TH_STITCH_PREVIEW_STITCHABLE:
574                                         cp = ts->preview_stitch_stitchable;
575                                         break;
576
577                                 case TH_STITCH_PREVIEW_UNSTITCHABLE:
578                                         cp = ts->preview_stitch_unstitchable;
579                                         break;
580                                 case TH_STITCH_PREVIEW_ACTIVE:
581                                         cp = ts->preview_stitch_active;
582                                         break;
583
584                                 case TH_PAINT_CURVE_HANDLE:
585                                         cp = ts->paint_curve_handle;
586                                         break;
587                                 case TH_PAINT_CURVE_PIVOT:
588                                         cp = ts->paint_curve_pivot;
589                                         break;
590
591                                 case TH_METADATA_BG:
592                                         cp = ts->metadatabg;
593                                         break;
594                                 case TH_METADATA_TEXT:
595                                         cp = ts->metadatatext;
596                                         break;
597
598                                 case TH_UV_OTHERS:
599                                         cp = ts->uv_others;
600                                         break;
601                                 case TH_UV_SHADOW:
602                                         cp = ts->uv_shadow;
603                                         break;
604
605                                 case TH_MARKER_OUTLINE:
606                                         cp = ts->marker_outline; break;
607                                 case TH_MARKER:
608                                         cp = ts->marker; break;
609                                 case TH_ACT_MARKER:
610                                         cp = ts->act_marker; break;
611                                 case TH_SEL_MARKER:
612                                         cp = ts->sel_marker; break;
613                                 case TH_BUNDLE_SOLID:
614                                         cp = ts->bundle_solid; break;
615                                 case TH_DIS_MARKER:
616                                         cp = ts->dis_marker; break;
617                                 case TH_PATH_BEFORE:
618                                         cp = ts->path_before; break;
619                                 case TH_PATH_AFTER:
620                                         cp = ts->path_after; break;
621                                 case TH_CAMERA_PATH:
622                                         cp = ts->camera_path; break;
623                                 case TH_LOCK_MARKER:
624                                         cp = ts->lock_marker; break;
625
626                                 case TH_MATCH:
627                                         cp = ts->match;
628                                         break;
629
630                                 case TH_SELECT_HIGHLIGHT:
631                                         cp = ts->selected_highlight;
632                                         break;
633
634                                 case TH_SKIN_ROOT:
635                                         cp = ts->skin_root;
636                                         break;
637
638                                 case TH_ANIM_ACTIVE:
639                                         cp = ts->anim_active;
640                                         break;
641                                 case TH_ANIM_INACTIVE:
642                                         cp = ts->anim_non_active;
643                                         break;
644                                 case TH_ANIM_PREVIEW_RANGE:
645                                         cp = ts->anim_preview_range;
646                                         break;
647
648                                 case TH_NLA_TWEAK:
649                                         cp = ts->nla_tweaking;
650                                         break;
651                                 case TH_NLA_TWEAK_DUPLI:
652                                         cp = ts->nla_tweakdupli;
653                                         break;
654
655                                 case TH_NLA_TRANSITION:
656                                         cp = ts->nla_transition;
657                                         break;
658                                 case TH_NLA_TRANSITION_SEL:
659                                         cp = ts->nla_transition_sel;
660                                         break;
661                                 case TH_NLA_META:
662                                         cp = ts->nla_meta;
663                                         break;
664                                 case TH_NLA_META_SEL:
665                                         cp = ts->nla_meta_sel;
666                                         break;
667                                 case TH_NLA_SOUND:
668                                         cp = ts->nla_sound;
669                                         break;
670                                 case TH_NLA_SOUND_SEL:
671                                         cp = ts->nla_sound_sel;
672                                         break;
673
674                                 case TH_WIDGET_EMBOSS:
675                                         cp = btheme->tui.widget_emboss; break;
676
677                                 case TH_EDITOR_OUTLINE:
678                                         cp = btheme->tui.editor_outline;
679                                         break;
680                                 case TH_AXIS_X:
681                                         cp = btheme->tui.xaxis; break;
682                                 case TH_AXIS_Y:
683                                         cp = btheme->tui.yaxis; break;
684                                 case TH_AXIS_Z:
685                                         cp = btheme->tui.zaxis; break;
686
687                                 case TH_GIZMO_HI:
688                                         cp = btheme->tui.gizmo_hi; break;
689                                 case TH_GIZMO_PRIMARY:
690                                         cp = btheme->tui.gizmo_primary; break;
691                                 case TH_GIZMO_SECONDARY:
692                                         cp = btheme->tui.gizmo_secondary; break;
693                                 case TH_GIZMO_A:
694                                         cp = btheme->tui.gizmo_a; break;
695                                 case TH_GIZMO_B:
696                                         cp = btheme->tui.gizmo_b; break;
697
698                                 case TH_ICON_COLLECTION:
699                                         cp = btheme->tui.icon_collection; break;
700                                 case TH_ICON_OBJECT:
701                                         cp = btheme->tui.icon_object; break;
702                                 case TH_ICON_OBJECT_DATA:
703                                         cp = btheme->tui.icon_object_data; break;
704                                 case TH_ICON_MODIFIER:
705                                         cp = btheme->tui.icon_modifier; break;
706                                 case TH_ICON_SHADING:
707                                         cp = btheme->tui.icon_shading; break;
708
709                                 case TH_INFO_SELECTED:
710                                         cp = ts->info_selected;
711                                         break;
712                                 case TH_INFO_SELECTED_TEXT:
713                                         cp = ts->info_selected_text;
714                                         break;
715                                 case TH_INFO_ERROR:
716                                         cp = ts->info_error;
717                                         break;
718                                 case TH_INFO_ERROR_TEXT:
719                                         cp = ts->info_error_text;
720                                         break;
721                                 case TH_INFO_WARNING:
722                                         cp = ts->info_warning;
723                                         break;
724                                 case TH_INFO_WARNING_TEXT:
725                                         cp = ts->info_warning_text;
726                                         break;
727                                 case TH_INFO_INFO:
728                                         cp = ts->info_info;
729                                         break;
730                                 case TH_INFO_INFO_TEXT:
731                                         cp = ts->info_info_text;
732                                         break;
733                                 case TH_INFO_DEBUG:
734                                         cp = ts->info_debug;
735                                         break;
736                                 case TH_INFO_DEBUG_TEXT:
737                                         cp = ts->info_debug_text;
738                                         break;
739                                 case TH_V3D_CLIPPING_BORDER:
740                                         cp = ts->clipping_border_3d;
741                                         break;
742                         }
743                 }
744         }
745
746         return (const uchar *)cp;
747 }
748
749 /**
750  * initialize default theme
751  * \note: when you add new colors, created & saved themes need initialized
752  * use function below, init_userdef_do_versions()
753  */
754 void ui_theme_init_default(void)
755 {
756
757         /* we search for the theme with name Default */
758         bTheme *btheme = BLI_findstring(&U.themes, "Default", offsetof(bTheme, name));
759         if (btheme == NULL) {
760                 btheme = MEM_callocN(sizeof(bTheme), __func__);
761                 BLI_addtail(&U.themes, btheme);
762         }
763
764         UI_SetTheme(0, 0);  /* make sure the global used in this file is set */
765
766         const int active_theme_area = btheme->active_theme_area;
767         memcpy(btheme, &U_theme_default, sizeof(*btheme));
768         btheme->active_theme_area = active_theme_area;
769 }
770
771 void ui_style_init_default(void)
772 {
773         BLI_freelistN(&U.uistyles);
774         /* gets automatically re-allocated */
775         uiStyleInit();
776 }
777
778
779 void UI_SetTheme(int spacetype, int regionid)
780 {
781         if (spacetype) {
782                 /* later on, a local theme can be found too */
783                 theme_active = U.themes.first;
784                 theme_spacetype = spacetype;
785                 theme_regionid = regionid;
786         }
787         else if (regionid) {
788                 /* popups */
789                 theme_active = U.themes.first;
790                 theme_spacetype = SPACE_BUTS;
791                 theme_regionid = regionid;
792         }
793         else {
794                 /* for safety, when theme was deleted */
795                 theme_active = U.themes.first;
796                 theme_spacetype = SPACE_VIEW3D;
797                 theme_regionid = RGN_TYPE_WINDOW;
798         }
799 }
800
801 bTheme *UI_GetTheme(void)
802 {
803         return U.themes.first;
804 }
805
806 /**
807  * for the rare case we need to temp swap in a different theme (offscreen render)
808  */
809 void UI_Theme_Store(struct bThemeState *theme_state)
810 {
811         *theme_state = g_theme_state;
812 }
813 void UI_Theme_Restore(struct bThemeState *theme_state)
814 {
815         g_theme_state = *theme_state;
816 }
817
818 void UI_GetThemeColorShadeAlpha4ubv(int colorid, int coloffset, int alphaoffset, uchar col[4])
819 {
820         int r, g, b, a;
821         const uchar *cp;
822
823         cp = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid);
824         r = coloffset + (int) cp[0];
825         CLAMP(r, 0, 255);
826         g = coloffset + (int) cp[1];
827         CLAMP(g, 0, 255);
828         b = coloffset + (int) cp[2];
829         CLAMP(b, 0, 255);
830         a = alphaoffset + (int) cp[3];
831         CLAMP(a, 0, 255);
832
833         col[0] = r;
834         col[1] = g;
835         col[2] = b;
836         col[3] = a;
837 }
838
839 void UI_GetThemeColorBlend3ubv(int colorid1, int colorid2, float fac, uchar col[3])
840 {
841         const uchar *cp1, *cp2;
842
843         cp1 = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid1);
844         cp2 = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid2);
845
846         CLAMP(fac, 0.0f, 1.0f);
847         col[0] = floorf((1.0f - fac) * cp1[0] + fac * cp2[0]);
848         col[1] = floorf((1.0f - fac) * cp1[1] + fac * cp2[1]);
849         col[2] = floorf((1.0f - fac) * cp1[2] + fac * cp2[2]);
850 }
851
852 void UI_GetThemeColorBlend3f(int colorid1, int colorid2, float fac, float r_col[3])
853 {
854         const uchar *cp1, *cp2;
855
856         cp1 = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid1);
857         cp2 = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid2);
858
859         CLAMP(fac, 0.0f, 1.0f);
860         r_col[0] = ((1.0f - fac) * cp1[0] + fac * cp2[0]) / 255.0f;
861         r_col[1] = ((1.0f - fac) * cp1[1] + fac * cp2[1]) / 255.0f;
862         r_col[2] = ((1.0f - fac) * cp1[2] + fac * cp2[2]) / 255.0f;
863 }
864
865 void UI_FontThemeColor(int fontid, int colorid)
866 {
867         uchar color[4];
868         UI_GetThemeColor4ubv(colorid, color);
869         BLF_color4ubv(fontid, color);
870 }
871
872 /* get individual values, not scaled */
873 float UI_GetThemeValuef(int colorid)
874 {
875         const uchar *cp;
876
877         cp = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid);
878         return ((float)cp[0]);
879 }
880
881 /* get individual values, not scaled */
882 int UI_GetThemeValue(int colorid)
883 {
884         const uchar *cp;
885
886         cp = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid);
887         return ((int) cp[0]);
888 }
889
890 /* versions of the function above, which take a space-type */
891 float UI_GetThemeValueTypef(int colorid, int spacetype)
892 {
893         const uchar *cp;
894
895         cp = UI_ThemeGetColorPtr(theme_active, spacetype, colorid);
896         return ((float)cp[0]);
897 }
898
899 int UI_GetThemeValueType(int colorid, int spacetype)
900 {
901         const uchar *cp;
902
903         cp = UI_ThemeGetColorPtr(theme_active, spacetype, colorid);
904         return ((int)cp[0]);
905 }
906
907
908 /* get the color, range 0.0-1.0 */
909 void UI_GetThemeColor3fv(int colorid, float col[3])
910 {
911         const uchar *cp;
912
913         cp = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid);
914         col[0] = ((float)cp[0]) / 255.0f;
915         col[1] = ((float)cp[1]) / 255.0f;
916         col[2] = ((float)cp[2]) / 255.0f;
917 }
918
919 void UI_GetThemeColor4fv(int colorid, float col[4])
920 {
921         const uchar *cp;
922
923         cp = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid);
924         col[0] = ((float)cp[0]) / 255.0f;
925         col[1] = ((float)cp[1]) / 255.0f;
926         col[2] = ((float)cp[2]) / 255.0f;
927         col[3] = ((float)cp[3]) / 255.0f;
928 }
929
930 /* get the color, range 0.0-1.0, complete with shading offset */
931 void UI_GetThemeColorShade3fv(int colorid, int offset, float col[3])
932 {
933         int r, g, b;
934         const uchar *cp;
935
936         cp = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid);
937
938         r = offset + (int) cp[0];
939         CLAMP(r, 0, 255);
940         g = offset + (int) cp[1];
941         CLAMP(g, 0, 255);
942         b = offset + (int) cp[2];
943         CLAMP(b, 0, 255);
944
945         col[0] = ((float)r) / 255.0f;
946         col[1] = ((float)g) / 255.0f;
947         col[2] = ((float)b) / 255.0f;
948 }
949
950 void UI_GetThemeColorShade3ubv(int colorid, int offset, uchar col[3])
951 {
952         int r, g, b;
953         const uchar *cp;
954
955         cp = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid);
956
957         r = offset + (int) cp[0];
958         CLAMP(r, 0, 255);
959         g = offset + (int) cp[1];
960         CLAMP(g, 0, 255);
961         b = offset + (int) cp[2];
962         CLAMP(b, 0, 255);
963
964         col[0] = r;
965         col[1] = g;
966         col[2] = b;
967 }
968
969 void UI_GetThemeColorBlendShade3ubv(int colorid1, int colorid2, float fac, int offset, uchar col[3])
970 {
971         const uchar *cp1, *cp2;
972
973         cp1 = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid1);
974         cp2 = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid2);
975
976         CLAMP(fac, 0.0f, 1.0f);
977
978         float blend[3];
979         blend[0] = offset + floorf((1.0f - fac) * cp1[0] + fac * cp2[0]);
980         blend[1] = offset + floorf((1.0f - fac) * cp1[1] + fac * cp2[1]);
981         blend[2] = offset + floorf((1.0f - fac) * cp1[2] + fac * cp2[2]);
982
983         unit_float_to_uchar_clamp_v3(col, blend);
984 }
985
986 void UI_GetThemeColorShade4ubv(int colorid, int offset, uchar col[4])
987 {
988         int r, g, b;
989         const uchar *cp;
990
991         cp = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid);
992         r = offset + (int) cp[0];
993         CLAMP(r, 0, 255);
994         g = offset + (int) cp[1];
995         CLAMP(g, 0, 255);
996         b = offset + (int) cp[2];
997         CLAMP(b, 0, 255);
998
999         col[0] = r;
1000         col[1] = g;
1001         col[2] = b;
1002         col[3] = cp[3];
1003 }
1004
1005 void UI_GetThemeColorShadeAlpha4fv(int colorid, int coloffset, int alphaoffset, float col[4])
1006 {
1007         int r, g, b, a;
1008         const uchar *cp;
1009
1010         cp = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid);
1011
1012         r = coloffset + (int) cp[0];
1013         CLAMP(r, 0, 255);
1014         g = coloffset + (int) cp[1];
1015         CLAMP(g, 0, 255);
1016         b = coloffset + (int) cp[2];
1017         CLAMP(b, 0, 255);
1018         a = alphaoffset + (int) cp[3];
1019         CLAMP(b, 0, 255);
1020
1021         col[0] = ((float)r) / 255.0f;
1022         col[1] = ((float)g) / 255.0f;
1023         col[2] = ((float)b) / 255.0f;
1024         col[3] = ((float)a) / 255.0f;
1025 }
1026
1027 void UI_GetThemeColorBlendShade3fv(int colorid1, int colorid2, float fac, int offset, float col[3])
1028 {
1029         int r, g, b;
1030         const uchar *cp1, *cp2;
1031
1032         cp1 = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid1);
1033         cp2 = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid2);
1034
1035         CLAMP(fac, 0.0f, 1.0f);
1036
1037         r = offset + floorf((1.0f - fac) * cp1[0] + fac * cp2[0]);
1038         CLAMP(r, 0, 255);
1039         g = offset + floorf((1.0f - fac) * cp1[1] + fac * cp2[1]);
1040         CLAMP(g, 0, 255);
1041         b = offset + floorf((1.0f - fac) * cp1[2] + fac * cp2[2]);
1042         CLAMP(b, 0, 255);
1043
1044         col[0] = ((float)r) / 255.0f;
1045         col[1] = ((float)g) / 255.0f;
1046         col[2] = ((float)b) / 255.0f;
1047 }
1048
1049 void UI_GetThemeColorBlendShade4fv(int colorid1, int colorid2, float fac, int offset, float col[4])
1050 {
1051         int r, g, b, a;
1052         const uchar *cp1, *cp2;
1053
1054         cp1 = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid1);
1055         cp2 = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid2);
1056
1057         CLAMP(fac, 0.0f, 1.0f);
1058
1059         r = offset + floorf((1.0f - fac) * cp1[0] + fac * cp2[0]);
1060         CLAMP(r, 0, 255);
1061         g = offset + floorf((1.0f - fac) * cp1[1] + fac * cp2[1]);
1062         CLAMP(g, 0, 255);
1063         b = offset + floorf((1.0f - fac) * cp1[2] + fac * cp2[2]);
1064         CLAMP(b, 0, 255);
1065         a = offset + floorf((1.0f - fac) * cp1[3] + fac * cp2[3]);
1066         CLAMP(a, 0, 255);
1067
1068         col[0] = ((float)r) / 255.0f;
1069         col[1] = ((float)g) / 255.0f;
1070         col[2] = ((float)b) / 255.0f;
1071         col[3] = ((float)a) / 255.0f;
1072 }
1073
1074 /* get the color, in char pointer */
1075 void UI_GetThemeColor3ubv(int colorid, uchar col[3])
1076 {
1077         const uchar *cp;
1078
1079         cp = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid);
1080         col[0] = cp[0];
1081         col[1] = cp[1];
1082         col[2] = cp[2];
1083 }
1084
1085 /* get the color, range 0.0-1.0, complete with shading offset */
1086 void UI_GetThemeColorShade4fv(int colorid, int offset, float col[4])
1087 {
1088         int r, g, b, a;
1089         const uchar *cp;
1090
1091         cp = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid);
1092
1093         r = offset + (int) cp[0];
1094         CLAMP(r, 0, 255);
1095         g = offset + (int) cp[1];
1096         CLAMP(g, 0, 255);
1097         b = offset + (int) cp[2];
1098         CLAMP(b, 0, 255);
1099
1100         a = (int) cp[3]; /* no shading offset... */
1101         CLAMP(a, 0, 255);
1102
1103         col[0] = ((float)r) / 255.0f;
1104         col[1] = ((float)g) / 255.0f;
1105         col[2] = ((float)b) / 255.0f;
1106         col[3] = ((float)a) / 255.0f;
1107 }
1108
1109 /* get the color, in char pointer */
1110 void UI_GetThemeColor4ubv(int colorid, uchar col[4])
1111 {
1112         const uchar *cp;
1113
1114         cp = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid);
1115         col[0] = cp[0];
1116         col[1] = cp[1];
1117         col[2] = cp[2];
1118         col[3] = cp[3];
1119 }
1120
1121 void UI_GetThemeColorType3ubv(int colorid, int spacetype, uchar col[3])
1122 {
1123         const uchar *cp;
1124
1125         cp = UI_ThemeGetColorPtr(theme_active, spacetype, colorid);
1126         col[0] = cp[0];
1127         col[1] = cp[1];
1128         col[2] = cp[2];
1129 }
1130
1131 void UI_GetThemeColorType4ubv(int colorid, int spacetype, uchar col[4])
1132 {
1133         const uchar *cp;
1134
1135         cp = UI_ThemeGetColorPtr(theme_active, spacetype, colorid);
1136         col[0] = cp[0];
1137         col[1] = cp[1];
1138         col[2] = cp[2];
1139         col[3] = cp[3];
1140 }
1141
1142 bool UI_GetIconThemeColor4fv(int colorid, float col[4])
1143 {
1144         if (colorid == 0) {
1145                 return false;
1146         }
1147
1148         /* Only colored icons in outliner and popups, overall UI is intended
1149          * to stay monochrome and out of the way except a few places where it
1150          * is important to communicate different data types. */
1151         if (!((theme_spacetype == SPACE_OUTLINER) ||
1152               (theme_regionid == RGN_TYPE_TEMPORARY)))
1153         {
1154                 return false;
1155         }
1156
1157         const uchar *cp;
1158         cp = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid);
1159         col[0] = ((float)cp[0]) / 255.0f;
1160         col[1] = ((float)cp[1]) / 255.0f;
1161         col[2] = ((float)cp[2]) / 255.0f;
1162         col[3] = ((float)cp[3]) / 255.0f;
1163
1164         return true;
1165 }
1166
1167 void UI_GetColorPtrShade3ubv(const uchar cp[3], uchar col[3], int offset)
1168 {
1169         int r, g, b;
1170
1171         r = offset + (int)cp[0];
1172         g = offset + (int)cp[1];
1173         b = offset + (int)cp[2];
1174
1175         CLAMP(r, 0, 255);
1176         CLAMP(g, 0, 255);
1177         CLAMP(b, 0, 255);
1178
1179         col[0] = r;
1180         col[1] = g;
1181         col[2] = b;
1182 }
1183
1184 /* get a 3 byte color, blended and shaded between two other char color pointers */
1185 void UI_GetColorPtrBlendShade3ubv(
1186         const uchar cp1[3], const uchar cp2[3], uchar col[3],
1187         float fac, int offset)
1188 {
1189         int r, g, b;
1190
1191         CLAMP(fac, 0.0f, 1.0f);
1192         r = offset + floor((1.0f - fac) * cp1[0] + fac * cp2[0]);
1193         g = offset + floor((1.0f - fac) * cp1[1] + fac * cp2[1]);
1194         b = offset + floor((1.0f - fac) * cp1[2] + fac * cp2[2]);
1195
1196         CLAMP(r, 0, 255);
1197         CLAMP(g, 0, 255);
1198         CLAMP(b, 0, 255);
1199
1200         col[0] = r;
1201         col[1] = g;
1202         col[2] = b;
1203 }
1204
1205 void UI_ThemeClearColor(int colorid)
1206 {
1207         float col[3];
1208
1209         UI_GetThemeColor3fv(colorid, col);
1210         GPU_clear_color(col[0], col[1], col[2], 0.0f);
1211 }
1212
1213 void UI_ThemeClearColorAlpha(int colorid, float alpha)
1214 {
1215         float col[3];
1216         UI_GetThemeColor3fv(colorid, col);
1217         GPU_clear_color(col[0], col[1], col[2], alpha);
1218 }
1219
1220
1221 int UI_ThemeMenuShadowWidth(void)
1222 {
1223         bTheme *btheme = UI_GetTheme();
1224         return (int)(btheme->tui.menu_shadow_width * UI_DPI_FAC);
1225 }
1226
1227 void UI_make_axis_color(const uchar src_col[3], uchar dst_col[3], const char axis)
1228 {
1229         uchar col[3];
1230
1231         switch (axis) {
1232                 case 'X':
1233                         UI_GetThemeColor3ubv(TH_AXIS_X, col);
1234                         UI_GetColorPtrBlendShade3ubv(src_col, col, dst_col, 0.5f, -10);
1235                         break;
1236                 case 'Y':
1237                         UI_GetThemeColor3ubv(TH_AXIS_Y, col);
1238                         UI_GetColorPtrBlendShade3ubv(src_col, col, dst_col, 0.5f, -10);
1239                         break;
1240                 case 'Z':
1241                         UI_GetThemeColor3ubv(TH_AXIS_Z, col);
1242                         UI_GetColorPtrBlendShade3ubv(src_col, col, dst_col, 0.5f, -10);
1243                         break;
1244                 default:
1245                         BLI_assert(0);
1246                         break;
1247         }
1248 }
1249
1250 /* patching UserDef struct and Themes */
1251 void init_userdef_do_versions(Main *bmain)
1252 {
1253         BLO_version_defaults_userpref_blend(bmain, &U);
1254
1255         if (STREQ(U.tempdir, "/")) {
1256                 BKE_tempdir_system_init(U.tempdir);
1257         }
1258
1259         /* Not versioning, just avoid errors. */
1260 #ifndef WITH_CYCLES
1261         BKE_addon_remove_safe(&U.addons, "cycles");
1262 #endif
1263
1264 }