Draw function for operators (just like panels), used for the redo popup, file selecto...
[blender-staging.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, int reverse)
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, i_iter, 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_iter=0; i_iter< nentries;++i_iter) {
90                 char dir[FILE_MAX];
91                 char temp[FILE_MAX];
92                 uiLayout* layout = uiLayoutRow(col, 0);
93                 char *entry;
94
95                 i= reverse ? nentries-(i_iter+1) : i_iter;
96                 
97                 entry = fsmenu_get_entry(fsmenu, category, i);
98                 
99                 /* set this list item as active if we have a match */
100                 if(strcmp(curdir, entry) == 0)
101                         *nr= i;
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, 0);
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                 file_panel_category(C, pa, FS_CATEGORY_RECENT, &sfile->recentnr, ICON_FILE_FOLDER, 0, 1);
154 }
155
156
157 static int file_panel_operator_poll(const bContext *C, PanelType *pt)
158 {
159         SpaceFile *sfile= CTX_wm_space_file(C);
160         return (sfile && sfile->op);
161 }
162
163 static void file_panel_operator_header(const bContext *C, Panel *pa)
164 {
165         SpaceFile *sfile= CTX_wm_space_file(C);
166         wmOperator *op= sfile->op;
167
168         BLI_strncpy(pa->drawname, op->type->name, sizeof(pa->drawname));
169 }
170
171 static void file_panel_operator(const bContext *C, Panel *pa)
172 {
173         SpaceFile *sfile= CTX_wm_space_file(C);
174         wmOperator *op= sfile->op;
175         int empty= 1, flag;
176
177         if(op->type->ui) {
178                 op->type->ui((bContext*)C, op, pa->layout);
179         }
180         else {
181                 RNA_STRUCT_BEGIN(op->ptr, prop) {
182                         flag= RNA_property_flag(prop);
183
184                         if(flag & PROP_HIDDEN)
185                                 continue;
186                         if(strcmp(RNA_property_identifier(prop), "path") == 0)
187                                 continue;
188                         if(strcmp(RNA_property_identifier(prop), "directory") == 0)
189                                 continue;
190                         if(strcmp(RNA_property_identifier(prop), "filename") == 0)
191                                 continue;
192
193                         uiItemFullR(pa->layout, NULL, 0, op->ptr, prop, -1, 0, 0);
194                         empty= 0;
195                 }
196                 RNA_STRUCT_END;
197
198                 if(empty)
199                         uiItemL(pa->layout, "No properties.", 0);
200         }
201 }
202
203 void file_panels_register(ARegionType *art)
204 {
205         PanelType *pt;
206
207         pt= MEM_callocN(sizeof(PanelType), "spacetype file system directories");
208         strcpy(pt->idname, "FILE_PT_system");
209         strcpy(pt->label, "System");
210         pt->draw= file_panel_system;
211         BLI_addtail(&art->paneltypes, pt);
212
213         pt= MEM_callocN(sizeof(PanelType), "spacetype file bookmarks");
214         strcpy(pt->idname, "FILE_PT_bookmarks");
215         strcpy(pt->label, "Bookmarks");
216         pt->draw= file_panel_bookmarks;
217         BLI_addtail(&art->paneltypes, pt);
218
219         pt= MEM_callocN(sizeof(PanelType), "spacetype file recent directories");
220         strcpy(pt->idname, "FILE_PT_recent");
221         strcpy(pt->label, "Recent");
222         pt->draw= file_panel_recent;
223         BLI_addtail(&art->paneltypes, pt);
224
225         pt= MEM_callocN(sizeof(PanelType), "spacetype file operator properties");
226         strcpy(pt->idname, "FILE_PT_operator");
227         strcpy(pt->label, "Operator");
228         pt->poll= file_panel_operator_poll;
229         pt->draw_header= file_panel_operator_header;
230         pt->draw= file_panel_operator;
231         BLI_addtail(&art->paneltypes, pt);
232 }
233