Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/updater: parsing argv's works now. But no events can be handled, like gui-quit, autoupdate and so on.

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