Drag and drop 2.5 integration! Finally, slashdot regulars can use
[blender.git] / source / blender / editors / space_console / console_report.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) 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_execute", 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->description= "Replay selected reports.";
118         ot->idname= "CONSOLE_OT_report_replay";
119
120         /* api callbacks */
121         ot->poll= console_report_poll;
122         ot->exec= report_replay_exec;
123
124         /* flags */
125         /* ot->flag= OPTYPE_REGISTER; */
126
127         /* properties */
128 }
129
130 static int select_report_pick_exec(bContext *C, wmOperator *op)
131 {
132         int report_index= RNA_int_get(op->ptr, "report_index");
133         Report *report= BLI_findlink(&CTX_wm_reports(C)->list, report_index);
134
135         if(!report)
136                 return OPERATOR_CANCELLED;
137
138         report->flag ^= SELECT; /* toggle */
139
140         ED_area_tag_redraw(CTX_wm_area(C));
141
142         return OPERATOR_FINISHED;
143 }
144
145 static int select_report_pick_invoke(bContext *C, wmOperator *op, wmEvent *event)
146 {
147         SpaceConsole *sc= CTX_wm_space_console(C);
148         ARegion *ar= CTX_wm_region(C);
149         ReportList *reports= CTX_wm_reports(C);
150         Report *report;
151
152         report= console_text_pick(sc, ar, reports, event->mval[1]);
153
154         RNA_int_set(op->ptr, "report_index", BLI_findindex(&reports->list, report));
155
156         return select_report_pick_exec(C, op);
157 }
158
159
160 void CONSOLE_OT_select_pick(wmOperatorType *ot)
161 {
162         /* identifiers */
163         ot->name= "Select report";
164     ot->description= "Select reports by index.";
165         ot->idname= "CONSOLE_OT_select_pick";
166
167         /* api callbacks */
168         ot->poll= console_report_poll;
169         ot->invoke= select_report_pick_invoke;
170         ot->exec= select_report_pick_exec;
171
172         /* flags */
173         /* ot->flag= OPTYPE_REGISTER; */
174
175         /* properties */
176         RNA_def_int(ot->srna, "report_index", 0, 0, INT_MAX, "Report", "The index of the report.", 0, INT_MAX);
177 }
178
179
180
181 static int report_select_all_toggle_exec(bContext *C, wmOperator *op)
182 {
183         SpaceConsole *sc= CTX_wm_space_console(C);
184         ReportList *reports= CTX_wm_reports(C);
185         int report_mask= console_report_mask(sc);
186         int deselect= 0;
187
188         Report *report;
189
190         for(report=reports->list.last; report; report=report->prev) {
191                 if((report->type & report_mask) && (report->flag & SELECT)) {
192                         deselect= 1;
193                         break;
194                 }
195         }
196
197
198         if(deselect) {
199                 for(report=reports->list.last; report; report=report->prev)
200                         if(report->type & report_mask)
201                                 report->flag &= ~SELECT;
202         }
203         else {
204                 for(report=reports->list.last; report; report=report->prev)
205                         if(report->type & report_mask)
206                                 report->flag |= SELECT;
207         }
208
209         ED_area_tag_redraw(CTX_wm_area(C));
210
211         return OPERATOR_FINISHED;
212 }
213
214 void CONSOLE_OT_select_all_toggle(wmOperatorType *ot)
215 {
216         /* identifiers */
217         ot->name= "(De)Select All";
218     ot->description= "(de)select all reports.";
219         ot->idname= "CONSOLE_OT_select_all_toggle";
220
221         /* api callbacks */
222         ot->poll= console_report_poll;
223         ot->exec= report_select_all_toggle_exec;
224
225         /* flags */
226         /*ot->flag= OPTYPE_REGISTER;*/
227
228         /* properties */
229 }
230
231 /* borderselect operator */
232 static int borderselect_exec(bContext *C, wmOperator *op)
233 {
234         SpaceConsole *sc= CTX_wm_space_console(C);
235         ARegion *ar= CTX_wm_region(C);
236         ReportList *reports= CTX_wm_reports(C);
237         int report_mask= console_report_mask(sc);
238         Report *report_min, *report_max, *report;
239
240         //View2D *v2d= UI_view2d_fromcontext(C);
241
242
243         rcti rect;
244         //rctf rectf, rq;
245         short selecting= (RNA_int_get(op->ptr, "gesture_mode")==GESTURE_MODAL_SELECT);
246         //short mval[2];
247
248         rect.xmin= RNA_int_get(op->ptr, "xmin");
249         rect.ymin= RNA_int_get(op->ptr, "ymin");
250         rect.xmax= RNA_int_get(op->ptr, "xmax");
251         rect.ymax= RNA_int_get(op->ptr, "ymax");
252
253         /*
254         mval[0]= rect.xmin;
255         mval[1]= rect.ymin;
256         UI_view2d_region_to_view(v2d, mval[0], mval[1], &rectf.xmin, &rectf.ymin);
257         mval[0]= rect.xmax;
258         mval[1]= rect.ymax;
259         UI_view2d_region_to_view(v2d, mval[0], mval[1], &rectf.xmax, &rectf.ymax);
260 */
261
262         report_min= console_text_pick(sc, ar, reports, rect.ymax);
263         report_max= console_text_pick(sc, ar, reports, rect.ymin);
264
265         /* get the first report if none found */
266         if(report_min==NULL) {
267                 // printf("find_min\n");
268                 for(report=reports->list.first; report; report=report->next) {
269                         if(report->type & report_mask) {
270                                 report_min= report;
271                                 break;
272                         }
273                 }
274         }
275
276         if(report_max==NULL) {
277                 // printf("find_max\n");
278                 for(report=reports->list.last; report; report=report->prev) {
279                         if(report->type & report_mask) {
280                                 report_max= report;
281                                 break;
282                         }
283                 }
284         }
285
286         if(report_min==NULL || report_max==NULL)
287                 return OPERATOR_CANCELLED;
288
289         for(report= report_min; (report != report_max->next); report= report->next) {
290
291                 if((report->type & report_mask)==0)
292                         continue;
293
294                 if(selecting)
295                         report->flag |= SELECT;
296                 else
297                         report->flag &= ~SELECT;
298         }
299
300         ED_area_tag_redraw(CTX_wm_area(C));
301
302         return OPERATOR_FINISHED;
303 }
304
305
306 /* ****** Border Select ****** */
307 void CONSOLE_OT_select_border(wmOperatorType *ot)
308 {
309         /* identifiers */
310         ot->name= "Border Select";
311     ot->description= "Toggle border selection.";
312         ot->idname= "CONSOLE_OT_select_border";
313
314         /* api callbacks */
315         ot->invoke= WM_border_select_invoke;
316         ot->exec= borderselect_exec;
317         ot->modal= WM_border_select_modal;
318
319         ot->poll= console_report_poll;
320
321         /* flags */
322         /* ot->flag= OPTYPE_REGISTER; */
323
324         /* rna */
325         WM_operator_properties_gesture_border(ot, FALSE);
326 }
327
328
329
330 static int report_delete_exec(bContext *C, wmOperator *op)
331 {
332         SpaceConsole *sc= CTX_wm_space_console(C);
333         ReportList *reports= CTX_wm_reports(C);
334         int report_mask= console_report_mask(sc);
335
336
337         Report *report, *report_next;
338
339         for(report=reports->list.first; report; ) {
340
341                 report_next=report->next;
342
343                 if((report->type & report_mask) && (report->flag & SELECT)) {
344                         BLI_remlink(&reports->list, report);
345                         MEM_freeN(report->message);
346                         MEM_freeN(report);
347                 }
348
349                 report= report_next;
350         }
351
352         ED_area_tag_redraw(CTX_wm_area(C));
353
354         return OPERATOR_FINISHED;
355 }
356
357 void CONSOLE_OT_report_delete(wmOperatorType *ot)
358 {
359         /* identifiers */
360         ot->name= "Delete Reports";
361     ot->description= "Delete selected reports.";
362         ot->idname= "CONSOLE_OT_report_delete";
363
364         /* api callbacks */
365         ot->poll= console_report_poll;
366         ot->exec= report_delete_exec;
367
368         /* flags */
369         /*ot->flag= OPTYPE_REGISTER;*/
370
371         /* properties */
372 }
373
374
375 static int report_copy_exec(bContext *C, wmOperator *op)
376 {
377         SpaceConsole *sc= CTX_wm_space_console(C);
378         ReportList *reports= CTX_wm_reports(C);
379         int report_mask= console_report_mask(sc);
380
381         Report *report;
382
383         DynStr *buf_dyn= BLI_dynstr_new();
384         char *buf_str;
385
386         for(report=reports->list.first; report; report= report->next) {
387                 if((report->type & report_mask) && (report->flag & SELECT)) {
388                         BLI_dynstr_append(buf_dyn, report->message);
389                         BLI_dynstr_append(buf_dyn, "\n");
390                 }
391         }
392
393         buf_str= BLI_dynstr_get_cstring(buf_dyn);
394         BLI_dynstr_free(buf_dyn);
395
396         WM_clipboard_text_set(buf_str, 0);
397
398         MEM_freeN(buf_str);
399         return OPERATOR_FINISHED;
400 }
401
402 void CONSOLE_OT_report_copy(wmOperatorType *ot)
403 {
404         /* identifiers */
405         ot->name= "Copy Reports to Clipboard";
406     ot->description= "Copy selected reports to Clipboard.";
407         ot->idname= "CONSOLE_OT_report_copy";
408
409         /* api callbacks */
410         ot->poll= console_report_poll;
411         ot->exec= report_copy_exec;
412
413         /* flags */
414         /*ot->flag= OPTYPE_REGISTER;*/
415
416         /* properties */
417 }