2.5: Text editor, port menu code to python.
[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 static void do_text_template_scriptsmenu(bContext *C, void *arg, int event)
92 {
93         // XXX BPY_menu_do_python(PYMENU_SCRIPTTEMPLATE, event);
94 }
95
96 static uiBlock *text_template_scriptsmenu(bContext *C, void *args_unused)
97 {
98         ARegion *ar= CTX_wm_region(C);
99         uiBlock *block;
100         // XXX BPyMenu *pym;
101         // int i= 0;
102         // short yco = 20, menuwidth = 120;
103         
104         block= uiBeginBlock(C, ar, "text_template_scriptsmenu", UI_EMBOSSP);
105         uiBlockSetButmFunc(block, do_text_template_scriptsmenu, NULL);
106         
107         /* note that we acount for the N previous entries with i+20: */
108         /* XXX for (pym = BPyMenuTable[PYMENU_SCRIPTTEMPLATE]; pym; pym = pym->next, i++) {
109                 
110                 uiDefIconTextBut(block, BUTM, 1, ICON_PYTHON, pym->name, 0, yco-=20, menuwidth, 19, 
111                                                  NULL, 0.0, 0.0, 1, i, 
112                                                  pym->tooltip?pym->tooltip:pym->filename);
113         }*/
114         
115         uiBlockSetDirection(block, UI_RIGHT);
116         uiTextBoundsBlock(block, 60);
117
118         uiEndBlock(C, block);
119         uiDrawBlock(C, block);
120         
121         return block;
122 }
123
124 static void do_text_plugin_scriptsmenu(bContext *C, void *arg, int event)
125 {
126         // XXX BPY_menu_do_python(PYMENU_TEXTPLUGIN, event);
127 }
128
129 static uiBlock *text_plugin_scriptsmenu(bContext *C, void *args_unused)
130 {
131         ARegion *ar= CTX_wm_region(C);
132         uiBlock *block;
133         // XXX BPyMenu *pym;
134         // int i= 0;
135         // short yco = 20, menuwidth = 120;
136         
137         block= uiBeginBlock(C, ar, "text_plugin_scriptsmenu", UI_EMBOSSP);
138         uiBlockSetButmFunc(block, do_text_plugin_scriptsmenu, NULL);
139         
140         /* note that we acount for the N previous entries with i+20: */
141         /* XXX for (pym = BPyMenuTable[PYMENU_TEXTPLUGIN]; pym; pym = pym->next, i++) {
142                 
143                 uiDefIconTextBut(block, BUTM, 1, ICON_PYTHON, pym->name, 0, yco-=20, menuwidth, 19, 
144                                                  NULL, 0.0, 0.0, 1, i, 
145                                                  pym->tooltip?pym->tooltip:pym->filename);
146         }*/
147         
148         uiBlockSetDirection(block, UI_RIGHT);
149         uiTextBoundsBlock(block, 60);
150
151         uiEndBlock(C, block);
152         uiDrawBlock(C, block);
153
154         return block;
155 }
156 #endif
157
158 /************************** properties ******************************/
159
160 ARegion *text_has_properties_region(ScrArea *sa)
161 {
162         ARegion *ar, *arnew;
163         
164         for(ar= sa->regionbase.first; ar; ar= ar->next)
165                 if(ar->regiontype==RGN_TYPE_UI)
166                         return ar;
167         
168         /* add subdiv level; after header */
169         for(ar= sa->regionbase.first; ar; ar= ar->next)
170                 if(ar->regiontype==RGN_TYPE_HEADER)
171                         break;
172         
173         /* is error! */
174         if(ar==NULL) return NULL;
175         
176         arnew= MEM_callocN(sizeof(ARegion), "properties region");
177         
178         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
179         arnew->regiontype= RGN_TYPE_UI;
180         arnew->alignment= RGN_ALIGN_LEFT;
181         
182         arnew->flag = RGN_FLAG_HIDDEN;
183         
184         return arnew;
185 }
186
187 static int properties_poll(bContext *C)
188 {
189         SpaceText *st= CTX_wm_space_text(C);
190         Text *text= CTX_data_edit_text(C);
191
192         return (st && text);
193 }
194
195 static int properties_exec(bContext *C, wmOperator *op)
196 {
197         ScrArea *sa= CTX_wm_area(C);
198         ARegion *ar= text_has_properties_region(sa);
199         
200         if(ar) {
201                 ar->flag ^= RGN_FLAG_HIDDEN;
202                 ar->v2d.flag &= ~V2D_IS_INITIALISED; /* XXX should become hide/unhide api? */
203                 
204                 ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
205                 ED_area_tag_redraw(sa);
206         }
207
208         return OPERATOR_FINISHED;
209 }
210
211 void TEXT_OT_properties(wmOperatorType *ot)
212 {
213         /* identifiers */
214         ot->name= "Properties";
215         ot->idname= "TEXT_OT_properties";
216         
217         /* api callbacks */
218         ot->exec= properties_exec;
219         ot->poll= properties_poll;
220 }
221
222 /******************** XXX popup menus *******************/
223
224 #if 0
225 {
226         // RMB
227
228         uiPopupMenu *pup;
229
230         if(text) {
231                 pup= uiPupMenuBegin(C, "Text", 0);
232                 if(txt_has_sel(text)) {
233                         uiItemO(layout, NULL, 0, "TEXT_OT_cut");
234                         uiItemO(layout, NULL, 0, "TEXT_OT_copy");
235                 }
236                 uiItemO(layout, NULL, 0, "TEXT_OT_paste");
237                 uiItemO(layout, NULL, 0, "TEXT_OT_new");
238                 uiItemO(layout, NULL, 0, "TEXT_OT_open");
239                 uiItemO(layout, NULL, 0, "TEXT_OT_save");
240                 uiItemO(layout, NULL, 0, "TEXT_OT_save_as");
241                 uiItemO(layout, NULL, 0, "TEXT_OT_run_script");
242                 uiPupMenuEnd(C, pup);
243         }
244         else {
245                 pup= uiPupMenuBegin(C, "File", 0);
246                 uiItemO(layout, NULL, 0, "TEXT_OT_new");
247                 uiItemO(layout, NULL, 0, "TEXT_OT_open");
248                 uiPupMenuEnd(C, pup);
249         }
250 }
251
252 {
253         // Alt+Shift+E
254
255         uiPopupMenu *pup;
256
257         pup= uiPupMenuBegin(C, "Edit", 0);
258         uiItemO(layout, NULL, 0, "TEXT_OT_cut");
259         uiItemO(layout, NULL, 0, "TEXT_OT_copy");
260         uiItemO(layout, NULL, 0, "TEXT_OT_paste");
261         uiPupMenuEnd(C, pup);
262 }
263
264 {
265         // Alt+Shift+F
266
267         uiPopupMenu *pup;
268
269         if(text) {
270                 pup= uiPupMenuBegin(C, "Text", 0);
271                 uiItemO(layout, NULL, 0, "TEXT_OT_new");
272                 uiItemO(layout, NULL, 0, "TEXT_OT_open");
273                 uiItemO(layout, NULL, 0, "TEXT_OT_save");
274                 uiItemO(layout, NULL, 0, "TEXT_OT_save_as");
275                 uiItemO(layout, NULL, 0, "TEXT_OT_run_script");
276                 uiPupMenuEnd(C, pup);
277         }
278         else {
279                 pup= uiPupMenuBegin(C, "File", 0);
280                 uiItemO(layout, NULL, 0, "TEXT_OT_new");
281                 uiItemO(layout, NULL, 0, "TEXT_OT_open");
282                 uiPupMenuEnd(C, pup);
283         }
284 }
285
286 {
287         // Alt+Shift+V
288
289         uiPopupMenu *pup;
290
291         pup= uiPupMenuBegin(C, "Text", 0);
292         uiItemEnumO(layout, "Top of File", 0, "TEXT_OT_move", "type", FILE_TOP);
293         uiItemEnumO(layout, "Bottom of File", 0, "TEXT_OT_move", "type", FILE_BOTTOM);
294         uiItemEnumO(layout, "Page Up", 0, "TEXT_OT_move", "type", PREV_PAGE);
295         uiItemEnumO(layout,  "Page Down", 0, "TEXT_OT_move", "type", NEXT_PAGE);
296         uiPupMenuEnd(C, pup);
297 }
298 #endif
299