Big i18n commit: add "reports" from bmesh/readfile/tracking/dynapaint (and a few...
[blender.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_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) && (report->type & RPT_OPERATOR_ALL) && (report->flag & SELECT)) {
81                         console_history_add_str(sc, report->message, 0);
82                         WM_operator_name_call(C, "CONSOLE_OT_execute", WM_OP_EXEC_DEFAULT, NULL);
83
84                         ED_area_tag_redraw(CTX_wm_area(C));
85                 }
86         }
87
88         sc->type = CONSOLE_TYPE_REPORT;
89 #endif
90         ED_area_tag_redraw(CTX_wm_area(C));
91
92         return OPERATOR_FINISHED;
93 }
94
95 void INFO_OT_report_replay(wmOperatorType *ot)
96 {
97         /* identifiers */
98         ot->name = "Replay Operators";
99         ot->description = "Replay selected reports";
100         ot->idname = "INFO_OT_report_replay";
101
102         /* api callbacks */
103         ot->poll = ED_operator_info_active;
104         ot->exec = report_replay_exec;
105
106         /* flags */
107         /* ot->flag = OPTYPE_REGISTER; */
108
109         /* properties */
110 }
111
112 static int select_report_pick_exec(bContext *C, wmOperator *op)
113 {
114         int report_index = RNA_int_get(op->ptr, "report_index");
115         Report *report = BLI_findlink(&CTX_wm_reports(C)->list, report_index);
116
117         if (!report)
118                 return OPERATOR_CANCELLED;
119
120         report->flag ^= SELECT; /* toggle */
121
122         ED_area_tag_redraw(CTX_wm_area(C));
123
124         return OPERATOR_FINISHED;
125 }
126
127 static int select_report_pick_invoke(bContext *C, wmOperator *op, wmEvent *event)
128 {
129         SpaceInfo *sinfo = CTX_wm_space_info(C);
130         ARegion *ar = CTX_wm_region(C);
131         ReportList *reports = CTX_wm_reports(C);
132         Report *report;
133
134         /* uses opengl */
135         wmSubWindowSet(CTX_wm_window(C), ar->swinid);
136         
137         report = info_text_pick(sinfo, ar, reports, event->mval[1]);
138
139         RNA_int_set(op->ptr, "report_index", BLI_findindex(&reports->list, report));
140
141         return select_report_pick_exec(C, op);
142 }
143
144
145 void INFO_OT_select_pick(wmOperatorType *ot)
146 {
147         /* identifiers */
148         ot->name = "Select report";
149         ot->description = "Select reports by index";
150         ot->idname = "INFO_OT_select_pick";
151
152         /* api callbacks */
153         ot->poll = ED_operator_info_active;
154         ot->invoke = select_report_pick_invoke;
155         ot->exec = select_report_pick_exec;
156
157         /* flags */
158         /* ot->flag = OPTYPE_REGISTER; */
159
160         /* properties */
161         RNA_def_int(ot->srna, "report_index", 0, 0, INT_MAX, "Report", "Index of the report", 0, INT_MAX);
162 }
163
164
165
166 static int report_select_all_toggle_exec(bContext *C, wmOperator *UNUSED(op))
167 {
168         SpaceInfo *sinfo = CTX_wm_space_info(C);
169         ReportList *reports = CTX_wm_reports(C);
170         int report_mask = info_report_mask(sinfo);
171         int deselect = 0;
172
173         Report *report;
174
175         for (report = reports->list.last; report; report = report->prev) {
176                 if ((report->type & report_mask) && (report->flag & SELECT)) {
177                         deselect = 1;
178                         break;
179                 }
180         }
181
182
183         if (deselect) {
184                 for (report = reports->list.last; report; report = report->prev)
185                         if (report->type & report_mask)
186                                 report->flag &= ~SELECT;
187         }
188         else {
189                 for (report = reports->list.last; report; report = report->prev)
190                         if (report->type & report_mask)
191                                 report->flag |= SELECT;
192         }
193
194         ED_area_tag_redraw(CTX_wm_area(C));
195
196         return OPERATOR_FINISHED;
197 }
198
199 void INFO_OT_select_all_toggle(wmOperatorType *ot)
200 {
201         /* identifiers */
202         ot->name = "(De)select All";
203         ot->description = "Select or deselect all reports";
204         ot->idname = "INFO_OT_select_all_toggle";
205
206         /* api callbacks */
207         ot->poll = ED_operator_info_active;
208         ot->exec = report_select_all_toggle_exec;
209
210         /* flags */
211         /*ot->flag = OPTYPE_REGISTER;*/
212
213         /* properties */
214 }
215
216 /* borderselect operator */
217 static int borderselect_exec(bContext *C, wmOperator *op)
218 {
219         SpaceInfo *sinfo = CTX_wm_space_info(C);
220         ARegion *ar = CTX_wm_region(C);
221         ReportList *reports = CTX_wm_reports(C);
222         int report_mask = info_report_mask(sinfo);
223         int extend = RNA_boolean_get(op->ptr, "extend");
224         Report *report_min, *report_max, *report;
225
226         //View2D *v2d = UI_view2d_fromcontext(C);
227
228
229         rcti rect;
230         //rctf rectf, rq;
231         short selecting = (RNA_int_get(op->ptr, "gesture_mode") == GESTURE_MODAL_SELECT);
232         //int mval[2];
233
234         WM_operator_properties_border_to_rcti(op, &rect);
235
236 #if 0
237         mval[0] = rect.xmin;
238         mval[1] = rect.ymin;
239         UI_view2d_region_to_view(v2d, mval[0], mval[1], &rectf.xmin, &rectf.ymin);
240         mval[0] = rect.xmax;
241         mval[1] = rect.ymax;
242         UI_view2d_region_to_view(v2d, mval[0], mval[1], &rectf.xmax, &rectf.ymax);
243 #endif
244
245         if (!extend) {
246                 for (report = reports->list.first; report; report = report->next) {
247
248                         if ((report->type & report_mask) == 0)
249                                 continue;
250
251                         report->flag &= ~SELECT;
252                 }
253         }
254
255         report_min = info_text_pick(sinfo, ar, reports, rect.ymax);
256         report_max = info_text_pick(sinfo, ar, reports, rect.ymin);
257
258         /* get the first report if none found */
259         if (report_min == NULL) {
260                 // printf("find_min\n");
261                 for (report = reports->list.first; report; report = report->next) {
262                         if (report->type & report_mask) {
263                                 report_min = report;
264                                 break;
265                         }
266                 }
267         }
268
269         if (report_max == NULL) {
270                 // printf("find_max\n");
271                 for (report = reports->list.last; report; report = report->prev) {
272                         if (report->type & report_mask) {
273                                 report_max = report;
274                                 break;
275                         }
276                 }
277         }
278
279         if (report_min == NULL || report_max == NULL)
280                 return OPERATOR_CANCELLED;
281
282         for (report = report_min; (report != report_max->next); report = report->next) {
283
284                 if ((report->type & report_mask) == 0)
285                         continue;
286
287                 if (selecting)
288                         report->flag |= SELECT;
289                 else
290                         report->flag &= ~SELECT;
291         }
292
293         ED_area_tag_redraw(CTX_wm_area(C));
294
295         return OPERATOR_FINISHED;
296 }
297
298
299 /* ****** Border Select ****** */
300 void INFO_OT_select_border(wmOperatorType *ot)
301 {
302         /* identifiers */
303         ot->name = "Border Select";
304         ot->description = "Toggle border selection";
305         ot->idname = "INFO_OT_select_border";
306
307         /* api callbacks */
308         ot->invoke = WM_border_select_invoke;
309         ot->exec = borderselect_exec;
310         ot->modal = WM_border_select_modal;
311         ot->cancel = WM_border_select_cancel;
312
313         ot->poll = ED_operator_info_active;
314
315         /* flags */
316         /* ot->flag = OPTYPE_REGISTER; */
317
318         /* rna */
319         WM_operator_properties_gesture_border(ot, TRUE);
320 }
321
322
323
324 static int report_delete_exec(bContext *C, wmOperator *UNUSED(op))
325 {
326         SpaceInfo *sinfo = CTX_wm_space_info(C);
327         ReportList *reports = CTX_wm_reports(C);
328         int report_mask = info_report_mask(sinfo);
329
330
331         Report *report, *report_next;
332
333         for (report = reports->list.first; report; ) {
334
335                 report_next = report->next;
336
337                 if ((report->type & report_mask) && (report->flag & SELECT)) {
338                         BLI_remlink(&reports->list, report);
339                         MEM_freeN((void *)report->message);
340                         MEM_freeN(report);
341                 }
342
343                 report = report_next;
344         }
345
346         ED_area_tag_redraw(CTX_wm_area(C));
347
348         return OPERATOR_FINISHED;
349 }
350
351 void INFO_OT_report_delete(wmOperatorType *ot)
352 {
353         /* identifiers */
354         ot->name = "Delete Reports";
355         ot->description = "Delete selected reports";
356         ot->idname = "INFO_OT_report_delete";
357
358         /* api callbacks */
359         ot->poll = ED_operator_info_active;
360         ot->exec = report_delete_exec;
361
362         /* flags */
363         /*ot->flag = OPTYPE_REGISTER;*/
364
365         /* properties */
366 }
367
368
369 static int report_copy_exec(bContext *C, wmOperator *UNUSED(op))
370 {
371         SpaceInfo *sinfo = CTX_wm_space_info(C);
372         ReportList *reports = CTX_wm_reports(C);
373         int report_mask = info_report_mask(sinfo);
374
375         Report *report;
376
377         DynStr *buf_dyn = BLI_dynstr_new();
378         char *buf_str;
379
380         for (report = reports->list.first; report; report = report->next) {
381                 if ((report->type & report_mask) && (report->flag & SELECT)) {
382                         BLI_dynstr_append(buf_dyn, report->message);
383                         BLI_dynstr_append(buf_dyn, "\n");
384                 }
385         }
386
387         buf_str = BLI_dynstr_get_cstring(buf_dyn);
388         BLI_dynstr_free(buf_dyn);
389
390         WM_clipboard_text_set(buf_str, 0);
391
392         MEM_freeN(buf_str);
393         return OPERATOR_FINISHED;
394 }
395
396 void INFO_OT_report_copy(wmOperatorType *ot)
397 {
398         /* identifiers */
399         ot->name = "Copy Reports to Clipboard";
400         ot->description = "Copy selected reports to Clipboard";
401         ot->idname = "INFO_OT_report_copy";
402
403         /* api callbacks */
404         ot->poll = ED_operator_info_active;
405         ot->exec = report_copy_exec;
406
407         /* flags */
408         /*ot->flag = OPTYPE_REGISTER;*/
409
410         /* properties */
411 }