Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/updater/src/gui/orxonox_gui_gtk.cc @ 3298

Last change on this file since 3298 was 3298, checked in by bensch, 19 years ago

orxonox/branches/updater: autoupdate at beginning works partly

File size: 35.1 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
27#include <iostream>
28
29#include "orxonox_gui_gtk.h"
30
31
32using namespace std;
33
34// temporarily.
35#include "orxonox_gui_flags.h"
36#include "orxonox_gui_exec.h"
37extern Window* orxonoxGUI;
38extern OrxonoxGuiFlags* flags;
39extern OrxonoxGuiExec* exec;
40
41#ifdef HAVE_GTK2
42/**
43   \brief Initializes the Guis GTK-stuff.
44   \param argc the argument count.
45   \param argv The Argument strings.
46*/
47bool initGTK(int argc, char *argv[])
48{
49#ifdef HAVE_GTHREAD
50  PRINTF(3)("Initializing the ThreadSystem of the GUI\n");
51  g_thread_init(NULL);
52  gdk_threads_init();
53#endif /* HAVE_GTHREAD */
54  gtk_init (&argc, &argv);
55  gtk_rc_parse( "rc" );
56}
57
58/**
59   \brief enters the GTK-main-loop
60*/
61bool mainloopGTK(void)
62{
63  gdk_threads_enter();
64  gtk_main();
65  gdk_threads_leave();
66  delete Window::mainWindow;
67}
68#endif /* HAVE_GTK2 */
69
70
71//////////////////////////////
72/// DEFINING WIDGET-CLASES ///
73//////////////////////////////
74
75/* WIDGET */
76
77/**
78   \brief deletes any given Widget
79   This is still pretty crappy.
80*/
81Widget::~Widget()
82{
83  this->destroy();
84}
85
86/**
87   \brief Initializes a widget.
88   Initializes the next Pointer and the other Widget-specific Defaults.
89*/
90void Widget::init()
91{
92  next = NULL;
93  this->title = NULL;
94  return;
95}
96
97/**
98   \brief Destroys a Widget
99*/
100void Widget::destroy(void)
101{
102  if (this->title)
103    {
104      delete []this->title;
105    }
106 
107  PRINTF(4)("deleting the Widget part.\n");
108
109  PRINTF(4)("deleting recursively\n");
110
111  // deleting next item if existent
112  if (this->next)
113    delete this->next;
114  this->next = NULL;
115
116  //!  \todo not hiding widget, deleting.
117  //  this->hide();
118  //  delete this->widget;
119}
120
121/**
122   \brief makes the widget visible.
123*/
124void Widget::show()
125{
126#ifdef HAVE_GTK2
127  gtk_widget_show (this->widget);
128#endif /* HAVE_GTK2 */
129}
130
131/**
132   \brief hides the widget.
133*/
134void Widget::hide()
135{
136#ifdef HAVE_GTK2
137  gtk_widget_hide (this->widget);
138#endif /* HAVE_GTK2 */
139}
140
141/**
142   \brief Sets the resolution of a specific widget to the given size.
143   \param width the width of the widget to set.
144   \param height the height of the widget to set.
145*/
146void Widget::setSize(int width, int height)
147{
148#ifdef HAVE_GTK2
149  gtk_widget_set_usize (this->widget, width, height);
150#endif /* HAVE_GTK2 */
151}
152
153/**
154   \brief searches through widgets for a Name.
155*/
156Widget* Widget::findWidgetByName(char* name, unsigned int depth)
157{
158  Widget* tmp = NULL;
159
160  if (this->title && !strcmp(this->title, name))
161    return this;
162
163  if (this->isOption < 0 && static_cast<Packer*>(this)->down)
164    tmp = static_cast<Packer*>(this)->down->findWidgetByName(name, depth+1);
165  if (tmp)
166    return tmp;
167
168 
169  if (depth>0 && this->next)
170    return this->next->findWidgetByName(name, depth);
171
172  return NULL;
173}
174
175/**
176   \brief Moves through all the Widgets downwards from this and executes the function on them.
177   \param function must be of type void and takes a Widget* as an Input.
178   \param depth the current depth. if > 0 then the next Widget will also be walked through.
179*/
180void Widget::walkThrough (void (*function)(Widget*), unsigned int depth)
181{
182  function(this);
183  if (this->isOption < 0)
184    {
185      static_cast<Packer*>(this)->down->walkThrough (function, depth+1);
186    } 
187
188  if (this->next && depth != 0)
189    this->next->walkThrough(function, depth);
190}
191
192/**
193   \brief Moves through all the Widgets downwards from this and executes the function on them.
194   \param function must be of type void and takes a Widget* as an Input.
195   \param data Additional Data you want to pass to the function.
196   \param depth the current depth. if > 0 then the next Widget will also be walked through.
197*/
198void Widget::walkThrough (void (*function)(Widget*, void*), void* data, unsigned int depth)
199{
200  function(this, data);
201  if (this->isOption < 0)
202    {
203      static_cast<Packer*>(this)->down->walkThrough(function, data, depth+1);
204    }
205  if (this->next && depth != 0)
206    this->next->walkThrough(function, data, depth);
207}
208
209/**
210    \brief This is for listing the option of "widget"
211    \param widget specifies the widget that should be listed
212*/
213void Widget::listOptions (Widget* widget)
214{
215  if (widget->isOption < 0 && static_cast<Packer*>(widget)->groupName)
216    cout << "[" << static_cast<Packer*>(widget)->groupName << "]\n";
217  if (widget->isOption >= 1 && widget->isOption <= 3)
218    cout << "  " << static_cast<Option*>(widget)->title <<" is : " << static_cast<Option*>(widget)->value <<endl;
219  else if (widget->isOption == 5)
220    cout << "  " << static_cast<Option*>(widget)->title <<" is : " << static_cast<OptionLabel*>(widget)->cValue <<endl;
221}
222
223/**
224    \brief This is for setting the option of "widget"
225    \param widget specifies the widget that should be set.
226*/
227void Widget::setOptions (Widget* widget)
228{
229  if (widget->isOption >= 1)
230    static_cast<Option*>(widget)->redraw();
231}
232
233/**
234   \brief Walks through all the Flags given at startuptime.
235*/
236void Widget::flagCheck(Widget* widget, void* flagName)
237{
238  if (widget->isOption>=1)
239    {     
240      Option* option = (Option*)widget;
241      char* name = (char*)flagName;
242      char* value = NULL;
243      bool found = false;
244      // check if long flag matches
245      if ((option->flagName && strlen(name)>2 && !strncmp(name+2, option->flagName, strlen(option->flagName)) && (name[strlen(option->flagName)+2] == '\0' || name[strlen(option->flagName)+2] == '=') ))
246        {
247          found = true;
248          if (name[strlen(option->flagName)+2] == '=')
249            {
250              value = name+strlen(option->flagName)+3;
251            }
252        }
253      else if (option->flagNameShort && strlen(name)>1 && !strncmp(name+1, option->flagNameShort, strlen(option->flagNameShort))&& (name[strlen(option->flagNameShort)+1] == '\0' || name[strlen(option->flagNameShort)+1] == '=')) // check if short flag matches
254        {
255          found = true;
256          if (name[strlen(option->flagNameShort)+1] == '=')
257            {
258              value = name+strlen(option->flagNameShort)+2;
259            }     
260        }
261
262      if (found)
263        {
264          PRINT(3)("found matching Flag %s\n", name);
265          if (value)
266            {
267              PRINT(3)("with Value %s\n", value);
268              option->value = atoi(value);
269            }
270          else
271            {
272              option->value = !option->defaultValue;
273            }
274          option->redraw();
275        }
276
277    }
278}
279
280#ifdef HAVE_GTK2
281/**
282    \brief Connect any signal to any given Sub-widget
283*/
284gulong Widget::connectSignal (char* event, gint (*signal)(GtkWidget*, GdkEvent*, void *))
285{
286  return g_signal_connect (G_OBJECT (this->widget), event, G_CALLBACK (signal), NULL);
287}
288
289/**
290   \brief Connect a signal with additionally passing the whole Object
291*/
292gulong Widget::connectSignal (char* event, gint (*signal)( GtkWidget*, Widget *))
293{
294  return g_signal_connect (G_OBJECT (this->widget), event, G_CALLBACK (signal), this);
295}
296
297/**
298   \brief Connect a signal with additionally passing a whole external Object
299*/
300gulong Widget::connectSignal (char* event, void* extObj, gint (*signal)(GtkWidget*, GdkEvent*, void *))
301{
302  return g_signal_connect (G_OBJECT (this->widget), event, G_CALLBACK (signal), extObj);
303}
304
305/**
306   \brief Connect a signal with additionally passing a whole external Object
307*/
308gulong Widget::connectSignal (char* event, void* extObj, gint (*signal)(GtkWidget*, GdkEventKey*, void *))
309{
310  return g_signal_connect (G_OBJECT (this->widget), event, G_CALLBACK (signal), extObj);
311}
312
313void Widget::disconnectSignal (gulong signalID)
314{
315  g_signal_handler_disconnect (G_OBJECT (this->widget), signalID);
316}
317
318/**
319   \brief Signal that does absolutely nothing
320   \param widget The widget that initiated the Signal
321   \param event The event-type.
322   \param nothing nothin.
323*/
324gint Widget::doNothingSignal (GtkWidget *widget, GdkEvent* event, void* nothing)
325{
326}
327#endif /* HAVE_GTK2 */
328
329//void deleteWidget(Widget* lastWidget)
330
331
332/* PACKERS */
333
334/**
335   \brief Initializes a Packer.
336
337   Sets the down-pinter to NULL and other PackerSpecific-values to their defaults.
338*/
339void Packer::init (void)
340{
341  down = NULL;
342  groupName = NULL;
343
344
345  static_cast<Widget*>(this)->init();
346  return;
347}
348
349/**
350   \brief Destroys a Packer.
351*/
352void Packer::destroy(void)
353{ 
354  PRINTF(4)("deleting the Packer part.\n");
355 
356  if (groupName)
357    delete []groupName;
358
359  //deleting recursively.
360  if (this->down)
361    delete this->down;
362  this->down = NULL;
363
364  static_cast<Widget*>(this)->destroy();
365}
366
367/**
368   \brief Sets the group name under which all the lower widgets of this will be saved.
369   \param name The name of the group.
370*/
371void Packer::setGroupName (char* name)
372{
373  if (groupName)
374    delete groupName;
375  groupName = new char [strlen(name)+1];
376  strcpy(groupName, name);
377}
378
379/**
380   \brief Retrieves the group name under which all the lower widgets of this will be saved.
381   \returns name The name of the group.
382*/
383char* Packer::getGroupName (void)
384{
385  return groupName;
386}
387
388/* CONTAINERS */
389
390/**
391   \brief Initializes a Container.
392
393   sets the Container-Specific defaults.
394*/
395void Container::init (void)
396{
397  isOption = -1;
398
399  static_cast<Packer*>(this)->init();
400
401  return;
402}
403
404/**
405   \brief Destroys a Container.
406*/
407void Container::destroy(void)
408{ 
409  PRINTF(4)("deleting the Container part.\n");
410
411  static_cast<Packer*>(this)->destroy();
412}
413
414/**
415   \briefFills a Container with lowerWidget.
416   \param lowerWidget the Widget that should be filled into the Container.
417
418   It does this by filling up the down pointer only if down points to NULL.
419*/
420void Container::fill (Widget *lowerWidget)
421{
422  if (this->down == NULL)
423    {
424#ifdef HAVE_GTK2
425      gtk_container_add (GTK_CONTAINER (this->widget), lowerWidget->widget);
426#endif /* HAVE_GTK2 */
427      this->down = lowerWidget;
428    }
429  else
430    PRINTF(1)("!!error!! You try to put more than one Widget into a Container. \nNot including this item.\nThis is only possible with Boxes.\n");
431}
432
433// gtk_container_set_border_width (GTK_CONTAINER (widget), 5);
434
435/* WINDOW */
436
437Window* Window::mainWindow = NULL;
438
439/**
440   \brief Adds a new Window Windows to the List of Windows.
441   \param windowToAdd The Windows that should be added to the List
442   \todo this instead of windowToAdd (possibly)
443*/
444void Window::addWindow(Window* windowToAdd)
445{
446  if (!mainWindow)
447    {
448      mainWindow = windowToAdd;
449      return;
450    }
451
452  Widget* tmpWindow = mainWindow;
453  while (tmpWindow->next)
454    tmpWindow = tmpWindow->next;
455  tmpWindow->next = windowToAdd;
456 
457  return;
458}
459     
460
461/**
462   \brief Creating a new Window without a Name
463*/
464Window::Window (void)
465{
466  this->init();
467}
468
469/**
470   \brief Creating a Window with a name
471   \param windowName the name the window should get.
472*/
473
474Window::Window (char* windowName)
475{
476  this->init();
477  this->setTitle (windowName);
478}
479
480/**
481   \brief Destructs a Window.
482*/
483Window::~Window(void)
484{
485  this->destroy();
486}
487
488/**
489   \brief initializes a new Window
490*/
491void Window::init()
492{
493  if (!mainWindow)
494    mainWindow = this;
495 
496  isOpen = false;
497
498  static_cast<Container*>(this)->init();
499
500#ifdef HAVE_GTK2
501  widget = gtk_window_new (GTK_WINDOW_TOPLEVEL);
502  gtk_window_set_policy (GTK_WINDOW(widget), TRUE, TRUE, TRUE);
503#if !defined(__WIN32__)
504  //  gtk_window_set_decorated (GTK_WINDOW (widget), FALSE);
505#endif
506  gtk_container_set_border_width (GTK_CONTAINER (widget), 3);
507#endif /* HAVE_GTK2 */
508}
509
510/**
511   \brief Destroys a Window.
512*/
513void Window::destroy(void)
514{ 
515  if (this->title)
516    PRINTF(3)("deleting the Window: %s\n", this->title);
517  else 
518    PRINTF(3)("deleting the Window.\n");
519
520  this->hide();
521  static_cast<Container*>(this)->destroy();
522 
523}
524
525/**
526   \brief Shows all Widgets that are included within this->widget.
527*/
528void Window::showall ()
529{
530  if (!isOpen)
531    {
532      //      printf ("showall\n");
533#ifdef HAVE_GTK2
534      gtk_widget_show_all  (widget);
535#endif /* HAVE_GTK2 */ 
536     isOpen = true;
537    }
538  else
539    {
540      //      printf ("showone\n");
541#ifdef HAVE_GTK2
542      gtk_widget_show (widget);
543#endif /* HAVE_GTK2 */
544    }
545}
546
547/**
548   \brief Set The Window-title to title
549   \param title title the Window should get.
550*/
551void Window::setTitle (char* title)
552{
553  if (this->title)
554    delete []this->title;
555  this->title = new char[strlen(title)+1];
556  strcpy(this->title, title);
557#ifdef HAVE_GTK2
558  gtk_window_set_title (GTK_WINDOW (widget), title);
559#endif /* HAVE_GTK2 */
560}
561
562/**
563   \brief opens up a Window and fixes the Focus to it
564*/
565void Window::open()
566{
567  if (this != mainWindow)
568    {
569      isOpen = true;
570#ifdef HAVE_GTK2
571      gtk_widget_show_all(widget);
572      gtk_grab_add(widget);
573#endif /* HAVE_GTK2 */
574    }
575}
576
577/**
578   \brief closes up a Window and removes the Focus from it
579*/
580void Window::close()
581{
582  if (this != mainWindow)
583    {
584      isOpen = false;
585#ifdef HAVE_GTK2
586      gtk_grab_remove(widget);
587      gtk_widget_hide (widget);
588#endif /* HAVE_GTK2 */
589    }
590}
591
592#ifdef HAVE_GTK2
593/**
594   \brief opens up a window (not topmost Window).
595   this is the Signal that does it. !!SIGNALS ARE STATIC!!
596   \param widget the widget that did it.
597   \param event the event that did it.
598   \param window the Window that should be opened
599*/
600gint Window::windowOpen (GtkWidget *widget, GdkEvent* event, void* window)
601{
602  static_cast<Window*>(window)->open();
603}
604
605/**
606   \brief closes a window (not topmost Window).
607   this is the Signal that does it. !!SIGNALS ARE STATIC!!
608   \param widget the widget that did it!
609   \param event the event that did it!
610   \param window the Window that should be closed
611*/
612gint Window::windowClose (GtkWidget *widget, GdkEvent* event, void* window)
613{
614  static_cast<Window*>(window)->close();
615}
616
617/**
618 * Quits the orxonox_GUI.
619 * This can be called as a Signal and is therefor static
620 \param widget The widget that called this function
621 \param event the event that happened to execute this function
622 \param data some data passed with the Signal
623 */
624gint Window::orxonox_gui_quit (GtkWidget *widget, GdkEvent *event, gpointer data)
625{
626  if (exec->shouldsave())
627    exec->writeToFile (Window::mainWindow);
628
629  gtk_main_quit();
630  return FALSE;
631}
632#endif /* HAVE_GTK2 */
633
634
635/* FRAME */
636
637/**
638    \brief Creates a new Frame without a name
639*/
640Frame::Frame (void)
641{
642  this->init();
643}
644
645/**
646   \brief Creates a new Frame with name title
647*/
648Frame::Frame (char* title)
649{
650  this->init();
651  this->setTitle(title);
652}
653
654/**
655   \brief destrcucts a Frame
656*/
657Frame::~Frame()
658{
659  this->destroy();
660}
661
662/**
663    \brief Initializes a new Frame with default settings
664*/
665void Frame::init()
666{
667  static_cast<Container*>(this)->init();
668
669#ifdef HAVE_GTK2
670  widget = gtk_frame_new ("");
671  gtk_container_set_border_width (GTK_CONTAINER (widget), 3);
672#endif /* HAVE_GTK2 */
673}
674
675/**
676   \brief Destroys a Frame.
677*/
678void Frame::destroy(void)
679{ 
680  if (this->title)
681    PRINTF(3)("deleting the Frame: %s\n", this->title);
682  else 
683    PRINTF(3)("deleting the Frame.\n");
684
685   static_cast<Container*>(this)->destroy();
686}
687
688/**
689   \brief Sets the Frames name to title
690   \param title The title the Frame should get.
691*/
692void Frame::setTitle (char* title)
693{
694  if (this->title)
695    delete []this->title;
696  this->title = new char[strlen(title)+1];
697  strcpy(this->title, title);
698#ifdef HAVE_GTK2
699  gtk_frame_set_label (GTK_FRAME (widget), title);
700#endif /* HAVE_GTK2 */
701}
702
703// EVENTBOX //
704
705/**
706   \brief Creates a new EventBox with default settings.
707*/
708EventBox::EventBox (void)
709{
710  this->init();
711}
712
713/**
714   \brief Creates a new EventBox with name title
715   \param title title the Eventbox should get (only data-structure-internal)
716*/
717EventBox::EventBox (char* title)
718{
719  this->init();
720  this->setTitle(title);
721}
722
723/**
724   \brief destructs an EventBox.
725*/
726EventBox::~EventBox(void)
727{
728  this->destroy();
729
730}
731
732/**
733   \brief Initializes a new EventBox
734*/
735void EventBox::init(void)
736{
737  isOption = -1;
738
739  static_cast<Container*>(this)->init();
740
741#ifdef HAVE_GTK2
742  widget = gtk_event_box_new ();
743  gtk_container_set_border_width (GTK_CONTAINER (widget), 3);
744#endif /* HAVE_GTK2 */
745}
746
747/**
748   \brief Destroys an EventBox.
749*/
750void EventBox::destroy(void)
751{ 
752  if (this->title)
753    PRINTF(3)("deleting the EventBox: %s\n", this->title);
754  else 
755    PRINTF(3)("deleting the EventBox.\n");
756
757  static_cast<Container*>(this)->destroy();
758}
759
760/**
761   \brief Sets the Title of the EventBox (not implemented)
762   \param title Name the EventBox should get (only datastructure-internal).
763*/
764void EventBox::setTitle (char* title)
765{
766  if (this->title)
767    delete []this->title;
768  this->title = new char[strlen(title)+1];
769  strcpy(this->title, title);
770}
771
772/* BOX */
773
774/**
775   \brief Creates a new horizontal Box
776*/
777Box::Box (void)
778{
779  this->init('h');
780}
781
782/**
783   \brief Creates a new Box of type boxtype
784   \param boxtype if 'v' the Box will be vertically, if 'h' the Box will be horizontally
785*/
786Box::Box (char boxtype)
787{
788  this->init(boxtype);
789}
790
791/**
792   \brief destructs a Box.
793*/
794Box::~Box(void)
795{
796  this->destroy();
797}
798
799/**
800   \brief Initializes a new Box with type boxtype
801   \param boxtype see Box(char boxtype)
802*/
803void Box::init(char boxtype)
804{
805  isOption = -2;
806
807  static_cast<Packer*>(this)->init();
808#ifdef HAVE_GTK2
809  if (boxtype == 'v')
810    {
811      widget = gtk_vbox_new (FALSE, 0);
812    }
813  else
814    {
815      widget = gtk_hbox_new (FALSE, 0);
816    }
817#endif /* HAVE_GTK2 */
818}
819
820/**
821   \brief Destroys a Box.
822*/
823void Box::destroy(void)
824{ 
825  if (this->title)
826    PRINTF(3)("deleting the Box: %s\n", this->title);
827  else 
828    PRINTF(3)("deleting the Box.\n");
829
830  static_cast<Packer*>(this)->destroy();
831}
832
833/**
834    \brief Fills a box with a given Widget.
835    \param lowerWidget the next Widget that should be appendet to this Box
836
837    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
838*/
839void Box::fill (Widget *lowerWidget)
840{
841#ifdef HAVE_GTK2
842  gtk_box_pack_start (GTK_BOX (this->widget), lowerWidget->widget, TRUE, TRUE, 0);
843#endif /* HAVE_GTK2 */
844  if (this->down == NULL)
845    this->down = lowerWidget;
846  else
847    {
848      Widget* tmp;
849      tmp = this->down;
850      while (tmp->next != NULL)
851        {
852          tmp = tmp->next;
853        }
854      tmp->next = lowerWidget;
855    }
856}
857
858/**
859   \brief Sets the Title of a Box.
860   \param title the new Title to set.
861*/
862void Box::setTitle(char* title)
863{
864  if (this->title)
865    delete []this->title;
866  this->title = new char[strlen(title)+1];
867  strcpy(this->title, title);
868}
869
870/* OPTION */
871
872/**
873   \brief Initializes a new Option.
874   sets all Option-Specific-Values to their defaults.
875*/
876void Option::init()
877{
878  value = 0;
879  flagName = NULL;
880  flagNameShort = NULL;
881  saveable = false;
882  defaultValue = 0;
883
884  static_cast<Widget*>(this)->init();
885
886  return;
887}
888
889/**
890   \brief Destroys an Option.
891*/
892void Option::destroy(void)
893{ 
894  PRINTF(4)("deleting the Option Part.\n");
895  if (flagName)
896    delete []flagName;
897  if (flagNameShort)
898    delete []flagNameShort;
899
900  static_cast<Widget*>(this)->destroy();
901}
902
903/**
904   \brief This sets The FlagName of an Option and defines its default Values
905   !! Options will be saved if flagname is different from NULL !!
906   \param flagname the Name that will be displayed in the output
907   \param defaultvalue the default Value for this Option (see definition of defaultvalue
908*/
909void Option::setFlagName (char* flagname, int defaultvalue)
910{
911  if (flagName)
912    delete flagName;
913  flagName = new char [strlen(flagname)+1];
914  strcpy(flagName, flagname);
915  defaultValue = defaultvalue;
916
917  //  cout << "Set Flagname of " << this->title << " to " << flagname << endl;
918}
919
920/**
921    \brief see Option::setFlagName (char* flagname, int defaultvalue)
922    \param flagname the Name that will be displayed in the output
923    \param defaultvalue the default Value for this Option (see definition of defaultvalue
924    \param flagnameshort a short flagname to be displayed in the output
925*/
926void Option::setFlagName (char* flagname, char* flagnameshort,  int defaultvalue)
927{
928  if (flagName)
929    delete flagName;
930  flagName = new char [strlen(flagname)+1];
931  strcpy(flagName, flagname);
932
933  if (flagNameShort)
934    delete flagNameShort;
935  flagNameShort = new char [strlen(flagnameshort)+1];
936  strcpy(flagNameShort, flagnameshort);
937  defaultValue = defaultvalue;
938  //  cout << "Set Flagname of " << this->title << " to " << flagname << endl;
939}
940
941/**
942   \brief Sets the saveable-state of the option to true.
943*/
944void Option::saveability(void)
945{
946  this->saveable = true;
947}
948
949/**
950   \brief Sets the saveable-state of the option.
951   \param isSaveable the saveable-state to set.
952*/
953void Option::saveability(bool isSaveable)
954{
955  this->saveable = isSaveable;
956}
957
958/**
959   \returns The saveable-state.
960*/
961bool Option::isSaveable(void)
962{
963  return this->saveable;
964}
965
966/* BUTTON */
967
968/**
969   \brief Creates a new Button with a buttonname
970   \param buttonname sets the Name of the Button
971*/
972Button::Button(char* buttonname)
973{
974  this->init();
975  this->setTitle(buttonname);
976}
977
978/**
979   \brief destructs a Button.
980*/
981Button::~Button(void)
982{
983  this->destroy();
984}
985
986/**
987   \brief Initializes a new Button
988*/
989void Button::init(void)
990{
991  isOption = 0;
992
993  static_cast<Option*>(this)->init();
994
995#ifdef HAVE_GTK2
996  widget = gtk_button_new_with_label ("");
997#endif /* HAVE_GTK2 */
998}
999
1000/**
1001   \brief Destroys a Button.
1002*/
1003void Button::destroy(void)
1004{ 
1005  if (this->title)
1006    PRINTF(3)("deleting the Label: %s\n", this->title);
1007  else 
1008    PRINTF(3)("deleting the Label.\n");
1009
1010  static_cast<Option*>(this)->destroy();
1011}
1012
1013/**
1014   \brief Sets a new name to the Button
1015   \param title The name the Button should get
1016*/
1017void Button::setTitle (char *title)
1018{
1019  if (this->title)
1020    delete []this->title;
1021  this->title = new char[strlen(title)+1];
1022  strcpy(this->title, title);
1023#ifdef HAVE_GTK2
1024  gtk_button_set_label (GTK_BUTTON(widget), title);
1025#endif /* HAVE_GTK2 */
1026}
1027
1028/**
1029   \brief redraws the Button
1030   not implemented yet
1031*/
1032void Button::redraw ()
1033{
1034}
1035
1036/* CHECKBUTTON */
1037
1038/**
1039   \brief Creates a new CheckButton with an ame
1040   \param buttonname The name the CheckButton should display.
1041*/
1042CheckButton::CheckButton (char* buttonname)
1043{
1044  this->init();
1045  this->setTitle(buttonname);
1046
1047#ifdef HAVE_GTK2
1048  this->connectSignal ("clicked", this->OptionChange);
1049#endif /* HAVE_GTK2 */
1050}
1051
1052/**
1053   \brief destructs a CheckButton.
1054*/
1055CheckButton::~CheckButton(void)
1056{
1057  this->destroy();
1058}
1059
1060/**
1061   \brief Initialize a new CheckButton with default settings
1062*/
1063void CheckButton::init(void)
1064{
1065  isOption = 1;
1066
1067  static_cast<Option*>(this)->init();
1068
1069#ifdef HAVE_GTK2
1070  widget = gtk_check_button_new_with_label ("");
1071#endif /* HAVE_GTK2 */
1072}
1073
1074/**
1075   \brief Destroys a CheckButton.
1076*/
1077void CheckButton::destroy(void)
1078{ 
1079  if (this->title)
1080    PRINTF(3)("deleting the CheckButton: %s\n", this->title);
1081  else 
1082    PRINTF(3)("deleting the CheckButton.\n");
1083
1084  static_cast<Option*>(this)->destroy();
1085}
1086
1087/**
1088   \brief Sets a new Title to a CheckButton
1089   \param title The new Name the CheckButton should display.
1090*/
1091void CheckButton::setTitle(char* title)
1092{
1093  if (this->title)
1094    delete []this->title;
1095  this->title = new char[strlen(title)+1];
1096  strcpy(this->title, title);
1097#ifdef HAVE_GTK2
1098  gtk_button_set_label(GTK_BUTTON(widget), title);
1099#endif /* HAVE_GTK2 */
1100}
1101
1102bool CheckButton::isActive()
1103{
1104#ifdef HAVE_GTK2
1105  return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
1106#endif /* HAVE_GTK2 */
1107}
1108
1109#ifdef HAVE_GTK2
1110/**
1111    \brief Signal OptionChange writes the Value from the CheckButton to its Object-Database.
1112    \param widget The widget(CheckButton) that has a changed Value
1113    \param checkbutton the CheckButton-Object that should receive the change.
1114*/
1115gint CheckButton::OptionChange (GtkWidget *widget, Widget* checkbutton)
1116{
1117  static_cast<CheckButton*>(checkbutton)->value = (int)gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON ((CheckButton*)checkbutton->widget));
1118  flags->setTextFromFlags(orxonoxGUI);   ////// must be different!!!
1119  cout << static_cast<CheckButton*>(checkbutton)->title << " set to: " << static_cast<CheckButton*>(checkbutton)->value << endl;
1120}
1121#endif /* HAVE_GTK2 */
1122
1123/**
1124   \brief Redraws the CheckButton (if option has changed).
1125   Example: if new settings are loaded the Button must be redrawn for the GUI to display that Change
1126*/
1127void CheckButton::redraw ()
1128{
1129#ifdef HAVE_GTK2
1130  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), value);
1131#endif /* HAVE_GTK2 */
1132}
1133
1134/* SLIDER */
1135
1136/**
1137   \brief Creates a new Slider
1138   \param slidername The data-structure-name of the slider.
1139   \param start The minimal Value of the slider.
1140   \param end The maximal Value of the slider.
1141*/
1142Slider::Slider (char* slidername, int start, int end)
1143{
1144  this->init(start, end);
1145  this->setValue(start);
1146  this->setTitle(slidername);
1147#ifdef HAVE_GTK2
1148  this->connectSignal ("value_changed", this->OptionChange);
1149#endif /* HAVE_GTK2 */
1150}
1151
1152/**
1153   \brief destructs a Slider.
1154*/
1155Slider::~Slider(void)
1156{
1157  this->destroy();
1158}
1159
1160/**
1161   \brief Initializes a Slider with start and end Values
1162   params: see Slider::Slider (char* slidername, int start, int end)
1163*/
1164void Slider::init(int start, int end)
1165{
1166  isOption = 2;
1167
1168  static_cast<Option*>(this)->init();
1169
1170#ifdef HAVE_GTK2
1171 widget = gtk_hscale_new_with_range (start, end, 5);
1172#endif /* HAVE_GTK2 */
1173}
1174
1175/**
1176   \brief Destroys a Slider.
1177*/
1178void Slider::destroy(void)
1179{ 
1180  if (this->title)
1181    PRINTF(3)("deleting the Slider: %s\n", this->title);
1182  else 
1183    PRINTF(3)("deleting the Slider.\n");
1184
1185  static_cast<Option*>(this)->destroy();
1186
1187}
1188
1189/**
1190   \brief Sets a new Title to the Slider
1191   \param title The new Name of the slider
1192*/
1193void Slider::setTitle(char* title)
1194{
1195  if (this->title)
1196    delete []this->title;
1197  this->title = new char[strlen(title)+1];
1198  strcpy(this->title, title);
1199}
1200
1201/**
1202   \brief Setting a new value to the Slider.
1203   Maybe you also require a Slider::redraw() for this to display
1204*/
1205void Slider::setValue(int value)
1206{
1207  this->value = value;
1208}
1209
1210/**
1211   \brief Redraws the widget
1212   Example: see void CheckButton::redraw ()
1213*/
1214void Slider::redraw ()
1215{
1216#ifdef HAVE_GTK2
1217  gtk_range_set_value (GTK_RANGE (widget), value);
1218#endif /* HAVE_GTK2 */
1219}
1220
1221#ifdef HAVE_GTK2
1222/**
1223    \brief Signal OptionChange writes the Value from the Slider to its Object-Database.
1224    \param widget The widget(Slider) that has a changed Value
1225    \param slider the Slider-Object that should receive the change.
1226*/
1227gint Slider::OptionChange (GtkWidget *widget, Widget* slider)
1228{
1229  static_cast<Slider*>(slider)->value = (int)gtk_range_get_value (GTK_RANGE ((Slider*)slider->widget));
1230  flags->setTextFromFlags(orxonoxGUI);  //// must be different !!!
1231  cout << static_cast<Slider*>(slider)->title << " set to: "<< static_cast<Slider*>(slider)->value << endl;
1232}
1233#endif /* HAVE_GTK2 */
1234
1235
1236/* MENU */
1237
1238/**
1239    \brief Creates a Menu-Item-list out of multiple input.
1240    !! Consider, that the last input argument has to be "lastItem" for this to work!!
1241    \param menuname The Database-Name of this Menu
1242    \param ... items to be added to this Menu. !! Consider, that the last input argument has to be "lastItem" for this to work!!
1243*/
1244Menu::Menu (char* menuname, ...)
1245{
1246  this->init();
1247  this->setTitle(menuname);
1248   
1249  char *itemName;
1250
1251#ifdef HAVE_GTK2             /////////////////////// REINPLEMENT
1252  va_start (itemlist, menuname);
1253  while (strcmp (itemName = va_arg (itemlist, char*), "lastItem"))
1254    {
1255      this->addItem(itemName);
1256    }
1257  va_end(itemlist);
1258#endif /* HAVE_GTK2 */
1259
1260#ifdef HAVE_GTK2
1261  gtk_option_menu_set_menu (GTK_OPTION_MENU (widget), menu);
1262  this->connectSignal ("changed", this->OptionChange);
1263#endif /* HAVE_GTK2 */
1264}
1265
1266/**
1267   \brief destructs a Menu.
1268*/
1269Menu::~Menu(void)
1270{
1271  this->destroy();
1272}
1273
1274/**
1275   \brief Initializes a new Menu with no items
1276*/
1277void Menu::init(void)
1278{
1279  isOption = 2;
1280
1281  static_cast<Option*>(this)->init();
1282
1283#ifdef HAVE_GTK2
1284  widget = gtk_option_menu_new ();
1285  menu = gtk_menu_new ();
1286#endif /* HAVE_GTK2 */
1287
1288}
1289
1290/**
1291   \brief Destroys a Menu.
1292*/
1293void Menu::destroy(void)
1294{ 
1295  if (this->title)
1296    PRINTF(3)("deleting the Menu: %s\n", this->title);
1297  else 
1298    PRINTF(3)("deleting the Menu.\n");
1299  //! \todo destroy menu
1300 
1301  static_cast<Option*>(this)->destroy();
1302}
1303
1304
1305/**
1306 * Sets the Database-Name of this Menu
1307 \param title Database-Name to be set.
1308*/
1309void Menu::setTitle(char* title)
1310{
1311  if (this->title)
1312    delete []this->title;
1313  this->title = new char[strlen(title)+1];
1314  strcpy(this->title, title);
1315}
1316
1317/**
1318   \brief appends a new Item to the Menu-List.
1319   \param itemName the itemName to be appendet.
1320*/
1321void Menu::addItem (char* itemName)
1322{
1323#ifdef HAVE_GTK2
1324  item = gtk_menu_item_new_with_label (itemName);
1325  gtk_menu_shell_append(GTK_MENU_SHELL (menu), item);
1326#endif /* HAVE_GTK2 */
1327}
1328
1329/**
1330   \brief Redraws the widget
1331   Example: see void CheckButton::redraw ()
1332*/
1333void Menu::redraw ()
1334{
1335#ifdef HAVE_GTK2
1336 gtk_option_menu_set_history (GTK_OPTION_MENU (widget), value);
1337#endif /* HAVE_GTK2 */
1338}
1339
1340#ifdef HAVE_GTK2
1341/**
1342    \brief Signal OptionChange writes the Value from the Menu to its Object-Database.
1343    \param widget The widget(Menu) that has a changed Value
1344    \param menu the Menu-Object that should receive the change.
1345*/
1346gint Menu::OptionChange (GtkWidget *widget, Widget* menu)
1347{
1348  static_cast<Menu*>(menu)->value = (int)gtk_option_menu_get_history (GTK_OPTION_MENU (menu->widget));
1349  flags->setTextFromFlags(orxonoxGUI); //// must be different !!!
1350  cout << static_cast<Menu*>(menu)->title << " changed to : " << static_cast<Menu*>(menu)->value << endl;
1351}
1352#endif /* HAVE_GTK2 */
1353
1354/* OPTION LABEL */
1355
1356/**
1357   \brief Creates a new OptionLabel with a LabelName and a Value.
1358   \param label The name of the OptionLabel.
1359   \param value The Value of the OptionLabel (what will be displayed).
1360*/
1361OptionLabel::OptionLabel(char* label, char* value)
1362{
1363  init();
1364  setTitle(label);
1365  setValue(value);
1366}
1367
1368/**
1369   \brief destructs an OptionLabel.
1370*/
1371OptionLabel::~OptionLabel(void)
1372{
1373  this->destroy();
1374}
1375
1376/**
1377   \brief Initializes an OptionLabel
1378*/
1379void OptionLabel::init(void)
1380{
1381  static_cast<Option*>(this)->init();
1382  isOption = 5;
1383  cValue = NULL;
1384
1385#ifdef HAVE_GTK2
1386  widget = gtk_label_new ("");
1387#endif /* HAVE_GTK2 */
1388}
1389
1390/**
1391   \brief Destroys a OptionLabel.
1392*/
1393void OptionLabel::destroy(void)
1394{ 
1395  if (this->title)
1396    PRINTF(3)("deleting the OptionLabel: %s\n", this->title);
1397  else 
1398    PRINTF(3)("deleting the OptionLabel.\n");
1399  if (cValue)
1400    delete []cValue;
1401
1402  static_cast<Option*>(this)->destroy();
1403}
1404
1405
1406/**
1407   \brief Updates the value of an OptionLabel
1408   \param newValue The new Name that should be displayed.
1409*/
1410void OptionLabel::setValue(char* newValue)
1411{
1412  if (cValue)
1413    delete cValue;
1414  cValue = new char [strlen(newValue)+1];
1415  strcpy(cValue, newValue);
1416#ifdef HAVE_GTK2
1417  gtk_label_set_text (GTK_LABEL (widget), cValue);
1418#endif /* HAVE_GTK2 */
1419}
1420
1421/**
1422   \brief Sets a ned Title to the OptionLabel.
1423   \param title The now title of the OptionLabel.
1424*/
1425void OptionLabel::setTitle(char* title)
1426{
1427  if (this->title)
1428    delete []this->title;
1429  this->title = new char [strlen(title)+1];
1430  strcpy(this->title, title);
1431#ifdef HAVE_GTK2
1432  gtk_label_set_text (GTK_LABEL (widget), title);
1433#endif /* HAVE_GTK2 */
1434}
1435
1436/**
1437   \brief Redraws an OptionLabel (not implemented yet, but it works).
1438*/
1439void OptionLabel::redraw(void)
1440{
1441 
1442}
1443
1444/**
1445   \brief Creates a new default Label with no Text.
1446   You migth consider adding Label::setTitle with this.
1447*/
1448Label::Label ()
1449{
1450  this->init();
1451}
1452
1453/**
1454   \brief Creates a new Label with a Text.
1455   \param text The text to be displayed.
1456*/
1457Label:: Label (char* text)
1458{
1459  this->init();
1460  this->setTitle(text);
1461}
1462
1463/**
1464   \brief destructs a Label.
1465*/
1466Label::~Label(void)
1467{
1468  this->destroy();
1469}
1470
1471/**
1472   \brief initializes a new Label
1473*/
1474void Label::init(void)
1475{
1476  isOption = 0;
1477
1478  static_cast<Widget*>(this)->init();
1479
1480#ifdef HAVE_GTK2
1481  widget = gtk_label_new ("");
1482  gtk_label_set_line_wrap (GTK_LABEL(widget), TRUE);
1483#endif /* HAVE_GTK2 */
1484}
1485
1486/**
1487   \brief Destroys a Label.
1488*/
1489void Label::destroy(void)
1490{ 
1491  if (this->title)
1492    PRINTF(3)("deleting the Label: %s\n", this->title);
1493  else 
1494    PRINTF(3)("deleting the Label.\n");
1495
1496  static_cast<Widget*>(this)->destroy();
1497}
1498
1499/**
1500   \brief Sets a new Text to a Label.
1501   \param text The text to be inserted into the Label.
1502*/
1503void Label::setTitle(char* text)
1504{
1505  if (this->title)
1506    delete []this->title;
1507  this->title = new char[strlen(text)+1];
1508  strcpy(this->title, text);
1509#ifdef HAVE_GTK2
1510  gtk_label_set_text (GTK_LABEL (this->widget), text);
1511#endif /* HAVE_GTK2 */
1512}
1513
1514/**
1515   \brief get the Text of a Label
1516   \return The Text the Label holds.
1517*/
1518char* Label::getText ()
1519{
1520  return this->title;
1521}
1522
1523/**
1524   \brief Creates a new ProgressBar.
1525*/
1526ProgressBar::ProgressBar (void)
1527{
1528  this->init ();
1529}
1530
1531/**
1532   \brief Creates a new ProgressBar.
1533   \param label The name you want to get the ProgressBar.
1534*/
1535ProgressBar::ProgressBar (char* label)
1536{
1537  this->init();
1538  this->setTitle (label);
1539}
1540
1541/**
1542   \brief destructs a ProgressBar
1543*/
1544ProgressBar::~ProgressBar ()
1545{
1546  this->destroy();
1547}
1548
1549/**
1550   \brief Initializes a ProgressBar
1551*/
1552void ProgressBar::init (void)
1553{
1554  isOption = 0;
1555  progress = 0.0;
1556  totalSize = 0.0;
1557
1558  static_cast<Widget*>(this)->init();
1559#ifdef HAVE_GTK2
1560  adjustment = (GtkAdjustment*)gtk_adjustment_new(0, 0, 100, 0, 0, 0);
1561  widget = gtk_progress_bar_new_with_adjustment(adjustment);
1562#endif /* HAVE_GTK2 */
1563}
1564
1565/**
1566   \brief Destroys a ProgressBar.
1567*/
1568void ProgressBar::destroy(void)
1569{ 
1570  if (this->title)
1571    PRINTF(3)("deleting the ProgressBar: %s\n", this->title);
1572  else 
1573    PRINTF(3)("deleting the ProgressBar.\n");
1574
1575  static_cast<Widget*>(this)->destroy();
1576}
1577
1578/**
1579   \brief Sets a ned Title to the ProgressBar.
1580   \param title The now title of the ProgressBar.
1581*/
1582void ProgressBar::setTitle(char* title)
1583{
1584  if (this->title)
1585    delete []this->title;
1586  this->title = new char [strlen(title)+1];
1587  strcpy(this->title, title);
1588}
1589
1590/**
1591   \brief Sets the Total size of the Bar. (ex. The maximum one can download)
1592*/
1593void ProgressBar::setTotalSize (double totalSize)
1594{
1595  this->totalSize = totalSize;
1596}
1597
1598/**
1599   \brief Sets the progress maximum is this->totalSize
1600*/
1601void ProgressBar::setProgress (double progress)
1602{
1603  this->progress = progress;
1604
1605  if (this->progress > this->totalSize)
1606    this->progress = this->totalSize;
1607
1608#ifdef HAVE_GTK2
1609  gtk_progress_set_value(GTK_PROGRESS(widget), this->progress*100.0/this->totalSize);
1610#endif /* HAVE_GTK2 */
1611  PRINTF(3)("Progress: %f\n", progress*100.0/totalSize);
1612}
1613
1614/**
1615    \brief returns the Progress Status
1616*/
1617double ProgressBar::getProgress (void)
1618{
1619  return this->progress;
1620}
1621
1622/* IMAGE */
1623
1624/**
1625   \brief Creates a new Image
1626   \param imagename the location of the Image on the Hard Disc
1627*/
1628Image::Image (char* imagename)
1629{
1630  this->init();
1631  if (this->title)
1632    delete []this->title;
1633  this->title = new char[strlen(imagename)+1];
1634  strcpy(this->title, imagename);
1635
1636#ifdef HAVE_GTK2
1637  widget = gtk_image_new_from_file (imagename);
1638#endif /* HAVE_GTK2 */
1639}
1640
1641/**
1642   \brief destructs an Image.
1643*/
1644Image::~Image(void)
1645{
1646  this->destroy();
1647}
1648
1649/**
1650    \brief Initializes a new Image
1651*/
1652void Image::init()
1653{
1654  isOption = 0;
1655
1656  static_cast<Widget*>(this)->init();
1657}
1658
1659/**
1660   \brief Destroys a Image.
1661*/
1662void Image::destroy(void)
1663{ 
1664  if (this->title)
1665    PRINTF(3)("deleting the Image: %s\n", this->title);
1666  else 
1667    PRINTF(3)("deleting the Image.\n");
1668
1669  static_cast<Widget*>(this)->destroy();
1670}
1671
1672/**
1673   \brief Sets a ned Title to the Image.
1674   \param title The now title of the OptionLabel.
1675*/
1676void Image::setTitle(char* title)
1677{
1678  if (this->title)
1679    delete []this->title;
1680  this->title = new char [strlen(title)+1];
1681  strcpy(this->title, title);
1682}
Note: See TracBrowser for help on using the repository browser.