Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/gui/orxonox_gui.cc @ 2588

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

orxonox/trunk/gui: doxygen orxodox created for all h-files, classes and functions.

File size: 17.9 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program; if not, write to the Free Software Foundation,
18   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
19
20
21   ### File Specific:
22   main-programmer: Benjamin Grauer
23
24*/
25
26#include <iostream.h>
27
28#include "orxonox_gui.h"
29#include "orxonox_gui_video.h"
30#include "orxonox_gui_audio.h"
31#include "orxonox_gui_exec.h"
32#include "orxonox_gui_flags.h"
33#include "orxonox_gui_banner.h"
34
35  Window* orxonoxGUI;
36  OrxonoxGuiVideo* video;
37  OrxonoxGuiAudio* audio;
38  OrxonoxGuiExec* exec;
39  OrxonoxGuiFlags* flags;
40  OrxonoxGuiBanner* banner;
41
42int main( int argc, char *argv[] )
43{
44  OrxonoxGui* orxonoxgui = new OrxonoxGui(argc, argv);
45  return 0;
46}
47
48/* ORXONOXGUI */
49
50/**
51 * Initializes the Gui
52*/
53OrxonoxGui::OrxonoxGui (int argc, char *argv[])
54{
55  gtk_init (&argc, &argv);
56  gtk_rc_parse( "rc" );
57 
58  orxonoxGUI = new Window("Graphical Orxonox Launcher");
59  orxonoxGUI->connectSignal ("destroy", orxonoxGUI->orxonox_gui_quit);
60  orxonoxGUI->connectSignal ("delete_event", orxonoxGUI->orxonox_gui_quit);
61 
62  Box* windowBox = new Box ('h');
63
64  banner = new OrxonoxGuiBanner();
65  windowBox->fill (banner->getEventBox());
66 
67  Box* optionBox = new Box ('v');
68 
69  Box* avBox = new Box ('h');
70
71  video = new OrxonoxGuiVideo ();
72  avBox->fill (video->getFrame ());
73  audio = new OrxonoxGuiAudio ();
74  avBox->fill (audio->getFrame ());
75     
76  optionBox->fill (avBox);
77   
78  exec = new OrxonoxGuiExec (orxonoxGUI);
79  optionBox->fill (exec->getFrame ());
80
81  flags = new OrxonoxGuiFlags (orxonoxGUI);
82
83
84  optionBox->fill (flags->getFrame ());
85  windowBox->fill (optionBox);
86 
87  orxonoxGUI->fill (windowBox);
88  flags->setTextFromFlags (orxonoxGUI);
89
90  exec->setFilename ("~/.orxonox.conf");
91  exec->readFromFile (orxonoxGUI);
92  orxonoxGUI->walkThrough(orxonoxGUI->listOptions);
93
94  orxonoxGUI->showall ();
95
96 
97  gtk_main();
98}
99
100/* WIDGET */
101/**
102 * Initializes a widget.
103 * Nothing to do here.
104 */
105void Widget::init()
106{
107  return;
108}
109
110/**
111 * Connect any signal to any given Sub-widget
112 */
113void Widget::connectSignal (char* event, gint (*signal)(GtkWidget*, GdkEvent*, void *))
114{
115  g_signal_connect (G_OBJECT (this->widget), event, G_CALLBACK (signal), NULL);
116}
117
118/**
119 * Connect a signal with additionally passing the whole Object
120 */
121void Widget::connectSignal (char* event, gint (*signal)( GtkWidget*, Widget *))
122{
123g_signal_connect (G_OBJECT (this->widget), event, G_CALLBACK (signal), this);
124}
125
126/**
127 * Connect a signal with additionally passing a whole external Object
128 */
129void Widget::connectSignal (char* event, void* extObj, gint (*signal)(GtkWidget*, GdkEvent*, void *))
130{
131  g_signal_connect (G_OBJECT (this->widget), event, G_CALLBACK (signal), extObj);
132}
133
134
135/**
136 * Function to Shows a specific widget.
137 */
138void Widget::show()
139{
140  gtk_widget_show (this->widget);
141}
142
143/**
144 * Moves through all the Widgets downwards from this and executes the function on them.
145 \param function must be of type void and takes a Widget* as an Input.
146
147 */
148void Widget::walkThrough (void (*function)(Widget*))
149{
150  function(this);
151  switch (this->is_option)
152    {
153    case -1:
154      static_cast<Container*>(this)->down->walkThrough (function);
155      break;
156    case -2:
157      static_cast<Box*>(this)->down->walkThrough (function);
158      break;
159    } 
160
161  if (this->next != NULL)
162    this->next->walkThrough(function);
163}
164
165
166/**
167 * This is for listing the option of "widget"
168 \param widget specifies the widget that should be listed
169*/
170void Widget::listOptions (Widget* widget)
171{
172  if (widget->is_option >= 1)
173    cout << static_cast<Option*>(widget)->option_name <<" is : " << static_cast<Option*>(widget)->value <<endl;
174}
175
176/**
177 * This is for setting the option of "widget"
178 \param widget specifies the widget that should be set.
179*/
180void Widget::setOptions (Widget* widget)
181{
182  if (widget->is_option >= 1)
183    static_cast<Option*>(widget)->redraw();// <<" is : " << static_cast<Option*>(this)->value <<endl;
184}
185
186/* CONTAINERS*/
187
188/**
189 * Initializes a Container.
190 * nothing to do here
191 */
192void Container::init (void)
193{
194  return;
195}
196
197/**
198 * Fills a Container with lowerWidget.
199 * It does this by filling up the down pointer only if down points to NULL.
200 \param lowerWidget the Widget that should be filled into the Container.
201*/
202void Container::fill (Widget *lowerWidget)
203{
204  if (this->down == NULL)
205    {
206      gtk_container_add (GTK_CONTAINER (this->widget), lowerWidget->widget);
207      this->down = lowerWidget;
208    }
209  else
210    cout << "!!error!! You try to put more than one Widget into a container.\nnot including this item."<<endl;
211}
212
213// gtk_container_set_border_width (GTK_CONTAINER (widget), 5);
214
215/* WINDOW */
216
217  /**
218   * Creating a new Window without a Name
219   */
220Window::Window (void)
221{
222  this->init();
223}
224
225/**
226 * Creating a Window with a name
227 \param windowName the name the window should get.
228 */
229Window::Window (char* windowName)
230{
231  this->init();
232  this->setTitle (windowName);
233}
234
235/**
236 * Destoying a Window (very BAD implemented).
237 * For now it just hides the window.
238 */
239Window::~Window ()
240{
241  gtk_widget_hide (widget);
242}
243
244/**
245 *initializes a new Window
246 */
247void Window::init()
248{
249  is_option = -1;
250  next = NULL;
251  down = NULL;
252  widget = gtk_window_new (GTK_WINDOW_TOPLEVEL);
253  gtk_window_set_policy (GTK_WINDOW(widget), TRUE, TRUE, TRUE);
254#if !defined(__WIN32__)
255  gtk_window_set_decorated (GTK_WINDOW (widget), FALSE);
256#endif
257  gtk_container_set_border_width (GTK_CONTAINER (widget), 3);
258}
259
260/**
261 * Shows all Widgets that are included within this->widget.
262 */
263void Window::showall ()
264{
265  gtk_widget_show_all  (widget);
266}
267
268/**
269 * Set The Window-title to title
270 \param title title the Window should get.
271*/
272void Window::setTitle (char* title)
273{
274  gtk_window_set_title (GTK_WINDOW (widget), title);
275}
276
277/**
278 * Quits the orxonox_GUI.
279 * This can be called as a Signal and is therefor static
280 \param widget The widget that called this function
281 \param event the event that happened to execute this function
282 \param data some data passed with the Signal
283 */
284gint Window::orxonox_gui_quit (GtkWidget *widget, GdkEvent *event, gpointer data)
285{
286  if (exec->shouldsave())
287    exec->writeToFile (orxonoxGUI);
288
289  gtk_main_quit();
290  return FALSE;
291}
292
293
294/* FRAME */
295
296/**
297 * Creates a new Frame without a name
298 */
299Frame::Frame (void)
300{
301  this->init();
302}
303
304/**
305 * Creates a new Frame with name title
306 */
307Frame::Frame (char* title)
308{
309  this->init();
310  this->setTitle(title);
311}
312
313/**
314 * Destroys given Frame (not implemented yet)
315 */
316Frame::~Frame ()
317{
318}
319
320/**
321 * Initializes a new Frame with default settings
322 */
323void Frame::init()
324{
325  is_option = -1;
326  next = NULL;
327  down = NULL;
328  widget = gtk_frame_new ("");
329  gtk_container_set_border_width (GTK_CONTAINER (widget), 3);
330}
331
332/**
333 * Sets the Frames name to title
334 \param title The title the Frame should get.
335 */
336void Frame::setTitle (char* title)
337{
338  gtk_frame_set_label (GTK_FRAME (widget), title);
339}
340
341// EVENTBOX //
342
343/**
344 * Creates a new EventBox with default settings.
345 */
346EventBox::EventBox ()
347{
348  this->init();
349}
350/**
351 * Creates a new EventBox with name title
352 \param title title the Eventbox should get (only data-structure-internal)
353*/
354EventBox::EventBox (char* title)
355{
356  this->init();
357  this->setTitle(title);
358
359}
360
361/**
362 * Initializes a new EventBox
363 */
364void EventBox::init(void)
365{
366  is_option = -1;
367  next = NULL;
368  down = NULL;
369  widget = gtk_event_box_new ();
370  gtk_container_set_border_width (GTK_CONTAINER (widget), 3);
371 
372}
373
374/**
375 * Sets the Title of the EventBox (not implemented)
376 \param title Name the EventBox should get (only datastructure-internal).
377*/
378void EventBox::setTitle (char* title)
379{
380
381}
382
383/**
384 * Destructs an EventBox (not Implemented)
385 */
386EventBox::~EventBox ()
387{
388}
389
390
391/* BOX */
392
393/**
394 * Creates a new horizontal Box
395 */
396Box::Box (void)
397{
398  this->init('h');
399}
400
401/**
402 * Creates a new Box of type boxtype
403 \param boxtype if 'v' the Box will be vertically, if 'h' the Box will be horizontally
404 */
405Box::Box (char boxtype)
406{
407  this->init(boxtype);
408}
409
410/**
411 * Destroys given Box (not implemented yet)
412 */
413Box::~Box ()
414{
415}
416
417/**
418 * Initializes a new Box with type boxtype
419 \param boxtype see Box(char boxtype)
420*/
421void Box::init(char boxtype)
422{
423  is_option = -2;
424  next = NULL;
425  down = NULL;
426  if (boxtype == 'v')
427    {
428      widget = gtk_vbox_new (FALSE, 0);
429    }
430  else
431    {
432      widget = gtk_hbox_new (FALSE, 0);
433    }
434}
435
436/**
437 * Fills a box with a given Widget.
438 * It does this by apending the first one to its down-pointer and all its following ones to the preceding next-pointer. The last one will receive a NULL pointer as Next
439 \param lowerWidget the next Widget that should be appendet to this Box
440 */
441void Box::fill (Widget *lowerWidget)
442{
443  /**
444   * Fill a Box with its lowerwidgets
445   */
446  gtk_box_pack_start (GTK_BOX (this->widget), lowerWidget->widget, TRUE, TRUE, 0);
447  if (this->down == NULL)
448    this->down = lowerWidget;
449  else
450    {
451      Widget* tmp;
452      tmp = this->down;
453      while (tmp->next != NULL)
454        {
455          tmp = tmp->next;
456        }
457      tmp->next = lowerWidget;
458    }
459}
460
461/* IMAGE */
462
463/**
464 * Creates a new Image
465 \param imagename the location of the Image on the Hard Disc
466*/
467Image::Image (char* imagename)
468{
469  this->init();
470  widget = gtk_image_new_from_file (imagename);
471}
472
473/**
474 * Initializes a new Image
475 */
476void Image::init()
477{
478  is_option = 0;
479  next = NULL;
480}
481
482
483/* OPTION */
484
485/**
486 * Initializes a new Option: nothing to do here
487 */
488void Option::init()
489{
490  return;
491}
492
493/**
494 * This sets The FlagName of an Option and defines its default Values
495 !! Options will be saved if flagname is different from "" !!
496\param flagname the Name that will be displayed in the output
497\param defaultvalue the default Value for this Option (see definition of defaultvalue
498*/
499void Option::setFlagName (char* flagname, int defaultvalue)
500{
501  flag_name = flagname;
502  default_value = defaultvalue;
503  cout << "Set Flagname of " << option_name << " to " << flagname << endl;
504}
505
506/**
507 * see Option::setFlagName (char* flagname, int defaultvalue)
508 \param flagname the Name that will be displayed in the output
509 \param defaultvalue the default Value for this Option (see definition of defaultvalue
510 \param flagnameshort a short flagname to be displayed in the output
511*/
512void Option::setFlagName (char* flagname, char* flagnameshort,  int defaultvalue)
513{
514  flag_name = flagname;
515  flag_name_short = flagnameshort;
516  default_value = defaultvalue;
517  cout << "Set Flagname of " << option_name << " to " << flagname << endl;
518}
519
520
521/* BUTTON */
522
523/**
524 * Creates a new Button with a buttonname
525 \param buttonname sets the Name of the Button
526*/
527Button::Button(char* buttonname)
528{
529  this->init();
530  this->setTitle(buttonname);
531}
532
533/**
534 * Initializes a new Button
535 */
536void Button::init(void)
537{
538  is_option = 0;
539  value = 0;
540  next = NULL;
541  option_name ="";
542  flag_name = "";
543  flag_name_short = "";
544  default_value = 0;
545  widget = gtk_button_new_with_label ("");
546}
547
548/**
549 * Sets a new name to the Button
550\param title The name the Button should get
551*/
552void Button::setTitle (char *title)
553{
554  option_name = title;
555  gtk_button_set_label (GTK_BUTTON(widget), title);
556}
557
558/**
559 * redraws the Button
560 * not implemented yet
561 */
562void Button::redraw ()
563{
564}
565
566/* CHECKBUTTON */
567
568/**
569 * Creates a new CheckButton with an ame
570 \param buttonname The name the CheckButton should display.
571 */
572CheckButton::CheckButton (char* buttonname)
573{
574  this->init();
575  this->setTitle(buttonname);
576
577  this->connectSignal ("clicked", this->OptionChange);
578}
579
580/**
581 * Destructs a CheckButton (not Implemented yet).
582 */
583CheckButton::~CheckButton()
584{
585}
586
587/**
588 * Initiali a new CheckButton with default settings
589 */
590void CheckButton::init(void)
591{
592  is_option = 1;
593  value = 0;
594  next = NULL;
595  option_name = "";
596  flag_name = "";
597  flag_name_short = "";
598  default_value = 0;
599  widget = gtk_check_button_new_with_label ("");
600}
601
602/**
603 * Sets a new Title to a CheckButton
604 \param title The new Name the CheckButton should display.
605*/
606void CheckButton::setTitle(char* title)
607{
608  option_name = title;
609  gtk_button_set_label(GTK_BUTTON(widget), title);
610}
611
612
613/**
614 * Signal OptionChange writes the Value from the CheckButton to its Object-Database.
615 \param widget The widget(CheckButton) that has a changed Value
616 \param checkbutton the CheckButton-Object that should receive the change.
617 */
618gint CheckButton::OptionChange (GtkWidget *widget, Widget* checkbutton)
619{
620  static_cast<CheckButton*>(checkbutton)->value = (int)gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON ((CheckButton*)checkbutton->widget));
621  flags->setTextFromFlags(orxonoxGUI);
622  cout << static_cast<CheckButton*>(checkbutton)->option_name << " set to: " << static_cast<CheckButton*>(checkbutton)->value << endl;
623}
624
625/**
626 * Redraws the CheckButton (if option has changed).
627 * Example: if new settings are loaded the Button must be redrawn for the GUI to display that Change
628 */
629void CheckButton::redraw ()
630{
631  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), value);
632}
633
634/* SLIDER */
635
636/**
637 * Creates a new Slider
638 \param slidername The data-structure-name of the slider.
639 \param start The minimal Value of the slider.
640 \param end The maximal Value of the slider.
641*/
642Slider::Slider (char* slidername, int start, int end)
643{
644  this->init(start, end);
645  this->setValue(start);
646  this->setTitle(slidername);
647  this->connectSignal ("value_changed", this->OptionChange);
648}
649
650/**
651 * Destructs a Slider (not implemented yet)
652 */
653Slider::~Slider()
654{
655}
656
657/**
658 * Initializes a Slider with start and end Values
659 * params: see Slider::Slider (char* slidername, int start, int end)
660 */
661void Slider::init(int start, int end)
662{
663  is_option = 2;
664  value = 0;
665  next = NULL;
666  option_name = "";
667  flag_name = "";
668  flag_name_short = "";
669  default_value = 0;
670  widget = gtk_hscale_new_with_range (start, end, 5);
671}
672
673/**
674 * Sets a new Title to the Slider
675 \param title The new Name of the slider
676*/
677void Slider::setTitle(char* title)
678{
679  option_name = title;
680}
681
682/**
683 * Setting a new value to the Slider.
684 * Maybe you also require a Slider::redraw() for this to display
685 */
686void Slider::setValue(int value)
687{
688  this->value = value;
689}
690
691/**
692 * Signal OptionChange writes the Value from the Slider to its Object-Database.
693 \param widget The widget(Slider) that has a changed Value
694 \param slider the Slider-Object that should receive the change.
695 */
696gint Slider::OptionChange (GtkWidget *widget, Widget* slider)
697{
698  static_cast<Slider*>(slider)->value = (int)gtk_range_get_value (GTK_RANGE ((Slider*)slider->widget));
699  flags->setTextFromFlags(orxonoxGUI);
700  cout << static_cast<Slider*>(slider)->option_name << " set to: "<< static_cast<Slider*>(slider)->value << endl;
701}
702
703/**
704 * Redraws the widget
705 * Example: see void CheckButton::redraw ()
706 */
707void Slider::redraw ()
708{
709  gtk_range_set_value (GTK_RANGE (widget), value);
710}
711
712/* MENU */
713
714/**
715 * Creates a Menu-Item-list out of multiple input.
716 * !! Consider, that the last input argument has to be "lastItem" for this to work!!
717 \param menuname The Database-Name of this Menu
718 \param ... items to be added to this Menu. !! Consider, that the last input argument has to be "lastItem" for this to work!!
719 */
720Menu::Menu (char* menuname, ...)
721{
722  this->init();
723  this->setTitle(menuname);
724   
725  char *itemName;
726 
727  va_start (itemlist, menuname);
728  while (strcmp (itemName = va_arg (itemlist, char*), "lastItem"))
729    {
730      this->addItem(itemName);
731    }
732  va_end(itemlist);
733
734  gtk_option_menu_set_menu (GTK_OPTION_MENU (widget), menu);
735  this->connectSignal ("changed", this->OptionChange);
736}
737
738/**
739 * Initializes a new Menu with no items
740 */
741void Menu::init(void)
742{
743  is_option = 2;
744  value = 0;
745  next = NULL;
746  flag_name = "";
747  flag_name_short = "";
748  default_value = 0;
749  widget = gtk_option_menu_new ();
750  menu = gtk_menu_new ();
751
752}
753
754/**
755 * Sets the Database-Name of this Menu
756 \param title Database-Name to be set.
757*/
758void Menu::setTitle(char* title)
759{
760  option_name = title;
761}
762
763/**
764 * appends a new Item to the Menu-List.
765 \param itemName the itemName to be appendet.
766*/
767void Menu::addItem (char* itemName)
768{
769  item = gtk_menu_item_new_with_label (itemName);
770  gtk_menu_shell_append(GTK_MENU_SHELL (menu), item);
771}
772
773/**
774 * Signal OptionChange writes the Value from the Menu to its Object-Database.
775 \param widget The widget(Menu) that has a changed Value
776 \param menu the Menu-Object that should receive the change.
777 */gint Menu::OptionChange (GtkWidget *widget, Widget* menu)
778{
779  static_cast<Menu*>(menu)->value = (int)gtk_option_menu_get_history (GTK_OPTION_MENU (menu->widget));
780  flags->setTextFromFlags(orxonoxGUI);
781  cout << static_cast<Menu*>(menu)->option_name << " changed to : " << static_cast<Menu*>(menu)->value << endl;
782}
783
784/**
785 * Redraws the widget
786 * Example: see void CheckButton::redraw ()
787 */
788void Menu::redraw ()
789{
790  gtk_option_menu_set_history (GTK_OPTION_MENU (widget), value);
791}
792
793/**
794 * Creates a new default Label with no Text.
795 * You migth consider adding Label::setTitle with this.
796 */
797Label:: Label ()
798{
799  this->init();
800}
801
802/**
803 * Creates a new Label with a Text.
804 \param text The text to be displayed.
805*/
806Label:: Label (char* text)
807{
808  is_option = 0;
809  next = NULL;
810  widget = gtk_label_new (text);
811  gtk_label_set_line_wrap (GTK_LABEL(widget), TRUE);
812}
813
814/**
815 * Destructs a Label
816 */
817Label::~Label ()
818{}
819
820/**
821 * initializes a new Label
822 */
823void Label::init(void)
824{
825  is_option = 0;
826  next = NULL;
827  widget = gtk_label_new ("");
828  gtk_widget_set_usize (widget, 260, 60);
829  gtk_label_set_line_wrap (GTK_LABEL(widget), TRUE);
830}
831
832/**
833 * Sets a new Text to a Label.
834 \param text The text to be inserted into the Label.
835 */
836void Label::setText (char * text)
837{
838  gtk_label_set_text (GTK_LABEL (this->widget), text);
839}
840
841/**
842 * get the Text of a Label
843 \return The Text the Label holds.
844*/
845char* Label::getText ()
846{
847  return ((char*)gtk_label_get_text (GTK_LABEL (this->widget)));
848}
Note: See TracBrowser for help on using the repository browser.