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