Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/updater: threads are cool after all. trying again.

File size: 26.6 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program; if not, write to the Free Software Foundation,
18   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
19
20
21   ### File Specific:
22   main-programmer: Benjamin Grauer
23
24*/
25
26
27#include <iostream>
28
29#include "orxonox_gui_gtk.h"
30
31
32using namespace std;
33
34// temporarily.
35#include "orxonox_gui_flags.h"
36#include "orxonox_gui_exec.h"
37extern Window* orxonoxGUI;
38extern OrxonoxGuiFlags* flags;
39extern OrxonoxGuiExec* exec;
40
41#ifdef HAVE_GTK2
42/**
43   \brief Initializes the Guis GTK-stuff.
44   \param argc the argument count.
45   \param argv The Argument strings.
46*/
47bool initGTK(int argc, char *argv[])
48{
49#ifdef HAVE_GTHREAD
50  PRINTF(3)("Initializing the ThreadSystem of the GUI\n");
51  g_thread_init(NULL);
52  gdk_threads_init();
53#endif /* HAVE_GTHREAD */
54  gtk_init (&argc, &argv);
55  gtk_rc_parse( "rc" );
56}
57
58/**
59   \brief enters the GTK-main-loop
60*/
61bool mainloopGTK(void)
62{
63  gdk_threads_enter();
64  gtk_main();
65  gdk_threads_leave();
66}
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/**
618    \brief Fills a box with a given Widget.
619    \param lowerWidget the next Widget that should be appendet to this Box
620
621    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
622*/
623void Box::fill (Widget *lowerWidget)
624{
625#ifdef HAVE_GTK2
626  gtk_box_pack_start (GTK_BOX (this->widget), lowerWidget->widget, TRUE, TRUE, 0);
627#endif /* HAVE_GTK2 */
628  if (this->down == NULL)
629    this->down = lowerWidget;
630  else
631    {
632      Widget* tmp;
633      tmp = this->down;
634      while (tmp->next != NULL)
635        {
636          tmp = tmp->next;
637        }
638      tmp->next = lowerWidget;
639    }
640}
641
642/* IMAGE */
643
644/**
645   \brief Creates a new Image
646   \param imagename the location of the Image on the Hard Disc
647*/
648Image::Image (char* imagename)
649{
650  this->init();
651  if (label)
652    delete []label;
653  label = new char[strlen(imagename)+1];
654  strcpy(label, imagename);
655
656#ifdef HAVE_GTK2
657  widget = gtk_image_new_from_file (imagename);
658#endif /* HAVE_GTK2 */
659}
660
661/**
662    \brief Initializes a new Image
663*/
664void Image::init()
665{
666  isOption = 0;
667
668  static_cast<Widget*>(this)->init();
669}
670
671
672/* OPTION */
673
674/**
675   \brief Initializes a new Option.
676   sets all Option-Specific-Values to their defaults.
677*/
678void Option::init()
679{
680  value = 0;
681  flagName = NULL;
682  flagNameShort = NULL;
683  saveable = false;
684  defaultValue = 0;
685
686  static_cast<Widget*>(this)->init();
687
688  return;
689}
690
691/**
692   \brief This sets The FlagName of an Option and defines its default Values
693   !! Options will be saved if flagname is different from NULL !!
694   \param flagname the Name that will be displayed in the output
695   \param defaultvalue the default Value for this Option (see definition of defaultvalue
696*/
697void Option::setFlagName (char* flagname, int defaultvalue)
698{
699  if (flagName)
700    delete flagName;
701  flagName = new char [strlen(flagname)+1];
702  strcpy(flagName, flagname);
703  defaultValue = defaultvalue;
704
705  //  cout << "Set Flagname of " << label << " to " << flagname << endl;
706}
707
708/**
709    \brief see Option::setFlagName (char* flagname, int defaultvalue)
710    \param flagname the Name that will be displayed in the output
711    \param defaultvalue the default Value for this Option (see definition of defaultvalue
712    \param flagnameshort a short flagname to be displayed in the output
713*/
714void Option::setFlagName (char* flagname, char* flagnameshort,  int defaultvalue)
715{
716  if (flagName)
717    delete flagName;
718  flagName = new char [strlen(flagname)+1];
719  strcpy(flagName, flagname);
720
721  if (flagNameShort)
722    delete flagNameShort;
723  flagNameShort = new char [strlen(flagnameshort)+1];
724  strcpy(flagNameShort, flagnameshort);
725  defaultValue = defaultvalue;
726  //  cout << "Set Flagname of " << label << " to " << flagname << endl;
727}
728
729
730/* BUTTON */
731
732/**
733   \brief Creates a new Button with a buttonname
734   \param buttonname sets the Name of the Button
735*/
736Button::Button(char* buttonname)
737{
738  this->init();
739  this->setTitle(buttonname);
740}
741
742/**
743   \brief Initializes a new Button
744*/
745void Button::init(void)
746{
747  isOption = 0;
748
749  static_cast<Option*>(this)->init();
750
751#ifdef HAVE_GTK2
752  widget = gtk_button_new_with_label ("");
753#endif /* HAVE_GTK2 */
754}
755
756/**
757   \brief Sets a new name to the Button
758   \param title The name the Button should get
759*/
760void Button::setTitle (char *title)
761{
762  if (label)
763    delete []label;
764  label = new char[strlen(title)+1];
765  strcpy(label, title);
766#ifdef HAVE_GTK2
767  gtk_button_set_label (GTK_BUTTON(widget), title);
768#endif /* HAVE_GTK2 */
769}
770
771/**
772   \brief redraws the Button
773   not implemented yet
774*/
775void Button::redraw ()
776{
777}
778
779/* CHECKBUTTON */
780
781/**
782   \brief Creates a new CheckButton with an ame
783   \param buttonname The name the CheckButton should display.
784*/
785CheckButton::CheckButton (char* buttonname)
786{
787  this->init();
788  this->setTitle(buttonname);
789
790#ifdef HAVE_GTK2
791  this->connectSignal ("clicked", this->OptionChange);
792#endif /* HAVE_GTK2 */
793}
794
795/**
796   \brief Initialize a new CheckButton with default settings
797*/
798void CheckButton::init(void)
799{
800  isOption = 1;
801
802  static_cast<Option*>(this)->init();
803
804#ifdef HAVE_GTK2
805  widget = gtk_check_button_new_with_label ("");
806#endif /* HAVE_GTK2 */
807}
808
809/**
810   \brief Sets a new Title to a CheckButton
811   \param title The new Name the CheckButton should display.
812*/
813void CheckButton::setTitle(char* title)
814{
815  if (label)
816    delete []label;
817  label = new char[strlen(title)+1];
818  strcpy(label, title);
819#ifdef HAVE_GTK2
820  gtk_button_set_label(GTK_BUTTON(widget), title);
821#endif /* HAVE_GTK2 */
822}
823
824bool CheckButton::isActive()
825{
826#ifdef HAVE_GTK2
827  return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
828#endif /* HAVE_GTK2 */
829}
830
831#ifdef HAVE_GTK2
832/**
833    \brief Signal OptionChange writes the Value from the CheckButton to its Object-Database.
834    \param widget The widget(CheckButton) that has a changed Value
835    \param checkbutton the CheckButton-Object that should receive the change.
836*/
837gint CheckButton::OptionChange (GtkWidget *widget, Widget* checkbutton)
838{
839  static_cast<CheckButton*>(checkbutton)->value = (int)gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON ((CheckButton*)checkbutton->widget));
840  flags->setTextFromFlags(orxonoxGUI);   ////// must be different!!!
841  cout << static_cast<CheckButton*>(checkbutton)->label << " set to: " << static_cast<CheckButton*>(checkbutton)->value << endl;
842}
843#endif /* HAVE_GTK2 */
844
845/**
846   \brief Redraws the CheckButton (if option has changed).
847   Example: if new settings are loaded the Button must be redrawn for the GUI to display that Change
848*/
849void CheckButton::redraw ()
850{
851#ifdef HAVE_GTK2
852  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), value);
853#endif /* HAVE_GTK2 */
854}
855
856/* SLIDER */
857
858/**
859   \brief Creates a new Slider
860   \param slidername The data-structure-name of the slider.
861   \param start The minimal Value of the slider.
862   \param end The maximal Value of the slider.
863*/
864Slider::Slider (char* slidername, int start, int end)
865{
866  this->init(start, end);
867  this->setValue(start);
868  this->setTitle(slidername);
869#ifdef HAVE_GTK2
870  this->connectSignal ("value_changed", this->OptionChange);
871#endif /* HAVE_GTK2 */
872}
873
874/**
875   \brief Initializes a Slider with start and end Values
876   params: see Slider::Slider (char* slidername, int start, int end)
877*/
878void Slider::init(int start, int end)
879{
880  isOption = 2;
881
882  static_cast<Option*>(this)->init();
883
884#ifdef HAVE_GTK2
885 widget = gtk_hscale_new_with_range (start, end, 5);
886#endif /* HAVE_GTK2 */
887}
888
889/**
890   \brief Sets a new Title to the Slider
891   \param title The new Name of the slider
892*/
893void Slider::setTitle(char* title)
894{
895  if (label)
896    delete []label;
897  label = new char[strlen(title)+1];
898  strcpy(label, title);
899}
900
901/**
902   \brief Setting a new value to the Slider.
903   Maybe you also require a Slider::redraw() for this to display
904*/
905void Slider::setValue(int value)
906{
907  this->value = value;
908}
909
910#ifdef HAVE_GTK2
911/**
912    \brief Signal OptionChange writes the Value from the Slider to its Object-Database.
913    \param widget The widget(Slider) that has a changed Value
914    \param slider the Slider-Object that should receive the change.
915*/
916gint Slider::OptionChange (GtkWidget *widget, Widget* slider)
917{
918  static_cast<Slider*>(slider)->value = (int)gtk_range_get_value (GTK_RANGE ((Slider*)slider->widget));
919  flags->setTextFromFlags(orxonoxGUI);  //// must be different !!!
920  cout << static_cast<Slider*>(slider)->label << " set to: "<< static_cast<Slider*>(slider)->value << endl;
921}
922#endif /* HAVE_GTK2 */
923
924/**
925   \brief Redraws the widget
926   Example: see void CheckButton::redraw ()
927*/
928void Slider::redraw ()
929{
930#ifdef HAVE_GTK2
931  gtk_range_set_value (GTK_RANGE (widget), value);
932#endif /* HAVE_GTK2 */
933}
934
935/* MENU */
936
937/**
938    \brief Creates a Menu-Item-list out of multiple input.
939    !! Consider, that the last input argument has to be "lastItem" for this to work!!
940    \param menuname The Database-Name of this Menu
941    \param ... items to be added to this Menu. !! Consider, that the last input argument has to be "lastItem" for this to work!!
942*/
943Menu::Menu (char* menuname, ...)
944{
945  this->init();
946  this->setTitle(menuname);
947   
948  char *itemName;
949
950#ifdef HAVE_GTK2             /////////////////////// REINPLEMENT
951  va_start (itemlist, menuname);
952  while (strcmp (itemName = va_arg (itemlist, char*), "lastItem"))
953    {
954      this->addItem(itemName);
955    }
956  va_end(itemlist);
957#endif /* HAVE_GTK2 */
958
959#ifdef HAVE_GTK2
960  gtk_option_menu_set_menu (GTK_OPTION_MENU (widget), menu);
961  this->connectSignal ("changed", this->OptionChange);
962#endif /* HAVE_GTK2 */
963}
964
965/**
966   \brief Initializes a new Menu with no items
967*/
968void Menu::init(void)
969{
970  isOption = 2;
971
972  static_cast<Option*>(this)->init();
973
974#ifdef HAVE_GTK2
975  widget = gtk_option_menu_new ();
976  menu = gtk_menu_new ();
977#endif /* HAVE_GTK2 */
978
979}
980
981/**
982 * Sets the Database-Name of this Menu
983 \param title Database-Name to be set.
984*/
985void Menu::setTitle(char* title)
986{
987  if (label)
988    delete []label;
989  label = new char[strlen(title)+1];
990  strcpy(label, title);
991}
992
993/**
994   \brief appends a new Item to the Menu-List.
995   \param itemName the itemName to be appendet.
996*/
997void Menu::addItem (char* itemName)
998{
999#ifdef HAVE_GTK2
1000  item = gtk_menu_item_new_with_label (itemName);
1001  gtk_menu_shell_append(GTK_MENU_SHELL (menu), item);
1002#endif /* HAVE_GTK2 */
1003}
1004
1005#ifdef HAVE_GTK2
1006/**
1007    \brief Signal OptionChange writes the Value from the Menu to its Object-Database.
1008    \param widget The widget(Menu) that has a changed Value
1009    \param menu the Menu-Object that should receive the change.
1010*/
1011gint Menu::OptionChange (GtkWidget *widget, Widget* menu)
1012{
1013  static_cast<Menu*>(menu)->value = (int)gtk_option_menu_get_history (GTK_OPTION_MENU (menu->widget));
1014  flags->setTextFromFlags(orxonoxGUI); //// must be different !!!
1015  cout << static_cast<Menu*>(menu)->label << " changed to : " << static_cast<Menu*>(menu)->value << endl;
1016}
1017#endif /* HAVE_GTK2 */
1018
1019/**
1020   \brief Redraws the widget
1021   Example: see void CheckButton::redraw ()
1022*/
1023void Menu::redraw ()
1024{
1025#ifdef HAVE_GTK2
1026 gtk_option_menu_set_history (GTK_OPTION_MENU (widget), value);
1027#endif /* HAVE_GTK2 */
1028}
1029
1030/**
1031   \brief Creates a new OptionLabel with a LabelName and a Value.
1032   \param label The name of the OptionLabel.
1033   \param value The Value of the OptionLabel (what will be displayed).
1034*/
1035OptionLabel::OptionLabel(char* label, char* value)
1036{
1037  init();
1038  setTitle(label);
1039  setValue(value);
1040}
1041
1042/**
1043   \brief Initializes an OptionLabel
1044*/
1045void OptionLabel::init(void)
1046{
1047  static_cast<Option*>(this)->init();
1048  isOption = 5;
1049  cValue = NULL;
1050
1051#ifdef HAVE_GTK2
1052  widget = gtk_label_new ("");
1053#endif /* HAVE_GTK2 */
1054}
1055
1056/**
1057   \brief Updates the value of an OptionLabel
1058   \param newValue The new Name that should be displayed.
1059*/
1060void OptionLabel::setValue(char* newValue)
1061{
1062  if (cValue)
1063    delete cValue;
1064  cValue = new char [strlen(newValue)+1];
1065  strcpy(cValue, newValue);
1066#ifdef HAVE_GTK2
1067  gtk_label_set_text (GTK_LABEL (widget), cValue);
1068#endif /* HAVE_GTK2 */
1069}
1070
1071/**
1072   \brief Sets a ned Title to the OptionLabel.
1073   \param title The now title of the OptionLabel.
1074*/
1075void OptionLabel::setTitle(char* title)
1076{
1077  if (label)
1078    delete []label;
1079  label = new char [strlen(title)+1];
1080  strcpy(label, title);
1081#ifdef HAVE_GTK2
1082  gtk_label_set_text (GTK_LABEL (widget), title);
1083#endif /* HAVE_GTK2 */
1084}
1085
1086/**
1087   \brief Redraws an OptionLabel (not implemented yet, but it works).
1088*/
1089void OptionLabel::redraw(void)
1090{
1091 
1092}
1093
1094/**
1095   \brief Creates a new default Label with no Text.
1096   You migth consider adding Label::setTitle with this.
1097*/
1098Label::Label ()
1099{
1100  this->init();
1101}
1102
1103/**
1104   \brief Creates a new Label with a Text.
1105   \param text The text to be displayed.
1106*/
1107Label:: Label (char* text)
1108{
1109  this->init();
1110  this->setText(text);
1111}
1112
1113/**
1114   \brief initializes a new Label
1115*/
1116void Label::init(void)
1117{
1118  isOption = 0;
1119
1120  static_cast<Widget*>(this)->init();
1121
1122#ifdef HAVE_GTK2
1123  widget = gtk_label_new ("");
1124  gtk_label_set_line_wrap (GTK_LABEL(widget), TRUE);
1125#endif /* HAVE_GTK2 */
1126}
1127
1128/**
1129   \brief Sets a new Text to a Label.
1130   \param text The text to be inserted into the Label.
1131*/
1132void Label::setText (char* text)
1133{
1134  if (label)
1135    delete []label;
1136  label = new char[strlen(text)+1];
1137  strcpy(label, text);
1138#ifdef HAVE_GTK2
1139  gtk_label_set_text (GTK_LABEL (this->widget), text);
1140#endif /* HAVE_GTK2 */
1141}
1142
1143/**
1144   \brief get the Text of a Label
1145   \return The Text the Label holds.
1146*/
1147char* Label::getText ()
1148{
1149  return label;
1150}
1151
1152/**
1153   \breif Creates a new ProgressBar.
1154*/
1155ProgressBar::ProgressBar (void)
1156{
1157  this->init ();
1158}
1159
1160/**
1161   \breif Creates a new ProgressBar.
1162   \param label The name you want to get the ProgressBar.
1163*/
1164ProgressBar::ProgressBar (char* label)
1165{
1166  this->init();
1167  //  this->setLabel (label);
1168}
1169
1170/**
1171   \destructs a ProgressBar
1172*/
1173ProgressBar::~ProgressBar ()
1174{
1175}
1176
1177/**
1178   \brief Initializes a ProgressBar
1179*/
1180void ProgressBar::init (void)
1181{
1182  isOption = 0;
1183  progress = 0.0;
1184  totalSize = 0.0;
1185
1186  static_cast<Widget*>(this)->init();
1187#ifdef HAVE_GTK2
1188  adjustment = (GtkAdjustment*)gtk_adjustment_new(0, 0, 100, 0, 0, 0);
1189  widget = gtk_progress_bar_new_with_adjustment(adjustment);
1190#endif /* HAVE_GTK2 */
1191}
1192
1193/**
1194   \brief Sets the Total size of the Bar. (ex. The maximum one can download)
1195*/
1196void ProgressBar::setTotalSize (double totalSize)
1197{
1198  this->totalSize = totalSize;
1199}
1200
1201/**
1202   \brief Sets the progress maximum is this->totalSize
1203*/
1204void ProgressBar::setProgress (double progress)
1205{
1206  this->progress = progress;
1207
1208  if (this->progress > this->totalSize)
1209    this->progress = this->totalSize;
1210
1211#ifdef HAVE_GTK2
1212  gtk_progress_set_value(GTK_PROGRESS(widget), this->progress*100.0/this->totalSize);
1213#endif /* HAVE_GTK2 */
1214  PRINTF(3)("Progress: %f\n", progress*100.0/totalSize);
1215
1216}
1217
1218/**
1219    \brief returns the Progress Status
1220*/
1221double ProgressBar::getProgress (void)
1222{
1223  return this->progress;
1224}
Note: See TracBrowser for help on using the repository browser.