summaryrefslogtreecommitdiffhomepage
path: root/mixgtk/mixgtk_mixal.c
blob: 8cbb015552437773af6a9cc42a1090d8f78134cf (plain)
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* -*-c-*- -------------- mixgtk_mixal.c :
 * Implementation of the functions declared in mixgtk_mixal.h
 * ------------------------------------------------------------------
 *  Last change: Time-stamp: "2001-04-29 22:30:43 jao"
 * ------------------------------------------------------------------
 * Copyright (C) 2001 Free Software Foundation, Inc.
 *  
 * 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.
 *  
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *  
 */


#include <stdlib.h>
#include <string.h>

#include "mixgtk_widgets.h"
#include "mixgtk_config.h"
#include "mixgtk_mixal.h"


static mix_vm_t *vm_;
static GtkCList *clist_;
static gulong lineno_;
static GdkColor colors_[3][2];
static GdkColormap *colormap_ = NULL;
static const char* default_colors_[3][2] = {
  {"red", "black"},
  {"lightgrey", "black"},
  {"white", "black"}
};

static const char* keys_[3][2] = {
  {"MIXAL.bp.color.bg", "MIXAL.bp.color.fg"},
  {"MIXAL.lc.color.bg", "MIXAL.lc.color.fg"},
  {"MIXAL.pl.color.bg", "MIXAL.pl.color.fg"}
};

static const GdkColor *
parse_color_ (const gchar *str)
{
  static GdkColor color;
  if (sscanf (str, "%hd%hd%hd", &(color.red), &(color.green), &(color.blue)) < 3)
    {
      g_warning ("Wrong color spec: %s\n", str);
      return NULL;
    }
  
  return &color;
}

static const gchar *
color_to_string_ (const GdkColor *color)
{
  enum {LEN = 100};
  static gchar buffer[LEN];
  g_snprintf (buffer, LEN, "%hd %hd %hd", color->red, color->green, color->blue);
  return buffer;
}

static gboolean
init_color_ (GdkColor *c, const gchar *name)
{
  return (gdk_color_parse (name, c) &&
	  gdk_colormap_alloc_color (colormap_, c, FALSE, TRUE));
}

/* initialise the mixal widgets */
gboolean
mixgtk_mixal_init (mix_vm_t *vm)
{
  int i,j;
  
  g_return_val_if_fail (vm != NULL, FALSE);
  vm_ = vm;
  clist_ = GTK_CLIST (mixgtk_widget_factory_get (MIXGTK_WIDGET_MIXAL));
  g_return_val_if_fail (clist_ != NULL, FALSE);

  /* allocate colors */
  colormap_ = gtk_widget_get_colormap (GTK_WIDGET (clist_));
  for (i = 0; i < 3; ++i)
    for (j = 0; j < 2; ++j)
      {
	const gchar *ccol = mixgtk_config_get (keys_[i][j]);
	const GdkColor *color = NULL;
	if (ccol && (color = parse_color_ (ccol)))
	  {
	    mixgtk_mixal_set_color (i, j, color);
	  }
	else
	  {
	    g_return_val_if_fail (init_color_
				  (&colors_[i][j], default_colors_[i][j]),
				  FALSE);
	  }
      }
  return TRUE;
}

/* set the plain, location pointer and break colors */
void
mixgtk_mixal_set_color (mixal_line_t line, mixal_line_zone_t zone,
			const GdkColor *color)
{
  g_return_if_fail (color != NULL);
  g_return_if_fail (line <= MIXAL_LINE_PLAIN);
  g_return_if_fail (zone <= MIXAL_LINE_FG);
  colors_[line][zone].red = color->red;
  colors_[line][zone].green = color->green;
  colors_[line][zone].blue = color->blue;
  gdk_colormap_alloc_color (colormap_, &colors_[line][zone], FALSE, TRUE);
  mixgtk_mixal_update_bp_all ();
  mixgtk_config_update (keys_[line][zone],
			color_to_string_(&colors_[line][zone]));
}

const GdkColor *
mixgtk_mixal_get_color (mixal_line_t line, mixal_line_zone_t zone)
{
  g_return_val_if_fail (line <= MIXAL_LINE_PLAIN, FALSE);
  g_return_val_if_fail (zone <= MIXAL_LINE_FG, FALSE);
  return &colors_[line][zone];
}

/* load the corresponding mixal file */
void
mixgtk_mixal_load_file (void)
{
  enum {ADDR_SIZE = 20, CONT_SIZE = 200};
  static gchar ADDR[ADDR_SIZE], CONT[CONT_SIZE];
  static gchar *TEXT[] = {ADDR, CONT};
  static gchar *NULL_TEXT[] = {NULL, NULL};
  
  const mix_src_file_t *file;
  
  g_assert (vm_);
  g_assert (clist_);
  
  gtk_clist_clear (clist_);
  
  file = mix_vm_get_src_file (vm_);
  if (file != NULL)
    {
      gint k;
      mix_address_t addr;

      lineno_ = mix_src_file_get_line_no (file);

      gtk_clist_freeze (clist_);
      for (k = 0; k < lineno_; ++k)
	{
	  const gchar *line = mix_src_file_get_line (file, k + 1);
	  snprintf (CONT, CONT_SIZE, "%03d:     %s", k + 1, line);
	  addr = mix_vm_get_lineno_address (vm_, k + 1);
	  if (addr != MIX_VM_CELL_NO)
	    {
	      sprintf (ADDR, "%04d:   ", mix_short_magnitude (addr));
	      mix_word_print_to_buffer (mix_vm_get_addr_contents (vm_, addr),
					ADDR + strlen (ADDR));
	    }
	  else
	    ADDR[0] = '\0';
	  gtk_clist_append (clist_, TEXT);
	  gtk_clist_set_row_data (clist_, k, GINT_TO_POINTER
				  (mix_short_magnitude (addr)));
	}
      gtk_clist_append (clist_, NULL_TEXT);
      gtk_clist_set_row_data (clist_, k, GINT_TO_POINTER (MIX_VM_CELL_NO));
      gtk_clist_unselect_row (clist_, 0, 0);
      gtk_clist_thaw (clist_);
    }
  else
    lineno_ = 0;
}


/* update the widgets */
static void
reset_bg_ (gint row)
{
  gint addr = GPOINTER_TO_INT (gtk_clist_get_row_data (clist_, row));
  gboolean isset = mix_vm_has_breakpoint_at_address (vm_, addr);
  gtk_clist_set_background (clist_, row,
			    isset ? &colors_[MIXAL_LINE_BREAK][MIXAL_LINE_BG]:
			    &colors_[MIXAL_LINE_PLAIN][MIXAL_LINE_BG]);
  gtk_clist_set_foreground (clist_, row,
			    isset ? &colors_[MIXAL_LINE_BREAK][MIXAL_LINE_FG]:
			    &colors_[MIXAL_LINE_PLAIN][MIXAL_LINE_FG]);
}
  
static void
select_row_ (gint row)
{
  static gint last = -1;
  
  gtk_clist_set_background (clist_, row, &colors_[MIXAL_LINE_LOC][MIXAL_LINE_BG]);
  gtk_clist_set_foreground (clist_, row, &colors_[MIXAL_LINE_LOC][MIXAL_LINE_FG]);
  if (gtk_clist_row_is_visible (clist_, row) != GTK_VISIBILITY_FULL)
    gtk_clist_moveto (clist_, row, 0, 0.25, 0);
  if (last != -1 && last != row) reset_bg_ (last);
  last = row;
}

void
mixgtk_mixal_update (void)
{
  gint addr = 0;
  gint k = 0;
  
  g_assert (vm_);
  g_assert (clist_);
  
  addr = mix_short_magnitude (mix_vm_get_prog_count (vm_));
  k = gtk_clist_find_row_from_data (clist_, GINT_TO_POINTER (addr));
  select_row_ (k);
}

/* breakpoints */
void
mixgtk_mixal_update_bp_at_address (guint addr)
{
  gint k;

  g_assert (vm_);
  g_assert (clist_);

  k = gtk_clist_find_row_from_data (clist_, GINT_TO_POINTER (addr));
  reset_bg_ (k);
}

void
mixgtk_mixal_update_bp_at_line (guint line)
{
  if ( line < 1 ) return;
  
  while (line < lineno_)
    {
      gint addr = GPOINTER_TO_INT (gtk_clist_get_row_data (clist_, line - 1));
      if (addr != MIX_VM_CELL_NO) break;
      ++line;
    }
  reset_bg_ (line - 1);
}

void
mixgtk_mixal_update_bp_all ()
{
  gint k, addr;
  for (k = 0; k < lineno_; ++k) reset_bg_ (k);
  addr = mix_vm_get_prog_count (vm_);
  k = gtk_clist_find_row_from_data (clist_, GINT_TO_POINTER (addr));
  select_row_ (k);
}

/* callbacks */
void
on_mixal_select_row (GtkWidget *w, gint row, gint col, GdkEventButton *e,
		     gpointer data)
{
  gboolean isset;
  gint addr, pc;
  
  gtk_clist_unselect_row (clist_, row, col);
  addr = GPOINTER_TO_INT (gtk_clist_get_row_data (clist_, row));
  pc = mix_vm_get_prog_count (vm_);
  if (addr < MIX_VM_CELL_NO)
    {
      isset = mix_vm_has_breakpoint_at_address (vm_, addr);
      if (isset)
	mix_vm_clear_breakpoint_address (vm_, addr);
      else
	mix_vm_set_breakpoint_address (vm_, addr);
      reset_bg_ (row);
    }
}