Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/updater: walkThrough is better now.

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