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   init();
00063 }
00064 
00065 
00066 //--------------------------------------------------------------------------------
00067 //: Constructor
00068 vgui_gtk2_window::vgui_gtk2_window(int w, int h, const vgui_menu& menu, const char* title)
00069   : use_menubar(true)
00070   , use_statusbar(true)
00071   , adaptor(new vgui_gtk2_adaptor(this))
00072   , statusbar(new vgui_gtk2_statusbar)
00073   , last_menubar(new vgui_menu)
00074 {
00075   if (debug) vcl_cerr << "vgui_gtk2_window::vgui_gtk2_window\n";
00076 
00077   window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
00078   gtk_window_set_title(GTK_WINDOW(window), title);
00079   gtk_window_set_default_size(GTK_WINDOW(window),w,h);
00080 
00081   set_menubar(menu);
00082 
00083 #ifndef __SGI_CC // SGI's iostream does not allow re-initialising
00084   vgui::out.rdbuf(static_cast<vgui_gtk2_statusbar*>(statusbar)->statusbuf);
00085 #endif
00086 
00087   gtk_signal_connect(GTK_OBJECT(window), "delete_event",
00088                      GTK_SIGNAL_FUNC(delete_event_callback),
00089                      static_cast<vgui_gtk2_adaptor*>(adaptor));
00090   init();
00091 }
00092 
00093 
00094 //--------------------------------------------------------------------------------
00095 //: Destructor
00096 vgui_gtk2_window::~vgui_gtk2_window()
00097 {
00098   gtk_widget_destroy(window);
00099   delete last_menubar;
00100   delete statusbar;
00101 }
00102 
00103 
00104 //--------------------------------------------------------------------------------
00105 //: Useful initialisation functions
00106 void vgui_gtk2_window::init()
00107 {
00108   box = gtk_vbox_new(FALSE, 0);
00109   gtk_container_add(GTK_CONTAINER (window), box);
00110 
00111   if (use_menubar) {
00112     gtk_box_pack_start(GTK_BOX(box), menubar, FALSE, TRUE, 0);
00113     gtk_widget_show(menubar);
00114   }
00115 
00116   // place glarea inside a frame
00117   GtkWidget *frame = gtk_frame_new(0);
00118   gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_NONE);
00119   gtk_container_set_border_width(GTK_CONTAINER(frame), 2);
00120 
00121   gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 0);
00122   gtk_widget_show(frame);
00123 
00124 
00125   // This re-parents the glarea widget, so the adaptor should yield
00126   // ownership.
00127   GtkWidget *glarea = static_cast<vgui_gtk2_adaptor*>(adaptor)->get_glarea_widget();
00128   gtk_container_add(GTK_CONTAINER(frame), glarea);
00129   gtk_widget_show(glarea);
00130 
00131   if (use_statusbar) {
00132     vgui_gtk2_statusbar* s = static_cast<vgui_gtk2_statusbar*>(statusbar);
00133     s->widget = gtk_statusbar_new();
00134     gtk_box_pack_start(GTK_BOX(box), s->widget, FALSE, TRUE, 0);
00135     gtk_widget_show(s->widget);
00136   }
00137 
00138   gtk_widget_show(box);
00139 }
00140 
00141 
00142 //: Puts the given menubar onto the window.
00143 void vgui_gtk2_window::set_menubar(const vgui_menu &menu)
00144 {
00145   if (debug) vcl_cerr << "vgui_gtk2_window::set_menubar\n";
00146 
00147   use_menubar = true;
00148 
00149   // fsm - assign menu to 'last_menubar' to ensure the commands
00150   // stay in scope for the lifetime of the menubar :
00151   *last_menubar = menu;
00152 
00153   menubar = gtk_menu_bar_new();
00154   if (vgui_gtk2_utils::accel_group == NULL)
00155   {
00156     vgui_gtk2_utils::accel_group = gtk_accel_group_new();
00157     gtk_window_add_accel_group(GTK_WINDOW(window),vgui_gtk2_utils::accel_group);
00158   }
00159   vgui_gtk2_utils::set_menu(menubar, *last_menubar, true);
00160 }
00161 
00162 void vgui_gtk2_window::show()
00163 {
00164 
00165   if (debug) vcl_cerr << "vgui_gtk2_window::show\n";
00166   gtk_widget_show(window);
00167 }
00168 
00169 void vgui_gtk2_window::hide()
00170 {
00171   if (debug) vcl_cerr << "vgui_gtk2_window::hide\n";
00172 }
00173 
00174 void vgui_gtk2_window::reshape(unsigned w, unsigned h)
00175 {
00176   if (debug) vcl_cerr << "vgui_gtk2_window::reshape\n";
00177   gtk_widget_set_size_request(window,w,h);
00178 }
00179 
00180 void vgui_gtk2_window::set_title(vcl_string const &title)
00181 {
00182   gtk_window_set_title(GTK_WINDOW(window), title.c_str());
00183 }