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