Code Cleanup: style and redundant casts
[blender.git] / source / blender / editors / space_file / file_panels.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2009 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Blender Foundation, Andrea Weikert
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_file/file_panels.c
28  *  \ingroup spfile
29  */
30
31 #include "BLI_blenlib.h"
32 #include "BLI_utildefines.h"
33
34 #include "BKE_context.h"
35 #include "BKE_screen.h"
36
37 #include "BLF_translation.h"
38
39 #include "DNA_screen_types.h"
40 #include "DNA_space_types.h"
41 #include "DNA_userdef_types.h"
42
43 #include "MEM_guardedalloc.h"
44
45 #include "RNA_access.h"
46
47 #include "UI_interface.h"
48 #include "UI_resources.h"
49
50 #include "WM_api.h"
51 #include "WM_types.h"
52
53 #include "file_intern.h"
54 #include "fsmenu.h"
55
56 #include <string.h>
57
58 static void file_panel_cb(bContext *C, void *arg_entry, void *UNUSED(arg_v))
59 {
60         PointerRNA ptr;
61         char *entry = (char *)arg_entry;
62
63         WM_operator_properties_create(&ptr, "FILE_OT_select_bookmark");
64         RNA_string_set(&ptr, "dir", entry);
65         WM_operator_name_call(C, "FILE_OT_select_bookmark", WM_OP_INVOKE_REGION_WIN, &ptr);
66         WM_operator_properties_free(&ptr);
67 }
68
69 static void file_panel_category(const bContext *C, Panel *pa, FSMenuCategory category, short *nr, int icon, int allow_delete)
70 {
71         SpaceFile *sfile = CTX_wm_space_file(C);
72         uiBlock *block;
73         uiBut *but;
74         uiLayout *box, *col;
75         struct FSMenu *fsmenu = fsmenu_get();
76         int i, nentries = fsmenu_get_nentries(fsmenu, category);
77
78         /* reset each time */
79         *nr = -1;
80
81         /* hide if no entries */
82         if (nentries == 0)
83                 return;
84
85         /* layout */
86         uiLayoutSetAlignment(pa->layout, UI_LAYOUT_ALIGN_LEFT);
87         block = uiLayoutGetBlock(pa->layout);
88         box = uiLayoutBox(pa->layout);
89         col = uiLayoutColumn(box, TRUE);
90
91         for (i = 0; i < nentries; ++i) {
92                 char dir[FILE_MAX];
93                 char temp[FILE_MAX];
94                 uiLayout *layout = uiLayoutRow(col, FALSE);
95                 char *entry;
96                 
97                 entry = fsmenu_get_entry(fsmenu, category, i);
98                 
99                 /* set this list item as active if we have a match */
100                 if (sfile->params) {
101                         if (BLI_path_cmp(sfile->params->dir, entry) == 0) {
102                                 *nr = i;
103                         }
104                 }
105
106                 /* create nice bookmark name, shows last directory in the full path currently */
107                 BLI_strncpy(temp, entry, FILE_MAX);
108                 BLI_add_slash(temp);
109                 BLI_getlastdir(temp, dir, FILE_MAX);
110                 BLI_del_slash(dir);
111
112                 if (dir[0] == 0)
113                         BLI_strncpy(dir, entry, FILE_MAX);
114
115                 /* create list item */
116                 but = uiDefIconTextButS(block, LISTROW, 0, icon, dir, 0, 0, UI_UNIT_X * 10, UI_UNIT_Y, nr, 0, i, 0, 0, entry);
117                 uiButSetFunc(but, file_panel_cb, entry, NULL);
118                 uiButClearFlag(but, UI_BUT_UNDO); /* skip undo on screen buttons */
119                 uiButSetDrawFlag(but, UI_BUT_ICON_LEFT | UI_BUT_TEXT_LEFT);
120
121                 /* create delete button */
122                 if (allow_delete && fsmenu_can_save(fsmenu, category, i)) {
123                         uiBlockSetEmboss(block, UI_EMBOSSN);
124                         uiItemIntO(layout, "", ICON_X, "FILE_OT_bookmark_delete", "index", i);
125                         uiBlockSetEmboss(block, UI_EMBOSS);
126                 }
127         }
128 }
129
130 static void file_panel_system(const bContext *C, Panel *pa)
131 {
132         SpaceFile *sfile = CTX_wm_space_file(C);
133
134         if (sfile)
135                 file_panel_category(C, pa, FS_CATEGORY_SYSTEM, &sfile->systemnr, ICON_DISK_DRIVE, 0);
136 }
137
138 static void file_panel_system_bookmarks(const bContext *C, Panel *pa)
139 {
140         SpaceFile *sfile = CTX_wm_space_file(C);
141
142         if (sfile && !(U.uiflag & USER_HIDE_SYSTEM_BOOKMARKS)) {
143                 file_panel_category(C, pa, FS_CATEGORY_SYSTEM_BOOKMARKS, &sfile->systemnr, ICON_BOOKMARKS, 0);
144         }
145
146 }
147
148 static void file_panel_bookmarks(const bContext *C, Panel *pa)
149 {
150         SpaceFile *sfile = CTX_wm_space_file(C);
151         uiLayout *row;
152
153         if (sfile) {
154                 row = uiLayoutRow(pa->layout, FALSE);
155                 uiItemO(row, IFACE_("Add"), ICON_ZOOMIN, "file.bookmark_add");
156                 uiItemL(row, NULL, ICON_NONE);
157
158                 file_panel_category(C, pa, FS_CATEGORY_BOOKMARKS, &sfile->bookmarknr, ICON_BOOKMARKS, 1);
159         }
160 }
161
162 static void file_panel_recent(const bContext *C, Panel *pa)
163 {
164         SpaceFile *sfile = CTX_wm_space_file(C);
165         uiLayout *row;
166
167         if (sfile) {
168                 if (!(U.uiflag & USER_HIDE_RECENT)) {
169                         row = uiLayoutRow(pa->layout, FALSE);
170                         uiItemO(row, IFACE_("Reset"), ICON_X, "file.reset_recent");
171                         uiItemL(row, NULL, ICON_NONE);
172
173                         file_panel_category(C, pa, FS_CATEGORY_RECENT, &sfile->recentnr, ICON_FILE_FOLDER, 0);
174                 }
175         }
176 }
177
178
179 static int file_panel_operator_poll(const bContext *C, PanelType *UNUSED(pt))
180 {
181         SpaceFile *sfile = CTX_wm_space_file(C);
182         return (sfile && sfile->op);
183 }
184
185 static void file_panel_operator_header(const bContext *C, Panel *pa)
186 {
187         SpaceFile *sfile = CTX_wm_space_file(C);
188         wmOperator *op = sfile->op;
189
190         BLI_strncpy(pa->drawname, RNA_struct_ui_name(op->type->srna), sizeof(pa->drawname));
191 }
192
193 static bool file_panel_check_prop(PointerRNA *UNUSED(ptr), PropertyRNA *prop)
194 {
195         const char *prop_id = RNA_property_identifier(prop);
196         return !(STREQ(prop_id, "filepath") ||
197                  STREQ(prop_id, "directory") ||
198                  STREQ(prop_id, "filename")
199                  );
200 }
201
202 static void file_panel_operator(const bContext *C, Panel *pa)
203 {
204         SpaceFile *sfile = CTX_wm_space_file(C);
205         wmOperator *op = sfile->op;
206         // int empty = 1, flag;
207         
208         uiBlockSetFunc(uiLayoutGetBlock(pa->layout), file_draw_check_cb, NULL, NULL);
209
210         uiLayoutOperatorButs(C, pa->layout, op, file_panel_check_prop, '\0', UI_LAYOUT_OP_SHOW_EMPTY);
211
212         uiBlockSetFunc(uiLayoutGetBlock(pa->layout), NULL, NULL, NULL);
213 }
214
215 void file_panels_register(ARegionType *art)
216 {
217         PanelType *pt;
218
219         pt = MEM_callocN(sizeof(PanelType), "spacetype file system directories");
220         strcpy(pt->idname, "FILE_PT_system");
221         strcpy(pt->label, N_("System"));
222         strcpy(pt->translation_context, BLF_I18NCONTEXT_DEFAULT_BPYRNA);
223         pt->draw = file_panel_system;
224         BLI_addtail(&art->paneltypes, pt);
225
226         pt = MEM_callocN(sizeof(PanelType), "spacetype file system bookmarks");
227         strcpy(pt->idname, "FILE_PT_system_bookmarks");
228         strcpy(pt->label, N_("System Bookmarks"));
229         strcpy(pt->translation_context, BLF_I18NCONTEXT_DEFAULT_BPYRNA);
230         pt->draw = file_panel_system_bookmarks;
231         BLI_addtail(&art->paneltypes, pt);
232
233         pt = MEM_callocN(sizeof(PanelType), "spacetype file bookmarks");
234         strcpy(pt->idname, "FILE_PT_bookmarks");
235         strcpy(pt->label, N_("Bookmarks"));
236         strcpy(pt->translation_context, BLF_I18NCONTEXT_DEFAULT_BPYRNA);
237         pt->draw = file_panel_bookmarks;
238         BLI_addtail(&art->paneltypes, pt);
239
240         pt = MEM_callocN(sizeof(PanelType), "spacetype file recent directories");
241         strcpy(pt->idname, "FILE_PT_recent");
242         strcpy(pt->label, N_("Recent"));
243         strcpy(pt->translation_context, BLF_I18NCONTEXT_DEFAULT_BPYRNA);
244         pt->draw = file_panel_recent;
245         BLI_addtail(&art->paneltypes, pt);
246
247         pt = MEM_callocN(sizeof(PanelType), "spacetype file operator properties");
248         strcpy(pt->idname, "FILE_PT_operator");
249         strcpy(pt->label, N_("Operator"));
250         strcpy(pt->translation_context, BLF_I18NCONTEXT_DEFAULT_BPYRNA);
251         pt->poll = file_panel_operator_poll;
252         pt->draw_header = file_panel_operator_header;
253         pt->draw = file_panel_operator;
254         BLI_addtail(&art->paneltypes, pt);
255 }
256