Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/updater: updated gui, so now it deletes itself when closing the app.

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