Prechádzať zdrojové kódy

Adding android/ios OpenURL

Mark Sibly 8 rokov pred
rodič
commit
8b098a181d

+ 0 - 248
modules/std/requesters/native/requesters.cpp

@@ -1,248 +0,0 @@
-
-#include "requesters.h"
-
-#if _WIN32
-
-#include <windows.h>
-#include <shlobj.h>
-
-namespace{
-
-	HWND focHwnd;
-
-	void beginPanel(){
-		focHwnd=GetFocus();
-	}
-
-	void endPanel(){
-		SetFocus( focHwnd );
-	}
-
-	int panel( bbString title,bbString text,int flags ){
-		beginPanel();
-		int n=MessageBoxW( GetActiveWindow(),bbWString( text ),bbWString( title ),flags );
-		endPanel();
-		return n;
-	}
-	
-	WCHAR *tmpWString( bbString str ){
-		WCHAR *p=(WCHAR*)malloc( str.length()*2+2 );
-		memcpy( p,str.data(),str.length()*2 );
-		p[str.length()]=0;
-		return p;
-	}
-	
-	int CALLBACK BrowseForFolderCallbackW( HWND hwnd,UINT uMsg,LPARAM lp,LPARAM pData ){
-		wchar_t szPath[MAX_PATH];
-		switch( uMsg ){
-		case BFFM_INITIALIZED:
-			SendMessageW( hwnd,BFFM_SETSELECTIONW,TRUE,pData );
-			break;
-		case BFFM_SELCHANGED: 
-			if( SHGetPathFromIDListW( (LPITEMIDLIST)lp,szPath ) ){
-				SendMessageW( hwnd,BFFM_SETSTATUSTEXTW,0,(LPARAM)szPath );
-			}
-			break;
-		}
-		return 0;
-	}
-	
-	int CALLBACK BrowseForFolderCallbackA( HWND hwnd,UINT uMsg,LPARAM lp,LPARAM pData ){
-		char szPath[MAX_PATH];
-		switch( uMsg ){
-		case BFFM_INITIALIZED:
-			SendMessageA( hwnd,BFFM_SETSELECTIONA,TRUE,pData );
-			break;
-		case BFFM_SELCHANGED: 
-			if( SHGetPathFromIDListA( (LPITEMIDLIST)lp,szPath ) ){
-				SendMessageA( hwnd,BFFM_SETSTATUSTEXTA,0,(LPARAM)szPath );
-			}
-			break;
-		}
-		return 0;
-	}
-}
-	
-void bbRequesters::Notify( bbString title,bbString text,bbBool serious ){
-	int flags=(serious ? MB_ICONWARNING : MB_ICONINFORMATION)|MB_OK|MB_APPLMODAL|MB_TOPMOST;
-	panel( title,text,flags );
-}
-
-bbBool bbRequesters::Confirm( bbString title,bbString text,bbBool serious ){
-	int flags=(serious ? MB_ICONWARNING : MB_ICONINFORMATION)|MB_OKCANCEL|MB_APPLMODAL|MB_TOPMOST;
-	int n=panel( title,text,flags );
-	if( n==IDOK ) return 1;
-	return 0;
-}
-
-int bbRequesters::Proceed( bbString title,bbString text,bbBool serious ){
-	int flags=(serious ? MB_ICONWARNING : MB_ICONINFORMATION)|MB_YESNOCANCEL|MB_APPLMODAL|MB_TOPMOST;
-	int n=panel( title,text,flags );
-	if( n==IDYES ) return 1;
-	if( n==IDNO ) return 0;
-	return -1;
-}
-
-bbString bbRequesters::RequestFile( bbString title,bbString exts,bbBool save,bbString path ){
-
-	bbString file,dir;
-	path=path.replace( "/","\\" );
-		
-	int i=path.findLast( "\\" );
-	if( i!=-1 ){
-		dir=path.slice( 0,i );
-		file=path.slice( i+1 );
-	}else{
-		file=path;
-	}
-
-	if( file.length()>MAX_PATH ) return "";
-
-	if( exts.length() ){
-		if( exts.find( ":" )==-1 ){
-			exts=bbString( "Files\0*.",8 )+exts;
-		}else{
-			exts=exts.replace( ":",bbString( "\0*.",3 ) );
-		}
-		exts=exts.replace( ";",bbString( "\0",1 ) );
-		exts=exts.replace( ",",";*." )+bbString( "\0",1 );
-	}
-
-	WCHAR buf[MAX_PATH+1];
-	memcpy( buf,file.data(),file.length()*2 );
-	buf[file.length()]=0;
-
-	OPENFILENAMEW of={sizeof(of)};
-
-	of.hwndOwner=GetActiveWindow();
-	of.lpstrTitle=tmpWString( title );
-	of.lpstrFilter=tmpWString( exts );
-	of.lpstrFile=buf;
-	of.lpstrInitialDir=dir.length() ? tmpWString( dir ) : 0;
-	of.nMaxFile=MAX_PATH;
-	of.Flags=OFN_HIDEREADONLY|OFN_NOCHANGEDIR;
-	
-	bbString str;
-	
-	beginPanel();
-	
-	if( save ){
-		of.lpstrDefExt=L"";
-		of.Flags|=OFN_OVERWRITEPROMPT;
-		if( GetSaveFileNameW( &of ) ){
-			str=bbString( buf );
-		}
-	}else{
-		of.Flags|=OFN_FILEMUSTEXIST;
-		if( GetOpenFileNameW( &of ) ){
-			str=bbString( buf );
-		}
-	}
-	
-	endPanel();
-	
-	free( (void*)of.lpstrTitle );
-	free( (void*)of.lpstrFilter );
-	free( (void*)of.lpstrInitialDir );
-	
-	str=str.replace( "\\","/" );
-	
-	return str;
-}
-
-bbString bbRequesters::RequestDir( bbString title,bbString dir ){
-
-	CoInitialize( 0 );
-	
-	dir=dir.replace( "/","\\" );
-
-	LPMALLOC shm;
-	BROWSEINFOW bi={0};
-	
-	WCHAR buf[MAX_PATH],*p;
-	GetFullPathNameW( bbWString( dir ),MAX_PATH,buf,&p );
-	
-	bi.hwndOwner=GetActiveWindow();
-	bi.lpszTitle=tmpWString( title );
-	bi.ulFlags=BIF_RETURNONLYFSDIRS|BIF_NEWDIALOGSTYLE;
-	bi.lpfn=BrowseForFolderCallbackW;
-	bi.lParam=(LPARAM)buf;
-	
-	beginPanel();
-
-	bbString str;
-	
-	if( ITEMIDLIST *idlist=SHBrowseForFolderW( &bi ) ){
-	
-		SHGetPathFromIDListW( idlist,buf );
-		str=bbString( buf );
-		
-		//SHFree( idlist );	//?!?
-		
-		str=str.replace( "\\","/" );
-		if( !str.endsWith( "/" ) ) str+="/";
-	}
-	
-	endPanel();
-	
-	free( (void*)bi.lpszTitle );
-
-	return str;
-}
-
-void bbRequesters::OpenUrl( bbString url ){
-
-	CoInitializeEx( NULL,COINIT_APARTMENTTHREADED|COINIT_DISABLE_OLE1DDE );
-	
-	ShellExecute( HWND_DESKTOP,0,url.c_str(),0,0,SW_SHOWNORMAL );
-//	ShellExecute( HWND_DESKTOP,"open",url.c_str(),0,0,SW_SHOWNORMAL );
-}
-
-#elif __linux
-
-#include <limits.h>
-
-bbString bbRequesters::RequestFile( bbString title,bbString exts,bbBool save,bbString path ){
-
-	bbString cmd=BB_T("zenity --title=\"")+title+BB_T("\" --file-selection");
-	
-	if( save ) cmd+=" --save";
-	
-	FILE *f=popen( cmd.c_str(),"r" );
-	if( !f ) return "";
-	
-	char buf[PATH_MAX];
-	int n=fread( buf,1,PATH_MAX,f );
-	pclose( f );
-	
-	if( n<0 || n>PATH_MAX ) return "";
-	
-	while( n && buf[n-1]<=32 ) --n;
-	
-	return bbString::fromCString( buf,n );
-}
-
-bbString bbRequesters::RequestDir( bbString title,bbString dir ){
-
-	bbString cmd=BB_T("zenity --title=\"")+title+BB_T("\" --file-selection --directory");
-
-	FILE *f=popen( cmd.c_str(),"r" );
-	if( !f ) return "";
-	
-	char buf[PATH_MAX];
-	int n=fread( buf,1,PATH_MAX,f );
-	pclose( f );
-	
-	if( n<0 || n>PATH_MAX ) return "";
-	
-	while( n && buf[n-1]<=32 ) --n;
-	
-	return bbString::fromCString( buf,n );
-}
-
-void bbRequesters::OpenUrl( bbString url ){
-
-	system( ( bbString( "xdg-open \"" )+url+"\"" ).c_str() );
-}
-
-#endif

+ 0 - 245
modules/std/requesters/native/requesters.mm

@@ -1,245 +0,0 @@
-
-#include "requesters.h"
-
-#import <Cocoa/Cocoa.h>
-
-namespace{
-
-	typedef int (*AlertPanel)( 
-		NSString *title,
-		NSString *msg,
-		NSString *defaultButton,
-		NSString *alternateButton,
-		NSString *otherButton );
-	
-	NSWindow *keyWin;
-	NSOpenGLContext *glContext;
-	
-	void beginPanel(){
-	
-		glContext=[NSOpenGLContext currentContext];
-
-		keyWin=[NSApp keyWindow];
-	}
-	
-	void endPanel(){
-
-		if( glContext ) [glContext makeCurrentContext];
-
-		if( keyWin ) [keyWin makeKeyWindow];
-	}
-	
-	NSString *ConvString( bbString str ){
-		return [NSString stringWithCharacters:(const unichar*)str.data() length:str.length()];
-	}
-	
-	bbString ConvString( NSString *str ){
-		int n=[str length];
-		unichar *buf=new unichar[ n ];
-		[str getCharacters:buf range:NSMakeRange( 0,n )];
-		bbString t=bbString( buf,n );
-		delete[] buf;
-		return t;
-	}
-}
-
-void bbRequesters::Notify( bbString title,bbString text,bbBool serious ){
-
-	AlertPanel panel=(AlertPanel) ( serious ? (void*)NSRunCriticalAlertPanel : (void*)NSRunAlertPanel );
-	
-	beginPanel();
-	
-	panel( ConvString( title ),ConvString( text ),@"OK",0,0 );
-	
-	endPanel();
-}
-
-bbBool bbRequesters::Confirm( bbString title,bbString text,bbBool serious ){
-
-	AlertPanel panel=(AlertPanel) ( serious ? (void*)NSRunCriticalAlertPanel : (void*)NSRunAlertPanel );
-	
-	beginPanel();
-	
-	int n=panel( ConvString( title ),ConvString( text ),@"OK",@"Cancel",0 );
-
-	endPanel();
-	
-	switch( n ){
-	case NSAlertDefaultReturn:return 1;
-	}
-	return 0;
-}
-
-int bbRequesters::Proceed( bbString title,bbString text,bbBool serious ){
-
-	AlertPanel panel=(AlertPanel) ( serious ? (void*)NSRunCriticalAlertPanel : (void*)NSRunAlertPanel );
-	
-	beginPanel();
-	
-	int n=panel( ConvString( title ),ConvString( text ),@"Yes",@"No",@"Cancel" );
-	
-	endPanel();
-	
-	switch( n ){
-	case NSAlertDefaultReturn:return 1;
-	case NSAlertAlternateReturn:return 0;
-	}
-	return -1;
-}
-
-bbString bbRequesters::RequestFile( bbString title,bbString filter,bbBool save,bbString path ){
-
-	bbString file,dir;
-	int i=path.findLast( "\\" );
-	if( i!=-1 ){
-		dir=path.slice( 0,i );
-		file=path.slice( 1+1 );
-	}else{
-		file=path;
-	}
-	
-	NSMutableArray *nsfilter=0;
-	bool allowOthers=true;
-
-	if( filter.length() ){
-	
-		nsfilter=[NSMutableArray arrayWithCapacity:10];
-		
-		allowOthers=false;
-		
-		if( filter.find( ":" )!=-1 ){
-
-			int i0=0;
-			while( i0<filter.length() ){
-			
-				int i1=filter.find( ":",i0 );
-				if( i1==-1 ) break;
-				i1+=1;
-				
-				int i2=filter.find( ";",i1 );
-				if( i2==-1 ) i2=filter.length();
-				
-				while( i1<i2 ){
-				
-					int i3=filter.find( ",",i1 );
-					if( i3==-1 ) i3=i2;
-					
-					bbString ext=filter.slice( i1,i3 );
-					if( ext==BB_T("*") ){
-						allowOthers=true;
-					}else{
-						[nsfilter addObject:ConvString( ext )];
-					}
-					i1=i3+1;
-				}
-				i0=i2+1;
-			}
-			
-		}else{
-		
-			int i0=0;
-			while( i0<filter.length() ){
-			
-				int i1=filter.find( ",",i0 );
-				if( i1==-1 ) i1=filter.length();
-				
-				bbString ext=filter.slice( i0,i1 );
-				if( ext==BB_T("*") ){
-					allowOthers=true;
-				}else{
-					[nsfilter addObject:ConvString( ext )];
-				}
-				
-				i0=i1+1;
-			}
-		}
-	}
-
-	if( ![nsfilter count] ){
-		nsfilter=0;
-		allowOthers=true;
-	}
-	
-	NSString *nsdir=0;
-	NSString *nsfile=0;
-	NSString *nstitle=0;
-	NSMutableArray *nsexts=0;
-
-	if( dir.length() ) nsdir=ConvString( dir );
-	if( file.length() ) nsfile=ConvString( file );
-	if( title.length() ) nstitle=ConvString( title );
-
-	beginPanel();
-	
-	bbString str;
-
-	if( save ){
-		NSSavePanel *panel=[NSSavePanel savePanel];
-		
-		if( nstitle ) [panel setTitle:nstitle];
-		
-		if( nsfilter ){
-			[panel setAllowedFileTypes:nsfilter];
-			[panel setAllowsOtherFileTypes:allowOthers];
-		}
-		
-		if( [panel runModalForDirectory:nsdir file:nsfile]==NSFileHandlingPanelOKButton ){
-			str=ConvString( [panel filename] );
-		}
-
-	}else{
-		NSOpenPanel *panel=[NSOpenPanel openPanel];
-
-		if( nstitle ) [panel setTitle:nstitle];
-		
-		if( allowOthers ) nsfilter=0;
-		
-		if( [panel runModalForDirectory:nsdir file:nsfile types:nsfilter]==NSFileHandlingPanelOKButton ){
-			str=ConvString( [panel filename] );
-		}
-	}
-
-	endPanel();
-
-	return str;
-}
-
-bbString bbRequesters::RequestDir( bbString title,bbString dir ){
-
-	NSString *nsdir=0;
-	NSString *nstitle=0;
-	NSOpenPanel *panel;
-	
-	if( dir.length() ) nsdir=ConvString( dir );
-	if( title.length() ) nstitle=ConvString( title );
-
-	panel=[NSOpenPanel openPanel];
-	
-	[panel setCanChooseFiles:NO];
-	[panel setCanChooseDirectories:YES];
-	[panel setCanCreateDirectories:YES];
-	
-	if( nstitle ) [panel setTitle:nstitle];
-
-	beginPanel();
-	
-	bbString str;
-	
-	if( [panel runModalForDirectory:nsdir file:0 types:0]==NSFileHandlingPanelOKButton ){
-	
-		str=ConvString( [panel filename] );
-	}
-
-	endPanel();
-	
-	return str;
-}
-
-void bbRequesters::OpenUrl( bbString url ){
-
-	if( CFURLRef cfurl=CFURLCreateWithBytes( 0,(const UInt8*)url.c_str(),url.length(),kCFStringEncodingASCII,0 ) ){
-		LSOpenCFURLRef( cfurl,0 );
-		CFRelease( cfurl );
-	}
-}
-

+ 41 - 40
modules/std/requesters/requesters.monkey2

@@ -1,26 +1,38 @@
 
 Namespace std.requesters
 
-#If __DESKTOP_TARGET__
+#If __TARGET__="windows"
 
-#Import "native/requesters.h"
+	#Import "<libole32.a>"
+	#Import "<libComdlg32.a>"
+	
+	#Import "native/requesters_windows.cpp"
+	#Import "native/requesters.h"
 
-#If __TARGET__="macos"
+#Elseif __TARGET__="macos"
 
-	#Import "native/requesters.mm"
+	#Import "native/requesters_macos.mm"
+	#Import "native/requesters.h"
 
-#Else
+#Elseif __TARGET__="linux"
 
-	#Import "native/requesters.cpp"
-	
-	#if __TARGET__="windows"
+	#Import "native/requesters_linux.cpp"
+	#Import "native/requesters.h"
 
-		#Import "<libole32.a>"
-		#Import "<libComdlg32.a>"
+#Elseif __TARGET__="android"
 
-	#endif
-	
-#endif
+	#Import "<sdl2>"
+	#Import "<jni>"
+	#Import "native/Monkey2Requesters.java"
+
+#Elseif __TARGET__="ios"
+
+	#Import "native/requesters_ios.mm"
+	#Import "native/requesters.h"
+
+#Endif
+
+#If __DESKTOP_TARGET__
 
 Extern
 
@@ -82,38 +94,27 @@ The behavior of OpenURL is highly target dependent, but in general it should at
 #end
 Function OpenUrl( url:String )="bbRequesters::OpenUrl"
 
-#else
-
-#rem
-
-#rem monkeydoc @hidden
-#end
-Function Notify:Void( title:String,text:String,serious:Bool=False )
-End
+Public
 
-#rem monkeydoc @hidden
-#end
-Function Confirm:Bool( title:String,text:String,serious:Bool=False )
-	Return False
-End
+#Elseif __TARGET__="android"
 
-#rem monkeydoc @hidden
-#end
-Function RequestFile:String( title:String,filter:String="",save:Bool=False,file:String="" )
-	Return ""
+Function OpenUrl( url:String )
+	
+	Local env:=sdl2.Android_JNI_GetEnv()	
+	
+	Local cls:=env.FindClass( "com/monkey2/lib/Monkey2Requesters" )
+	
+	Local mth:=env.GetStaticMethodID( cls,"openUrl","(Ljava/lang/String;)V" )
+	
+	env.CallStaticVoidMethod( cls,mth,New Variant[]( url ) )
 End
 
-#rem monkeydoc @hidden
-#end
-Function RequestDir:String( title:String,dir:String="" )
-	Return ""
-End
+#Elseif __TARGET__="ios"
 
-#rem monkeydoc @hidden
-#end
-Function OpenUrl( url:String )
-End
+Extern
 
-#end
+Function OpenUrl( url:String )="bbRequesters::OpenUrl"
+	
+Public
 
 #Endif