made most variables which are only used in a single file and not defined in header...
[blender.git] / source / blender / editors / space_text / space_text.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 <string.h>
30 #include <stdio.h>
31
32 #include "DNA_text_types.h"
33 #include "DNA_object_types.h"
34
35 #include "MEM_guardedalloc.h"
36
37 #include "BLI_blenlib.h"
38 #include "BLI_math.h"
39 #include "BLI_rand.h"
40 #include "BLI_utildefines.h"
41
42 #include "BKE_context.h"
43 #include "BKE_screen.h"
44
45 #include "ED_space_api.h"
46 #include "ED_screen.h"
47
48 #include "BIF_gl.h"
49
50 #include "WM_api.h"
51 #include "WM_types.h"
52
53 #include "UI_interface.h"
54 #include "UI_resources.h"
55 #include "UI_view2d.h"
56
57 #include "RNA_access.h"
58
59
60 #include "text_intern.h"        // own include
61
62 /* ******************** default callbacks for text space ***************** */
63
64 static SpaceLink *text_new(const bContext *UNUSED(C))
65 {
66         ARegion *ar;
67         SpaceText *stext;
68         
69         stext= MEM_callocN(sizeof(SpaceText), "inittext");
70         stext->spacetype= SPACE_TEXT;
71
72         stext->lheight= 12;
73         stext->tabnumber= 4;
74         
75         /* header */
76         ar= MEM_callocN(sizeof(ARegion), "header for text");
77         
78         BLI_addtail(&stext->regionbase, ar);
79         ar->regiontype= RGN_TYPE_HEADER;
80         ar->alignment= RGN_ALIGN_BOTTOM;
81         
82         /* main area */
83         ar= MEM_callocN(sizeof(ARegion), "main area for text");
84         
85         BLI_addtail(&stext->regionbase, ar);
86         ar->regiontype= RGN_TYPE_WINDOW;
87         
88         return (SpaceLink *)stext;
89 }
90
91 /* not spacelink itself */
92 static void text_free(SpaceLink *sl)
93 {       
94         SpaceText *stext= (SpaceText*) sl;
95         
96         stext->text= NULL;
97         text_free_caches(stext);
98 }
99
100
101 /* spacetype; init callback */
102 static void text_init(struct wmWindowManager *UNUSED(wm), ScrArea *UNUSED(sa))
103 {
104
105 }
106
107 static SpaceLink *text_duplicate(SpaceLink *sl)
108 {
109         SpaceText *stextn= MEM_dupallocN(sl);
110
111         /* clear or remove stuff from old */
112
113         stextn->drawcache= NULL; /* space need it's own cache */
114
115         return (SpaceLink *)stextn;
116 }
117
118 static void text_listener(ScrArea *sa, wmNotifier *wmn)
119 {
120         SpaceText *st= sa->spacedata.first;
121
122         /* context changes */
123         switch(wmn->category) {
124                 case NC_TEXT:
125                         /* check if active text was changed, no need to redraw if text isn't active
126                            reference==NULL means text was unlinked, should update anyway for this
127                            case -- no way to know was text active before unlinking or not */
128                         if(wmn->reference && wmn->reference != st->text)
129                                 break;
130
131                         switch(wmn->data) {
132                                 case ND_DISPLAY:
133                                 case ND_CURSOR:
134                                         ED_area_tag_redraw(sa);
135                                         break;
136                         }
137
138                         switch(wmn->action) {
139                                 case NA_EDITED:
140                                         if(st->text) {
141                                                 text_drawcache_tag_update(st, 1);
142                                                 text_update_edited(st->text);
143                                         }
144
145                                         ED_area_tag_redraw(sa);
146                                         /* no break -- fall down to tag redraw */
147                                 case NA_ADDED:
148                                 case NA_REMOVED:
149                                         ED_area_tag_redraw(sa);
150                                         break;
151                         }
152
153                         break;
154                 case NC_SPACE:
155                         if(wmn->data == ND_SPACE_TEXT)
156                                 ED_area_tag_redraw(sa);
157                         break;
158         }
159 }
160
161 static void text_operatortypes(void)
162 {
163         WM_operatortype_append(TEXT_OT_new);
164         WM_operatortype_append(TEXT_OT_open);
165         WM_operatortype_append(TEXT_OT_reload);
166         WM_operatortype_append(TEXT_OT_unlink);
167         WM_operatortype_append(TEXT_OT_save);
168         WM_operatortype_append(TEXT_OT_save_as);
169         WM_operatortype_append(TEXT_OT_make_internal);
170         WM_operatortype_append(TEXT_OT_run_script);
171         WM_operatortype_append(TEXT_OT_refresh_pyconstraints);
172
173         WM_operatortype_append(TEXT_OT_paste);
174         WM_operatortype_append(TEXT_OT_copy);
175         WM_operatortype_append(TEXT_OT_cut);
176
177         WM_operatortype_append(TEXT_OT_convert_whitespace);
178         WM_operatortype_append(TEXT_OT_uncomment);
179         WM_operatortype_append(TEXT_OT_comment);
180         WM_operatortype_append(TEXT_OT_unindent);
181         WM_operatortype_append(TEXT_OT_indent);
182
183         WM_operatortype_append(TEXT_OT_markers_clear);
184         WM_operatortype_append(TEXT_OT_next_marker);
185         WM_operatortype_append(TEXT_OT_previous_marker);
186
187         WM_operatortype_append(TEXT_OT_select_line);
188         WM_operatortype_append(TEXT_OT_select_all);
189
190         WM_operatortype_append(TEXT_OT_jump);
191         WM_operatortype_append(TEXT_OT_move);
192         WM_operatortype_append(TEXT_OT_move_select);
193         WM_operatortype_append(TEXT_OT_delete);
194         WM_operatortype_append(TEXT_OT_overwrite_toggle);
195
196         WM_operatortype_append(TEXT_OT_cursor_set);
197         WM_operatortype_append(TEXT_OT_scroll);
198         WM_operatortype_append(TEXT_OT_scroll_bar);
199         WM_operatortype_append(TEXT_OT_line_number);
200
201         WM_operatortype_append(TEXT_OT_line_break);
202         WM_operatortype_append(TEXT_OT_insert);
203
204         WM_operatortype_append(TEXT_OT_properties);
205
206         WM_operatortype_append(TEXT_OT_find);
207         WM_operatortype_append(TEXT_OT_find_set_selected);
208         WM_operatortype_append(TEXT_OT_replace);
209         WM_operatortype_append(TEXT_OT_replace_set_selected);
210         WM_operatortype_append(TEXT_OT_mark_all);
211
212         WM_operatortype_append(TEXT_OT_to_3d_object);
213
214         WM_operatortype_append(TEXT_OT_resolve_conflict);
215 }
216
217 static void text_keymap(struct wmKeyConfig *keyconf)
218 {
219         wmKeyMap *keymap;
220         wmKeyMapItem *kmi;
221         
222         keymap= WM_keymap_find(keyconf, "Text", SPACE_TEXT, 0);
223         
224         #ifdef __APPLE__
225         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", LEFTARROWKEY, KM_PRESS, KM_OSKEY, 0)->ptr, "type", LINE_BEGIN);
226         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", RIGHTARROWKEY, KM_PRESS, KM_OSKEY, 0)->ptr, "type", LINE_END);
227         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", LEFTARROWKEY, KM_PRESS, KM_ALT, 0)->ptr, "type", PREV_WORD);
228         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", RIGHTARROWKEY, KM_PRESS, KM_ALT, 0)->ptr, "type", NEXT_WORD);
229         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", UPARROWKEY, KM_PRESS, KM_OSKEY, 0)->ptr, "type", FILE_TOP);
230         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", DOWNARROWKEY, KM_PRESS, KM_OSKEY, 0)->ptr, "type", FILE_BOTTOM);
231         
232         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", LEFTARROWKEY, KM_PRESS, KM_SHIFT|KM_OSKEY, 0)->ptr, "type", LINE_BEGIN);
233         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", RIGHTARROWKEY, KM_PRESS, KM_SHIFT|KM_OSKEY, 0)->ptr, "type", LINE_END);
234         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", LEFTARROWKEY, KM_PRESS, KM_SHIFT|KM_ALT, 0)->ptr, "type", PREV_WORD);
235         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", RIGHTARROWKEY, KM_PRESS, KM_SHIFT|KM_ALT, 0)->ptr, "type", NEXT_WORD);
236         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", UPARROWKEY, KM_PRESS, KM_SHIFT|KM_OSKEY, 0)->ptr, "type", FILE_TOP);
237         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", DOWNARROWKEY, KM_PRESS, KM_SHIFT|KM_OSKEY, 0)->ptr, "type", FILE_BOTTOM);
238         
239         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_delete", BACKSPACEKEY, KM_PRESS, KM_ALT, 0)->ptr, "type", DEL_PREV_WORD);
240         
241         WM_keymap_add_item(keymap, "TEXT_OT_save", SKEY, KM_PRESS, KM_ALT|KM_OSKEY, 0);
242         WM_keymap_add_item(keymap, "TEXT_OT_save_as", SKEY, KM_PRESS, KM_ALT|KM_SHIFT|KM_OSKEY, 0);
243         WM_keymap_add_item(keymap, "TEXT_OT_cut", XKEY, KM_PRESS, KM_OSKEY, 0);
244         WM_keymap_add_item(keymap, "TEXT_OT_copy", CKEY, KM_PRESS, KM_OSKEY, 0); 
245         WM_keymap_add_item(keymap, "TEXT_OT_paste", VKEY, KM_PRESS, KM_OSKEY, 0);
246         WM_keymap_add_item(keymap, "TEXT_OT_properties", FKEY, KM_PRESS, KM_OSKEY, 0);
247         WM_keymap_add_item(keymap, "TEXT_OT_find_set_selected", EKEY, KM_PRESS, KM_OSKEY, 0);
248         WM_keymap_add_item(keymap, "TEXT_OT_find", GKEY, KM_PRESS, KM_OSKEY, 0);
249         WM_keymap_add_item(keymap, "TEXT_OT_select_all", AKEY, KM_PRESS, KM_OSKEY, 0);
250         WM_keymap_add_item(keymap, "TEXT_OT_select_line", AKEY, KM_PRESS, KM_SHIFT|KM_OSKEY, 0);
251         #endif
252         
253         kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", WHEELUPMOUSE, KM_PRESS, KM_CTRL, 0);
254         RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
255         RNA_boolean_set(kmi->ptr, "reverse", 0);
256         
257         kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", WHEELDOWNMOUSE, KM_PRESS, KM_CTRL, 0);
258         RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
259         RNA_boolean_set(kmi->ptr, "reverse", 1);
260
261         kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", PADPLUSKEY, KM_PRESS, KM_CTRL, 0);
262         RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
263         RNA_boolean_set(kmi->ptr, "reverse", 0);
264         
265         kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", PADMINUS, KM_PRESS, KM_CTRL, 0);
266         RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
267         RNA_boolean_set(kmi->ptr, "reverse", 1);
268         
269         WM_keymap_add_item(keymap, "TEXT_OT_new", NKEY, KM_PRESS, KM_ALT, 0);
270         WM_keymap_add_item(keymap, "TEXT_OT_open", OKEY, KM_PRESS, KM_ALT, 0);
271         WM_keymap_add_item(keymap, "TEXT_OT_reload", RKEY, KM_PRESS, KM_ALT, 0);
272         WM_keymap_add_item(keymap, "TEXT_OT_save", SKEY, KM_PRESS, KM_ALT, 0);
273         WM_keymap_add_item(keymap, "TEXT_OT_save_as", SKEY, KM_PRESS, KM_ALT|KM_SHIFT|KM_CTRL, 0);
274
275         WM_keymap_add_item(keymap, "TEXT_OT_run_script", PKEY, KM_PRESS, KM_ALT, 0);
276         
277         WM_keymap_add_item(keymap, "TEXT_OT_cut", XKEY, KM_PRESS, KM_CTRL, 0);
278         WM_keymap_add_item(keymap, "TEXT_OT_copy", CKEY, KM_PRESS, KM_CTRL, 0);
279         WM_keymap_add_item(keymap, "TEXT_OT_paste", VKEY, KM_PRESS, KM_CTRL, 0);
280
281         if(U.uiflag & USER_MMB_PASTE) // XXX not dynamic
282                 RNA_boolean_set(WM_keymap_add_item(keymap, "TEXT_OT_paste", MIDDLEMOUSE, KM_PRESS, 0, 0)->ptr, "selection", 1);
283
284         WM_keymap_add_item(keymap, "TEXT_OT_jump", JKEY, KM_PRESS, KM_CTRL, 0);
285         WM_keymap_add_item(keymap, "TEXT_OT_find", GKEY, KM_PRESS, KM_CTRL, 0);
286         
287         WM_keymap_add_item(keymap, "TEXT_OT_properties", FKEY, KM_PRESS, KM_CTRL, 0);
288         WM_keymap_add_item(keymap, "TEXT_OT_replace", HKEY, KM_PRESS, KM_CTRL, 0);
289
290         WM_keymap_add_item(keymap, "TEXT_OT_to_3d_object", MKEY, KM_PRESS, KM_ALT, 0);
291
292         WM_keymap_add_item(keymap, "TEXT_OT_select_all", AKEY, KM_PRESS, KM_CTRL, 0);
293         WM_keymap_add_item(keymap, "TEXT_OT_select_line", AKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0);
294         
295
296         WM_keymap_add_item(keymap, "TEXT_OT_indent", TABKEY, KM_PRESS, 0, 0);
297         WM_keymap_add_item(keymap, "TEXT_OT_unindent", TABKEY, KM_PRESS, KM_SHIFT, 0);
298         WM_keymap_add_item(keymap, "TEXT_OT_uncomment", DKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
299
300         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", HOMEKEY, KM_PRESS, 0, 0)->ptr, "type", LINE_BEGIN);
301         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", ENDKEY, KM_PRESS, 0, 0)->ptr, "type", LINE_END);
302         
303         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", EKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", LINE_END);
304         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", EKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0)->ptr, "type", LINE_END);
305         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", LEFTARROWKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_CHAR);
306         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", RIGHTARROWKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_CHAR);
307         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", LEFTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", PREV_WORD);
308         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", RIGHTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", NEXT_WORD);
309         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", UPARROWKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_LINE);
310         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", DOWNARROWKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_LINE);
311         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", PAGEUPKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_PAGE);
312         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", PAGEDOWNKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_PAGE);
313         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", HOMEKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", FILE_TOP);
314         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", ENDKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", FILE_BOTTOM);
315
316         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", HOMEKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", LINE_BEGIN);
317         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", ENDKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", LINE_END);
318         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", LEFTARROWKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", PREV_CHAR);
319         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", RIGHTARROWKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", NEXT_CHAR);
320         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", LEFTARROWKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0)->ptr, "type", PREV_WORD);
321         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", RIGHTARROWKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0)->ptr, "type", NEXT_WORD);
322         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", UPARROWKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", PREV_LINE);
323         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", DOWNARROWKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", NEXT_LINE);
324         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", PAGEUPKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", PREV_PAGE);
325         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", PAGEDOWNKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", NEXT_PAGE);
326         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", HOMEKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0)->ptr, "type", FILE_TOP);
327         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", ENDKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0)->ptr, "type", FILE_BOTTOM);
328
329         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_delete", DELKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_NEXT_CHAR);
330         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_delete", DKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", DEL_NEXT_CHAR);
331         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_delete", BACKSPACEKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_PREV_CHAR);
332         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_delete", DELKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", DEL_NEXT_WORD);
333         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_delete", BACKSPACEKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", DEL_PREV_WORD);
334         
335         WM_keymap_add_item(keymap, "TEXT_OT_overwrite_toggle", INSERTKEY, KM_PRESS, 0, 0);
336
337         WM_keymap_add_item(keymap, "TEXT_OT_scroll", MIDDLEMOUSE, KM_PRESS, 0, 0);
338         WM_keymap_add_item(keymap, "TEXT_OT_scroll", MOUSEPAN, 0, 0, 0);
339         WM_keymap_add_item(keymap, "TEXT_OT_scroll_bar", LEFTMOUSE, KM_PRESS, 0, 0);
340         WM_keymap_add_item(keymap, "TEXT_OT_cursor_set", LEFTMOUSE, KM_PRESS, 0, 0);
341         RNA_boolean_set(WM_keymap_add_item(keymap, "TEXT_OT_cursor_set", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "select", 1);
342         RNA_int_set(WM_keymap_add_item(keymap, "TEXT_OT_scroll", WHEELUPMOUSE, KM_PRESS, 0, 0)->ptr, "lines", -1);
343         RNA_int_set(WM_keymap_add_item(keymap, "TEXT_OT_scroll", WHEELDOWNMOUSE, KM_PRESS, 0, 0)->ptr, "lines", 1);
344
345         WM_keymap_add_item(keymap, "TEXT_OT_line_break", RETKEY, KM_PRESS, 0, 0);
346         WM_keymap_add_item(keymap, "TEXT_OT_line_break", PADENTER, KM_PRESS, 0, 0);
347
348         WM_keymap_add_menu(keymap, "TEXT_MT_toolbox", RIGHTMOUSE, KM_PRESS, KM_ANY, 0);
349         
350         WM_keymap_add_item(keymap, "TEXT_OT_line_number", KM_TEXTINPUT, KM_ANY, KM_ANY, 0);
351         WM_keymap_add_item(keymap, "TEXT_OT_insert", KM_TEXTINPUT, KM_ANY, KM_ANY, 0); // last!
352 }
353
354 static int text_context(const bContext *C, const char *member, bContextDataResult *result)
355 {
356         SpaceText *st= CTX_wm_space_text(C);
357
358         if(CTX_data_dir(member)) {
359                 static const char *dir[] = {"edit_text", NULL};
360                 CTX_data_dir_set(result, dir);
361                 return 1;
362         }
363         else if(CTX_data_equals(member, "edit_text")) {
364                 CTX_data_id_pointer_set(result, &st->text->id);
365                 return 1;
366         }
367
368         return 0;
369 }
370
371 /********************* main region ********************/
372
373 /* add handlers, stuff you only do once or on area/region changes */
374 static void text_main_area_init(wmWindowManager *wm, ARegion *ar)
375 {
376         wmKeyMap *keymap;
377         ListBase *lb;
378         
379         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
380         
381         /* own keymap */
382         keymap= WM_keymap_find(wm->defaultconf, "Text", SPACE_TEXT, 0);
383         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
384         
385         /* add drop boxes */
386         lb = WM_dropboxmap_find("Text", SPACE_TEXT, RGN_TYPE_WINDOW);
387         
388         WM_event_add_dropbox_handler(&ar->handlers, lb);
389 }
390
391 static void text_main_area_draw(const bContext *C, ARegion *ar)
392 {
393         /* draw entirely, view changes should be handled here */
394         SpaceText *st= CTX_wm_space_text(C);
395         //View2D *v2d= &ar->v2d;
396         
397         /* clear and setup matrix */
398         UI_ThemeClearColor(TH_BACK);
399         glClear(GL_COLOR_BUFFER_BIT);
400         
401         // UI_view2d_view_ortho(v2d);
402                 
403         /* data... */
404         draw_text_main(st, ar);
405         
406         /* reset view matrix */
407         // UI_view2d_view_restore(C);
408         
409         /* scrollers? */
410 }
411
412 static void text_cursor(wmWindow *win, ScrArea *UNUSED(sa), ARegion *UNUSED(ar))
413 {
414         WM_cursor_set(win, BC_TEXTEDITCURSOR);
415 }
416
417
418
419 /* ************* dropboxes ************* */
420
421 static int text_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
422 {
423         if(drag->type==WM_DRAG_PATH)
424                 if(ELEM(drag->icon, ICON_FILE_SCRIPT, ICON_FILE_BLANK)) /* rule might not work? */
425                         return 1;
426         return 0;
427 }
428
429 static void text_drop_copy(wmDrag *drag, wmDropBox *drop)
430 {
431         /* copy drag path to properties */
432         RNA_string_set(drop->ptr, "filepath", drag->path);
433 }
434
435 /* this region dropbox definition */
436 static void text_dropboxes(void)
437 {
438         ListBase *lb= WM_dropboxmap_find("Text", SPACE_TEXT, RGN_TYPE_WINDOW);
439         
440         WM_dropbox_add(lb, "TEXT_OT_open", text_drop_poll, text_drop_copy);
441
442 }
443
444 /* ************* end drop *********** */
445
446
447 /****************** header region ******************/
448
449 /* add handlers, stuff you only do once or on area/region changes */
450 static void text_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
451 {
452         ED_region_header_init(ar);
453 }
454
455 static void text_header_area_draw(const bContext *C, ARegion *ar)
456 {
457         ED_region_header(C, ar);
458 }
459
460 /****************** properties region ******************/
461
462 /* add handlers, stuff you only do once or on area/region changes */
463 static void text_properties_area_init(wmWindowManager *wm, ARegion *ar)
464 {
465         ED_region_panels_init(wm, ar);
466 }
467
468 static void text_properties_area_draw(const bContext *C, ARegion *ar)
469 {
470         ED_region_panels(C, ar, 1, NULL, -1);
471 }
472
473 /********************* registration ********************/
474
475 /* only called once, from space/spacetypes.c */
476 void ED_spacetype_text(void)
477 {
478         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype text");
479         ARegionType *art;
480         
481         st->spaceid= SPACE_TEXT;
482         strncpy(st->name, "Text", BKE_ST_MAXNAME);
483         
484         st->new= text_new;
485         st->free= text_free;
486         st->init= text_init;
487         st->duplicate= text_duplicate;
488         st->operatortypes= text_operatortypes;
489         st->keymap= text_keymap;
490         st->listener= text_listener;
491         st->context= text_context;
492         st->dropboxes = text_dropboxes;
493         
494         /* regions: main window */
495         art= MEM_callocN(sizeof(ARegionType), "spacetype text region");
496         art->regionid = RGN_TYPE_WINDOW;
497         art->init= text_main_area_init;
498         art->draw= text_main_area_draw;
499         art->cursor= text_cursor;
500
501         BLI_addhead(&st->regiontypes, art);
502         
503         /* regions: properties */
504         art= MEM_callocN(sizeof(ARegionType), "spacetype text region");
505         art->regionid = RGN_TYPE_UI;
506         art->prefsizex= UI_COMPACT_PANEL_WIDTH;
507         art->keymapflag= ED_KEYMAP_UI;
508         
509         art->init= text_properties_area_init;
510         art->draw= text_properties_area_draw;
511         BLI_addhead(&st->regiontypes, art);
512
513         /* regions: header */
514         art= MEM_callocN(sizeof(ARegionType), "spacetype text region");
515         art->regionid = RGN_TYPE_HEADER;
516         art->prefsizey= HEADERY;
517         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_HEADER;
518         
519         art->init= text_header_area_init;
520         art->draw= text_header_area_draw;
521
522         BLI_addhead(&st->regiontypes, art);
523
524         BKE_spacetype_register(st);
525 }
526