Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/updater: gtk_gui: now setTitle is a virtual function of Widget.

File size: 27.3 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/* BUTTON */
712
713/**
714   \brief Creates a new Button with a buttonname
715   \param buttonname sets the Name of the Button
716*/
717Button::Button(char* buttonname)
718{
719  this->init();
720  this->setTitle(buttonname);
721}
722
723/**
724   \brief Initializes a new Button
725*/
726void Button::init(void)
727{
728  isOption = 0;
729
730  static_cast<Option*>(this)->init();
731
732#ifdef HAVE_GTK2
733  widget = gtk_button_new_with_label ("");
734#endif /* HAVE_GTK2 */
735}
736
737/**
738   \brief Sets a new name to the Button
739   \param title The name the Button should get
740*/
741void Button::setTitle (char *title)
742{
743  if (label)
744    delete []label;
745  label = new char[strlen(title)+1];
746  strcpy(label, title);
747#ifdef HAVE_GTK2
748  gtk_button_set_label (GTK_BUTTON(widget), title);
749#endif /* HAVE_GTK2 */
750}
751
752/**
753   \brief redraws the Button
754   not implemented yet
755*/
756void Button::redraw ()
757{
758}
759
760/* CHECKBUTTON */
761
762/**
763   \brief Creates a new CheckButton with an ame
764   \param buttonname The name the CheckButton should display.
765*/
766CheckButton::CheckButton (char* buttonname)
767{
768  this->init();
769  this->setTitle(buttonname);
770
771#ifdef HAVE_GTK2
772  this->connectSignal ("clicked", this->OptionChange);
773#endif /* HAVE_GTK2 */
774}
775
776/**
777   \brief Initialize a new CheckButton with default settings
778*/
779void CheckButton::init(void)
780{
781  isOption = 1;
782
783  static_cast<Option*>(this)->init();
784
785#ifdef HAVE_GTK2
786  widget = gtk_check_button_new_with_label ("");
787#endif /* HAVE_GTK2 */
788}
789
790/**
791   \brief Sets a new Title to a CheckButton
792   \param title The new Name the CheckButton should display.
793*/
794void CheckButton::setTitle(char* title)
795{
796  if (label)
797    delete []label;
798  label = new char[strlen(title)+1];
799  strcpy(label, title);
800#ifdef HAVE_GTK2
801  gtk_button_set_label(GTK_BUTTON(widget), title);
802#endif /* HAVE_GTK2 */
803}
804
805bool CheckButton::isActive()
806{
807#ifdef HAVE_GTK2
808  return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
809#endif /* HAVE_GTK2 */
810}
811
812#ifdef HAVE_GTK2
813/**
814    \brief Signal OptionChange writes the Value from the CheckButton to its Object-Database.
815    \param widget The widget(CheckButton) that has a changed Value
816    \param checkbutton the CheckButton-Object that should receive the change.
817*/
818gint CheckButton::OptionChange (GtkWidget *widget, Widget* checkbutton)
819{
820  static_cast<CheckButton*>(checkbutton)->value = (int)gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON ((CheckButton*)checkbutton->widget));
821  flags->setTextFromFlags(orxonoxGUI);   ////// must be different!!!
822  cout << static_cast<CheckButton*>(checkbutton)->label << " set to: " << static_cast<CheckButton*>(checkbutton)->value << endl;
823}
824#endif /* HAVE_GTK2 */
825
826/**
827   \brief Redraws the CheckButton (if option has changed).
828   Example: if new settings are loaded the Button must be redrawn for the GUI to display that Change
829*/
830void CheckButton::redraw ()
831{
832#ifdef HAVE_GTK2
833  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), value);
834#endif /* HAVE_GTK2 */
835}
836
837/* SLIDER */
838
839/**
840   \brief Creates a new Slider
841   \param slidername The data-structure-name of the slider.
842   \param start The minimal Value of the slider.
843   \param end The maximal Value of the slider.
844*/
845Slider::Slider (char* slidername, int start, int end)
846{
847  this->init(start, end);
848  this->setValue(start);
849  this->setTitle(slidername);
850#ifdef HAVE_GTK2
851  this->connectSignal ("value_changed", this->OptionChange);
852#endif /* HAVE_GTK2 */
853}
854
855/**
856   \brief Initializes a Slider with start and end Values
857   params: see Slider::Slider (char* slidername, int start, int end)
858*/
859void Slider::init(int start, int end)
860{
861  isOption = 2;
862
863  static_cast<Option*>(this)->init();
864
865#ifdef HAVE_GTK2
866 widget = gtk_hscale_new_with_range (start, end, 5);
867#endif /* HAVE_GTK2 */
868}
869
870/**
871   \brief Sets a new Title to the Slider
872   \param title The new Name of the slider
873*/
874void Slider::setTitle(char* title)
875{
876  if (label)
877    delete []label;
878  label = new char[strlen(title)+1];
879  strcpy(label, title);
880}
881
882/**
883   \brief Setting a new value to the Slider.
884   Maybe you also require a Slider::redraw() for this to display
885*/
886void Slider::setValue(int value)
887{
888  this->value = value;
889}
890
891#ifdef HAVE_GTK2
892/**
893    \brief Signal OptionChange writes the Value from the Slider to its Object-Database.
894    \param widget The widget(Slider) that has a changed Value
895    \param slider the Slider-Object that should receive the change.
896*/
897gint Slider::OptionChange (GtkWidget *widget, Widget* slider)
898{
899  static_cast<Slider*>(slider)->value = (int)gtk_range_get_value (GTK_RANGE ((Slider*)slider->widget));
900  flags->setTextFromFlags(orxonoxGUI);  //// must be different !!!
901  cout << static_cast<Slider*>(slider)->label << " set to: "<< static_cast<Slider*>(slider)->value << endl;
902}
903#endif /* HAVE_GTK2 */
904
905/**
906   \brief Redraws the widget
907   Example: see void CheckButton::redraw ()
908*/
909void Slider::redraw ()
910{
911#ifdef HAVE_GTK2
912  gtk_range_set_value (GTK_RANGE (widget), value);
913#endif /* HAVE_GTK2 */
914}
915
916/* MENU */
917
918/**
919    \brief Creates a Menu-Item-list out of multiple input.
920    !! Consider, that the last input argument has to be "lastItem" for this to work!!
921    \param menuname The Database-Name of this Menu
922    \param ... items to be added to this Menu. !! Consider, that the last input argument has to be "lastItem" for this to work!!
923*/
924Menu::Menu (char* menuname, ...)
925{
926  this->init();
927  this->setTitle(menuname);
928   
929  char *itemName;
930
931#ifdef HAVE_GTK2             /////////////////////// REINPLEMENT
932  va_start (itemlist, menuname);
933  while (strcmp (itemName = va_arg (itemlist, char*), "lastItem"))
934    {
935      this->addItem(itemName);
936    }
937  va_end(itemlist);
938#endif /* HAVE_GTK2 */
939
940#ifdef HAVE_GTK2
941  gtk_option_menu_set_menu (GTK_OPTION_MENU (widget), menu);
942  this->connectSignal ("changed", this->OptionChange);
943#endif /* HAVE_GTK2 */
944}
945
946/**
947   \brief Initializes a new Menu with no items
948*/
949void Menu::init(void)
950{
951  isOption = 2;
952
953  static_cast<Option*>(this)->init();
954
955#ifdef HAVE_GTK2
956  widget = gtk_option_menu_new ();
957  menu = gtk_menu_new ();
958#endif /* HAVE_GTK2 */
959
960}
961
962/**
963 * Sets the Database-Name of this Menu
964 \param title Database-Name to be set.
965*/
966void Menu::setTitle(char* title)
967{
968  if (label)
969    delete []label;
970  label = new char[strlen(title)+1];
971  strcpy(label, title);
972}
973
974/**
975   \brief appends a new Item to the Menu-List.
976   \param itemName the itemName to be appendet.
977*/
978void Menu::addItem (char* itemName)
979{
980#ifdef HAVE_GTK2
981  item = gtk_menu_item_new_with_label (itemName);
982  gtk_menu_shell_append(GTK_MENU_SHELL (menu), item);
983#endif /* HAVE_GTK2 */
984}
985
986#ifdef HAVE_GTK2
987/**
988    \brief Signal OptionChange writes the Value from the Menu to its Object-Database.
989    \param widget The widget(Menu) that has a changed Value
990    \param menu the Menu-Object that should receive the change.
991*/
992gint Menu::OptionChange (GtkWidget *widget, Widget* menu)
993{
994  static_cast<Menu*>(menu)->value = (int)gtk_option_menu_get_history (GTK_OPTION_MENU (menu->widget));
995  flags->setTextFromFlags(orxonoxGUI); //// must be different !!!
996  cout << static_cast<Menu*>(menu)->label << " changed to : " << static_cast<Menu*>(menu)->value << endl;
997}
998#endif /* HAVE_GTK2 */
999
1000/**
1001   \brief Redraws the widget
1002   Example: see void CheckButton::redraw ()
1003*/
1004void Menu::redraw ()
1005{
1006#ifdef HAVE_GTK2
1007 gtk_option_menu_set_history (GTK_OPTION_MENU (widget), value);
1008#endif /* HAVE_GTK2 */
1009}
1010
1011/**
1012   \brief Creates a new OptionLabel with a LabelName and a Value.
1013   \param label The name of the OptionLabel.
1014   \param value The Value of the OptionLabel (what will be displayed).
1015*/
1016OptionLabel::OptionLabel(char* label, char* value)
1017{
1018  init();
1019  setTitle(label);
1020  setValue(value);
1021}
1022
1023/**
1024   \brief Initializes an OptionLabel
1025*/
1026void OptionLabel::init(void)
1027{
1028  static_cast<Option*>(this)->init();
1029  isOption = 5;
1030  cValue = NULL;
1031
1032#ifdef HAVE_GTK2
1033  widget = gtk_label_new ("");
1034#endif /* HAVE_GTK2 */
1035}
1036
1037/**
1038   \brief Updates the value of an OptionLabel
1039   \param newValue The new Name that should be displayed.
1040*/
1041void OptionLabel::setValue(char* newValue)
1042{
1043  if (cValue)
1044    delete cValue;
1045  cValue = new char [strlen(newValue)+1];
1046  strcpy(cValue, newValue);
1047#ifdef HAVE_GTK2
1048  gtk_label_set_text (GTK_LABEL (widget), cValue);
1049#endif /* HAVE_GTK2 */
1050}
1051
1052/**
1053   \brief Sets a ned Title to the OptionLabel.
1054   \param title The now title of the OptionLabel.
1055*/
1056void OptionLabel::setTitle(char* title)
1057{
1058  if (label)
1059    delete []label;
1060  label = new char [strlen(title)+1];
1061  strcpy(label, title);
1062#ifdef HAVE_GTK2
1063  gtk_label_set_text (GTK_LABEL (widget), title);
1064#endif /* HAVE_GTK2 */
1065}
1066
1067/**
1068   \brief Redraws an OptionLabel (not implemented yet, but it works).
1069*/
1070void OptionLabel::redraw(void)
1071{
1072 
1073}
1074
1075/**
1076   \brief Creates a new default Label with no Text.
1077   You migth consider adding Label::setTitle with this.
1078*/
1079Label::Label ()
1080{
1081  this->init();
1082}
1083
1084/**
1085   \brief Creates a new Label with a Text.
1086   \param text The text to be displayed.
1087*/
1088Label:: Label (char* text)
1089{
1090  this->init();
1091  this->setTitle(text);
1092}
1093
1094/**
1095   \brief initializes a new Label
1096*/
1097void Label::init(void)
1098{
1099  isOption = 0;
1100
1101  static_cast<Widget*>(this)->init();
1102
1103#ifdef HAVE_GTK2
1104  widget = gtk_label_new ("");
1105  gtk_label_set_line_wrap (GTK_LABEL(widget), TRUE);
1106#endif /* HAVE_GTK2 */
1107}
1108
1109/**
1110   \brief Sets a new Text to a Label.
1111   \param text The text to be inserted into the Label.
1112*/
1113void Label::setTitle(char* text)
1114{
1115  if (label)
1116    delete []label;
1117  label = new char[strlen(text)+1];
1118  strcpy(label, text);
1119#ifdef HAVE_GTK2
1120  gtk_label_set_text (GTK_LABEL (this->widget), text);
1121#endif /* HAVE_GTK2 */
1122}
1123
1124/**
1125   \brief get the Text of a Label
1126   \return The Text the Label holds.
1127*/
1128char* Label::getText ()
1129{
1130  return label;
1131}
1132
1133/**
1134   \brief Creates a new ProgressBar.
1135*/
1136ProgressBar::ProgressBar (void)
1137{
1138  this->init ();
1139}
1140
1141/**
1142   \brief Creates a new ProgressBar.
1143   \param label The name you want to get the ProgressBar.
1144*/
1145ProgressBar::ProgressBar (char* label)
1146{
1147  this->init();
1148  this->setTitle (label);
1149}
1150
1151/**
1152   \brief destructs a ProgressBar
1153*/
1154ProgressBar::~ProgressBar ()
1155{
1156}
1157
1158/**
1159   \brief Initializes a ProgressBar
1160*/
1161void ProgressBar::init (void)
1162{
1163  isOption = 0;
1164  progress = 0.0;
1165  totalSize = 0.0;
1166
1167  static_cast<Widget*>(this)->init();
1168#ifdef HAVE_GTK2
1169  adjustment = (GtkAdjustment*)gtk_adjustment_new(0, 0, 100, 0, 0, 0);
1170  widget = gtk_progress_bar_new_with_adjustment(adjustment);
1171#endif /* HAVE_GTK2 */
1172}
1173
1174/**
1175   \brief Sets a ned Title to the ProgressBar.
1176   \param title The now title of the ProgressBar.
1177*/
1178void ProgressBar::setTitle(char* title)
1179{
1180  if (label)
1181    delete []label;
1182  label = new char [strlen(title)+1];
1183  strcpy(label, title);
1184}
1185
1186/**
1187   \brief Sets the Total size of the Bar. (ex. The maximum one can download)
1188*/
1189void ProgressBar::setTotalSize (double totalSize)
1190{
1191  this->totalSize = totalSize;
1192}
1193
1194/**
1195   \brief Sets the progress maximum is this->totalSize
1196*/
1197void ProgressBar::setProgress (double progress)
1198{
1199  this->progress = progress;
1200
1201  if (this->progress > this->totalSize)
1202    this->progress = this->totalSize;
1203
1204#ifdef HAVE_GTK2
1205  gtk_progress_set_value(GTK_PROGRESS(widget), this->progress*100.0/this->totalSize);
1206#endif /* HAVE_GTK2 */
1207  PRINTF(3)("Progress: %f\n", progress*100.0/totalSize);
1208}
1209
1210/**
1211    \brief returns the Progress Status
1212*/
1213double ProgressBar::getProgress (void)
1214{
1215  return this->progress;
1216}
1217
1218/* IMAGE */
1219
1220/**
1221   \brief Creates a new Image
1222   \param imagename the location of the Image on the Hard Disc
1223*/
1224Image::Image (char* imagename)
1225{
1226  this->init();
1227  if (label)
1228    delete []label;
1229  label = new char[strlen(imagename)+1];
1230  strcpy(label, imagename);
1231
1232#ifdef HAVE_GTK2
1233  widget = gtk_image_new_from_file (imagename);
1234#endif /* HAVE_GTK2 */
1235}
1236
1237/**
1238    \brief Initializes a new Image
1239*/
1240void Image::init()
1241{
1242  isOption = 0;
1243
1244  static_cast<Widget*>(this)->init();
1245}
1246
1247/**
1248   \brief Sets a ned Title to the Image.
1249   \param title The now title of the OptionLabel.
1250*/
1251void Image::setTitle(char* title)
1252{
1253  if (label)
1254    delete []label;
1255  label = new char [strlen(title)+1];
1256  strcpy(label, title);
1257}
Note: See TracBrowser for help on using the repository browser.