diff --git a/pgadmin/ctl/explainCanvas.cpp b/pgadmin/ctl/explainCanvas.cpp
index f8a533d..666c8ef 100644
--- a/pgadmin/ctl/explainCanvas.cpp
+++ b/pgadmin/ctl/explainCanvas.cpp
@@ -26,7 +26,6 @@ ExplainCanvas::ExplainCanvas(wxWindow *parent)
     SetDiagram(new wxDiagram);
     GetDiagram()->SetCanvas(this);
     SetBackgroundColour(*wxWHITE);
-    lastShape=0;
     popup = new ExplainPopup(this);
 }
 
@@ -39,7 +38,6 @@ ExplainCanvas::~ExplainCanvas()
 void ExplainCanvas::Clear()
 {
     GetDiagram()->DeleteAllShapes();
-    lastShape=0;
 }
 
 
@@ -183,6 +181,37 @@ void ExplainCanvas::ShowPopup(ExplainShape *s)
 }
 
 
+void ExplainCanvas::SaveAsImage(const wxString& fileName)
+{
+    if (GetDiagram()->GetCount() == 0)
+    {
+        wxMessageBox(_("Nothing to be saved!"), _("Save As an image"), wxOK | wxICON_INFORMATION);
+        return;
+    }
+
+    int width = 0, height = 0;
+    GetVirtualSize(&width, &height);
+    
+    /*
+    * Create the bitmap from the Explain window
+    */
+    wxMemoryDC memDC;
+    wxBitmap tempBitmap(width, height);
+    
+    memDC.SelectObject(tempBitmap);
+    memDC.Clear();
+    
+    // Draw the diagram on the bitmap (Memory Device Context)
+    GetDiagram()->Redraw(memDC);
+    
+    memDC.SelectObject(wxNullBitmap);
+
+    if (!tempBitmap.SaveFile(fileName, wxBITMAP_TYPE_BMP))
+    {
+        wxLogError(__("Could not write the file %s: Errcode=%d."), fileName.c_str(), wxSysErrorCode());
+    }
+}
+
 class ExplainText : public wxWindow
 {
 public:
diff --git a/pgadmin/ctl/module.mk b/pgadmin/ctl/module.mk
index ef9976e..ad9b637 100644
--- a/pgadmin/ctl/module.mk
+++ b/pgadmin/ctl/module.mk
@@ -17,11 +17,13 @@ pgadmin3_SOURCES += \
         $(srcdir)/ctl/ctlMenuToolbar.cpp \
         $(srcdir)/ctl/ctlSQLBox.cpp \
         $(srcdir)/ctl/ctlSQLGrid.cpp \
+        $(srcdir)/ctl/queryPrintout.cpp \
         $(srcdir)/ctl/ctlSQLResult.cpp \
         $(srcdir)/ctl/ctlSecurityPanel.cpp \
         $(srcdir)/ctl/ctlTree.cpp \
         $(srcdir)/ctl/explainCanvas.cpp \
         $(srcdir)/ctl/explainShape.cpp \
+        $(srcdir)/ctl/explainPrintout.cpp \
         $(srcdir)/ctl/timespin.cpp \
         $(srcdir)/ctl/xh_calb.cpp \
         $(srcdir)/ctl/xh_ctlcombo.cpp \
diff --git a/pgadmin/frm/frmQuery.cpp b/pgadmin/frm/frmQuery.cpp
index 50979f3..817969d 100644
--- a/pgadmin/frm/frmQuery.cpp
+++ b/pgadmin/frm/frmQuery.cpp
@@ -24,6 +24,14 @@
 #include <wx/aui/aui.h>
 #include <wx/bmpcbox.h>
 
+#if wxUSE_PRINTING_ARCHITECTURE
+#include "wx/print.h"
+#include "wx/printdlg.h"
+#ifdef __WXMAC__
+#include "wx/mac/printdlg.h"
+#endif
+#endif // wxUSE_PRINTING_ARCHITECTURE
+
 // App headers
 #include "frm/frmMain.h"
 #include "frm/frmQuery.h"
@@ -101,6 +109,23 @@ EVT_MENU(MNU_OPEN,              frmQuery::OnOpen)
 EVT_MENU(MNU_SAVE,              frmQuery::OnSave)
 EVT_MENU(MNU_SAVEAS,            frmQuery::OnSaveAs)
 EVT_MENU(MNU_EXPORT,            frmQuery::OnExport)
+#ifdef wxUSE_PRINTING_ARCHITECTURE
+EVT_MENU(MNU_PAGESETUP_EXPLAINGQB, frmQuery::OnPageSetup)
+#ifdef __WXMAC__
+EVT_MENU(MNU_PAGEMARGINS_EXPLAINGQB, frmQuery::OnPageMargins)
+#endif // __WXMAC__
+EVT_MENU(MNU_PRINTPREVIEW_GQB,     frmQuery::OnPrintPreview)
+EVT_MENU(MNU_PRINTPREVIEW_EXPLAIN, frmQuery::OnPrintPreview)
+#ifndef __WXMAC__
+EVT_MENU(MNU_PRINTPREVIEW_QUERY,   frmQuery::OnPrintPreview)
+EVT_MENU(MNU_PAGESETUP_QUERY,      frmQuery::OnPageSetup)
+EVT_MENU(MNU_PRINT_QUERY,          frmQuery::OnPrint)
+#endif
+EVT_MENU(MNU_PRINT_GQB,            frmQuery::OnPrint)
+EVT_MENU(MNU_PRINT_EXPLAIN,        frmQuery::OnPrint)
+#endif //wxUSE_PRINTING_ARCHITECTURE
+EVT_MENU(MNU_SAVEAS_IMAGE_GQB,     frmQuery::SaveExplainAsImage)
+EVT_MENU(MNU_SAVEAS_IMAGE_EXPLAIN, frmQuery::SaveExplainAsImage)
 EVT_MENU(MNU_EXIT,              frmQuery::OnExit)
 EVT_MENU(MNU_CUT,               frmQuery::OnCut)
 EVT_MENU(MNU_COPY,              frmQuery::OnCopy)
@@ -221,6 +246,8 @@ pgsTimer(new pgScriptTimer(this))
     recentKey = wxT("RecentFiles");
     RestorePosition(100, 100, 600, 500, 450, 300);
 
+    explainCanvas = NULL;
+
     // notify wxAUI which frame to use
     manager.SetManagedWindow(this);
     manager.SetFlags(wxAUI_MGR_DEFAULT | wxAUI_MGR_TRANSPARENT_DRAG);
@@ -236,11 +263,52 @@ pgsTimer(new pgScriptTimer(this))
     fileMenu->Append(MNU_NEW, _("&New window\tCtrl-N"), _("Open a new query window"));
     fileMenu->Append(MNU_OPEN, _("&Open...\tCtrl-O"),   _("Open a query file"));
     fileMenu->Append(MNU_SAVE, _("&Save\tCtrl-S"),      _("Save current file"));
-    fileMenu->Append(MNU_SAVEAS, _("Save &as..."),      _("Save file under new name"));
+    saveasImageMenu = new wxMenu();
+    saveasImageMenu->Append(MNU_SAVEAS, _("Query (text)..."), _("Save file under new name"));
+#ifdef wxUSE_PRINTING_ARCHITECTURE
+    saveasImageMenu->Append(MNU_SAVEAS_IMAGE_EXPLAIN, _("&Explain (image)..."), _("Save Explain as an image"));
+    saveasImageMenu->Append(MNU_SAVEAS_IMAGE_GQB,     _("&GQB (image)..."), _("Save Graphical Query Builder (GQB) as an image"));
+#endif
+    fileMenu->Append(wxID_ANY, _("Save as"), saveasImageMenu);
     fileMenu->AppendSeparator();
     fileMenu->Append(MNU_EXPORT, _("&Export..."),  _("Export data to file"));
     fileMenu->Append(MNU_QUICKREPORT, _("&Quick report..."),  _("Run a quick report..."));
     fileMenu->AppendSeparator();
+#ifdef wxUSE_PRINTING_ARCHITECTURE
+    pageSetupMenu = new wxMenu();
+#ifndef __WXMAC__
+    pageSetupMenu->Append(MNU_PAGESETUP_QUERY,      _("Q&uery"),       _("Page setup for the Query Editor"));
+#endif
+    pageSetupMenu->Append(MNU_PAGESETUP_EXPLAINGQB, _("Exp&lain/GQB"), _("Page setup for the Explain & Graphical Query Builder"));
+
+#if __WXMAC__
+    pageMarginsMenu = new wxMenu();
+    pageMarginsMenu->Append(MNU_PAGEMARGINS_EXPLAINGQB, _("Exp&lain/GQB"), _("Page margins for the Explain & Graphical Query Builder"));
+#endif
+
+    printMenu = new wxMenu();
+#ifndef __WXMAC__
+    printMenu->Append(MNU_PRINT_QUERY,    _("&Query"),                   _("Take print out of Query..."));
+#endif
+    printMenu->Append(MNU_PRINT_GQB,      _("&Graphical Query Builder"), _("Take print out of Graphical Query Builder..."));
+    printMenu->Append(MNU_PRINT_EXPLAIN,  _("&Explain"),                 _("Take print out of Explain..."));
+
+    printPreviewMenu = new wxMenu();
+#ifndef __WXMAC__
+    printPreviewMenu->Append(MNU_PRINTPREVIEW_QUERY,   _("&Query"),                   _("Show the print preview for Query Editor..."));
+#endif
+    printPreviewMenu->Append(MNU_PRINTPREVIEW_GQB,     _("&Graphical Query Builder"), _("Show the print preview for Graphical Query Builder..."));
+    printPreviewMenu->Append(MNU_PRINTPREVIEW_EXPLAIN, _("&Explain"),                 _("Show the print preview for Explain..."));
+
+    fileMenu->Append(wxID_ANY, _("Pa&ge Setup"),    pageSetupMenu);
+#if __WXMAC__
+    fileMenu->Append(wxID_ANY, _("Page &Margins"),    pageMarginsMenu);
+#endif
+    fileMenu->Append(wxID_ANY, _("Print Pre&view"), printPreviewMenu);
+    fileMenu->Append(wxID_ANY, _("&Print"),         printMenu);
+    fileMenu->AppendSeparator();
+#endif //wxUSE_PRINTING_ARCHITECTURE
+
     fileMenu->Append(MNU_RECENT, _("&Recent files"), recentFileMenu);
     fileMenu->Append(MNU_EXIT, _("E&xit\tCtrl-W"), _("Exit query window"));
 
@@ -465,7 +533,7 @@ pgsTimer(new pgScriptTimer(this))
     // Graphical Canvas
     // initialize values
     model=new gqbModel();
-    controller = new gqbController(model,sqlNotebook, outputPane, wxSize(1280,800));
+    controller = new gqbController(model,sqlNotebook, outputPane, wxSize(GQB_MIN_WIDTH, GQB_MIN_HEIGHT));
     firstTime=true;                               // Inform to GQB that the tree of table haven't filled.
     gqbUpdateRunning = false;                      // Are we already updating the SQL query - event recursion protection.
     adjustSizesTimer=NULL;                        // Timer used to avoid a bug when close outputPane
@@ -1318,6 +1386,8 @@ void frmQuery::updateMenu(wxObject *obj)
     bool canClear=false;
     bool canFind=false;
     bool canAddFavourite=false;
+    bool canSaveExplain=false;
+    bool canSaveGQB=false;
 
     wxAuiFloatingFrame *fp = wxDynamicCastThis(wxAuiFloatingFrame);
     if (fp)
@@ -1345,6 +1415,9 @@ void frmQuery::updateMenu(wxObject *obj)
         canClear = true;
     }
 
+    canSaveExplain = explainCanvas->GetDiagram()->GetCount() > 0;
+    canSaveGQB = controller->getView()->canSaveAsImage();
+
     toolBar->EnableTool(MNU_UNDO, canUndo);
     editMenu->Enable(MNU_UNDO, canUndo);
 
@@ -1364,6 +1437,32 @@ void frmQuery::updateMenu(wxObject *obj)
     editMenu->Enable(MNU_FIND, canFind);
 
     favouritesMenu->Enable(MNU_FAVOURITES_ADD, canAddFavourite);
+
+#ifdef wxUSE_PRINTING_ARCHITECTURE
+    saveasImageMenu->Enable(MNU_SAVEAS_IMAGE_EXPLAIN, canSaveExplain);
+    saveasImageMenu->Enable(MNU_SAVEAS_IMAGE_GQB, canSaveGQB);
+
+#ifndef __WXMAC__
+    pageSetupMenu->Enable(MNU_PAGESETUP_QUERY, canAddFavourite);
+#endif
+    pageSetupMenu->Enable(MNU_PAGESETUP_EXPLAINGQB, canSaveExplain || canSaveGQB);
+
+#if __WXMAC__
+    pageMarginsMenu->Enable(MNU_PAGEMARGINS_EXPLAINGQB, canSaveExplain || canSaveGQB);
+#endif
+
+#ifndef __WXMAC__
+    printMenu->Enable(MNU_PRINT_QUERY, canAddFavourite);
+#endif
+    printMenu->Enable(MNU_PRINT_GQB, canSaveGQB);
+    printMenu->Enable(MNU_PRINT_EXPLAIN, canSaveExplain);
+
+#ifndef __WXMAC__
+    printPreviewMenu->Enable(MNU_PRINTPREVIEW_QUERY, canAddFavourite);
+#endif
+    printPreviewMenu->Enable(MNU_PRINTPREVIEW_GQB, canSaveGQB);
+    printPreviewMenu->Enable(MNU_PRINTPREVIEW_EXPLAIN, canSaveExplain);
+#endif //wxUSE_PRINTING_ARCHITECTURE
 }
 
 
@@ -1951,6 +2050,7 @@ bool frmQuery::updateFromGqb(bool executing)
     // Make sure this doesn't get call recursively through an event
     if (gqbUpdateRunning)
         return false;
+    updateMenu();
 
     gqbUpdateRunning = true;
 
@@ -3018,6 +3118,119 @@ bool queryToolInsertFactory::CheckEnable(pgObject *obj)
     return view->HasInsertRule();
 }
 
+#ifdef wxUSE_PRINTING_ARCHITECTURE
+extern wxPrintData *g_explainGQBPrintData;
+extern wxPageSetupDialogData *g_explainGQBPageSetupData;
+extern wxPrintData *g_queryPrintData;
+extern wxPageSetupDialogData *g_queryPageSetupData;
+
+void frmQuery::OnPageSetup(wxCommandEvent& ev)
+{
+    if (ev.GetId() == MNU_PAGESETUP_QUERY)
+    {
+        (*g_queryPageSetupData) = * g_queryPrintData;
+        wxPageSetupDialog pageSetupDialog(this, g_queryPageSetupData);
+        pageSetupDialog.ShowModal();
+        (*g_queryPrintData) = pageSetupDialog.GetPageSetupData().GetPrintData();
+        (*g_queryPageSetupData) = pageSetupDialog.GetPageSetupData();
+
+        settings->SetQueryPrinterPaperId(g_queryPrintData->GetPaperId());
+        settings->SetQueryPrinterOrientation(g_queryPrintData->GetOrientation());
+        settings->SetQueryPageMarginTopLeft(g_queryPageSetupData->GetMarginTopLeft());
+        settings->SetQueryPageMarginBottomRight(g_queryPageSetupData->GetMarginBottomRight());
+        settings->SetQueryPaperSize(g_queryPageSetupData->GetPaperSize());
+    }
+    else if (ev.GetId() == MNU_PAGESETUP_EXPLAINGQB)
+    {
+        (*g_explainGQBPageSetupData) = * g_explainGQBPrintData;
+        wxPageSetupDialog pageSetupDialog(this, g_explainGQBPageSetupData);
+        pageSetupDialog.ShowModal();
+        (*g_explainGQBPrintData) = pageSetupDialog.GetPageSetupData().GetPrintData();
+        (*g_explainGQBPageSetupData) = pageSetupDialog.GetPageSetupData();
+
+        settings->SetExplainGQBPrinterPaperId(g_explainGQBPrintData->GetPaperId());
+        settings->SetExplainGQBPrinterOrientation(g_explainGQBPrintData->GetOrientation());
+        settings->SetExplainGQBPageMarginTopLeft(g_explainGQBPageSetupData->GetMarginTopLeft());
+        settings->SetExplainGQBPageMarginBottomRight(g_explainGQBPageSetupData->GetMarginBottomRight());
+        settings->SetExplainGQBPaperSize(g_explainGQBPageSetupData->GetPaperSize());
+    }
+}
+
+#ifdef __WXMAC__
+void frmQuery::OnPageMargins(wxCommandEvent& ev)
+{
+    if (ev.GetId() == MNU_PAGEMARGINS_QUERY)
+    {
+        (*g_queryPageSetupData) = *g_queryPrintData;
+    
+        wxMacPageMarginsDialog pageMarginsDialog(this, g_queryPageSetupData);
+        pageMarginsDialog.ShowModal();
+    
+        (*g_queryPrintData) = pageMarginsDialog.GetPageSetupDialogData().GetPrintData();
+        (*g_queryPageSetupData) = pageMarginsDialog.GetPageSetupDialogData();
+
+        settings->SetQueryPageMarginTopLeft(g_queryPageSetupData->GetMarginTopLeft());
+        settings->SetQueryPageMarginBottomRight(g_queryPageSetupData->GetMarginBottomRight());
+        settings->SetQueryPaperSize(g_queryPageSetupData->GetPaperSize());
+    }
+    else if (ev.GetId() == MNU_PAGEMARGINS_EXPLAINGQB)
+    {
+        (*g_explainGQBPageSetupData) = *g_explainGQBPrintData;
+    
+        wxMacPageMarginsDialog pageMarginsDialog(this, g_explainGQBPageSetupData);
+        pageMarginsDialog.ShowModal();
+    
+        (*g_explainGQBPrintData) = pageMarginsDialog.GetPageSetupDialogData().GetPrintData();
+        (*g_explainGQBPageSetupData) = pageMarginsDialog.GetPageSetupDialogData();
+
+        settings->SetExplainGQBPageMarginTopLeft(g_explainGQBPageSetupData->GetMarginTopLeft());
+        settings->SetExplainGQBPageMarginBottomRight(g_explainGQBPageSetupData->GetMarginBottomRight());
+        settings->SetExplainGQBPaperSize(g_explainGQBPageSetupData->GetPaperSize());
+    }
+}
+#endif // __WXMAC__
+
+void frmQuery::OnPrintPreview(wxCommandEvent& ev)
+{
+    if (ev.GetId() == MNU_PRINTPREVIEW_QUERY)
+        sqlQuery->PrintPreview();
+    else if (ev.GetId() == MNU_PRINTPREVIEW_GQB)
+        controller->getView()->PrintPreview();
+    else if (ev.GetId() == MNU_PRINTPREVIEW_EXPLAIN)
+        explainCanvas->PrintPreview();
+}
+
+void frmQuery::OnPrint(wxCommandEvent& ev)
+{
+    if (ev.GetId() == MNU_PRINT_QUERY)
+        sqlQuery->Print();
+    else if (ev.GetId() == MNU_PRINT_GQB)
+        controller->getView()->Print();
+    else if (ev.GetId() == MNU_PRINT_EXPLAIN)
+        explainCanvas->Print();
+}
+#endif // wxUSE_PRINTING_ARCHITECTURE
+
+void frmQuery::SaveExplainAsImage(wxCommandEvent& ev)
+{
+    wxFileDialog *dlg=new wxFileDialog(this, _("Save Explain As image file"), lastDir, lastFilename,
+           _("Bitmap files (*.bmp)|*.bmp"), wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
+    if (dlg->ShowModal() == wxID_OK)
+    {
+        lastFilename=dlg->GetFilename();
+        lastDir = dlg->GetDirectory();
+        lastPath = dlg->GetPath();
+
+        if (!lastPath.Contains(wxT(".")))
+            lastPath += wxT(".bmp");
+
+        if (ev.GetId() == MNU_SAVEAS_IMAGE_GQB)
+            controller->getView()->SaveAsImage(lastPath);
+        else if (ev.GetId() == MNU_SAVEAS_IMAGE_EXPLAIN)
+            explainCanvas->SaveAsImage(lastPath);
+    }
+}
+
 ///////////////////////////////////////////////////////
 
 pgScriptTimer::pgScriptTimer(frmQuery * parent) :
diff --git a/pgadmin/gqb/gqbController.cpp b/pgadmin/gqb/gqbController.cpp
index 0bf2c2c..1f2c144 100644
--- a/pgadmin/gqb/gqbController.cpp
+++ b/pgadmin/gqb/gqbController.cpp
@@ -29,7 +29,7 @@
 
 wxWindowID CTL_NTBKPANELS = ::wxNewId();
 
-gqbController::gqbController(gqbModel *_model, wxWindow *gqbParent, wxNotebook *gridParent, wxSize size=wxSize(800,1280))
+gqbController::gqbController(gqbModel *_model, wxWindow *gqbParent, wxNotebook *gridParent, wxSize size=wxSize(GQB_MIN_WIDTH, GQB_MIN_HEIGHT))
 : wxObject()
 {
     pparent = gqbParent;
diff --git a/pgadmin/gqb/gqbGraphSimple.cpp b/pgadmin/gqb/gqbGraphSimple.cpp
index 7371fbd..47cf5d2 100644
--- a/pgadmin/gqb/gqbGraphSimple.cpp
+++ b/pgadmin/gqb/gqbGraphSimple.cpp
@@ -49,7 +49,7 @@ gqbGraphSimple::gqbGraphSimple()
 
 // NOTES:(1) store values of width & height at queryTable.
 // (2)Need to set a font for the device context before get font metrics with GetTextExtent
-void gqbGraphSimple::drawTable(wxBufferedDC& bdc, wxPoint *origin, gqbQueryObject *queryTable)
+void gqbGraphSimple::drawTable(wxMemoryDC& bdc, wxPoint *origin, gqbQueryObject *queryTable)
 {
     long  w=0,h=0,height=0,width=0,margin=5;
 
@@ -188,7 +188,7 @@ gqbColumn* gqbGraphSimple::getColumnAtPosition(wxPoint *clickPoint, gqbQueryObje
 }
 
 
-void gqbGraphSimple::drawTempJoinLine(wxBufferedDC& bdc, wxPoint &origin, wxPoint &end)
+void gqbGraphSimple::drawTempJoinLine(wxMemoryDC& bdc, wxPoint &origin, wxPoint &end)
 {
     wxPoint anchorsUsed = wxPoint(0,0);
 
@@ -207,7 +207,7 @@ void gqbGraphSimple::drawTempJoinLine(wxBufferedDC& bdc, wxPoint &origin, wxPoin
 }
 
 
-void gqbGraphSimple::drawJoin(wxBufferedDC& bdc, wxPoint& origin, wxPoint& dest, wxPoint& anchorUsed, bool selected=false, type_Join joinKind=_equally)
+void gqbGraphSimple::drawJoin(wxMemoryDC& bdc, wxPoint& origin, wxPoint& dest, wxPoint& anchorUsed, bool selected=false, type_Join joinKind=_equally)
 {
     wxPoint origin2=origin;
     wxPoint dest2=dest;
@@ -451,8 +451,11 @@ void  gqbGraphSimple::UpdatePosObject(gqbQueryObject *queryTable, int x, int y,
 {
     x-=cursorAdjustment;                          // Move Pointer to a better Position;
     y-=rowHeight/2;
-    queryTable->position.x=x;                     // Update position of table
-    queryTable->position.y=y;
+    
+    // Update position of table
+    // Do not allow table/view moved/repositioned less than (0, 0) cordinates
+    queryTable->position.x= x > 0 ? x : 0;
+    queryTable->position.y= y > 0 ? y : 0;
 
     // Update position of anchor points of Joins that origin from this table
     if(queryTable->getHaveJoins())
diff --git a/pgadmin/gqb/gqbView.cpp b/pgadmin/gqb/gqbView.cpp
index e62fa37..f3423a5 100644
--- a/pgadmin/gqb/gqbView.cpp
+++ b/pgadmin/gqb/gqbView.cpp
@@ -47,6 +47,7 @@ EVT_KEY_DOWN(gqbView::OnKeyDown)
 EVT_MENU(GQB_RMJ_DELETE,	gqbView::OnMenuJoinDelete)
 EVT_MENU(GQB_RMT_DELETE,	gqbView::OnMenuTableDelete)
 EVT_MENU(GQB_RMT_SETALIAS,	gqbView::OnMenuTableSetAlias)
+EVT_MENU(GQB_REFRESH,	    gqbView::OnRefresh)
 END_EVENT_TABLE()
 
 gqbView::gqbView(wxWindow *gqbParent, wxNotebook *gridParent, wxSize size, gqbController *controller, gqbModel *model)
@@ -72,6 +73,7 @@ wxHSCROLL | wxVSCROLL | wxBORDER | wxRETAINED)
     joinCursor= wxCursor(joinCursorImage);
     m_rightJoins=NULL;
     m_rightTables=NULL;
+    m_gqbPopup=NULL;
     jTempSelected=NULL;
     cTempSelected=NULL;
 
@@ -117,6 +119,9 @@ gqbView::~gqbView()
 
     if(m_rightJoins)
         delete m_rightJoins;
+    
+    if(m_gqbPopup)
+        delete m_gqbPopup;
 }
 
 
@@ -131,14 +136,14 @@ void gqbView::onPaint(wxPaintEvent& event)
 {
     wxPaintDC dcc(this);                          // Prepare Context for Buffered Draw
     wxBufferedDC dc(&dcc, canvasSize);
-    drawAll(dc);                                  // Call Function to draw all
+    drawAll(dc, true);                            // Call Function to draw all
 }
 
 
 // GQB-TODO: remove all possible modification to model from here to controller.
 void gqbView::onRightClick(wxMouseEvent& event)
 {
-// GQB-TODO: Validate Alias
+    // GQB-TODO: Validate Alias
     gqbObject *anySelected=NULL;
     wxPoint pdc=event.GetPosition();
     pdc.x=event.GetPosition().x;
@@ -154,6 +159,8 @@ void gqbView::onRightClick(wxMouseEvent& event)
                 m_rightTables = new wxMenu;
                 m_rightTables->Append(GQB_RMT_SETALIAS, _("&Set Alias for table"));
                 m_rightTables->Append(GQB_RMT_DELETE, _("&Delete Table"));
+                m_rightTables->AppendSeparator();
+                m_rightTables->Append(GQB_REFRESH, _("&Refresh"));
             }
             cTempSelected=(gqbQueryObject *) (gqbObjectCollection *) anySelected;
             jTempSelected=NULL;
@@ -166,12 +173,23 @@ void gqbView::onRightClick(wxMouseEvent& event)
             {
                 m_rightJoins = new wxMenu;
                 m_rightJoins->Append(GQB_RMJ_DELETE, _("&Delete Join"));
+                m_rightJoins->AppendSeparator();
+                m_rightJoins->Append(GQB_REFRESH, _("&Refresh"));
             }
             cTempSelected=NULL;
             jTempSelected=(gqbQueryJoin *) anySelected;;
             PopupMenu(m_rightJoins, event.GetPosition());
         }
     }
+    else
+    {
+        if(!m_gqbPopup)
+        {
+            m_gqbPopup = new wxMenu;
+            m_gqbPopup->Append(GQB_REFRESH, _("&Refresh"));
+        }
+        PopupMenu(m_gqbPopup, event.GetPosition());
+    }
 }
 
 
@@ -481,8 +499,9 @@ void gqbView::onMotion(wxMouseEvent& event)
                 {
                     this->Refresh();
                     refresh=1;
-                }else
-                refresh++;
+                }
+                else
+                    refresh++;
 
             }
         }
@@ -521,7 +540,7 @@ void gqbView::OnKeyDown(wxKeyEvent& event)
 }
 
 
-void gqbView::drawAll(wxBufferedDC& bdc)
+void gqbView::drawAll(wxMemoryDC& bdc, bool adjustScrolling)
 {
     bdc.Clear();
     if(!iterator)
@@ -537,8 +556,11 @@ void gqbView::drawAll(wxBufferedDC& bdc)
         wxPoint pt = wxPoint(tmp->position);      // Use a copy because I don't want to store the modified 
 		                                          // version of point after CalcScrolledPosition was called
 
-		// adjust coordinates
-        this->CalcScrolledPosition(pt.x,pt.y,&pt.x,&pt.y);
+        if (adjustScrolling)
+        {
+            // adjust coordinates
+            this->CalcScrolledPosition(pt.x,pt.y,&pt.x,&pt.y);
+        }
         graphBehavior->drawTable(bdc,&pt,tmp);    // graph table
     }
 
@@ -557,11 +579,14 @@ void gqbView::drawAll(wxBufferedDC& bdc)
                 wxPoint o = join->getSourceAnchor();
                 wxPoint d = join->getDestAnchor();
 
-                // adjust coordinates origin
-                this->CalcScrolledPosition(o.x,o.y,&o.x,&o.y);
-
-                // adjust coordinates destination
-                this->CalcScrolledPosition(d.x,d.y,&d.x,&d.y);
+                if (adjustScrolling)
+                {
+                    // adjust coordinates origin
+                    this->CalcScrolledPosition(o.x,o.y,&o.x,&o.y);
+    
+                    // adjust coordinates destination
+                    this->CalcScrolledPosition(d.x,d.y,&d.x,&d.y);
+                }
                 graphBehavior->drawJoin(bdc,o,d,join->getAnchorsUsed(), join->getSelected(), join->getKindofJoin());
             }
             delete joinsIterator;
@@ -575,8 +600,11 @@ void gqbView::drawAll(wxBufferedDC& bdc)
         // Draw temporary line while creating a join
         wxPoint source=jpos;
         wxPoint destination=pos;
-        this->CalcScrolledPosition(source.x,source.y,&source.x,&source.y);
-        this->CalcScrolledPosition(destination.x,destination.y,&destination.x,&destination.y);
+        if(adjustScrolling)
+        {
+            this->CalcScrolledPosition(source.x,source.y,&source.x,&source.y);
+            this->CalcScrolledPosition(destination.x,destination.y,&destination.x,&destination.y);
+        }
         graphBehavior->drawTempJoinLine(bdc,source,destination);
     }
 }
@@ -645,3 +673,131 @@ void gqbView::updateTable(gqbQueryObject *queryTable)
     this->Refresh();
 }
 
+
+void gqbView::OnRefresh(wxCommandEvent& ev)
+{
+    updateModelSize(NULL, true);
+    this->Update();
+}
+
+
+/*
+*  updateModelSize
+*     - Update the model size.
+*     - Calculate the maximum width and maximum height of the model
+*  * When added a new table/view, the obj parameter must not be null,
+*    otherwise it should be null
+*  * When removed a table/view from model, the obj parameter must be null,
+*    and update parameter should be true, otherwise update parameter should
+*    be false (Dragging event)
+*/
+void gqbView::updateModelSize(gqbQueryObject* obj, bool updateAnyWay)
+{
+    static int callCount = 0;
+    callCount++;
+    if (!obj)
+    {
+        // Do not update model size, everytime it gets called
+        // Update the size once in 10 times
+        // Update the size only if update flag is true
+        if (callCount < 10 && !updateAnyWay)
+            return;
+        callCount = 0;
+        // Figure out the actual model size.
+        // Remove table
+        int w = 0, h = 0, maxW = 0, maxH = 0;
+        if(!iterator)
+            // Get an iterator for the objects (tables/views) in the model.
+            iterator = this->model->createQueryIterator();
+        else
+            iterator->ResetIterator();
+    
+        while (iterator->HasNext())
+        {
+            gqbQueryObject *tmp= (gqbQueryObject *)iterator->Next();;
+            w = tmp->position.x + tmp->getWidth();
+            h = tmp->position.y + tmp->getHeight();
+    
+            if (maxW < w)
+                maxW = w;
+            if (maxH < h)
+                maxH = h;
+        }
+ 
+        // Reset Model size
+        modelSize.Set(maxW, maxH);
+    }
+    else
+    {
+        int w = 0, h = 0;
+        w = obj->position.x + obj->getWidth();
+        h = obj->position.y + obj->getHeight();
+
+        if (w > modelSize.GetWidth())
+            modelSize.SetWidth(w);
+        if (h > modelSize.GetHeight())
+            modelSize.SetHeight(h);
+    }
+    bool updateView = false;
+
+    if ((modelSize.GetWidth() > GQB_MIN_WIDTH || canvasSize.GetWidth() > GQB_MIN_WIDTH) &&
+         modelSize.GetWidth() != canvasSize.GetWidth())
+    {
+        canvasSize.SetWidth((modelSize.GetWidth() > GQB_MIN_WIDTH ? modelSize.GetWidth() : GQB_MIN_WIDTH));
+        updateView = true;
+    }
+    if ((modelSize.GetHeight() > GQB_MIN_HEIGHT || canvasSize.GetHeight() > GQB_MIN_HEIGHT) &&
+         modelSize.GetHeight() != canvasSize.GetHeight())
+    {
+        canvasSize.SetHeight((modelSize.GetHeight() > GQB_MIN_HEIGHT ? modelSize.GetHeight() : GQB_MIN_HEIGHT));
+        updateView = true;
+    }
+
+    if (updateView)
+    {
+        SetVirtualSize(canvasSize);
+    }
+}
+
+bool gqbView::canSaveAsImage()
+{
+    updateModelSize(NULL, true);
+    return !(modelSize.GetWidth() == 0 || modelSize.GetHeight() == 0);
+}
+
+void gqbView::SaveAsImage(const wxString& fileName)
+{
+
+    updateModelSize(NULL, true);
+
+    if (modelSize.GetWidth() == 0 || modelSize.GetHeight() == 0)
+    {
+        wxMessageBox(_("Nothing to be saved!"), _("Save As an image"), wxOK | wxICON_INFORMATION);
+        return;
+    }
+
+    int width = 0, height = 0;
+    GetVirtualSize(&width, &height);
+    
+    /*
+    * Create the bitmap from the Explain window
+    */
+    wxMemoryDC memDC;
+    wxBitmap tempBitmap(width, height);
+    
+    memDC.SelectObject(tempBitmap);
+    memDC.Clear();
+    
+    // Draw the diagram on the bitmap (Memory Device Context)
+    drawAll(memDC, false);
+    
+    memDC.SelectObject(wxNullBitmap);
+    
+    if (!tempBitmap.SaveFile(fileName, wxBITMAP_TYPE_BMP))
+    {
+        wxLogError(__("Could not write the file %s: Errcode=%d."), fileName.c_str(), wxSysErrorCode());
+    }
+}
+
+
+
diff --git a/pgadmin/gqb/module.mk b/pgadmin/gqb/module.mk
index f98ca1d..a430e22 100644
--- a/pgadmin/gqb/module.mk
+++ b/pgadmin/gqb/module.mk
@@ -20,7 +20,7 @@ pgadmin3_SOURCES += \
 	$(srcdir)/gqb/gqbGridOrderTable.cpp \
 	$(srcdir)/gqb/gqbGridProjTable.cpp \
 	$(srcdir)/gqb/gqbGridRestTable.cpp \
-        $(srcdir)/gqb/gqbGridJoinTable.cpp \
+	$(srcdir)/gqb/gqbGridJoinTable.cpp \
 	$(srcdir)/gqb/gqbModel.cpp \
 	$(srcdir)/gqb/gqbObject.cpp \
 	$(srcdir)/gqb/gqbObjectCollection.cpp \
@@ -28,7 +28,8 @@ pgadmin3_SOURCES += \
 	$(srcdir)/gqb/gqbSchema.cpp \
 	$(srcdir)/gqb/gqbTable.cpp \
 	$(srcdir)/gqb/gqbViewPanels.cpp \
-	$(srcdir)/gqb/gqbView.cpp
+	$(srcdir)/gqb/gqbView.cpp \
+	$(srcdir)/gqb/gqbPrintout.cpp
 
 EXTRA_DIST += \
     $(srcdir)/gqb/module.mk
diff --git a/pgadmin/include/ctl/ctlSQLBox.h b/pgadmin/include/ctl/ctlSQLBox.h
index ebfc3ea..bcc14a9 100644
--- a/pgadmin/include/ctl/ctlSQLBox.h
+++ b/pgadmin/include/ctl/ctlSQLBox.h
@@ -17,6 +17,15 @@
 #include <wx/stc/stc.h>
 #include <wx/fdrepdlg.h>
 
+#if wxUSE_PRINTING_ARCHITECTURE
+#include "wx/print.h"
+#include "wx/printdlg.h"
+#ifdef __WXMAC__
+#include "wx/mac/printdlg.h"
+#endif
+#endif // wxUSE_PRINTING_ARCHITECTURE
+
+
 #include "db/pgConn.h"
 #include "dlg/dlgFindReplace.h"
 
@@ -58,6 +67,10 @@ public:
     void SetAutoIndent(bool on) { m_autoIndent = on; }
     void EnableAutoComp(bool on) { m_autocompDisabled = on; }
     bool BlockComment(bool uncomment=false);
+#if wxUSE_PRINTING_ARCHITECTURE
+    void Print();
+    void PrintPreview();
+#endif // wxUSE_PRINTING_ARCHITECTURE
 
     CharacterRange RegexFindText(int minPos, int maxPos, const wxString& text);
 
@@ -71,7 +84,38 @@ private:
     dlgFindReplace* m_dlgFindReplace;
 	pgConn *m_database;
     bool m_autoIndent, m_autocompDisabled;
+
+    friend class QueryPrintout;
 };
 
+#if wxUSE_PRINTING_ARCHITECTURE
+//----------------------------------------------------------------------------
+// QueryPrintout
+class QueryPrintout : public wxPrintout
+{
+
+public:
+    // Constructor
+    QueryPrintout (ctlSQLBox *queryeditor, wxString title = _(""));
+    ~QueryPrintout ();
+    
+    //! Event Handlers
+    bool OnPrintPage (int page);
+    bool OnBeginDocument (int startPage, int endPage);
+    
+    // Print Functions
+    bool HasPage (int page);
+    void GetPageInfo (int *minPage, int *maxPage, int *selPageFrom, int *selPageTo);
+    
+private:
+    ctlSQLBox     *m_queryEditor;
+    int m_printed;
+    wxRect m_pageRect;
+    wxRect m_printRect;
+    
+    bool PrintScaling (wxDC *dc);
+};
+#endif // wxUSE_PRINTING_ARCHITECTURE
 
 #endif
+
diff --git a/pgadmin/include/ctl/explainCanvas.h b/pgadmin/include/ctl/explainCanvas.h
index b18d5bc..6056ddb 100644
--- a/pgadmin/include/ctl/explainCanvas.h
+++ b/pgadmin/include/ctl/explainCanvas.h
@@ -14,6 +14,13 @@
 
 #include <wx/ogl/ogl.h>
 
+#if wxUSE_PRINTING_ARCHITECTURE
+#include "wx/print.h"
+#include "wx/printdlg.h"
+#ifdef __WXMAC__
+#include "wx/mac/printdlg.h"
+#endif
+#endif // wxUSE_PRINTING_ARCHITECTURE
 
 #if wxUSE_DEPRECATED
 #error wxUSE_DEPRECATED should be 0!
@@ -33,10 +40,15 @@ public:
     void ShowPopup(ExplainShape *s);
     void SetExplainString(const wxString &str);
     void Clear();
+#if wxUSE_PRINTING_ARCHITECTURE
+    void PrintPreview();
+    void Print();
+#endif //wxUSE_PRINTING_ARCHITECTURE
+    void SaveAsImage(const wxString& fileName);
 
 private:
 
-    ExplainShape *rootShape, *lastShape;
+    ExplainShape *rootShape;
     ExplainPopup *popup;
 };
 
@@ -104,4 +116,34 @@ private:
     DECLARE_EVENT_TABLE()
 };
 
+
+#if wxUSE_PRINTING_ARCHITECTURE
+//----------------------------------------------------------------------------
+// ExplainPrintout
+class ExplainPrintout : public wxPrintout {
+
+public:
+    // Constructor
+    ExplainPrintout (ExplainCanvas *explain, wxString title = _T(""));
+    ~ExplainPrintout ();
+    
+    //! Event Handlers
+    bool OnPrintPage (int page);
+    bool OnBeginDocument (int startPage, int endPage);
+    
+    // Print Functions
+    bool HasPage (int page);
+    void GetPageInfo (int *minPage, int *maxPage, int *selPageFrom, int *selPageTo);
+    
+private:
+    ExplainCanvas *m_explain;
+    int            m_noPages;
+    wxRect         m_pageRect;
+    wxBitmap      *m_printBitmap;
+    
+    bool PrintScaling (wxDC *dc);
+};
+#endif // wxUSE_PRINTING_ARCHITECTURE
+
+
 #endif
diff --git a/pgadmin/include/frm/frmQuery.h b/pgadmin/include/frm/frmQuery.h
index 3ffc24c..b88e2e3 100644
--- a/pgadmin/include/frm/frmQuery.h
+++ b/pgadmin/include/frm/frmQuery.h
@@ -138,6 +138,15 @@ private:
     void OnOpen(wxCommandEvent& event);
     void OnSave(wxCommandEvent& event);
     void OnSaveAs(wxCommandEvent& event);
+#ifdef wxUSE_PRINTING_ARCHITECTURE
+    void OnPageSetup(wxCommandEvent& ev);
+#ifdef __WXMAC__
+    void OnPageMargins(wxCommandEvent& ev);
+#endif
+    void OnPrintPreview(wxCommandEvent& ev);
+    void OnPrint(wxCommandEvent& ev);
+#endif //wxUSE_PRINTING_ARCHITECTURE
+    void SaveExplainAsImage(wxCommandEvent& event);
     void OnExport(wxCommandEvent& event);
     void OnExit(wxCommandEvent& event);
     void OnCut(wxCommandEvent& event);
@@ -211,6 +220,11 @@ private:
     wxMenu *macrosMenu;
     wxMenu *lineEndMenu;
     wxMenu *formatMenu;
+    wxMenu *saveasImageMenu;
+    wxMenu *pageSetupMenu;
+    wxMenu *printMenu;
+    wxMenu *printPreviewMenu;
+
     wxString title;
     wxString lastFilename, lastDir;
 
diff --git a/pgadmin/include/frm/menu.h b/pgadmin/include/frm/menu.h
index bb11c7d..1760df6 100644
--- a/pgadmin/include/frm/menu.h
+++ b/pgadmin/include/frm/menu.h
@@ -19,6 +19,18 @@ enum
     MNU_ADDSERVER = 101,
     MNU_SAVEDEFINITION,
     MNU_EXIT,
+    MNU_PAGESETUP_QUERY,
+    MNU_PAGESETUP_EXPLAINGQB,
+    MNU_PAGEMARGINS_QUERY,
+    MNU_PAGEMARGINS_EXPLAINGQB,
+    MNU_PRINTPREVIEW_QUERY,
+    MNU_PRINTPREVIEW_GQB,
+    MNU_PRINTPREVIEW_EXPLAIN,
+    MNU_PRINT_QUERY,
+    MNU_PRINT_GQB,
+    MNU_PRINT_EXPLAIN,
+    MNU_SAVEAS_IMAGE_GQB,
+    MNU_SAVEAS_IMAGE_EXPLAIN,
     MNU_CONTEXTMENU,
     MNU_SQLPANE,
     MNU_OBJECTBROWSER,
diff --git a/pgadmin/include/gqb/gqbEvents.h b/pgadmin/include/gqb/gqbEvents.h
index 4ea0901..ee7caf1 100644
--- a/pgadmin/include/gqb/gqbEvents.h
+++ b/pgadmin/include/gqb/gqbEvents.h
@@ -24,6 +24,7 @@ enum gqb_rMenus
     GQB_RMJ_DELETE = 2000,
     GQB_RMJ_SETTYPE,
     GQB_RMT_DELETE,
-    GQB_RMT_SETALIAS
+    GQB_RMT_SETALIAS,
+    GQB_REFRESH
 };
 #endif
diff --git a/pgadmin/include/gqb/gqbGraphBehavior.h b/pgadmin/include/gqb/gqbGraphBehavior.h
index fe6364e..ce85dc7 100644
--- a/pgadmin/include/gqb/gqbGraphBehavior.h
+++ b/pgadmin/include/gqb/gqbGraphBehavior.h
@@ -23,9 +23,9 @@ class gqbGraphBehavior : public wxObject
 public:
     // Important: The drawTable function always should store the width & height of the graphic
     // representation of the table inside the gqbQueryObject for use of controller.
-    virtual void drawTable(wxBufferedDC& bdc, wxPoint *origin, gqbQueryObject *queryTable)=0;
-    virtual void drawTempJoinLine(wxBufferedDC& bdc, wxPoint &origin, wxPoint &end)=0;
-    virtual void drawJoin(wxBufferedDC& bdc, wxPoint& origin, wxPoint& dest, wxPoint& anchorUsed, bool selected, type_Join joinKind)=0;
+    virtual void drawTable(wxMemoryDC& bdc, wxPoint *origin, gqbQueryObject *queryTable)=0;
+    virtual void drawTempJoinLine(wxMemoryDC& bdc, wxPoint &origin, wxPoint &end)=0;
+    virtual void drawJoin(wxMemoryDC& bdc, wxPoint& origin, wxPoint& dest, wxPoint& anchorUsed, bool selected, type_Join joinKind)=0;
     virtual void calcAnchorPoint(gqbQueryJoin *join)=0;
     virtual void UpdatePosObject(gqbQueryObject *queryTable, int x, int y, int cursorAdjustment)=0;
     
diff --git a/pgadmin/include/gqb/gqbGraphSimple.h b/pgadmin/include/gqb/gqbGraphSimple.h
index 549f474..ff10fb4 100644
--- a/pgadmin/include/gqb/gqbGraphSimple.h
+++ b/pgadmin/include/gqb/gqbGraphSimple.h
@@ -23,10 +23,10 @@ class gqbGraphSimple : public gqbGraphBehavior
 {
 public:
     gqbGraphSimple();
-    void drawTable(wxBufferedDC& bdc, wxPoint *origin, gqbQueryObject *queryTable);
-    void drawTempJoinLine(wxBufferedDC& bdc, wxPoint &origin, wxPoint &end);
+    void drawTable(wxMemoryDC& bdc, wxPoint *origin, gqbQueryObject *queryTable);
+    void drawTempJoinLine(wxMemoryDC& bdc, wxPoint &origin, wxPoint &end);
     void calcAnchorPoint(gqbQueryJoin *join);
-    void drawJoin(wxBufferedDC& bdc, wxPoint& origin, wxPoint& dest, wxPoint& anchorUsed, bool selected, type_Join joinKind);
+    void drawJoin(wxMemoryDC& bdc, wxPoint& origin, wxPoint& dest, wxPoint& anchorUsed, bool selected, type_Join joinKind);
     void UpdatePosObject(gqbQueryObject *queryTable, int x, int y, int cursorAdjustment);
     gqbColumn* getColumnAtPosition(wxPoint *clickPoint, gqbQueryObject *queryTable, int sensibility=17);
     bool clickOnJoin(gqbQueryJoin *join, wxPoint &pt, wxPoint &origin, wxPoint &dest);
diff --git a/pgadmin/include/gqb/gqbViewController.h b/pgadmin/include/gqb/gqbViewController.h
index 80c6489..6a43487 100644
--- a/pgadmin/include/gqb/gqbViewController.h
+++ b/pgadmin/include/gqb/gqbViewController.h
@@ -18,6 +18,23 @@
 #include <wx/dnd.h>
 #include <wx/dataobj.h>
 
+#if wxUSE_PRINTING_ARCHITECTURE
+#include "wx/print.h"
+#include "wx/printdlg.h"
+#ifdef __WXMAC__
+#include "wx/mac/printdlg.h"
+#endif
+ 
+#if wxUSE_LIBGNOMEPRINT
+#include "wx/html/forcelnk.h"
+FORCE_LINK(gnome_print)
+#endif
+#endif // wxUSE_PRINTING_ARCHITECTURE
+
+#define GQB_MIN_WIDTH  1280
+#define GQB_MIN_HEIGHT 800
+
+
 // App headers
 #include "gqb/gqbObject.h"
 #include "gqb/gqbModel.h"
@@ -106,7 +123,13 @@ class gqbView: public wxScrolledWindow
 public:
     gqbView(wxWindow *gqbParent, wxNotebook *gridParent, wxSize size, gqbController *controller, gqbModel *model);
     ~gqbView();
-    void drawAll(wxBufferedDC& bdc);
+#if wxUSE_PRINTING_ARCHITECTURE
+    void PrintPreview();
+    void Print();
+#endif //wxUSE_PRINTING_ARCHITECTURE
+    void SaveAsImage(const wxString& path);
+    bool canSaveAsImage();
+    void drawAll(wxMemoryDC& bdc, bool adjustScrolling);
     void setPointerMode(pointerMode pm);
 
 	// Events for wxScrolledWindow
@@ -121,9 +144,11 @@ public:
     wxPanel* getCriteriaPanel(){ return (wxPanel*)criteriaPanel; };
     wxPanel* getOrderPanel(){ return (wxPanel*)orderPanel; };
     wxPanel* getJoinsPanel() { return joinsPanel; }
+    const wxSize& getCanvasSize() { return canvasSize; }
     void newTableAdded(gqbQueryObject *item);
     bool clickOnJoin (gqbQueryJoin *join, wxPoint &pt, wxPoint &origin, wxPoint &dest);
     void updateTable(gqbQueryObject *table);
+    const wxSize& getModelSize() { return modelSize; }
 
 	// Functions for all gqb extra Panels (projection, criteria..)
     void emptyPanelsData();
@@ -144,6 +169,7 @@ private:
                           
     gqbGridOrderTable *orderByLGridTable, *orderByRGridTable;	// Data model for order by grid internals
     wxSize canvasSize;
+    wxSize modelSize;
     bool changeTOpressed;
     
 	// just a point to the selected item on the collection, shouldn't be destroy inside this class
@@ -155,10 +181,13 @@ private:
     pointerMode mode;             // pointer is used as normally or as in joins by example
     wxImage joinCursorImage;
     wxCursor joinCursor;
-    wxMenu *m_rightJoins, *m_rightTables;
+    wxMenu *m_rightJoins, *m_rightTables, *m_gqbPopup;
     void OnMenuJoinDelete(wxCommandEvent& event);
     void OnMenuTableDelete(wxCommandEvent& event);
     void OnMenuTableSetAlias(wxCommandEvent& event);
+    void OnRefresh(wxCommandEvent& ev);
+    void updateModelSize(gqbQueryObject* obj, bool updateAnyWay);
+
     wxArrayString joinTypeChoices;
     DECLARE_EVENT_TABLE()
 };
@@ -177,4 +206,34 @@ public:
 private:
     gqbBrowser *tree;
 };
+
+#if wxUSE_PRINTING_ARCHITECTURE
+//----------------------------------------------------------------------------
+// GQBPrintout
+class GQBPrintout : public wxPrintout {
+
+public:
+
+    // Constructor
+    GQBPrintout (gqbView *view, wxString title = _T(""));
+    ~GQBPrintout ();
+
+    //! Event Handlers
+    bool OnPrintPage (int page);
+    bool OnBeginDocument (int startPage, int endPage);
+
+    // Print Functions
+    bool HasPage (int page);
+    void GetPageInfo (int *minPage, int *maxPage, int *selPageFrom, int *selPageTo);
+
+private:
+    gqbView  *m_view;
+    int       m_noPage;
+    wxRect    m_pageRect;
+    wxBitmap *m_printBitmap;
+
+    bool PrintScaling (wxDC *dc);
+};
+#endif // wxUSE_PRINTING_ARCHITECTURE
+
 #endif
diff --git a/pgadmin/include/utils/sysSettings.h b/pgadmin/include/utils/sysSettings.h
index 81e6788..cd5919e 100644
--- a/pgadmin/include/utils/sysSettings.h
+++ b/pgadmin/include/utils/sysSettings.h
@@ -121,7 +121,30 @@ public:
 	wxFont GetSQLFont();
     void SetSQLFont(const wxFont &font);
     int GetLineEndingType() const { int i; Read(wxT("LineEndingType"), &i, 2); return i; }
-	void SetLineEndingType(const int newval) { Write(wxT("LineEndingType"), newval); }
+    void SetLineEndingType(const int newval) { Write(wxT("LineEndingType"), newval); }
+
+    // Printer Support
+    wxPaperSize GetQueryPrinterPaperId() const { long l; l = Read(wxT("frmQuery/QueryPrinter/PaperId"), wxPAPER_A4); return (wxPaperSize)l; }
+    void SetQueryPrinterPaperId(wxPaperSize paperId) { Write(wxT("frmQuery/QueryPrinter/PaperId"), (long)paperId); }
+    long GetQueryPrinterOrientation() const { long l; l = Read(wxT("frmQuery/QueryPrinter/Orientation"), wxPORTRAIT); return l; }
+    void SetQueryPrinterOrientation(long orientation) { Write(wxT("frmQuery/QueryPrinter/Orientation"), orientation); }
+    void SetQueryPageMarginTopLeft(const wxPoint& topleft) { Write(wxT("frmQuery/QueryPrinter/Page/TopLeft"), topleft); } 
+    wxPoint GetQueryPageMarginTopLeft() const { wxPoint p; p = Read(wxT("frmQuery/QueryPrinter/Page/TopLeft"), wxPoint(10, 10)); return p; } 
+    wxPoint GetQueryPageMarginBottomRight () const { wxPoint p; p = Read(wxT("frmQuery/QueryPrinter/Page/BottomRight"), wxPoint(10, 10)); return p; } 
+    void SetQueryPageMarginBottomRight (const wxPoint& bottomright) { Write(wxT("frmQuery/QueryPrinter/Page/BottomRight"), bottomright); }
+    wxSize GetQueryPaperSize() const { wxSize s; s = Read(wxT("frmQuery/QueryPrinter/PaperSize"), wxSize(210, 297)); return s; }
+    void SetQueryPaperSize(const wxSize& size) { Write(wxT("frmQuery/QueryPrinter/PaperSize"), size); }
+
+    wxPaperSize GetExplainGQBPrinterPaperId() const { long l; l = Read(wxT("frmQuery/ExplaingGQBPrinter/PaperId"), wxPAPER_A4); return (wxPaperSize)l; }
+    void SetExplainGQBPrinterPaperId(wxPaperSize paperId) { Write(wxT("frmQuery/ExplaingGQBPrinter/PaperId"), (long)paperId); }
+    long GetExplainGQBPrinterOrientation() const { long l; l = Read(wxT("frmQuery/ExplaingGQBPrinter/Orientation"), wxLANDSCAPE); return l; }
+    void SetExplainGQBPrinterOrientation(long orientation) { Write(wxT("frmQuery/ExplaingGQBPrinter/Orientation"), orientation); }
+    void SetExplainGQBPageMarginTopLeft(const wxPoint& topleft) { Write(wxT("frmQuery/ExplaingGQBPrinter/Page/TopLeft"), topleft); } 
+    wxPoint GetExplainGQBPageMarginTopLeft() const { wxPoint p; p = Read(wxT("frmQuery/ExplaingGQBPrinter/Page/TopLeft"), wxPoint(5, 5)); return p; } 
+    wxPoint GetExplainGQBPageMarginBottomRight () const { wxPoint p; p = Read(wxT("frmQuery/ExplaingGQBPrinter/Page/BottomRight"), wxPoint(5, 5)); return p; } 
+    void SetExplainGQBPageMarginBottomRight (const wxPoint& bottomright) { Write(wxT("frmQuery/ExplaingGQBPrinter/Page/BottomRight"), bottomright); } 
+    wxSize GetExplainGQBPaperSize() const { wxSize s; s = Read(wxT("frmQuery/ExplainGQBPrinter/PaperSize"), wxSize(210, 297)); return s; }
+    void SetExplainGQBPaperSize(const wxSize& size) { Write(wxT("frmQuery/ExplainGQBPrinter/PaperSize"), size); }
 
     // Status Colours options
     wxString GetIdleProcessColour() const { wxString s; Read(wxT("IdleProcessColour"), &s, wxT("#5fa4d9")); return s; }
diff --git a/pgadmin/pgAdmin3.cpp b/pgadmin/pgAdmin3.cpp
index 0bf94a5..53b0314 100644
--- a/pgadmin/pgAdmin3.cpp
+++ b/pgadmin/pgAdmin3.cpp
@@ -111,6 +111,16 @@ bool dialogTestMode=false;
 
 #define LANG_FILE   wxT("pgadmin3.lng")
 
+#if wxUSE_PRINTING_ARCHITECTURE
+
+//! global print data, to remember settings
+wxPrintData *g_explainGQBPrintData = NULL;
+wxPageSetupDialogData *g_explainGQBPageSetupData = NULL;
+wxPrintData *g_queryPrintData = NULL;
+wxPageSetupDialogData *g_queryPageSetupData = NULL;
+
+#endif // wxUSE_PRINTING_ARCHITECTURE
+
 IMPLEMENT_APP(pgAdmin3)
 
 #ifdef __WXGTK__
@@ -296,9 +306,10 @@ bool pgAdmin3::OnInit()
         dialogTestMode = true;
 
     // Setup the image handlers and appearance factory before we do any GUI or config stuff
-    wxImage::AddHandler(new wxJPEGHandler());
-    wxImage::AddHandler(new wxPNGHandler());
-    wxImage::AddHandler(new wxGIFHandler());
+    ::wxInitAllImageHandlers();
+    // wxImage::AddHandler(new wxJPEGHandler());
+    // wxImage::AddHandler(new wxPNGHandler());
+    // wxImage::AddHandler(new wxGIFHandler());
 
     appearanceFactory = new pgAppearanceFactory();
 
@@ -399,6 +410,26 @@ bool pgAdmin3::OnInit()
     wxSleep(2);
 #endif
 
+#if wxUSE_PRINTING_ARCHITECTURE
+    // initialize print data and setup
+    g_explainGQBPrintData = new wxPrintData;
+    g_explainGQBPrintData->SetPaperId(settings->GetExplainGQBPrinterPaperId());
+    g_explainGQBPrintData->SetOrientation(settings->GetExplainGQBPrinterOrientation());
+    g_explainGQBPageSetupData = new wxPageSetupDialogData;
+    g_explainGQBPageSetupData->SetMarginTopLeft(settings->GetExplainGQBPageMarginTopLeft());
+    g_explainGQBPageSetupData->SetMarginBottomRight(settings->GetExplainGQBPageMarginBottomRight());
+    g_explainGQBPageSetupData->SetPaperSize(settings->GetExplainGQBPaperSize());
+
+    g_queryPrintData = new wxPrintData;
+    g_queryPrintData->SetPaperId(settings->GetQueryPrinterPaperId());
+    g_queryPrintData->SetOrientation(settings->GetQueryPrinterOrientation());
+    g_queryPageSetupData = new wxPageSetupDialogData;
+    g_queryPageSetupData->SetMarginTopLeft(settings->GetQueryPageMarginTopLeft());
+    g_queryPageSetupData->SetMarginBottomRight(settings->GetQueryPageMarginBottomRight());
+    g_queryPageSetupData->SetPaperSize(settings->GetQueryPaperSize());
+#endif // wxUSE_PRINTING_ARCHITECTURE
+
+
     if (configMode)
     {
         if (configMode == frmConfig::ANYFILE && wxDir::Exists(configFile))
@@ -674,6 +705,31 @@ int pgAdmin3::OnExit()
 
     // Delete the settings object to ensure settings are saved.
     delete settings;
+    
+#if wxUSE_PRINTING_ARCHITECTURE
+    // delete global print data and setup
+    if (g_explainGQBPrintData)
+    {
+        delete g_explainGQBPrintData;
+        g_explainGQBPrintData = NULL;
+    }
+    if (g_explainGQBPageSetupData) 
+    {
+        delete g_explainGQBPageSetupData;
+        g_explainGQBPageSetupData = NULL;
+    }
+    if (g_queryPrintData)
+    {
+        delete g_queryPrintData;
+        g_queryPrintData = NULL;
+    }
+    if (g_queryPageSetupData) 
+    {
+        delete g_queryPageSetupData;
+        g_queryPageSetupData = NULL;
+    }
+#endif // wxUSE_PRINTING_ARCHITECTURE
+
 
 #ifdef __WXMSW__
     WSACleanup();
diff --git a/pgadmin/pgAdmin3.vcproj b/pgadmin/pgAdmin3.vcproj
index 2fd55bf..fbbc0a8 100644
--- a/pgadmin/pgAdmin3.vcproj
+++ b/pgadmin/pgAdmin3.vcproj
@@ -424,6 +424,14 @@
 				>
 			</File>
 			<File
+				RelativePath=".\ctl\explainPrintout.cpp"
+				>
+			</File>
+			<File
+				RelativePath=".\ctl\queryPrintout.cpp"
+				>
+			</File>
+			<File
 				RelativePath=".\ctl\explainShape.cpp"
 				>
 			</File>
@@ -4000,6 +4008,10 @@
 				>
 			</File>
 			<File
+				RelativePath=".\gqb\gqbPrintout.cpp"
+				>
+			</File>
+			<File
 				RelativePath=".\gqb\gqbViewPanels.cpp"
 				>
 			</File>
diff --git a/xtra/wx-build/build-wxgtk b/xtra/wx-build/build-wxgtk
index f7c9e60..f046d7d 100755
--- a/xtra/wx-build/build-wxgtk
+++ b/xtra/wx-build/build-wxgtk
@@ -13,7 +13,7 @@ cd contrib
 make clean
 cd ..
 make clean
-./configure --with-gtk --enable-gtk2 --enable-unicode --enable-mimetype=no --enable-shared --enable-debug
+./configure --with-gtk --enable-gtk2 --enable-unicode --enable-mimetype=no --enable-shared --enable-debug --enable-printarch --with-gnomeprint
 make all
 make install
 cd contrib
@@ -24,7 +24,7 @@ make install
 make clean
 cd .. 
 make clean
-./configure --with-gtk --enable-gtk2 --enable-unicode --enable-mimetype=no --enable-shared --disable-debug
+./configure --with-gtk --enable-gtk2 --enable-unicode --enable-mimetype=no --enable-shared --disable-debug --enable-printarch --with-gnomeprint
 make all
 make install
 cd contrib
@@ -35,7 +35,7 @@ make install
 make clean
 cd ..
 make clean
-./configure --with-gtk --enable-gtk2 --enable-unicode --enable-mimetype=no --disable-shared --enable-debug
+./configure --with-gtk --enable-gtk2 --enable-unicode --enable-mimetype=no --disable-shared --enable-debug --enable-printarch --with-gnomeprint
 make all
 make install
 cd contrib
@@ -46,7 +46,7 @@ make install
 make clean
 cd ..
 make clean
-./configure --with-gtk --enable-gtk2 --enable-unicode --enable-mimetype=no --disable-shared --disable-debug
+./configure --with-gtk --enable-gtk2 --enable-unicode --enable-mimetype=no --disable-shared --disable-debug --enable-printarch --with-gnomeprint
 make all
 make install
 cd contrib
