File: commandread.cc

package info (click to toggle)
littlewizard 1.2.2-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,436 kB
  • sloc: sh: 9,389; cpp: 7,685; xml: 420; makefile: 380; awk: 40; ansic: 38
file content (150 lines) | stat: -rw-r--r-- 3,893 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 */

 /* (C) Marcin Kwadrans <quarter@users.sourceforge.net> */

#include <gdk/gdkkeysyms.h>
#include "include/support.h"
#include "include/command.h"
#include "include/message.h"
#include "include/environment.h"
#include "include/wizard.h"

class LWCommandRead: public LWCommand {

LWValue *execute1 (LWContext *context, guint argc, LWValue *args[])
{
	(void) args;
	
	g_return_val_if_fail (argc == 1, NULL);

	LWValue *value=new LWValue();
	
	g_queue_push_head (context->stack, (gpointer) value);
	
	context->resume = TRUE;
	context->wait_for = LW_RESUME_KEYPRESS;
	return NULL;
}	
	
LWValue *resume1 (LWContext *context, guint argc, LWValue *args[])	
{
	g_return_val_if_fail (argc == 1, NULL);

	LWValue *value = (LWValue *) g_queue_pop_head (context->stack);
	
	while (FALSE == g_queue_is_empty(context->queue_keys)) {
	
		guint key = GPOINTER_TO_UINT(g_queue_pop_head (context->queue_keys));

		LWValue *vchar = NULL;

			if (key >= GDK_A && key <= GDK_Z) {
				gchar *s = g_strdup_printf ("letter-%c", 'a'+key-GDK_A);
				vchar = new LWValue(LWEnvironment::getPixmapSet()->getPixmap(s));
				g_free (s);
			} else
			if (key >= GDK_a && key <= GDK_z) {
				gchar *s = g_strdup_printf ("letter-%c", 'a'+key-GDK_a);
				vchar = new LWValue(LWEnvironment::getPixmapSet()->getPixmap(s));
				g_free (s);
			} else
			if (key >= GDK_0 && key <= GDK_9) {
				gchar *s = g_strdup_printf ("%c", '0'+key-GDK_0);
				vchar = new LWValue(LWEnvironment::getPixmapSet()->getPixmap(s));
				g_free (s);
			} else
			if (key >= GDK_KP_0 && key <= GDK_KP_9) {
				gchar *s = g_strdup_printf ("%c", '0'+key-GDK_KP_0);
				vchar = new LWValue(LWEnvironment::getPixmapSet()->getPixmap(s));
				g_free (s);
			} else
			if (key == GDK_minus || key == GDK_KP_Subtract) {
				vchar = new LWValue(LWEnvironment::getPixmapSet()->getPixmap("-"));				
			} else
			if (key == GDK_space || key == GDK_KP_Space) {
				vchar = new LWValue((LWPixmap *) NULL);
			} else
			if (key == GDK_Return || key == GDK_KP_Enter) {
				args[0]->set (value);
				return NULL;
			} else
			if (key == GDK_BackSpace) {
				guint cnt = value->count();
				if (cnt > 0) {
					context->wizard->stepBack();
					LWValue v((LWPixmap *) NULL);
					context->wizard->create (&v);
					
					for (guint i=0; i < cnt; i++)
						context->wizard->stepBack();
					
					value->deleteIndex(cnt);
					context->wizard->create (value);
				}
				continue;
			} else
				continue;
		
			//Check is there a room for next character
			if (FALSE == context->wizard->isCreatingPossible())
				continue;
			
			for (guint i=0; i < value->count(); i++)
				context->wizard->stepBack();
		
			value->concat (vchar);
			delete vchar;
				
			context->wizard->create (value);
	}

	g_queue_push_head (context->stack, (gpointer) value);
	
	context->resume = TRUE;
	context->wait_for = LW_RESUME_KEYPRESS;
	return NULL;
}	

const gchar *getName (void) 
{
	return "read";
}	

gchar *getHint ()
{
	return _("Read a string from the user");
}

void checkArgument (guint n, LWValue *value)
{
	(void) n;
	
	if (FALSE == value->isVariable())
		throw new LWMessage (LW_ERROR_LValueIsNotVariable);
	
	if (TRUE == value->isSpecial())
		throw new LWMessage (LW_ERROR_DontUseSpecial);
}

void checkArgc (guint n)
{
	if (n != 1) 
		throw new LWMessage (LW_ERROR_WrongNumberOfArguments);
}

};

LWSymbol *new_LWCommandRead()
{
	return new LWCommandRead();
}