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