operator macro playback (run operator reports in the console)
[blender-staging.git] / source / blender / editors / space_console / console_report.c
1 /**
2  * $Id: console_ops.c 21679 2009-07-18 16:27:25Z campbellbarton $
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) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): Campbell Barton
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "MEM_guardedalloc.h"
34
35 #include "DNA_scene_types.h"
36 #include "DNA_screen_types.h"
37 #include "DNA_space_types.h"
38 #include "DNA_windowmanager_types.h"
39
40 #include "BLI_blenlib.h"
41 #include "BLI_dynstr.h"
42 #include "PIL_time.h"
43
44 #include "BKE_utildefines.h"
45 #include "BKE_context.h"
46 #include "BKE_depsgraph.h"
47 #include "BKE_global.h"
48 #include "BKE_library.h"
49 #include "BKE_main.h"
50 #include "BKE_report.h"
51
52 #include "WM_api.h"
53 #include "WM_types.h"
54
55 #include "ED_screen.h"
56 #include "ED_types.h"
57 #include "UI_interface.h"
58 #include "UI_resources.h"
59
60 #include "RNA_access.h"
61 #include "RNA_define.h"
62
63 #include "console_intern.h"
64
65 int console_report_mask(SpaceConsole *sc)
66 {
67         int report_mask = 0;
68
69         if(sc->rpt_mask & CONSOLE_RPT_DEBUG)    report_mask |= RPT_DEBUG_ALL;
70         if(sc->rpt_mask & CONSOLE_RPT_INFO)             report_mask |= RPT_INFO_ALL;
71         if(sc->rpt_mask & CONSOLE_RPT_OP)               report_mask |= RPT_OPERATOR_ALL;
72         if(sc->rpt_mask & CONSOLE_RPT_WARN)             report_mask |= RPT_WARNING_ALL;
73         if(sc->rpt_mask & CONSOLE_RPT_ERR)              report_mask |= RPT_ERROR_ALL;
74
75         return report_mask;
76 }
77
78 static int console_report_poll(bContext *C)
79 {
80         SpaceConsole *sc= CTX_wm_space_console(C);
81
82         if(!sc || sc->type != CONSOLE_TYPE_REPORT)
83                 return 0;
84
85         return 1;
86 }
87
88 static int report_replay_exec(bContext *C, wmOperator *op)
89 {
90         SpaceConsole *sc= CTX_wm_space_console(C);
91         ReportList *reports= CTX_wm_reports(C);
92         int report_mask= console_report_mask(sc);
93         Report *report;
94
95         sc->type= CONSOLE_TYPE_PYTHON;
96
97         for(report=reports->list.last; report; report=report->prev) {
98                 if((report->type & report_mask) && (report->type & RPT_OPERATOR_ALL) && (report->flag & SELECT)) {
99                         console_history_add_str(C, report->message, 0);
100                         WM_operator_name_call(C, "CONSOLE_OT_exec", WM_OP_EXEC_DEFAULT, NULL);
101
102                         ED_area_tag_redraw(CTX_wm_area(C));
103                 }
104         }
105
106         sc->type= CONSOLE_TYPE_REPORT;
107
108         ED_area_tag_redraw(CTX_wm_area(C));
109
110         return OPERATOR_FINISHED;
111 }
112
113 void CONSOLE_OT_report_replay(wmOperatorType *ot)
114 {
115         /* identifiers */
116         ot->name= "Replay Operators";
117         ot->idname= "CONSOLE_OT_report_replay";
118
119         /* api callbacks */
120         ot->poll= console_report_poll;
121         ot->exec= report_replay_exec;
122
123         /* flags */
124         /* ot->flag= OPTYPE_REGISTER; */
125
126         /* properties */
127 }
128
129 static int select_report_pick_exec(bContext *C, wmOperator *op)
130 {
131         int report_index= RNA_int_get(op->ptr, "report_index");
132         Report *report= BLI_findlink(&CTX_wm_reports(C)->list, report_index);
133
134         if(!report)
135                 return OPERATOR_CANCELLED;
136
137         report->flag ^= SELECT; /* toggle */
138
139         ED_area_tag_redraw(CTX_wm_area(C));
140
141         return OPERATOR_FINISHED;
142 }
143
144 static int select_report_pick_invoke(bContext *C, wmOperator *op, wmEvent *event)
145 {
146         SpaceConsole *sc= CTX_wm_space_console(C);
147         ARegion *ar= CTX_wm_region(C);
148         ReportList *reports= CTX_wm_reports(C);
149         Report *report;
150
151         report= console_text_pick(sc, ar, reports, event->mval[1]);
152
153         RNA_int_set(op->ptr, "report_index", BLI_findindex(&reports->list, report));
154
155         return select_report_pick_exec(C, op);
156 }
157
158
159 void CONSOLE_OT_select_pick(wmOperatorType *ot)
160 {
161         /* identifiers */
162         ot->name= "Select report";
163         ot->idname= "CONSOLE_OT_select_pick";
164
165         /* api callbacks */
166         ot->poll= console_report_poll;
167         ot->invoke= select_report_pick_invoke;
168         ot->exec= select_report_pick_exec;
169
170         /* flags */
171         /* ot->flag= OPTYPE_REGISTER; */
172
173         /* properties */
174         RNA_def_int(ot->srna, "report_index", 0, 0, INT_MAX, "Report", "The index of the report.", 0, INT_MAX);
175 }
176
177
178
179 static int report_select_all_toggle_exec(bContext *C, wmOperator *op)
180 {
181         SpaceConsole *sc= CTX_wm_space_console(C);
182         ReportList *reports= CTX_wm_reports(C);
183         int report_mask= console_report_mask(sc);
184         int deselect= 0;
185
186         Report *report;
187
188         for(report=reports->list.last; report; report=report->prev) {
189                 if((report->type & report_mask) && (report->flag & SELECT)) {
190                         deselect= 1;
191                         break;
192                 }
193         }
194
195
196         if(deselect) {
197                 for(report=reports->list.last; report; report=report->prev)
198                         if(report->type & report_mask)
199                                 report->flag &= ~SELECT;
200         }
201         else {
202                 for(report=reports->list.last; report; report=report->prev)
203                         if(report->type & report_mask)
204                                 report->flag |= SELECT;
205         }
206
207         ED_area_tag_redraw(CTX_wm_area(C));
208
209         return OPERATOR_FINISHED;
210 }
211
212 void CONSOLE_OT_select_all_toggle(wmOperatorType *ot)
213 {
214         /* identifiers */
215         ot->name= "(De)Select All";
216         ot->idname= "CONSOLE_OT_select_all_toggle";
217
218         /* api callbacks */
219         ot->poll= console_report_poll;
220         ot->exec= report_select_all_toggle_exec;
221
222         /* flags */
223         /*ot->flag= OPTYPE_REGISTER;*/
224
225         /* properties */
226 }
227
228 /* borderselect operator */
229 static int borderselect_exec(bContext *C, wmOperator *op)
230 {
231         SpaceConsole *sc= CTX_wm_space_console(C);
232         ARegion *ar= CTX_wm_region(C);
233         ReportList *reports= CTX_wm_reports(C);
234         int report_mask= console_report_mask(sc);
235         Report *report_min, *report_max, *report;
236
237         //View2D *v2d= UI_view2d_fromcontext(C);
238
239
240         rcti rect;
241         //rctf rectf, rq;
242         int val;
243         //short mval[2];
244
245         val= RNA_int_get(op->ptr, "event_type");
246         rect.xmin= RNA_int_get(op->ptr, "xmin");
247         rect.ymin= RNA_int_get(op->ptr, "ymin");
248         rect.xmax= RNA_int_get(op->ptr, "xmax");
249         rect.ymax= RNA_int_get(op->ptr, "ymax");
250
251         /*
252         mval[0]= rect.xmin;
253         mval[1]= rect.ymin;
254         UI_view2d_region_to_view(v2d, mval[0], mval[1], &rectf.xmin, &rectf.ymin);
255         mval[0]= rect.xmax;
256         mval[1]= rect.ymax;
257         UI_view2d_region_to_view(v2d, mval[0], mval[1], &rectf.xmax, &rectf.ymax);
258 */
259
260         report_min= console_text_pick(sc, ar, reports, rect.ymax);
261         report_max= console_text_pick(sc, ar, reports, rect.ymin);
262
263         /* get the first report if none found */
264         if(report_min==NULL) {
265                 printf("find_min\n");
266                 for(report=reports->list.first; report; report=report->next) {
267                         if(report->type & report_mask) {
268                                 report_min= report;
269                                 break;
270                         }
271                 }
272         }
273
274         if(report_max==NULL) {
275                 printf("find_max\n");
276                 for(report=reports->list.last; report; report=report->prev) {
277                         if(report->type & report_mask) {
278                                 report_max= report;
279                                 break;
280                         }
281                 }
282         }
283
284         if(report_min==NULL || report_max==NULL)
285                 return OPERATOR_CANCELLED;
286
287         for(report= report_min; (report != report_max->next); report= report->next) {
288
289                 if((report->type & report_mask)==0)
290                         continue;
291
292                 if(val==LEFTMOUSE)      report->flag |= SELECT;
293                 else                            report->flag &= ~SELECT;
294         }
295
296         ED_area_tag_redraw(CTX_wm_area(C));
297
298         return OPERATOR_FINISHED;
299 }
300
301
302 /* ****** Border Select ****** */
303 void CONSOLE_OT_select_border(wmOperatorType *ot)
304 {
305         /* identifiers */
306         ot->name= "Border Select";
307         ot->idname= "CONSOLE_OT_select_border";
308
309         /* api callbacks */
310         ot->invoke= WM_border_select_invoke;
311         ot->exec= borderselect_exec;
312         ot->modal= WM_border_select_modal;
313
314         ot->poll= console_report_poll;
315
316         /* flags */
317         /* ot->flag= OPTYPE_REGISTER; */
318
319         /* rna */
320         RNA_def_int(ot->srna, "event_type", 0, INT_MIN, INT_MAX, "Event Type", "", INT_MIN, INT_MAX);
321         RNA_def_int(ot->srna, "xmin", 0, INT_MIN, INT_MAX, "X Min", "", INT_MIN, INT_MAX);
322         RNA_def_int(ot->srna, "xmax", 0, INT_MIN, INT_MAX, "X Max", "", INT_MIN, INT_MAX);
323         RNA_def_int(ot->srna, "ymin", 0, INT_MIN, INT_MAX, "Y Min", "", INT_MIN, INT_MAX);
324         RNA_def_int(ot->srna, "ymax", 0, INT_MIN, INT_MAX, "Y Max", "", INT_MIN, INT_MAX);
325 }
326
327
328
329 static int report_delete_exec(bContext *C, wmOperator *op)
330 {
331         SpaceConsole *sc= CTX_wm_space_console(C);
332         ReportList *reports= CTX_wm_reports(C);
333         int report_mask= console_report_mask(sc);
334
335
336         Report *report, *report_next;
337
338         for(report=reports->list.first; report; ) {
339
340                 report_next=report->next;
341
342                 if((report->type & report_mask) && (report->flag & SELECT)) {
343                         BLI_remlink(&reports->list, report);
344                         MEM_freeN(report->message);
345                         MEM_freeN(report);
346                 }
347
348                 report= report_next;
349         }
350
351         ED_area_tag_redraw(CTX_wm_area(C));
352
353         return OPERATOR_FINISHED;
354 }
355
356 void CONSOLE_OT_report_delete(wmOperatorType *ot)
357 {
358         /* identifiers */
359         ot->name= "Delete Reports";
360         ot->idname= "CONSOLE_OT_report_delete";
361
362         /* api callbacks */
363         ot->poll= console_report_poll;
364         ot->exec= report_delete_exec;
365
366         /* flags */
367         /*ot->flag= OPTYPE_REGISTER;*/
368
369         /* properties */
370 }
371
372
373 static int report_copy_exec(bContext *C, wmOperator *op)
374 {
375         SpaceConsole *sc= CTX_wm_space_console(C);
376         ReportList *reports= CTX_wm_reports(C);
377         int report_mask= console_report_mask(sc);
378
379         Report *report;
380
381         DynStr *buf_dyn= BLI_dynstr_new();
382         char *buf_str;
383
384         for(report=reports->list.first; report; report= report->next) {
385                 if((report->type & report_mask) && (report->flag & SELECT)) {
386                         BLI_dynstr_append(buf_dyn, report->message);
387                         BLI_dynstr_append(buf_dyn, "\n");
388                 }
389         }
390
391         buf_str= BLI_dynstr_get_cstring(buf_dyn);
392         BLI_dynstr_free(buf_dyn);
393
394         WM_clipboard_text_set(buf_str, 0);
395
396         MEM_freeN(buf_str);
397         return OPERATOR_FINISHED;
398 }
399
400 void CONSOLE_OT_report_copy(wmOperatorType *ot)
401 {
402         /* identifiers */
403         ot->name= "Copy Reports to Clipboard";
404         ot->idname= "CONSOLE_OT_report_copy";
405
406         /* api callbacks */
407         ot->poll= console_report_poll;
408         ot->exec= report_copy_exec;
409
410         /* flags */
411         /*ot->flag= OPTYPE_REGISTER;*/
412
413         /* properties */
414 }