Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/updater: saveable flag is now protected

File size: 27.7 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program; if not, write to the Free Software Foundation,
18   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
19
20
21   ### File Specific:
22   main-programmer: Benjamin Grauer
23
24*/
25
26
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}
67#endif /* HAVE_GTK2 */
68
69/* WIDGET */
70
71/**
72   \brief deletes any given Widget
73   This is still pretty crappy.
74*/
75Widget::~Widget()
76{
77  //  cout << "hiding: " <<this->label <<"\n";
78  this->hide();
79  //  cout << "check if Packer: "<<this->label <<"\n";
80  if (this->isOption < 0)
81    {
82      //  cout << "get Down "<<this->label <<"\n";
83      static_cast<Packer*>(this)->down->~Widget();
84    }
85  //  cout << "next != NULL?: " <<this->label <<"\n";
86  if (this->next != NULL)
87    this->next->~Widget();
88  cout << "delete Widget: " <<this->label <<"\n";
89  //  delete widget;
90}
91
92/**
93   \brief Initializes a widget.
94   Initializes the next Pointer and the other Widget-specific Defaults.
95*/
96void Widget::init()
97{
98  next = NULL;
99  label = NULL;
100  return;
101}
102
103/**
104   \brief makes the widget visible.
105*/
106void Widget::show()
107{
108#ifdef HAVE_GTK2
109  gtk_widget_show (this->widget);
110#endif /* HAVE_GTK2 */
111}
112
113/**
114   \brief hides the widget.
115*/
116void Widget::hide()
117{
118#ifdef HAVE_GTK2
119  gtk_widget_hide (this->widget);
120#endif /* HAVE_GTK2 */
121}
122
123/**
124   \brief Sets the resolution of a specific widget to the given size.
125   \param width the width of the widget to set.
126   \param height the height of the widget to set.
127*/
128void Widget::setSize(int width, int height)
129{
130#ifdef HAVE_GTK2
131  gtk_widget_set_usize (this->widget, width, height);
132#endif /* HAVE_GTK2 */
133}
134
135#ifdef HAVE_GTK2
136/**
137    \brief Connect any signal to any given Sub-widget
138*/
139gulong Widget::connectSignal (char* event, gint (*signal)(GtkWidget*, GdkEvent*, void *))
140{
141  return g_signal_connect (G_OBJECT (this->widget), event, G_CALLBACK (signal), NULL);
142}
143
144/**
145   \brief Connect a signal with additionally passing the whole Object
146*/
147gulong Widget::connectSignal (char* event, gint (*signal)( GtkWidget*, Widget *))
148{
149  return g_signal_connect (G_OBJECT (this->widget), event, G_CALLBACK (signal), this);
150}
151
152/**
153   \brief Connect a signal with additionally passing a whole external Object
154*/
155gulong Widget::connectSignal (char* event, void* extObj, gint (*signal)(GtkWidget*, GdkEvent*, void *))
156{
157  return g_signal_connect (G_OBJECT (this->widget), event, G_CALLBACK (signal), extObj);
158}
159
160/**
161   \brief Connect a signal with additionally passing a whole external Object
162*/
163gulong Widget::connectSignal (char* event, void* extObj, gint (*signal)(GtkWidget*, GdkEventKey*, void *))
164{
165  return g_signal_connect (G_OBJECT (this->widget), event, G_CALLBACK (signal), extObj);
166}
167
168void Widget::disconnectSignal (gulong signalID)
169{
170  g_signal_handler_disconnect (G_OBJECT (this->widget), signalID);
171}
172#endif /* HAVE_GTK2 */
173
174/**
175   \brief Moves through all the Widgets downwards from this and executes the function on them.
176   \param function must be of type void and takes a Widget* as an Input.
177*/
178void Widget::walkThrough (void (*function)(Widget*))
179{
180  function(this);
181  if (this->isOption < 0)
182    {
183      static_cast<Packer*>(this)->down->walkThrough (function);
184    } 
185
186  if (this->next != NULL)
187    this->next->walkThrough(function);
188}
189
190/**
191    \brief This is for listing the option of "widget"
192    \param widget specifies the widget that should be listed
193*/
194void Widget::listOptions (Widget* widget)
195{
196  if (widget->isOption < 0 && static_cast<Packer*>(widget)->groupName)
197    cout << "[" << static_cast<Packer*>(widget)->groupName << "]\n";
198  if (widget->isOption >= 1 && widget->isOption <= 3)
199    cout << "  " << static_cast<Option*>(widget)->label <<" is : " << static_cast<Option*>(widget)->value <<endl;
200  else if (widget->isOption == 5)
201    cout << "  " << static_cast<Option*>(widget)->label <<" is : " << static_cast<OptionLabel*>(widget)->cValue <<endl;
202}
203
204/**
205    \brief This is for setting the option of "widget"
206    \param widget specifies the widget that should be set.
207*/
208void Widget::setOptions (Widget* widget)
209{
210  if (widget->isOption >= 1)
211    static_cast<Option*>(widget)->redraw();// <<" is : " << static_cast<Option*>(this)->value <<endl;
212}
213
214#ifdef HAVE_GTK2
215
216/**
217   \brief Signal that does absolutely nothing
218   \param widget The widget that initiated the Signal
219   \param event The event-type.
220   \param nothing nothin.
221*/
222gint Widget::doNothingSignal (GtkWidget *widget, GdkEvent* event, void* nothing)
223{
224}
225#endif /* HAVE_GTK2 */
226
227//void deleteWidget(Widget* lastWidget)
228
229
230/* PACKERS */
231
232/**
233   \brief Initializes a Packer.
234
235   Sets the down-pinter to NULL and other PackerSpecific-values to their defaults.
236*/
237void Packer::init (void)
238{
239  down = NULL;
240  groupName = NULL;
241
242
243  static_cast<Widget*>(this)->init();
244  return;
245}
246
247/**
248   \brief Sets the group name under which all the lower widgets of this will be saved.
249   \param name The name of the group.
250*/
251void Packer::setGroupName (char* name)
252{
253  if (groupName)
254    delete groupName;
255  groupName = new char [strlen(name)+1];
256  strcpy(groupName, name);
257}
258
259/**
260   \brief Retrieves the group name under which all the lower widgets of this will be saved.
261   \returns name The name of the group.
262*/
263char* Packer::getGroupName (void)
264{
265  return groupName;
266}
267
268/* CONTAINERS */
269
270/**
271   \brief Initializes a Container.
272
273   sets the Container-Specific defaults.
274*/
275void Container::init (void)
276{
277  isOption = -1;
278
279  static_cast<Packer*>(this)->init();
280
281  return;
282}
283
284/**
285   \briefFills a Container with lowerWidget.
286   \param lowerWidget the Widget that should be filled into the Container.
287
288   It does this by filling up the down pointer only if down points to NULL.
289*/
290void Container::fill (Widget *lowerWidget)
291{
292  if (this->down == NULL)
293    {
294#ifdef HAVE_GTK2
295      gtk_container_add (GTK_CONTAINER (this->widget), lowerWidget->widget);
296#endif /* HAVE_GTK2 */
297      this->down = lowerWidget;
298    }
299  else
300    cout << "!!error!! You try to put more than one Widget into a Container. \nNot including this item.\nThis is only possible with Boxes.\n"<<endl;
301}
302
303// gtk_container_set_border_width (GTK_CONTAINER (widget), 5);
304
305/* WINDOW */
306
307Window* Window::mainWindow = NULL;
308
309/**
310   \brief Adds a new Window Windows to the List of Windows.
311   \param windowToAdd The Windows that should be added to the List
312   \todo this instead of windowToAdd (possibly)
313*/
314void Window::addWindow(Window* windowToAdd)
315{
316  if (!mainWindow)
317    {
318      mainWindow = windowToAdd;
319      return;
320    }
321
322  Widget* tmpWindow = mainWindow;
323  while (tmpWindow->next)
324    tmpWindow = tmpWindow->next;
325  tmpWindow->next = windowToAdd;
326 
327  return;
328}
329     
330
331
332/**
333   \brief Creating a new Window without a Name
334*/
335Window::Window (void)
336{
337  this->init();
338}
339
340/**
341   \brief Creating a Window with a name
342   \param windowName the name the window should get.
343*/
344Window::Window (char* windowName)
345{
346  this->init();
347  this->setTitle (windowName);
348}
349
350/**
351   \brief initializes a new Window
352*/
353void Window::init()
354{
355  if (!mainWindow)
356    mainWindow = this;
357 
358  isOpen = false;
359
360  static_cast<Container*>(this)->init();
361
362#ifdef HAVE_GTK2
363  widget = gtk_window_new (GTK_WINDOW_TOPLEVEL);
364  gtk_window_set_policy (GTK_WINDOW(widget), TRUE, TRUE, TRUE);
365#if !defined(__WIN32__)
366  //  gtk_window_set_decorated (GTK_WINDOW (widget), FALSE);
367#endif
368  gtk_container_set_border_width (GTK_CONTAINER (widget), 3);
369#endif /* HAVE_GTK2 */
370}
371
372/**
373   \brief Shows all Widgets that are included within this->widget.
374*/
375void Window::showall ()
376{
377  if (!isOpen)
378    {
379      //      printf ("showall\n");
380#ifdef HAVE_GTK2
381      gtk_widget_show_all  (widget);
382#endif /* HAVE_GTK2 */ 
383     isOpen = true;
384    }
385  else
386    {
387      //      printf ("showone\n");
388#ifdef HAVE_GTK2
389      gtk_widget_show (widget);
390#endif /* HAVE_GTK2 */
391    }
392}
393
394/**
395   \brief Set The Window-title to title
396   \param title title the Window should get.
397*/
398void Window::setTitle (char* title)
399{
400  if (label)
401    delete []label;
402  label = new char[strlen(title)+1];
403  strcpy(label, title);
404#ifdef HAVE_GTK2
405  gtk_window_set_title (GTK_WINDOW (widget), title);
406#endif /* HAVE_GTK2 */
407}
408
409/**
410   \brief opens up a Window and fixes the Focus to it
411*/
412void Window::open()
413{
414  if (this != mainWindow)
415    {
416      isOpen = true;
417#ifdef HAVE_GTK2
418      gtk_widget_show_all(widget);
419      gtk_grab_add(widget);
420#endif /* HAVE_GTK2 */
421    }
422}
423
424/**
425   \brief closes up a Window and removes the Focus from it
426*/
427void Window::close()
428{
429  if (this != mainWindow)
430    {
431      isOpen = false;
432#ifdef HAVE_GTK2
433      gtk_grab_remove(widget);
434      gtk_widget_hide (widget);
435#endif /* HAVE_GTK2 */
436    }
437}
438
439#ifdef HAVE_GTK2
440/**
441   \brief opens up a window (not topmost Window).
442   this is the Signal that does it. !!SIGNALS ARE STATIC!!
443   \param widget the widget that did it.
444   \param event the event that did it.
445   \param window the Window that should be opened
446*/
447gint Window::windowOpen (GtkWidget *widget, GdkEvent* event, void* window)
448{
449  static_cast<Window*>(window)->open();
450}
451
452/**
453   \brief closes a window (not topmost Window).
454   this is the Signal that does it. !!SIGNALS ARE STATIC!!
455   \param widget the widget that did it!
456   \param event the event that did it!
457   \param window the Window that should be closed
458*/
459gint Window::windowClose (GtkWidget *widget, GdkEvent* event, void* window)
460{
461  static_cast<Window*>(window)->close();
462}
463
464/**
465 * Quits the orxonox_GUI.
466 * This can be called as a Signal and is therefor static
467 \param widget The widget that called this function
468 \param event the event that happened to execute this function
469 \param data some data passed with the Signal
470 */
471gint Window::orxonox_gui_quit (GtkWidget *widget, GdkEvent *event, gpointer data)
472{
473  if (exec->shouldsave())
474    exec->writeToFile (Window::mainWindow);
475
476  gtk_main_quit();
477  return FALSE;
478}
479#endif /* HAVE_GTK2 */
480
481
482/* FRAME */
483
484/**
485    \brief Creates a new Frame without a name
486*/
487Frame::Frame (void)
488{
489  this->init();
490}
491
492/**
493   \brief Creates a new Frame with name title
494*/
495Frame::Frame (char* title)
496{
497  this->init();
498  this->setTitle(title);
499}
500
501/**
502    \brief Initializes a new Frame with default settings
503*/
504void Frame::init()
505{
506  static_cast<Container*>(this)->init();
507
508#ifdef HAVE_GTK2
509  widget = gtk_frame_new ("");
510  gtk_container_set_border_width (GTK_CONTAINER (widget), 3);
511#endif /* HAVE_GTK2 */
512}
513
514/**
515   \brief Sets the Frames name to title
516   \param title The title the Frame should get.
517*/
518void Frame::setTitle (char* title)
519{
520  if (label)
521    delete []label;
522  label = new char[strlen(title)+1];
523  strcpy(label, title);
524#ifdef HAVE_GTK2
525  gtk_frame_set_label (GTK_FRAME (widget), title);
526#endif /* HAVE_GTK2 */
527}
528
529// EVENTBOX //
530
531/**
532   \brief Creates a new EventBox with default settings.
533*/
534EventBox::EventBox ()
535{
536  this->init();
537}
538
539/**
540   \brief Creates a new EventBox with name title
541   \param title title the Eventbox should get (only data-structure-internal)
542*/
543EventBox::EventBox (char* title)
544{
545  this->init();
546  this->setTitle(title);
547}
548
549/**
550   \brief Initializes a new EventBox
551*/
552void EventBox::init(void)
553{
554  isOption = -1;
555
556  static_cast<Container*>(this)->init();
557
558#ifdef HAVE_GTK2
559  widget = gtk_event_box_new ();
560  gtk_container_set_border_width (GTK_CONTAINER (widget), 3);
561#endif /* HAVE_GTK2 */
562}
563
564/**
565   \brief Sets the Title of the EventBox (not implemented)
566   \param title Name the EventBox should get (only datastructure-internal).
567*/
568void EventBox::setTitle (char* title)
569{
570  if (label)
571    delete []label;
572  label = new char[strlen(title)+1];
573  strcpy(label, title);
574}
575
576/* BOX */
577
578/**
579   \brief Creates a new horizontal Box
580*/
581Box::Box (void)
582{
583  this->init('h');
584}
585
586/**
587   \brief Creates a new Box of type boxtype
588   \param boxtype if 'v' the Box will be vertically, if 'h' the Box will be horizontally
589*/
590Box::Box (char boxtype)
591{
592  this->init(boxtype);
593}
594
595/**
596   \brief Initializes a new Box with type boxtype
597   \param boxtype see Box(char boxtype)
598*/
599void Box::init(char boxtype)
600{
601  isOption = -2;
602
603  static_cast<Packer*>(this)->init();
604#ifdef HAVE_GTK2
605  if (boxtype == 'v')
606    {
607      widget = gtk_vbox_new (FALSE, 0);
608    }
609  else
610    {
611      widget = gtk_hbox_new (FALSE, 0);
612    }
613#endif /* HAVE_GTK2 */
614}
615
616/**
617    \brief Fills a box with a given Widget.
618    \param lowerWidget the next Widget that should be appendet to this Box
619
620    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
621*/
622void Box::fill (Widget *lowerWidget)
623{
624#ifdef HAVE_GTK2
625  gtk_box_pack_start (GTK_BOX (this->widget), lowerWidget->widget, TRUE, TRUE, 0);
626#endif /* HAVE_GTK2 */
627  if (this->down == NULL)
628    this->down = lowerWidget;
629  else
630    {
631      Widget* tmp;
632      tmp = this->down;
633      while (tmp->next != NULL)
634        {
635          tmp = tmp->next;
636        }
637      tmp->next = lowerWidget;
638    }
639}
640
641/**
642   \brief Sets the Title of a Box.
643   \title the new Title to set.
644*/
645void Box::setTitle(char* title)
646{
647  if (label)
648    delete []label;
649  label = new char[strlen(title)+1];
650  strcpy(label, title);
651}
652
653/* OPTION */
654
655/**
656   \brief Initializes a new Option.
657   sets all Option-Specific-Values to their defaults.
658*/
659void Option::init()
660{
661  value = 0;
662  flagName = NULL;
663  flagNameShort = NULL;
664  saveable = false;
665  defaultValue = 0;
666
667  static_cast<Widget*>(this)->init();
668
669  return;
670}
671
672/**
673   \brief This sets The FlagName of an Option and defines its default Values
674   !! Options will be saved if flagname is different from NULL !!
675   \param flagname the Name that will be displayed in the output
676   \param defaultvalue the default Value for this Option (see definition of defaultvalue
677*/
678void Option::setFlagName (char* flagname, int defaultvalue)
679{
680  if (flagName)
681    delete flagName;
682  flagName = new char [strlen(flagname)+1];
683  strcpy(flagName, flagname);
684  defaultValue = defaultvalue;
685
686  //  cout << "Set Flagname of " << label << " to " << flagname << endl;
687}
688
689/**
690    \brief see Option::setFlagName (char* flagname, int defaultvalue)
691    \param flagname the Name that will be displayed in the output
692    \param defaultvalue the default Value for this Option (see definition of defaultvalue
693    \param flagnameshort a short flagname to be displayed in the output
694*/
695void Option::setFlagName (char* flagname, char* flagnameshort,  int defaultvalue)
696{
697  if (flagName)
698    delete flagName;
699  flagName = new char [strlen(flagname)+1];
700  strcpy(flagName, flagname);
701
702  if (flagNameShort)
703    delete flagNameShort;
704  flagNameShort = new char [strlen(flagnameshort)+1];
705  strcpy(flagNameShort, flagnameshort);
706  defaultValue = defaultvalue;
707  //  cout << "Set Flagname of " << label << " to " << flagname << endl;
708}
709
710/**
711   \brief Sets the saveable-state of the option to true.
712*/
713void Option::saveability(void)
714{
715  this->saveable = true;
716}
717
718/**
719   \brief Sets the saveable-state of the option.
720   \param isSaveable the saveable-state to set.
721*/
722void Option::saveability(bool isSaveable)
723{
724  this->saveable = isSaveable;
725}
726
727/**
728   \returns The saveable-state.
729*/
730bool Option::isSaveable(void)
731{
732  return this->saveable;
733}
734
735/* BUTTON */
736
737/**
738   \brief Creates a new Button with a buttonname
739   \param buttonname sets the Name of the Button
740*/
741Button::Button(char* buttonname)
742{
743  this->init();
744  this->setTitle(buttonname);
745}
746
747/**
748   \brief Initializes a new Button
749*/
750void Button::init(void)
751{
752  isOption = 0;
753
754  static_cast<Option*>(this)->init();
755
756#ifdef HAVE_GTK2
757  widget = gtk_button_new_with_label ("");
758#endif /* HAVE_GTK2 */
759}
760
761/**
762   \brief Sets a new name to the Button
763   \param title The name the Button should get
764*/
765void Button::setTitle (char *title)
766{
767  if (label)
768    delete []label;
769  label = new char[strlen(title)+1];
770  strcpy(label, title);
771#ifdef HAVE_GTK2
772  gtk_button_set_label (GTK_BUTTON(widget), title);
773#endif /* HAVE_GTK2 */
774}
775
776/**
777   \brief redraws the Button
778   not implemented yet
779*/
780void Button::redraw ()
781{
782}
783
784/* CHECKBUTTON */
785
786/**
787   \brief Creates a new CheckButton with an ame
788   \param buttonname The name the CheckButton should display.
789*/
790CheckButton::CheckButton (char* buttonname)
791{
792  this->init();
793  this->setTitle(buttonname);
794
795#ifdef HAVE_GTK2
796  this->connectSignal ("clicked", this->OptionChange);
797#endif /* HAVE_GTK2 */
798}
799
800/**
801   \brief Initialize a new CheckButton with default settings
802*/
803void CheckButton::init(void)
804{
805  isOption = 1;
806
807  static_cast<Option*>(this)->init();
808
809#ifdef HAVE_GTK2
810  widget = gtk_check_button_new_with_label ("");
811#endif /* HAVE_GTK2 */
812}
813
814/**
815   \brief Sets a new Title to a CheckButton
816   \param title The new Name the CheckButton should display.
817*/
818void CheckButton::setTitle(char* title)
819{
820  if (label)
821    delete []label;
822  label = new char[strlen(title)+1];
823  strcpy(label, title);
824#ifdef HAVE_GTK2
825  gtk_button_set_label(GTK_BUTTON(widget), title);
826#endif /* HAVE_GTK2 */
827}
828
829bool CheckButton::isActive()
830{
831#ifdef HAVE_GTK2
832  return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
833#endif /* HAVE_GTK2 */
834}
835
836#ifdef HAVE_GTK2
837/**
838    \brief Signal OptionChange writes the Value from the CheckButton to its Object-Database.
839    \param widget The widget(CheckButton) that has a changed Value
840    \param checkbutton the CheckButton-Object that should receive the change.
841*/
842gint CheckButton::OptionChange (GtkWidget *widget, Widget* checkbutton)
843{
844  static_cast<CheckButton*>(checkbutton)->value = (int)gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON ((CheckButton*)checkbutton->widget));
845  flags->setTextFromFlags(orxonoxGUI);   ////// must be different!!!
846  cout << static_cast<CheckButton*>(checkbutton)->label << " set to: " << static_cast<CheckButton*>(checkbutton)->value << endl;
847}
848#endif /* HAVE_GTK2 */
849
850/**
851   \brief Redraws the CheckButton (if option has changed).
852   Example: if new settings are loaded the Button must be redrawn for the GUI to display that Change
853*/
854void CheckButton::redraw ()
855{
856#ifdef HAVE_GTK2
857  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), value);
858#endif /* HAVE_GTK2 */
859}
860
861/* SLIDER */
862
863/**
864   \brief Creates a new Slider
865   \param slidername The data-structure-name of the slider.
866   \param start The minimal Value of the slider.
867   \param end The maximal Value of the slider.
868*/
869Slider::Slider (char* slidername, int start, int end)
870{
871  this->init(start, end);
872  this->setValue(start);
873  this->setTitle(slidername);
874#ifdef HAVE_GTK2
875  this->connectSignal ("value_changed", this->OptionChange);
876#endif /* HAVE_GTK2 */
877}
878
879/**
880   \brief Initializes a Slider with start and end Values
881   params: see Slider::Slider (char* slidername, int start, int end)
882*/
883void Slider::init(int start, int end)
884{
885  isOption = 2;
886
887  static_cast<Option*>(this)->init();
888
889#ifdef HAVE_GTK2
890 widget = gtk_hscale_new_with_range (start, end, 5);
891#endif /* HAVE_GTK2 */
892}
893
894/**
895   \brief Sets a new Title to the Slider
896   \param title The new Name of the slider
897*/
898void Slider::setTitle(char* title)
899{
900  if (label)
901    delete []label;
902  label = new char[strlen(title)+1];
903  strcpy(label, title);
904}
905
906/**
907   \brief Setting a new value to the Slider.
908   Maybe you also require a Slider::redraw() for this to display
909*/
910void Slider::setValue(int value)
911{
912  this->value = value;
913}
914
915#ifdef HAVE_GTK2
916/**
917    \brief Signal OptionChange writes the Value from the Slider to its Object-Database.
918    \param widget The widget(Slider) that has a changed Value
919    \param slider the Slider-Object that should receive the change.
920*/
921gint Slider::OptionChange (GtkWidget *widget, Widget* slider)
922{
923  static_cast<Slider*>(slider)->value = (int)gtk_range_get_value (GTK_RANGE ((Slider*)slider->widget));
924  flags->setTextFromFlags(orxonoxGUI);  //// must be different !!!
925  cout << static_cast<Slider*>(slider)->label << " set to: "<< static_cast<Slider*>(slider)->value << endl;
926}
927#endif /* HAVE_GTK2 */
928
929/**
930   \brief Redraws the widget
931   Example: see void CheckButton::redraw ()
932*/
933void Slider::redraw ()
934{
935#ifdef HAVE_GTK2
936  gtk_range_set_value (GTK_RANGE (widget), value);
937#endif /* HAVE_GTK2 */
938}
939
940/* MENU */
941
942/**
943    \brief Creates a Menu-Item-list out of multiple input.
944    !! Consider, that the last input argument has to be "lastItem" for this to work!!
945    \param menuname The Database-Name of this Menu
946    \param ... items to be added to this Menu. !! Consider, that the last input argument has to be "lastItem" for this to work!!
947*/
948Menu::Menu (char* menuname, ...)
949{
950  this->init();
951  this->setTitle(menuname);
952   
953  char *itemName;
954
955#ifdef HAVE_GTK2             /////////////////////// REINPLEMENT
956  va_start (itemlist, menuname);
957  while (strcmp (itemName = va_arg (itemlist, char*), "lastItem"))
958    {
959      this->addItem(itemName);
960    }
961  va_end(itemlist);
962#endif /* HAVE_GTK2 */
963
964#ifdef HAVE_GTK2
965  gtk_option_menu_set_menu (GTK_OPTION_MENU (widget), menu);
966  this->connectSignal ("changed", this->OptionChange);
967#endif /* HAVE_GTK2 */
968}
969
970/**
971   \brief Initializes a new Menu with no items
972*/
973void Menu::init(void)
974{
975  isOption = 2;
976
977  static_cast<Option*>(this)->init();
978
979#ifdef HAVE_GTK2
980  widget = gtk_option_menu_new ();
981  menu = gtk_menu_new ();
982#endif /* HAVE_GTK2 */
983
984}
985
986/**
987 * Sets the Database-Name of this Menu
988 \param title Database-Name to be set.
989*/
990void Menu::setTitle(char* title)
991{
992  if (label)
993    delete []label;
994  label = new char[strlen(title)+1];
995  strcpy(label, title);
996}
997
998/**
999   \brief appends a new Item to the Menu-List.
1000   \param itemName the itemName to be appendet.
1001*/
1002void Menu::addItem (char* itemName)
1003{
1004#ifdef HAVE_GTK2
1005  item = gtk_menu_item_new_with_label (itemName);
1006  gtk_menu_shell_append(GTK_MENU_SHELL (menu), item);
1007#endif /* HAVE_GTK2 */
1008}
1009
1010#ifdef HAVE_GTK2
1011/**
1012    \brief Signal OptionChange writes the Value from the Menu to its Object-Database.
1013    \param widget The widget(Menu) that has a changed Value
1014    \param menu the Menu-Object that should receive the change.
1015*/
1016gint Menu::OptionChange (GtkWidget *widget, Widget* menu)
1017{
1018  static_cast<Menu*>(menu)->value = (int)gtk_option_menu_get_history (GTK_OPTION_MENU (menu->widget));
1019  flags->setTextFromFlags(orxonoxGUI); //// must be different !!!
1020  cout << static_cast<Menu*>(menu)->label << " changed to : " << static_cast<Menu*>(menu)->value << endl;
1021}
1022#endif /* HAVE_GTK2 */
1023
1024/**
1025   \brief Redraws the widget
1026   Example: see void CheckButton::redraw ()
1027*/
1028void Menu::redraw ()
1029{
1030#ifdef HAVE_GTK2
1031 gtk_option_menu_set_history (GTK_OPTION_MENU (widget), value);
1032#endif /* HAVE_GTK2 */
1033}
1034
1035/**
1036   \brief Creates a new OptionLabel with a LabelName and a Value.
1037   \param label The name of the OptionLabel.
1038   \param value The Value of the OptionLabel (what will be displayed).
1039*/
1040OptionLabel::OptionLabel(char* label, char* value)
1041{
1042  init();
1043  setTitle(label);
1044  setValue(value);
1045}
1046
1047/**
1048   \brief Initializes an OptionLabel
1049*/
1050void OptionLabel::init(void)
1051{
1052  static_cast<Option*>(this)->init();
1053  isOption = 5;
1054  cValue = NULL;
1055
1056#ifdef HAVE_GTK2
1057  widget = gtk_label_new ("");
1058#endif /* HAVE_GTK2 */
1059}
1060
1061/**
1062   \brief Updates the value of an OptionLabel
1063   \param newValue The new Name that should be displayed.
1064*/
1065void OptionLabel::setValue(char* newValue)
1066{
1067  if (cValue)
1068    delete cValue;
1069  cValue = new char [strlen(newValue)+1];
1070  strcpy(cValue, newValue);
1071#ifdef HAVE_GTK2
1072  gtk_label_set_text (GTK_LABEL (widget), cValue);
1073#endif /* HAVE_GTK2 */
1074}
1075
1076/**
1077   \brief Sets a ned Title to the OptionLabel.
1078   \param title The now title of the OptionLabel.
1079*/
1080void OptionLabel::setTitle(char* title)
1081{
1082  if (label)
1083    delete []label;
1084  label = new char [strlen(title)+1];
1085  strcpy(label, title);
1086#ifdef HAVE_GTK2
1087  gtk_label_set_text (GTK_LABEL (widget), title);
1088#endif /* HAVE_GTK2 */
1089}
1090
1091/**
1092   \brief Redraws an OptionLabel (not implemented yet, but it works).
1093*/
1094void OptionLabel::redraw(void)
1095{
1096 
1097}
1098
1099/**
1100   \brief Creates a new default Label with no Text.
1101   You migth consider adding Label::setTitle with this.
1102*/
1103Label::Label ()
1104{
1105  this->init();
1106}
1107
1108/**
1109   \brief Creates a new Label with a Text.
1110   \param text The text to be displayed.
1111*/
1112Label:: Label (char* text)
1113{
1114  this->init();
1115  this->setTitle(text);
1116}
1117
1118/**
1119   \brief initializes a new Label
1120*/
1121void Label::init(void)
1122{
1123  isOption = 0;
1124
1125  static_cast<Widget*>(this)->init();
1126
1127#ifdef HAVE_GTK2
1128  widget = gtk_label_new ("");
1129  gtk_label_set_line_wrap (GTK_LABEL(widget), TRUE);
1130#endif /* HAVE_GTK2 */
1131}
1132
1133/**
1134   \brief Sets a new Text to a Label.
1135   \param text The text to be inserted into the Label.
1136*/
1137void Label::setTitle(char* text)
1138{
1139  if (label)
1140    delete []label;
1141  label = new char[strlen(text)+1];
1142  strcpy(label, text);
1143#ifdef HAVE_GTK2
1144  gtk_label_set_text (GTK_LABEL (this->widget), text);
1145#endif /* HAVE_GTK2 */
1146}
1147
1148/**
1149   \brief get the Text of a Label
1150   \return The Text the Label holds.
1151*/
1152char* Label::getText ()
1153{
1154  return label;
1155}
1156
1157/**
1158   \brief Creates a new ProgressBar.
1159*/
1160ProgressBar::ProgressBar (void)
1161{
1162  this->init ();
1163}
1164
1165/**
1166   \brief Creates a new ProgressBar.
1167   \param label The name you want to get the ProgressBar.
1168*/
1169ProgressBar::ProgressBar (char* label)
1170{
1171  this->init();
1172  this->setTitle (label);
1173}
1174
1175/**
1176   \brief destructs a ProgressBar
1177*/
1178ProgressBar::~ProgressBar ()
1179{
1180}
1181
1182/**
1183   \brief Initializes a ProgressBar
1184*/
1185void ProgressBar::init (void)
1186{
1187  isOption = 0;
1188  progress = 0.0;
1189  totalSize = 0.0;
1190
1191  static_cast<Widget*>(this)->init();
1192#ifdef HAVE_GTK2
1193  adjustment = (GtkAdjustment*)gtk_adjustment_new(0, 0, 100, 0, 0, 0);
1194  widget = gtk_progress_bar_new_with_adjustment(adjustment);
1195#endif /* HAVE_GTK2 */
1196}
1197
1198/**
1199   \brief Sets a ned Title to the ProgressBar.
1200   \param title The now title of the ProgressBar.
1201*/
1202void ProgressBar::setTitle(char* title)
1203{
1204  if (label)
1205    delete []label;
1206  label = new char [strlen(title)+1];
1207  strcpy(label, title);
1208}
1209
1210/**
1211   \brief Sets the Total size of the Bar. (ex. The maximum one can download)
1212*/
1213void ProgressBar::setTotalSize (double totalSize)
1214{
1215  this->totalSize = totalSize;
1216}
1217
1218/**
1219   \brief Sets the progress maximum is this->totalSize
1220*/
1221void ProgressBar::setProgress (double progress)
1222{
1223  this->progress = progress;
1224
1225  if (this->progress > this->totalSize)
1226    this->progress = this->totalSize;
1227
1228#ifdef HAVE_GTK2
1229  gtk_progress_set_value(GTK_PROGRESS(widget), this->progress*100.0/this->totalSize);
1230#endif /* HAVE_GTK2 */
1231  PRINTF(3)("Progress: %f\n", progress*100.0/totalSize);
1232}
1233
1234/**
1235    \brief returns the Progress Status
1236*/
1237double ProgressBar::getProgress (void)
1238{
1239  return this->progress;
1240}
1241
1242/* IMAGE */
1243
1244/**
1245   \brief Creates a new Image
1246   \param imagename the location of the Image on the Hard Disc
1247*/
1248Image::Image (char* imagename)
1249{
1250  this->init();
1251  if (label)
1252    delete []label;
1253  label = new char[strlen(imagename)+1];
1254  strcpy(label, imagename);
1255
1256#ifdef HAVE_GTK2
1257  widget = gtk_image_new_from_file (imagename);
1258#endif /* HAVE_GTK2 */
1259}
1260
1261/**
1262    \brief Initializes a new Image
1263*/
1264void Image::init()
1265{
1266  isOption = 0;
1267
1268  static_cast<Widget*>(this)->init();
1269}
1270
1271/**
1272   \brief Sets a ned Title to the Image.
1273   \param title The now title of the OptionLabel.
1274*/
1275void Image::setTitle(char* title)
1276{
1277  if (label)
1278    delete []label;
1279  label = new char [strlen(title)+1];
1280  strcpy(label, title);
1281}
Note: See TracBrowser for help on using the repository browser.