File: drgeo_adaptDialog.cc

package info (click to toggle)
drgeo 1.1.0-1.2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 8,340 kB
  • ctags: 2,481
  • sloc: cpp: 22,807; sh: 11,294; lisp: 1,285; makefile: 378; ansic: 343
file content (449 lines) | stat: -rw-r--r-- 13,392 bytes parent folder | download | duplicates (5)
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*
 *  Dr Genius interactive geometry software
 * (C) Copyright Hilaire Fernandes  2002
 * 
 * 
 * 
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public Licences as by published
 * by the Free Software Foundation; either version 2; or (at your option)
 * any later version
 *
 * This program is distributed in the hope that it will entertaining,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILTY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Publis 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.
 * 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdio.h>
#include <time.h>
#include <unistd.h>

#include <string.h>

#include <glade/glade.h>
#include "drgeo_adaptDialog.h"
#include "drgeo_figure.h"
#include "drgeo_menu.h"
#include "drgenius_mdi.h"

#ifndef G_PLATFORM_WIN32
   #include <crypt.h>
#endif 

extern drgeniusMDI *mdi;

static GladeXML *xml = NULL;
static drgeoFigure *myFigure;
static geoView *myChild;
static GtkWindow *dialog;
gchar *toolName[] =
  { "pointMenu", "curveMenu", "transformationMenu", "numericMenu",
  "macroMenu", "otherMenu", "moveItem", "freePoint", "middle",
  "intersection", "coordinatesPoint", "line", "halfLine", "segment",
  "vector", "circle", "arcCircle", "locus", "polygon",
  "parallel", "perpendicular", "reflexion", "symmetry", "translation",
  "rotation", "scale", "distance", "angle", "equation", "script",
  "buildMacro", "runMacro", "deleteItem", "styleItem", "propertyItem"
};


static void
updateDrgeoDialog ()
{
  /* update the dialog according to the unselected tools in this
   * figure */
  gint i;

  for (i = 0; i < DRGEO_TOOLS_NUMBER; i++)
    setSensitiveState (toolName[i], myFigure->getToolState ((drgeoToolId) i));
}

void
adaptDrgeoDialog (geoView * child)
/* Adapt the Drgeo dialog */
{
  GtkWidget *w;

  if (xml != NULL)
    /* only one adaptDrgeoDialog at a time */
    return;
  myFigure = child->figure ();
  myChild = child;

  xml = glade_xml_new (DRGEO_GLADEDIR "/drgeo2.glade", "uiAdaptDialog", NULL);
  dialog = GTK_WINDOW (glade_xml_get_widget (xml, "uiAdaptDialog"));
  glade_xml_signal_autoconnect (xml);

  mdi->setTransientDialog (GTK_WINDOW (dialog));

  /* Force the toolbar style, we do not want the default one from the Gnome Desktop */
  w = glade_xml_get_widget (xml, "tb0");
  gtk_toolbar_set_style (GTK_TOOLBAR (w), GTK_TOOLBAR_ICONS);
  // FIXME gtk_toolbar_set_button_relief (GTK_TOOLBAR (w), GTK_RELIEF_HALF);
  w = glade_xml_get_widget (xml, "tb1");
  gtk_toolbar_set_style (GTK_TOOLBAR (w), GTK_TOOLBAR_ICONS);
  // FIXME gtk_toolbar_set_button_relief (GTK_TOOLBAR (w), GTK_RELIEF_NONE);
  w = glade_xml_get_widget (xml, "tb2");
  gtk_toolbar_set_style (GTK_TOOLBAR (w), GTK_TOOLBAR_ICONS);
  // FIXME gtk_toolbar_set_button_relief (GTK_TOOLBAR (w), GTK_RELIEF_NONE);
  w = glade_xml_get_widget (xml, "tb2");
  gtk_toolbar_set_style (GTK_TOOLBAR (w), GTK_TOOLBAR_ICONS);
  // FIXME gtk_toolbar_set_button_relief (GTK_TOOLBAR (w), GTK_RELIEF_NONE);
  w = glade_xml_get_widget (xml, "tb3");
  gtk_toolbar_set_style (GTK_TOOLBAR (w), GTK_TOOLBAR_ICONS);
  // FIXME gtk_toolbar_set_button_relief (GTK_TOOLBAR (w), GTK_RELIEF_NONE);
  w = glade_xml_get_widget (xml, "tb4");
  gtk_toolbar_set_style (GTK_TOOLBAR (w), GTK_TOOLBAR_ICONS);
  // FIXME gtk_toolbar_set_button_relief (GTK_TOOLBAR (w), GTK_RELIEF_NONE);
  w = glade_xml_get_widget (xml, "tb5");
  gtk_toolbar_set_style (GTK_TOOLBAR (w), GTK_TOOLBAR_ICONS);
  // FIXME gtk_toolbar_set_button_relief (GTK_TOOLBAR (w), GTK_RELIEF_NONE);
  w = glade_xml_get_widget (xml, "tb6");
  gtk_toolbar_set_style (GTK_TOOLBAR (w), GTK_TOOLBAR_ICONS);
  // FIXME gtk_toolbar_set_button_relief (GTK_TOOLBAR (w), GTK_RELIEF_NONE);

  updateDrgeoDialog ();
}

void
on_pointMenuHide_clicked (GtkWidget * w, gpointer data)
{
  gboolean activated;

  activated = !getSensitiveState (GTK_BIN(w)->child);
  gtk_widget_set_sensitive (GTK_BIN (w)->child, activated);
  /* set the state of the tools packed in this barre */
  setSensitiveState ("freePoint", activated);
  setSensitiveState ("middle", activated);
  setSensitiveState ("intersection", activated);
  setSensitiveState ("coordinatesPoint", activated);

}

void
on_curveMenuHide_clicked (GtkWidget * w, gpointer data)
{
  gboolean activated;

  activated = !getSensitiveState (GTK_BIN(w)->child);
  gtk_widget_set_sensitive (GTK_BIN (w)->child, activated);
  /* set the state of the tools packed in this barre */
  setSensitiveState ("line", activated);
  setSensitiveState ("halfLine", activated);
  setSensitiveState ("segment", activated);
  setSensitiveState ("vector", activated);
  setSensitiveState ("circle", activated);
  setSensitiveState ("arcCircle", activated);
  setSensitiveState ("locus", activated);
  setSensitiveState ("polygon", activated);
}

void
on_transformationMenuHide_clicked (GtkWidget * w, gpointer data)
{
  gboolean activated;

  activated = !getSensitiveState (GTK_BIN(w)->child);
  gtk_widget_set_sensitive (GTK_BIN (w)->child, activated);
  /* set the state of the tools packed in this barre */
  setSensitiveState ("parallel", activated);
  setSensitiveState ("perpendicular", activated);
  setSensitiveState ("reflexion", activated);
  setSensitiveState ("symmetry", activated);
  setSensitiveState ("translation", activated);
  setSensitiveState ("rotation", activated);
  setSensitiveState ("scale", activated);
}

void
on_numericMenuHide_clicked (GtkWidget * w, gpointer data)
{
  gboolean activated;

  activated = !getSensitiveState (GTK_BIN(w)->child);
  gtk_widget_set_sensitive (GTK_BIN (w)->child, activated);
  /* set the state of the tools packed in this barre */
  setSensitiveState ("distance", activated);
  setSensitiveState ("angle", activated);
  setSensitiveState ("equation", activated);
  setSensitiveState ("script", activated);
}

void
on_macroMenuHide_clicked (GtkWidget * w, gpointer data)
{
  gboolean activated;

  activated = !getSensitiveState (GTK_BIN(w)->child);
  gtk_widget_set_sensitive (GTK_BIN (w)->child, activated);
  /* set the state of the tools packed in this barre */
  setSensitiveState ("buildMacro", activated);
  setSensitiveState ("runMacro", activated);
}

void
on_otherMenuHide_clicked (GtkWidget * w, gpointer data)
{
  gboolean activated;

  activated = !getSensitiveState (GTK_BIN(w)->child);
  gtk_widget_set_sensitive (GTK_BIN (w)->child, activated);
  /* set the state of the tools packed in this barre */
  setSensitiveState ("moveItem", activated);
  setSensitiveState ("deleteItem", activated);
  setSensitiveState ("styleItem", activated);
  setSensitiveState ("propertyItem", activated);
}

void
on_toolHide_clicked (GtkWidget * w, gpointer data)
{
  if (getSensitiveState (GTK_BIN(w)->child))
    gtk_widget_set_sensitive (GTK_BIN (w)->child, FALSE);
  else
    gtk_widget_set_sensitive (GTK_BIN (w)->child, TRUE);
}

void 
on_uiAdaptDialog_response (GtkWidget * widget, gint b, gpointer data)
{
  GtkWidget *p_dialog, *p_entry;
  GladeXML *p_tree;

  switch (b)
    {
    case GTK_RESPONSE_CLOSE:
      gtk_widget_destroy (GTK_WIDGET (dialog));
      g_object_unref (G_OBJECT (xml));
      xml = NULL;
      /* Eventually needed */
      // FIXME : do we need myChild->reconcile_popup_menu ();      
      break;
    case 1:
      /* Lock */
      /* Ask for a password if none was specified before */
      if (myFigure->getPassword ())
	{
	  /* Ask the previous password when it is not null */
	  p_tree = glade_xml_new (DRGEO_GLADEDIR "/drgeo2.glade",
				"relockDialog", NULL);
	  p_dialog = glade_xml_get_widget (p_tree, "relockDialog");
	  p_entry = glade_xml_get_widget (p_tree, "relockEntry");
	  mdi->setTransientDialog (GTK_WINDOW (p_dialog));
	  
	  g_signal_connect (G_OBJECT (p_dialog), "response",
			    G_CALLBACK (on_relockDialog_response),
			    (gpointer) p_entry);
	  g_object_unref (G_OBJECT (p_tree));
	}
      else
	{
	  p_tree = glade_xml_new (DRGEO_GLADEDIR "/drgeo2.glade",
				"lockDialog", NULL);
	  p_dialog = glade_xml_get_widget (p_tree, "lockDialog");
	  p_entry = glade_xml_get_widget (p_tree, "lockEntry");
	  mdi->setTransientDialog (GTK_WINDOW (p_dialog));
	  
	  g_signal_connect (G_OBJECT (p_dialog), "response",
			    G_CALLBACK (on_lockDialog_response),
			    (gpointer) p_entry);
	  g_object_unref (G_OBJECT (p_tree));	  
	}
      break;
    case 2:
      /* Unock */
      if (myFigure->getPassword ())
	{
	  p_tree = glade_xml_new (DRGEO_GLADEDIR "/drgeo2.glade",
				"unlockDialog", NULL);
	  p_dialog = glade_xml_get_widget (p_tree, "unlockDialog");
	  p_entry = glade_xml_get_widget (p_tree, "unlockEntry");
	  mdi->setTransientDialog (GTK_WINDOW (p_dialog));
	  
	  g_signal_connect (G_OBJECT (p_dialog), "response",
			    G_CALLBACK (on_unlockDialog_response),
			    (gpointer) p_entry);
	  g_object_unref (G_OBJECT (p_tree));	  
	}
      break;
    }
}


void 
on_relockDialog_response (GtkWidget *dialog, gint r, GtkWidget *entry)
{
  char *result, *password;
  gint i;
  GtkWidget *deniedDialog;

  switch (r)
    {
    case GTK_RESPONSE_OK:
      /* The interface is already locked, get the password to relock
	 it with the new customization */
      password = (gchar *) gtk_entry_get_text (GTK_ENTRY (entry));
      if (password == NULL)
	break;

#ifndef G_PLATFORM_WIN32
      result = crypt (password, myFigure->getPassword ());
#endif
      if (!strcmp (result, myFigure->getPassword ()))
	{
	  /* access granted */
	  /* Record and Update the changes to the UI */
	  for (i = 0; i < DRGEO_TOOLS_NUMBER; i++)
	    myFigure->setToolState ((drgeoToolId) i,
				    getSensitiveStateByName (toolName[i]));
	  myChild->updateUserInterface ();
	}
      else
	{
	  /* access denied */
	  deniedDialog = gtk_message_dialog_new
	    (mdi->mainWindow (),
	     GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR,
	     GTK_BUTTONS_OK,
	     "Password does not match\n Access denied!");
	  gtk_dialog_run (GTK_DIALOG (deniedDialog));
	  gtk_widget_destroy (deniedDialog);
	}
      break;
    case GTK_RESPONSE_CANCEL:
      break;
    }
  gtk_widget_destroy (dialog);
}

void 
on_lockDialog_response (GtkWidget *dialog, gint r, GtkWidget *entry)
{
  gint i;
  unsigned long seed[2];
  char salt[] = "$1$........";
  const char *const seedchars =
    "./0123456789ABCDEFGHIJKLMNOPQRST" "UVWXYZabcdefghijklmnopqrstuvwxyz";
  char *cryptPassword, *password;

  switch (r)
    {
    case GTK_RESPONSE_OK:
      password = (gchar *) gtk_entry_get_text (GTK_ENTRY (entry));
      if (password == NULL)
	break;

      seed[0] = time (NULL);
      seed[1] = getpid () ^ (seed[0] >> 14 & 0x30000);

      /* Turn it into printable characters from `seedchars'. */
      for (i = 0; i < 8; i++)
	salt[3 + i] = seedchars[(seed[i / 5] >> (i % 5) * 6) & 0x3f];
      
#ifndef G_PLATFORM_WIN32
      cryptPassword = crypt (password, salt);
#endif      
      /* Save the crypted password into the figure */
      myFigure->setPassword (cryptPassword);
      
      /* Record and Update the changes to the UI */
      for (i = 0; i < DRGEO_TOOLS_NUMBER; i++)
	myFigure->setToolState ((drgeoToolId) i,
				getSensitiveStateByName (toolName[i]));
      myChild->updateUserInterface ();
      break;
    case GTK_RESPONSE_CANCEL:
      break;
    }
  gtk_widget_destroy (dialog);
}

void 
on_unlockDialog_response (GtkWidget *dialog, gint r, GtkWidget *entry)
{
  GtkWidget *deniedDialog;
  char *result, *password;
  gint i;


  switch (r)
    {
    case GTK_RESPONSE_OK:
      password = (gchar *) gtk_entry_get_text (GTK_ENTRY (entry));
      if (password == NULL)
	break;

#ifndef G_PLATFORM_WIN32
      result = crypt (password, myFigure->getPassword ());
#endif
      if (!strcmp (result, myFigure->getPassword ()))
	{
	  /* access granted */
	  /* Set visible all the tool in the interface */
	  for (i = 0; i < DRGEO_TOOLS_NUMBER; i++)
	    myFigure->setToolState ((drgeoToolId) i, TRUE);
	  myChild->updateUserInterface ();
	  /* forget the password */
	  myFigure->setPassword (NULL);
	  updateDrgeoDialog ();
	}
      else
	{
	  /* access denied */
	  deniedDialog = gtk_message_dialog_new
	    (mdi->mainWindow (),
	     GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR,
	     GTK_BUTTONS_OK,
	     _("Password does not match\n Access denied!"));
	  gtk_dialog_run (GTK_DIALOG (deniedDialog));
	  gtk_widget_destroy (deniedDialog);
	}
      break;
    case GTK_RESPONSE_CANCEL:
      break;
    }
  gtk_widget_destroy (dialog);
}

gint
on_uiAdaptDialog_delete_event (GtkWidget * w, GdkEventAny * e, gpointer data)
{
  gtk_object_destroy (GTK_OBJECT (xml));
  gtk_widget_destroy (GTK_WIDGET (dialog));
  xml = NULL;
 /* Eventually needed */
  // FIXME: do we need it? reconcile_popup_menu (myChild);
  return FALSE;
}


gboolean
getSensitiveState (GtkWidget * button)
/* return the senstitive state of the button */
{
  return GTK_WIDGET_IS_SENSITIVE (button);
}

gboolean
getSensitiveStateByName (gchar * buttonName)
{
  // the sensitivity is on the image of the button
  return getSensitiveState 
    (GTK_BIN(glade_xml_get_widget (xml, buttonName))->child);
}

void
setSensitiveState (gchar * button, gboolean state)
{
  GtkWidget *w;

  w = glade_xml_get_widget (xml, button);
  gtk_widget_set_sensitive (GTK_BIN (w)->child, state);
}