style cleanup: follow style guide for formatting of if/for/while loops, and else...
[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         rect.xmin = RNA_int_get(op->ptr, "xmin");
235         rect.ymin = RNA_int_get(op->ptr, "ymin");
236         rect.xmax = RNA_int_get(op->ptr, "xmax");
237         rect.ymax = RNA_int_get(op->ptr, "ymax");
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 }