moving dir strings into global namespace for doc access (coming up)
[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 const char *text_context_dir[] = {"edit_text", NULL};
355
356 static int text_context(const bContext *C, const char *member, bContextDataResult *result)
357 {
358         SpaceText *st= CTX_wm_space_text(C);
359
360         if(CTX_data_dir(member)) {
361                 CTX_data_dir_set(result, text_context_dir);
362                 return 1;
363         }
364         else if(CTX_data_equals(member, "edit_text")) {
365                 CTX_data_id_pointer_set(result, &st->text->id);
366                 return 1;
367         }
368
369         return 0;
370 }
371
372 /********************* main region ********************/
373
374 /* add handlers, stuff you only do once or on area/region changes */
375 static void text_main_area_init(wmWindowManager *wm, ARegion *ar)
376 {
377         wmKeyMap *keymap;
378         ListBase *lb;
379         
380         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
381         
382         /* own keymap */
383         keymap= WM_keymap_find(wm->defaultconf, "Text", SPACE_TEXT, 0);
384         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
385         
386         /* add drop boxes */
387         lb = WM_dropboxmap_find("Text", SPACE_TEXT, RGN_TYPE_WINDOW);
388         
389         WM_event_add_dropbox_handler(&ar->handlers, lb);
390 }
391
392 static void text_main_area_draw(const bContext *C, ARegion *ar)
393 {
394         /* draw entirely, view changes should be handled here */
395         SpaceText *st= CTX_wm_space_text(C);
396         //View2D *v2d= &ar->v2d;
397         
398         /* clear and setup matrix */
399         UI_ThemeClearColor(TH_BACK);
400         glClear(GL_COLOR_BUFFER_BIT);
401         
402         // UI_view2d_view_ortho(v2d);
403                 
404         /* data... */
405         draw_text_main(st, ar);
406         
407         /* reset view matrix */
408         // UI_view2d_view_restore(C);
409         
410         /* scrollers? */
411 }
412
413 static void text_cursor(wmWindow *win, ScrArea *UNUSED(sa), ARegion *UNUSED(ar))
414 {
415         WM_cursor_set(win, BC_TEXTEDITCURSOR);
416 }
417
418
419
420 /* ************* dropboxes ************* */
421
422 static int text_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
423 {
424         if(drag->type==WM_DRAG_PATH)
425                 if(ELEM(drag->icon, ICON_FILE_SCRIPT, ICON_FILE_BLANK)) /* rule might not work? */
426                         return 1;
427         return 0;
428 }
429
430 static void text_drop_copy(wmDrag *drag, wmDropBox *drop)
431 {
432         /* copy drag path to properties */
433         RNA_string_set(drop->ptr, "filepath", drag->path);
434 }
435
436 /* this region dropbox definition */
437 static void text_dropboxes(void)
438 {
439         ListBase *lb= WM_dropboxmap_find("Text", SPACE_TEXT, RGN_TYPE_WINDOW);
440         
441         WM_dropbox_add(lb, "TEXT_OT_open", text_drop_poll, text_drop_copy);
442
443 }
444
445 /* ************* end drop *********** */
446
447
448 /****************** header region ******************/
449
450 /* add handlers, stuff you only do once or on area/region changes */
451 static void text_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
452 {
453         ED_region_header_init(ar);
454 }
455
456 static void text_header_area_draw(const bContext *C, ARegion *ar)
457 {
458         ED_region_header(C, ar);
459 }
460
461 /****************** properties region ******************/
462
463 /* add handlers, stuff you only do once or on area/region changes */
464 static void text_properties_area_init(wmWindowManager *wm, ARegion *ar)
465 {
466         ED_region_panels_init(wm, ar);
467 }
468
469 static void text_properties_area_draw(const bContext *C, ARegion *ar)
470 {
471         ED_region_panels(C, ar, 1, NULL, -1);
472 }
473
474 /********************* registration ********************/
475
476 /* only called once, from space/spacetypes.c */
477 void ED_spacetype_text(void)
478 {
479         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype text");
480         ARegionType *art;
481         
482         st->spaceid= SPACE_TEXT;
483         strncpy(st->name, "Text", BKE_ST_MAXNAME);
484         
485         st->new= text_new;
486         st->free= text_free;
487         st->init= text_init;
488         st->duplicate= text_duplicate;
489         st->operatortypes= text_operatortypes;
490         st->keymap= text_keymap;
491         st->listener= text_listener;
492         st->context= text_context;
493         st->dropboxes = text_dropboxes;
494         
495         /* regions: main window */
496         art= MEM_callocN(sizeof(ARegionType), "spacetype text region");
497         art->regionid = RGN_TYPE_WINDOW;
498         art->init= text_main_area_init;
499         art->draw= text_main_area_draw;
500         art->cursor= text_cursor;
501
502         BLI_addhead(&st->regiontypes, art);
503         
504         /* regions: properties */
505         art= MEM_callocN(sizeof(ARegionType), "spacetype text region");
506         art->regionid = RGN_TYPE_UI;
507         art->prefsizex= UI_COMPACT_PANEL_WIDTH;
508         art->keymapflag= ED_KEYMAP_UI;
509         
510         art->init= text_properties_area_init;
511         art->draw= text_properties_area_draw;
512         BLI_addhead(&st->regiontypes, art);
513
514         /* regions: header */
515         art= MEM_callocN(sizeof(ARegionType), "spacetype text region");
516         art->regionid = RGN_TYPE_HEADER;
517         art->prefsizey= HEADERY;
518         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_HEADER;
519         
520         art->init= text_header_area_init;
521         art->draw= text_header_area_draw;
522
523         BLI_addhead(&st->regiontypes, art);
524
525         BKE_spacetype_register(st);
526 }
527