www.gusucode.com > rmidemos工具箱matlab源码程序 > rmidemos/powerwin_reqs/rmidemo_pp_linktype_step7.m

    function linkType = rmidemo_pp_linktype

    linkType = ReqMgr.LinkType;
    linkType.Registration = mfilename;
    linkType.Version = '';  % not used

    % Label describing this link type
    linkType.Label = 'Microsoft Power Point';

    % File information
    linkType.IsFile = 1;
    linkType.Extensions = {'.ppt', '.pptx'};

    % Location delimiters
    linkType.LocDelimiters = '#@';

    % Supported methods
    linkType.NavigateFcn = @NavigateFcn;
    linkType.ContentsFcn = @ContentsFcn;
    
    % For selection-based linking
    linkType.SelectionLinkLabel = 'Link to Slide in PowerPoint';
    linkType.SelectionLinkFcn = @SelectionLinkFcn;    
end

function NavigateFcn(filePath, locationStr)
    if isempty(locationStr)
        disp(['Navigating to "' filePath '"']);
    else
        disp(['Navigating to "' locationStr '" in "' filePath '"']);
    end
    hDoc = openFile(filePath);
    if isempty(hDoc)
        disp(['Failed to open ' filePath]);
    else
        if ~isempty(locationStr)
            switch(locationStr(1))
                case '#'
                    slideNum = str2num(locationStr(2:end)); %#ok<ST2NM>
                    goToSlide(hDoc, slideNum);
                case '@' 
                    goToItem(hDoc, locationStr(2:end));
                otherwise
                    warning('Invalid location specifier: %s', locationStr);
            end
        end
        % try to bring the window forward
        [~, fileName, ext] = fileparts(filePath);
        reqmgt('winFocus', [fileName ext]); % locate by window title match
    end
end

function [labels, depths, locations] = ContentsFcn(filePath)
    hDoc = openFile(filePath);
    slidecount = hDoc.Slides.Count;
    if slidecount == 0
        labels = {'no slides in presentation!'};
        locations = {''};
        depths = 0;
    else
        labels = cell(slidecount,1);
        locations = cell(slidecount,1);
        depths = zeros(slidecount,1);
    end
    for k = 1:slidecount    
        try
            txt = hDoc.Slides.Item(k).Shapes.Item(1).TextFrame.TextRange.Text;
            txt(txt < 32) = ' ';  % avoid control characters in labels
        catch  
            txt = '[slide with no title]';
        end
        labels{k} = strtrim(txt);
        locations{k} = ['@' num2str(hDoc.Slides.Item(k).SlideID)];
    end
end

function reqstruct = SelectionLinkFcn(objH, make2way, varargin)
    reqstruct=[];
    
    hDoc = getCurrentDoc();
    if isempty(hDoc)
        errordlg('PowerPoint presentation is not open', 'Linking Failed');
        return;
    end
    
    % PPT file we are linking with:
    docName = hDoc.Name;   % or should we use .FullName ??
    
    % PPT slide we are linking with:
    currentSlide = hDoc.Application.ActiveWindow.Selection.Slide;
    
    [objName, objType] = rmi.objname(objH);
    disp(['Linking ' objName ' (' objType ') to ' currentSlide.Name ' in ' docName]);
     
    % Add a new requirement
    reqstruct = rmi('createempty');
    reqstruct.reqsys = mfilename;
    reqstruct.doc = docName;
    reqstruct.id = sprintf('@%d', currentSlide.SlideId);
    
    % Label for navigation shortcut
    try
        txt = currentSlide.Shapes.Item(1).TextFrame.TextRange.Text;
        txt(txt < 32) = ' ';  % avoid control characters in labels
    catch
        txt = '[slide with no title]';
    end
    if length(txt) > 40
        reqstruct.description = [txt(1:40) '...'];
    else
        reqstruct.description = txt;
    end
    
    if make2way
        % Insert a Simulink RMI icon with navigation hyperlink: 
        slPicture = [matlabroot '\toolbox\shared\reqmgt\icons\mwlink.bmp'];
        currentSlide.Shapes.AddPicture(slPicture, 'msoTrue', 'msoTrue', 20, 20);
        myNewShape = currentSlide.Shapes.Item(currentSlide.Shapes.Count);
        myNewShape.ActionSettings.Item(1).Hyperlink.Address = rmi.getURL(objH);
        myNewShape.ActionSettings.Item(1).Hyperlink.ScreenTip = ['Linked from ' objName ' (' objType ')'];
    end
end

function hDoc = getCurrentDoc()
    hDoc = [];
    try
        % try to connecto to already running application
        hApp = actxGetRunningServer('powerpoint.application');
        hDoc = hApp.ActivePresentation;
    catch
        return;  % PowerPoint is not running
    end
end

function goToItem(hDoc, itemIdStr)
    disp(['Opening item ID ' itemIdStr]);
    itemId = str2num(itemIdStr); %#ok<ST2NM>
    hDoc.Slides.FindBySlideID(itemId).Select;
end

function goToSlide(hDoc, slideNum)
    disp(['Opening slide #' num2str(slideNum)]);
    hDoc.Slides.Item(slideNum).Select;
end

function hDoc = openFile(fileName)
    disp(['Checking for ' fileName]);
    hApp = getApplication();
    hDoc = findOpenDoc(hApp, fileName);
    if isempty(hDoc)
        try
            hDoc = hApp.Presentations.Open(fileName);
        catch
            hDoc = [];
        end
    end
end

function hApp = getApplication()
    try
        % try to connecto to already running application
        hApp = actxGetRunningServer('powerpoint.application');
    catch
        % start-up PowerPoint and connect to it
        hApp = actxserver('powerpoint.application');
    end
end

function hDoc = findOpenDoc(hApp, fileName)
    hDoc = [];
    hDocs = hApp.Presentations;
    for i = 1:hDocs.Count
        if strcmp(hDocs.Item(i).FullName, fileName)
            hDoc = hDocs.Item(i);
            return;
        end
    end
end