2.5: UI Layout Engine, initial code.
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 #include "DNA_space_types.h"
35 #include "DNA_scene_types.h"
36 #include "DNA_screen_types.h"
37 #include "DNA_userdef_types.h"
38
39 #include "MEM_guardedalloc.h"
40
41 #include "BLI_blenlib.h"
42 #include "BLI_arithb.h"
43 #include "BLI_rand.h"
44
45 #include "BKE_colortools.h"
46 #include "BKE_context.h"
47 #include "BKE_screen.h"
48
49 #include "ED_space_api.h"
50 #include "ED_screen.h"
51
52 #include "BIF_gl.h"
53
54 #include "WM_api.h"
55 #include "WM_types.h"
56
57 #include "UI_interface.h"
58 #include "UI_resources.h"
59 #include "UI_view2d.h"
60
61 #include "RNA_access.h"
62
63 #include "ED_markers.h"
64
65 #include "text_intern.h"        // own include
66
67 /* ******************** default callbacks for text space ***************** */
68
69 static SpaceLink *text_new(const bContext *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         
80         /* header */
81         ar= MEM_callocN(sizeof(ARegion), "header for text");
82         
83         BLI_addtail(&stext->regionbase, ar);
84         ar->regiontype= RGN_TYPE_HEADER;
85         ar->alignment= RGN_ALIGN_BOTTOM;
86         
87         /* main area */
88         ar= MEM_callocN(sizeof(ARegion), "main area for text");
89         
90         BLI_addtail(&stext->regionbase, ar);
91         ar->regiontype= RGN_TYPE_WINDOW;
92         
93         return (SpaceLink *)stext;
94 }
95
96 /* not spacelink itself */
97 static void text_free(SpaceLink *sl)
98 {       
99         SpaceText *stext= (SpaceText*) sl;
100         
101         stext->text= NULL;
102 }
103
104
105 /* spacetype; init callback */
106 static void text_init(struct wmWindowManager *wm, ScrArea *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         return (SpaceLink *)stextn;
118 }
119
120 static void text_listener(ScrArea *sa, wmNotifier *wmn)
121 {
122         SpaceText *st= sa->spacedata.first;
123
124         /* context changes */
125         switch(wmn->category) {
126                 case NC_TEXT:
127                         if(!wmn->reference || wmn->reference == st->text) {
128                                 ED_area_tag_redraw(sa);
129
130                                 if(wmn->data == ND_CURSOR) {
131                                         ARegion *ar;
132
133                                         for(ar=sa->regionbase.first; ar; ar= ar->next)
134                                                 if(ar->regiontype==RGN_TYPE_WINDOW)
135                                                         text_update_cursor_moved(st, ar);
136                                 }
137                         }
138                         else if(wmn->data == ND_DISPLAY)
139                                 ED_area_tag_redraw(sa);
140
141                         break;
142         }
143 }
144
145 static void text_operatortypes(void)
146 {
147         WM_operatortype_append(TEXT_OT_new);
148         WM_operatortype_append(TEXT_OT_open);
149         WM_operatortype_append(TEXT_OT_reload);
150         WM_operatortype_append(TEXT_OT_save);
151         WM_operatortype_append(TEXT_OT_save_as);
152         WM_operatortype_append(TEXT_OT_make_internal);
153         WM_operatortype_append(TEXT_OT_run_script);
154         WM_operatortype_append(TEXT_OT_refresh_pyconstraints);
155
156         WM_operatortype_append(TEXT_OT_paste);
157         WM_operatortype_append(TEXT_OT_copy);
158         WM_operatortype_append(TEXT_OT_cut);
159
160         WM_operatortype_append(TEXT_OT_convert_whitespace);
161         WM_operatortype_append(TEXT_OT_uncomment);
162         WM_operatortype_append(TEXT_OT_comment);
163         WM_operatortype_append(TEXT_OT_unindent);
164         WM_operatortype_append(TEXT_OT_indent);
165
166         WM_operatortype_append(TEXT_OT_clear_all_markers);
167         WM_operatortype_append(TEXT_OT_next_marker);
168         WM_operatortype_append(TEXT_OT_previous_marker);
169
170         WM_operatortype_append(TEXT_OT_select_line);
171         WM_operatortype_append(TEXT_OT_select_all);
172
173         WM_operatortype_append(TEXT_OT_jump);
174         WM_operatortype_append(TEXT_OT_move);
175         WM_operatortype_append(TEXT_OT_move_select);
176         WM_operatortype_append(TEXT_OT_delete);
177         WM_operatortype_append(TEXT_OT_toggle_overwrite);
178
179         WM_operatortype_append(TEXT_OT_set_cursor);
180         WM_operatortype_append(TEXT_OT_scroll);
181         WM_operatortype_append(TEXT_OT_scroll_bar);
182         WM_operatortype_append(TEXT_OT_line_number);
183
184         WM_operatortype_append(TEXT_OT_line_break);
185         WM_operatortype_append(TEXT_OT_insert);
186
187         WM_operatortype_append(TEXT_OT_properties);
188
189         WM_operatortype_append(TEXT_OT_find);
190         WM_operatortype_append(TEXT_OT_find_set_selected);
191         WM_operatortype_append(TEXT_OT_replace);
192         WM_operatortype_append(TEXT_OT_replace_set_selected);
193         WM_operatortype_append(TEXT_OT_mark_all);
194
195         WM_operatortype_append(TEXT_OT_to_3d_object);
196
197         WM_operatortype_append(TEXT_OT_resolve_conflict);
198 }
199
200 static void text_keymap(struct wmWindowManager *wm)
201 {
202         ListBase *keymap= WM_keymap_listbase(wm, "Text", SPACE_TEXT, 0);
203         
204         WM_keymap_add_item(keymap, "TEXT_OT_new", NKEY, KM_PRESS, KM_ALT, 0);
205         WM_keymap_add_item(keymap, "TEXT_OT_open", OKEY, KM_PRESS, KM_ALT, 0);
206         WM_keymap_add_item(keymap, "TEXT_OT_reload", RKEY, KM_PRESS, KM_ALT, 0);
207         WM_keymap_add_item(keymap, "TEXT_OT_save", SKEY, KM_PRESS, KM_ALT, 0);
208
209         WM_keymap_add_item(keymap, "TEXT_OT_run_script", PKEY, KM_PRESS, KM_ALT, 0);
210
211         WM_keymap_add_item(keymap, "TEXT_OT_cut", XKEY, KM_PRESS, KM_ALT, 0);
212         WM_keymap_add_item(keymap, "TEXT_OT_cut", XKEY, KM_PRESS, KM_CTRL, 0);
213         WM_keymap_add_item(keymap, "TEXT_OT_copy", CKEY, KM_PRESS, KM_ALT, 0);
214         WM_keymap_add_item(keymap, "TEXT_OT_copy", CKEY, KM_PRESS, KM_CTRL, 0);
215         WM_keymap_add_item(keymap, "TEXT_OT_paste", VKEY, KM_PRESS, KM_ALT, 0);
216         WM_keymap_add_item(keymap, "TEXT_OT_paste", VKEY, KM_PRESS, KM_CTRL, 0);
217
218         if(U.uiflag & USER_MMB_PASTE) // XXX not dynamic
219                 RNA_boolean_set(WM_keymap_add_item(keymap, "TEXT_OT_paste", MIDDLEMOUSE, KM_PRESS, 0, 0)->ptr, "selection", 1);
220
221         WM_keymap_add_item(keymap, "TEXT_OT_jump", JKEY, KM_PRESS, KM_ALT, 0);
222         WM_keymap_add_item(keymap, "TEXT_OT_find", FKEY, KM_PRESS, KM_ALT, 0);
223         WM_keymap_add_item(keymap, "TEXT_OT_properties", FKEY, KM_PRESS, KM_ALT, 0);
224         WM_keymap_add_item(keymap, "TEXT_OT_properties", FKEY, KM_PRESS, KM_CTRL, 0);
225         WM_keymap_add_item(keymap, "TEXT_OT_replace", HKEY, KM_PRESS, KM_ALT, 0);
226         WM_keymap_add_item(keymap, "TEXT_OT_replace", HKEY, KM_PRESS, KM_CTRL, 0);
227
228         WM_keymap_add_item(keymap, "TEXT_OT_to_3d_object", MKEY, KM_PRESS, KM_ALT, 0);
229
230         WM_keymap_add_item(keymap, "TEXT_OT_select_all", AKEY, KM_PRESS, KM_CTRL, 0);
231
232         WM_keymap_add_item(keymap, "TEXT_OT_indent", TABKEY, KM_PRESS, 0, 0);
233         WM_keymap_add_item(keymap, "TEXT_OT_unindent", TABKEY, KM_PRESS, KM_SHIFT, 0);
234         WM_keymap_add_item(keymap, "TEXT_OT_uncomment", DKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
235
236         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", HOMEKEY, KM_PRESS, 0, 0)->ptr, "type", LINE_BEGIN);
237         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", LEFTARROWKEY, KM_PRESS, KM_OSKEY, 0)->ptr, "type", LINE_BEGIN);
238         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", ENDKEY, KM_PRESS, 0, 0)->ptr, "type", LINE_END);
239         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", RIGHTARROWKEY, KM_PRESS, KM_OSKEY, 0)->ptr, "type", LINE_BEGIN);
240         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", EKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", LINE_END);
241         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", EKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0)->ptr, "type", LINE_END);
242         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", ENDKEY, KM_PRESS, 0, 0)->ptr, "type", LINE_END);
243         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", LEFTARROWKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_CHAR);
244         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", RIGHTARROWKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_CHAR);
245         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", LEFTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", PREV_WORD);
246         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", RIGHTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", NEXT_WORD);
247         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", UPARROWKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_LINE);
248         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", DOWNARROWKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_LINE);
249         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", PAGEUPKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_PAGE);
250         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", PAGEDOWNKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_PAGE);
251
252         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", HOMEKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", LINE_BEGIN);
253         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", ENDKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", LINE_END);
254         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", LEFTARROWKEY, KM_PRESS, KM_SHIFT|KM_OSKEY, 0)->ptr, "type", LINE_BEGIN);
255         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", RIGHTARROWKEY, KM_PRESS, KM_SHIFT|KM_OSKEY, 0)->ptr, "type", LINE_BEGIN);
256         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", LEFTARROWKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", PREV_CHAR);
257         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", RIGHTARROWKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", NEXT_CHAR);
258         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", LEFTARROWKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0)->ptr, "type", PREV_WORD);
259         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", RIGHTARROWKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0)->ptr, "type", NEXT_WORD);
260         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", UPARROWKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", PREV_LINE);
261         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", DOWNARROWKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", NEXT_LINE);
262         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", PAGEUPKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", PREV_PAGE);
263         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", PAGEDOWNKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", NEXT_PAGE);
264
265         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_delete", DELKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_NEXT_CHAR);
266         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_delete", DKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", DEL_NEXT_CHAR);
267         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_delete", BACKSPACEKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_PREV_CHAR);
268         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_delete", DELKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", DEL_NEXT_WORD);
269         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_delete", BACKSPACEKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", DEL_PREV_WORD);
270
271         WM_keymap_add_item(keymap, "TEXT_OT_toggle_overwrite", INSERTKEY, KM_PRESS, 0, 0);
272
273         WM_keymap_add_item(keymap, "TEXT_OT_scroll", MIDDLEMOUSE, KM_PRESS, 0, 0);
274         WM_keymap_add_item(keymap, "TEXT_OT_scroll_bar", LEFTMOUSE, KM_PRESS, 0, 0);
275         WM_keymap_add_item(keymap, "TEXT_OT_set_cursor", LEFTMOUSE, KM_PRESS, 0, 0);
276         RNA_boolean_set(WM_keymap_add_item(keymap, "TEXT_OT_set_cursor", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "select", 1);
277         RNA_int_set(WM_keymap_add_item(keymap, "TEXT_OT_scroll", WHEELUPMOUSE, KM_PRESS, 0, 0)->ptr, "lines", -1);
278         RNA_int_set(WM_keymap_add_item(keymap, "TEXT_OT_scroll", WHEELDOWNMOUSE, KM_PRESS, 0, 0)->ptr, "lines", 1);
279
280         WM_keymap_add_item(keymap, "TEXT_OT_to_3d_object", MKEY, KM_PRESS, KM_ALT, 0);
281
282         WM_keymap_add_item(keymap, "TEXT_OT_line_break", RETKEY, KM_PRESS, 0, 0);
283         WM_keymap_add_item(keymap, "TEXT_OT_line_number", KM_TEXTINPUT, KM_ANY, KM_ANY, 0);
284         WM_keymap_add_item(keymap, "TEXT_OT_insert", KM_TEXTINPUT, KM_ANY, KM_ANY, 0); // last!
285 }
286
287 static int text_context(const bContext *C, bContextDataMember member, bContextDataResult *result)
288 {
289         SpaceText *st= CTX_wm_space_text(C);
290
291         if(member == CTX_DATA_EDIT_TEXT) {
292                 CTX_data_pointer_set(result, st->text);
293                 return 1;
294         }
295
296         return 0;
297 }
298
299 /********************* main region ********************/
300
301 /* add handlers, stuff you only do once or on area/region changes */
302 static void text_main_area_init(wmWindowManager *wm, ARegion *ar)
303 {
304         ListBase *keymap;
305         
306         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
307         
308         /* own keymap */
309         keymap= WM_keymap_listbase(wm, "Text", SPACE_TEXT, 0);  /* XXX weak? */
310         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
311 }
312
313 static void text_main_area_draw(const bContext *C, ARegion *ar)
314 {
315         /* draw entirely, view changes should be handled here */
316         SpaceText *st= CTX_wm_space_text(C);
317         //View2D *v2d= &ar->v2d;
318         float col[3];
319         
320         /* clear and setup matrix */
321         UI_GetThemeColor3fv(TH_BACK, col);
322         glClearColor(col[0], col[1], col[2], 0.0);
323         glClear(GL_COLOR_BUFFER_BIT);
324         
325         // UI_view2d_view_ortho(C, v2d);
326                 
327         /* data... */
328         draw_text_main(st, ar);
329         
330         /* reset view matrix */
331         // UI_view2d_view_restore(C);
332         
333         /* scrollers? */
334 }
335
336 static void text_cursor(wmWindow *win, ScrArea *sa, ARegion *ar)
337 {
338         WM_cursor_set(win, BC_TEXTEDITCURSOR);
339 }
340
341 /****************** header region ******************/
342
343 /* add handlers, stuff you only do once or on area/region changes */
344 static void text_header_area_init(wmWindowManager *wm, ARegion *ar)
345 {
346         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
347 }
348
349 static void text_header_area_draw(const bContext *C, ARegion *ar)
350 {
351         float col[3];
352         
353         /* clear */
354         if(ED_screen_area_active(C))
355                 UI_GetThemeColor3fv(TH_HEADER, col);
356         else
357                 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
358         
359         glClearColor(col[0], col[1], col[2], 0.0);
360         glClear(GL_COLOR_BUFFER_BIT);
361         
362         /* set view2d view matrix for scrolling (without scrollers) */
363         UI_view2d_view_ortho(C, &ar->v2d);
364         
365         text_header_buttons(C, ar);
366         
367         /* restore view matrix? */
368         UI_view2d_view_restore(C);
369 }
370
371 /****************** properties region ******************/
372
373 /* add handlers, stuff you only do once or on area/region changes */
374 static void text_properties_area_init(wmWindowManager *wm, ARegion *ar)
375 {
376         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_PANELS_UI, ar->winx, ar->winy);
377 }
378
379 static void text_properties_area_draw(const bContext *C, ARegion *ar)
380 {
381         float col[3];
382         
383         /* clear */
384         UI_GetThemeColor3fv(TH_HEADER, col);
385         glClearColor(col[0], col[1], col[2], 0.0);
386         glClear(GL_COLOR_BUFFER_BIT);
387         
388         /* set view2d view matrix for scrolling (without scrollers) */
389         UI_view2d_view_ortho(C, &ar->v2d);
390         
391         text_properties_buttons(C, ar);
392         
393         /* restore view matrix? */
394         UI_view2d_view_restore(C);
395 }
396
397 /********************* registration ********************/
398
399 /* only called once, from space/spacetypes.c */
400 void ED_spacetype_text(void)
401 {
402         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype text");
403         ARegionType *art;
404         
405         st->spaceid= SPACE_TEXT;
406         
407         st->new= text_new;
408         st->free= text_free;
409         st->init= text_init;
410         st->duplicate= text_duplicate;
411         st->operatortypes= text_operatortypes;
412         st->keymap= text_keymap;
413         st->listener= text_listener;
414         st->context= text_context;
415         
416         /* regions: main window */
417         art= MEM_callocN(sizeof(ARegionType), "spacetype text region");
418         art->regionid = RGN_TYPE_WINDOW;
419         art->init= text_main_area_init;
420         art->draw= text_main_area_draw;
421         art->cursor= text_cursor;
422
423         BLI_addhead(&st->regiontypes, art);
424         
425         /* regions: header */
426         art= MEM_callocN(sizeof(ARegionType), "spacetype text region");
427         art->regionid = RGN_TYPE_HEADER;
428         art->minsizey= HEADERY;
429         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
430         
431         art->init= text_header_area_init;
432         art->draw= text_header_area_draw;
433         
434         BLI_addhead(&st->regiontypes, art);
435
436         /* regions: properties */
437         art= MEM_callocN(sizeof(ARegionType), "spacetype text region");
438         art->regionid = RGN_TYPE_UI;
439         art->minsizex= UI_COMPACT_PANEL_WIDTH;
440         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
441         
442         art->init= text_properties_area_init;
443         art->draw= text_properties_area_draw;
444         
445         BLI_addhead(&st->regiontypes, art);
446         
447         BKE_spacetype_register(st);
448 }
449