~ubuntu-branches/ubuntu/utopic/gdis/utopic

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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
Copyright (C) 2003 by Sean David Fleming

sean@ivec.org

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.

The GNU GPL can also be found at http://www.gnu.org
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>
#include <math.h>

#include "gdis.h"
#include "coords.h"
#include "matrix.h"
#include "space.h"
#include "gui_shorts.h"
#include "interface.h"
#include "dialog.h"
#include "opengl.h"

/* main structure */
extern struct sysenv_pak sysenv;
extern struct elem_pak elements[];

/************************/
/* draw periodic images */
/************************/
void gtk_refresh_images(GtkWidget *w, gpointer dummy)
{
struct model_pak *model;

model = sysenv.active_model;
g_assert(model != NULL);

/* atom colour/display updates */
space_make_images(CREATE, model);
coords_init(CENT_COORDS, model);
redraw_canvas(SINGLE);
}

/****************************/
/* display property updates */
/****************************/
void cb_make_asymmetric(GtkWidget *w, gpointer dummy)
{
GSList *list;
struct core_pak *core;
struct shel_pak *shel;
struct model_pak *model;

model = sysenv.active_model;

g_assert(model != NULL);

if (model->periodic)
  {
  for (list=model->cores ; list ; list=g_slist_next(list))
    {
    core = list->data;
    if (core->primary)
      continue;

    if (model->asym_on)
      core->status |= HIDDEN;
    else
      core->status &= ~HIDDEN;
    }
  for (list=model->shels ; list ; list=g_slist_next(list))
    {
    shel = list->data;
    if (shel->primary)
      continue;
    if (model->asym_on)
      shel->status |= HIDDEN;
    else
      shel->status &= ~HIDDEN;
    }
  }

/* update */
coords_compute(model);
redraw_canvas(SINGLE);
}

/***************************/
/* perioidic image globals */
/***************************/
gboolean image_update = TRUE;
GtkWidget *image_spin[6];

/****************************/
/* periodic image callbacks */
/****************************/
void cb_space_image_spinner(GtkWidget *w, gpointer data)
{
gint i;
struct model_pak *model;

model = sysenv.active_model;
if (!model)
  return;

/* alter the spinner value */
i = GPOINTER_TO_INT(data);
g_assert(i >= 0);
g_assert(i < 6);
model->image_limit[i] = SPIN_FVAL(GTK_SPIN_BUTTON(w));

/* update only if this is a genuine call for perioidic image creation */
if (image_update)
  {
  space_make_images(CREATE, model);
  coords_init(CENT_COORDS, model);
  redraw_canvas(SINGLE);
  }
}

/********************************/
/* periodic image widget update */
/********************************/
void space_image_widget_redraw(void)
{
gint i, periodic;
struct model_pak *model;

/* setup */
model = sysenv.active_model;
if (!model)
  periodic = 0;
else
  periodic = model->periodic;

/* NB: don't allow updates as we're not making periodic images, */
/* we're setting the widget values to what they should be */
image_update = FALSE;
for (i=0 ; i<3 ; i++)
  {
  if (i < periodic)
    {
/* show and set available spinners */
    gtk_widget_set_sensitive(GTK_WIDGET(image_spin[2*i]), TRUE);
    gtk_widget_set_sensitive(GTK_WIDGET(image_spin[2*i+1]), TRUE);
    gtk_spin_button_set_value(GTK_SPIN_BUTTON(image_spin[2*i]), model->image_limit[2*i]);
    gtk_spin_button_set_value(GTK_SPIN_BUTTON(image_spin[2*i+1]), model->image_limit[2*i+1]);
    }
  else
    {
/* reset unavailable spinners to default values */
    gtk_spin_button_set_value(GTK_SPIN_BUTTON(image_spin[2*i]), 0.0);
    gtk_spin_button_set_value(GTK_SPIN_BUTTON(image_spin[2*i+1]), 1.0);
/* hide unavailable spinners */
    gtk_widget_set_sensitive(GTK_WIDGET(image_spin[2*i]), FALSE);
    gtk_widget_set_sensitive(GTK_WIDGET(image_spin[2*i+1]), FALSE);
    }
  }
image_update = TRUE;
}

/**********************************/
/* periodic image default setting */
/**********************************/
void space_image_widget_reset(void)
{
struct model_pak *model;

/* update model (if exists) */
model = sysenv.active_model;
if (model)
  {
  space_make_images(INITIAL, model);
  coords_init(CENT_COORDS, model);
  }

/* update widget and canvas */
gui_refresh(GUI_MODEL_PROPERTIES);
gui_refresh(GUI_CANVAS);
}

/*********************************************/
/* periodic image creation for the main pane */
/*********************************************/
void space_image_widget_setup(GtkWidget *box)
{
gint i;
GtkWidget *table, *label;

/* use table for nice spacing */
table = gtk_table_new(3, 4, FALSE);
gtk_box_pack_start(GTK_BOX(box), table, FALSE, FALSE, 0);

/* image directions */
label = gtk_label_new ("-");
gtk_table_attach_defaults(GTK_TABLE(table),label,1,2,0,1);
label = gtk_label_new ("+");
gtk_table_attach_defaults(GTK_TABLE(table),label,2,3,0,1);

/* labels and spinners */
for (i=0 ; i<3 ; i++)
  {
/* negative direction */
  image_spin[2*i] = new_spinner(NULL, 0, 10, 1, 
                                cb_space_image_spinner,
                                GINT_TO_POINTER(2*i),
                                NULL);
/* TODO - set these to 1, when fractional periodic images are working */
  gtk_spin_button_set_digits(GTK_SPIN_BUTTON(image_spin[2*i]), 0);
  gtk_table_attach_defaults(GTK_TABLE(table), image_spin[2*i], 1, 2, i+1, i+2);

/* positive direction */
  image_spin[2*i+1] = new_spinner(NULL, 1, 10, 1,
                                  cb_space_image_spinner,
                                  GINT_TO_POINTER(2*i+1),
                                  NULL);
/* TODO - set these to 1, when fractional periodic images are working */
  gtk_spin_button_set_digits(GTK_SPIN_BUTTON(image_spin[2*i+1]), 0);
  gtk_table_attach_defaults(GTK_TABLE(table),
                            image_spin[2*i+1], 2, 3, i+1, i+2);
  }

/* set the spinner values */
gui_refresh(GUI_MODEL_PROPERTIES);
}

/***************************/
/* Space Group info dialog */
/***************************/
void periodicity_page(GtkWidget *box)
{
GString *info, *item;
GtkWidget *frame, *vbox;
struct model_pak *model;

/* checks */
model = sysenv.active_model;
if (!model)
  return;
if (!model->periodic)
  return;

item = g_string_new(NULL);
info = g_string_new(NULL);

#if GENPOS
/* FRAME */
frame = gtk_frame_new ("General positions");
gtk_box_pack_start(GTK_BOX(main_box),frame,TRUE,TRUE,0);
gtk_container_set_border_width (GTK_CONTAINER(frame), PANEL_SPACING);
vbox = gtk_vbox_new (FALSE, 5);
gtk_container_set_border_width(GTK_CONTAINER(vbox), PANEL_SPACING);
gtk_container_add (GTK_CONTAINER(frame),vbox);
/* table for nice spacing */
/* more intelligent dimension choice? */
cols = 4;
rows = model->sginfo.order / cols;
table = gtk_table_new(cols, rows, FALSE);
gtk_container_add(GTK_CONTAINER(vbox), table);

/* order of the group (ie mth matrix) */
m=0;
/* loop over rows */
while (m<model->sginfo.order)
  {
/* get row(i) and column(j) */
  i = m / cols;
  j = m % cols;
/* construct the three generator elements */
  mat = *(model->sginfo.matrix+m);
  g_string_sprintf(info," ");
  for (k=0 ; k<3 ; k++)
    {
/* offset elements */
    value = *(*(model->sginfo.offset+m)+k);
    need_sign = 0;
/* separate 2nd and 3rd elements with a comma */
    if (k)
      g_string_append(info, ", ");
/* clear the string for the new element to append */
    g_string_assign(item, "");
/* append the offset */
    if (fabs(2.0*value-1.0) < FRACTION_TOLERANCE)
      {
      g_string_sprintfa(item, "1/2");
      need_sign++;
      }
    if (fabs(2.0*value+1.0) < FRACTION_TOLERANCE)
      {
      g_string_sprintfa(item, "-1/2");
      need_sign++;
      }
    if (fabs(3.0*value-1.0) < FRACTION_TOLERANCE)
      {
      g_string_sprintfa(item, "1/3");
      need_sign++;
      }
    if (fabs(3.0*value+1.0) < FRACTION_TOLERANCE)
      {
      g_string_sprintfa(item, "-1/3");
      need_sign++;
      }
    if (fabs(4.0*value-1.0) < FRACTION_TOLERANCE)
      {
      g_string_sprintfa(item, "1/4");
      need_sign++;
      }
    if (fabs(4.0*value+1.0) < FRACTION_TOLERANCE)
      {
      g_string_sprintfa(item, "-1/4");
      need_sign++;
      }
/* append the appropriate matrix element(s) */
    for (l='x' ; l<='z' ; l++)
      {
      if (*mat > FRACTION_TOLERANCE)
        {
        if (need_sign)
          g_string_sprintfa(item, "+%c", l);
        else
          {
          g_string_sprintfa(item, "%c", l);
          need_sign++;
          }
        }
      if (*mat < -FRACTION_TOLERANCE)
        {
        g_string_sprintfa(item, "-%c",l);
        need_sign++;
        }
      mat++;
      }
    g_string_append(info, item->str);
    }
/* write the generator to the table */
  label = gtk_label_new(info->str);
  gtk_table_attach_defaults(GTK_TABLE(table),label,j,j+1,i,i+1);
  m++;
  }
#endif

/* FRAME */
frame = gtk_frame_new(NULL);
gtk_box_pack_start(GTK_BOX (box), frame, FALSE, FALSE, 0);
gtk_container_set_border_width(GTK_CONTAINER(frame), PANEL_SPACING);
vbox = gtk_vbox_new(TRUE, PANEL_SPACING);
gtk_container_add(GTK_CONTAINER(frame), vbox);
gtk_container_set_border_width(GTK_CONTAINER(GTK_BOX(vbox)), PANEL_SPACING);

gui_auto_check("Asymmetric cell", cb_make_asymmetric, NULL, &model->asym_on, vbox);

/* done, draw, exit */
gtk_widget_show_all(box);

g_string_free(info, TRUE);
g_string_free(item, TRUE);
}