351d1619508beff51204a7217eac9a4b4cd7e743
[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 "file_intern.h"
48 #include "fsmenu.h"
49
50 #include <string.h>
51
52 static void do_file_panel_events(bContext *C, void *arg, int event)
53 {
54
55 }
56
57 static void file_panel_category(const bContext *C, Panel *pa, FSMenuCategory category, int icon, int allow_delete)
58 {
59         uiBlock *block;
60         struct FSMenu* fsmenu = fsmenu_get();
61         int nentries = fsmenu_get_nentries(fsmenu, category);
62         int i;
63
64         uiLayoutSetAlignment(pa->layout, UI_LAYOUT_ALIGN_LEFT);
65         block= uiLayoutFreeBlock(pa->layout);
66         uiBlockSetHandleFunc(block, do_file_panel_events, NULL);
67         uiBlockSetEmboss(block, UI_EMBOSSP);
68         uiBlockBeginAlign(block);
69         for (i=0; i< nentries;++i) {
70                 char dir[FILE_MAX];
71                 char temp[FILE_MAX];
72                 uiLayout* layout = uiLayoutRow(pa->layout, UI_LAYOUT_ALIGN_LEFT);
73                 char *entry = fsmenu_get_entry(fsmenu, category, i);
74
75                 /* create nice bookmark name, shows last directory in the full path currently */
76                 BLI_strncpy(temp, entry, FILE_MAX);
77                 BLI_add_slash(temp);
78                 BLI_getlastdir(temp, dir, FILE_MAX);
79                 BLI_del_slash(dir);
80
81                 /* operator shows the short bookmark name, should eventually have tooltip */
82                 uiItemStringO(layout, dir, icon, "FILE_OT_select_bookmark", "dir", entry);
83                 if (allow_delete && fsmenu_can_save(fsmenu, category, i) )
84                         uiItemIntO(layout, "", ICON_X, "FILE_OT_delete_bookmark", "index", i);
85         }
86         uiBlockEndAlign(block);
87 }
88
89 static void file_panel_system(const bContext *C, Panel *pa)
90 {
91         file_panel_category(C, pa, FS_CATEGORY_SYSTEM, ICON_DISK_DRIVE, 0);
92 }
93
94 static void file_panel_bookmarks(const bContext *C, Panel *pa)
95 {
96         file_panel_category(C, pa, FS_CATEGORY_BOOKMARKS, ICON_BOOKMARKS, 1);
97 }
98
99
100 static void file_panel_recent(const bContext *C, Panel *pa)
101 {
102         file_panel_category(C, pa, FS_CATEGORY_RECENT, ICON_FILE_FOLDER, 0);
103 }
104
105
106 static void file_panel_operator(const bContext *C, Panel *pa)
107 {
108         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
109         struct wmOperator *op = sfile ? sfile->op : NULL;
110         uiBlock *block;
111         int sy;
112
113         block= uiLayoutFreeBlock(pa->layout);
114         uiBlockSetHandleFunc(block, do_file_panel_events, NULL);
115
116         sy= 0;
117         if (op) {
118                 uiBlockBeginAlign(block);
119                 RNA_STRUCT_BEGIN(op->ptr, prop) {
120                         if(strcmp(RNA_property_identifier(prop), "rna_type") == 0)
121                                 continue;
122                         if(strcmp(RNA_property_identifier(prop), "filename") == 0)
123                                 continue;
124                         if(strcmp(RNA_property_identifier(prop), "display") == 0)
125                                 continue;
126                         if(strncmp(RNA_property_identifier(prop), "filter", 6) == 0)
127                                 continue;
128                         uiItemFullR(pa->layout, NULL, 0, op->ptr, prop, -1, 0, 0, 0, 0);
129                 }
130                 RNA_STRUCT_END;
131                 uiBlockEndAlign(block);
132         }
133         uiBlockLayoutResolve(C, block, NULL, &sy);
134         uiEndBlock(C, block);
135         uiDrawBlock(C, block);
136 }
137
138
139 void file_panels_register(ARegionType *art)
140 {
141         PanelType *pt;
142
143         pt= MEM_callocN(sizeof(PanelType), "spacetype file system directories");
144         strcpy(pt->idname, "FILE_PT_system");
145         strcpy(pt->label, "System");
146         pt->draw= file_panel_system;
147         BLI_addtail(&art->paneltypes, pt);
148
149         pt= MEM_callocN(sizeof(PanelType), "spacetype file bookmarks");
150         strcpy(pt->idname, "FILE_PT_bookmarks");
151         strcpy(pt->label, "Bookmarks");
152         pt->draw= file_panel_bookmarks;
153         BLI_addtail(&art->paneltypes, pt);
154
155         pt= MEM_callocN(sizeof(PanelType), "spacetype file recent directories");
156         strcpy(pt->idname, "FILE_PT_recent");
157         strcpy(pt->label, "Recent");
158         pt->draw= file_panel_recent;
159         BLI_addtail(&art->paneltypes, pt);
160
161         pt= MEM_callocN(sizeof(PanelType), "spacetype file operator properties");
162         strcpy(pt->idname, "FILE_PT_operator");
163         strcpy(pt->label, "Operator");
164         pt->draw= file_panel_operator;
165         BLI_addtail(&art->paneltypes, pt);
166 }
167