Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/chris/gui/orxonox_gui.cc @ 3176

Last change on this file since 3176 was 2058, checked in by chris, 20 years ago

orxonox/branches/chris: Trunk remerged into my branch started on SDL reconfiguration

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