Blender V2.61 - r43446

view3d_toolbar.c

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