Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/gui/guicc/orxonox_gui.cc @ 1995

Last change on this file since 1995 was 1993, checked in by bensch, 21 years ago

orxonox/branches/gui: removed segmentation fault created by not putting a NULL to Window::down

File size: 9.0 KB
Line 
1#include <iostream.h>
2
3#include "orxonox_gui.h"
4#include "orxonox_gui_video.h"
5#include "orxonox_gui_audio.h"
6#include "orxonox_gui_exec.h"
7
8
9int main( int argc, char *argv[] )
10{
11  OrxonoxGui* orxonoxgui = new OrxonoxGui(argc, argv);
12  return 0;
13}
14
15/* ORXONOXGUI */
16
17OrxonoxGui::OrxonoxGui (int argc, char *argv[])
18{
19  /**
20   * Initializes the Gui
21   */
22  gtk_init (&argc, &argv);
23
24  Window* orxonoxGUI = new Window("Graphical Orxonox Launcher");
25  orxonoxGUI->connectSignal ("destroy", orxonoxGUI->orxonox_gui_quit);
26  orxonoxGUI->connectSignal ("delete_event", orxonoxGUI->orxonox_gui_quit);
27 
28  Box* windowBox = new Box ('v');
29  {
30    Box* avBox = new Box ('h');
31    {
32      OrxonoxGuiVideo* video = new OrxonoxGuiVideo ();
33      avBox->fill (video->getFrame ());
34      OrxonoxGuiAudio* audio = new OrxonoxGuiAudio ();
35      avBox->fill (audio->getFrame ());
36     
37    }
38    windowBox->fill (avBox);
39   
40    OrxonoxGuiExec* exec = new OrxonoxGuiExec (orxonoxGUI);
41    windowBox->fill (exec->getFrame ());
42  }
43  orxonoxGUI->fill (windowBox);
44  orxonoxGUI->listOptions();
45
46  /* TEST ENVIRONMENT
47  Frame* Frametest = new Frame ("Test");
48  orxonoxGUI->fill((Frame*) Frametest);
49  Box* box = new Box ('v');
50  Frametest->fill(box);
51 
52  CheckButton* button = new CheckButton("button");
53  button->connectSignal ("clicked", orxonoxGUI->orxonox_gui_quit);
54  box->fill(button);
55  Slider* slider = new Slider("slider", 0, 100);
56  slider->connectSignal ("value_changed", slider->OptionChange);
57  box->fill(slider);
58  Menu* menu = new Menu("menu1", "no output", "verbose", "debug", "lastItem");
59  menu->connectSignal ("changed", menu->OptionChange);
60  box->fill(menu);
61  Menu* menu2 = new Menu("menu2", "no output", "verbose", "debug", "lastItem");
62  menu2->connectSignal ("changed", menu->OptionChange);
63  box->fill(menu2);
64  orxonoxGUI->listOptions();
65  */
66  orxonoxGUI->showall ();
67
68 
69  gtk_main();
70}
71
72/* WIDGET */
73
74void Widget::connectSignal (char* event, gint (*signal)(GtkWidget*, GdkEvent*, void *))
75{
76  /**
77   * Connect any signal to any given Sub-widget
78   */
79  g_signal_connect (G_OBJECT (this->widget), event, G_CALLBACK (signal), NULL);
80}
81
82void Widget::connectSignal (char* event, gint (*signal)( GtkWidget*, Widget *))
83{
84  /**
85   * Connect a signal with additionally passing the whole Object
86   */
87  g_signal_connect (G_OBJECT (this->widget), event, G_CALLBACK (signal), this);
88}
89
90void Widget::show()
91{
92  /**
93   * Function to Show any Widget
94   */
95  gtk_widget_show (widget);
96}
97
98void Widget::listOptions ()
99{
100  /**
101   * This is For listing all the Options witch are located beneath this Widget.
102   */
103  if (this->is_option >= 1)
104    cout << static_cast<Option*>(this)->option_name <<" is : " << static_cast<Option*>(this)->value <<endl;
105
106  switch (this->is_option)
107    {
108    case -1:
109      static_cast<Container*>(this)->down->listOptions ();
110      break;
111    case -2:
112      static_cast<Box*>(this)->down->listOptions ();
113      break;
114    } 
115
116  if (this->next != NULL)
117    this->next->listOptions ();
118
119
120}
121
122/* CONTAINERS*/
123
124void Container::fill (Widget *lowerWidget)
125{
126  /**
127   * fill any given Container with a lowerwidet
128   */
129  if (this->down == NULL)
130    {
131      gtk_container_add (GTK_CONTAINER (this->widget), lowerWidget->widget);
132      this->down = lowerWidget;
133    }
134  else
135    cout << "!!error!! You try to put more than one Widget into a container.\nnot including this item."<<endl;
136}
137
138// gtk_container_set_border_width (GTK_CONTAINER (widget), 5);
139
140/* WINDOW */
141
142Window::Window (void)
143{
144  /**
145   * Creating a Window
146   */
147  is_option = -1;
148  next = NULL;
149  down = NULL;
150  widget = gtk_window_new (GTK_WINDOW_TOPLEVEL);
151  gtk_window_set_policy (GTK_WINDOW(widget), TRUE, TRUE, TRUE);
152}
153
154Window::Window (char* windowName)
155{
156  /**
157   * Creating a Window with passing a name
158   */
159  is_option = -1;
160  next = NULL;
161  down = NULL;
162  widget = gtk_window_new (GTK_WINDOW_TOPLEVEL);
163  gtk_window_set_policy (GTK_WINDOW (widget), TRUE, TRUE, TRUE);
164  this->setTitle (windowName);
165}
166
167Window::~Window ()
168{
169  /**
170   * Destoying a Window (not implemented)
171   */
172}
173
174void Window::showall ()
175{
176  /**
177   * Shows all Widgets that are included within this Widget
178   */
179  gtk_widget_show_all  (widget);
180}
181
182void Window::setTitle (char* title)
183{
184  /**
185   * Set The Window title to title
186   */
187  gtk_window_set_title (GTK_WINDOW (widget), title);
188}
189
190
191gint Window::orxonox_gui_quit (GtkWidget *widget, GdkEvent *event, gpointer data)
192{
193  /**
194   * Quits the orxonox_GUI
195   */
196  gtk_main_quit();
197  return FALSE;
198}
199
200
201/* FRAME */
202
203Frame::Frame (void)
204{
205  /**
206   * Creates a new Frame without a name
207   */
208  is_option = -1;
209  next = NULL;
210  down = NULL;
211  widget = gtk_frame_new ("");
212}
213Frame::Frame (char* title)
214{
215  /**
216   * Creates a new Frame with name title
217   */
218  is_option = -1;
219  next = NULL;
220  down = NULL;
221  widget = gtk_frame_new (title);
222}
223Frame::~Frame ()
224{
225  /**
226   * Destroys given Frame (not implemented yet)
227   */
228}
229
230void Frame::setTitle (char* title)
231{
232  /**
233   * Sets the Frames name to title
234   */
235  gtk_frame_set_label (GTK_FRAME (widget), title);
236}
237
238
239
240/* BOX */
241
242Box::Box (void)
243{
244  /**
245   * Creates a new horizontal Box
246   */
247  is_option = -2;
248  next = NULL;
249  down = NULL;
250  widget = gtk_hbox_new(FALSE, 0);
251}
252Box::Box (char boxtype)
253{
254  /**
255   * Creates a new Box if boxtype is 'v' it will be vertically, if 'h' it will be horizontally
256   */
257  is_option = -2;
258  next = NULL;
259  down = NULL;
260  if (boxtype == 'v')
261    {
262      widget = gtk_vbox_new (FALSE, 0);
263    }
264  else
265    {
266      widget = gtk_hbox_new (FALSE, 0);
267    }
268}
269
270Box::~Box ()
271{
272  /**
273   * Destroys given Frame (not implemented yet)
274   */
275}
276
277void Box::fill (Widget *lowerWidget)
278{
279  /**
280   * Fill a Box with its lowerwidgets
281   */
282  gtk_box_pack_start (GTK_BOX (this->widget), lowerWidget->widget, TRUE, TRUE, 0);
283  if (this->down == NULL)
284    this->down = lowerWidget;
285  else
286    {
287      Widget* tmp;
288      tmp = this->down;
289      while (tmp->next != NULL)
290        {
291          tmp = tmp->next;
292        }
293      tmp->next = lowerWidget;
294    }
295}
296
297
298/* OPTION */
299
300void Option::setFlagName (char *flagname)
301{
302  /**
303   * \brief Sets the Flagname of an Option. If it is set different then "" this Option will be saved to the Configurationfile
304   */
305  flag_name = flagname;
306  cout << "Set Flagname of " << option_name << " to " << flagname << endl;
307}
308
309
310/* BUTTON */
311Button::Button(char* buttonname)
312{
313  /**
314   * Creates a new Button with name buttonname
315   */
316  is_option = 0;
317  value = 0;
318  next = NULL;
319  option_name = buttonname;
320  flag_name = "";
321  widget = gtk_button_new_with_label (buttonname);
322}
323
324/* CHECKBUTTON */
325CheckButton::CheckButton (char* buttonname)
326{
327  /**
328   * Creates a new CheckButton with name buttonname
329   */
330  is_option = 1;
331  value = 0;
332  next = NULL;
333  option_name = buttonname;
334  flag_name = "";
335  widget = gtk_check_button_new_with_label (buttonname);
336
337  this->connectSignal ("clicked", this->OptionChange);
338}
339gint CheckButton::OptionChange (GtkWidget *widget, Widget* checkbutton)
340{
341  /**
342   * Writes value, if changed on the checkbutton, to the object.
343   */
344  static_cast<CheckButton*>(checkbutton)->value = (int)gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON ((CheckButton*)checkbutton->widget));
345  cout << static_cast<CheckButton*>(checkbutton)->option_name << " set to: " << static_cast<CheckButton*>(checkbutton)->value << endl;
346}
347
348/* SLIDER */
349Slider::Slider (char* slidername, int start, int end)
350{
351  /**
352   * Creates a new Slider with name slidername a beginning value of start and an end value of end.
353   */
354  is_option = 1;
355  value = 0;
356  next = NULL;
357  option_name = slidername;
358  flag_name = "";
359  widget = gtk_hscale_new_with_range (start, end, 5);
360  value = start;
361
362  this->connectSignal ("value_changed", this->OptionChange);
363}
364
365gint Slider::OptionChange (GtkWidget *widget, Widget* slider)
366{
367  /**
368   * Writes value, if changed on the slider, to the object.
369   */
370  static_cast<Slider*>(slider)->value = (int)gtk_range_get_value (GTK_RANGE ((Slider*)slider->widget));
371  cout << static_cast<Slider*>(slider)->option_name << " set to: "<< static_cast<Slider*>(slider)->value << endl;
372}
373
374
375Menu::Menu (char* menuname, ...)
376{
377  /**
378   * Creates an Menu-Item-list out of multiple input. Consider, that the last input argument has to be "lastItem" for this to work.
379   */
380  is_option = 1;
381  value = 0;
382  next = NULL;
383  option_name = menuname;
384  flag_name = "";
385  char *tmp;
386  va_list itemlist;
387  widget = gtk_option_menu_new ();
388  GtkWidget* menu = gtk_menu_new ();
389  GtkWidget* item;
390
391  va_start (itemlist, menuname);
392  while (strcmp (tmp = va_arg (itemlist, char*), "lastItem"))
393    {
394      item = gtk_menu_item_new_with_label (tmp);
395      gtk_menu_shell_append(GTK_MENU_SHELL (menu), item);
396    }
397  va_end(itemlist);
398
399  gtk_option_menu_set_menu (GTK_OPTION_MENU (widget), menu);
400  this->connectSignal ("changed", this->OptionChange);
401}
402
403gint Menu::OptionChange (GtkWidget *widget, Widget* menu)
404{
405  /**
406   * Writes value, if changed on the Menu, to the object.
407   */
408  static_cast<Menu*>(menu)->value = (int)gtk_option_menu_get_history (GTK_OPTION_MENU (menu->widget));
409  cout << static_cast<Menu*>(menu)->option_name << " changed to : " << static_cast<Menu*>(menu)->value << endl;
410}
Note: See TracBrowser for help on using the repository browser.