1 /* -*- Mode: C; tab-width: 4 -*-
3 * Copyright (c) 1997-2004 Apple Computer, Inc. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #include "PrinterSetupWizardApp.h"
20 #include "PrinterSetupWizardSheet.h"
21 #include "SecondPage.h"
22 #include "DebugServices.h"
23 #include "WinServices.h"
26 // local variable is initialize but not referenced
27 #pragma warning(disable:4189)
31 IMPLEMENT_DYNAMIC(CSecondPage
, CPropertyPage
)
32 CSecondPage::CSecondPage()
33 : CPropertyPage(CSecondPage::IDD
)
35 m_psp
.dwFlags
&= ~(PSP_HASHELP
);
36 m_psp
.dwFlags
|= PSP_DEFAULT
|PSP_USEHEADERTITLE
|PSP_USEHEADERSUBTITLE
;
38 m_psp
.pszHeaderTitle
= MAKEINTRESOURCE(IDS_BROWSE_TITLE
);
39 m_psp
.pszHeaderSubTitle
= MAKEINTRESOURCE(IDS_BROWSE_SUBTITLE
);
41 m_emptyListItem
= NULL
;
42 m_initialized
= false;
47 CSecondPage::~CSecondPage()
53 CSecondPage::InitBrowseList()
55 CPrinterSetupWizardSheet
* psheet
;
58 psheet
= reinterpret_cast<CPrinterSetupWizardSheet
*>(GetParent());
59 require_quiet( psheet
, exit
);
61 // Initialize so that nothing is selected when we add to the list
63 psheet
->SetSelectedPrinter( NULL
);
65 m_browseList
.Select( NULL
, TVGN_FIRSTVISIBLE
);
68 // load the no printers message until something shows up in the browse list
70 text
.LoadString(IDS_NO_PRINTERS
);
72 LoadTextAndDisableWindow( text
);
75 // disable the next button until there's a printer to select
77 psheet
->SetWizardButtons(PSWIZB_BACK
);
80 // disable the printer information box
82 SetPrinterInformationState( FALSE
);
83 m_descriptionField
.SetWindowText( L
"" );
84 m_locationField
.SetWindowText( L
"" );
92 void CSecondPage::DoDataExchange(CDataExchange
* pDX
)
94 CPropertyPage::DoDataExchange(pDX
);
95 DDX_Control(pDX
, IDC_BROWSE_LIST
, m_browseList
);
96 DDX_Control(pDX
, IDC_PRINTER_INFORMATION
, m_printerInformation
);
98 DDX_Control(pDX
, IDC_DESCRIPTION_LABEL
, m_descriptionLabel
);
100 DDX_Control(pDX
, IDC_DESCRIPTION_FIELD
, m_descriptionField
);
102 DDX_Control(pDX
, IDC_LOCATION_LABEL
, m_locationLabel
);
104 DDX_Control(pDX
, IDC_LOCATION_FIELD
, m_locationField
);
110 CSecondPage::OnSetCursor(CWnd
* pWnd
, UINT nHitTest
, UINT message
)
113 DEBUG_UNUSED(nHitTest
);
114 DEBUG_UNUSED(message
);
116 CPrinterSetupWizardSheet
* psheet
;
118 psheet
= reinterpret_cast<CPrinterSetupWizardSheet
*>(GetParent());
119 require_quiet( psheet
, exit
);
121 SetCursor(psheet
->GetCursor());
130 CSecondPage::OnSetActive()
132 CPrinterSetupWizardSheet
* psheet
;
135 Printers::iterator it
;
136 OSStatus err
= kNoErr
;
139 b
= CPropertyPage::OnSetActive();
141 psheet
= reinterpret_cast<CPrinterSetupWizardSheet
*>(GetParent());
142 require_action( psheet
, exit
, err
= kUnknownErr
);
144 // Stash the selected printer if any
146 printer
= psheet
->GetSelectedPrinter();
148 // initialize the browse list...this will remove everything currently
149 // in it, and add the no printers item
153 // Populate the list with any printers that we currently know about
155 for ( it
= psheet
->m_printers
.begin(); it
!= psheet
->m_printers
.end(); it
++ )
157 OnAddPrinter( *it
, false );
160 if ( ( !printer
&& ( psheet
->m_printers
.size() > 0 ) ) || ( printer
!= psheet
->GetSelectedPrinter() ) )
164 printer
= psheet
->m_printers
.front();
167 psheet
->SetSelectedPrinter( printer
);
172 m_browseList
.SelectItem( printer
->item
);
173 ::SetFocus( m_browseList
);
176 // Hide the back button
177 pWnd
= ((CPropertySheet
*)GetParent())->GetDlgItem(ID_WIZBACK
);
180 pWnd
->ShowWindow(SW_HIDE
);
190 CSecondPage::OnKillActive()
192 CPrinterSetupWizardSheet
* psheet
;
195 psheet
= reinterpret_cast<CPrinterSetupWizardSheet
*>(GetParent());
196 require_quiet( psheet
, exit
);
198 psheet
->SetLastPage(this);
200 // Show the back button
201 pWnd
= ((CPropertySheet
*)GetParent())->GetDlgItem(ID_WIZBACK
);
204 pWnd
->ShowWindow(SW_SHOW
);
209 return CPropertyPage::OnKillActive();
213 BEGIN_MESSAGE_MAP(CSecondPage
, CPropertyPage
)
214 ON_NOTIFY(TVN_SELCHANGED
, IDC_BROWSE_LIST
, OnTvnSelchangedBrowseList
)
215 ON_NOTIFY(NM_CLICK
, IDC_BROWSE_LIST
, OnNmClickBrowseList
)
216 ON_NOTIFY(TVN_KEYDOWN
, IDC_BROWSE_LIST
, OnTvnKeyDownBrowseList
)
221 // Printer::EventHandler implementation
223 CSecondPage::OnAddPrinter(
227 CPrinterSetupWizardSheet
* psheet
;
228 Printer
* selectedPrinter
;
229 OSStatus err
= kNoErr
;
231 check( IsWindow( m_hWnd
) );
233 m_browseList
.SetRedraw(FALSE
);
235 psheet
= reinterpret_cast<CPrinterSetupWizardSheet
*>(GetParent());
236 require_quiet( psheet
, exit
);
240 selectedPrinter
= psheet
->GetSelectedPrinter();
242 printer
->item
= m_browseList
.InsertItem(printer
->displayName
);
244 m_browseList
.SetItemData( printer
->item
, (DWORD_PTR
) printer
);
246 m_browseList
.SortChildren(TVI_ROOT
);
249 // if the searching item is still in the list
252 // note that order is important here. Insert the printer
253 // item before removing the placeholder so we always have
254 // an item in the list to avoid experiencing the bug
255 // in Microsoft's implementation of CTreeCtrl
257 if (m_emptyListItem
!= NULL
)
259 m_browseList
.DeleteItem(m_emptyListItem
);
260 m_emptyListItem
= NULL
;
261 m_browseList
.EnableWindow(TRUE
);
264 if ( !selectedPrinter
)
266 psheet
->SetSelectedPrinter( printer
);
267 m_browseList
.SelectItem( printer
->item
);
268 ::SetFocus( m_browseList
);
276 m_browseList
.SetRedraw(TRUE
);
277 m_browseList
.Invalidate();
285 CSecondPage::OnRemovePrinter(
289 CPrinterSetupWizardSheet
* psheet
;
290 OSStatus err
= kNoErr
;
292 check( IsWindow( m_hWnd
) );
295 psheet
= reinterpret_cast<CPrinterSetupWizardSheet
*>(GetParent());
296 require_quiet( psheet
, exit
);
298 m_browseList
.SetRedraw(FALSE
);
303 // check to make sure if we're the only item in the control...i.e.
304 // the list size is 1.
306 if (m_browseList
.GetCount() > 1)
309 // if we're not the only thing in the list, then
310 // simply remove it from the list
312 m_browseList
.DeleteItem( printer
->item
);
317 // if we're the only thing in the list, then redisplay
318 // it with the no printers message
328 m_browseList
.SetRedraw(TRUE
);
329 m_browseList
.Invalidate();
337 CSecondPage::OnResolveService( Service
* service
)
339 CPrinterSetupWizardSheet
* psheet
= reinterpret_cast<CPrinterSetupWizardSheet
*>(GetParent());
340 require_quiet( psheet
, exit
);
344 Queue
* q
= service
->SelectedQueue();
350 // and set it to selected
353 m_selectedName
= service
->printer
->name
;
356 // and update the printer information box
358 SetPrinterInformationState( TRUE
);
360 m_descriptionField
.SetWindowText( q
->description
);
361 m_locationField
.SetWindowText( q
->location
);
367 SetCursor(psheet
->m_active
);
375 void CSecondPage::OnTvnSelchangedBrowseList(NMHDR
*pNMHDR
, LRESULT
*pResult
)
377 LPNMTREEVIEW pNMTreeView
= reinterpret_cast<LPNMTREEVIEW
>(pNMHDR
);
378 CPrinterSetupWizardSheet
* psheet
;
382 psheet
= reinterpret_cast<CPrinterSetupWizardSheet
*>(GetParent());
383 require_action( psheet
, exit
, err
= kUnknownErr
);
385 // The strange code here is to workaround a bug in the CTreeCtrl, whereupon the item
386 // we selected isn't passed through correctly to this callback routine.
390 printer
= psheet
->GetSelectedPrinter();
392 // If we really haven't selected a printer, then re-select NULL and exit
396 m_browseList
.SelectItem( NULL
);
401 // If we already have selected a printer, fake like we've clicked on it, but only
402 // if the CTreeCtrl hasn't already selected it
404 else if ( printer
->item
!= m_browseList
.GetSelectedItem() )
408 m_browseList
.SelectItem( printer
->item
);
414 HTREEITEM item
= m_browseList
.GetSelectedItem();
415 require_quiet( item
, exit
);
417 printer
= reinterpret_cast<Printer
*>(m_browseList
.GetItemData( item
) );
418 require_quiet( printer
, exit
);
421 // this call will trigger a resolve. When the resolve is complete,
422 // our OnResolve will be called.
424 err
= psheet
->StartResolve( printer
);
425 require_noerr( err
, exit
);
428 // And clear out the printer information box
430 SetPrinterInformationState( FALSE
);
431 m_descriptionField
.SetWindowText(L
"");
432 m_locationField
.SetWindowText(L
"");
441 text
.LoadString(IDS_ERROR_SELECTING_PRINTER_TEXT
);
442 caption
.LoadString(IDS_ERROR_SELECTING_PRINTER_CAPTION
);
444 MessageBox(text
, caption
, MB_OK
|MB_ICONEXCLAMATION
);
451 void CSecondPage::OnNmClickBrowseList(NMHDR
*pNMHDR
, LRESULT
*pResult
)
453 DEBUG_UNUSED( pNMHDR
);
461 void CSecondPage::OnTvnKeyDownBrowseList( NMHDR
* pNMHDR
, LRESULT
* pResult
)
463 DEBUG_UNUSED( pNMHDR
);
472 CSecondPage::LoadTextAndDisableWindow( CString
& text
)
474 m_emptyListItem
= m_browseList
.InsertItem( text
, 0, 0, NULL
, TVI_FIRST
);
475 m_browseList
.SelectItem( NULL
);
478 // this will remove everything else in the list...we might be navigating
479 // back to this window, and the browse list might have changed since
480 // we last displayed it.
482 if ( m_emptyListItem
)
484 HTREEITEM item
= m_browseList
.GetNextVisibleItem( m_emptyListItem
);
488 m_browseList
.DeleteItem( item
);
489 item
= m_browseList
.GetNextVisibleItem( m_emptyListItem
);
493 m_browseList
.EnableWindow( FALSE
);
498 CSecondPage::SetPrinterInformationState( BOOL state
)
500 m_printerInformation
.EnableWindow( state
);
502 m_descriptionLabel
.EnableWindow( state
);
504 m_descriptionField
.EnableWindow( state
);
506 m_locationLabel
.EnableWindow( state
);
508 m_locationField
.EnableWindow( state
);