~ubuntu-branches/ubuntu/dapper/malaga/dapper

« back to all changes in this revision

Viewing changes to variables.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Bushnell, BSG
  • Date: 2005-01-10 11:52:04 UTC
  • mfrom: (2.1.2 hoary)
  • Revision ID: james.westby@ubuntu.com-20050110115204-hpgncw5pb0m1t8i6
Tags: 6.13-5
debian/control (malaga-doc Recommends): Suggest gv as a
postscript-viewer instead of ghostview.  (Closes: #289701).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 1995 Bjoern Beutel. */
 
2
 
 
3
/* Description. =============================================================*/
 
4
 
 
5
/* Read in and display Malaga Variables. */
 
6
 
 
7
/* Includes. ================================================================*/
 
8
 
 
9
#define _XOPEN_SOURCE
 
10
#include <stdio.h>
 
11
#include <stdlib.h>
 
12
#include <stdarg.h>
 
13
#include <setjmp.h>
 
14
#include <ctype.h>
 
15
#include <string.h>
 
16
#include <gtk/gtk.h>
 
17
#include "basic.h"
 
18
#include "scanner.h"
 
19
#include "input.h"
 
20
#include "canvas.h"
 
21
#include "variables.h"
 
22
 
 
23
/* Types. ===================================================================*/
 
24
 
 
25
typedef struct 
 
26
 
27
  list_node_t *next;
 
28
  string_t string;
 
29
  pos_string_t *name;
 
30
  pos_value_t *value;
 
31
  bool_t is_shown;
 
32
} variable_t;
 
33
 
 
34
typedef struct 
 
35
 
36
  list_node_t *next;
 
37
  string_t name;
 
38
} hidden_var_t;
 
39
 
 
40
/* Global variables. ========================================================*/
 
41
 
 
42
rectangle_t variables_geometry;
 
43
string_t variables_font_name;
 
44
int_t variables_font_size;
 
45
 
 
46
/* Variables. ===============================================================*/
 
47
 
 
48
static list_t variables;
 
49
static list_t hidden_vars;
 
50
static canvas_t *variables_canvas;
 
51
static pos_string_t *equal, *dots;
 
52
 
 
53
/* Functions. ===============================================================*/
 
54
 
 
55
static void
 
56
configure_variables( canvas_t *canvas, int_t *width_p, int_t *height_p )
 
57
{
 
58
  int_t width, height;
 
59
  variable_t *variable;
 
60
  int_t font_height = get_font_height( canvas );
 
61
  int_t space_width = get_space_width( canvas );
 
62
  int_t border_width = get_border_width( canvas );
 
63
 
 
64
  config_pos_string( equal, canvas );
 
65
  config_pos_string( dots, canvas );
 
66
 
 
67
  width = height = border_width;
 
68
  FOREACH( variable, variables ) 
 
69
  { 
 
70
    if (variable != (variable_t *) variables.first) 
 
71
      height += font_height;
 
72
 
 
73
    config_pos_string( variable->name, canvas );
 
74
    variable->name->x = border_width;
 
75
    variable->name->y = height;
 
76
    equal->x = variable->name->x + variable->name->width + space_width;
 
77
    if (variable->is_shown)
 
78
    {
 
79
      config_pos_value( variable->value, canvas );
 
80
      variable->value->x = equal->x + equal->width + space_width;
 
81
      variable->value->y = height;
 
82
      variable->name->y += (variable->value->height - font_height) / 2;
 
83
      width = MAX( width, variable->value->x + variable->value->width );
 
84
      height += variable->value->height;
 
85
    }
 
86
    else
 
87
    {
 
88
      dots->x = equal->x + equal->width + space_width;
 
89
      width = MAX( width, dots->x + dots->width );
 
90
      height += font_height;
 
91
    }
 
92
  }
 
93
 
 
94
  *width_p = width + border_width;
 
95
  *height_p = height + border_width;
 
96
}
 
97
 
 
98
/*---------------------------------------------------------------------------*/
 
99
 
 
100
static void
 
101
expose_variables( canvas_t *canvas, rectangle_t *area )
 
102
{
 
103
  int_t space_width = get_space_width( canvas );
 
104
  variable_t *variable;
 
105
  
 
106
  set_color( BLACK );
 
107
  FOREACH( variable, variables ) 
 
108
  { 
 
109
    draw_pos_string( variable->name, canvas );
 
110
    equal->x = variable->name->x + variable->name->width + space_width;
 
111
    equal->y = variable->name->y;
 
112
    draw_pos_string( equal, canvas );
 
113
    if (variable->is_shown) 
 
114
      draw_pos_value( variable->value, canvas );
 
115
    else 
 
116
    {
 
117
      dots->x = equal->x + equal->width + space_width;
 
118
      dots->y = equal->y;
 
119
      draw_pos_string( dots, canvas );
 
120
    }
 
121
  }
 
122
}
 
123
 
 
124
/*---------------------------------------------------------------------------*/
 
125
 
 
126
static void
 
127
show_variable( variable_t *variable, bool_t do_show )
 
128
{
 
129
  hidden_var_t *hidden_var;
 
130
 
 
131
  if (variable->is_shown == do_show) 
 
132
    return;
 
133
  if (do_show) 
 
134
  { 
 
135
    /* Remove the variable in the list of hidden variables. */
 
136
    FOREACH( hidden_var, hidden_vars )
 
137
    {
 
138
      if (strcmp_no_case( hidden_var->name, variable->string ) == 0) 
 
139
        break;
 
140
    }
 
141
    free_mem( &hidden_var->name );
 
142
    free_node( &hidden_vars, (list_node_t *) hidden_var );
 
143
  }
 
144
  else 
 
145
  { 
 
146
    /* Add the variable to the list of hidden variables. */
 
147
    hidden_var = new_node( &hidden_vars, sizeof( hidden_var_t ), LIST_END );
 
148
    hidden_var->name = new_string( variable->string, NULL );
 
149
  }
 
150
  variable->is_shown = do_show;
 
151
}
 
152
 
 
153
/*---------------------------------------------------------------------------*/
 
154
 
 
155
static bool_t
 
156
mouse_event( canvas_t *canvas, int_t x, int_t y, int_t button )
 
157
/* Called if pointer has entered CANVAS at position (X,Y), 
 
158
 * if mouse has been moved to position (X,Y),
 
159
 * or if BUTTON has been pressed at position (X,Y). */
 
160
{
 
161
  variable_t *variable;
 
162
  int_t font_height = get_font_height( canvas );
 
163
 
 
164
  FOREACH( variable, variables )
 
165
  {
 
166
    if (x >= variable->name->x && x < variable->name->x + variable->name->width
 
167
        && y >= variable->name->y && y < variable->name->y + font_height)
 
168
    {
 
169
      if (button != 0)
 
170
      {
 
171
        show_variable( variable, ! variable->is_shown );
 
172
        configure_canvas( canvas );
 
173
      }
 
174
      else 
 
175
        set_cursor( canvas, TRUE );
 
176
      return TRUE;
 
177
    }
 
178
  }
 
179
  set_cursor( canvas, FALSE );
 
180
  return FALSE;
 
181
}
 
182
 
 
183
/*---------------------------------------------------------------------------*/
 
184
 
 
185
static void
 
186
show_all_variables( canvas_t *canvas, guint do_show )
 
187
{
 
188
  variable_t *variable;
 
189
 
 
190
  FOREACH( variable, variables ) 
 
191
    show_variable( variable, do_show );
 
192
  configure_canvas( canvas );
 
193
}
 
194
 
 
195
/*---------------------------------------------------------------------------*/
 
196
 
 
197
static GtkItemFactoryEntry menu_items[] = 
 
198
{
 
199
  { "/Variables", NULL, NULL, 0, "<Branch>" },
 
200
  { "/Variables/Show All", NULL, show_all_variables, TRUE, NULL },
 
201
  { "/Variables/Hide All", NULL, show_all_variables, FALSE, NULL },
 
202
};
 
203
 
 
204
/*---------------------------------------------------------------------------*/
 
205
 
 
206
static void free_variables( canvas_t *canvas )
 
207
{
 
208
  variable_t *variable;
 
209
 
 
210
  free_pos_string( &equal );
 
211
  free_pos_string( &dots );
 
212
  FOREACH_FREE( variable, variables ) 
 
213
  { 
 
214
    free_mem( &variable->string );
 
215
    free_pos_string( &variable->name );
 
216
    free_pos_value( &variable->value );
 
217
  }
 
218
}
 
219
 
 
220
/*---------------------------------------------------------------------------*/
 
221
 
 
222
void 
 
223
read_variables( void )
 
224
/* Read new variables from STDIN. */
 
225
{
 
226
  string_t line;
 
227
  variable_t *variable;
 
228
  hidden_var_t *hidden_var;
 
229
 
 
230
  free_variables( NULL );
 
231
 
 
232
  while (TRUE) 
 
233
  { 
 
234
    line = read_line( stdin );
 
235
    if (line == NULL) 
 
236
      complain( "Premature EOF." );
 
237
    if (strcmp_no_case( line, "end" ) == 0) 
 
238
      break;
 
239
 
 
240
    /* Read a new variable. */
 
241
    variable = new_node( &variables, sizeof( variable_t ), LIST_END );
 
242
    set_scanner_input( line );
 
243
 
 
244
    /* Read variable name. */
 
245
    test_token( TOK_STRING );
 
246
    variable->name = new_pos_string( token_string );
 
247
    variable->string = new_string( token_string, NULL );
 
248
    read_next_token();
 
249
 
 
250
    /* Read variable value. */
 
251
    parse_token( '{' );
 
252
    variable->value = parse_pos_value();
 
253
    parse_token( '}' );
 
254
    parse_token( EOF );
 
255
    set_scanner_input( NULL );
 
256
    free_mem( &line );
 
257
 
 
258
    /* Check if variable is to be shown. */
 
259
    FOREACH( hidden_var, hidden_vars )
 
260
    {
 
261
      if (strcmp_no_case( hidden_var->name, variable->string ) == 0) 
 
262
        break;
 
263
    }
 
264
    variable->is_shown = (hidden_var == NULL);
 
265
  }
 
266
  free_mem( &line );
 
267
  
 
268
  equal = new_pos_string( "=" );
 
269
  dots = new_pos_string( "..." );
 
270
 
 
271
  if (variables_canvas == NULL) 
 
272
  { 
 
273
    variables_canvas = create_canvas(
 
274
      "Malaga Variables", "variables.eps", &variables_geometry, 
 
275
      configure_variables, expose_variables, free_variables, mouse_event,
 
276
      TRUE, menu_items, sizeof( menu_items ) / sizeof( menu_items[0] ) );
 
277
  } 
 
278
  else
 
279
  {
 
280
    configure_canvas( variables_canvas );
 
281
    show_canvas( variables_canvas );
 
282
  }
 
283
}
 
284
 
 
285
/* End of file. =============================================================*/