core/vgui/impl/gtk2/vgui_gtk2_window.cxx

Go to the documentation of this file.
00001 // This is core/vgui/impl/gtk2/vgui_gtk2_window.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \author Philip C. Pritchett, RRG, University of Oxford
00008 // \date   18 Dec 99
00009 // \brief  See vgui_gtk2_window.h for a description of this file.
00010 
00011 #include "vgui_gtk2_window.h"
00012 
00013 #include <vgui/vgui.h>
00014 #include <vgui/vgui_menu.h>
00015 
00016 #include <gtk/gtk.h>
00017 
00018 #include "vgui_gtk2_adaptor.h"
00019 #include "vgui_gtk2_utils.h"
00020 #include "vgui_gtk2_statusbar.h"
00021 
00022 static bool debug = false;
00023 
00024 extern "C" {
00025 
00026 // capes@robots. Catch window-manager closing window.
00027 // post_destroy on adaptor and block emission of "destroy" signal so that this window
00028 // is not prematurely destroyed.
00029 static gint delete_event_callback(GtkWidget* w, GdkEvent* e, gpointer data)
00030 {
00031   static_cast<vgui_gtk2_adaptor*>(data)->post_destroy();
00032 
00033   // Don't emit the "destroy" signal. The adaptor will take care of calling
00034   // gtk_widget_destroy on this window after it has disconnected and destroyed itself.
00035   return true;
00036 }
00037 
00038 } // extern "C"
00039 
00040 //--------------------------------------------------------------------------------
00041 //: Constructor
00042 vgui_gtk2_window::vgui_gtk2_window(int w, int h, const char* title)
00043   : use_menubar(false)
00044   , use_statusbar(true)
00045   , adaptor(new vgui_gtk2_adaptor(this))
00046   , statusbar(new vgui_gtk2_statusbar)
00047   , last_menubar(new vgui_menu)
00048 {
00049   if (debug) vcl_cerr << "vgui_gtk2_window::vgui_gtk2_window\n";
00050 
00051   window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
00052   gtk_window_set_title(GTK_WINDOW(window), title);
00053   gtk_window_set_default_size(GTK_WINDOW(window),w,h);
00054 
00055 #ifndef __SGI_CC // SGI's iostream does not allow re-initialising
00056   vgui::out.rdbuf(static_cast<vgui_gtk2_statusbar*>(statusbar)->statusbuf);
00057 #endif
00058 
00059   gtk_signal_connect(GTK_OBJECT(window), "delete_event",
00060                      GTK_SIGNAL_FUNC(delete_event_callback),
00061                      static_cast<vgui_gtk2_adaptor*>(adaptor));
00062 }
00063 
00064 
00065 //--------------------------------------------------------------------------------
00066 //: Constructor
00067 vgui_gtk2_window::vgui_gtk2_window(int w, int h, const vgui_menu& menu, const char* title)
00068   : use_menubar(true)
00069   , use_statusbar(true)
00070   , adaptor(new vgui_gtk2_adaptor(this))
00071   , statusbar(new vgui_gtk2_statusbar)
00072   , last_menubar(new vgui_menu)
00073 {
00074   if (debug) vcl_cerr << "vgui_gtk2_window::vgui_gtk2_window\n";
00075 
00076   window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
00077   gtk_window_set_title(GTK_WINDOW(window), title);
00078   gtk_window_set_default_size(GTK_WINDOW(window),w,h);
00079 
00080   set_menubar(menu);
00081 
00082 #ifndef __SGI_CC // SGI's iostream does not allow re-initialising
00083   vgui::out.rdbuf(static_cast<vgui_gtk2_statusbar*>(statusbar)->statusbuf);
00084 #endif
00085 
00086   gtk_signal_connect(GTK_OBJECT(window), "delete_event",
00087                      GTK_SIGNAL_FUNC(delete_event_callback),
00088                      static_cast<vgui_gtk2_adaptor*>(adaptor));
00089 }
00090 
00091 
00092 //--------------------------------------------------------------------------------
00093 //: Destructor
00094 vgui_gtk2_window::~vgui_gtk2_window()
00095 {
00096   gtk_widget_destroy(window);
00097   delete last_menubar;
00098   delete statusbar;
00099 }
00100 
00101 
00102 //--------------------------------------------------------------------------------
00103 //: Useful initialisation functions
00104 void vgui_gtk2_window::init()
00105 {
00106   box = gtk_vbox_new(FALSE, 0);
00107   gtk_container_add(GTK_CONTAINER (window), box);
00108 
00109   if (use_menubar) {
00110     gtk_box_pack_start(GTK_BOX(box), menubar, FALSE, TRUE, 0);
00111     gtk_widget_show(menubar);
00112   }
00113 
00114   // place glarea inside a frame
00115   GtkWidget *frame = gtk_frame_new(0);
00116   gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_NONE);
00117   gtk_container_set_border_width(GTK_CONTAINER(frame), 2);
00118 
00119   gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 0);
00120   gtk_widget_show(frame);
00121 
00122 
00123   // This re-parents the glarea widget, so the adaptor should yield
00124   // ownership.
00125   GtkWidget *glarea = static_cast<vgui_gtk2_adaptor*>(adaptor)->get_glarea_widget();
00126   gtk_container_add(GTK_CONTAINER(frame), glarea);
00127   gtk_widget_show(glarea);
00128 
00129   if (use_statusbar) {
00130     vgui_gtk2_statusbar* s = static_cast<vgui_gtk2_statusbar*>(statusbar);
00131     s->widget = gtk_statusbar_new();
00132     gtk_box_pack_start(GTK_BOX(box), s->widget, FALSE, TRUE, 0);
00133     gtk_widget_show(s->widget);
00134   }
00135 
00136   gtk_widget_show(box);
00137 }
00138 
00139 
00140 //: Puts the given menubar onto the window.
00141 void vgui_gtk2_window::set_menubar(const vgui_menu &menu)
00142 {
00143   if (debug) vcl_cerr << "vgui_gtk2_window::set_menubar\n";
00144 
00145   use_menubar = true;
00146 
00147   // fsm - assign menu to 'last_menubar' to ensure the commands
00148   // stay in scope for the lifetime of the menubar :
00149   *last_menubar = menu;
00150 
00151   menubar = gtk_menu_bar_new();
00152   if (vgui_gtk2_utils::accel_group == NULL)
00153   {
00154     vgui_gtk2_utils::accel_group = gtk_accel_group_new();
00155     gtk_window_add_accel_group(GTK_WINDOW(window),vgui_gtk2_utils::accel_group);
00156   }
00157   vgui_gtk2_utils::set_menu(menubar, *last_menubar, true);
00158 }
00159 
00160 void vgui_gtk2_window::show()
00161 {
00162   init();
00163 
00164   if (debug) vcl_cerr << "vgui_gtk2_window::show\n";
00165   gtk_widget_show(window);
00166 }
00167 
00168 void vgui_gtk2_window::hide()
00169 {
00170   if (debug) vcl_cerr << "vgui_gtk2_window::hide\n";
00171 }
00172 
00173 void vgui_gtk2_window::reshape(unsigned w, unsigned h)
00174 {
00175   if (debug) vcl_cerr << "vgui_gtk2_window::reshape\n";
00176   gtk_widget_set_size_request(window,w,h);
00177 }
00178 
00179 void vgui_gtk2_window::set_title(vcl_string const &title)
00180 {
00181   gtk_window_set_title(GTK_WINDOW(window), title.c_str());
00182 }

Generated on Mon Mar 8 05:12:25 2010 for core/vgui by  doxygen 1.5.1