1 /*
2    Copyright (C) 2002 Kai Sterker <kai.sterker@gmail.com>
3    Part of the Adonthell Project  <http://adonthell.nongnu.org>
4 
5    Dlgedit 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    Dlgedit 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 Dlgedit.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include "gui_code.h"
20 #include "gui_dlgedit.h"
21 
22 /**
23  * @file gui_code.cc
24  *
25  * @author Kai Sterker
26  * @brief The Custom Code dialog
27  */
28 
29 // OK button pressed
on_button_ok_clicked(GtkButton * button,gpointer user_data)30 void on_button_ok_clicked (GtkButton *button, gpointer user_data)
31 {
32     GuiCode::dialog->applyChanges ();
33     delete GuiCode::dialog;
34 }
35 
36 // callback for closing the window
on_close_window(GtkButton * button,gpointer user_data)37 void on_close_window (GtkButton *button, gpointer user_data)
38 {
39     delete GuiCode::dialog;
40 }
41 
42 // global pointer to the dialog
43 GuiCode *GuiCode::dialog = NULL;
44 
45 // ctor
GuiCode()46 GuiCode::GuiCode ()
47 {
48     GtkWidget *vbox;
49     GtkWidget *notebook;
50     GtkWidget *label;
51     GtkWidget *hbuttonbox;
52     GtkWidget *button_ok;
53     GtkWidget *button_cancel;
54 
55     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
56     g_object_set_data (G_OBJECT (window), "window", window);
57     gtk_widget_set_size_request (window, 400, 320);
58     gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
59 
60     vbox = gtk_vbox_new (FALSE, 2);
61     g_object_ref (vbox);
62     g_object_set_data_full (G_OBJECT (window), "vbox", vbox, (GDestroyNotify)  g_object_unref);
63     gtk_widget_show (vbox);
64     gtk_container_add (GTK_CONTAINER (window), vbox);
65 
66     notebook = gtk_notebook_new ();
67     g_object_ref (notebook);
68     g_object_set_data_full (G_OBJECT (window), "notebook", notebook, (GDestroyNotify)  g_object_unref);
69     gtk_widget_show (notebook);
70     gtk_box_pack_start (GTK_BOX (vbox), notebook, TRUE, TRUE, 0);
71 
72     // entry for import statements
73     imports = new GuiEdit (notebook);
74     gtk_widget_set_tooltip_text (imports->widget (),
75         "Additional modules can be included here");
76 
77     label = gtk_label_new ("Imports");
78     g_object_ref (label);
79     g_object_set_data_full (G_OBJECT (window), "label", label, (GDestroyNotify)  g_object_unref);
80     gtk_widget_show (label);
81     gtk_notebook_set_tab_label (GTK_NOTEBOOK (notebook), gtk_notebook_get_nth_page (GTK_NOTEBOOK (notebook), 0), label);
82 
83     // entry for constructor code
84     ctor = new GuiEdit (notebook);
85     gtk_widget_set_tooltip_text (ctor->widget (),
86         "Code entered here will be added to the dialogue's __init__ method");
87 
88     label = gtk_label_new ("Constructor");
89     g_object_ref (label);
90     g_object_set_data_full (G_OBJECT (window), "label", label, (GDestroyNotify)  g_object_unref);
91     gtk_widget_show (label);
92     gtk_notebook_set_tab_label (GTK_NOTEBOOK (notebook), gtk_notebook_get_nth_page (GTK_NOTEBOOK (notebook), 1), label);
93 
94     // entry for destructor code
95     dtor = new GuiEdit (notebook);
96     gtk_widget_set_tooltip_text (dtor->widget (),
97         "This entry allows you to add additional code to the __del__ method of the dialogue");
98 
99     label = gtk_label_new ("Destructor");
100     g_object_ref (label);
101     g_object_set_data_full (G_OBJECT (window), "label", label, (GDestroyNotify)  g_object_unref);
102     gtk_widget_show (label);
103     gtk_notebook_set_tab_label (GTK_NOTEBOOK (notebook), gtk_notebook_get_nth_page (GTK_NOTEBOOK (notebook), 2), label);
104 
105     // entry for custom methods
106     methods = new GuiEdit (notebook);
107     gtk_widget_set_tooltip_text (methods->widget (),
108         "Custom methods to be inserted into the dialogue");
109 
110     label = gtk_label_new ("Methods");
111     g_object_ref (label);
112     g_object_set_data_full (G_OBJECT (window), "label", label, (GDestroyNotify)  g_object_unref);
113     gtk_widget_show (label);
114     gtk_notebook_set_tab_label (GTK_NOTEBOOK (notebook), gtk_notebook_get_nth_page (GTK_NOTEBOOK (notebook), 3), label);
115 
116     // buttons
117     hbuttonbox = gtk_hbutton_box_new ();
118     g_object_ref (hbuttonbox);
119     g_object_set_data_full (G_OBJECT (window), "hbuttonbox", hbuttonbox, (GDestroyNotify)  g_object_unref);
120     gtk_widget_show (hbuttonbox);
121     gtk_box_pack_start (GTK_BOX (vbox), hbuttonbox, FALSE, FALSE, 0);
122     gtk_button_box_set_layout (GTK_BUTTON_BOX (hbuttonbox), GTK_BUTTONBOX_END);
123     gtk_box_set_spacing (GTK_BOX (hbuttonbox), 10);
124 
125     // OK button
126     button_ok = gtk_button_new_with_label ("OK");
127     g_object_ref (button_ok);
128     g_object_set_data_full (G_OBJECT (window), "button_ok", button_ok, (GDestroyNotify)  g_object_unref);
129     gtk_widget_show (button_ok);
130     gtk_container_add (GTK_CONTAINER (hbuttonbox), button_ok);
131     gtk_widget_set_can_default (button_ok, TRUE);
132 
133     // Cancel button
134     button_cancel = gtk_button_new_with_label ("Cancel");
135     g_object_ref (button_cancel);
136     g_object_set_data_full (G_OBJECT (window), "button_cancel", button_cancel, (GDestroyNotify)  g_object_unref);
137     gtk_widget_show (button_cancel);
138     gtk_container_add (GTK_CONTAINER (hbuttonbox), button_cancel);
139     gtk_widget_set_can_default (button_cancel, TRUE);
140 
141     g_signal_connect (G_OBJECT (button_ok), "clicked", G_CALLBACK (on_button_ok_clicked), NULL);
142     g_signal_connect (G_OBJECT (button_cancel), "clicked", G_CALLBACK (on_close_window), NULL);
143     g_signal_connect (G_OBJECT (window), "delete_event", G_CALLBACK (on_close_window), window);
144 
145     // set transient for dialogue editor main window
146     gtk_window_set_transient_for (GTK_WINDOW (window), GTK_WINDOW (GuiDlgedit::window->getWindow ()));
147 
148     dialog = this;
149     entry = NULL;
150 }
151 
152 // dtor
~GuiCode()153 GuiCode::~GuiCode ()
154 {
155     gtk_widget_destroy (window);
156     dialog = NULL;
157 }
158 
159 // display the dialog
display(DlgModuleEntry * e,const std::string & name)160 void GuiCode::display (DlgModuleEntry *e, const std::string &name)
161 {
162     if (entry != e)
163     {
164         entry = e;
165 
166         // display the contents of the module
167         imports->setText (entry->imports ());
168         ctor->setText (entry->ctor ());
169         dtor->setText (entry->dtor ());
170         methods->setText (entry->methods ());
171     }
172 
173     // set the title
174     gchar *title = g_strjoin (NULL, "Custom Python Code - [",
175         name.c_str (), "]", NULL);
176     gtk_window_set_title (GTK_WINDOW (window), title);
177 
178     // now show the window
179     gtk_widget_show (window);
180 }
181 
182 // store the user's entries
applyChanges()183 void GuiCode::applyChanges ()
184 {
185     entry->setImports (imports->getText ());
186     entry->setCtor (ctor->getText ());
187     entry->setDtor (dtor->getText ());
188     entry->setMethods (methods->getText ());
189 }
190