Freeciv-3.2
Loading...
Searching...
No Matches
luaconsole.c
Go to the documentation of this file.
1/*****************************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12*****************************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18#include <gdk/gdkkeysyms.h>
19
20/* utility */
21#include "fcintl.h"
22#include "log.h"
23#include "shared.h"
24
25/* common */
26#include "featured_text.h"
27#include "game.h"
28
29/* client */
30#include "options.h"
31
32/* client/gui-gtk-3.22 */
33#include "chatline.h"
34#include "gui_main.h"
35#include "gui_stuff.h"
36
37/* client/luascript */
38#include "script_client.h"
39
40#include "luaconsole.h"
41
45
46#define MAX_LUACONSOLE_HISTORY 20
47
57
59
60
61static struct luaconsole_data *luaconsole_dialog_get(void);
62static void luaconsole_dialog_create(struct luaconsole_data *pdialog);
63static void luaconsole_dialog_refresh(struct luaconsole_data *pdialog);
64static void luaconsole_dialog_destroy(struct luaconsole_data *pdialog);
65
68 gpointer data);
70
71static void luaconsole_input_return(GtkEntry *w, gpointer data);
73
75 int response, gpointer data);
76static void luaconsole_load_file_popup(void);
78 gpointer data);
79
80/*************************************************************************/
84{
86
87 /* Create a container for the dialog. */
88 luaconsole = fc_calloc(1, sizeof(*luaconsole));
91
94
96}
97
98/*************************************************************************/
102{
103 if (luaconsole != NULL) {
106 }
107
110
112 }
113}
114
115/*************************************************************************/
119{
120 return luaconsole;
121}
122
123/*************************************************************************/
127{
128 struct luaconsole_data *pdialog = luaconsole_dialog_get();
129
130 fc_assert_ret(pdialog);
131 if (pdialog->shell == NULL) {
133 }
134
135 gui_dialog_present(pdialog->shell);
136 if (raise) {
137 gui_dialog_raise(pdialog->shell);
138 }
139}
140
141/*************************************************************************/
145{
146 struct luaconsole_data *pdialog = luaconsole_dialog_get();
147
148 fc_assert_ret(pdialog);
149 if (pdialog->shell == NULL) {
151 }
152}
153
154/*************************************************************************/
158{
159 struct luaconsole_data *pdialog = luaconsole_dialog_get();
160
161 fc_assert_ret_val(pdialog, FALSE);
162
163 return (NULL != pdialog->shell);
164}
165
166/*************************************************************************/
170{
171 struct luaconsole_data *pdialog = luaconsole_dialog_get();
172
173 fc_assert_ret(pdialog);
174
175 if (NULL != pdialog->shell) {
177 }
178}
179
180/*************************************************************************/
183static void luaconsole_dialog_create(struct luaconsole_data *pdialog)
184{
185 GtkWidget *entry, *box, *vbox, *sw, *text, *notebook;
186
187 fc_assert_ret(NULL != pdialog);
188
190 notebook = right_notebook;
191 } else {
192 notebook = bottom_notebook;
193 }
194
195 gui_dialog_new(&pdialog->shell, GTK_NOTEBOOK(notebook), pdialog, TRUE);
196 gui_dialog_set_title(pdialog->shell, _("Client Lua Console"));
197
198 box = pdialog->shell->vbox;
199
200 vbox = gtk_grid_new();
204
211
218 g_signal_connect(text, "size-allocate",
220
222 gtk_widget_realize(text);
224
225 pdialog->message_area = GTK_TEXT_VIEW(text);
227 &pdialog->message_area);
228
229 /* The lua console input line. */
231 g_object_set(entry, "margin", 2, NULL);
234 NULL);
235 g_signal_connect(entry, "key_press_event",
237 pdialog->entry = entry;
239 &pdialog->entry);
240
241 /* Load lua script command button. */
242 gui_dialog_add_button(pdialog->shell, "window-close", _("_Close"),
244
245 gui_dialog_add_button(pdialog->shell, "document-open",
246 _("Load Lua Script"), LUACONSOLE_RES_OPEN);
249
251 gui_dialog_show_all(pdialog->shell);
252}
253
254/*************************************************************************/
258{
259 const char *theinput;
260 struct luaconsole_data *pdialog = luaconsole_dialog_get();
261
262 fc_assert_ret(pdialog);
263 fc_assert_ret(pdialog->history_list);
264
266
267 if (*theinput) {
270
272 void *history_data;
273
274 history_data = genlist_get(pdialog->history_list, -1);
277 }
278
280 pdialog->history_pos = -1;
281 }
282
283 gtk_entry_set_text(w, "");
284}
285
286/*************************************************************************/
290 int response, gpointer data)
291{
292 switch (response) {
294 break;
295 default:
297 return;
298 }
299
300 switch (response) {
303 break;
304 }
305}
306
307/*************************************************************************/
311{
313
314 /* Create the selector */
317 _("_Cancel"), GTK_RESPONSE_CANCEL,
318 _("_Open"), GTK_RESPONSE_OK,
319 NULL);
322
323 g_signal_connect(filesel, "response",
325
326 /* Display that dialog */
328}
329
330/*************************************************************************/
334 gpointer data)
335{
336 if (response == GTK_RESPONSE_OK) {
339 -1, NULL, NULL, NULL);
340
341 if (NULL != filename) {
342 luaconsole_printf(ftc_luaconsole_input, "(file)> %s", filename);
343 script_client_do_file(filename);
344 g_free(filename);
345 }
346 }
348}
349
350/*************************************************************************/
354{
355 struct luaconsole_data *pdialog = luaconsole_dialog_get();
356
357 fc_assert_ret_val(pdialog, FALSE);
359
360 switch (ev->keyval) {
361 case GDK_KEY_Up:
362 if (pdialog->history_pos < genlist_size(pdialog->history_list) - 1) {
364 ++pdialog->history_pos));
366 }
367 return TRUE;
368
369 case GDK_KEY_Down:
370 if (pdialog->history_pos >= 0) {
371 pdialog->history_pos--;
372 }
373
374 if (pdialog->history_pos >= 0) {
376 pdialog->history_pos));
377 } else {
379 }
381 return TRUE;
382
383 default:
384 break;
385 }
386
387 return FALSE;
388}
389
390/*************************************************************************/
397 gpointer data)
398{
399 static int old_width = 0, old_height = 0;
400 if (allocation->width != old_width
401 || allocation->height != old_height) {
403 old_width = allocation->width;
404 old_height = allocation->height;
405 }
406}
407
408/*************************************************************************/
415{
416 struct luaconsole_data *pdialog = luaconsole_dialog_get();
417
418 fc_assert_ret(pdialog);
419
420 if (pdialog->shell) {
421 GtkTextIter end;
422
423 fc_assert_ret(NULL != pdialog->message_buffer);
424 fc_assert_ret(NULL != pdialog->message_area);
425
428 &end, 0.0, TRUE, 1.0, 0.0);
429 }
430}
431
432/*************************************************************************/
435static void luaconsole_dialog_refresh(struct luaconsole_data *pdialog)
436{
437 fc_assert_ret(NULL != pdialog);
438}
439
440/*************************************************************************/
443static void luaconsole_dialog_destroy(struct luaconsole_data *pdialog)
444{
445 fc_assert_ret(NULL != pdialog);
446
447 if (pdialog->shell) {
448 gui_dialog_destroy(pdialog->shell);
449 fc_assert(NULL == pdialog->shell);
450 }
451 fc_assert(NULL == pdialog->message_area);
452 fc_assert(NULL == pdialog->entry);
453}
454
455/*************************************************************************/
char * incite_cost
Definition comments.c:75
#define _(String)
Definition fcintl.h:67
const struct ft_color ftc_luaconsole_input
#define text_tag_list_iterate_end
#define text_tag_list_iterate(tags, ptag)
int ft_offset_t
bool genlist_remove(struct genlist *pgenlist, const void *punlink)
Definition genlist.c:329
struct genlist * genlist_new(void)
Definition genlist.c:31
void * genlist_get(const struct genlist *pgenlist, int idx)
Definition genlist.c:224
void genlist_prepend(struct genlist *pgenlist, void *data)
Definition genlist.c:526
void genlist_destroy(struct genlist *pgenlist)
Definition genlist.c:57
int genlist_size(const struct genlist *pgenlist)
Definition genlist.c:192
void set_message_buffer_view_link_handlers(GtkWidget *view)
Definition chatline.c:744
void scroll_if_necessary(GtkTextView *textview, GtkTextMark *scroll_target)
Definition chatline.c:557
void apply_text_tag(const struct text_tag *ptag, GtkTextBuffer *buf, ft_offset_t text_start_offset, const char *text)
Definition chatline.c:755
GtkWidget * bottom_notebook
Definition gui_main.c:129
GtkWidget * toplevel
Definition gui_main.c:125
GtkWidget * right_notebook
Definition gui_main.c:129
#define GUI_GTK_OPTION(optname)
Definition gui_main.h:25
void gui_dialog_destroy(struct gui_dialog *dlg)
Definition gui_stuff.c:954
void gui_dialog_present(struct gui_dialog *dlg)
Definition gui_stuff.c:835
void gui_dialog_raise(struct gui_dialog *dlg)
Definition gui_stuff.c:865
void gui_dialog_new(struct gui_dialog **pdlg, GtkNotebook *notebook, gpointer user_data, bool check_top)
Definition gui_stuff.c:517
void gui_dialog_response_set_callback(struct gui_dialog *dlg, GUI_DIALOG_RESPONSE_FUN fun)
Definition gui_stuff.c:988
void gui_dialog_show_all(struct gui_dialog *dlg)
Definition gui_stuff.c:795
void gui_dialog_set_title(struct gui_dialog *dlg, const char *title)
Definition gui_stuff.c:935
GtkWidget * gui_dialog_add_button(struct gui_dialog *dlg, const char *icon_name, const char *text, int response)
Definition gui_stuff.c:706
void setup_dialog(GtkWidget *shell, GtkWidget *parent)
Definition gui_stuff.c:287
#define MAX_LUACONSOLE_HISTORY
Definition luaconsole.c:46
void luaconsole_dialog_popdown(void)
Definition luaconsole.c:144
static struct luaconsole_data * luaconsole_dialog_get(void)
Definition luaconsole.c:118
static void luaconsole_dialog_scroll_to_bottom(void)
Definition luaconsole.c:414
void luaconsole_dialog_init(void)
Definition luaconsole.c:83
bool luaconsole_dialog_is_open(void)
Definition luaconsole.c:157
static void luaconsole_load_file_callback(GtkWidget *widget, gint response, gpointer data)
Definition luaconsole.c:333
static void luaconsole_dialog_create(struct luaconsole_data *pdialog)
Definition luaconsole.c:183
static void luaconsole_response_callback(struct gui_dialog *pgui_dialog, int response, gpointer data)
Definition luaconsole.c:289
static struct luaconsole_data * luaconsole
Definition luaconsole.c:58
static void luaconsole_dialog_refresh(struct luaconsole_data *pdialog)
Definition luaconsole.c:435
luaconsole_res
Definition luaconsole.c:42
@ LUACONSOLE_RES_OPEN
Definition luaconsole.c:43
static void luaconsole_dialog_destroy(struct luaconsole_data *pdialog)
Definition luaconsole.c:443
static void luaconsole_input_return(GtkEntry *w, gpointer data)
Definition luaconsole.c:257
void real_luaconsole_append(const char *astring, const struct text_tag_list *tags)
Definition luaconsole.c:459
static void luaconsole_load_file_popup(void)
Definition luaconsole.c:310
void real_luaconsole_dialog_update(void)
Definition luaconsole.c:169
void luaconsole_dialog_popup(bool raise)
Definition luaconsole.c:126
static gboolean luaconsole_input_handler(GtkWidget *w, GdkEventKey *ev)
Definition luaconsole.c:353
void luaconsole_dialog_done(void)
Definition luaconsole.c:101
static void luaconsole_dialog_area_size_allocate(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
Definition luaconsole.c:395
#define fc_assert_ret(condition)
Definition log.h:191
#define fc_assert(condition)
Definition log.h:176
#define fc_assert_ret_val(condition, val)
Definition log.h:194
void luaconsole_welcome_message(void)
void luaconsole_printf(const struct ft_color color, const char *format,...)
#define fc_calloc(n, esz)
Definition mem.h:38
#define fc_strdup(str)
Definition mem.h:43
@ GUI_GTK_MSGCHAT_SPLIT
Definition options.h:67
bool script_client_do_file(const char *filename)
bool script_client_do_string(const char *str)
GtkWidget * vbox
Definition gui_stuff.h:72
struct gui_dialog * shell
Definition luaconsole.c:49
struct genlist * history_list
Definition luaconsole.c:54
GtkWidget * entry
Definition luaconsole.c:52
GtkTextView * message_area
Definition luaconsole.c:51
GtkTextBuffer * message_buffer
Definition luaconsole.c:50
struct tm * fc_localtime(const time_t *timep, struct tm *result)
Definition support.c:1315
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47