Blender  V2.59
view3d_toolbar.c
Go to the documentation of this file.
00001 /*
00002  * $Id: view3d_toolbar.c 39293 2011-08-11 06:06:17Z campbellbarton $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version. 
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * The Original Code is Copyright (C) 2009 Blender Foundation.
00021  * All rights reserved.
00022  *
00023  * 
00024  * Contributor(s): Blender Foundation
00025  *
00026  * ***** END GPL LICENSE BLOCK *****
00027  */
00028 
00034 #include <string.h>
00035 #include <stdio.h>
00036 #include <math.h>
00037 #include <float.h>
00038 
00039 #include "DNA_object_types.h"
00040 #include "DNA_scene_types.h"
00041 
00042 #include "MEM_guardedalloc.h"
00043 
00044 #include "BLI_math.h"
00045 #include "BLI_blenlib.h"
00046 #include "BLI_editVert.h"
00047 #include "BLI_rand.h"
00048 #include "BLI_utildefines.h"
00049 #include "BLI_ghash.h"
00050 
00051 #include "BKE_context.h"
00052 #include "BKE_idprop.h"
00053 #include "BKE_global.h"
00054 #include "BKE_screen.h"
00055 
00056 
00057 #include "WM_api.h"
00058 #include "WM_types.h"
00059 
00060 #include "RNA_access.h"
00061 
00062 #include "ED_screen.h"
00063 #include "ED_util.h"
00064 
00065 #include "UI_interface.h"
00066 #include "UI_resources.h"
00067 
00068 #include "view3d_intern.h"      // own include
00069 
00070 
00071 /* ******************* view3d space & buttons ************** */
00072 
00073 static void view3d_panel_operator_redo_buts(const bContext *C, Panel *pa, wmOperator *op)
00074 {
00075         uiLayoutOperatorButs(C, pa->layout, op, NULL, 'V', 0);
00076 }
00077 
00078 static void view3d_panel_operator_redo_header(const bContext *C, Panel *pa)
00079 {
00080         wmOperator *op= WM_operator_last_redo(C);
00081 
00082         if(op) BLI_strncpy(pa->drawname, op->type->name, sizeof(pa->drawname));
00083         else BLI_strncpy(pa->drawname, "Operator", sizeof(pa->drawname));
00084 }
00085 
00086 static void view3d_panel_operator_redo_operator(const bContext *C, Panel *pa, wmOperator *op)
00087 {
00088         if(op->type->flag & OPTYPE_MACRO) {
00089                 for(op= op->macro.first; op; op= op->next) {
00090                         uiItemL(pa->layout, op->type->name, ICON_NONE);
00091                         view3d_panel_operator_redo_operator(C, pa, op);
00092                 }
00093         }
00094         else {
00095                 view3d_panel_operator_redo_buts(C, pa, op);
00096         }
00097 }
00098 
00099 static void view3d_panel_operator_redo(const bContext *C, Panel *pa)
00100 {
00101         wmOperator *op= WM_operator_last_redo(C);
00102         uiBlock *block;
00103         
00104         if(op==NULL)
00105                 return;
00106         if(WM_operator_poll((bContext*)C, op->type) == 0)
00107                 return;
00108         
00109         block= uiLayoutGetBlock(pa->layout);
00110         
00111         if(ED_undo_valid(C, op->type->name)==0)
00112                 uiLayoutSetEnabled(pa->layout, 0);
00113 
00114         /* note, blockfunc is a default but->func, use Handle func to allow button callbacks too */
00115         uiBlockSetHandleFunc(block, ED_undo_operator_repeat_cb_evt, op);
00116         
00117         view3d_panel_operator_redo_operator(C, pa, op);
00118 }
00119 
00120 /* ******************* */
00121 
00122 typedef struct CustomTool {
00123         struct CustomTool *next, *prev;
00124         char opname[OP_MAX_TYPENAME];
00125         char context[OP_MAX_TYPENAME];
00126 } CustomTool;
00127 
00128 static void operator_call_cb(struct bContext *C, void *arg_listbase, void *arg2)
00129 {
00130         wmOperatorType *ot= arg2;
00131         
00132         if(ot) {
00133                 CustomTool *ct= MEM_callocN(sizeof(CustomTool), "CustomTool");
00134                 
00135                 BLI_addtail(arg_listbase, ct);
00136                 BLI_strncpy(ct->opname, ot->idname, OP_MAX_TYPENAME);
00137                 BLI_strncpy(ct->context, CTX_data_mode_string(C), OP_MAX_TYPENAME);
00138         }
00139                 
00140 }
00141 
00142 static void operator_search_cb(const struct bContext *C, void *UNUSED(arg), const char *str, uiSearchItems *items)
00143 {
00144         GHashIterator *iter= WM_operatortype_iter();
00145 
00146         for( ; !BLI_ghashIterator_isDone(iter); BLI_ghashIterator_step(iter)) {
00147                 wmOperatorType *ot= BLI_ghashIterator_getValue(iter);
00148 
00149                 if(BLI_strcasestr(ot->name, str)) {
00150                         if(WM_operator_poll((bContext*)C, ot)) {
00151                                 
00152                                 if(0==uiSearchItemAdd(items, ot->name, ot, 0))
00153                                         break;
00154                         }
00155                 }
00156         }
00157         BLI_ghashIterator_free(iter);
00158 }
00159 
00160 
00161 /* ID Search browse menu, open */
00162 static uiBlock *tool_search_menu(bContext *C, ARegion *ar, void *arg_listbase)
00163 {
00164         static char search[OP_MAX_TYPENAME];
00165         wmEvent event;
00166         wmWindow *win= CTX_wm_window(C);
00167         uiBlock *block;
00168         uiBut *but;
00169         
00170         /* clear initial search string, then all items show */
00171         search[0]= 0;
00172         
00173         block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
00174         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_RET_1);
00175         
00176         /* fake button, it holds space for search items */
00177         uiDefBut(block, LABEL, 0, "", 10, 15, 150, uiSearchBoxhHeight(), NULL, 0, 0, 0, 0, NULL);
00178         
00179         but= uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, OP_MAX_TYPENAME, 10, 0, 150, 19, 0, 0, "");
00180         uiButSetSearchFunc(but, operator_search_cb, arg_listbase, operator_call_cb, NULL);
00181         
00182         uiBoundsBlock(block, 6);
00183         uiBlockSetDirection(block, UI_DOWN);    
00184         uiEndBlock(C, block);
00185         
00186         event= *(win->eventstate);      /* XXX huh huh? make api call */
00187         event.type= EVT_BUT_OPEN;
00188         event.val= KM_PRESS;
00189         event.customdata= but;
00190         event.customdatafree= FALSE;
00191         wm_event_add(win, &event);
00192         
00193         return block;
00194 }
00195 
00196 
00197 static void view3d_panel_tool_shelf(const bContext *C, Panel *pa)
00198 {
00199         SpaceLink *sl= CTX_wm_space_data(C);
00200         SpaceType *st= NULL;
00201         uiLayout *col;
00202         const char *context= CTX_data_mode_string(C);
00203         
00204         if(sl)
00205                 st= BKE_spacetype_from_id(sl->spacetype);
00206         
00207         if(st && st->toolshelf.first) {
00208                 CustomTool *ct;
00209                 
00210                 for(ct= st->toolshelf.first; ct; ct= ct->next) {
00211                         if(0==strncmp(context, ct->context, OP_MAX_TYPENAME)) {
00212                                 col= uiLayoutColumn(pa->layout, 1);
00213                                 uiItemFullO(col, ct->opname, NULL, ICON_NONE, NULL, WM_OP_INVOKE_REGION_WIN, 0);
00214                         }
00215                 }
00216         }
00217         col= uiLayoutColumn(pa->layout, 1);
00218         uiDefBlockBut(uiLayoutGetBlock(pa->layout), tool_search_menu, &st->toolshelf, "Add Tool", 0, 0, UI_UNIT_X, UI_UNIT_Y, "Add Tool in shelf, gets saved in files");
00219 }
00220 
00221 
00222 void view3d_toolshelf_register(ARegionType *art)
00223 {
00224         PanelType *pt;
00225 
00226         pt= MEM_callocN(sizeof(PanelType), "spacetype view3d panel tools");
00227         strcpy(pt->idname, "VIEW3D_PT_tool_shelf");
00228         strcpy(pt->label, "Tool Shelf");
00229         pt->draw= view3d_panel_tool_shelf;
00230         BLI_addtail(&art->paneltypes, pt);
00231 }
00232 
00233 void view3d_tool_props_register(ARegionType *art)
00234 {
00235         PanelType *pt;
00236         
00237         pt= MEM_callocN(sizeof(PanelType), "spacetype view3d panel last operator");
00238         strcpy(pt->idname, "VIEW3D_PT_last_operator");
00239         strcpy(pt->label, "Operator");
00240         pt->draw_header= view3d_panel_operator_redo_header;
00241         pt->draw= view3d_panel_operator_redo;
00242         BLI_addtail(&art->paneltypes, pt);
00243 }
00244 
00245 /* ********** operator to open/close toolshelf region */
00246 
00247 static int view3d_toolshelf(bContext *C, wmOperator *UNUSED(op))
00248 {
00249         ScrArea *sa= CTX_wm_area(C);
00250         ARegion *ar= view3d_has_tools_region(sa);
00251         
00252         if(ar)
00253                 ED_region_toggle_hidden(C, ar);
00254 
00255         return OPERATOR_FINISHED;
00256 }
00257 
00258 void VIEW3D_OT_toolshelf(wmOperatorType *ot)
00259 {
00260         ot->name= "Tool Shelf";
00261         ot->description= "Toggles tool shelf display";
00262         ot->idname= "VIEW3D_OT_toolshelf";
00263         
00264         ot->exec= view3d_toolshelf;
00265         ot->poll= ED_operator_view3d_active;
00266         
00267         /* flags */
00268         ot->flag= 0;
00269 }