1 // GTK_SIMPLE_DIALOGS.CPP
2
3 // Copyright (C) 1999 Tommi Hassinen.
4
5 // This package is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9
10 // This package is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14
15 // You should have received a copy of the GNU General Public License
16 // along with this package; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19 /*################################################################################################*/
20
21 #include "gtk_simple_dialogs.h"
22
23 #include <ghemical/atom.h>
24 #include <ghemical/bond.h>
25
26 #include "gtk_project.h"
27
28 #include "gtk_trajview_dialog.h"
29
30 #include "local_i18n.h"
31
32 #include <cstring>
33 using namespace std;
34
35 /*################################################################################################*/
36
37 gtk_element_dialog * gtk_element_dialog::current_object = NULL;
38
39 GtkWidget * gtk_element_dialog::dialog = NULL;
40 GtkWidget * gtk_element_dialog::current_element_widget = NULL;
41
gtk_element_dialog(void)42 gtk_element_dialog::gtk_element_dialog(void)
43 {
44 current_object = this;
45 dialog = gtk_dialog_new();
46
47 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE); // request a modal window.
48
49 gtk_window_set_title(GTK_WINDOW(dialog), _("Set Current Element"));
50 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
51
52 GtkWidget * table = gtk_table_new(21, 38, TRUE);
53 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), table);
54
55 int attachdata[ELEMENT_SYMBOLS][2] =
56 {
57 { 1, 1 }, { 35, 1 }, { 1, 3 }, { 3, 3 }, { 25, 3 }, // B
58 { 27, 3 }, { 29, 3 }, { 31, 3 }, { 33, 3 }, { 35, 3 }, // Ne
59 { 1, 5 }, { 3, 5 }, { 25, 5 }, { 27, 5 }, { 29, 5 }, // P
60 { 31, 5 }, { 33, 5 }, { 35, 5 }, { 1, 7 }, { 3, 7 }, // Ca
61 { 5, 7 }, { 7, 7 }, { 9, 7 }, { 11, 7 }, { 13, 7 }, // Mn
62 { 15, 7 }, { 17, 7 }, { 19, 7 }, { 21, 7 }, { 23, 7 }, // Zn
63 { 25, 7 }, { 27, 7 }, { 29, 7 }, { 31, 7 }, { 33, 7 }, // Br
64 { 35, 7 }, { 1, 9 }, { 3, 9 }, { 5, 9 }, { 7, 9 }, // Zr
65 { 9, 9 }, { 11, 9 }, { 13, 9 }, { 15, 9 }, { 17, 9 }, // Rh
66 { 19, 9 }, { 21, 9 }, { 23, 9 }, { 25, 9 }, { 27, 9 }, // Sn
67 { 29, 9 }, { 31, 9 }, { 33, 9 }, { 35, 9 }, { 1, 11 }, // Cs
68 { 3, 11 }, { 5, 11 }, { 9, 16 }, { 11, 16 }, { 13, 16 }, // Nd
69 { 15, 16 }, { 17, 16 }, { 19, 16 }, { 21, 16 }, { 23, 16 }, // Tb
70 { 25, 16 }, { 27, 16 }, { 29, 16 }, { 31, 16 }, { 33, 16 }, // Yb
71 { 35, 16 }, { 7, 11 }, { 9, 11 }, { 11, 11 }, { 13, 11 }, // Re
72 { 15, 11 }, { 17, 11 }, { 19, 11 }, { 21, 11 }, { 23, 11 }, // Hg
73 { 25, 11 }, { 27, 11 }, { 29, 11 }, { 31, 11 }, { 33, 11 }, // At
74 { 35, 11 }, { 1, 13 }, { 3, 13 }, { 5, 13 }, { 9, 18 }, // Th
75 { 11, 18 }, { 13, 18 }, { 15, 18 }, { 17, 18 }, { 19, 18 }, // Am
76 { 21, 18 }, { 23, 18 }, { 25, 18 }, { 27, 18 }, { 29, 18 }, // Fm
77 { 31, 18 }, { 33, 18 }, { 35, 18 }, { 7, 13 }, { 9, 13 }, // Db
78 { 11, 13 }, { 13, 13 }, { 15, 13 }, { 17, 13 }, { 19, 13 }
79 };
80
81 GtkSignalFunc sf = (GtkSignalFunc) gtk_element_dialog::SignalHandler;
82 GtkWidget * button;
83
84 for (int n1 = 0; n1 < ELEMENT_SYMBOLS; n1++)
85 {
86 element tmp_element(n1 + 1);
87 button = gtk_toggle_button_new_with_label(tmp_element.GetSymbol());
88
89 if (n1 + 1 == element::current_element.GetAtomicNumber())
90 {
91 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), true);
92 current_element_widget = button; // do we need this???
93 }
94
95 gtk_signal_connect(GTK_OBJECT(button), "clicked", sf, (void *) (n1 + 1));
96 gtk_widget_show(button);
97
98 int x = attachdata[n1][0]; int y = attachdata[n1][1];
99 gtk_table_attach_defaults(GTK_TABLE(table), button, x, x + 2, y, y + 2);
100 }
101
102 gtk_widget_show(table);
103 gtk_widget_show(dialog);
104 }
105
~gtk_element_dialog(void)106 gtk_element_dialog::~gtk_element_dialog(void)
107 {
108 }
109
SignalHandler(GtkWidget *,int signal)110 void gtk_element_dialog::SignalHandler(GtkWidget *, int signal)
111 {
112 element::current_element = element(signal);
113 cout << _("Current element is now ") << element::current_element.GetSymbol() << "." << endl; // which prj???
114
115 gtk_widget_destroy(dialog);
116 delete current_object; // the object was created with new!!!
117 }
118
119 /*################################################################################################*/
120
121 gtk_bondtype_dialog * gtk_bondtype_dialog::current_object = NULL;
122
123 GtkWidget * gtk_bondtype_dialog::dialog = NULL;
124 GtkWidget * gtk_bondtype_dialog::current_bond_widget = NULL;
125
gtk_bondtype_dialog(void)126 gtk_bondtype_dialog::gtk_bondtype_dialog(void)
127 {
128 current_object = this;
129 dialog = gtk_dialog_new();
130
131 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE); // request a modal window.
132
133 gtk_window_set_title(GTK_WINDOW(dialog), _("Set Current Bondtype"));
134 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
135
136 GtkWidget * table = gtk_table_new((2 + 4 + 1), 6, TRUE);
137 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), table);
138
139 char bt_symbols[4] = { 'S', 'D', 'T', 'C' };
140
141 GtkSignalFunc sf = (GtkSignalFunc) gtk_bondtype_dialog::SignalHandler;
142 GtkWidget * button;
143
144 int x = 1; int y = 0;
145 for (int n1 = 0;n1 < 4;n1++)
146 {
147 bondtype tmp_bondtype(bt_symbols[n1]);
148 button = gtk_toggle_button_new_with_label(tmp_bondtype.GetString());
149
150 if (bt_symbols[n1] == bondtype::current_bondtype.GetSymbol1())
151 {
152 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), true);
153 current_bond_widget = button; // do we need this??
154 }
155
156 i32s long_bt_symbol = bt_symbols[n1]; // convert 8bit -> 32bit
157 gtk_signal_connect(GTK_OBJECT(button), "clicked", sf, (void *) long_bt_symbol);
158
159 y += 1;
160 gtk_table_attach_defaults(GTK_TABLE(table), button, x, x + 4, y, y + 1);
161 gtk_widget_show(button);
162
163 if (n1 == 2) // add a separator ; 20050415
164 {
165 button = gtk_hseparator_new();
166
167 y += 1;
168 gtk_table_attach_defaults(GTK_TABLE(table), button, x, x + 4, y, y + 1);
169 gtk_widget_show(button);
170 }
171 }
172
173 gtk_widget_show(table);
174 gtk_widget_show(dialog);
175 }
176
~gtk_bondtype_dialog(void)177 gtk_bondtype_dialog::~gtk_bondtype_dialog(void)
178 {
179 }
180
SignalHandler(GtkWidget *,int signal)181 void gtk_bondtype_dialog::SignalHandler(GtkWidget *, int signal)
182 {
183 bondtype::current_bondtype = bondtype((char) signal);
184 cout << _("Current bondtype is now ") << bondtype::current_bondtype.GetString() << "." << endl; // which prj???
185
186 gtk_widget_destroy(dialog);
187 delete current_object; // the object was created with new!!!
188 }
189
190 /*################################################################################################*/
191
make_tmp_filename(gtk_project * prj)192 const char * make_tmp_filename(gtk_project * prj)
193 {
194 static char tmp_filename[256];
195 prj->GetFullProjectFileName(tmp_filename, 256);
196 return tmp_filename;
197 }
198
199 GtkWidget * gtk_file_dialog::dialog;
200
gtk_file_dialog(const char * title,const char * filter,const char * def_fn)201 gtk_file_dialog::gtk_file_dialog(const char * title, const char * filter, const char * def_fn)
202 {
203 dialog = gtk_file_selection_new(title);
204 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE); // request a modal window.
205
206 if (filter != NULL) gtk_file_selection_complete(GTK_FILE_SELECTION(dialog), filter);
207 if (def_fn != NULL) gtk_file_selection_set_filename(GTK_FILE_SELECTION(dialog), def_fn);
208
209 gtk_signal_connect(GTK_OBJECT(dialog),
210 "destroy", GTK_SIGNAL_FUNC(DestroyHandler), this);
211
212 gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(dialog)->ok_button),
213 "clicked", GTK_SIGNAL_FUNC(OkButtonHandler), this);
214
215 gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(dialog)->cancel_button),
216 "clicked", GTK_SIGNAL_FUNC(CancelButtonHandler), this);
217
218 gtk_widget_show(dialog);
219 }
220
~gtk_file_dialog(void)221 gtk_file_dialog::~gtk_file_dialog(void)
222 {
223 }
224
DestroyHandler(GtkWidget *,gpointer data)225 void gtk_file_dialog::DestroyHandler(GtkWidget *, gpointer data)
226 {
227 gtk_file_dialog * ref = (gtk_file_dialog *) data;
228
229 delete ref; // the object was created using new!!!
230 }
231
OkButtonHandler(GtkWidget *,gpointer data)232 void gtk_file_dialog::OkButtonHandler(GtkWidget *, gpointer data)
233 {
234 gtk_file_dialog * ref = (gtk_file_dialog *) data;
235
236 // do not close the dialog if we had for example wrong file type...
237
238 if (ref->OkEvent(gtk_file_selection_get_filename(GTK_FILE_SELECTION(dialog))))
239 {
240 gtk_widget_destroy(dialog);
241 }
242 }
243
CancelButtonHandler(GtkWidget *,gpointer data)244 void gtk_file_dialog::CancelButtonHandler(GtkWidget *, gpointer data)
245 {
246 gtk_file_dialog * ref = (gtk_file_dialog *) data;
247 ref->CancelEvent();
248
249 gtk_widget_destroy(dialog);
250 }
251
252 /*################################################################################################*/
253
254 const char gtk_file_open_dialog::title[] = "Open Project File";
255 gtk_project * gtk_file_open_dialog::prj = NULL;
256
gtk_file_open_dialog(gtk_project * p1)257 gtk_file_open_dialog::gtk_file_open_dialog(gtk_project * p1) :
258 gtk_file_dialog(title, NULL, NULL)
259 {
260 prj = p1;
261 }
262
~gtk_file_open_dialog(void)263 gtk_file_open_dialog::~gtk_file_open_dialog(void)
264 {
265 }
266
OkEvent(const char * filename)267 bool gtk_file_open_dialog::OkEvent(const char * filename)
268 {
269 if (prj == NULL)
270 {
271 gtk_app::GetAppX()->SetNewProject();
272 }
273
274 bool open = prj->IsEmpty(); // open, not insert
275
276 ifstream ifile;
277 ifile.open(filename, ios::in);
278 ReadGPR(* prj, ifile, !open);
279 ifile.close();
280
281 if (open) // we're "switching" to this file
282 {
283 prj->ParseProjectFileNameAndPath(filename);
284
285 char buffer[256];
286 prj->GetProjectFileName(buffer, 256, true);
287
288 // the file name change here is not yet properly working.
289 // the project notebook widget should be stored (if not already) in gtk_project and
290 // a call of UpdateAllWindowTitles() should change the labels.
291
292 custom_app::GetAppC()->UpdateAllWindowTitles();
293 }
294 else // we append this file to the current project
295 {
296 if (prj->selected_object != NULL) // if an object was selected, remove
297 { // that selection and leave the newly added
298 prj->selected_object = NULL; // atoms as selection; now the mouse tools
299 } // also affect the newly added atoms!
300 }
301
302 prj->UpdateAllGraphicsViews();
303 return true;
304 }
305
CancelEvent(void)306 void gtk_file_open_dialog::CancelEvent(void)
307 {
308 }
309
310 /*################################################################################################*/
311
312 const char gtk_file_save_dialog::title[] = "Save Project File";
313 gtk_project * gtk_file_save_dialog::prj = NULL;
314
gtk_file_save_dialog(gtk_project * p1)315 gtk_file_save_dialog::gtk_file_save_dialog(gtk_project * p1) :
316 gtk_file_dialog(title, FILENAME_FILTER, make_tmp_filename(p1))
317 {
318 prj = p1;
319 }
320
~gtk_file_save_dialog(void)321 gtk_file_save_dialog::~gtk_file_save_dialog(void)
322 {
323 }
324
OkEvent(const char * filename)325 bool gtk_file_save_dialog::OkEvent(const char * filename)
326 {
327 if (prj == NULL)
328 {
329 assertion_failed(__FILE__, __LINE__, "prj == NULL");
330 }
331
332 // should check to see if file already exists...
333
334 prj->ParseProjectFileNameAndPath(filename);
335
336 char buffer[256];
337 prj->GetProjectFileName(buffer, 256, true);
338
339 custom_app::GetAppC()->UpdateAllWindowTitles();
340
341 char tmp_filename[256];
342 prj->GetFullProjectFileName(tmp_filename, 256);
343
344 ofstream ofile;
345 ofile.open(tmp_filename, ios::out);
346 WriteGPR(* prj, ofile);
347 ofile.close();
348
349 return true;
350 }
351
CancelEvent(void)352 void gtk_file_save_dialog::CancelEvent(void)
353 {
354 }
355
356 /*################################################################################################*/
357
358 const char gtk_file_save_graphics_dialog::title[] = "Save Graphics File - ps";
359 gtk_project * gtk_file_save_graphics_dialog::prj = NULL;
360
gtk_file_save_graphics_dialog(gtk_project * p1)361 gtk_file_save_graphics_dialog::gtk_file_save_graphics_dialog(gtk_project * p1) :
362 gtk_file_dialog(title, "ps", make_tmp_filename(p1))
363 {
364 prj = p1;
365 }
366
~gtk_file_save_graphics_dialog(void)367 gtk_file_save_graphics_dialog::~gtk_file_save_graphics_dialog(void)
368 {
369 }
370
OkEvent(const char * filename)371 bool gtk_file_save_graphics_dialog::OkEvent(const char * filename)
372 {
373 if (prj == NULL)
374 {
375 assertion_failed(__FILE__, __LINE__, "prj == NULL");
376 }
377
378 // write graphics event
379 // write graphics event
380 // write graphics event
381
382 return true;
383 }
384
CancelEvent(void)385 void gtk_file_save_graphics_dialog::CancelEvent(void)
386 {
387 }
388
389 /*################################################################################################*/
390
391 const char gtk_trajfile_dialog::title[] = "Open Trajectory File - traj";
392 gtk_project * gtk_trajfile_dialog::prj = NULL;
393
gtk_trajfile_dialog(gtk_project * p1)394 gtk_trajfile_dialog::gtk_trajfile_dialog(gtk_project * p1) :
395 gtk_file_dialog(title, NULL, NULL)
396 {
397 prj = p1;
398 }
399
~gtk_trajfile_dialog(void)400 gtk_trajfile_dialog::~gtk_trajfile_dialog(void)
401 {
402 }
403
OkEvent(const char * filename)404 bool gtk_trajfile_dialog::OkEvent(const char * filename)
405 {
406 cout << "DEBUG : trying to open \"" << filename << "\"." << endl;
407
408 prj->OpenTrajectory(filename);
409 // check if there were problems with OpenTrajectory()?!?!?!
410 // check if there were problems with OpenTrajectory()?!?!?!
411 // check if there were problems with OpenTrajectory()?!?!?!
412
413 static gtk_trajview_dialog * tvd = NULL;
414
415 if (tvd != NULL) delete tvd; // how to safely release the memory...
416 tvd = new gtk_trajview_dialog(prj); // ...right after the dialog is closed?
417
418 // the dialog will call prj->CloseTrajectory() itself when closed!!!
419 // the dialog will call prj->CloseTrajectory() itself when closed!!!
420 // the dialog will call prj->CloseTrajectory() itself when closed!!!
421
422 gtk_widget_destroy(dialog); // close the dialog -> must return false...
423 return false;
424 }
425
CancelEvent(void)426 void gtk_trajfile_dialog::CancelEvent(void)
427 {
428 }
429
430 /*################################################################################################*/
431
432 const char gtk_importpdb_dialog::title[] = "Import File - Brookhaven PDB/ENT";
433 gtk_project * gtk_importpdb_dialog::prj = NULL;
434
gtk_importpdb_dialog(gtk_project * p1)435 gtk_importpdb_dialog::gtk_importpdb_dialog(gtk_project * p1) :
436 gtk_file_dialog(title, NULL, NULL)
437 {
438 prj = p1;
439 }
440
~gtk_importpdb_dialog(void)441 gtk_importpdb_dialog::~gtk_importpdb_dialog(void)
442 {
443 }
444
OkEvent(const char * filename)445 bool gtk_importpdb_dialog::OkEvent(const char * filename)
446 {
447 if (prj != NULL)
448 {
449 prj->WarningMessage(_("This alternative PDB reader is designed to remove any gaps that might\nexist in the structure data. Therefore the sequence might be altered!"));
450
451 prj->ParseProjectFileNameAndPath(filename);
452
453 prj->importpdb_mdata = prj->readpdb_ReadMData(filename);
454 prj->readpdb_ReadData(filename, prj->importpdb_mdata, -1);
455
456 prj->UpdateAllGraphicsViews();
457 custom_app::GetAppC()->UpdateAllWindowTitles();
458 }
459
460 return true;
461 }
462
CancelEvent(void)463 void gtk_importpdb_dialog::CancelEvent(void)
464 {
465 }
466
467 /*################################################################################################*/
468
469 gtk_project * gtk_command_dialog::prj;
470 oglview_wcl * gtk_command_dialog::wcl;
471
472 GtkWidget * gtk_command_dialog::dialog;
473 GtkWidget * gtk_command_dialog::entry;
474
gtk_command_dialog(gtk_project * p1,oglview_wcl * p2,const char * def_str)475 gtk_command_dialog::gtk_command_dialog(gtk_project * p1, oglview_wcl * p2, const char * def_str)
476 {
477 prj = p1; wcl = p2;
478
479 dialog = gtk_dialog_new();
480 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE); // request a modal window.
481
482 gtk_window_set_title(GTK_WINDOW(dialog), _("Command Interpreter"));
483 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
484 gtk_window_set_default_size(GTK_WINDOW(dialog), 400, 50);
485
486 gtk_container_set_border_width(GTK_CONTAINER(dialog), 10);
487
488 gtk_signal_connect(GTK_OBJECT(dialog), "destroy", GTK_SIGNAL_FUNC(DestroyHandler), this);
489
490 GtkWidget * vbox = gtk_vbox_new(TRUE, 5);
491 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
492
493 entry = gtk_entry_new();
494 if (def_str != NULL) gtk_entry_set_text(GTK_ENTRY(entry), def_str);
495
496 GtkWidget * hbox = gtk_hbox_new(TRUE, 5);
497
498 gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, TRUE, 0);
499 gtk_widget_show(entry);
500
501 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
502
503 GtkWidget * button_ok = gtk_button_new_with_label(_("Execute"));
504 gtk_signal_connect(GTK_OBJECT(button_ok), "clicked", GTK_SIGNAL_FUNC(OkButtonHandler), this);
505
506 GtkWidget * button_cancel = gtk_button_new_with_label(_("Cancel"));
507 gtk_signal_connect(GTK_OBJECT(button_cancel), "clicked", GTK_SIGNAL_FUNC(CancelButtonHandler), this);
508
509 gtk_box_pack_start(GTK_BOX(hbox), button_ok, FALSE, TRUE, 0);
510 gtk_widget_show(button_ok);
511
512 gtk_box_pack_start(GTK_BOX(hbox), button_cancel, FALSE, TRUE, 0);
513 gtk_widget_show(button_cancel);
514
515 gtk_widget_show(hbox);
516 gtk_widget_show(vbox);
517
518 gtk_widget_show(dialog);
519 }
520
~gtk_command_dialog(void)521 gtk_command_dialog::~gtk_command_dialog(void)
522 {
523 prj = NULL;
524 wcl = NULL;
525 }
526
DestroyHandler(GtkWidget *,gpointer data)527 void gtk_command_dialog::DestroyHandler(GtkWidget *, gpointer data)
528 {
529 gtk_command_dialog * ref = (gtk_command_dialog *) data;
530
531 delete ref; // the object was created using new!!!
532 }
533
OkButtonHandler(GtkWidget *,gpointer data)534 void gtk_command_dialog::OkButtonHandler(GtkWidget *, gpointer data)
535 {
536 gtk_command_dialog * ref = (gtk_command_dialog *) data;
537
538 const char * command = gtk_entry_get_text(GTK_ENTRY(ref->entry));
539
540 // first get rid of the dialog...
541 // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
542
543 gtk_widget_hide(dialog);
544
545 // ...and then process the command string (if given).
546 // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
547
548 if (strlen(command) > 0) prj->ProcessCommandString(wcl, command);
549
550 gtk_widget_destroy(dialog);
551 }
552
CancelButtonHandler(GtkWidget *,gpointer data)553 void gtk_command_dialog::CancelButtonHandler(GtkWidget *, gpointer data)
554 {
555 gtk_command_dialog * ref = (gtk_command_dialog *) data;
556
557 gtk_widget_destroy(dialog);
558 }
559
560 /*################################################################################################*/
561
562 // eof
563