File: inputbox.c

package info (click to toggle)
pdmenu 1.2.96
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 840 kB
  • ctags: 250
  • sloc: sh: 3,048; ansic: 2,037; makefile: 131; perl: 58
file content (185 lines) | stat: -rw-r--r-- 4,843 bytes parent folder | download | duplicates (6)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* 
 * Copyright (c) 1995-1999 Joey Hess (joey@kitenet.net)
 * All rights reserved. See COPYING for full copyright information (GPL).
 */

#include "global.h"
#include "screen.h"
#include "menu.h"
#include "window.h"
#include "mouse.h"
#include "keyboard.h"
#include "pdmenu.h"
#include "inputbox.h"
#include "pdstring.h"
#include <string.h>
#include <stdlib.h>
#include "slang.h"
#include <libintl.h>
#define _(String) gettext (String)

/* 
 * Draw the input box on screen.
 */
void DrawInputBox (char *title) {
  /* Make the previous topmost menu be redrawn in unselected menu color. */
  if (!Retro)
    DrawMenu(CurrentWindow->menu,0);
  /* Draw the input box. */
  SLsmg_set_color(MENU);
  DrawDialog(title,1,(SLtt_Screen_Rows-3)/2,SLtt_Screen_Cols-2,3,1);
  SLsmg_set_color(SELBAR);
}

/*
 * Display an input box with title and text, get input.
 * ret->ignore is set if the user hits escape.
 */
Conditional_String *DoInputBox (char *title, char *contents) {
  int curspos,key,retcount;
  Conditional_String *ret=malloc(sizeof(Conditional_String));

  retcount=SLtt_Screen_Cols;
  ret->value=malloc(retcount + 1);
  strcpy(ret->value,contents);

#ifdef GPM_SUPPORT
  if (gpm_ok) 
    EndMouse(); /* let gpm cut and paste work as normal in edit box */
#endif

  curspos=strlen(ret->value);

  DrawBase(_("Press Enter when done, or Esc to cancel"));
  DrawInputBox(title);
	
  while (1) {
    SLsmg_gotorc((SLtt_Screen_Rows-3)/2+1,3);
    SLsmg_write_nstring(ret->value,SLtt_Screen_Cols-6);
    SLsmg_gotorc((SLtt_Screen_Rows-3)/2+1,3+curspos);
    SLsmg_refresh();
    key=getch();
    switch (key) {
    case 0:
      if (Want_Screen_Resize) {
	Resize_Screen();
	/*
	 * If the screen got wider, I need to malloc more memory for 
	 * ret->value 
	 */
	if (retcount < SLtt_Screen_Cols) {
	  retcount=SLtt_Screen_Cols;
	  ret->value=realloc(ret->value,retcount + 1);
	}
				
	/* This isn't done too well -- there's some flicker. */
	Force_Redraw();
	DrawBase(_("Press Enter when done, or Esc to cancel"));
	DrawInputBox(title);
      }
      break;
    case '\n':
    case '\r':
      DrawAll();
      ret->value[curspos]='\0';
#ifdef GPM_SUPPORT
      /* Start handling our own mouse events again. */
      if (gpm_ok)
	gpm_ok=InitMouse();
#endif
      ret->ignore=0;
      return ret;
    case 12: /* ctrl-l */
    case 18: /* ctrl-r */
      Force_Redraw();
      DrawInputBox(title);
      break;
    case SL_KEY_BACKSPACE: 
      /* backspace in my xterm generates 127. :-( */
    case 127: /* ctrl-h */
      if (curspos>0)
	ret->value[--curspos]='\0';
      break;
    case 27: /* escape */
      DrawAll();
#ifdef GPM_SUPPORT
      /* Start handling our own mouse events again. */
      if (gpm_ok)
	gpm_ok=InitMouse();
#endif
      ret->ignore=1;
      return ret;
    default:
      if ((key>=32) && (key<127) && (curspos<SLtt_Screen_Cols -6)) {
	ret->value[curspos++]=key;
	ret->value[curspos]='\0';
      }
    }
  }
}

/*
 * Process the string, replacing ~title:text~ flags with input from the user.
 * ret->ignore is set if the user hits escape.
 */
Conditional_String *EditTags(char *s) {
  int tagtitlestart,tagvaluestart,i,old_i,ok,retcount=0;
  char *tagtitle, *input;
  Conditional_String *cs, *ret=malloc(sizeof(Conditional_String));
  ret->value=malloc(strlen(s) + 1);

  for (i=0;i<strlen(s);i++) {
    if ((s[i] == '~') && 
	(((i>0) && (s[i-1]!='\\')) || (i==0))) { /* Start of tag (?) */
      old_i=i;
      ok=0;
      tagtitlestart=i+1;
      for (i++;i<strlen(s);i++) {
	if ((s[i]==':') && (s[i-1]!='\\')) { /* End of tag title */
	  tagtitle=malloc(i - tagtitlestart + 1);
	  strncpy(tagtitle,(char *) s + tagtitlestart,i - tagtitlestart);
	  tagtitle[i - tagtitlestart] ='\0';
	  tagtitle = unescape(tagtitle, ':');
	  tagvaluestart=i+1;
	  for (i++;i<strlen(s);i++) {
	    if ((s[i]=='~') && (s[i-1]!='\\')) { /* End of tag */
	      ok=1;
	      input=malloc(i - tagvaluestart + 1);
	      strncpy(input,(char *) s + tagvaluestart,i - tagvaluestart);
	      input[i - tagvaluestart]='\0';
	      cs=DoInputBox(tagtitle, input);
	      free(input);
	      free(tagtitle);
	      if (cs->ignore) { /* user hit escape */
		free(cs->value);
		free(cs);
		ret->ignore=1;
		return ret;
	      }
	      /*
	       * This is safe because ret->value[retcount] = the
	       * beginning of this tag so was allocated.
	       */
	      ret->value[retcount]='\0';
	      retcount=retcount+strlen(cs->value);
	      /* FIXME: this is allocating too much */
	      ret->value=realloc(ret->value,retcount + strlen(s) + 1);
	      strcat(ret->value,cs->value);
	      free(cs->value);
	      free(cs);
	      break;
	    }
	  }
	  break;
	}
      }
      if (ok==0) 
	i=old_i; /* wasn't a tag, after all */
    }
    else 
      ret->value[retcount++]=s[i];
  }
  ret->value[retcount]='\0';
  ret->ignore=0;
  return ret;
}