Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

CtrlHomeSearch.cpp

Go to the documentation of this file.
00001 //
00002 // CtrlHomeSearch.cpp
00003 //
00004 // Copyright (c) Shareaza Development Team, 2002-2005.
00005 // This file is part of SHAREAZA (www.shareaza.com)
00006 //
00007 // Shareaza is free software; you can redistribute it
00008 // and/or modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2 of
00010 // the License, or (at your option) any later version.
00011 //
00012 // Shareaza is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with Shareaza; if not, write to the Free Software
00019 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 //
00021 
00022 #include "StdAfx.h"
00023 #include "Shareaza.h"
00024 #include "Settings.h"
00025 #include "Schema.h"
00026 #include "SchemaCache.h"
00027 #include "QuerySearch.h"
00028 #include "CoolInterface.h"
00029 #include "CtrlHomeSearch.h"
00030 #include "DlgNewSearch.h"
00031 #include "Skin.h"
00032 #include "DlgHelp.h"
00033 #include "Security.h"
00034 
00035 #ifdef _DEBUG
00036 #define new DEBUG_NEW
00037 #undef THIS_FILE
00038 static char THIS_FILE[] = __FILE__;
00039 #endif
00040 
00041 BEGIN_MESSAGE_MAP(CHomeSearchCtrl, CWnd)
00042         //{{AFX_MSG_MAP(CHomeSearchCtrl)
00043         ON_WM_CREATE()
00044         ON_WM_SIZE()
00045         ON_WM_PAINT()
00046         ON_CBN_CLOSEUP(IDC_SEARCH_TEXT, OnCloseUpText)
00047         ON_BN_CLICKED(IDC_SEARCH_CREATE, OnSearchCreate)
00048         ON_BN_CLICKED(IDC_SEARCH_ADVANCED, OnSearchAdvanced)
00049         ON_WM_SETFOCUS()
00050         //}}AFX_MSG_MAP
00051 END_MESSAGE_MAP()
00052 
00053 
00055 // CHomeSearchCtrl construction
00056 
00057 CHomeSearchCtrl::CHomeSearchCtrl()
00058 {
00059         m_pTextInput = NULL;
00060 }
00061 
00062 CHomeSearchCtrl::~CHomeSearchCtrl()
00063 {
00064 }
00065 
00067 // CHomeSearchCtrl message handlers
00068 
00069 BOOL CHomeSearchCtrl::PreTranslateMessage(MSG* pMsg)
00070 {
00071         if ( pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN )
00072         {
00073                 PostMessage( WM_COMMAND, MAKELONG( IDC_SEARCH_CREATE, BN_CLICKED ), (LPARAM)m_wndSearch.GetSafeHwnd() );
00074                 return TRUE;
00075         }
00076         else if ( pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_TAB )
00077         {
00078                 if ( m_pTextInput == &m_wndText && m_wndText.GetWindowTextLength() > 0 )
00079                 {
00080                         m_wndSchema.SetFocus();
00081                         m_pTextInput = &m_wndSchema;
00082                         return TRUE;
00083                 }
00084                 else
00085                 {
00086                         m_wndText.SetFocus();
00087                         m_pTextInput = &m_wndText;
00088                         return TRUE;
00089                 }
00090         }
00091 
00092         return CWnd::PreTranslateMessage( pMsg );
00093 }
00094 
00095 BOOL CHomeSearchCtrl::Create(CWnd* pParentWnd, UINT nID)
00096 {
00097         CRect rect( 0, 0, 0, 0 );
00098         return CWnd::Create( NULL, NULL, WS_CHILD|WS_CLIPCHILDREN, rect, pParentWnd, nID );
00099 }
00100 
00101 int CHomeSearchCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
00102 {
00103         if ( CWnd::OnCreate( lpCreateStruct ) == -1 ) return -1;
00104 
00105         CRect rc( 0, 0, 0, 0 );
00106 
00107         if ( ! m_wndText.Create( WS_CHILD|WS_VISIBLE|WS_TABSTOP|WS_GROUP|CBS_AUTOHSCROLL|CBS_DROPDOWN,
00108                 rc, this, IDC_SEARCH_TEXT ) ) return -1;
00109 
00110         m_wndText.SetFont( &theApp.m_gdiFont );
00111 
00112         if ( ! m_wndSchema.Create( WS_CHILD|WS_VISIBLE|WS_TABSTOP, rc, this, IDC_SEARCH_SCHEMAS ) )
00113                 return -1;
00114 
00115         m_wndSchema.SetDroppedWidth( 200 );
00116         LoadString( m_wndSchema.m_sNoSchemaText, IDS_SEARCH_PANEL_AFT );
00117         m_wndSchema.Load( Settings.Search.LastSchemaURI );
00118 
00119         m_wndSearch.Create( rc, this, IDC_SEARCH_CREATE );
00120         m_wndSearch.SetHandCursor( TRUE );
00121         m_wndAdvanced.Create( rc, this, IDC_SEARCH_ADVANCED );
00122         m_wndAdvanced.SetHandCursor( TRUE );
00123 
00124         Setup( CoolInterface.m_crWindow );
00125 
00126         return 0;
00127 }
00128 
00129 void CHomeSearchCtrl::Setup(COLORREF crWindow)
00130 {
00131         CString strCaption;
00132 
00133         m_crWindow = crWindow;
00134 
00135         LoadString( strCaption, IDS_SEARCH_PANEL_START );
00136         m_wndSearch.SetWindowText( strCaption );
00137         HICON hIcon = CoolInterface.ExtractIcon( ID_SEARCH_SEARCH );
00138         m_wndSearch.SetIcon( theApp.m_bRTL ? CreateMirroredIcon( hIcon ) : hIcon );
00139 
00140         LoadString( strCaption, IDS_SEARCH_PANEL_ADVANCED );
00141         m_wndAdvanced.SetWindowText( strCaption + _T('\x2026') );
00142         hIcon = CoolInterface.ExtractIcon( ID_SEARCH_DETAILS );
00143         m_wndAdvanced.SetIcon( theApp.m_bRTL ? CreateMirroredIcon( hIcon ) : hIcon );
00144 
00145         LoadString( m_wndSchema.m_sNoSchemaText, IDS_SEARCH_PANEL_AFT );
00146 
00147         FillHistory();
00148 }
00149 
00150 void CHomeSearchCtrl::FillHistory()
00151 {
00152         int nCount = theApp.GetProfileInt( _T("Search"), _T("Recent.Count"), 0 );
00153 
00154         m_wndText.ResetContent();
00155 
00156         for ( int nItem = 0 ; nItem < nCount ; nItem++ )
00157         {
00158                 CString strEntry;
00159                 strEntry.Format( _T("Recent.%.2i.Text"), nItem + 1 );
00160                 int nIndex = m_wndText.InsertString( 0, theApp.GetProfileString( _T("Search"), strEntry ) );
00161                 strEntry.Format( _T("Recent.%.2i.SchemaURI"), nItem + 1 );
00162                 CSchema* pSchema = SchemaCache.Get( theApp.GetProfileString( _T("Search"), strEntry ) );
00163                 m_wndText.SetItemData( nIndex, (DWORD)pSchema );
00164         }
00165 
00166         CString strClear;
00167         LoadString( strClear, IDS_SEARCH_PAD_CLEAR_HISTORY );
00168         m_wndText.SetItemData( m_wndText.AddString( strClear ), 0 );
00169 }
00170 
00171 void CHomeSearchCtrl::OnSize(UINT nType, int cx, int cy)
00172 {
00173         CWnd::OnSize( nType, cx, cy );
00174 
00175         CRect rcClient( 0, 0, cx, cy );
00176         CRect rcItem;
00177 
00178         rcClient.DeflateRect( 1, 1 );
00179 
00180         rcClient.top += 18;
00181         rcItem.SetRect( rcClient.left, rcClient.top, rcClient.right - 104, rcClient.top + 256 );
00182         m_wndText.MoveWindow( &rcItem );
00183 
00184         rcItem.SetRect( rcClient.right - 92, rcClient.top - 2, rcClient.right, rcClient.top + 22 );
00185         m_wndSearch.MoveWindow( &rcItem );
00186 
00187         rcClient.top += 32;
00188 
00189         rcItem.SetRect( rcClient.right - 104 - 160, rcClient.top, rcClient.right - 104, rcClient.top + 256 );
00190         rcItem.left = max( rcItem.left, rcClient.left );
00191         m_wndSchema.MoveWindow( &rcItem );
00192 
00193         rcItem.SetRect( rcClient.right - 92, rcClient.top, rcClient.right, rcClient.top + 24 );
00194         m_wndAdvanced.MoveWindow( &rcItem );
00195 }
00196 
00197 void CHomeSearchCtrl::OnPaint()
00198 {
00199         CRect rcClient, rcItem;
00200         CPaintDC dc( this );
00201         CString str;
00202 
00203         GetClientRect( &rcClient );
00204         rcClient.DeflateRect( 1, 1 );
00205 
00206         CFont* pOldFont = (CFont*)dc.SelectObject( &CoolInterface.m_fntBold );
00207         dc.SetBkMode( OPAQUE );
00208         dc.SetBkColor( m_crWindow );
00209         dc.SetTextColor( 0 );
00210 
00211         LoadString( str, IDS_SEARCH_PAD_WORDS );
00212 
00213         rcItem.SetRect( rcClient.left, rcClient.top, rcClient.right, rcClient.top + 16 );
00214         dc.ExtTextOut( rcItem.left + 2, rcItem.top + 2, ETO_CLIPPED|ETO_OPAQUE, &rcItem, str, NULL );
00215         dc.ExcludeClipRect( &rcItem );
00216 
00217         rcClient.top += 18;
00218         rcClient.top += 32;
00219 
00220         rcItem.SetRect( rcClient.left, rcClient.top,
00221                 rcClient.right - 104 - 160 - 8, rcClient.top + 22 );
00222 
00223         LoadString( str, IDS_SEARCH_PAD_TYPE );
00224         CSize sz = dc.GetTextExtent( str );
00225         dc.ExtTextOut( rcItem.right - sz.cx, ( rcItem.top + rcItem.bottom ) / 2 - sz.cy / 2,
00226                 ETO_CLIPPED|ETO_OPAQUE, &rcItem, str, NULL );
00227         dc.ExcludeClipRect( &rcItem );
00228 
00229         dc.SelectObject( pOldFont );
00230         GetClientRect( &rcClient );
00231         dc.FillSolidRect( &rcClient, m_crWindow );
00232 }
00233 
00234 void CHomeSearchCtrl::OnCloseUpText()
00235 {
00236         int nSel = m_wndText.GetCurSel();
00237         if ( nSel < 0 ) return;
00238 
00239         if ( nSel == m_wndText.GetCount() - 1 )
00240         {
00241                 m_wndText.SetWindowText( _T("") );
00242                 theApp.WriteProfileInt( _T("Search"), _T("Recent.Count"), 0 );
00243                 FillHistory();
00244         }
00245         else
00246         {
00247                 m_wndSchema.Select( (CSchema*)m_wndText.GetItemData( nSel ) );
00248         }
00249 }
00250 
00251 void CHomeSearchCtrl::OnSearchCreate()
00252 {
00253         CString strText, strURI, strEntry, strClear;
00254 
00255         m_wndText.GetWindowText( strText );
00256         strText.TrimLeft();
00257         strText.TrimRight();
00258         if ( strText.IsEmpty() ) return;
00259 
00260         LoadString( strClear, IDS_SEARCH_PAD_CLEAR_HISTORY );
00261         if ( _tcscmp ( strClear , strText ) == 0 ) return;
00262 
00263         CSchema* pSchema = m_wndSchema.GetSelected();
00264         if ( pSchema != NULL ) strURI = pSchema->m_sURI;
00265 
00266         Settings.Search.LastSchemaURI = strURI;
00267 
00268         int nCount = theApp.GetProfileInt( _T("Search"), _T("Recent.Count"), 0 );
00269 
00270     int nItem = 0;
00271         for ( ; nItem < nCount ; nItem++ )
00272         {
00273                 strEntry.Format( _T("Recent.%.2i.Text"), nItem + 1 );
00274 
00275                 if ( strText.CompareNoCase( theApp.GetProfileString( _T("Search"), strEntry ) ) == 0 )
00276                 {
00277                         strEntry.Format( _T("Recent.%.2i.SchemaURI"), nItem + 1 );
00278                         theApp.WriteProfileString( _T("Search"), strEntry, strURI );
00279                         break;
00280                 }
00281         }
00282 
00283         if ( nItem >= nCount )
00284         {
00285                 theApp.WriteProfileInt( _T("Search"), _T("Recent.Count"), ++nCount );
00286                 strEntry.Format( _T("Recent.%.2i.Text"), nItem + 1 );
00287                 theApp.WriteProfileString( _T("Search"), strEntry, strText );
00288                 strEntry.Format( _T("Recent.%.2i.SchemaURI"), nItem + 1 );
00289                 theApp.WriteProfileString( _T("Search"), strEntry, strURI );
00290                 m_wndText.SetItemData( m_wndText.InsertString( 0, strText ), (DWORD)pSchema );
00291         }
00292 
00293         CQuerySearch* pSearch   = new CQuerySearch();
00294         pSearch->m_sSearch              = strText;
00295         pSearch->m_pSchema              = pSchema;
00296 
00297         if ( AdultFilter.IsSearchFiltered( pSearch->m_sSearch ) )
00298         {                                                               //Adult search blocked, open help window
00299                 CHelpDlg::Show( _T("SearchHelp.AdultSearch") );
00300         }
00301         else if ( NULL == pSearch->OpenWindow() )
00302         {                                                               //Invalid search, open help window
00303                 CHelpDlg::Show( _T("SearchHelp.BadSearch") );
00304                 delete pSearch;
00305         }
00306 
00307 
00308 
00309         m_wndText.SetWindowText( _T("") );
00310 }
00311 
00312 void CHomeSearchCtrl::OnSearchAdvanced()
00313 {
00314         /*
00315         CNewSearchDlg dlg;
00316 
00317         if ( dlg.DoModal() == IDOK )
00318         {
00319                 if ( CQuerySearch* pSearch = dlg.GetSearch() )
00320                 {
00321                         if ( NULL == pSearch->OpenWindow() ) delete pSearch;
00322                 }
00323         }
00324         */
00325         AfxGetMainWnd()->PostMessage( WM_COMMAND, ID_TAB_SEARCH );
00326 }
00327 
00328 void CHomeSearchCtrl::OnSetFocus(CWnd* pOldWnd)
00329 {
00330         CWnd::OnSetFocus( pOldWnd );
00331         //m_wndText.SetFocus();
00332 }

Generated on Thu Dec 15 10:39:35 2005 for Shareaza 2.2.1.0 by  doxygen 1.4.2