example_sutoolbar.cpp

This is an example of how to use the SUToolBar class. It assumes there is a toolbar resource named IDR_TOOLBAR, with two buttons, ID_LOAD_TBBUTTON and ID_SAVE_TBBUTTON.

00001 #include <ShapeUpAPI.h>
00002 #include "./resource.h"
00003 
00004 SURESULT __cdecl HookFunc(SUHANDLE hWorkspace, int hookType, SUHookData *args);
00005 SURESULT __cdecl OnToolBarButton(SUHANDLE hWorkspace, SUToolBarButtonHookData* args);
00006 SURESULT __cdecl OnIdle(SUHANDLE hWorkspace, SUHookData *args);
00007 
00008 // Our toolbar
00009 SUToolBar toolbar;
00010 
00011 // Every plug-in needs an instance of SUExport called suapi
00012 SUExport suapi;
00013 
00014 size_t __cdecl fnVersion()
00015 {
00016     return SHAPEUP_API_VERSION;
00017 }
00018 
00019 const GUID* __cdecl fnGUID()
00020 {
00021     // CREATE YOUR OWN GUID !!
00022     // {97F4EA41-7D7E-42c1-B555-2D4C86C912A7}
00023     static const GUID guid = 
00024         { 0x97f4ea41, 0x7d7e, 0x42c1, { 0xb5, 0x55, 0x2d, 0x4c, 0x86, 0xc9, 0x12, 0xa7 } };
00025     return &guid;
00026 }
00027 
00028 SURESULT __cdecl fnInit(SUExport *pExport)
00029 {
00030     AFX_MANAGE_STATE(AfxGetStaticModuleState());
00031 
00032     pExport->import.pszName = "Test of GPI and ShapeUp API";
00033     pExport->import.pszDescription = "Test of GPI and ShapeUp API\nGPI Test";
00034     pExport->import.pszInfo = "Copyright (c) 2007";
00035     pExport->import.pszPluginVersion = "1.00";
00036 
00037     // Now keep the entire struct which is the actual ShapeUp API
00038     // (we need the function pointers later)
00039     ::memcpy(&suapi, pExport, sizeof(SUExport));
00040 
00041     // Set-up our hook callback
00042     pExport->import.pfnHook = HookFunc;
00043 
00044     // We want to get notified when the user presses our toolbar buttons
00045     ShapeUp_AddToolBarButtonHook(0);
00046     // The idle hook is used to set the state of toolbar buttons (disabled/checked/...)
00047     ShapeUp_AddIdleHook(0);
00048 
00049     // Create our toolbar
00050     toolbar.Create(AfxGetInstanceHandle(), IDR_TOOLBAR, "Test of GPI Toolbar");
00051 
00052     return SURESULT_OK;
00053 }
00054 
00055 // ShapeUp will call this function when an event occurs
00056 SURESULT __cdecl HookFunc(SUHANDLE hWorkspace, int hookType, SUHookData *args)
00057 {
00058     AFX_MANAGE_STATE(AfxGetStaticModuleState());
00059 
00060     switch (hookType)
00061     {
00062     case HOOKTYPE_TOOLBARBUTTON:
00063         return OnToolBarButton(hWorkspace, (SUToolBarButtonHookData*) args);
00064     case HOOKTYPE_IDLE:
00065         return OnIdle(hWorkspace, args);
00066     default:
00067         break;
00068     }
00069 
00070     return SURESULT_OK;
00071 }
00072 
00073 SURESULT __cdecl OnToolBarButton(SUHANDLE hWorkspace, SUToolBarButtonHookData* args)
00074 {
00075     try
00076     {
00077         switch (args->nCommandID)
00078         {
00079         case ID_LOAD_TBBUTTON:
00080             {
00081                 CFileDialog dlg(TRUE);
00082                 if (dlg.DoModal() == IDOK)
00083                 {
00084                 }
00085             }
00086             break;
00087         case ID_SAVE_TBBUTTON:
00088             {
00089                 CFileDialog dlg(FALSE);
00090                 if (dlg.DoModal() == IDOK)
00091                 {
00092                 }
00093             }
00094             break;
00095         default:
00096             break;
00097         }
00098     }
00099     catch (SUException &ex)
00100     {
00101         AfxMessageBox(ex.GetErrorDesc().c_str(), 0, 0);
00102     }
00103 
00104     return SURESULT_OK;
00105 }
00106 
00107 
00108 SURESULT __cdecl OnIdle(SUHANDLE hWorkspace, SUHookData *args)
00109 {
00110     try
00111     {
00112         if (hWorkspace != NULL)
00113         {
00114             SUWorkspace workspace(hWorkspace);
00115 
00116             bool bChecked = /* some condition */;
00117             bool bEnabled = /* some condition */;
00118 
00119             toolbar.SetButtonChecked(ID_SAVE_TBBUTTON, bChecked);
00120 
00121             toolbar.SetButtonEnabled(ID_LOAD_TBBUTTON, bEnabled);
00122             toolbar.SetButtonEnabled(ID_SAVE_TBBUTTON, bEnabled);
00123         }
00124         else
00125         {
00126             // If there's no active workspace, we disable the buttons
00127             toolbar.SetButtonEnabled(ID_LOAD_TBBUTTON, false);
00128             toolbar.SetButtonEnabled(ID_SAVE_TBBUTTON, false);
00129         }
00130     }
00131     catch (SUException &/*ex*/)
00132     {
00133         // Do NOT open any message boxes from here
00134     }
00135 
00136     return SURESULT_OK;
00137 }

Generated on Thu Apr 15 10:55:33 2010 for ShapeUp API by  doxygen 1.5.2