Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/updater: deleted threading again. I have found a different Way
Now I am redrawing the GUI every time a ProgressBar is changed.

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