Merge branch 'master' into blender2.8
[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_curve_types.h"
39 #include "DNA_screen_types.h"
40 #include "DNA_space_types.h"
41 #include "DNA_userdef_types.h"
42 #include "DNA_windowmanager_types.h"
43
44 #include "BLI_blenlib.h"
45 #include "BLI_utildefines.h"
46 #include "BLI_math.h"
47
48 #include "BKE_addon.h"
49 #include "BKE_appdir.h"
50 #include "BKE_colorband.h"
51 #include "BKE_global.h"
52 #include "BKE_main.h"
53 #include "BKE_mesh_runtime.h"
54
55 #include "BIF_gl.h"
56
57 #include "BLF_api.h"
58
59 #include "ED_screen.h"
60
61 #include "UI_interface.h"
62 #include "UI_interface_icons.h"
63
64 #include "interface_intern.h"
65 #include "GPU_framebuffer.h"
66
67
68 extern const bTheme U_theme_default;
69
70 /* global for themes */
71 typedef void (*VectorDrawFunc)(int x, int y, int w, int h, float alpha);
72
73 /* be sure to keep 'bThemeState' in sync */
74 static struct bThemeState g_theme_state = {
75     NULL,
76     SPACE_VIEW3D,
77     RGN_TYPE_WINDOW,
78 };
79
80 #define theme_active g_theme_state.theme
81 #define theme_spacetype g_theme_state.spacetype
82 #define theme_regionid g_theme_state.regionid
83
84 void ui_resources_init(void)
85 {
86         UI_icons_init(BIFICONID_LAST);
87 }
88
89 void ui_resources_free(void)
90 {
91         UI_icons_free();
92 }
93
94
95 /* ******************************************************** */
96 /*    THEMES */
97 /* ******************************************************** */
98
99 const unsigned char *UI_ThemeGetColorPtr(bTheme *btheme, int spacetype, int colorid)
100 {
101         ThemeSpace *ts = NULL;
102         static char error[4] = {240, 0, 240, 255};
103         static char alert[4] = {240, 60, 60, 255};
104         static char headerdesel[4] = {0, 0, 0, 255};
105         static char back[4] = {0, 0, 0, 255};
106         static char setting = 0;
107         const char *cp = error;
108
109         /* ensure we're not getting a color after running BKE_blender_userdef_free */
110         BLI_assert(BLI_findindex(&U.themes, theme_active) != -1);
111         BLI_assert(colorid != TH_UNDEFINED);
112
113         if (btheme) {
114
115                 /* first check for ui buttons theme */
116                 if (colorid < TH_THEMEUI) {
117
118                         switch (colorid) {
119
120                                 case TH_REDALERT:
121                                         cp = alert; break;
122                         }
123                 }
124                 else {
125
126                         switch (spacetype) {
127                                 case SPACE_BUTS:
128                                         ts = &btheme->tbuts;
129                                         break;
130                                 case SPACE_VIEW3D:
131                                         ts = &btheme->tv3d;
132                                         break;
133                                 case SPACE_IPO:
134                                         ts = &btheme->tipo;
135                                         break;
136                                 case SPACE_FILE:
137                                         ts = &btheme->tfile;
138                                         break;
139                                 case SPACE_NLA:
140                                         ts = &btheme->tnla;
141                                         break;
142                                 case SPACE_ACTION:
143                                         ts = &btheme->tact;
144                                         break;
145                                 case SPACE_SEQ:
146                                         ts = &btheme->tseq;
147                                         break;
148                                 case SPACE_IMAGE:
149                                         ts = &btheme->tima;
150                                         break;
151                                 case SPACE_TEXT:
152                                         ts = &btheme->text;
153                                         break;
154                                 case SPACE_OUTLINER:
155                                         ts = &btheme->toops;
156                                         break;
157                                 case SPACE_INFO:
158                                         ts = &btheme->tinfo;
159                                         break;
160                                 case SPACE_USERPREF:
161                                         ts = &btheme->tuserpref;
162                                         break;
163                                 case SPACE_CONSOLE:
164                                         ts = &btheme->tconsole;
165                                         break;
166                                 case SPACE_NODE:
167                                         ts = &btheme->tnode;
168                                         break;
169                                 case SPACE_CLIP:
170                                         ts = &btheme->tclip;
171                                         break;
172                                 case SPACE_TOPBAR:
173                                         ts = &btheme->ttopbar;
174                                         break;
175                                 case SPACE_STATUSBAR:
176                                         ts = &btheme->tstatusbar;
177                                         break;
178                                 default:
179                                         ts = &btheme->tv3d;
180                                         break;
181                         }
182
183                         switch (colorid) {
184                                 case TH_BACK:
185                                         if (ELEM(theme_regionid, RGN_TYPE_WINDOW, RGN_TYPE_PREVIEW))
186                                                 cp = ts->back;
187                                         else if (theme_regionid == RGN_TYPE_CHANNELS)
188                                                 cp = ts->list;
189                                         else if (theme_regionid == RGN_TYPE_HEADER)
190                                                 cp = ts->header;
191                                         else
192                                                 cp = ts->button;
193
194                                         copy_v4_v4_char(back, cp);
195                                         if (!ED_region_is_overlap(spacetype, theme_regionid)) {
196                                                 back[3] = 255;
197                                         }
198                                         cp = back;
199                                         break;
200                                 case TH_LOW_GRAD:
201                                         cp = ts->gradients.gradient;
202                                         break;
203                                 case TH_HIGH_GRAD:
204                                         cp = ts->gradients.high_gradient;
205                                         break;
206                                 case TH_SHOW_BACK_GRAD:
207                                         cp = &setting;
208                                         setting = ts->gradients.show_grad;
209                                         break;
210                                 case TH_TEXT:
211                                         if (theme_regionid == RGN_TYPE_WINDOW)
212                                                 cp = ts->text;
213                                         else if (theme_regionid == RGN_TYPE_CHANNELS)
214                                                 cp = ts->list_text;
215                                         else if (theme_regionid == RGN_TYPE_HEADER)
216                                                 cp = ts->header_text;
217                                         else
218                                                 cp = ts->button_text;
219                                         break;
220                                 case TH_TEXT_HI:
221                                         if (theme_regionid == RGN_TYPE_WINDOW)
222                                                 cp = ts->text_hi;
223                                         else if (theme_regionid == RGN_TYPE_CHANNELS)
224                                                 cp = ts->list_text_hi;
225                                         else if (theme_regionid == RGN_TYPE_HEADER)
226                                                 cp = ts->header_text_hi;
227                                         else
228                                                 cp = ts->button_text_hi;
229                                         break;
230                                 case TH_TITLE:
231                                         if (theme_regionid == RGN_TYPE_WINDOW)
232                                                 cp = ts->title;
233                                         else if (theme_regionid == RGN_TYPE_CHANNELS)
234                                                 cp = ts->list_title;
235                                         else if (theme_regionid == RGN_TYPE_HEADER)
236                                                 cp = ts->header_title;
237                                         else
238                                                 cp = ts->button_title;
239                                         break;
240
241                                 case TH_HEADER:
242                                         cp = ts->header; break;
243                                 case TH_HEADERDESEL:
244                                         /* we calculate a dynamic builtin header deselect color, also for pulldowns... */
245                                         cp = ts->header;
246                                         headerdesel[0] = cp[0] > 10 ? cp[0] - 10 : 0;
247                                         headerdesel[1] = cp[1] > 10 ? cp[1] - 10 : 0;
248                                         headerdesel[2] = cp[2] > 10 ? cp[2] - 10 : 0;
249                                         headerdesel[3] = cp[3];
250                                         cp = headerdesel;
251                                         break;
252                                 case TH_HEADER_TEXT:
253                                         cp = ts->header_text; break;
254                                 case TH_HEADER_TEXT_HI:
255                                         cp = ts->header_text_hi; break;
256
257                                 case TH_PANEL_HEADER:
258                                         cp = ts->panelcolors.header; break;
259                                 case TH_PANEL_BACK:
260                                         cp = ts->panelcolors.back; break;
261                                 case TH_PANEL_SUB_BACK:
262                                         cp = ts->panelcolors.sub_back; break;
263                                 case TH_PANEL_SHOW_HEADER:
264                                         cp = &setting;
265                                         setting = ts->panelcolors.show_header;
266                                         break;
267                                 case TH_PANEL_SHOW_BACK:
268                                         cp = &setting;
269                                         setting = ts->panelcolors.show_back;
270                                         break;
271
272                                 case TH_BUTBACK:
273                                         cp = ts->button; break;
274                                 case TH_BUTBACK_TEXT:
275                                         cp = ts->button_text; break;
276                                 case TH_BUTBACK_TEXT_HI:
277                                         cp = ts->button_text_hi; break;
278
279                                 case TH_TAB_ACTIVE:
280                                         cp = ts->tab_active; break;
281                                 case TH_TAB_INACTIVE:
282                                         cp = ts->tab_inactive; break;
283                                 case TH_TAB_BACK:
284                                         cp = ts->tab_back; break;
285                                 case TH_TAB_OUTLINE:
286                                         cp = ts->tab_outline; break;
287
288                                 case TH_SHADE1:
289                                         cp = ts->shade1; break;
290                                 case TH_SHADE2:
291                                         cp = ts->shade2; break;
292                                 case TH_HILITE:
293                                         cp = ts->hilite; break;
294
295                                 case TH_GRID:
296                                         cp = ts->grid; break;
297                                 case TH_VIEW_OVERLAY:
298                                         cp = ts->view_overlay; break;
299                                 case TH_WIRE:
300                                         cp = ts->wire; break;
301                                 case TH_WIRE_INNER:
302                                         cp = ts->syntaxr; break;
303                                 case TH_WIRE_EDIT:
304                                         cp = ts->wire_edit; break;
305                                 case TH_LAMP:
306                                         cp = ts->lamp; break;
307                                 case TH_SPEAKER:
308                                         cp = ts->speaker; break;
309                                 case TH_CAMERA:
310                                         cp = ts->camera; break;
311                                 case TH_EMPTY:
312                                         cp = ts->empty; break;
313                                 case TH_SELECT:
314                                         cp = ts->select; break;
315                                 case TH_ACTIVE:
316                                         cp = ts->active; break;
317                                 case TH_GROUP:
318                                         cp = ts->group; break;
319                                 case TH_GROUP_ACTIVE:
320                                         cp = ts->group_active; break;
321                                 case TH_TRANSFORM:
322                                         cp = ts->transform; break;
323                                 case TH_VERTEX:
324                                         cp = ts->vertex; break;
325                                 case TH_VERTEX_SELECT:
326                                         cp = ts->vertex_select; break;
327                                 case TH_VERTEX_BEVEL:
328                                         cp = ts->vertex_bevel; break;
329                                 case TH_VERTEX_UNREFERENCED:
330                                         cp = ts->vertex_unreferenced; break;
331                                 case TH_VERTEX_SIZE:
332                                         cp = &ts->vertex_size; break;
333                                 case TH_OUTLINE_WIDTH:
334                                         cp = &ts->outline_width; break;
335                                 case TH_EDGE:
336                                         cp = ts->edge; break;
337                                 case TH_EDGE_SELECT:
338                                         cp = ts->edge_select; break;
339                                 case TH_EDGE_SEAM:
340                                         cp = ts->edge_seam; break;
341                                 case TH_EDGE_SHARP:
342                                         cp = ts->edge_sharp; break;
343                                 case TH_EDGE_CREASE:
344                                         cp = ts->edge_crease; break;
345                                 case TH_EDGE_BEVEL:
346                                         cp = ts->edge_bevel; break;
347                                 case TH_EDITMESH_ACTIVE:
348                                         cp = ts->editmesh_active; break;
349                                 case TH_EDGE_FACESEL:
350                                         cp = ts->edge_facesel; break;
351                                 case TH_FACE:
352                                         cp = ts->face; break;
353                                 case TH_FACE_SELECT:
354                                         cp = ts->face_select; break;
355                                 case TH_FACE_DOT:
356                                         cp = ts->face_dot; break;
357                                 case TH_FACEDOT_SIZE:
358                                         cp = &ts->facedot_size; break;
359                                 case TH_DRAWEXTRA_EDGELEN:
360                                         cp = ts->extra_edge_len; break;
361                                 case TH_DRAWEXTRA_EDGEANG:
362                                         cp = ts->extra_edge_angle; break;
363                                 case TH_DRAWEXTRA_FACEAREA:
364                                         cp = ts->extra_face_area; break;
365                                 case TH_DRAWEXTRA_FACEANG:
366                                         cp = ts->extra_face_angle; break;
367                                 case TH_NORMAL:
368                                         cp = ts->normal; break;
369                                 case TH_VNORMAL:
370                                         cp = ts->vertex_normal; break;
371                                 case TH_LNORMAL:
372                                         cp = ts->loop_normal; break;
373                                 case TH_BONE_SOLID:
374                                         cp = ts->bone_solid; break;
375                                 case TH_BONE_POSE:
376                                         cp = ts->bone_pose; break;
377                                 case TH_BONE_POSE_ACTIVE:
378                                         cp = ts->bone_pose_active; break;
379                                 case TH_STRIP:
380                                         cp = ts->strip; break;
381                                 case TH_STRIP_SELECT:
382                                         cp = ts->strip_select; break;
383                                 case TH_KEYTYPE_KEYFRAME:
384                                         cp = ts->keytype_keyframe; break;
385                                 case TH_KEYTYPE_KEYFRAME_SELECT:
386                                         cp = ts->keytype_keyframe_select; break;
387                                 case TH_KEYTYPE_EXTREME:
388                                         cp = ts->keytype_extreme; break;
389                                 case TH_KEYTYPE_EXTREME_SELECT:
390                                         cp = ts->keytype_extreme_select; break;
391                                 case TH_KEYTYPE_BREAKDOWN:
392                                         cp = ts->keytype_breakdown; break;
393                                 case TH_KEYTYPE_BREAKDOWN_SELECT:
394                                         cp = ts->keytype_breakdown_select; break;
395                                 case TH_KEYTYPE_JITTER:
396                                         cp = ts->keytype_jitter; break;
397                                 case TH_KEYTYPE_JITTER_SELECT:
398                                         cp = ts->keytype_jitter_select; break;
399                                 case TH_KEYBORDER:
400                                         cp = ts->keyborder; break;
401                                 case TH_KEYBORDER_SELECT:
402                                         cp = ts->keyborder_select; break;
403                                 case TH_CFRAME:
404                                         cp = ts->cframe; break;
405                                 case TH_TIME_KEYFRAME:
406                                         cp = ts->time_keyframe; break;
407                                 case TH_TIME_GP_KEYFRAME:
408                                         cp = ts->time_gp_keyframe; break;
409                                 case TH_NURB_ULINE:
410                                         cp = ts->nurb_uline; break;
411                                 case TH_NURB_VLINE:
412                                         cp = ts->nurb_vline; break;
413                                 case TH_NURB_SEL_ULINE:
414                                         cp = ts->nurb_sel_uline; break;
415                                 case TH_NURB_SEL_VLINE:
416                                         cp = ts->nurb_sel_vline; break;
417                                 case TH_ACTIVE_SPLINE:
418                                         cp = ts->act_spline; break;
419                                 case TH_ACTIVE_VERT:
420                                         cp = ts->lastsel_point; break;
421                                 case TH_HANDLE_FREE:
422                                         cp = ts->handle_free; break;
423                                 case TH_HANDLE_AUTO:
424                                         cp = ts->handle_auto; break;
425                                 case TH_HANDLE_AUTOCLAMP:
426                                         cp = ts->handle_auto_clamped; break;
427                                 case TH_HANDLE_VECT:
428                                         cp = ts->handle_vect; break;
429                                 case TH_HANDLE_ALIGN:
430                                         cp = ts->handle_align; break;
431                                 case TH_HANDLE_SEL_FREE:
432                                         cp = ts->handle_sel_free; break;
433                                 case TH_HANDLE_SEL_AUTO:
434                                         cp = ts->handle_sel_auto; break;
435                                 case TH_HANDLE_SEL_AUTOCLAMP:
436                                         cp = ts->handle_sel_auto_clamped; break;
437                                 case TH_HANDLE_SEL_VECT:
438                                         cp = ts->handle_sel_vect; break;
439                                 case TH_HANDLE_SEL_ALIGN:
440                                         cp = ts->handle_sel_align; break;
441                                 case TH_FREESTYLE_EDGE_MARK:
442                                         cp = ts->freestyle_edge_mark; break;
443                                 case TH_FREESTYLE_FACE_MARK:
444                                         cp = ts->freestyle_face_mark; break;
445
446                                 case TH_SYNTAX_B:
447                                         cp = ts->syntaxb; break;
448                                 case TH_SYNTAX_V:
449                                         cp = ts->syntaxv; break;
450                                 case TH_SYNTAX_C:
451                                         cp = ts->syntaxc; break;
452                                 case TH_SYNTAX_L:
453                                         cp = ts->syntaxl; break;
454                                 case TH_SYNTAX_D:
455                                         cp = ts->syntaxd; break;
456                                 case TH_SYNTAX_R:
457                                         cp = ts->syntaxr; break;
458                                 case TH_SYNTAX_N:
459                                         cp = ts->syntaxn; break;
460                                 case TH_SYNTAX_S:
461                                         cp = ts->syntaxs; break;
462
463                                 case TH_NODE:
464                                         cp = ts->syntaxl; break;
465                                 case TH_NODE_INPUT:
466                                         cp = ts->syntaxn; break;
467                                 case TH_NODE_OUTPUT:
468                                         cp = ts->nodeclass_output; break;
469                                 case TH_NODE_COLOR:
470                                         cp = ts->syntaxb; break;
471                                 case TH_NODE_FILTER:
472                                         cp = ts->nodeclass_filter; break;
473                                 case TH_NODE_VECTOR:
474                                         cp = ts->nodeclass_vector; break;
475                                 case TH_NODE_TEXTURE:
476                                         cp = ts->nodeclass_texture; break;
477                                 case TH_NODE_PATTERN:
478                                         cp = ts->nodeclass_pattern; break;
479                                 case TH_NODE_SCRIPT:
480                                         cp = ts->nodeclass_script; break;
481                                 case TH_NODE_LAYOUT:
482                                         cp = ts->nodeclass_layout; break;
483                                 case TH_NODE_SHADER:
484                                         cp = ts->nodeclass_shader; break;
485                                 case TH_NODE_CONVERTOR:
486                                         cp = ts->syntaxv; break;
487                                 case TH_NODE_GROUP:
488                                         cp = ts->syntaxc; break;
489                                 case TH_NODE_INTERFACE:
490                                         cp = ts->console_output; break;
491                                 case TH_NODE_FRAME:
492                                         cp = ts->movie; break;
493                                 case TH_NODE_MATTE:
494                                         cp = ts->syntaxs; break;
495                                 case TH_NODE_DISTORT:
496                                         cp = ts->syntaxd; break;
497                                 case TH_NODE_CURVING:
498                                         cp = &ts->noodle_curving; break;
499
500                                 case TH_SEQ_MOVIE:
501                                         cp = ts->movie; break;
502                                 case TH_SEQ_MOVIECLIP:
503                                         cp = ts->movieclip; break;
504                                 case TH_SEQ_MASK:
505                                         cp = ts->mask; break;
506                                 case TH_SEQ_IMAGE:
507                                         cp = ts->image; break;
508                                 case TH_SEQ_SCENE:
509                                         cp = ts->scene; break;
510                                 case TH_SEQ_AUDIO:
511                                         cp = ts->audio; break;
512                                 case TH_SEQ_EFFECT:
513                                         cp = ts->effect; break;
514                                 case TH_SEQ_TRANSITION:
515                                         cp = ts->transition; break;
516                                 case TH_SEQ_META:
517                                         cp = ts->meta; break;
518                                 case TH_SEQ_TEXT:
519                                         cp = ts->text_strip; break;
520                                 case TH_SEQ_PREVIEW:
521                                         cp = ts->preview_back; break;
522
523                                 case TH_CONSOLE_OUTPUT:
524                                         cp = ts->console_output; break;
525                                 case TH_CONSOLE_INPUT:
526                                         cp = ts->console_input; break;
527                                 case TH_CONSOLE_INFO:
528                                         cp = ts->console_info; break;
529                                 case TH_CONSOLE_ERROR:
530                                         cp = ts->console_error; break;
531                                 case TH_CONSOLE_CURSOR:
532                                         cp = ts->console_cursor; break;
533                                 case TH_CONSOLE_SELECT:
534                                         cp = ts->console_select; break;
535
536                                 case TH_HANDLE_VERTEX:
537                                         cp = ts->handle_vertex;
538                                         break;
539                                 case TH_HANDLE_VERTEX_SELECT:
540                                         cp = ts->handle_vertex_select;
541                                         break;
542                                 case TH_HANDLE_VERTEX_SIZE:
543                                         cp = &ts->handle_vertex_size;
544                                         break;
545
546                                 case TH_GP_VERTEX:
547                                         cp = ts->gp_vertex;
548                                         break;
549                                 case TH_GP_VERTEX_SELECT:
550                                         cp = ts->gp_vertex_select;
551                                         break;
552                                 case TH_GP_VERTEX_SIZE:
553                                         cp = &ts->gp_vertex_size;
554                                         break;
555
556                                 case TH_DOPESHEET_CHANNELOB:
557                                         cp = ts->ds_channel;
558                                         break;
559                                 case TH_DOPESHEET_CHANNELSUBOB:
560                                         cp = ts->ds_subchannel;
561                                         break;
562
563                                 case TH_PREVIEW_BACK:
564                                         cp = ts->preview_back;
565                                         break;
566
567                                 case TH_STITCH_PREVIEW_FACE:
568                                         cp = ts->preview_stitch_face;
569                                         break;
570
571                                 case TH_STITCH_PREVIEW_EDGE:
572                                         cp = ts->preview_stitch_edge;
573                                         break;
574
575                                 case TH_STITCH_PREVIEW_VERT:
576                                         cp = ts->preview_stitch_vert;
577                                         break;
578
579                                 case TH_STITCH_PREVIEW_STITCHABLE:
580                                         cp = ts->preview_stitch_stitchable;
581                                         break;
582
583                                 case TH_STITCH_PREVIEW_UNSTITCHABLE:
584                                         cp = ts->preview_stitch_unstitchable;
585                                         break;
586                                 case TH_STITCH_PREVIEW_ACTIVE:
587                                         cp = ts->preview_stitch_active;
588                                         break;
589
590                                 case TH_PAINT_CURVE_HANDLE:
591                                         cp = ts->paint_curve_handle;
592                                         break;
593                                 case TH_PAINT_CURVE_PIVOT:
594                                         cp = ts->paint_curve_pivot;
595                                         break;
596
597                                 case TH_METADATA_BG:
598                                         cp = ts->metadatabg;
599                                         break;
600                                 case TH_METADATA_TEXT:
601                                         cp = ts->metadatatext;
602                                         break;
603
604                                 case TH_UV_OTHERS:
605                                         cp = ts->uv_others;
606                                         break;
607                                 case TH_UV_SHADOW:
608                                         cp = ts->uv_shadow;
609                                         break;
610
611                                 case TH_MARKER_OUTLINE:
612                                         cp = ts->marker_outline; break;
613                                 case TH_MARKER:
614                                         cp = ts->marker; break;
615                                 case TH_ACT_MARKER:
616                                         cp = ts->act_marker; break;
617                                 case TH_SEL_MARKER:
618                                         cp = ts->sel_marker; break;
619                                 case TH_BUNDLE_SOLID:
620                                         cp = ts->bundle_solid; break;
621                                 case TH_DIS_MARKER:
622                                         cp = ts->dis_marker; break;
623                                 case TH_PATH_BEFORE:
624                                         cp = ts->path_before; break;
625                                 case TH_PATH_AFTER:
626                                         cp = ts->path_after; break;
627                                 case TH_CAMERA_PATH:
628                                         cp = ts->camera_path; break;
629                                 case TH_LOCK_MARKER:
630                                         cp = ts->lock_marker; break;
631
632                                 case TH_MATCH:
633                                         cp = ts->match;
634                                         break;
635
636                                 case TH_SELECT_HIGHLIGHT:
637                                         cp = ts->selected_highlight;
638                                         break;
639
640                                 case TH_SKIN_ROOT:
641                                         cp = ts->skin_root;
642                                         break;
643
644                                 case TH_ANIM_ACTIVE:
645                                         cp = ts->anim_active;
646                                         break;
647                                 case TH_ANIM_INACTIVE:
648                                         cp = ts->anim_non_active;
649                                         break;
650
651                                 case TH_NLA_TWEAK:
652                                         cp = ts->nla_tweaking;
653                                         break;
654                                 case TH_NLA_TWEAK_DUPLI:
655                                         cp = ts->nla_tweakdupli;
656                                         break;
657
658                                 case TH_NLA_TRANSITION:
659                                         cp = ts->nla_transition;
660                                         break;
661                                 case TH_NLA_TRANSITION_SEL:
662                                         cp = ts->nla_transition_sel;
663                                         break;
664                                 case TH_NLA_META:
665                                         cp = ts->nla_meta;
666                                         break;
667                                 case TH_NLA_META_SEL:
668                                         cp = ts->nla_meta_sel;
669                                         break;
670                                 case TH_NLA_SOUND:
671                                         cp = ts->nla_sound;
672                                         break;
673                                 case TH_NLA_SOUND_SEL:
674                                         cp = ts->nla_sound_sel;
675                                         break;
676
677                                 case TH_WIDGET_EMBOSS:
678                                         cp = btheme->tui.widget_emboss; break;
679
680                                 case TH_EDITOR_OUTLINE:
681                                         cp = btheme->tui.editor_outline;
682                                         break;
683                                 case TH_AXIS_X:
684                                         cp = btheme->tui.xaxis; break;
685                                 case TH_AXIS_Y:
686                                         cp = btheme->tui.yaxis; break;
687                                 case TH_AXIS_Z:
688                                         cp = btheme->tui.zaxis; break;
689
690                                 case TH_MANIPULATOR_HI:
691                                         cp = btheme->tui.manipulator_hi; break;
692                                 case TH_MANIPULATOR_PRIMARY:
693                                         cp = btheme->tui.manipulator_primary; break;
694                                 case TH_MANIPULATOR_SECONDARY:
695                                         cp = btheme->tui.manipulator_secondary; break;
696                                 case TH_MANIPULATOR_A:
697                                         cp = btheme->tui.manipulator_a; break;
698                                 case TH_MANIPULATOR_B:
699                                         cp = btheme->tui.manipulator_b; break;
700
701                                 case TH_INFO_SELECTED:
702                                         cp = ts->info_selected;
703                                         break;
704                                 case TH_INFO_SELECTED_TEXT:
705                                         cp = ts->info_selected_text;
706                                         break;
707                                 case TH_INFO_ERROR:
708                                         cp = ts->info_error;
709                                         break;
710                                 case TH_INFO_ERROR_TEXT:
711                                         cp = ts->info_error_text;
712                                         break;
713                                 case TH_INFO_WARNING:
714                                         cp = ts->info_warning;
715                                         break;
716                                 case TH_INFO_WARNING_TEXT:
717                                         cp = ts->info_warning_text;
718                                         break;
719                                 case TH_INFO_INFO:
720                                         cp = ts->info_info;
721                                         break;
722                                 case TH_INFO_INFO_TEXT:
723                                         cp = ts->info_info_text;
724                                         break;
725                                 case TH_INFO_DEBUG:
726                                         cp = ts->info_debug;
727                                         break;
728                                 case TH_INFO_DEBUG_TEXT:
729                                         cp = ts->info_debug_text;
730                                         break;
731                                 case TH_V3D_CLIPPING_BORDER:
732                                         cp = ts->clipping_border_3d;
733                                         break;
734                         }
735                 }
736         }
737
738         return (const unsigned char *)cp;
739 }
740
741 /**
742  * initialize default theme
743  * \note: when you add new colors, created & saved themes need initialized
744  * use function below, init_userdef_do_versions()
745  */
746 void ui_theme_init_default(void)
747 {
748
749         /* we search for the theme with name Default */
750         bTheme *btheme = BLI_findstring(&U.themes, "Default", offsetof(bTheme, name));
751         if (btheme == NULL) {
752                 btheme = MEM_callocN(sizeof(bTheme), __func__);
753                 BLI_addtail(&U.themes, btheme);
754         }
755
756         UI_SetTheme(0, 0);  /* make sure the global used in this file is set */
757
758         const int active_theme_area = btheme->active_theme_area;
759         memcpy(btheme, &U_theme_default, sizeof(*btheme));
760         btheme->active_theme_area = active_theme_area;
761 }
762
763 void ui_style_init_default(void)
764 {
765         BLI_freelistN(&U.uistyles);
766         /* gets automatically re-allocated */
767         uiStyleInit();
768 }
769
770
771 void UI_SetTheme(int spacetype, int regionid)
772 {
773         if (spacetype) {
774                 /* later on, a local theme can be found too */
775                 theme_active = U.themes.first;
776                 theme_spacetype = spacetype;
777                 theme_regionid = regionid;
778         }
779         else if (regionid) {
780                 /* popups */
781                 theme_active = U.themes.first;
782                 theme_spacetype = SPACE_BUTS;
783                 theme_regionid = regionid;
784         }
785         else {
786                 /* for safety, when theme was deleted */
787                 theme_active = U.themes.first;
788                 theme_spacetype = SPACE_VIEW3D;
789                 theme_regionid = RGN_TYPE_WINDOW;
790         }
791 }
792
793 bTheme *UI_GetTheme(void)
794 {
795         return U.themes.first;
796 }
797
798 /**
799  * for the rare case we need to temp swap in a different theme (offscreen render)
800  */
801 void UI_Theme_Store(struct bThemeState *theme_state)
802 {
803         *theme_state = g_theme_state;
804 }
805 void UI_Theme_Restore(struct bThemeState *theme_state)
806 {
807         g_theme_state = *theme_state;
808 }
809
810 /* for space windows only */
811 void UI_ThemeColor(int colorid)
812 {
813         const unsigned char *cp;
814
815         cp = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid);
816         glColor3ubv(cp);
817
818 }
819
820 /* plus alpha */
821 void UI_ThemeColor4(int colorid)
822 {
823         const unsigned char *cp;
824
825         cp = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid);
826         glColor4ubv(cp);
827 }
828
829 /* set the color with offset for shades */
830 void UI_ThemeColorShade(int colorid, int offset)
831 {
832         unsigned char col[4];
833         UI_GetThemeColorShade4ubv(colorid, offset, col);
834         glColor4ubv(col);
835 }
836
837 void UI_ThemeColorShadeAlpha(int colorid, int coloffset, int alphaoffset)
838 {
839         int r, g, b, a;
840         const unsigned char *cp;
841
842         cp = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid);
843         r = coloffset + (int) cp[0];
844         CLAMP(r, 0, 255);
845         g = coloffset + (int) cp[1];
846         CLAMP(g, 0, 255);
847         b = coloffset + (int) cp[2];
848         CLAMP(b, 0, 255);
849         a = alphaoffset + (int) cp[3];
850         CLAMP(a, 0, 255);
851
852         glColor4ub(r, g, b, a);
853 }
854
855 void UI_GetThemeColorShadeAlpha4ubv(int colorid, int coloffset, int alphaoffset, unsigned char col[4])
856 {
857         int r, g, b, a;
858         const unsigned char *cp;
859
860         cp = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid);
861         r = coloffset + (int) cp[0];
862         CLAMP(r, 0, 255);
863         g = coloffset + (int) cp[1];
864         CLAMP(g, 0, 255);
865         b = coloffset + (int) cp[2];
866         CLAMP(b, 0, 255);
867         a = alphaoffset + (int) cp[3];
868         CLAMP(a, 0, 255);
869
870         col[0] = r;
871         col[1] = g;
872         col[2] = b;
873         col[3] = a;
874 }
875
876 void UI_GetThemeColorBlend3ubv(int colorid1, int colorid2, float fac, unsigned char col[3])
877 {
878         const unsigned char *cp1, *cp2;
879
880         cp1 = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid1);
881         cp2 = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid2);
882
883         CLAMP(fac, 0.0f, 1.0f);
884         col[0] = floorf((1.0f - fac) * cp1[0] + fac * cp2[0]);
885         col[1] = floorf((1.0f - fac) * cp1[1] + fac * cp2[1]);
886         col[2] = floorf((1.0f - fac) * cp1[2] + fac * cp2[2]);
887 }
888
889 void UI_GetThemeColorBlend3f(int colorid1, int colorid2, float fac, float r_col[3])
890 {
891         const unsigned char *cp1, *cp2;
892
893         cp1 = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid1);
894         cp2 = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid2);
895
896         CLAMP(fac, 0.0f, 1.0f);
897         r_col[0] = ((1.0f - fac) * cp1[0] + fac * cp2[0]) / 255.0f;
898         r_col[1] = ((1.0f - fac) * cp1[1] + fac * cp2[1]) / 255.0f;
899         r_col[2] = ((1.0f - fac) * cp1[2] + fac * cp2[2]) / 255.0f;
900 }
901
902 /* blend between to theme colors, and set it */
903 void UI_ThemeColorBlend(int colorid1, int colorid2, float fac)
904 {
905         unsigned char col[3];
906         UI_GetThemeColorBlend3ubv(colorid1, colorid2, fac, col);
907         glColor3ubv(col);
908 }
909
910 /* blend between to theme colors, shade it, and set it */
911 void UI_ThemeColorBlendShade(int colorid1, int colorid2, float fac, int offset)
912 {
913         int r, g, b;
914         const unsigned char *cp1, *cp2;
915
916         cp1 = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid1);
917         cp2 = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid2);
918
919         CLAMP(fac, 0.0f, 1.0f);
920         r = offset + floorf((1.0f - fac) * cp1[0] + fac * cp2[0]);
921         g = offset + floorf((1.0f - fac) * cp1[1] + fac * cp2[1]);
922         b = offset + floorf((1.0f - fac) * cp1[2] + fac * cp2[2]);
923
924         CLAMP(r, 0, 255);
925         CLAMP(g, 0, 255);
926         CLAMP(b, 0, 255);
927
928         glColor3ub(r, g, b);
929 }
930
931 /* blend between to theme colors, shade it, and set it */
932 void UI_ThemeColorBlendShadeAlpha(int colorid1, int colorid2, float fac, int offset, int alphaoffset)
933 {
934         int r, g, b, a;
935         const unsigned char *cp1, *cp2;
936
937         cp1 = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid1);
938         cp2 = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid2);
939
940         CLAMP(fac, 0.0f, 1.0f);
941         r = offset + floorf((1.0f - fac) * cp1[0] + fac * cp2[0]);
942         g = offset + floorf((1.0f - fac) * cp1[1] + fac * cp2[1]);
943         b = offset + floorf((1.0f - fac) * cp1[2] + fac * cp2[2]);
944         a = alphaoffset + floorf((1.0f - fac) * cp1[3] + fac * cp2[3]);
945
946         CLAMP(r, 0, 255);
947         CLAMP(g, 0, 255);
948         CLAMP(b, 0, 255);
949         CLAMP(a, 0, 255);
950
951         glColor4ub(r, g, b, a);
952 }
953
954 void UI_FontThemeColor(int fontid, int colorid)
955 {
956         unsigned char color[4];
957         UI_GetThemeColor4ubv(colorid, color);
958         BLF_color4ubv(fontid, color);
959 }
960
961 /* get individual values, not scaled */
962 float UI_GetThemeValuef(int colorid)
963 {
964         const unsigned char *cp;
965
966         cp = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid);
967         return ((float)cp[0]);
968 }
969
970 /* get individual values, not scaled */
971 int UI_GetThemeValue(int colorid)
972 {
973         const unsigned char *cp;
974
975         cp = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid);
976         return ((int) cp[0]);
977 }
978
979 /* versions of the function above, which take a space-type */
980 float UI_GetThemeValueTypef(int colorid, int spacetype)
981 {
982         const unsigned char *cp;
983
984         cp = UI_ThemeGetColorPtr(theme_active, spacetype, colorid);
985         return ((float)cp[0]);
986 }
987
988 int UI_GetThemeValueType(int colorid, int spacetype)
989 {
990         const unsigned char *cp;
991
992         cp = UI_ThemeGetColorPtr(theme_active, spacetype, colorid);
993         return ((int)cp[0]);
994 }
995
996
997 /* get the color, range 0.0-1.0 */
998 void UI_GetThemeColor3fv(int colorid, float col[3])
999 {
1000         const unsigned char *cp;
1001
1002         cp = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid);
1003         col[0] = ((float)cp[0]) / 255.0f;
1004         col[1] = ((float)cp[1]) / 255.0f;
1005         col[2] = ((float)cp[2]) / 255.0f;
1006 }
1007
1008 void UI_GetThemeColor4fv(int colorid, float col[4])
1009 {
1010         const unsigned char *cp;
1011
1012         cp = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid);
1013         col[0] = ((float)cp[0]) / 255.0f;
1014         col[1] = ((float)cp[1]) / 255.0f;
1015         col[2] = ((float)cp[2]) / 255.0f;
1016         col[3] = ((float)cp[3]) / 255.0f;
1017 }
1018
1019 /* get the color, range 0.0-1.0, complete with shading offset */
1020 void UI_GetThemeColorShade3fv(int colorid, int offset, float col[3])
1021 {
1022         int r, g, b;
1023         const unsigned char *cp;
1024
1025         cp = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid);
1026
1027         r = offset + (int) cp[0];
1028         CLAMP(r, 0, 255);
1029         g = offset + (int) cp[1];
1030         CLAMP(g, 0, 255);
1031         b = offset + (int) cp[2];
1032         CLAMP(b, 0, 255);
1033
1034         col[0] = ((float)r) / 255.0f;
1035         col[1] = ((float)g) / 255.0f;
1036         col[2] = ((float)b) / 255.0f;
1037 }
1038
1039 void UI_GetThemeColorShade3ubv(int colorid, int offset, unsigned char col[3])
1040 {
1041         int r, g, b;
1042         const unsigned char *cp;
1043
1044         cp = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid);
1045
1046         r = offset + (int) cp[0];
1047         CLAMP(r, 0, 255);
1048         g = offset + (int) cp[1];
1049         CLAMP(g, 0, 255);
1050         b = offset + (int) cp[2];
1051         CLAMP(b, 0, 255);
1052
1053         col[0] = r;
1054         col[1] = g;
1055         col[2] = b;
1056 }
1057
1058 void UI_GetThemeColorBlendShade3ubv(int colorid1, int colorid2, float fac, int offset, unsigned char col[3])
1059 {
1060         const unsigned char *cp1, *cp2;
1061
1062         cp1 = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid1);
1063         cp2 = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid2);
1064
1065         CLAMP(fac, 0.0f, 1.0f);
1066
1067         float blend[3];
1068         blend[0] = offset + floorf((1.0f - fac) * cp1[0] + fac * cp2[0]);
1069         blend[1] = offset + floorf((1.0f - fac) * cp1[1] + fac * cp2[1]);
1070         blend[2] = offset + floorf((1.0f - fac) * cp1[2] + fac * cp2[2]);
1071
1072         unit_float_to_uchar_clamp_v3(col, blend);
1073 }
1074
1075 void UI_GetThemeColorShade4ubv(int colorid, int offset, unsigned char col[4])
1076 {
1077         int r, g, b;
1078         const unsigned char *cp;
1079
1080         cp = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid);
1081         r = offset + (int) cp[0];
1082         CLAMP(r, 0, 255);
1083         g = offset + (int) cp[1];
1084         CLAMP(g, 0, 255);
1085         b = offset + (int) cp[2];
1086         CLAMP(b, 0, 255);
1087
1088         col[0] = r;
1089         col[1] = g;
1090         col[2] = b;
1091         col[3] = cp[3];
1092 }
1093
1094 void UI_GetThemeColorShadeAlpha4fv(int colorid, int coloffset, int alphaoffset, float col[4])
1095 {
1096         int r, g, b, a;
1097         const unsigned char *cp;
1098
1099         cp = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid);
1100
1101         r = coloffset + (int) cp[0];
1102         CLAMP(r, 0, 255);
1103         g = coloffset + (int) cp[1];
1104         CLAMP(g, 0, 255);
1105         b = coloffset + (int) cp[2];
1106         CLAMP(b, 0, 255);
1107         a = alphaoffset + (int) cp[3];
1108         CLAMP(b, 0, 255);
1109
1110         col[0] = ((float)r) / 255.0f;
1111         col[1] = ((float)g) / 255.0f;
1112         col[2] = ((float)b) / 255.0f;
1113         col[3] = ((float)a) / 255.0f;
1114 }
1115
1116 void UI_GetThemeColorBlendShade3fv(int colorid1, int colorid2, float fac, int offset, float col[3])
1117 {
1118         int r, g, b;
1119         const unsigned char *cp1, *cp2;
1120
1121         cp1 = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid1);
1122         cp2 = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid2);
1123
1124         CLAMP(fac, 0.0f, 1.0f);
1125
1126         r = offset + floorf((1.0f - fac) * cp1[0] + fac * cp2[0]);
1127         CLAMP(r, 0, 255);
1128         g = offset + floorf((1.0f - fac) * cp1[1] + fac * cp2[1]);
1129         CLAMP(g, 0, 255);
1130         b = offset + floorf((1.0f - fac) * cp1[2] + fac * cp2[2]);
1131         CLAMP(b, 0, 255);
1132
1133         col[0] = ((float)r) / 255.0f;
1134         col[1] = ((float)g) / 255.0f;
1135         col[2] = ((float)b) / 255.0f;
1136 }
1137
1138 void UI_GetThemeColorBlendShade4fv(int colorid1, int colorid2, float fac, int offset, float col[4])
1139 {
1140         int r, g, b, a;
1141         const unsigned char *cp1, *cp2;
1142
1143         cp1 = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid1);
1144         cp2 = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid2);
1145
1146         CLAMP(fac, 0.0f, 1.0f);
1147
1148         r = offset + floorf((1.0f - fac) * cp1[0] + fac * cp2[0]);
1149         CLAMP(r, 0, 255);
1150         g = offset + floorf((1.0f - fac) * cp1[1] + fac * cp2[1]);
1151         CLAMP(g, 0, 255);
1152         b = offset + floorf((1.0f - fac) * cp1[2] + fac * cp2[2]);
1153         CLAMP(b, 0, 255);
1154         a = offset + floorf((1.0f - fac) * cp1[3] + fac * cp2[3]);
1155         CLAMP(a, 0, 255);
1156
1157         col[0] = ((float)r) / 255.0f;
1158         col[1] = ((float)g) / 255.0f;
1159         col[2] = ((float)b) / 255.0f;
1160         col[3] = ((float)a) / 255.0f;
1161 }
1162
1163 /* get the color, in char pointer */
1164 void UI_GetThemeColor3ubv(int colorid, unsigned char col[3])
1165 {
1166         const unsigned char *cp;
1167
1168         cp = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid);
1169         col[0] = cp[0];
1170         col[1] = cp[1];
1171         col[2] = cp[2];
1172 }
1173
1174 /* get the color, range 0.0-1.0, complete with shading offset */
1175 void UI_GetThemeColorShade4fv(int colorid, int offset, float col[4])
1176 {
1177         int r, g, b, a;
1178         const unsigned char *cp;
1179
1180         cp = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid);
1181
1182         r = offset + (int) cp[0];
1183         CLAMP(r, 0, 255);
1184         g = offset + (int) cp[1];
1185         CLAMP(g, 0, 255);
1186         b = offset + (int) cp[2];
1187         CLAMP(b, 0, 255);
1188
1189         a = (int) cp[3]; /* no shading offset... */
1190         CLAMP(a, 0, 255);
1191
1192         col[0] = ((float)r) / 255.0f;
1193         col[1] = ((float)g) / 255.0f;
1194         col[2] = ((float)b) / 255.0f;
1195         col[3] = ((float)a) / 255.0f;
1196 }
1197
1198 /* get the color, in char pointer */
1199 void UI_GetThemeColor4ubv(int colorid, unsigned char col[4])
1200 {
1201         const unsigned char *cp;
1202
1203         cp = UI_ThemeGetColorPtr(theme_active, theme_spacetype, colorid);
1204         col[0] = cp[0];
1205         col[1] = cp[1];
1206         col[2] = cp[2];
1207         col[3] = cp[3];
1208 }
1209
1210 void UI_GetThemeColorType4ubv(int colorid, int spacetype, char col[4])
1211 {
1212         const unsigned char *cp;
1213
1214         cp = UI_ThemeGetColorPtr(theme_active, spacetype, colorid);
1215         col[0] = cp[0];
1216         col[1] = cp[1];
1217         col[2] = cp[2];
1218         col[3] = cp[3];
1219 }
1220
1221 /* blends and shades between two char color pointers */
1222 void UI_ColorPtrBlendShade3ubv(const unsigned char cp1[3], const unsigned char cp2[3], float fac, int offset)
1223 {
1224         int r, g, b;
1225         CLAMP(fac, 0.0f, 1.0f);
1226         r = offset + floorf((1.0f - fac) * cp1[0] + fac * cp2[0]);
1227         g = offset + floorf((1.0f - fac) * cp1[1] + fac * cp2[1]);
1228         b = offset + floorf((1.0f - fac) * cp1[2] + fac * cp2[2]);
1229
1230         r = r < 0 ? 0 : (r > 255 ? 255 : r);
1231         g = g < 0 ? 0 : (g > 255 ? 255 : g);
1232         b = b < 0 ? 0 : (b > 255 ? 255 : b);
1233
1234         glColor3ub(r, g, b);
1235 }
1236
1237 void UI_GetColorPtrShade3ubv(const unsigned char cp[3], unsigned char col[3], int offset)
1238 {
1239         int r, g, b;
1240
1241         r = offset + (int)cp[0];
1242         g = offset + (int)cp[1];
1243         b = offset + (int)cp[2];
1244
1245         CLAMP(r, 0, 255);
1246         CLAMP(g, 0, 255);
1247         CLAMP(b, 0, 255);
1248
1249         col[0] = r;
1250         col[1] = g;
1251         col[2] = b;
1252 }
1253
1254 /* get a 3 byte color, blended and shaded between two other char color pointers */
1255 void UI_GetColorPtrBlendShade3ubv(
1256         const unsigned char cp1[3], const unsigned char cp2[3], unsigned char col[3],
1257         float fac, int offset)
1258 {
1259         int r, g, b;
1260
1261         CLAMP(fac, 0.0f, 1.0f);
1262         r = offset + floor((1.0f - fac) * cp1[0] + fac * cp2[0]);
1263         g = offset + floor((1.0f - fac) * cp1[1] + fac * cp2[1]);
1264         b = offset + floor((1.0f - fac) * cp1[2] + fac * cp2[2]);
1265
1266         CLAMP(r, 0, 255);
1267         CLAMP(g, 0, 255);
1268         CLAMP(b, 0, 255);
1269
1270         col[0] = r;
1271         col[1] = g;
1272         col[2] = b;
1273 }
1274
1275 void UI_ThemeClearColor(int colorid)
1276 {
1277         float col[3];
1278
1279         UI_GetThemeColor3fv(colorid, col);
1280         GPU_clear_color(col[0], col[1], col[2], 0.0f);
1281 }
1282
1283 void UI_ThemeClearColorAlpha(int colorid, float alpha)
1284 {
1285         float col[3];
1286         UI_GetThemeColor3fv(colorid, col);
1287         GPU_clear_color(col[0], col[1], col[2], alpha);
1288 }
1289
1290
1291 int UI_ThemeMenuShadowWidth(void)
1292 {
1293         bTheme *btheme = UI_GetTheme();
1294         return (int)(btheme->tui.menu_shadow_width * UI_DPI_FAC);
1295 }
1296
1297 void UI_make_axis_color(const unsigned char src_col[3], unsigned char dst_col[3], const char axis)
1298 {
1299         unsigned char col[3];
1300
1301         switch (axis) {
1302                 case 'X':
1303                         UI_GetThemeColor3ubv(TH_AXIS_X, col);
1304                         UI_GetColorPtrBlendShade3ubv(src_col, col, dst_col, 0.5f, -10);
1305                         break;
1306                 case 'Y':
1307                         UI_GetThemeColor3ubv(TH_AXIS_Y, col);
1308                         UI_GetColorPtrBlendShade3ubv(src_col, col, dst_col, 0.5f, -10);
1309                         break;
1310                 case 'Z':
1311                         UI_GetThemeColor3ubv(TH_AXIS_Z, col);
1312                         UI_GetColorPtrBlendShade3ubv(src_col, col, dst_col, 0.5f, -10);
1313                         break;
1314                 default:
1315                         BLI_assert(0);
1316                         break;
1317         }
1318 }
1319
1320 /* ************************************************************* */
1321
1322 /* patching UserDef struct and Themes */
1323 void init_userdef_do_versions(Main *bmain)
1324 {
1325 #define USER_VERSION_ATLEAST(ver, subver) MAIN_VERSION_ATLEAST(bmain, ver, subver)
1326
1327         /* the UserDef struct is not corrected with do_versions() .... ugh! */
1328         if (U.wheellinescroll == 0) U.wheellinescroll = 3;
1329         if (U.menuthreshold1 == 0) {
1330                 U.menuthreshold1 = 5;
1331                 U.menuthreshold2 = 2;
1332         }
1333         if (U.tb_leftmouse == 0) {
1334                 U.tb_leftmouse = 5;
1335                 U.tb_rightmouse = 5;
1336         }
1337         if (U.mixbufsize == 0) U.mixbufsize = 2048;
1338         if (STREQ(U.tempdir, "/")) {
1339                 BKE_tempdir_system_init(U.tempdir);
1340         }
1341         if (U.autokey_mode == 0) {
1342                 /* 'add/replace' but not on */
1343                 U.autokey_mode = 2;
1344         }
1345         if (U.savetime <= 0) {
1346                 U.savetime = 1;
1347 // XXX          error(STRINGIFY(BLENDER_STARTUP_FILE)" is buggy, please consider removing it.\n");
1348         }
1349         if (U.manipulator_size == 0) {
1350                 U.manipulator_size = 75;
1351                 U.manipulator_flag |= USER_MANIPULATOR_DRAW;
1352         }
1353         if (U.pad_rot_angle == 0.0f)
1354                 U.pad_rot_angle = 15.0f;
1355
1356         /* graph editor - unselected F-Curve visibility */
1357         if (U.fcu_inactive_alpha == 0) {
1358                 U.fcu_inactive_alpha = 0.25f;
1359         }
1360
1361         /* signal for evaluated mesh to use colorband */
1362         /* run in case this was on and is now off in the user prefs [#28096] */
1363         BKE_mesh_runtime_color_band_store((U.flag & USER_CUSTOM_RANGE) ? (&U.coba_weight) : NULL, UI_GetTheme()->tv3d.vertex_unreferenced);
1364
1365         if (!USER_VERSION_ATLEAST(192, 0)) {
1366                 strcpy(U.sounddir, "/");
1367         }
1368
1369         /* patch to set Dupli Armature */
1370         if (!USER_VERSION_ATLEAST(220, 0)) {
1371                 U.dupflag |= USER_DUP_ARM;
1372         }
1373
1374         /* added seam, normal color, undo */
1375         if (!USER_VERSION_ATLEAST(235, 0)) {
1376                 U.uiflag |= USER_GLOBALUNDO;
1377                 if (U.undosteps == 0) U.undosteps = 32;
1378         }
1379         if (!USER_VERSION_ATLEAST(236, 0)) {
1380                 /* illegal combo... */
1381                 if (U.flag & USER_LMOUSESELECT)
1382                         U.flag &= ~USER_TWOBUTTONMOUSE;
1383         }
1384         if (!USER_VERSION_ATLEAST(240, 0)) {
1385                 U.uiflag |= USER_PLAINMENUS;
1386                 if (U.obcenter_dia == 0) U.obcenter_dia = 6;
1387         }
1388         if (!USER_VERSION_ATLEAST(242, 0)) {
1389                 /* set defaults for 3D View rotating axis indicator */
1390                 /* since size can't be set to 0, this indicates it's not saved in startup.blend */
1391                 if (U.rvisize == 0) {
1392                         U.rvisize = 15;
1393                         U.rvibright = 8;
1394                         U.uiflag |= USER_SHOW_ROTVIEWICON;
1395                 }
1396
1397         }
1398         if (!USER_VERSION_ATLEAST(244, 0)) {
1399                 /* set default number of recently-used files (if not set) */
1400                 if (U.recent_files == 0) U.recent_files = 10;
1401         }
1402         if (!USER_VERSION_ATLEAST(245, 3)) {
1403                 if (U.coba_weight.tot == 0)
1404                         BKE_colorband_init(&U.coba_weight, true);
1405         }
1406         if (!USER_VERSION_ATLEAST(245, 3)) {
1407                 U.flag |= USER_ADD_VIEWALIGNED | USER_ADD_EDITMODE;
1408         }
1409         if (!USER_VERSION_ATLEAST(250, 0)) {
1410                 /* adjust grease-pencil distances */
1411                 U.gp_manhattendist = 1;
1412                 U.gp_euclideandist = 2;
1413
1414                 /* adjust default interpolation for new IPO-curves */
1415                 U.ipo_new = BEZT_IPO_BEZ;
1416         }
1417
1418         if (!USER_VERSION_ATLEAST(250, 3)) {
1419                 /* new audio system */
1420                 if (U.audiochannels == 0)
1421                         U.audiochannels = 2;
1422                 if (U.audiodevice == 0) {
1423 #ifdef WITH_OPENAL
1424                         U.audiodevice = 2;
1425 #endif
1426 #ifdef WITH_SDL
1427                         U.audiodevice = 1;
1428 #endif
1429                 }
1430                 if (U.audioformat == 0)
1431                         U.audioformat = 0x24;
1432                 if (U.audiorate == 0)
1433                         U.audiorate = 48000;
1434         }
1435
1436         if (!USER_VERSION_ATLEAST(250, 8)) {
1437                 wmKeyMap *km;
1438
1439                 for (km = U.user_keymaps.first; km; km = km->next) {
1440                         if (STREQ(km->idname, "Armature_Sketch"))
1441                                 strcpy(km->idname, "Armature Sketch");
1442                         else if (STREQ(km->idname, "View3D"))
1443                                 strcpy(km->idname, "3D View");
1444                         else if (STREQ(km->idname, "View3D Generic"))
1445                                 strcpy(km->idname, "3D View Generic");
1446                         else if (STREQ(km->idname, "EditMesh"))
1447                                 strcpy(km->idname, "Mesh");
1448                         else if (STREQ(km->idname, "UVEdit"))
1449                                 strcpy(km->idname, "UV Editor");
1450                         else if (STREQ(km->idname, "Animation_Channels"))
1451                                 strcpy(km->idname, "Animation Channels");
1452                         else if (STREQ(km->idname, "GraphEdit Keys"))
1453                                 strcpy(km->idname, "Graph Editor");
1454                         else if (STREQ(km->idname, "GraphEdit Generic"))
1455                                 strcpy(km->idname, "Graph Editor Generic");
1456                         else if (STREQ(km->idname, "Action_Keys"))
1457                                 strcpy(km->idname, "Dopesheet");
1458                         else if (STREQ(km->idname, "NLA Data"))
1459                                 strcpy(km->idname, "NLA Editor");
1460                         else if (STREQ(km->idname, "Node Generic"))
1461                                 strcpy(km->idname, "Node Editor");
1462                         else if (STREQ(km->idname, "Logic Generic"))
1463                                 strcpy(km->idname, "Logic Editor");
1464                         else if (STREQ(km->idname, "File"))
1465                                 strcpy(km->idname, "File Browser");
1466                         else if (STREQ(km->idname, "FileMain"))
1467                                 strcpy(km->idname, "File Browser Main");
1468                         else if (STREQ(km->idname, "FileButtons"))
1469                                 strcpy(km->idname, "File Browser Buttons");
1470                         else if (STREQ(km->idname, "Buttons Generic"))
1471                                 strcpy(km->idname, "Property Editor");
1472                 }
1473         }
1474
1475         if (!USER_VERSION_ATLEAST(252, 3)) {
1476                 if (U.flag & USER_LMOUSESELECT)
1477                         U.flag &= ~USER_TWOBUTTONMOUSE;
1478         }
1479         if (!USER_VERSION_ATLEAST(252, 4)) {
1480                 /* default new handle type is auto handles */
1481                 U.keyhandles_new = HD_AUTO;
1482         }
1483
1484         if (!USER_VERSION_ATLEAST(257, 0)) {
1485                 /* clear "AUTOKEY_FLAG_ONLYKEYINGSET" flag from userprefs,
1486                  * so that it doesn't linger around from old configs like a ghost */
1487                 U.autokey_flag &= ~AUTOKEY_FLAG_ONLYKEYINGSET;
1488         }
1489
1490         if (!USER_VERSION_ATLEAST(260, 3)) {
1491                 /* if new keyframes handle default is stuff "auto", make it "auto-clamped" instead
1492                  * was changed in 260 as part of GSoC11, but version patch was wrong
1493                  */
1494                 if (U.keyhandles_new == HD_AUTO)
1495                         U.keyhandles_new = HD_AUTO_ANIM;
1496
1497                 /* enable (Cycles) addon by default */
1498                 BKE_addon_ensure(&U.addons, "cycles");
1499         }
1500
1501         if (!USER_VERSION_ATLEAST(261, 4)) {
1502                 U.use_16bit_textures = true;
1503         }
1504
1505         if (!USER_VERSION_ATLEAST(267, 0)) {
1506
1507                 /* GL Texture Garbage Collection */
1508                 if (U.textimeout == 0) {
1509                         U.texcollectrate = 60;
1510                         U.textimeout = 120;
1511                 }
1512                 if (U.memcachelimit <= 0) {
1513                         U.memcachelimit = 32;
1514                 }
1515                 if (U.dbl_click_time == 0) {
1516                         U.dbl_click_time = 350;
1517                 }
1518                 if (U.v2d_min_gridsize == 0) {
1519                         U.v2d_min_gridsize = 35;
1520                 }
1521                 if (U.dragthreshold == 0)
1522                         U.dragthreshold = 5;
1523                 if (U.widget_unit == 0)
1524                         U.widget_unit = 20;
1525                 if (U.anisotropic_filter <= 0)
1526                         U.anisotropic_filter = 1;
1527
1528                 if (U.ndof_sensitivity == 0.0f) {
1529                         U.ndof_sensitivity = 1.0f;
1530                         U.ndof_flag = (NDOF_LOCK_HORIZON | NDOF_SHOULD_PAN | NDOF_SHOULD_ZOOM | NDOF_SHOULD_ROTATE);
1531                 }
1532
1533                 if (U.ndof_orbit_sensitivity == 0.0f) {
1534                         U.ndof_orbit_sensitivity = U.ndof_sensitivity;
1535
1536                         if (!(U.flag & USER_TRACKBALL))
1537                                 U.ndof_flag |= NDOF_TURNTABLE;
1538                 }
1539                 if (U.tweak_threshold == 0)
1540                         U.tweak_threshold = 10;
1541         }
1542
1543         /* NOTE!! from now on use U.versionfile and U.subversionfile */
1544 #undef USER_VERSION_ATLEAST
1545 #define USER_VERSION_ATLEAST(ver, subver) MAIN_VERSION_ATLEAST((&(U)), ver, subver)
1546
1547
1548         if (!USER_VERSION_ATLEAST(269, 9)) {
1549                 /* grease pencil - new layer color */
1550                 if (U.gpencil_new_layer_col[3] < 0.1f) {
1551                         /* defaults to black, but must at least be visible! */
1552                         U.gpencil_new_layer_col[3] = 0.9f;
1553                 }
1554         }
1555
1556         if (!USER_VERSION_ATLEAST(271, 5)) {
1557                 U.pie_menu_radius = 100;
1558                 U.pie_menu_threshold = 12;
1559                 U.pie_animation_timeout = 6;
1560         }
1561
1562         if (!USER_VERSION_ATLEAST(275, 2)) {
1563                 U.ndof_deadzone = 0.1;
1564         }
1565
1566         if (!USER_VERSION_ATLEAST(275, 4)) {
1567                 U.node_margin = 80;
1568         }
1569
1570         if (!USER_VERSION_ATLEAST(278, 6)) {
1571                 /* Clear preference flags for re-use. */
1572                 U.flag &= ~(
1573                     USER_FLAG_DEPRECATED_1 | USER_FLAG_DEPRECATED_2 | USER_FLAG_DEPRECATED_3 |
1574                     USER_FLAG_DEPRECATED_6 | USER_FLAG_DEPRECATED_7 |
1575                     USER_FLAG_DEPRECATED_9 | USER_DEVELOPER_UI);
1576                 U.uiflag &= ~(
1577                     USER_UIFLAG_DEPRECATED_7);
1578                 U.transopts &= ~(
1579                     USER_TR_DEPRECATED_2 | USER_TR_DEPRECATED_3 | USER_TR_DEPRECATED_4 |
1580                     USER_TR_DEPRECATED_6 | USER_TR_DEPRECATED_7);
1581
1582                 U.uiflag |= USER_LOCK_CURSOR_ADJUST;
1583         }
1584
1585
1586         if (!USER_VERSION_ATLEAST(280, 20)) {
1587                 U.gpu_viewport_quality = 0.6f;
1588
1589                 /* Reset theme, old themes will not be compatible with minor version updates from now on. */
1590                 for (bTheme *btheme = U.themes.first; btheme; btheme = btheme->next) {
1591                         memcpy(btheme, &U_theme_default, sizeof(*btheme));
1592                 }
1593         }
1594
1595         /**
1596          * Include next version bump.
1597          */
1598         {
1599                 /* (keep this block even if it becomes empty). */
1600         }
1601
1602         if (U.pixelsize == 0.0f)
1603                 U.pixelsize = 1.0f;
1604
1605         if (U.image_draw_method == 0)
1606                 U.image_draw_method = IMAGE_DRAW_METHOD_2DTEXTURE;
1607
1608         // we default to the first audio device
1609         U.audiodevice = 0;
1610
1611         /* Not versioning, just avoid errors. */
1612 #ifndef WITH_CYCLES
1613         BKE_addon_remove_safe(&U.addons, "cycles");
1614 #endif
1615
1616         /* funny name, but it is GE stuff, moves userdef stuff to engine */
1617 // XXX  space_set_commmandline_options();
1618         /* this timer uses U */
1619 // XXX  reset_autosave();
1620
1621 }