Blender V2.61 - r43446

file_panels.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, Andrea Weikert
00023  *
00024  * ***** END GPL LICENSE BLOCK *****
00025  */
00026 
00032 #include "BKE_context.h"
00033 #include "BKE_screen.h"
00034 
00035 #include "BLI_blenlib.h"
00036 #include "BLI_utildefines.h"
00037 
00038 #include "BLF_translation.h"
00039 
00040 #include "DNA_screen_types.h"
00041 #include "DNA_space_types.h"
00042 #include "DNA_userdef_types.h"
00043 
00044 #include "MEM_guardedalloc.h"
00045 
00046 #include "RNA_access.h"
00047 
00048 #include "UI_interface.h"
00049 #include "UI_resources.h"
00050 
00051 #include "WM_api.h"
00052 #include "WM_types.h"
00053 
00054 #include "file_intern.h"
00055 #include "fsmenu.h"
00056 
00057 #include <string.h>
00058 
00059 static void file_panel_cb(bContext *C, void *arg_entry, void *UNUSED(arg_v))
00060 {
00061     PointerRNA ptr;
00062     char *entry= (char*)arg_entry;
00063 
00064     WM_operator_properties_create(&ptr, "FILE_OT_select_bookmark");
00065     RNA_string_set(&ptr, "dir", entry);
00066     WM_operator_name_call(C, "FILE_OT_select_bookmark", WM_OP_INVOKE_REGION_WIN, &ptr);
00067     WM_operator_properties_free(&ptr);
00068 }
00069 
00070 static void file_panel_category(const bContext *C, Panel *pa, FSMenuCategory category, short *nr, int icon, int allow_delete, int reverse)
00071 {
00072     SpaceFile *sfile= CTX_wm_space_file(C);
00073     uiBlock *block;
00074     uiBut *but;
00075     uiLayout *box, *col;
00076     struct FSMenu* fsmenu = fsmenu_get();
00077     int i, i_iter, nentries = fsmenu_get_nentries(fsmenu, category);
00078 
00079     /* reset each time */
00080     *nr= -1;
00081 
00082     /* hide if no entries */
00083     if(nentries == 0)
00084         return;
00085 
00086     /* layout */
00087     uiLayoutSetAlignment(pa->layout, UI_LAYOUT_ALIGN_LEFT);
00088     block= uiLayoutGetBlock(pa->layout);
00089     box= uiLayoutBox(pa->layout);
00090     col= uiLayoutColumn(box, 1);
00091 
00092     for (i_iter=0; i_iter< nentries;++i_iter) {
00093         char dir[FILE_MAX];
00094         char temp[FILE_MAX];
00095         uiLayout* layout = uiLayoutRow(col, 0);
00096         char *entry;
00097 
00098         i= reverse ? nentries-(i_iter+1) : i_iter;
00099         
00100         entry = fsmenu_get_entry(fsmenu, category, i);
00101         
00102         /* set this list item as active if we have a match */
00103         if(sfile->params) {
00104             if(BLI_path_cmp(sfile->params->dir, entry) == 0) {
00105                 *nr= i;
00106             }
00107         }
00108 
00109         /* create nice bookmark name, shows last directory in the full path currently */
00110         BLI_strncpy(temp, entry, FILE_MAX);
00111         BLI_add_slash(temp);
00112         BLI_getlastdir(temp, dir, FILE_MAX);
00113         BLI_del_slash(dir);
00114 
00115         if(dir[0] == 0)
00116             BLI_strncpy(dir, entry, FILE_MAX);
00117 
00118         /* create list item */
00119         but= uiDefIconTextButS(block, LISTROW, 0, icon, dir, 0,0,UI_UNIT_X*10,UI_UNIT_Y, nr, 0, i, 0, 0, entry);
00120         uiButSetFunc(but, file_panel_cb, entry, NULL);
00121         uiButSetFlag(but, UI_ICON_LEFT|UI_TEXT_LEFT);
00122 
00123         /* create delete button */
00124         if(allow_delete && fsmenu_can_save(fsmenu, category, i)) {
00125             uiBlockSetEmboss(block, UI_EMBOSSN);
00126             uiItemIntO(layout, "", ICON_X, "FILE_OT_delete_bookmark", "index", i);
00127             uiBlockSetEmboss(block, UI_EMBOSS);
00128         }
00129     }
00130 }
00131 
00132 static void file_panel_system(const bContext *C, Panel *pa)
00133 {
00134     SpaceFile *sfile= CTX_wm_space_file(C);
00135 
00136     if(sfile)
00137         file_panel_category(C, pa, FS_CATEGORY_SYSTEM, &sfile->systemnr, ICON_DISK_DRIVE, 0, 0);
00138 }
00139 
00140 static void file_panel_bookmarks(const bContext *C, Panel *pa)
00141 {
00142     SpaceFile *sfile= CTX_wm_space_file(C);
00143     uiLayout *row;
00144 
00145     if(sfile) {
00146         row= uiLayoutRow(pa->layout, 0);
00147         uiItemO(row, IFACE_("Add"), ICON_ZOOMIN, "file.bookmark_add");
00148         uiItemL(row, NULL, ICON_NONE);
00149 
00150         file_panel_category(C, pa, FS_CATEGORY_BOOKMARKS, &sfile->bookmarknr, ICON_BOOKMARKS, 1, 0);
00151     }
00152 }
00153 
00154 static void file_panel_recent(const bContext *C, Panel *pa)
00155 {
00156     SpaceFile *sfile= CTX_wm_space_file(C);
00157 
00158     if(sfile) {
00159         if ( !(U.uiflag & USER_HIDE_RECENT) ) {
00160             file_panel_category(C, pa, FS_CATEGORY_RECENT, &sfile->recentnr, ICON_FILE_FOLDER, 0, 1);
00161         }
00162     }
00163 }
00164 
00165 
00166 static int file_panel_operator_poll(const bContext *C, PanelType *UNUSED(pt))
00167 {
00168     SpaceFile *sfile= CTX_wm_space_file(C);
00169     return (sfile && sfile->op);
00170 }
00171 
00172 static void file_panel_operator_header(const bContext *C, Panel *pa)
00173 {
00174     SpaceFile *sfile= CTX_wm_space_file(C);
00175     wmOperator *op= sfile->op;
00176 
00177     BLI_strncpy(pa->drawname, op->type->name, sizeof(pa->drawname));
00178 }
00179 
00180 static int file_panel_check_prop(PointerRNA *UNUSED(ptr), PropertyRNA *prop)
00181 {
00182     const char *prop_id= RNA_property_identifier(prop);
00183     return !(   strcmp(prop_id, "filepath") == 0 ||
00184                 strcmp(prop_id, "directory") == 0 ||
00185                 strcmp(prop_id, "filename") == 0
00186     );
00187 }
00188 
00189 static void file_panel_operator(const bContext *C, Panel *pa)
00190 {
00191     SpaceFile *sfile= CTX_wm_space_file(C);
00192     wmOperator *op= sfile->op;
00193     // int empty= 1, flag;
00194     
00195     uiBlockSetFunc(uiLayoutGetBlock(pa->layout), file_draw_check_cb, NULL, NULL);
00196 
00197     uiLayoutOperatorButs(C, pa->layout, op, file_panel_check_prop, '\0', UI_LAYOUT_OP_SHOW_EMPTY);
00198 
00199     uiBlockSetFunc(uiLayoutGetBlock(pa->layout), NULL, NULL, NULL);
00200 }
00201 
00202 void file_panels_register(ARegionType *art)
00203 {
00204     PanelType *pt;
00205 
00206     pt= MEM_callocN(sizeof(PanelType), "spacetype file system directories");
00207     strcpy(pt->idname, "FILE_PT_system");
00208     strcpy(pt->label, "System");
00209     pt->draw= file_panel_system;
00210     BLI_addtail(&art->paneltypes, pt);
00211 
00212     pt= MEM_callocN(sizeof(PanelType), "spacetype file bookmarks");
00213     strcpy(pt->idname, "FILE_PT_bookmarks");
00214     strcpy(pt->label, "Bookmarks");
00215     pt->draw= file_panel_bookmarks;
00216     BLI_addtail(&art->paneltypes, pt);
00217 
00218     pt= MEM_callocN(sizeof(PanelType), "spacetype file recent directories");
00219     strcpy(pt->idname, "FILE_PT_recent");
00220     strcpy(pt->label, "Recent");
00221     pt->draw= file_panel_recent;
00222     BLI_addtail(&art->paneltypes, pt);
00223 
00224     pt= MEM_callocN(sizeof(PanelType), "spacetype file operator properties");
00225     strcpy(pt->idname, "FILE_PT_operator");
00226     strcpy(pt->label, "Operator");
00227     pt->poll= file_panel_operator_poll;
00228     pt->draw_header= file_panel_operator_header;
00229     pt->draw= file_panel_operator;
00230     BLI_addtail(&art->paneltypes, pt);
00231 }
00232