2 * $Id: console_ops.c 21679 2009-07-18 16:27:25Z campbellbarton $
4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): Campbell Barton
27 * ***** END GPL LICENSE BLOCK *****
33 #include "MEM_guardedalloc.h"
35 #include "DNA_scene_types.h"
36 #include "DNA_screen_types.h"
37 #include "DNA_space_types.h"
38 #include "DNA_windowmanager_types.h"
40 #include "BLI_blenlib.h"
41 #include "BLI_dynstr.h"
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"
50 #include "BKE_report.h"
55 #include "ED_screen.h"
57 #include "UI_interface.h"
58 #include "UI_resources.h"
60 #include "RNA_access.h"
61 #include "RNA_define.h"
63 #include "console_intern.h"
65 int console_report_mask(SpaceConsole *sc)
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;
78 static int console_report_poll(bContext *C)
80 SpaceConsole *sc= CTX_wm_space_console(C);
82 if(!sc || sc->type != CONSOLE_TYPE_REPORT)
88 static int report_replay_exec(bContext *C, wmOperator *op)
90 SpaceConsole *sc= CTX_wm_space_console(C);
91 ReportList *reports= CTX_wm_reports(C);
92 int report_mask= console_report_mask(sc);
95 sc->type= CONSOLE_TYPE_PYTHON;
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);
102 ED_area_tag_redraw(CTX_wm_area(C));
106 sc->type= CONSOLE_TYPE_REPORT;
108 ED_area_tag_redraw(CTX_wm_area(C));
110 return OPERATOR_FINISHED;
113 void CONSOLE_OT_report_replay(wmOperatorType *ot)
116 ot->name= "Replay Operators";
117 ot->idname= "CONSOLE_OT_report_replay";
120 ot->poll= console_report_poll;
121 ot->exec= report_replay_exec;
124 /* ot->flag= OPTYPE_REGISTER; */
129 static int select_report_pick_exec(bContext *C, wmOperator *op)
131 int report_index= RNA_int_get(op->ptr, "report_index");
132 Report *report= BLI_findlink(&CTX_wm_reports(C)->list, report_index);
135 return OPERATOR_CANCELLED;
137 report->flag ^= SELECT; /* toggle */
139 ED_area_tag_redraw(CTX_wm_area(C));
141 return OPERATOR_FINISHED;
144 static int select_report_pick_invoke(bContext *C, wmOperator *op, wmEvent *event)
146 SpaceConsole *sc= CTX_wm_space_console(C);
147 ARegion *ar= CTX_wm_region(C);
148 ReportList *reports= CTX_wm_reports(C);
151 report= console_text_pick(sc, ar, reports, event->mval[1]);
153 RNA_int_set(op->ptr, "report_index", BLI_findindex(&reports->list, report));
155 return select_report_pick_exec(C, op);
159 void CONSOLE_OT_select_pick(wmOperatorType *ot)
162 ot->name= "Select report";
163 ot->idname= "CONSOLE_OT_select_pick";
166 ot->poll= console_report_poll;
167 ot->invoke= select_report_pick_invoke;
168 ot->exec= select_report_pick_exec;
171 /* ot->flag= OPTYPE_REGISTER; */
174 RNA_def_int(ot->srna, "report_index", 0, 0, INT_MAX, "Report", "The index of the report.", 0, INT_MAX);
179 static int report_select_all_toggle_exec(bContext *C, wmOperator *op)
181 SpaceConsole *sc= CTX_wm_space_console(C);
182 ReportList *reports= CTX_wm_reports(C);
183 int report_mask= console_report_mask(sc);
188 for(report=reports->list.last; report; report=report->prev) {
189 if((report->type & report_mask) && (report->flag & SELECT)) {
197 for(report=reports->list.last; report; report=report->prev)
198 if(report->type & report_mask)
199 report->flag &= ~SELECT;
202 for(report=reports->list.last; report; report=report->prev)
203 if(report->type & report_mask)
204 report->flag |= SELECT;
207 ED_area_tag_redraw(CTX_wm_area(C));
209 return OPERATOR_FINISHED;
212 void CONSOLE_OT_select_all_toggle(wmOperatorType *ot)
215 ot->name= "(De)Select All";
216 ot->idname= "CONSOLE_OT_select_all_toggle";
219 ot->poll= console_report_poll;
220 ot->exec= report_select_all_toggle_exec;
223 /*ot->flag= OPTYPE_REGISTER;*/
228 /* borderselect operator */
229 static int borderselect_exec(bContext *C, wmOperator *op)
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;
237 //View2D *v2d= UI_view2d_fromcontext(C);
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");
254 UI_view2d_region_to_view(v2d, mval[0], mval[1], &rectf.xmin, &rectf.ymin);
257 UI_view2d_region_to_view(v2d, mval[0], mval[1], &rectf.xmax, &rectf.ymax);
260 report_min= console_text_pick(sc, ar, reports, rect.ymax);
261 report_max= console_text_pick(sc, ar, reports, rect.ymin);
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) {
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) {
284 if(report_min==NULL || report_max==NULL)
285 return OPERATOR_CANCELLED;
287 for(report= report_min; (report != report_max->next); report= report->next) {
289 if((report->type & report_mask)==0)
292 if(val==LEFTMOUSE) report->flag |= SELECT;
293 else report->flag &= ~SELECT;
296 ED_area_tag_redraw(CTX_wm_area(C));
298 return OPERATOR_FINISHED;
302 /* ****** Border Select ****** */
303 void CONSOLE_OT_select_border(wmOperatorType *ot)
306 ot->name= "Border Select";
307 ot->idname= "CONSOLE_OT_select_border";
310 ot->invoke= WM_border_select_invoke;
311 ot->exec= borderselect_exec;
312 ot->modal= WM_border_select_modal;
314 ot->poll= console_report_poll;
317 /* ot->flag= OPTYPE_REGISTER; */
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);
329 static int report_delete_exec(bContext *C, wmOperator *op)
331 SpaceConsole *sc= CTX_wm_space_console(C);
332 ReportList *reports= CTX_wm_reports(C);
333 int report_mask= console_report_mask(sc);
336 Report *report, *report_next;
338 for(report=reports->list.first; report; ) {
340 report_next=report->next;
342 if((report->type & report_mask) && (report->flag & SELECT)) {
343 BLI_remlink(&reports->list, report);
344 MEM_freeN(report->message);
351 ED_area_tag_redraw(CTX_wm_area(C));
353 return OPERATOR_FINISHED;
356 void CONSOLE_OT_report_delete(wmOperatorType *ot)
359 ot->name= "Delete Reports";
360 ot->idname= "CONSOLE_OT_report_delete";
363 ot->poll= console_report_poll;
364 ot->exec= report_delete_exec;
367 /*ot->flag= OPTYPE_REGISTER;*/
373 static int report_copy_exec(bContext *C, wmOperator *op)
375 SpaceConsole *sc= CTX_wm_space_console(C);
376 ReportList *reports= CTX_wm_reports(C);
377 int report_mask= console_report_mask(sc);
381 DynStr *buf_dyn= BLI_dynstr_new();
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");
391 buf_str= BLI_dynstr_get_cstring(buf_dyn);
392 BLI_dynstr_free(buf_dyn);
394 WM_clipboard_text_set(buf_str, 0);
397 return OPERATOR_FINISHED;
400 void CONSOLE_OT_report_copy(wmOperatorType *ot)
403 ot->name= "Copy Reports to Clipboard";
404 ot->idname= "CONSOLE_OT_report_copy";
407 ot->poll= console_report_poll;
408 ot->exec= report_copy_exec;
411 /*ot->flag= OPTYPE_REGISTER;*/