Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk/gui: walkThrough-function implemented, that walks down through all the widgets, and executes static functions (so far: listOptions, setOptions) on them

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