ofopenFile function

Hi guys

**what is the best way to display a pdf on windows?
**

On mac I used the ofVideoplayer object that uses the quicktime player basically to load the pdf.

What is the best alternative for PC?

If you try to open the pdf directly with quicktime under windows it doesn’t work either (on mac it does) so… it’s most likely something outside our control…

but wouldn’t it be sweet if we had a “ofPdf” object like we have an ofImage…

…Any ideas?

  • I havent tried ofxLibharu yet. Does it even work to display a pdf on screen?

To answer my own question, best way to display pdf on both windows and mac OF programs, is by not displaying it inside the program at all…

what I mean is that it makes more sense performance-wise and otherwise to just open the pdfs in separate windows either on preview or Acrobat instead,

I first tried to do that with ofSystem, but I wanted a more clean function that would just work identically on both platforms, grab the path of the files that I wanted and simply open them using the default pdf viewer of the user.

So I wrote a small function for this and I thought I might share it, case anybody else wants to do something similar

  
  
template <class T>  
void ofOpenFile(T path){  
  
#ifdef TARGET_WIN32  
  string t___ =  string(path);  
const char * path__ = t___.c_str();  
ShellExecute(NULL, _T("open"), _T(path__), NULL, NULL, SW_SHOW);  
  
#else  
  
                        string comnt__;  
                        comnt__ = "open " + string(path);  
                        system(comnt__.c_str());  
#endif  
  
  

}

(main difference between ofSystem is that instead of using popen ofOpenFile uses system and ShellExecute.)

you can paste it directly inside your OF ofUtils.cpp file or wherever else you want to. it doesn’t matter.

Not sure if there is already something similar in OF , (if so I apologize)
The idea is to just have a simple cross-platform clean function that would just open all files by just puting the path so the average OF user like me wouldn’t have to worry about the more complicated stuff…

some examples:

**ofOpenFile("/Users/kyri/Desktop/myfile.png");
**
or on PC :

ofOpenFile(“C:\Users\kyri\Desktop\myfile.pdf”);

*note that on pc you must put “\” when you mean “” if you use it statically.

if you are using it dynamically *from withing a getPath() function of another object like ofFileDialogResult() etc then you will be just fine no need to worry about / or \,

To make things more clear I’ve also prepared a sexy example for you guys :

TestApp.h:

  
  
#pragma once  
  
#include "ofMain.h"  
  
template <class T>  
void ofOpenFile(T path);  
  
  
  
extern ofImage * extern_img;  
  
class simple_button{  
public:  
    bool isActive_;  
    ofImage * image_;  
    string path;  
    simple_button(){  
        image_ = extern_img;  
    }  
      
    void draw(int x, int y, int w, int h){  
        //----get is button pressed--  
        float dist = ofDist(0,y,0,ofGetMouseY());  
        float dist2 = ofDist(x+(w/2), 0, ofGetMouseX(), 0);  
        if(dist<h+10&&dist2<w/2)isActive_=true;else isActive_ =false;  
          
        if(isActive_)ofSetColor(200); else ofSetColor(255);  
        ofEnableAlphaBlending();  
        image_->draw(x,y, w, h);  
        ofDisableAlphaBlending();  
          
          
          
    }  
      
};  
  
  
class testApp : public ofBaseApp{  
  
	public:  
      
    ~testApp();  
		void setup();  
		void update();  
		void draw();  
  
		void keyPressed  (int key);  
		void keyReleased(int key);  
		void mouseMoved(int x, int y );  
		void mouseDragged(int x, int y, int button);  
		void mousePressed(int x, int y, int button);  
		void mouseReleased(int x, int y, int button);  
		void windowResized(int w, int h);  
		void gotMessage(ofMessage msg);  
		void dragEvent(ofDragInfo dragInfo);  
			  
		      
      
    vector <ofFile> myDraggedFiles;  
    vector <simple_button> dummyFiles;  
  
};  
  

TestApp.cpp

  
  
#include "testApp.h"  
  
  
template <class T>  
void ofOpenFile(T path){  
  
#ifdef TARGET_WIN32  
  string t___ =  string(path);  
const char * path__ = t___.c_str();  
ShellExecute(NULL, _T("open"), _T(path__), NULL, NULL, SW_SHOW);  
  
#else  
  
                        string comnt__;  
                        comnt__ = "open " + string(path);  
                        system(comnt__.c_str());  
#endif  
  
  
}  
  
  
  
ofImage * extern_img = NULL;  
  
  
//--------------------------------------------------------------  
void testApp::setup(){  
  
    extern_img = new ofImage;  
    extern_img->loadImage("pdfsave.png");  
  
  
      
}  
  
//---------------------------------------------------------------  
  
testApp::~testApp(){  
  
if (extern_img != NULL)delete extern_img;  
  
}  
  
  
//-------------------------------------------------------------  
  
  
//--------------------------------------------------------------  
void testApp::update(){  
  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
  
    ofDrawBitmapString("Drag a File here!", 50,50);  
  
  
    for (int i=0; i<dummyFiles.size(); i++) {  
  
        dummyFiles[i].draw(100+(i*50), 100, 50, 60);  
  
  
    }  
  
  
}  
  
//--------------------------------------------------------------  
void testApp::keyPressed(int key){  
  
}  
  
//--------------------------------------------------------------  
void testApp::keyReleased(int key){  
  
}  
  
//--------------------------------------------------------------  
void testApp::mouseMoved(int x, int y ){  
  
}  
  
//--------------------------------------------------------------  
void testApp::mouseDragged(int x, int y, int button){  
  
}  
  
//--------------------------------------------------------------  
void testApp::mousePressed(int x, int y, int button){  
  
}  
  
//--------------------------------------------------------------  
void testApp::mouseReleased(int x, int y, int button){  
  
    for (int i=0; i<dummyFiles.size(); i++) {  
  
        if(dummyFiles[i].isActive_){  
  
            ofOpenFile(dummyFiles[i].path);  
  
        }  
  
  
    }  
  
}  
  
//--------------------------------------------------------------  
void testApp::windowResized(int w, int h){  
  
}  
  
//--------------------------------------------------------------  
void testApp::gotMessage(ofMessage msg){  
  
}  
  
//--------------------------------------------------------------  
void testApp::dragEvent(ofDragInfo info){  
	ofDirectory direc;  
  
    direc.createDirectory("PDFS/");  
  
  
    if( info.files.size() > 0 ){  
		myDraggedFiles.assign( info.files.size(), ofFile() );  
		for(int k = 0; k < info.files.size(); k++){  
            ofFile fi__;  
            fi__.open(info.files[k]);  
			myDraggedFiles[k].copyFromTo(info.files[k], "PDFS/");  
            simple_button butt_;  
            butt_.path = ofToDataPath("PDFS/"+fi__.getFileName(),true);  
            dummyFiles.push_back(butt_);  
  
		}  
	}  
  
}  
  
  
  

////////////////////////----------------------------------------------------------------------------

Just one question thought…

I think i’ve noticed a small bug while building the above example that ofFile::getFileName() doesn’t work when the file has a ’ ’ space in its name… (this bug is on Macintosh only , for once the PC version works as it should)

anybody else noticed that?

For example on what happens:

you have a file named: “file name.png” it will return just “name.png” as your file name and ignore everything else behind the space.

I tried to dig into OF core to see if its something easily solvable but I don’t think it is…

ofFile gets the getFileName by the path.h file in the poco library

//poco/1.4/Foundation/include/Poco/Path.h

so maybe is a bug within Poco or something…

maybe we need to change the way we get our filename in our ofFileUtills.cpp file

in the string ofFilePath::getFileName(string filePath, bool bRelativeToData); function…

not sure If this is the right topic or place to ramble about this, but anyway…

yes this is probably a bug in Poco, but can you post an issue anyway:

http://github.com/openframeworks/openFrameworks/issues

hi arturo Thanks for the feedback, I was about to do that, I am trying to browse through the 555+ open issues and see if something similar exists already…

too many bugs too little time… :f

no worries i don’t remember such an issue but perhaps do a quick search and post if you can’t find anything, no need to read all the issues :slight_smile: