core/vgui/impl/mfc/vgui_mfc_window.cxx
Go to the documentation of this file.
00001 // This is core/vgui/impl/mfc/vgui_mfc_window.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \brief   See vgui_mfc_window.h for a description of this file.
00008 // \author  Marko Bacic, Oxford RRG
00009 // \date    24 July 2000
00010 //
00011 // \verbatim
00012 //  Modifications
00013 //   20-JUL-2001  K.Y.McGaul  Added init_window function carry out common
00014 //                            functionality from all constructors.
00015 // \endverbatim
00016 
00017 #include "vgui_mfc_window.h"
00018 #include "vgui_mfc_adaptor.h"
00019 #include "vgui_mfc_mainfrm.h"
00020 #include "vgui_mfc.h"
00021 #include "vgui_mfc_utils.h"
00022 #include "vgui_mfc_statusbar.h"
00023 #include <vgui/vgui.h>
00024 #include <vgui/vgui_gl.h>
00025 
00026 static bool first_window = true;
00027 
00028 void vgui_mfc_window::init_window(char const *title,
00029                                   vgui_menu const &menubar,
00030                                   bool has_menu,
00031                                   unsigned width, unsigned height,
00032                                   int posx,
00033                                   int posy)
00034 {
00035   //if (first_window) // First time use the main window
00036   {
00037     // Get a pointer to the single CWinApp object for the application:
00038     CWinApp *app = AfxGetApp();
00039     app->GetMainWnd()->SetWindowText(title);
00040     pos1 = app->GetFirstDocTemplatePosition();
00041     CDocTemplate *tmpl = app->GetNextDocTemplate(pos1);
00042     pos2 = tmpl->GetFirstDocPosition();
00043     CDocument *pdoc = tmpl->GetNextDoc(pos2);
00044     pos3 = pdoc->GetFirstViewPosition();
00045     mfcwin = (vgui_mfc_adaptor *)pdoc->GetNextView(pos3);
00046     mfcwin->set_window( this );
00047     if (has_menu)
00048       set_menubar(menubar);
00049     statusbar = new vgui_mfc_statusbar();
00050     CWnd *main_wnd = app->GetMainWnd();
00051     ((vgui_mfc_mainfrm *)main_wnd)->SetStatusBar(statusbar);
00052     vgui::out.rdbuf(statusbar->statusbuf);
00053     // This part of code changes the window size to the one requested
00054     WINDOWPLACEMENT w_placement;
00055     // Obtain window geometry information
00056     main_wnd->GetWindowPlacement(&w_placement);
00057     // Modify bottom right corner coordinates to account for the width
00058     w_placement.rcNormalPosition.right = w_placement.rcNormalPosition.left+width;
00059     w_placement.rcNormalPosition.bottom = w_placement.rcNormalPosition.top+height;
00060     // Store the geometry information back into window
00061     main_wnd->SetWindowPlacement(&w_placement);
00062     first_window = false;
00063   }
00064 }
00065 
00066 //: Constructor for window without menubar.
00067 vgui_mfc_window::vgui_mfc_window(char const *title,
00068                                  unsigned width, unsigned height,
00069                                  int posx,
00070                                  int posy)
00071   : vgui_window()
00072   , mfcwin(0)
00073   , statusbar(0)
00074 {
00075   init_window(title, vgui_menu(), false, width, height, posx, posy);
00076 }
00077 
00078 //: Constructor for window with menubar.
00079 vgui_mfc_window::vgui_mfc_window(unsigned width, unsigned height,
00080                                  vgui_menu const &menubar,
00081                                  char const *title)
00082   : vgui_window()
00083   , mfcwin(0)
00084   , statusbar(0)
00085 {
00086   init_window(title, menubar, true, width, height, 0, 0);
00087 }
00088 
00089 //: Destructor
00090 vgui_mfc_window::~vgui_mfc_window()
00091 {
00092   glFlush();
00093   /* AKhropov: seems like it's destroyed by MFC itself */
00094   //delete mfcwin;
00095   //mfcwin = 0;
00096   delete statusbar;
00097   statusbar = 0;
00098 }
00099 
00100 //: Set menubar and update main window's actual menu
00101 void vgui_mfc_window::set_menubar(vgui_menu const &m)
00102 {
00103   menubar = m;
00104   vgui_mfc::instance()->utils->set_menu(menubar);
00105 }
00106 
00107 //: Return the vgui_mfc_adaptor associated with this window.
00108 vgui_adaptor* vgui_mfc_window::get_adaptor()
00109 {
00110   return mfcwin;
00111 }
00112 
00113 //: Return the vgui_mfc_statusbar associated with this window.
00114 vgui_statusbar* vgui_mfc_window::get_statusbar()
00115 {
00116   return statusbar;
00117 }
00118 
00119 void vgui_mfc_window::show()
00120 {
00121   CWnd *wnd = AfxGetApp()->GetMainWnd();
00122   wnd->ShowWindow(SW_SHOWNORMAL);
00123 }
00124 
00125 void vgui_mfc_window::hide()
00126 {
00127   CWnd *wnd = AfxGetApp()->GetMainWnd();
00128   wnd->ShowWindow(SW_HIDE);
00129 }
00130 
00131 void vgui_mfc_window::enable_hscrollbar(bool show)
00132 {
00133   CWnd *wnd = AfxGetApp()->GetMainWnd();
00134   wnd->ShowScrollBar(SB_HORZ,show);
00135 }
00136 
00137 void vgui_mfc_window::enable_vscrollbar(bool show)
00138 {
00139   CWnd *wnd = AfxGetApp()->GetMainWnd();
00140   wnd->ShowScrollBar(SB_VERT,show);
00141 }
00142 
00143 void vgui_mfc_window::iconify()
00144 {
00145   CWnd *wnd = AfxGetApp()->GetMainWnd();
00146   wnd->ShowWindow(SW_MINIMIZE);
00147 }
00148 
00149 void vgui_mfc_window::reshape(unsigned w, unsigned h)
00150 {
00151   CWnd *main_wnd = AfxGetApp()->GetMainWnd();
00152   WINDOWPLACEMENT w_placement;
00153   // Obtain window geometry information
00154   main_wnd->GetWindowPlacement(&w_placement);
00155   // Quick hack, needs more research into MFC land
00156   // Modify bottom right position to adapt to new shape
00157   // But, there are margins associated with the scroll bars and
00158   // menu bar, which need to be determined.
00159   //Maybe there is a way to resize the active window and that will take
00160   //care of the other stuff.  JLM 10/3/2002
00161   int kludge_width_margin = 35, kludge_height_margin=100;
00162 
00163   w_placement.rcNormalPosition.right = w_placement.rcNormalPosition.left+w+kludge_width_margin;
00164   w_placement.rcNormalPosition.bottom = w_placement.rcNormalPosition.top+h+kludge_height_margin;
00165   // Store the geometry information back into window
00166   main_wnd->SetWindowPlacement(&w_placement);
00167 }
00168 
00169 void vgui_mfc_window::reposition(int x,int y)
00170 {
00171   CWnd *main_wnd = AfxGetApp()->GetMainWnd();
00172   WINDOWPLACEMENT w_placement;
00173 
00174   // Obtain window geometry information
00175   main_wnd->GetWindowPlacement(&w_placement);
00176   int width = w_placement.rcNormalPosition.right -
00177                w_placement.rcNormalPosition.left;
00178   int height = w_placement.rcNormalPosition.bottom -
00179                w_placement.rcNormalPosition.top;
00180 
00181   // Modify upper left hand corner
00182   w_placement.rcNormalPosition.left=x;
00183   w_placement.rcNormalPosition.top=y;
00184 
00185   // Adjust the lower right accordingly
00186   w_placement.rcNormalPosition.right=x+width;
00187   w_placement.rcNormalPosition.bottom=y+height;
00188 
00189   // Store the geometry information back into window
00190   main_wnd->SetWindowPlacement(&w_placement);
00191 }
00192 
00193 void vgui_mfc_window::set_title(const vcl_string &s)
00194 {
00195   CWinApp *app = AfxGetApp();
00196   app->GetMainWnd()->SetWindowText(s.c_str());
00197 }
00198 
00199 //Set the position of the horizontal scrollbar
00200 int vgui_mfc_window::set_hscrollbar(int pos)
00201 {
00202   CWnd *wnd = AfxGetApp()->GetMainWnd();
00203   return wnd->SetScrollPos(SB_HORZ, pos);
00204 }
00205 
00206 //Set the position of the vertical scrollbar
00207 int vgui_mfc_window::set_vscrollbar(int pos)
00208 {
00209   CWnd *wnd = AfxGetApp()->GetMainWnd();
00210   return wnd->SetScrollPos(SB_VERT, pos);
00211 }