72a4133fe750f6b3dc7dba6273644610775cf6aa
[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
125                         uiItemFullR(pa->layout, NULL, 0, op->ptr, prop, -1, 0, 0, 0, 0);
126                 }
127                 RNA_STRUCT_END;
128                 uiBlockEndAlign(block);
129         }
130         uiBlockLayoutResolve(C, block, NULL, &sy);
131         uiEndBlock(C, block);
132         uiDrawBlock(C, block);
133 }
134
135
136 void file_panels_register(ARegionType *art)
137 {
138         PanelType *pt;
139
140         pt= MEM_callocN(sizeof(PanelType), "spacetype file system directories");
141         strcpy(pt->idname, "FILE_PT_system");
142         strcpy(pt->label, "System");
143         pt->draw= file_panel_system;
144         BLI_addtail(&art->paneltypes, pt);
145
146         pt= MEM_callocN(sizeof(PanelType), "spacetype file bookmarks");
147         strcpy(pt->idname, "FILE_PT_bookmarks");
148         strcpy(pt->label, "Bookmarks");
149         pt->draw= file_panel_bookmarks;
150         BLI_addtail(&art->paneltypes, pt);
151
152         pt= MEM_callocN(sizeof(PanelType), "spacetype file recent directories");
153         strcpy(pt->idname, "FILE_PT_recent");
154         strcpy(pt->label, "Recent");
155         pt->draw= file_panel_recent;
156         BLI_addtail(&art->paneltypes, pt);
157
158         pt= MEM_callocN(sizeof(PanelType), "spacetype file operator properties");
159         strcpy(pt->idname, "FILE_PT_operator");
160         strcpy(pt->label, "Operator");
161         pt->draw= file_panel_operator;
162         BLI_addtail(&art->paneltypes, pt);
163 }