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