47f051e1ec40b0174298abbfc900883e908d949f
[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                                 case NA_SELECTED:
158                                         if(st->text && st->text == wmn->reference)
159                                                 text_scroll_to_cursor(st, sa);
160
161                                         break;
162                         }
163
164                         break;
165                 case NC_SPACE:
166                         if(wmn->data == ND_SPACE_TEXT)
167                                 ED_area_tag_redraw(sa);
168                         break;
169         }
170 }
171
172 static void text_operatortypes(void)
173 {
174         WM_operatortype_append(TEXT_OT_new);
175         WM_operatortype_append(TEXT_OT_open);
176         WM_operatortype_append(TEXT_OT_reload);
177         WM_operatortype_append(TEXT_OT_unlink);
178         WM_operatortype_append(TEXT_OT_save);
179         WM_operatortype_append(TEXT_OT_save_as);
180         WM_operatortype_append(TEXT_OT_make_internal);
181         WM_operatortype_append(TEXT_OT_run_script);
182         WM_operatortype_append(TEXT_OT_refresh_pyconstraints);
183
184         WM_operatortype_append(TEXT_OT_paste);
185         WM_operatortype_append(TEXT_OT_copy);
186         WM_operatortype_append(TEXT_OT_cut);
187
188         WM_operatortype_append(TEXT_OT_convert_whitespace);
189         WM_operatortype_append(TEXT_OT_uncomment);
190         WM_operatortype_append(TEXT_OT_comment);
191         WM_operatortype_append(TEXT_OT_unindent);
192         WM_operatortype_append(TEXT_OT_indent);
193
194         WM_operatortype_append(TEXT_OT_markers_clear);
195         WM_operatortype_append(TEXT_OT_next_marker);
196         WM_operatortype_append(TEXT_OT_previous_marker);
197
198         WM_operatortype_append(TEXT_OT_select_line);
199         WM_operatortype_append(TEXT_OT_select_all);
200         WM_operatortype_append(TEXT_OT_select_word);
201
202         WM_operatortype_append(TEXT_OT_jump);
203         WM_operatortype_append(TEXT_OT_move);
204         WM_operatortype_append(TEXT_OT_move_select);
205         WM_operatortype_append(TEXT_OT_delete);
206         WM_operatortype_append(TEXT_OT_overwrite_toggle);
207
208         WM_operatortype_append(TEXT_OT_selection_set);
209         WM_operatortype_append(TEXT_OT_cursor_set);
210         WM_operatortype_append(TEXT_OT_scroll);
211         WM_operatortype_append(TEXT_OT_scroll_bar);
212         WM_operatortype_append(TEXT_OT_line_number);
213
214         WM_operatortype_append(TEXT_OT_line_break);
215         WM_operatortype_append(TEXT_OT_insert);
216
217         WM_operatortype_append(TEXT_OT_properties);
218
219         WM_operatortype_append(TEXT_OT_find);
220         WM_operatortype_append(TEXT_OT_find_set_selected);
221         WM_operatortype_append(TEXT_OT_replace);
222         WM_operatortype_append(TEXT_OT_replace_set_selected);
223         WM_operatortype_append(TEXT_OT_mark_all);
224
225         WM_operatortype_append(TEXT_OT_to_3d_object);
226
227         WM_operatortype_append(TEXT_OT_resolve_conflict);
228 }
229
230 static void text_keymap(struct wmKeyConfig *keyconf)
231 {
232         wmKeyMap *keymap;
233         wmKeyMapItem *kmi;
234         
235         keymap= WM_keymap_find(keyconf, "Text", SPACE_TEXT, 0);
236         
237         #ifdef __APPLE__
238         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", LEFTARROWKEY, KM_PRESS, KM_OSKEY, 0)->ptr, "type", LINE_BEGIN);
239         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", RIGHTARROWKEY, KM_PRESS, KM_OSKEY, 0)->ptr, "type", LINE_END);
240         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", LEFTARROWKEY, KM_PRESS, KM_ALT, 0)->ptr, "type", PREV_WORD);
241         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", RIGHTARROWKEY, KM_PRESS, KM_ALT, 0)->ptr, "type", NEXT_WORD);
242         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", UPARROWKEY, KM_PRESS, KM_OSKEY, 0)->ptr, "type", FILE_TOP);
243         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", DOWNARROWKEY, KM_PRESS, KM_OSKEY, 0)->ptr, "type", FILE_BOTTOM);
244         
245         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", LEFTARROWKEY, KM_PRESS, KM_SHIFT|KM_OSKEY, 0)->ptr, "type", LINE_BEGIN);
246         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", RIGHTARROWKEY, KM_PRESS, KM_SHIFT|KM_OSKEY, 0)->ptr, "type", LINE_END);
247         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", LEFTARROWKEY, KM_PRESS, KM_SHIFT|KM_ALT, 0)->ptr, "type", PREV_WORD);
248         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", RIGHTARROWKEY, KM_PRESS, KM_SHIFT|KM_ALT, 0)->ptr, "type", NEXT_WORD);
249         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", UPARROWKEY, KM_PRESS, KM_SHIFT|KM_OSKEY, 0)->ptr, "type", FILE_TOP);
250         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", DOWNARROWKEY, KM_PRESS, KM_SHIFT|KM_OSKEY, 0)->ptr, "type", FILE_BOTTOM);
251         
252         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_delete", BACKSPACEKEY, KM_PRESS, KM_ALT, 0)->ptr, "type", DEL_PREV_WORD);
253         
254         WM_keymap_add_item(keymap, "TEXT_OT_save", SKEY, KM_PRESS, KM_ALT|KM_OSKEY, 0);
255         WM_keymap_add_item(keymap, "TEXT_OT_save_as", SKEY, KM_PRESS, KM_ALT|KM_SHIFT|KM_OSKEY, 0);
256         WM_keymap_add_item(keymap, "TEXT_OT_cut", XKEY, KM_PRESS, KM_OSKEY, 0);
257         WM_keymap_add_item(keymap, "TEXT_OT_copy", CKEY, KM_PRESS, KM_OSKEY, 0); 
258         WM_keymap_add_item(keymap, "TEXT_OT_paste", VKEY, KM_PRESS, KM_OSKEY, 0);
259         WM_keymap_add_item(keymap, "TEXT_OT_properties", FKEY, KM_PRESS, KM_OSKEY, 0);
260         WM_keymap_add_item(keymap, "TEXT_OT_find_set_selected", EKEY, KM_PRESS, KM_OSKEY, 0);
261         WM_keymap_add_item(keymap, "TEXT_OT_find", GKEY, KM_PRESS, KM_OSKEY, 0);
262         WM_keymap_add_item(keymap, "TEXT_OT_select_all", AKEY, KM_PRESS, KM_OSKEY, 0);
263         WM_keymap_add_item(keymap, "TEXT_OT_select_line", AKEY, KM_PRESS, KM_SHIFT|KM_OSKEY, 0);
264         #endif
265         
266         kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", WHEELUPMOUSE, KM_PRESS, KM_CTRL, 0);
267         RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
268         RNA_boolean_set(kmi->ptr, "reverse", 0);
269         
270         kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", WHEELDOWNMOUSE, KM_PRESS, KM_CTRL, 0);
271         RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
272         RNA_boolean_set(kmi->ptr, "reverse", 1);
273
274         kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", PADPLUSKEY, KM_PRESS, KM_CTRL, 0);
275         RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
276         RNA_boolean_set(kmi->ptr, "reverse", 0);
277         
278         kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", PADMINUS, KM_PRESS, KM_CTRL, 0);
279         RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
280         RNA_boolean_set(kmi->ptr, "reverse", 1);
281         
282         WM_keymap_add_item(keymap, "TEXT_OT_new", NKEY, KM_PRESS, KM_CTRL, 0);
283         WM_keymap_add_item(keymap, "TEXT_OT_open", OKEY, KM_PRESS, KM_ALT, 0);
284         WM_keymap_add_item(keymap, "TEXT_OT_reload", RKEY, KM_PRESS, KM_ALT, 0);
285         WM_keymap_add_item(keymap, "TEXT_OT_save", SKEY, KM_PRESS, KM_ALT, 0);
286         WM_keymap_add_item(keymap, "TEXT_OT_save_as", SKEY, KM_PRESS, KM_ALT|KM_SHIFT|KM_CTRL, 0);
287
288         WM_keymap_add_item(keymap, "TEXT_OT_run_script", PKEY, KM_PRESS, KM_ALT, 0);
289         
290         WM_keymap_add_item(keymap, "TEXT_OT_cut", XKEY, KM_PRESS, KM_CTRL, 0);
291         WM_keymap_add_item(keymap, "TEXT_OT_copy", CKEY, KM_PRESS, KM_CTRL, 0);
292         WM_keymap_add_item(keymap, "TEXT_OT_paste", VKEY, KM_PRESS, KM_CTRL, 0);
293
294         WM_keymap_add_item(keymap, "TEXT_OT_cut", DELKEY, KM_PRESS, KM_SHIFT, 0);
295         WM_keymap_add_item(keymap, "TEXT_OT_copy", INSERTKEY, KM_PRESS, KM_CTRL, 0);
296         WM_keymap_add_item(keymap, "TEXT_OT_paste", INSERTKEY, KM_PRESS, KM_SHIFT, 0);
297
298         if(U.uiflag & USER_MMB_PASTE) // XXX not dynamic
299                 RNA_boolean_set(WM_keymap_add_item(keymap, "TEXT_OT_paste", MIDDLEMOUSE, KM_PRESS, 0, 0)->ptr, "selection", 1);
300
301         WM_keymap_add_item(keymap, "TEXT_OT_jump", JKEY, KM_PRESS, KM_CTRL, 0);
302         WM_keymap_add_item(keymap, "TEXT_OT_find", GKEY, KM_PRESS, KM_CTRL, 0);
303         
304         WM_keymap_add_item(keymap, "TEXT_OT_properties", FKEY, KM_PRESS, KM_CTRL, 0);
305         WM_keymap_add_item(keymap, "TEXT_OT_replace", HKEY, KM_PRESS, KM_CTRL, 0);
306
307         WM_keymap_add_item(keymap, "TEXT_OT_to_3d_object", MKEY, KM_PRESS, KM_ALT, 0);
308         RNA_boolean_set(WM_keymap_add_item(keymap, "TEXT_OT_to_3d_object", MKEY, KM_PRESS, KM_CTRL, 0)->ptr, "split_lines", 1);
309
310         WM_keymap_add_item(keymap, "TEXT_OT_select_all", AKEY, KM_PRESS, KM_CTRL, 0);
311         WM_keymap_add_item(keymap, "TEXT_OT_select_line", AKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0);
312         WM_keymap_add_item(keymap, "TEXT_OT_select_word", LEFTMOUSE, KM_DBL_CLICK, 0, 0);
313         
314
315         WM_keymap_add_item(keymap, "TEXT_OT_indent", TABKEY, KM_PRESS, 0, 0);
316         WM_keymap_add_item(keymap, "TEXT_OT_unindent", TABKEY, KM_PRESS, KM_SHIFT, 0);
317         WM_keymap_add_item(keymap, "TEXT_OT_uncomment", DKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
318
319         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", HOMEKEY, KM_PRESS, 0, 0)->ptr, "type", LINE_BEGIN);
320         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", ENDKEY, KM_PRESS, 0, 0)->ptr, "type", LINE_END);
321         
322         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", EKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", LINE_END);
323         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", EKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0)->ptr, "type", LINE_END);
324         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", LEFTARROWKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_CHAR);
325         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", RIGHTARROWKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_CHAR);
326         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", LEFTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", PREV_WORD);
327         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", RIGHTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", NEXT_WORD);
328         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", UPARROWKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_LINE);
329         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", DOWNARROWKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_LINE);
330         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", PAGEUPKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_PAGE);
331         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", PAGEDOWNKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_PAGE);
332         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", HOMEKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", FILE_TOP);
333         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", ENDKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", FILE_BOTTOM);
334
335         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", HOMEKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", LINE_BEGIN);
336         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", ENDKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", LINE_END);
337         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", LEFTARROWKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", PREV_CHAR);
338         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", RIGHTARROWKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", NEXT_CHAR);
339         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", LEFTARROWKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0)->ptr, "type", PREV_WORD);
340         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", RIGHTARROWKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0)->ptr, "type", NEXT_WORD);
341         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", UPARROWKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", PREV_LINE);
342         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", DOWNARROWKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", NEXT_LINE);
343         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", PAGEUPKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", PREV_PAGE);
344         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", PAGEDOWNKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", NEXT_PAGE);
345         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", HOMEKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0)->ptr, "type", FILE_TOP);
346         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", ENDKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0)->ptr, "type", FILE_BOTTOM);
347
348         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_delete", DELKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_NEXT_CHAR);
349         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_delete", DKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", DEL_NEXT_CHAR);
350         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_delete", BACKSPACEKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_PREV_CHAR);
351         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] */
352         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_delete", DELKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", DEL_NEXT_WORD);
353         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_delete", BACKSPACEKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", DEL_PREV_WORD);
354         
355         WM_keymap_add_item(keymap, "TEXT_OT_overwrite_toggle", INSERTKEY, KM_PRESS, 0, 0);
356
357         WM_keymap_add_item(keymap, "TEXT_OT_scroll", MIDDLEMOUSE, KM_PRESS, 0, 0);
358         WM_keymap_add_item(keymap, "TEXT_OT_scroll", MOUSEPAN, 0, 0, 0);
359         WM_keymap_add_item(keymap, "TEXT_OT_scroll_bar", LEFTMOUSE, KM_PRESS, 0, 0);
360         WM_keymap_add_item(keymap, "TEXT_OT_selection_set", EVT_TWEAK_L, KM_ANY, 0, 0);
361         WM_keymap_add_item(keymap, "TEXT_OT_cursor_set", LEFTMOUSE, KM_PRESS, 0, 0);
362         RNA_boolean_set(WM_keymap_add_item(keymap, "TEXT_OT_selection_set", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "select", 1);
363         RNA_int_set(WM_keymap_add_item(keymap, "TEXT_OT_scroll", WHEELUPMOUSE, KM_PRESS, 0, 0)->ptr, "lines", -1);
364         RNA_int_set(WM_keymap_add_item(keymap, "TEXT_OT_scroll", WHEELDOWNMOUSE, KM_PRESS, 0, 0)->ptr, "lines", 1);
365
366         WM_keymap_add_item(keymap, "TEXT_OT_line_break", RETKEY, KM_PRESS, 0, 0);
367         WM_keymap_add_item(keymap, "TEXT_OT_line_break", PADENTER, KM_PRESS, 0, 0);
368
369         WM_keymap_add_menu(keymap, "TEXT_MT_toolbox", RIGHTMOUSE, KM_PRESS, KM_ANY, 0);
370         
371         WM_keymap_add_item(keymap, "TEXT_OT_line_number", KM_TEXTINPUT, KM_ANY, KM_ANY, 0);
372         WM_keymap_add_item(keymap, "TEXT_OT_insert", KM_TEXTINPUT, KM_ANY, KM_ANY, 0); // last!
373 }
374
375 const char *text_context_dir[] = {"edit_text", NULL};
376
377 static int text_context(const bContext *C, const char *member, bContextDataResult *result)
378 {
379         SpaceText *st= CTX_wm_space_text(C);
380
381         if(CTX_data_dir(member)) {
382                 CTX_data_dir_set(result, text_context_dir);
383                 return 1;
384         }
385         else if(CTX_data_equals(member, "edit_text")) {
386                 CTX_data_id_pointer_set(result, &st->text->id);
387                 return 1;
388         }
389
390         return 0;
391 }
392
393 /********************* main region ********************/
394
395 /* add handlers, stuff you only do once or on area/region changes */
396 static void text_main_area_init(wmWindowManager *wm, ARegion *ar)
397 {
398         wmKeyMap *keymap;
399         ListBase *lb;
400         
401         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
402         
403         /* own keymap */
404         keymap= WM_keymap_find(wm->defaultconf, "Text", SPACE_TEXT, 0);
405         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
406         
407         /* add drop boxes */
408         lb = WM_dropboxmap_find("Text", SPACE_TEXT, RGN_TYPE_WINDOW);
409         
410         WM_event_add_dropbox_handler(&ar->handlers, lb);
411 }
412
413 static void text_main_area_draw(const bContext *C, ARegion *ar)
414 {
415         /* draw entirely, view changes should be handled here */
416         SpaceText *st= CTX_wm_space_text(C);
417         //View2D *v2d= &ar->v2d;
418         
419         /* clear and setup matrix */
420         UI_ThemeClearColor(TH_BACK);
421         glClear(GL_COLOR_BUFFER_BIT);
422         
423         // UI_view2d_view_ortho(v2d);
424                 
425         /* data... */
426         draw_text_main(st, ar);
427         
428         /* reset view matrix */
429         // UI_view2d_view_restore(C);
430         
431         /* scrollers? */
432 }
433
434 static void text_cursor(wmWindow *win, ScrArea *UNUSED(sa), ARegion *UNUSED(ar))
435 {
436         WM_cursor_set(win, BC_TEXTEDITCURSOR);
437 }
438
439
440
441 /* ************* dropboxes ************* */
442
443 static int text_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
444 {
445         if(drag->type==WM_DRAG_PATH)
446                 if(ELEM(drag->icon, ICON_FILE_SCRIPT, ICON_FILE_BLANK)) /* rule might not work? */
447                         return 1;
448         return 0;
449 }
450
451 static void text_drop_copy(wmDrag *drag, wmDropBox *drop)
452 {
453         /* copy drag path to properties */
454         RNA_string_set(drop->ptr, "filepath", drag->path);
455 }
456
457 /* this region dropbox definition */
458 static void text_dropboxes(void)
459 {
460         ListBase *lb= WM_dropboxmap_find("Text", SPACE_TEXT, RGN_TYPE_WINDOW);
461         
462         WM_dropbox_add(lb, "TEXT_OT_open", text_drop_poll, text_drop_copy);
463
464 }
465
466 /* ************* end drop *********** */
467
468
469 /****************** header region ******************/
470
471 /* add handlers, stuff you only do once or on area/region changes */
472 static void text_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
473 {
474         ED_region_header_init(ar);
475 }
476
477 static void text_header_area_draw(const bContext *C, ARegion *ar)
478 {
479         ED_region_header(C, ar);
480 }
481
482 /****************** properties region ******************/
483
484 /* add handlers, stuff you only do once or on area/region changes */
485 static void text_properties_area_init(wmWindowManager *wm, ARegion *ar)
486 {
487         ED_region_panels_init(wm, ar);
488 }
489
490 static void text_properties_area_draw(const bContext *C, ARegion *ar)
491 {
492         ED_region_panels(C, ar, 1, NULL, -1);
493 }
494
495 /********************* registration ********************/
496
497 /* only called once, from space/spacetypes.c */
498 void ED_spacetype_text(void)
499 {
500         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype text");
501         ARegionType *art;
502         
503         st->spaceid= SPACE_TEXT;
504         strncpy(st->name, "Text", BKE_ST_MAXNAME);
505         
506         st->new= text_new;
507         st->free= text_free;
508         st->init= text_init;
509         st->duplicate= text_duplicate;
510         st->operatortypes= text_operatortypes;
511         st->keymap= text_keymap;
512         st->listener= text_listener;
513         st->context= text_context;
514         st->dropboxes = text_dropboxes;
515         
516         /* regions: main window */
517         art= MEM_callocN(sizeof(ARegionType), "spacetype text region");
518         art->regionid = RGN_TYPE_WINDOW;
519         art->init= text_main_area_init;
520         art->draw= text_main_area_draw;
521         art->cursor= text_cursor;
522
523         BLI_addhead(&st->regiontypes, art);
524         
525         /* regions: properties */
526         art= MEM_callocN(sizeof(ARegionType), "spacetype text region");
527         art->regionid = RGN_TYPE_UI;
528         art->prefsizex= UI_COMPACT_PANEL_WIDTH;
529         art->keymapflag= ED_KEYMAP_UI;
530         
531         art->init= text_properties_area_init;
532         art->draw= text_properties_area_draw;
533         BLI_addhead(&st->regiontypes, art);
534
535         /* regions: header */
536         art= MEM_callocN(sizeof(ARegionType), "spacetype text region");
537         art->regionid = RGN_TYPE_HEADER;
538         art->prefsizey= HEADERY;
539         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_HEADER;
540         
541         art->init= text_header_area_init;
542         art->draw= text_header_area_draw;
543
544         BLI_addhead(&st->regiontypes, art);
545
546         BKE_spacetype_register(st);
547 }
548