Manual merge of soc-2009-kazanbas branch:
[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 static int properties_poll(bContext *C)
190 {
191         return (CTX_wm_space_text(C) != NULL);
192 }
193
194 static int properties_exec(bContext *C, wmOperator *op)
195 {
196         ScrArea *sa= CTX_wm_area(C);
197         ARegion *ar= text_has_properties_region(sa);
198         
199         if(ar)
200                 ED_region_toggle_hidden(C, ar);
201
202         return OPERATOR_FINISHED;
203 }
204
205 void TEXT_OT_properties(wmOperatorType *ot)
206 {
207         /* identifiers */
208         ot->name= "Properties";
209     ot->description= "Toggle text properties panel.";
210         ot->idname= "TEXT_OT_properties";
211         
212         /* api callbacks */
213         ot->exec= properties_exec;
214         ot->poll= properties_poll;
215 }
216
217 /******************** XXX popup menus *******************/
218
219 #if 0
220 {
221         // RMB
222
223         uiPopupMenu *pup;
224
225         if(text) {
226                 pup= uiPupMenuBegin(C, "Text", 0);
227                 if(txt_has_sel(text)) {
228                         uiItemO(layout, NULL, 0, "TEXT_OT_cut");
229                         uiItemO(layout, NULL, 0, "TEXT_OT_copy");
230                 }
231                 uiItemO(layout, NULL, 0, "TEXT_OT_paste");
232                 uiItemO(layout, NULL, 0, "TEXT_OT_new");
233                 uiItemO(layout, NULL, 0, "TEXT_OT_open");
234                 uiItemO(layout, NULL, 0, "TEXT_OT_save");
235                 uiItemO(layout, NULL, 0, "TEXT_OT_save_as");
236                 uiItemO(layout, NULL, 0, "TEXT_OT_run_script");
237                 uiPupMenuEnd(C, pup);
238         }
239         else {
240                 pup= uiPupMenuBegin(C, "File", 0);
241                 uiItemO(layout, NULL, 0, "TEXT_OT_new");
242                 uiItemO(layout, NULL, 0, "TEXT_OT_open");
243                 uiPupMenuEnd(C, pup);
244         }
245 }
246
247 {
248         // Alt+Shift+E
249
250         uiPopupMenu *pup;
251
252         pup= uiPupMenuBegin(C, "Edit", 0);
253         uiItemO(layout, NULL, 0, "TEXT_OT_cut");
254         uiItemO(layout, NULL, 0, "TEXT_OT_copy");
255         uiItemO(layout, NULL, 0, "TEXT_OT_paste");
256         uiPupMenuEnd(C, pup);
257 }
258
259 {
260         // Alt+Shift+F
261
262         uiPopupMenu *pup;
263
264         if(text) {
265                 pup= uiPupMenuBegin(C, "Text", 0);
266                 uiItemO(layout, NULL, 0, "TEXT_OT_new");
267                 uiItemO(layout, NULL, 0, "TEXT_OT_open");
268                 uiItemO(layout, NULL, 0, "TEXT_OT_save");
269                 uiItemO(layout, NULL, 0, "TEXT_OT_save_as");
270                 uiItemO(layout, NULL, 0, "TEXT_OT_run_script");
271                 uiPupMenuEnd(C, pup);
272         }
273         else {
274                 pup= uiPupMenuBegin(C, "File", 0);
275                 uiItemO(layout, NULL, 0, "TEXT_OT_new");
276                 uiItemO(layout, NULL, 0, "TEXT_OT_open");
277                 uiPupMenuEnd(C, pup);
278         }
279 }
280
281 {
282         // Alt+Shift+V
283
284         uiPopupMenu *pup;
285
286         pup= uiPupMenuBegin(C, "Text", 0);
287         uiItemEnumO(layout, "Top of File", 0, "TEXT_OT_move", "type", FILE_TOP);
288         uiItemEnumO(layout, "Bottom of File", 0, "TEXT_OT_move", "type", FILE_BOTTOM);
289         uiItemEnumO(layout, "Page Up", 0, "TEXT_OT_move", "type", PREV_PAGE);
290         uiItemEnumO(layout,  "Page Down", 0, "TEXT_OT_move", "type", NEXT_PAGE);
291         uiPupMenuEnd(C, pup);
292 }
293 #endif
294