Fix for [#20226] 3DView header: hiding menu texts and layer button's issue.
[blender.git] / source / blender / editors / space_view3d / view3d_header.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2004-2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <string.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32
33 #include "DNA_armature_types.h"
34 #include "DNA_ID.h"
35 #include "DNA_image_types.h"
36 #include "DNA_meshdata_types.h"
37 #include "DNA_mesh_types.h"
38 #include "DNA_object_types.h"
39 #include "DNA_space_types.h"
40 #include "DNA_scene_types.h"
41 #include "DNA_screen_types.h"
42 #include "DNA_texture_types.h"
43 #include "DNA_userdef_types.h" /* U.smooth_viewtx */
44 #include "DNA_view3d_types.h"
45 #include "DNA_windowmanager_types.h"
46
47 #include "RNA_access.h"
48
49 #include "MEM_guardedalloc.h"
50
51 #include "BKE_action.h"
52 #include "BKE_brush.h"
53 #include "BKE_context.h"
54 #include "BKE_curve.h"
55 #include "BKE_depsgraph.h"
56 #include "BKE_displist.h"
57 #include "BKE_effect.h"
58 #include "BKE_global.h"
59 #include "BKE_image.h"
60 #include "BKE_library.h"
61 #include "BKE_main.h"
62 #include "BKE_mesh.h"
63 #include "BKE_modifier.h"
64 #include "BKE_paint.h"
65 #include "BKE_particle.h"
66 #include "BKE_screen.h"
67 #include "BKE_utildefines.h" /* for VECCOPY */
68
69 #include "ED_armature.h"
70 #include "ED_particle.h"
71 #include "ED_object.h"
72 #include "ED_mesh.h"
73 #include "ED_util.h"
74 #include "ED_screen.h"
75 #include "ED_transform.h"
76 #include "ED_types.h"
77
78 #include "WM_api.h"
79 #include "WM_types.h"
80
81 #include "RNA_access.h"
82 #include "RNA_define.h"
83 #include "RNA_enum_types.h"
84
85 #include "BIF_gl.h"
86 #include "BIF_glutil.h"
87
88 #include "BLI_math.h"
89 #include "BLI_blenlib.h"
90 #include "BLI_editVert.h"
91
92 #include "UI_interface.h"
93 #include "UI_interface_icons.h"
94 #include "UI_resources.h"
95 #include "UI_view2d.h"
96
97 #include "view3d_intern.h"
98
99
100 /* View3d->modeselect 
101  * This is a bit of a dodgy hack to enable a 'mode' menu with icons+labels
102  * rather than those buttons.
103  * I know the implementation's not good - it's an experiment to see if this
104  * approach would work well
105  *
106  * This can be cleaned when I make some new 'mode' icons.
107  */
108
109 #define TEST_EDITMESH   if(obedit==0) return; \
110                                                 if( (v3d->lay & obedit->lay)==0 ) return;
111
112 /* XXX port over */     
113 extern void borderselect();
114
115 /* view3d handler codes */
116 #define VIEW3D_HANDLER_BACKGROUND       1
117 #define VIEW3D_HANDLER_PROPERTIES       2
118 #define VIEW3D_HANDLER_OBJECT           3
119 #define VIEW3D_HANDLER_PREVIEW          4
120 #define VIEW3D_HANDLER_MULTIRES         5
121 #define VIEW3D_HANDLER_TRANSFORM        6
122 #define VIEW3D_HANDLER_GREASEPENCIL 7
123 #define VIEW3D_HANDLER_BONESKETCH       8
124
125 /* end XXX ************* */
126
127 static void do_view3d_header_buttons(bContext *C, void *arg, int event);
128
129 #define B_SCENELOCK 101
130 #define B_FULL          102
131 #define B_HOME          103
132 #define B_VIEWBUT       104
133 #define B_PERSP         105
134 #define B_MODESELECT 108
135 #define B_SEL_VERT      110
136 #define B_SEL_EDGE      111
137 #define B_SEL_FACE      112
138 #define B_MAN_TRANS     116
139 #define B_MAN_ROT       117
140 #define B_MAN_SCALE     118
141 #define B_NDOF          119     
142 #define B_MAN_MODE      120
143 #define B_REDR          122
144 #define B_NOP           123
145
146 // XXX quickly ported across
147 static void handle_view3d_lock(bContext *C) 
148 {
149         Main *bmain= CTX_data_main(C);
150         Scene *scene= CTX_data_scene(C);
151         ScrArea *sa= CTX_wm_area(C);
152         View3D *v3d= CTX_wm_view3d(C);
153         
154         if (v3d != NULL && sa != NULL) {
155                 if(v3d->localvd==NULL && v3d->scenelock && sa->spacetype==SPACE_VIEW3D) {
156                         /* copy to scene */
157                         scene->lay= v3d->lay;
158                         scene->camera= v3d->camera;
159
160                         /* not through notifiery, listener don't have context
161                            and non-open screens or spaces need to be updated too */
162                         ED_view3d_scene_layers_update(bmain, scene);
163                         
164                         /* notifiers for scene update */
165                         WM_event_add_notifier(C, NC_SCENE|ND_LAYER, scene);
166                 }
167         }
168 }
169
170 static int layers_exec(bContext *C, wmOperator *op)
171 {
172         Scene *scene= CTX_data_scene(C);
173         ScrArea *sa= CTX_wm_area(C);
174         View3D *v3d= sa->spacedata.first;
175         int nr= RNA_int_get(op->ptr, "nr");
176         
177         if(nr < 0)
178                 return OPERATOR_CANCELLED;
179         
180         
181         if(nr == 0) {
182                 /* all layers */
183                 v3d->lay |= (1<<20)-1;
184
185                 if(!v3d->layact)
186                         v3d->layact= 1;
187         }
188         else {
189                 nr--;
190
191                 if(RNA_boolean_get(op->ptr, "extend"))
192                         v3d->lay |= (1<<nr);
193                 else
194                         v3d->lay = (1<<nr);
195                 
196                 /* set active layer, ensure to always have one */
197                 if(v3d->lay & (1<<nr))
198                    v3d->layact= 1<<nr;
199                 else if((v3d->lay & v3d->layact)==0) {
200                         int bit= 0;
201
202                         while(bit<32) {
203                                 if(v3d->lay & (1<<bit)) {
204                                         v3d->layact= 1<<bit;
205                                         break;
206                                 }
207                                 bit++;
208                         }
209                 }
210         }
211         
212         if(v3d->scenelock) handle_view3d_lock(C);
213         
214         /* new layers might need unflushed events events */
215         DAG_scene_update_flags(scene, v3d->lay);        /* tags all that moves and flushes */
216
217         ED_area_tag_redraw(sa);
218         
219         return OPERATOR_FINISHED;
220 }
221
222 /* applies shift and alt, lazy coding or ok? :) */
223 /* the local per-keymap-entry keymap will solve it */
224 static int layers_invoke(bContext *C, wmOperator *op, wmEvent *event)
225 {
226         if(event->ctrl || event->oskey)
227                 return OPERATOR_PASS_THROUGH;
228         
229         if(event->shift)
230                 RNA_boolean_set(op->ptr, "extend", 1);
231         
232         if(event->alt) {
233                 int nr= RNA_int_get(op->ptr, "nr") + 10;
234                 RNA_int_set(op->ptr, "nr", nr);
235         }
236         layers_exec(C, op);
237         
238         return OPERATOR_FINISHED;
239 }
240
241 void VIEW3D_OT_layers(wmOperatorType *ot)
242 {
243         /* identifiers */
244         ot->name= "Layers";
245         ot->description= "Toggle layer(s) visibility.";
246         ot->idname= "VIEW3D_OT_layers";
247         
248         /* api callbacks */
249         ot->invoke= layers_invoke;
250         ot->exec= layers_exec;
251         ot->poll= ED_operator_view3d_active;
252         
253         /* flags */
254         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
255         
256         RNA_def_int(ot->srna, "nr", 1, 0, 20, "Number", "The layer number to set, zero for all layers", 0, 20);
257         RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Add this layer to the current view layers");
258 }
259
260 static char *view3d_modeselect_pup(Scene *scene)
261 {
262         Object *ob= OBACT;
263         static char string[1024];
264         static char formatstr[] = "|%s %%x%d %%i%d";
265         char *str = string;
266
267         str += sprintf(str, "Mode: %%t");
268         
269         str += sprintf(str, formatstr, "Object Mode", OB_MODE_OBJECT, ICON_OBJECT_DATA);
270         
271         if(ob==NULL) return string;
272         
273         /* if active object is editable */
274         if ( ((ob->type == OB_MESH)
275                 || (ob->type == OB_CURVE) || (ob->type == OB_SURF) || (ob->type == OB_FONT)
276                 || (ob->type == OB_MBALL) || (ob->type == OB_LATTICE))) {
277                 
278                 str += sprintf(str, formatstr, "Edit Mode", OB_MODE_EDIT, ICON_EDITMODE_HLT);
279         }
280         else if (ob->type == OB_ARMATURE) {
281                 if (ob->mode & OB_MODE_POSE)
282                         str += sprintf(str, formatstr, "Edit Mode", OB_MODE_EDIT|OB_MODE_POSE, ICON_EDITMODE_HLT);
283                 else
284                         str += sprintf(str, formatstr, "Edit Mode", OB_MODE_EDIT, ICON_EDITMODE_HLT);
285         }
286
287         if (ob->type == OB_MESH) {
288
289                 str += sprintf(str, formatstr, "Sculpt Mode", OB_MODE_SCULPT, ICON_SCULPTMODE_HLT);
290                 str += sprintf(str, formatstr, "Vertex Paint", OB_MODE_VERTEX_PAINT, ICON_VPAINT_HLT);
291                 str += sprintf(str, formatstr, "Texture Paint", OB_MODE_TEXTURE_PAINT, ICON_TPAINT_HLT);
292                 str += sprintf(str, formatstr, "Weight Paint", OB_MODE_WEIGHT_PAINT, ICON_WPAINT_HLT);
293         }
294
295         
296         /* if active object is an armature */
297         if (ob->type==OB_ARMATURE) {
298                 str += sprintf(str, formatstr, "Pose Mode", OB_MODE_POSE, ICON_POSE_HLT);
299         }
300
301         if (ob->particlesystem.first || modifiers_findByType(ob, eModifierType_Cloth) || modifiers_findByType(ob, eModifierType_Softbody)) {
302                 str += sprintf(str, formatstr, "Particle Mode", OB_MODE_PARTICLE_EDIT, ICON_PARTICLEMODE);
303         }
304
305         return (string);
306 }
307
308
309 static void do_view3d_header_buttons(bContext *C, void *arg, int event)
310 {
311         wmWindow *win= CTX_wm_window(C);
312         ToolSettings *ts= CTX_data_tool_settings(C);
313         ScrArea *sa= CTX_wm_area(C);
314         View3D *v3d= sa->spacedata.first;
315         Object *obedit = CTX_data_edit_object(C);
316         EditMesh *em= NULL;
317         int ctrl= win->eventstate->ctrl, shift= win->eventstate->shift;
318         PointerRNA props_ptr;
319         
320         if(obedit && obedit->type==OB_MESH) {
321                 em= BKE_mesh_get_editmesh((Mesh *)obedit->data);
322         }
323         /* watch it: if sa->win does not exist, check that when calling direct drawing routines */
324
325         switch(event) {
326         case B_REDR:
327                 ED_area_tag_redraw(sa);
328                 break;
329                 
330         case B_MODESELECT:
331                 WM_operator_properties_create(&props_ptr, "OBJECT_OT_mode_set");
332                 RNA_enum_set(&props_ptr, "mode", v3d->modeselect);
333                 WM_operator_name_call(C, "OBJECT_OT_mode_set", WM_OP_EXEC_REGION_WIN, &props_ptr);
334                 WM_operator_properties_free(&props_ptr);
335                 break;          
336                 
337         case B_SEL_VERT:
338                 if(em) {
339                         if(shift==0 || em->selectmode==0)
340                                 em->selectmode= SCE_SELECT_VERTEX;
341                         ts->selectmode= em->selectmode;
342                         EM_selectmode_set(em);
343                         WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data);
344                         ED_undo_push(C, "Selectmode Set: Vertex");
345                 }
346                 break;
347         case B_SEL_EDGE:
348                 if(em) {
349                         if(shift==0 || em->selectmode==0){
350                                 if( (em->selectmode ^ SCE_SELECT_EDGE) == SCE_SELECT_VERTEX){
351                                         if(ctrl) EM_convertsel(em, SCE_SELECT_VERTEX,SCE_SELECT_EDGE); 
352                                 }
353                                 em->selectmode = SCE_SELECT_EDGE;
354                         }
355                         ts->selectmode= em->selectmode;
356                         EM_selectmode_set(em);
357                         WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data);
358                         ED_undo_push(C, "Selectmode Set: Edge");
359                 }
360                 break;
361         case B_SEL_FACE:
362                 if(em) {
363                         if( shift==0 || em->selectmode==0){
364                                 if( ((ts->selectmode ^ SCE_SELECT_FACE) == SCE_SELECT_VERTEX) || ((ts->selectmode ^ SCE_SELECT_FACE) == SCE_SELECT_EDGE)){
365                                         if(ctrl) EM_convertsel(em, (ts->selectmode ^ SCE_SELECT_FACE),SCE_SELECT_FACE);
366                                 }
367                                 em->selectmode = SCE_SELECT_FACE;
368                         }
369                         ts->selectmode= em->selectmode;
370                         EM_selectmode_set(em);
371                         WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data);
372                         ED_undo_push(C, "Selectmode Set: Face");
373                 }
374                 break;  
375
376         case B_MAN_TRANS:
377                 if( shift==0 || v3d->twtype==0) {
378                         v3d->twtype= V3D_MANIP_TRANSLATE;
379                 }
380         ED_area_tag_redraw(sa);
381         break;
382         case B_MAN_ROT:
383                 if( shift==0 || v3d->twtype==0) {
384             v3d->twtype= V3D_MANIP_ROTATE;
385                 }
386         ED_area_tag_redraw(sa);
387                 break;
388         case B_MAN_SCALE:
389                 if( shift==0 || v3d->twtype==0) {
390             v3d->twtype= V3D_MANIP_SCALE;
391                 }
392         ED_area_tag_redraw(sa);
393                 break;
394         case B_NDOF:
395         ED_area_tag_redraw(sa);
396                 break;
397         case B_MAN_MODE:
398         ED_area_tag_redraw(sa);
399                 break;
400         default:
401                 break;
402         }
403
404         if(obedit && obedit->type==OB_MESH)
405                 BKE_mesh_end_editmesh(obedit->data, em);
406 }
407
408 /* Returns the icon associated with an object mode */
409 static int object_mode_icon(int mode)
410 {
411         EnumPropertyItem *item = object_mode_items;
412         
413         while(item->name != NULL) {
414                 if(item->value == mode)
415                         return item->icon;
416                 ++item;
417         }
418
419         return ICON_OBJECT_DATAMODE;
420 }
421
422 void uiTemplateHeader3D(uiLayout *layout, struct bContext *C)
423 {
424         bScreen *screen= CTX_wm_screen(C);
425         ScrArea *sa= CTX_wm_area(C);
426         View3D *v3d= sa->spacedata.first;
427         Scene *scene= CTX_data_scene(C);
428         ToolSettings *ts= CTX_data_tool_settings(C);
429         PointerRNA v3dptr, toolsptr, sceneptr;
430         Object *ob= OBACT;
431         Object *obedit = CTX_data_edit_object(C);
432         uiBlock *block;
433         uiLayout *row;
434         
435         RNA_pointer_create(&screen->id, &RNA_Space3DView, v3d, &v3dptr);        
436         RNA_pointer_create(&scene->id, &RNA_ToolSettings, ts, &toolsptr);
437         RNA_pointer_create(&scene->id, &RNA_Scene, scene, &sceneptr);
438
439         block= uiLayoutGetBlock(layout);
440         uiBlockSetHandleFunc(block, do_view3d_header_buttons, NULL);
441
442         /* other buttons: */
443         uiBlockSetEmboss(block, UI_EMBOSS);
444         
445         /* mode */
446         if(ob)
447                 v3d->modeselect = ob->mode;
448         else
449                 v3d->modeselect = OB_MODE_OBJECT;
450                 
451         v3d->flag &= ~V3D_MODE;
452         
453         /* not sure what the v3d->flag is useful for now... modeselect is confusing */
454         if(obedit) v3d->flag |= V3D_EDITMODE;
455         if(ob && (ob->mode & OB_MODE_POSE)) v3d->flag |= V3D_POSEMODE;
456         if(ob && (ob->mode & OB_MODE_VERTEX_PAINT)) v3d->flag |= V3D_VERTEXPAINT;
457         if(ob && (ob->mode & OB_MODE_WEIGHT_PAINT)) v3d->flag |= V3D_WEIGHTPAINT;
458         if(ob && (ob->mode & OB_MODE_TEXTURE_PAINT)) v3d->flag |= V3D_TEXTUREPAINT;
459         if(paint_facesel_test(ob)) v3d->flag |= V3D_FACESELECT;
460
461         uiBlockBeginAlign(block);
462         uiDefIconTextButS(block, MENU, B_MODESELECT, object_mode_icon(v3d->modeselect), view3d_modeselect_pup(scene) , 
463                           0,0,126,20, &(v3d->modeselect), 0, 0, 0, 0, "Mode");
464         uiBlockEndAlign(block);
465         
466         /* Draw type */
467         uiItemR(layout, "", 0, &v3dptr, "viewport_shading", UI_ITEM_R_ICON_ONLY);
468
469         if (obedit==NULL && ((ob && ob->mode & (OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT|OB_MODE_TEXTURE_PAINT)))) {
470                 /* Manipulators aren't used in weight paint mode */
471                 
472                 PointerRNA meshptr;
473
474                 RNA_pointer_create(&ob->id, &RNA_Mesh, ob->data, &meshptr);
475                 uiItemR(layout, "", 0, &meshptr, "use_paint_mask", UI_ITEM_R_ICON_ONLY);
476         } else {
477                 char *str_menu;
478
479                 row= uiLayoutRow(layout, 1);
480                 uiItemR(row, "", 0, &v3dptr, "pivot_point", UI_ITEM_R_ICON_ONLY);
481                 uiItemR(row, "", 0, &v3dptr, "pivot_point_align", UI_ITEM_R_ICON_ONLY);
482
483                 /* NDOF */
484                 /* Not implemented yet
485                  if (G.ndofdevice ==0 ) {
486                         uiDefIconTextButC(block, ICONTEXTROW,B_NDOF, ICON_NDOF_TURN, ndof_pup(), 0,0,XIC+10,YIC, &(v3d->ndofmode), 0, 3.0, 0, 0, "Ndof mode");
487                 
488                         uiDefIconButC(block, TOG, B_NDOF,  ICON_NDOF_DOM,
489                                       0,0,XIC,YIC,
490                                       &v3d->ndoffilter, 0, 1, 0, 0, "dominant axis");   
491                 }
492                  */
493
494                 /* Transform widget / manipulators */
495                 row= uiLayoutRow(layout, 1);
496                 uiItemR(row, "", 0, &v3dptr, "manipulator", UI_ITEM_R_ICON_ONLY);
497                 block= uiLayoutGetBlock(row);
498                 
499                 if(v3d->twflag & V3D_USE_MANIPULATOR) {
500                         uiDefIconButBitS(block, TOG, V3D_MANIP_TRANSLATE, B_MAN_TRANS, ICON_MAN_TRANS, 0,0,XIC,YIC, &v3d->twtype, 1.0, 0.0, 0, 0, "Translate manipulator mode");
501                         uiDefIconButBitS(block, TOG, V3D_MANIP_ROTATE, B_MAN_ROT, ICON_MAN_ROT, 0,0,XIC,YIC, &v3d->twtype, 1.0, 0.0, 0, 0, "Rotate manipulator mode");
502                         uiDefIconButBitS(block, TOG, V3D_MANIP_SCALE, B_MAN_SCALE, ICON_MAN_SCALE, 0,0,XIC,YIC, &v3d->twtype, 1.0, 0.0, 0, 0, "Scale manipulator mode");
503                 }
504                         
505                 if (v3d->twmode > (BIF_countTransformOrientation(C) - 1) + V3D_MANIP_CUSTOM) {
506                         v3d->twmode = 0;
507                 }
508                         
509                 str_menu = BIF_menustringTransformOrientation(C, "Orientation");
510                 uiDefButS(block, MENU, B_MAN_MODE, str_menu,0,0,70,YIC, &v3d->twmode, 0, 0, 0, 0, "Transform Orientation");
511                 MEM_freeN(str_menu);
512         }
513                 
514         if(obedit==NULL && v3d->localvd==NULL) {
515                 int ob_lay = ob ? ob->lay : 0;
516                 
517                 /* Layers */
518                 if (v3d->scenelock)
519                         uiTemplateLayers(layout, &sceneptr, "visible_layers", &v3dptr, "used_layers", ob_lay);
520                 else
521                         uiTemplateLayers(layout, &v3dptr, "visible_layers", &v3dptr, "used_layers", ob_lay);
522
523                 /* Scene lock */
524                 uiItemR(layout, "", 0, &v3dptr, "lock_camera_and_layers", UI_ITEM_R_ICON_ONLY);
525         }
526
527         /* selection modus */
528         if(obedit && (obedit->type == OB_MESH)) {
529                 EditMesh *em= BKE_mesh_get_editmesh((Mesh *)obedit->data);
530
531                 row= uiLayoutRow(layout, 1);
532                 block= uiLayoutGetBlock(row);
533                 uiDefIconButBitS(block, TOG, SCE_SELECT_VERTEX, B_SEL_VERT, ICON_VERTEXSEL, 0,0,XIC,YIC, &em->selectmode, 1.0, 0.0, 0, 0, "Vertex select mode");
534                 uiDefIconButBitS(block, TOG, SCE_SELECT_EDGE, B_SEL_EDGE, ICON_EDGESEL, 0,0,XIC,YIC, &em->selectmode, 1.0, 0.0, 0, 0, "Edge select mode");
535                 uiDefIconButBitS(block, TOG, SCE_SELECT_FACE, B_SEL_FACE, ICON_FACESEL, 0,0,XIC,YIC, &em->selectmode, 1.0, 0.0, 0, 0, "Face select mode");
536
537                 BKE_mesh_end_editmesh(obedit->data, em);
538         }
539 }