2.5: Headers
[blender.git] / source / blender / editors / space_text / text_header.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) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdio.h>
32
33 /* file time checking */
34 #include <ctype.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37
38 #ifndef _WIN32
39 #include <unistd.h>
40 #else
41 #include <io.h>
42 #include "BLI_winstuff.h"
43 #endif
44
45 #include "DNA_space_types.h"
46 #include "DNA_scene_types.h"
47 #include "DNA_screen_types.h"
48 #include "DNA_text_types.h"
49 #include "DNA_windowmanager_types.h"
50
51 #include "MEM_guardedalloc.h"
52
53 #include "BLI_blenlib.h"
54
55 #include "BKE_context.h"
56 #include "BKE_global.h"
57 #include "BKE_library.h"
58 #include "BKE_main.h"
59 #include "BKE_scene.h"
60 #include "BKE_screen.h"
61 #include "BKE_text.h"
62
63 #include "ED_screen.h"
64 #include "ED_types.h"
65 #include "ED_util.h"
66
67 #include "WM_api.h"
68 #include "WM_types.h"
69
70 #include "BIF_gl.h"
71 #include "BIF_glutil.h"
72
73 #include "UI_interface.h"
74 #include "UI_resources.h"
75 #include "UI_view2d.h"
76
77 #include "RNA_access.h"
78
79 #ifndef DISABLE_PYTHON
80 #include "BPY_extern.h"
81 // XXX #include "BPY_menus.h"
82 #endif
83
84 #include "text_intern.h"
85
86 #define HEADER_PATH_MAX 260
87
88 /* ************************ header area region *********************** */
89
90 #ifndef DISABLE_PYTHON
91 #if 0
92 static void do_text_template_scriptsmenu(bContext *C, void *arg, int event)
93 {
94         // XXX BPY_menu_do_python(PYMENU_SCRIPTTEMPLATE, event);
95 }
96
97 static uiBlock *text_template_scriptsmenu(bContext *C, void *args_unused)
98 {
99         ARegion *ar= CTX_wm_region(C);
100         uiBlock *block;
101         // XXX BPyMenu *pym;
102         // int i= 0;
103         // short yco = 20, menuwidth = 120;
104         
105         block= uiBeginBlock(C, ar, "text_template_scriptsmenu", UI_EMBOSSP);
106         uiBlockSetButmFunc(block, do_text_template_scriptsmenu, NULL);
107         
108         /* note that we acount for the N previous entries with i+20: */
109         /* XXX for (pym = BPyMenuTable[PYMENU_SCRIPTTEMPLATE]; pym; pym = pym->next, i++) {
110                 
111                 uiDefIconTextBut(block, BUTM, 1, ICON_PYTHON, pym->name, 0, yco-=20, menuwidth, 19, 
112                                                  NULL, 0.0, 0.0, 1, i, 
113                                                  pym->tooltip?pym->tooltip:pym->filename);
114         }*/
115         
116         uiBlockSetDirection(block, UI_RIGHT);
117         uiTextBoundsBlock(block, 60);
118
119         uiEndBlock(C, block);
120         uiDrawBlock(C, block);
121         
122         return block;
123 }
124
125 static void do_text_plugin_scriptsmenu(bContext *C, void *arg, int event)
126 {
127         // XXX BPY_menu_do_python(PYMENU_TEXTPLUGIN, event);
128 }
129
130 static uiBlock *text_plugin_scriptsmenu(bContext *C, void *args_unused)
131 {
132         ARegion *ar= CTX_wm_region(C);
133         uiBlock *block;
134         // XXX BPyMenu *pym;
135         // int i= 0;
136         // short yco = 20, menuwidth = 120;
137         
138         block= uiBeginBlock(C, ar, "text_plugin_scriptsmenu", UI_EMBOSSP);
139         uiBlockSetButmFunc(block, do_text_plugin_scriptsmenu, NULL);
140         
141         /* note that we acount for the N previous entries with i+20: */
142         /* XXX for (pym = BPyMenuTable[PYMENU_TEXTPLUGIN]; pym; pym = pym->next, i++) {
143                 
144                 uiDefIconTextBut(block, BUTM, 1, ICON_PYTHON, pym->name, 0, yco-=20, menuwidth, 19, 
145                                                  NULL, 0.0, 0.0, 1, i, 
146                                                  pym->tooltip?pym->tooltip:pym->filename);
147         }*/
148         
149         uiBlockSetDirection(block, UI_RIGHT);
150         uiTextBoundsBlock(block, 60);
151
152         uiEndBlock(C, block);
153         uiDrawBlock(C, block);
154
155         return block;
156 }
157 #endif
158 #endif
159
160 /************************** properties ******************************/
161
162 ARegion *text_has_properties_region(ScrArea *sa)
163 {
164         ARegion *ar, *arnew;
165         
166         for(ar= sa->regionbase.first; ar; ar= ar->next)
167                 if(ar->regiontype==RGN_TYPE_UI)
168                         return ar;
169         
170         /* add subdiv level; after header */
171         for(ar= sa->regionbase.first; ar; ar= ar->next)
172                 if(ar->regiontype==RGN_TYPE_HEADER)
173                         break;
174         
175         /* is error! */
176         if(ar==NULL) return NULL;
177         
178         arnew= MEM_callocN(sizeof(ARegion), "properties region");
179         
180         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
181         arnew->regiontype= RGN_TYPE_UI;
182         arnew->alignment= RGN_ALIGN_LEFT;
183         
184         arnew->flag = RGN_FLAG_HIDDEN;
185         
186         return arnew;
187 }
188
189 void text_toggle_properties_region(bContext *C, ScrArea *sa, ARegion *ar)
190 {
191         ar->flag ^= RGN_FLAG_HIDDEN;
192         ar->v2d.flag &= ~V2D_IS_INITIALISED; /* XXX should become hide/unhide api? */
193         
194         ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
195         ED_area_tag_redraw(sa);
196 }
197
198 static int properties_poll(bContext *C)
199 {
200         return (CTX_wm_space_text(C) != NULL);
201 }
202
203 static int properties_exec(bContext *C, wmOperator *op)
204 {
205         ScrArea *sa= CTX_wm_area(C);
206         ARegion *ar= text_has_properties_region(sa);
207         
208         if(ar)
209                 text_toggle_properties_region(C, sa, ar);
210
211         return OPERATOR_FINISHED;
212 }
213
214 void TEXT_OT_properties(wmOperatorType *ot)
215 {
216         /* identifiers */
217         ot->name= "Properties";
218         ot->idname= "TEXT_OT_properties";
219         
220         /* api callbacks */
221         ot->exec= properties_exec;
222         ot->poll= properties_poll;
223 }
224
225 /******************** XXX popup menus *******************/
226
227 #if 0
228 {
229         // RMB
230
231         uiPopupMenu *pup;
232
233         if(text) {
234                 pup= uiPupMenuBegin(C, "Text", 0);
235                 if(txt_has_sel(text)) {
236                         uiItemO(layout, NULL, 0, "TEXT_OT_cut");
237                         uiItemO(layout, NULL, 0, "TEXT_OT_copy");
238                 }
239                 uiItemO(layout, NULL, 0, "TEXT_OT_paste");
240                 uiItemO(layout, NULL, 0, "TEXT_OT_new");
241                 uiItemO(layout, NULL, 0, "TEXT_OT_open");
242                 uiItemO(layout, NULL, 0, "TEXT_OT_save");
243                 uiItemO(layout, NULL, 0, "TEXT_OT_save_as");
244                 uiItemO(layout, NULL, 0, "TEXT_OT_run_script");
245                 uiPupMenuEnd(C, pup);
246         }
247         else {
248                 pup= uiPupMenuBegin(C, "File", 0);
249                 uiItemO(layout, NULL, 0, "TEXT_OT_new");
250                 uiItemO(layout, NULL, 0, "TEXT_OT_open");
251                 uiPupMenuEnd(C, pup);
252         }
253 }
254
255 {
256         // Alt+Shift+E
257
258         uiPopupMenu *pup;
259
260         pup= uiPupMenuBegin(C, "Edit", 0);
261         uiItemO(layout, NULL, 0, "TEXT_OT_cut");
262         uiItemO(layout, NULL, 0, "TEXT_OT_copy");
263         uiItemO(layout, NULL, 0, "TEXT_OT_paste");
264         uiPupMenuEnd(C, pup);
265 }
266
267 {
268         // Alt+Shift+F
269
270         uiPopupMenu *pup;
271
272         if(text) {
273                 pup= uiPupMenuBegin(C, "Text", 0);
274                 uiItemO(layout, NULL, 0, "TEXT_OT_new");
275                 uiItemO(layout, NULL, 0, "TEXT_OT_open");
276                 uiItemO(layout, NULL, 0, "TEXT_OT_save");
277                 uiItemO(layout, NULL, 0, "TEXT_OT_save_as");
278                 uiItemO(layout, NULL, 0, "TEXT_OT_run_script");
279                 uiPupMenuEnd(C, pup);
280         }
281         else {
282                 pup= uiPupMenuBegin(C, "File", 0);
283                 uiItemO(layout, NULL, 0, "TEXT_OT_new");
284                 uiItemO(layout, NULL, 0, "TEXT_OT_open");
285                 uiPupMenuEnd(C, pup);
286         }
287 }
288
289 {
290         // Alt+Shift+V
291
292         uiPopupMenu *pup;
293
294         pup= uiPupMenuBegin(C, "Text", 0);
295         uiItemEnumO(layout, "Top of File", 0, "TEXT_OT_move", "type", FILE_TOP);
296         uiItemEnumO(layout, "Bottom of File", 0, "TEXT_OT_move", "type", FILE_BOTTOM);
297         uiItemEnumO(layout, "Page Up", 0, "TEXT_OT_move", "type", PREV_PAGE);
298         uiItemEnumO(layout,  "Page Down", 0, "TEXT_OT_move", "type", NEXT_PAGE);
299         uiPupMenuEnd(C, pup);
300 }
301 #endif
302