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