Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk/gui: added init-routine to Menu as promised in last comit-message

File size: 13.5 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
50OrxonoxGui::OrxonoxGui (int argc, char *argv[])
51{
52  /**
53   * Initializes the Gui
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
102void Widget::init()
103{
104  return;
105}
106
107void Widget::connectSignal (char* event, gint (*signal)(GtkWidget*, GdkEvent*, void *))
108{
109  /**
110   * Connect any signal to any given Sub-widget
111   */
112  g_signal_connect (G_OBJECT (this->widget), event, G_CALLBACK (signal), NULL);
113}
114
115void Widget::connectSignal (char* event, gint (*signal)( GtkWidget*, Widget *))
116{
117  /**
118   * Connect a signal with additionally passing the whole Object
119   */
120g_signal_connect (G_OBJECT (this->widget), event, G_CALLBACK (signal), this);
121}
122
123void Widget::connectSignal (char* event, void* extObj, gint (*signal)(GtkWidget*, GdkEvent*, void *))
124{
125  /**
126   * Connect a signal with additionally passing a whole external Object
127   */
128  g_signal_connect (G_OBJECT (this->widget), event, G_CALLBACK (signal), extObj);
129}
130void Widget::show()
131{
132  /**
133   * Function to Show any Widget
134   */
135  gtk_widget_show (widget);
136}
137
138void Widget::walkThrough (void (*function)(Widget*))
139{
140  /**
141   * Moves Through all the Widgets downwards from this and executes the function on them.
142   * function must be of type void and takes a Widget* as an Input.
143   */
144  function(this);
145  switch (this->is_option)
146    {
147    case -1:
148      static_cast<Container*>(this)->down->walkThrough (function);
149      break;
150    case -2:
151      static_cast<Box*>(this)->down->walkThrough (function);
152      break;
153    } 
154
155  if (this->next != NULL)
156    this->next->walkThrough(function);
157}
158
159
160void Widget::listOptions (Widget* widget)
161{
162  /**
163   * This is for listing the option of "widget"
164   */
165  if (widget->is_option >= 1)
166    cout << static_cast<Option*>(widget)->option_name <<" is : " << static_cast<Option*>(widget)->value <<endl;
167}
168
169void Widget::setOptions (Widget* widget)
170{
171  /**
172   * This is for setting the option of "widget"
173   */
174  if (widget->is_option >= 1)
175    static_cast<Option*>(widget)->redraw();// <<" is : " << static_cast<Option*>(this)->value <<endl;
176}
177
178/* CONTAINERS*/
179void Container::init (void)
180{
181  return;
182}
183void Container::fill (Widget *lowerWidget)
184{
185  /**
186   * fills any given Container with a lowerwidet
187   */
188  if (this->down == NULL)
189    {
190      gtk_container_add (GTK_CONTAINER (this->widget), lowerWidget->widget);
191      this->down = lowerWidget;
192    }
193  else
194    cout << "!!error!! You try to put more than one Widget into a container.\nnot including this item."<<endl;
195}
196
197// gtk_container_set_border_width (GTK_CONTAINER (widget), 5);
198
199/* WINDOW */
200
201Window::Window (void)
202{
203  /**
204   * Creating a Window
205   */
206  this->init();
207}
208
209Window::Window (char* windowName)
210{
211  /**
212   * Creating a Window with passing a name
213   */
214  this->init();
215  this->setTitle (windowName);
216}
217
218Window::~Window ()
219{
220  /**
221   * Destoying a Window (very BAD implemented)
222   */
223  gtk_widget_hide (widget);
224 
225}
226
227void Window::init()
228{
229  /**
230  *initializes a new Window
231  */
232  is_option = -1;
233  next = NULL;
234  down = NULL;
235  widget = gtk_window_new (GTK_WINDOW_TOPLEVEL);
236  gtk_window_set_policy (GTK_WINDOW(widget), TRUE, TRUE, TRUE);
237#if !defined(__WIN32__)
238  gtk_window_set_decorated (GTK_WINDOW (widget), FALSE);
239#endif
240  gtk_container_set_border_width (GTK_CONTAINER (widget), 3);
241}
242
243void Window::showall ()
244{
245  /**
246   * Shows all Widgets that are included within this Widget
247   */
248  gtk_widget_show_all  (widget);
249}
250
251void Window::setTitle (char* title)
252{
253  /**
254   * Set The Window title to title
255   */
256  gtk_window_set_title (GTK_WINDOW (widget), title);
257}
258
259
260gint Window::orxonox_gui_quit (GtkWidget *widget, GdkEvent *event, gpointer data)
261{
262  /**
263   * Quits the orxonox_GUI
264   */
265  if (exec->shouldsave())
266    exec->writeToFile (orxonoxGUI);
267
268  gtk_main_quit();
269  return FALSE;
270}
271
272
273/* FRAME */
274
275Frame::Frame (void)
276{
277  /**
278   * Creates a new Frame without a name
279   */
280  this->init();
281}
282Frame::Frame (char* title)
283{
284  /**
285   * Creates a new Frame with name title
286   */
287  this->init();
288  this->setTitle(title);
289}
290Frame::~Frame ()
291{
292  /**
293   * Destroys given Frame (not implemented yet)
294   */
295}
296
297void Frame::init()
298{
299  is_option = -1;
300  next = NULL;
301  down = NULL;
302  widget = gtk_frame_new ("");
303  gtk_container_set_border_width (GTK_CONTAINER (widget), 3);
304}
305
306void Frame::setTitle (char* title)
307{
308  /**
309   * Sets the Frames name to title
310   */
311  gtk_frame_set_label (GTK_FRAME (widget), title);
312}
313
314// EVENTBOX //
315EventBox::EventBox ()
316{
317  this->init();
318}
319
320EventBox::EventBox (char* title)
321{
322  /**
323   * Creates a new EventBox with name title
324   */
325  this->init();
326  this->setTitle(title);
327
328}
329void EventBox::init(void)
330{
331  is_option = -1;
332  next = NULL;
333  down = NULL;
334  widget = gtk_event_box_new ();
335  gtk_container_set_border_width (GTK_CONTAINER (widget), 3);
336 
337}
338
339
340void EventBox::setTitle (char* title)
341{
342  /**
343   * Sets the EventBoxes name to title
344   */
345  //  gtk_frame_set_label (GTK_FRAME (widget), title);
346}
347EventBox::~EventBox ()
348{
349}
350
351
352/* BOX */
353
354Box::Box (void)
355{
356  /**
357   * Creates a new horizontal Box
358   */
359  this->init('h');
360}
361Box::Box (char boxtype)
362{
363  /**
364   * Creates a new Box if boxtype is 'v' it will be vertically, if 'h' it will be horizontally
365   */
366  this->init(boxtype);
367}
368Box::~Box ()
369{
370  /**
371   * Destroys given Frame (not implemented yet)
372   */
373}
374
375void Box::init(char boxtype)
376{
377  is_option = -2;
378  next = NULL;
379  down = NULL;
380  if (boxtype == 'v')
381    {
382      widget = gtk_vbox_new (FALSE, 0);
383    }
384  else
385    {
386      widget = gtk_hbox_new (FALSE, 0);
387    }
388}
389
390
391void Box::fill (Widget *lowerWidget)
392{
393  /**
394   * Fill a Box with its lowerwidgets
395   */
396  gtk_box_pack_start (GTK_BOX (this->widget), lowerWidget->widget, TRUE, TRUE, 0);
397  if (this->down == NULL)
398    this->down = lowerWidget;
399  else
400    {
401      Widget* tmp;
402      tmp = this->down;
403      while (tmp->next != NULL)
404        {
405          tmp = tmp->next;
406        }
407      tmp->next = lowerWidget;
408    }
409}
410
411/* IMAGE */
412
413Image::Image (char* imagename)
414{
415  is_option = 0;
416  next = NULL;
417  widget = gtk_image_new_from_file (imagename);
418}
419void Image::init()
420{
421 
422}
423
424
425/* OPTION */
426void Option::init()
427{
428  return;
429}
430void Option::setFlagName (char* flagname, int defaultvalue)
431{
432  flag_name = flagname;
433  default_value = defaultvalue;
434  cout << "Set Flagname of " << option_name << " to " << flagname << endl;
435}
436
437void Option::setFlagName (char* flagname, char* flagnameshort,  int defaultvalue)
438{
439  /**
440   * \brief Sets the Flagname of an Option. If it is set different then "" this Option will be saved to the Configurationfile
441   */
442  flag_name = flagname;
443  flag_name_short = flagnameshort;
444  default_value = defaultvalue;
445  cout << "Set Flagname of " << option_name << " to " << flagname << endl;
446}
447
448
449/* BUTTON */
450Button::Button(char* buttonname)
451{
452  /**
453   * Creates a new Button with name buttonname
454   */
455  this->init();
456  this->setTitle(buttonname);
457}
458
459void Button::init(void)
460{
461  is_option = 0;
462  value = 0;
463  next = NULL;
464  option_name ="";
465  flag_name = "";
466  flag_name_short = "";
467  default_value = 0;
468  widget = gtk_button_new_with_label ("");
469}
470
471void Button::setTitle (char *title)
472{
473  option_name = title;
474  gtk_button_set_label (GTK_BUTTON(widget), title);
475}
476
477void Button::redraw ()
478{
479}
480
481/* CHECKBUTTON */
482CheckButton::CheckButton (char* buttonname)
483{
484  /**
485   * Creates a new CheckButton with name buttonname
486   */
487  this->init();
488  this->setTitle(buttonname);
489
490  this->connectSignal ("clicked", this->OptionChange);
491}
492CheckButton::~CheckButton()
493{
494}
495
496void CheckButton::init(void)
497{
498  is_option = 1;
499  value = 0;
500  next = NULL;
501  option_name = "";
502  flag_name = "";
503  flag_name_short = "";
504  default_value = 0;
505  widget = gtk_check_button_new_with_label ("");
506}
507void CheckButton::setTitle(char* title)
508{
509  option_name = title;
510  gtk_button_set_label(GTK_BUTTON(widget), title);
511}
512
513gint CheckButton::OptionChange (GtkWidget *widget, Widget* checkbutton)
514{
515  /**
516   * Writes value, if changed on the checkbutton, to the object.
517   */
518  static_cast<CheckButton*>(checkbutton)->value = (int)gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON ((CheckButton*)checkbutton->widget));
519  flags->setTextFromFlags(orxonoxGUI);
520  cout << static_cast<CheckButton*>(checkbutton)->option_name << " set to: " << static_cast<CheckButton*>(checkbutton)->value << endl;
521}
522
523void CheckButton::redraw ()
524{
525  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), value);
526}
527
528/* SLIDER */
529Slider::Slider (char* slidername, int start, int end)
530{
531  /**
532   * Creates a new Slider with name slidername a beginning value of start and an end value of end.
533   */
534  this->init(start, end);
535  this->setValue(start);
536  this->setTitle(slidername);
537  this->connectSignal ("value_changed", this->OptionChange);
538}
539
540Slider::~Slider()
541{
542}
543
544void Slider::init(int start, int end)
545{
546  is_option = 2;
547  value = 0;
548  next = NULL;
549  option_name = "";
550  flag_name = "";
551  flag_name_short = "";
552  default_value = 0;
553  widget = gtk_hscale_new_with_range (start, end, 5);
554}
555void Slider::setTitle(char* title)
556{
557  option_name = title;
558}
559void Slider::setValue(int value)
560{
561  this->value = value;
562}
563
564gint Slider::OptionChange (GtkWidget *widget, Widget* slider)
565{
566  /**
567   * Writes value, if changed on the slider, to the object.
568   */
569  static_cast<Slider*>(slider)->value = (int)gtk_range_get_value (GTK_RANGE ((Slider*)slider->widget));
570  flags->setTextFromFlags(orxonoxGUI);
571  cout << static_cast<Slider*>(slider)->option_name << " set to: "<< static_cast<Slider*>(slider)->value << endl;
572}
573
574void Slider::redraw ()
575{
576  gtk_range_set_value (GTK_RANGE (widget), value);
577}
578
579Menu::Menu (char* menuname, ...)
580{
581  /**
582   * Creates an Menu-Item-list out of multiple input. Consider, that the last input argument has to be "lastItem" for this to work.
583   */
584  this->init();
585  this->setTitle(menuname);
586   
587  char *itemName;
588 
589  va_start (itemlist, menuname);
590  while (strcmp (itemName = va_arg (itemlist, char*), "lastItem"))
591    {
592      this->addItem(itemName);
593    }
594  va_end(itemlist);
595
596  gtk_option_menu_set_menu (GTK_OPTION_MENU (widget), menu);
597  this->connectSignal ("changed", this->OptionChange);
598}
599
600void Menu::init(void)
601{
602  is_option = 2;
603  value = 0;
604  next = NULL;
605  flag_name = "";
606  flag_name_short = "";
607  default_value = 0;
608  widget = gtk_option_menu_new ();
609  menu = gtk_menu_new ();
610
611}
612
613void Menu::setTitle(char* title)
614{
615  option_name = title;
616}
617
618void Menu::addItem (char* itemName)
619{
620  item = gtk_menu_item_new_with_label (itemName);
621  gtk_menu_shell_append(GTK_MENU_SHELL (menu), item);
622}
623
624gint Menu::OptionChange (GtkWidget *widget, Widget* menu)
625{
626  /**
627   * Writes value, if changed on the Menu, to the object.
628   */
629  static_cast<Menu*>(menu)->value = (int)gtk_option_menu_get_history (GTK_OPTION_MENU (menu->widget));
630  flags->setTextFromFlags(orxonoxGUI);
631  cout << static_cast<Menu*>(menu)->option_name << " changed to : " << static_cast<Menu*>(menu)->value << endl;
632}
633
634void Menu::redraw ()
635{
636  gtk_option_menu_set_history (GTK_OPTION_MENU (widget), value);
637}
638
639Label:: Label ()
640{
641  this->init();
642}
643
644Label:: Label (char* text)
645{
646  is_option = 0;
647  next = NULL;
648  widget = gtk_label_new (text);
649  gtk_label_set_line_wrap (GTK_LABEL(widget), TRUE);
650}
651
652Label::~Label ()
653{}
654
655void Label::init(void)
656{
657  is_option = 0;
658  next = NULL;
659  widget = gtk_label_new ("");
660  gtk_widget_set_usize (widget, 260, 60);
661  gtk_label_set_line_wrap (GTK_LABEL(widget), TRUE);
662}
663 
664void Label::setText (char * text)
665{
666  gtk_label_set_text (GTK_LABEL (this->widget), text);
667}
668
669char* Label::getText ()
670{
671  return ((char*)gtk_label_get_text (GTK_LABEL (this->widget)));
672}
Note: See TracBrowser for help on using the repository browser.