]> git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/pythonconsole.cpp
Initial python support
[xonotic/netradiant.git] / radiant / pythonconsole.cpp
1 /*
2    Copyright (C) 2018, Sebastian Schmidt
3    All Rights Reserved.
4
5    This file is part of NetRadiant.
6
7    NetRadiant is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    NetRadiant is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with NetRadiant; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21
22 #include "pythonconsole.h"
23
24 #include <uilib/uilib.h>
25 #include <gtk/gtk.h>
26 #include <gdk/gdkkeysyms.h>
27
28 #include "gtkutil/accelerator.h"
29 #include "gtkutil/messagebox.h"
30 #include "gtkutil/container.h"
31 #include "gtkutil/menu.h"
32 #include "gtkutil/nonmodal.h"
33
34 #include "pybindconnector.h"
35
36
37 ui::TextView g_pythonconsole_output{ui::null};
38
39 static gint Input_keypress(ui::Entry widget, GdkEventKey *event, gpointer data)
40 {
41     if (event->keyval == GDK_KEY_Return) {
42         try
43         {
44 //             auto result = PYBIND_callfunction("os", std::string(widget.text()));
45             PYBIND_exec(std::string(widget.text()));
46             std::string result = PYBIND_scope_as_string();
47             g_pythonconsole_output.text(result.c_str());
48         }
49         catch (const std::exception& e)
50         {
51             g_pythonconsole_output.text(e.what());
52         }
53         widget.text("");
54         return TRUE;
55     }
56     if (event->keyval == GDK_KEY_Escape) {
57         gtk_window_set_focus(widget.window(), NULL);
58         return TRUE;
59     }
60
61     return FALSE;
62 }
63
64 ui::Widget PythonConsole_constructWindow(ui::Window toplevel)
65 {
66     auto vBox = ui::VBox(FALSE, 2);
67     vBox.show();
68     gtk_container_set_border_width(GTK_CONTAINER(vBox), 2);
69     auto scr = ui::ScrolledWindow(ui::New);
70     scr.overflow(ui::Policy::AUTOMATIC, ui::Policy::AUTOMATIC);
71     gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scr), GTK_SHADOW_IN);
72     scr.show();
73
74     {
75         auto text = ui::TextView(ui::New);
76         text.dimensions(0, -1); // allow shrinking
77         gtk_text_view_set_wrap_mode(text, GTK_WRAP_WORD);
78         gtk_text_view_set_editable(text, FALSE);
79         scr.add(text);
80         text.show();
81         g_pythonconsole_output = text;
82
83     }
84
85     auto input = ui::Entry(ui::New);
86     input.show();
87     gtk_widget_set_events(input, GDK_KEY_PRESS_MASK);
88     input.connect("key_press_event", G_CALLBACK(Input_keypress), 0);
89
90     vBox.pack_start(scr, TRUE, TRUE, 0);
91     vBox.pack_end(input, FALSE, FALSE, 0);
92
93     gtk_container_set_focus_chain(GTK_CONTAINER(vBox), NULL);
94
95     return vBox;
96 }