Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/gui/orxonox_gui_gtk.cc @ 3165

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

orxonox/trunk/gui: compiling as not-GTK possibe, but it is not usable yet

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