2.5: File browser
[blender.git] / source / blender / editors / space_file / file_panels.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) 2009 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation, Andrea Weikert
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include "BKE_context.h"
30 #include "BKE_screen.h"
31
32 #include "BLI_blenlib.h"
33
34 #include "DNA_screen_types.h"
35 #include "DNA_space_types.h"
36 #include "DNA_userdef_types.h"
37 #include "DNA_windowmanager_types.h"
38
39 #include "MEM_guardedalloc.h"
40
41 #include "RNA_access.h"
42
43 #include "UI_interface.h"
44 #include "UI_resources.h"
45 #include "UI_view2d.h"
46
47 #include "WM_api.h"
48 #include "WM_types.h"
49
50 #include "file_intern.h"
51 #include "fsmenu.h"
52
53 #include <string.h>
54
55 static void file_panel_cb(bContext *C, void *arg_entry, void *arg_unused)
56 {
57         PointerRNA ptr;
58         char *entry= (char*)arg_entry;
59
60         WM_operator_properties_create(&ptr, "FILE_OT_select_bookmark");
61         RNA_string_set(&ptr, "dir", entry);
62         WM_operator_name_call(C, "FILE_OT_select_bookmark", WM_OP_INVOKE_REGION_WIN, &ptr);
63         WM_operator_properties_free(&ptr);
64 }
65
66 static void file_panel_category(const bContext *C, Panel *pa, FSMenuCategory category, short *nr, int icon, int allow_delete)
67 {
68         SpaceFile *sfile= CTX_wm_space_file(C);
69         uiBlock *block;
70         uiBut *but;
71         uiLayout *box, *col;
72         struct FSMenu* fsmenu = fsmenu_get();
73         char *curdir= (sfile->params)? sfile->params->dir: "";
74         int i, nentries = fsmenu_get_nentries(fsmenu, category);
75
76         /* reset each time */
77         *nr= -1;
78
79         /* hide if no entries */
80         if(nentries == 0)
81                 return;
82
83         /* layout */
84         uiLayoutSetAlignment(pa->layout, UI_LAYOUT_ALIGN_LEFT);
85         block= uiLayoutGetBlock(pa->layout);
86         box= uiLayoutBox(pa->layout);
87         col= uiLayoutColumn(box, 1);
88
89         for (i=0; i< nentries;++i) {
90                 char dir[FILE_MAX];
91                 char temp[FILE_MAX];
92                 uiLayout* layout = uiLayoutRow(col, 0);
93                 char *entry = fsmenu_get_entry(fsmenu, category, i);
94
95                 /* set this list item as active if we have a match */
96                 if(strcmp(curdir, entry) == 0)
97                         *nr= i;
98
99                 /* create nice bookmark name, shows last directory in the full path currently */
100                 BLI_strncpy(temp, entry, FILE_MAX);
101                 BLI_add_slash(temp);
102                 BLI_getlastdir(temp, dir, FILE_MAX);
103                 BLI_del_slash(dir);
104
105                 if(dir[0] == 0)
106                         BLI_strncpy(dir, entry, FILE_MAX);
107
108                 /* create list item */
109                 but= uiDefIconTextButS(block, LISTROW, 0, icon, dir, 0,0,UI_UNIT_X*10,UI_UNIT_Y, nr, 0, i, 0, 0, entry);
110                 uiButSetFunc(but, file_panel_cb, entry, NULL);
111                 uiButSetFlag(but, UI_ICON_LEFT|UI_TEXT_LEFT);
112
113                 /* create delete button */
114                 if(allow_delete && fsmenu_can_save(fsmenu, category, i)) {
115                         uiBlockSetEmboss(block, UI_EMBOSSN);
116                         uiItemIntO(layout, "", ICON_X, "FILE_OT_delete_bookmark", "index", i);
117                         uiBlockSetEmboss(block, UI_EMBOSS);
118                 }
119         }
120 }
121
122 static void file_panel_system(const bContext *C, Panel *pa)
123 {
124         SpaceFile *sfile= CTX_wm_space_file(C);
125
126         if(sfile)
127                 file_panel_category(C, pa, FS_CATEGORY_SYSTEM, &sfile->systemnr, ICON_DISK_DRIVE, 0);
128 }
129
130 static void file_panel_bookmarks(const bContext *C, Panel *pa)
131 {
132         SpaceFile *sfile= CTX_wm_space_file(C);
133         uiLayout *row;
134
135         if(sfile) {
136                 row= uiLayoutRow(pa->layout, 0);
137                 uiItemO(row, "Add", ICON_ZOOMIN, "file.add_bookmark");
138                 uiItemL(row, NULL, 0);
139
140                 file_panel_category(C, pa, FS_CATEGORY_BOOKMARKS, &sfile->bookmarknr, ICON_BOOKMARKS, 1);
141         }
142 }
143
144 static void file_panel_recent(const bContext *C, Panel *pa)
145 {
146         SpaceFile *sfile= CTX_wm_space_file(C);
147
148         if(sfile)
149                 file_panel_category(C, pa, FS_CATEGORY_RECENT, &sfile->recentnr, ICON_FILE_FOLDER, 0);
150 }
151
152
153 static int file_panel_operator_poll(const bContext *C, PanelType *pt)
154 {
155         SpaceFile *sfile= CTX_wm_space_file(C);
156         return (sfile && sfile->op);
157 }
158
159 static void file_panel_operator_header(const bContext *C, Panel *pa)
160 {
161         SpaceFile *sfile= CTX_wm_space_file(C);
162         wmOperator *op= sfile->op;
163
164         BLI_strncpy(pa->drawname, op->type->name, sizeof(pa->drawname));
165 }
166
167 static void file_panel_operator(const bContext *C, Panel *pa)
168 {
169         SpaceFile *sfile= CTX_wm_space_file(C);
170         wmOperator *op= sfile->op;
171         int empty= 1;
172
173         RNA_STRUCT_BEGIN(op->ptr, prop) {
174                 if(strcmp(RNA_property_identifier(prop), "rna_type") == 0)
175                         continue;
176                 if(strcmp(RNA_property_identifier(prop), "filename") == 0)
177                         continue;
178                 if(strcmp(RNA_property_identifier(prop), "display") == 0)
179                         continue;
180                 if(strncmp(RNA_property_identifier(prop), "filter", 6) == 0)
181                         continue;
182
183                 uiItemFullR(pa->layout, NULL, 0, op->ptr, prop, -1, 0, 0, 0, 0);
184                 empty= 0;
185         }
186         RNA_STRUCT_END;
187
188         if(empty)
189                 uiItemL(pa->layout, "No properties.", 0);
190 }
191
192 void file_panels_register(ARegionType *art)
193 {
194         PanelType *pt;
195
196         pt= MEM_callocN(sizeof(PanelType), "spacetype file system directories");
197         strcpy(pt->idname, "FILE_PT_system");
198         strcpy(pt->label, "System");
199         pt->draw= file_panel_system;
200         BLI_addtail(&art->paneltypes, pt);
201
202         pt= MEM_callocN(sizeof(PanelType), "spacetype file bookmarks");
203         strcpy(pt->idname, "FILE_PT_bookmarks");
204         strcpy(pt->label, "Bookmarks");
205         pt->draw= file_panel_bookmarks;
206         BLI_addtail(&art->paneltypes, pt);
207
208         pt= MEM_callocN(sizeof(PanelType), "spacetype file recent directories");
209         strcpy(pt->idname, "FILE_PT_recent");
210         strcpy(pt->label, "Recent");
211         pt->draw= file_panel_recent;
212         BLI_addtail(&art->paneltypes, pt);
213
214         pt= MEM_callocN(sizeof(PanelType), "spacetype file operator properties");
215         strcpy(pt->idname, "FILE_PT_operator");
216         strcpy(pt->label, "Operator");
217         pt->poll= file_panel_operator_poll;
218         pt->draw_header= file_panel_operator_header;
219         pt->draw= file_panel_operator;
220         BLI_addtail(&art->paneltypes, pt);
221 }
222