debugging_console_view.cpp

Go to the documentation of this file.
00001 #ifndef DEBUGGING_CONSOLE_VIEW_IMPLEMENTATION_FILE
00002 #define DEBUGGING_CONSOLE_VIEW_IMPLEMENTATION_FILE
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : debugging_console_view                                            *
00007 *  Author : Chris Koeritz                                                     *
00008 *  Credits: Thanks to Andy Tan for initial help with OWL to MFC conversion    *
00009 *                                                                             *
00010 *******************************************************************************
00011 * Copyright (c) 1995-$now By Author.  This program is free software; you can  *
00012 * redistribute it and/or modify it under the terms of the GNU General Public  *
00013 * License as published by the Free Software Foundation; either version 2 of   *
00014 * the License or (at your option) any later version.  This is online at:      *
00015 *     http://www.fsf.org/copyleft/gpl.html                                    *
00016 * Please send any updates to: fred@gruntose.com                               *
00017 \*****************************************************************************/
00018 
00019 #include "debugging_console_view.h"
00020 #include "mfc_extensions.rh"
00021 
00022 #include <basis/convert_utf.h>
00023 #include <basis/istring.h>
00024 #include <data_struct/amorph.cpp>
00025 
00026 DECLARE_BUG_EVENT;
00027   // set the nasty variable for use in the message map.
00028 
00029 BEGIN_MESSAGE_MAP(debugging_console_view, CEditView)
00030   //{{AFX_MSG_MAP(debugging_console_view)
00031   ON_WM_CREATE()
00032   ON_WM_CLOSE()
00033   ON_WM_SIZE()
00034   ON_WM_MOVE()
00035   ON_WM_SETFOCUS()
00036   ON_COMMAND(ID_FILE_PRINT, CEditView::OnFilePrint)
00037   ON_COMMAND(ID_FILE_PRINT_DIRECT, CEditView::OnFilePrint)
00038   ON_COMMAND(ID_FILE_PRINT_PREVIEW, CEditView::OnFilePrintPreview)
00039   //}}AFX_MSG_MAP
00040   ADD_DEBUGGER_MAP(display_debug_message)
00041 END_MESSAGE_MAP()
00042 
00043 IMPLEMENT_DYNCREATE(debugging_console_view, CEditView)
00044 
00045 #undef LOG
00046 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger(), s)
00047 
00049 
00050 const int PRINTING_MARGIN = 2;
00051   // the space we leave blank on each side of printout.
00052 
00054 
00055 class printed_page : public amorph<istring>
00056 {
00057 };
00058 
00059 class printed_lines_list : public amorph<printed_page>
00060 {
00061 };
00062 
00064 
00065 debugging_console_view::debugging_console_view(bool file_logging)
00066 : CEditView(),
00067   debugging_base(GetEditCtrl(), debugging_base::MAX_BUFFER_SIZE,
00068       debugging_base::MAX_FILE_SIZE, file_logging),
00069   _printed(new printed_lines_list),
00070   _curr_page(0),
00071   _char_size_x(0),
00072   _char_size_y(0),
00073   _lines_per_page(0),
00074   _chars_per_line(0),
00075   _x_offset(0),
00076   _x_width(0)
00077 {}
00078 
00079 debugging_console_view::~debugging_console_view()
00080 {
00081   WHACK(_printed);
00082 }
00083 
00084 LRESULT debugging_console_view::display_debug_message(WPARAM wp, LPARAM lp)
00085 {
00086   display_message(wp, lp);
00087   return 0;
00088 }
00089 
00090 void debugging_console_view::OnUpdate(CView *pSender, LPARAM lHint,
00091     CObject *pHint)
00092 {
00093   CEditView::OnUpdate(pSender, lHint, pHint);
00094   CDocument *pDoc = GetDocument();
00095   ASSERT_VALID(pDoc);
00096 }
00097 
00098 BOOL debugging_console_view::Create(LPCTSTR class_name, LPCTSTR title,
00099     DWORD win_style, const RECT &dimensions, CWnd *parent, UINT id,
00100     CCreateContext *context)
00101 {
00102   // create the base class windows.
00103   if (!CEditView::Create(class_name, title, win_style, dimensions, parent,
00104       id, context))
00105     return 0;
00106   debugging_base::setup();
00107 
00108   // sets the limit on the size of the text buffer to be unlimited.
00109   GetEditCtrl().SetLimitText(0);
00110 
00111   // change to a fixed width font in the edit window.
00112   GetEditCtrl().SendMessage(WM_SETFONT, (WPARAM)GetStockObject
00113       (SYSTEM_FIXED_FONT), 0);
00114 
00115   return 1;
00116 }
00117 
00118 void debugging_console_view::hide()
00119 {
00120   ShowWindow(SW_HIDE);
00121 }
00122 
00123 void debugging_console_view::show()
00124 { 
00125   ShowWindow(SW_RESTORE); 
00126   UpdateWindow();
00127 }
00128 
00129 bool debugging_console_view::is_hidden() const
00130 { return !IsWindowVisible(); }
00131 
00132 bool debugging_console_view::is_visible() const
00133 { return !!IsWindowVisible(); }
00134 
00135 int debugging_console_view::OnCreate(LPCREATESTRUCT lpcs)
00136 {
00137   if (CEditView::OnCreate(lpcs) == -1) return -1;
00138 
00139   // set the icons for the window.
00140   HICON debugger_icon = LoadIcon(GET_INSTANCE_HANDLE(),
00141       MAKEINTRESOURCE(IDI_BUG));
00142   SetIcon(debugger_icon, true);  // set big icon.
00143   SetIcon(debugger_icon, false);  // set small icon.
00144 
00145   CDocument *pDoc = GetDocument();
00146   ASSERT_VALID(pDoc);
00147 
00148   return 0;
00149 }
00150 
00151 //consider removing these methods since they're no longer needed.
00152 
00153 void debugging_console_view::OnClose()
00154 {
00155   debugging_base::shut_down();
00156   CEditView::OnClose();
00157 }
00158 
00159 BOOL debugging_console_view::OnPreparePrinting(CPrintInfo *pInfo)
00160 {
00161   FUNCDEF("OnPreparePrinting");
00162   LOG("entry");
00163   pInfo->SetMinPage(1);
00164   if (pInfo->m_bPreview) {
00165     // set the page to be viewed; for real printing we are handed this.
00166     pInfo->m_nCurPage = 1;
00167 LOG(isprintf("*setting* current page to %d.", pInfo->m_nCurPage));
00168   }
00170   BOOL to_return = DoPreparePrinting(pInfo);
00171   LOG("exit");
00172   return to_return;
00173 }
00174 
00175 void debugging_console_view::OnBeginPrinting(CDC *pDC, CPrintInfo *pInfo)
00176 {
00177   FUNCDEF("OnBeginPrinting");
00178   LOG("entry");
00179 
00180   CEditView::OnBeginPrinting(pDC, pInfo);
00181 
00182   _printed->reset();  // clean up existing print state.
00183 
00184 pInfo->m_bContinuePrinting = true;
00185 
00186   // get font info.
00187   int font_height = -((pDC->GetDeviceCaps(LOGPIXELSY) * 10) / 72);
00188 //hmmm: is that 72 lines imagined for the page???
00189   _print_font.CreateFont(font_height, 0, 0, 0, FW_NORMAL, 0, 0, 0,
00190       DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
00191       DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
00192       to_unicode_temp("Courier New"));
00193   CFont *old_font = pDC->SelectObject(&_print_font);
00194     // use our printer font.
00195 
00196   TEXTMETRIC tm;
00197   pDC->GetTextMetrics(&tm);
00198 
00199   CSize size = pDC->GetTextExtent(to_unicode_temp("---------1---------"
00200       "2---------3---------4---------5---------6---------7---------8"), 80);
00201   _char_size_y = tm.tmHeight + tm.tmExternalLeading;
00202     // add in the size between rows.
00203   _char_size_x = size.cx / 80;
00204     // count on the dash and numbers being as big as any other chars.
00205 
00206   _x_offset = (pDC->GetDeviceCaps (HORZRES) - size.cx) / 2;
00207   _x_width = size.cx;
00208 
00209   // calculate how much we can fit on the page.
00210   _chars_per_line = (pDC->GetDeviceCaps(HORZRES) -
00211       (_char_size_x * (2 * PRINTING_MARGIN))) / _char_size_x;
00212   _lines_per_page = (pDC->GetDeviceCaps(VERTRES) -
00213       (_char_size_y * (3 + (2 * PRINTING_MARGIN)))) / _char_size_y;
00214 
00215 LOG(isprintf("chars per line=%d and lines per page=%d", _chars_per_line, _lines_per_page).s());
00216 
00217   pDC->SelectObject(old_font);  // put old font back in.
00218 
00219 
00220 /*
00221   // get the size of the printing region.
00222   int horiz = 0, vert = 0;
00223   CPrintDialog dlg(false);
00224   if (dlg.GetDefaults()) {
00225     CDC dc;
00226     dc.Attach (dlg.GetPrinterDC());
00227     horiz = pDC->GetDeviceCaps(HORZRES);
00228     vert = pDC->GetDeviceCaps(VERTRES);
00229   } else {
00230     MessageBox("No printers are available for this request.");
00231     return;
00232   }
00233 */
00234 
00235   // the following turns the document content into a series of pages.
00236   istring content;
00237   get_contents(content);
00238   istring accumulator;
00239   int line_number = 0;
00240   int current_page = 0;
00241   _printed->append(new printed_page);
00242   for (int i = 0; i < content.length(); i++) {
00243     if ( (content[i] != '\n') && (content[i] != '\r') )
00244       accumulator += content[i];
00245     if ( (content[i] == '\n') || (accumulator.length() >= _chars_per_line) ) {
00246       printed_page *page = _printed->borrow(current_page);
00247       page->append(new istring(accumulator));
00248       line_number++;
00249       accumulator = "";
00250       if (line_number >= _lines_per_page) {
00251         current_page++;
00252         _printed->append(new printed_page);
00253         line_number = 0;
00254       }
00255     }
00256   }
00257   if (accumulator.t()) {
00258     // get the last partial line into the printing.
00259     _printed->borrow(current_page)->append(new istring(accumulator));
00260   }
00261 
00262   pInfo->SetMaxPage(current_page + 1);
00263 LOG(isprintf("largest page is %d", current_page + 1).s());
00264 
00265   LOG("exit");
00266 }
00267 
00268 void debugging_console_view::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
00269 {
00270   ASSERT_VALID(this);
00271   ASSERT_VALID(pDC);
00272   ASSERT(pInfo != NULL);  // overriding OnPaint -- never get this.
00273 
00274   pInfo->m_bContinuePrinting = (int(pInfo->m_nCurPage) <= _printed->elements());
00275 /*
00276   if (pInfo->m_nCurPage > (UINT)m_aPageStart.GetSize() &&
00277       !PaginateTo(pDC, pInfo))
00278         {
00279                 // can't paginate to that page, thus cannot print it.
00280                 pInfo->m_bContinuePrinting = false;
00281         }
00282         ASSERT_VALID(this);
00283 */
00284 }
00285 
00286 void debugging_console_view::OnEndPrinting(CDC *pDC, CPrintInfo *pInfo)
00287 {
00288   FUNCDEF("OnEndPrinting");
00289   LOG("entry");
00290   // reclaim our temporary font.
00291   CEditView::OnEndPrinting(pDC, pInfo);
00292   _print_font.DeleteObject();
00293   LOG("exit");
00294 }
00295 
00296 void debugging_console_view::print_header(CDC* pDC, UINT nPageNumber)
00297 {
00298   CString strHeader = GetDocument ()->GetPathName();
00299   if (strHeader.GetLength () > 68)
00300     strHeader = GetDocument ()->GetTitle();
00301 
00302   CString strPageNumber;
00303 //hmmm: need i18n here.
00304   strPageNumber.Format(to_unicode_temp("Page %d"), nPageNumber);
00305 
00306   UINT nSpaces = 80 - strPageNumber.GetLength () - strHeader.GetLength ();
00307   for (UINT i=0; i<nSpaces; i++)
00308     strHeader += ' ';
00309   strHeader += strPageNumber;
00310 
00311   UINT y = _char_size_y * PRINTING_MARGIN;
00312   CFont *pOldFont = pDC->SelectObject (&_print_font);
00313   pDC->TextOut(_x_offset, y, strHeader);
00314 
00315   y += (_char_size_y * 3) / 2;
00316   pDC->MoveTo(_x_offset, y);
00317   pDC->LineTo(_x_offset + _x_width, y);
00318 
00319   pDC->SelectObject (pOldFont);
00320 }
00321 
00322 void debugging_console_view::OnPrint(CDC *pDC, CPrintInfo *pInfo)
00323 {
00324   FUNCDEF("OnPrint");
00325   LOG("entry");
00326 
00327 LOG(isprintf("num pages is %d", pInfo->GetMaxPage()));
00328 
00329   ASSERT_VALID(pDC);
00330   _curr_page = pInfo->m_nCurPage;
00331   print_header(pDC, _curr_page);
00332 pInfo->m_bContinuePrinting = true;
00333   OnDraw(pDC);
00334 pInfo->m_bContinuePrinting = true;
00335   if (pInfo->m_bPreview) {
00336 //    pInfo->m_nCurPage++;
00337   }
00338 
00339   LOG("exit");
00340 }
00341 
00342 void debugging_console_view::OnDraw(CDC *pDC)
00343 {
00344   FUNCDEF("OnDraw");
00345   LOG("entry");
00346   CFont *pOldFont = pDC->SelectObject (&_print_font);
00347 
00348 LOG(isprintf("current page is %d.", _curr_page).s());
00349   printed_page *page = _printed->borrow(_curr_page - 1);
00350   if (!page) {
00351 LOG("no prepared page existed to show.");
00352     LOG("exit");
00353     pDC->SelectObject (pOldFont);
00354     return;
00355   }
00356   for (int i = 0; i < page->elements(); i++) {
00357     pDC->TextOut(PRINTING_MARGIN * _char_size_x,
00358         (3 + PRINTING_MARGIN) * _char_size_y + (i * _char_size_y),
00359         page->borrow(i)->s());
00360   }
00361 
00362   pDC->SelectObject (pOldFont);
00363   LOG("exit");
00364 }
00365 
00366 
00367 void debugging_console_view::OnSetFocus(CWnd *old_window)
00368 {
00369   CEditView::OnSetFocus(old_window);
00370 }
00371 
00372 void debugging_console_view::OnMove(int x, int y)
00373 {
00374   CEditView::OnMove(x, y);
00375 }
00376 
00377 void debugging_console_view::OnSize(UINT nType, int cx, int cy)
00378 {
00379   CEditView::OnSize(nType, cx, cy);
00380 }
00381 
00382 /*
00383 void debugging_console_view::OnFilePrint()
00384 {
00385   CEditView::OnFilePrint();
00386 }
00387 
00388 void debugging_console_view::OnFilePrintPreview()
00389 {
00390   CEditView::OnFilePrintPreview();
00391 }
00392 */
00393 
00394 
00395 #endif //DEBUGGING_CONSOLE_VIEW_IMPLEMENTATION_FILE
00396 

Generated on Thu Nov 20 04:28:49 2008 for HOOPLE Libraries by  doxygen 1.5.1