13.07.2015 Views

A PowerBuilder Revolution - sys-con.com's archive of magazines ...

A PowerBuilder Revolution - sys-con.com's archive of magazines ...

A PowerBuilder Revolution - sys-con.com's archive of magazines ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The first thing we do is examine the value<strong>of</strong> the nCode argument that was passedto us. If the value <strong>of</strong> that argument is lessthan 0, we are being instructed by theWindows API that we need to exit ourown callback without further processingand allow the next callback in line to havethe message and then return back thevalue returned by that callback.if ( nCode < 0 ){DWORD lResult ;lResult = CallNextHookEx ( hHook,nCode, wParam, lParam ) ;return lResult ;}Otherwise, we declare a structure toreceive information about exactly whatthe dialog was doing when we were calledand cast the lParam argument into thatstructure. Once we have that, we check tosee if the message the dialog received wasthe CB_ADDSTRING message (the messagesent by the original creator <strong>of</strong> thedialog to add an item to a dropdown listbox). If it was, we also check to see if them_filetypes array is populated. That’sbecause the developer may not havecalled the FilterFileTypes method; theymay simply be using the SetDefaultFile-Type to set the default.CWPRETSTRUCT * cwpretStruct = (CWPRET-STRUCT*)(lParam);if ( ( cwpretStruct->message ==CB_ADDSTRING ) && ( m_filetypes ) ){If m_filetypes is populated, we loopthrough those file types and determine ifthe file type that was just added is one wewant to filter:pArrayItemCount = m_pSession->GetArrayLength(m_filetypes);for( i=1; i GetStringArrayItem( m_filetypes, dim, bIsNull );lpszFileType = (LPSTR)m_pSession->Get-String(pbFileType) ;if ( strcmp ( (LPCTSTR) cwpretStruct->lParam, lpszFileType ) == 0 )If so, we get a handle to the newlyadded dropdown item by sending aCB_FINDSTRINGEXACT message to thedropdown list box:lResult = SendMessage( (HWND)cwpretStruct->hwnd, (UINT)CB_FINDSTRINGEXACT,(WPARAM)0, (LPARAM)lpszFileType );Once we have that, we send aCB_DELETESTRING message to thedropdown list box to remove the itemfrom the list:lResult = SendMessage ( (HWND)cwpret-Struct->hwnd, (UINT)CB_DELETESTRING,(WPARAM)lResult, (LPARAM)0 ) ;If the message wasn’t the CB_ASD-STRING, we check to see if it was theCB_SETCURSEL (the message that is firedafter all the items have been added andthe dialog creator sets the default value).If it is that message and our m_defaultmember has been populated (it may nothave been), we use the CB_FINDSTRINGmessage to find the position within thedropdown list <strong>of</strong> the file type we want tomake the default:“Once you’ve gota hook to thedialog, there’sreally no end tothe customizationthat you can do”if ( ( cwpretStruct->message ==CB_SETCURSEL ) && ( m_default ) ){lResult = SendMessage( (HWND)cwpretStruct->hwnd, (UINT)CB_FINDSTRING, (WPARAM)0,(LPARAM)m_default );Once we have that, we send our ownCB_SETCURSEL message to set thedefault to that index value.lResult = SendMessage ( (HWND)cwpret-Struct->hwnd, (UINT)CB_SETCURSEL,(WPARAM)lResult, (LPARAM)0 ) ;But wait! That’s going to cause thecallback to fire again and we’ll get anotherCB_SETCURSEL message. We couldend up with recursion, except that we’realso testing the value <strong>of</strong> the index that’sbeing passed in the original messagebefore we fire our message. If the defaultvalue that’s being set is the value we want,we don’t send our message, so we don’tget recursion.Calling in from <strong>PowerBuilder</strong>What does it look like when you needto use it? Fortunately, it’s a lot simplerthan it was to create it:customsaveaslnv_saveasstring ls_filetypes[]ls_filetypes[1] = 'XML'ls_filetypes[2] = 'PDF'ls_filetypes[3] = 'XSL-FO'lnv_saveas = CREATE customsaveaslnv_saveas.register ( Handle ( parent ) )lnv_saveas.filterfiletypes (ls_filetypes )lnv_saveas.setdefaultfiletype ( 'Excel5with headers' )dw_1.SaveAs ( )Destroy lnv_saveasHere we’ve declared a local instance<strong>of</strong> the extension. We don’t want to usean instance variable or anything with alarger scope, because we’re relying onthe destructor <strong>of</strong> the object to unregisterthe hook. Otherwise we would needto add an additional Unregistermethod.We then create an instance <strong>of</strong> theextension, call the register method passingin the handle <strong>of</strong> the window that isabout the make the call, and then calleither the filterfiletypes method, the setdefaultfiletypemethod, or (as in this case)both methods. Once we’ve done that, wecall the SaveAs method <strong>of</strong> the DataWindow<strong>con</strong>trol. Immediately after theSaveAs call, we destroy the local instance<strong>of</strong> the extension.What happens at runtime is thatwhile the dialog is being created,<strong>PowerBuilder</strong> sends CB_ADDSTRINGmessages to the dialog to add file typesto the dropdown list box. As the dialogfinishes with each <strong>of</strong> those messages,our callback method is called and –when necessary – the file type is deletedfrom the list. Similarly, after Power-Builder has finished adding all <strong>of</strong> its filetypes, it sends the CB_SETCURSELmessage to set the default file type to“Text with headers,” at which point ourcallback method is called again and weset the default file type to “Excel 5 withheaders” instead. The end result is thatthe DDLB in the SaveAs dialog nolonger has those file types and thatExcel5 with headers is the new default(see Figure 1).Note: If you do use the FilterFileTypesmethod, you’ll generally want to call theSetDefaultFileType method as well, evenif you don’t want to change the defaultthat <strong>PowerBuilder</strong> initially sets. The issueis that <strong>PowerBuilder</strong> attempts to set thedefault file type by specifying the indexposition it thinks that file type has. Since26PBDJ volume12 issue4www.SYS-CON.COM/pbdj/

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!