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