Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2011 was 2011, checked in by bensch, 20 years ago

orxonox/branches/gui/guicc: now only saves, if it is told to ⇒ now fully functional.

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