Native Libraries: moved code to JNIUtils.cpp and *MessageDialog.cpp

This commit is contained in:
Karl Tauber
2025-01-14 16:29:27 +01:00
parent 078e59a443
commit 07fc190b5f
10 changed files with 405 additions and 189 deletions

View File

@@ -0,0 +1,67 @@
/*
* Copyright 2024 FormDev Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// avoid inlining of printf()
#define _NO_CRT_STDIO_INLINE
#include "JNIUtils.h"
/**
* @author Karl Tauber
*/
//---- class AutoReleaseString ------------------------------------------------
AutoReleaseString::AutoReleaseString( JNIEnv* _env, jstring _javaString ) {
env = _env;
javaString = _javaString;
chars = (javaString != NULL) ? env->GetStringChars( javaString, NULL ) : NULL;
}
AutoReleaseString::~AutoReleaseString() {
if( chars != NULL )
env->ReleaseStringChars( javaString, chars );
}
//---- class AutoReleaseStringArray -------------------------------------------
AutoReleaseStringArray::AutoReleaseStringArray( JNIEnv* _env, jobjectArray _javaStringArray ) {
env = _env;
count = (_javaStringArray != NULL) ? env->GetArrayLength( _javaStringArray ) : 0;
if( count <= 0 )
return;
javaStringArray = new jstring[count];
charsArray = new const jchar*[count];
for( int i = 0; i < count; i++ ) {
javaStringArray[i] = (jstring) env->GetObjectArrayElement( _javaStringArray, i );
charsArray[i] = env->GetStringChars( javaStringArray[i] , NULL );
}
}
AutoReleaseStringArray::~AutoReleaseStringArray() {
if( count == 0 )
return;
for( int i = 0; i < count; i++ ) {
env->ReleaseStringChars( javaStringArray[i], charsArray[i] );
env->DeleteLocalRef( javaStringArray[i] );
}
delete[] javaStringArray;
delete[] charsArray;
}

View File

@@ -19,6 +19,7 @@
#include <windows.h>
#include <shobjidl.h>
#include "JNIUtils.h"
#include "com_formdev_flatlaf_ui_FlatNativeWindowsLibrary.h"
/**
@@ -54,26 +55,6 @@ public:
operator T*() { return ptr; }
};
//---- class AutoReleaseString ------------------------------------------------
class AutoReleaseString {
JNIEnv* env;
jstring javaString;
const jchar* chars;
public:
AutoReleaseString( JNIEnv* _env, jstring _javaString ) {
env = _env;
javaString = _javaString;
chars = (javaString != NULL) ? env->GetStringChars( javaString, NULL ) : NULL;
}
~AutoReleaseString() {
if( chars != NULL )
env->ReleaseStringChars( javaString, chars );
}
operator LPCWSTR() { return (LPCWSTR) chars; }
};
//---- class AutoReleaseIShellItem --------------------------------------------
class AutoReleaseIShellItem : public AutoReleasePtr<IShellItem> {
@@ -87,49 +68,30 @@ public:
//---- class FilterSpec -------------------------------------------------------
class FilterSpec {
JNIEnv* env = NULL;
jstring* jnames = NULL;
jstring* jspecs = NULL;
AutoReleaseStringArray fileTypes;
public:
UINT count = 0;
COMDLG_FILTERSPEC* specs = NULL;
public:
FilterSpec( JNIEnv* _env, jobjectArray fileTypes ) {
if( fileTypes == NULL )
FilterSpec( JNIEnv* _env, jobjectArray _fileTypes )
: fileTypes( _env, _fileTypes )
{
if( fileTypes.count == 0 )
return;
env = _env;
count = env->GetArrayLength( fileTypes ) / 2;
if( count <= 0 )
return;
specs = new COMDLG_FILTERSPEC[count];
jnames = new jstring[count];
jspecs = new jstring[count];
count = fileTypes.count / 2;
specs = new COMDLG_FILTERSPEC[fileTypes.count];
for( int i = 0; i < count; i++ ) {
jnames[i] = (jstring) env->GetObjectArrayElement( fileTypes, i * 2 );
jspecs[i] = (jstring) env->GetObjectArrayElement( fileTypes, (i * 2) + 1 );
specs[i].pszName = (LPCWSTR) env->GetStringChars( jnames[i] , NULL );
specs[i].pszSpec = (LPCWSTR) env->GetStringChars( jspecs[i], NULL );
specs[i].pszName = fileTypes[i * 2];
specs[i].pszSpec = fileTypes[(i * 2) + 1];
}
}
~FilterSpec() {
if( specs == NULL )
return;
for( int i = 0; i < count; i++ ) {
env->ReleaseStringChars( jnames[i], (jchar *) specs[i].pszName );
env->ReleaseStringChars( jspecs[i], (jchar *) specs[i].pszSpec );
env->DeleteLocalRef( jnames[i] );
env->DeleteLocalRef( jspecs[i] );
}
delete[] jnames;
delete[] jspecs;
delete[] specs;
if( specs != NULL )
delete[] specs;
}
};
@@ -369,15 +331,3 @@ static jobjectArray getFiles( JNIEnv* env, jboolean open, IFileDialog* dialog )
return array;
}
}
extern "C"
JNIEXPORT jint JNICALL Java_com_formdev_flatlaf_ui_FlatNativeWindowsLibrary_showMessageDialog
( JNIEnv* env, jclass cls, jlong hwndParent, jstring text, jstring caption, jint type )
{
// convert Java strings to C strings
AutoReleaseString ctext( env, text );
AutoReleaseString ccaption( env, caption );
return ::MessageBox( reinterpret_cast<HWND>( hwndParent ), ctext, ccaption, type );
}

View File

@@ -0,0 +1,38 @@
/*
* Copyright 2024 FormDev Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// avoid inlining of printf()
#define _NO_CRT_STDIO_INLINE
#include <windows.h>
#include "JNIUtils.h"
#include "com_formdev_flatlaf_ui_FlatNativeWindowsLibrary.h"
/**
* @author Karl Tauber
* @since 3.6
*/
extern "C"
JNIEXPORT jint JNICALL Java_com_formdev_flatlaf_ui_FlatNativeWindowsLibrary_showMessageDialog
( JNIEnv* env, jclass cls, jlong hwndParent, jstring text, jstring caption, jint type )
{
// convert Java strings to C strings
AutoReleaseString ctext( env, text );
AutoReleaseString ccaption( env, caption );
return ::MessageBox( reinterpret_cast<HWND>( hwndParent ), ctext, ccaption, type );
}

View File

@@ -0,0 +1,53 @@
/*
* Copyright 2025 FormDev Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <windows.h>
#include <jni.h>
/**
* @author Karl Tauber
*/
//---- class AutoReleaseString ------------------------------------------------
class AutoReleaseString {
JNIEnv* env;
jstring javaString;
const jchar* chars;
public:
AutoReleaseString( JNIEnv* _env, jstring _javaString );
~AutoReleaseString();
operator LPCWSTR() { return (LPCWSTR) chars; }
};
//---- class AutoReleaseStringArray -------------------------------------------
class AutoReleaseStringArray {
JNIEnv* env;
jstring* javaStringArray;
const jchar** charsArray;
public:
UINT count;
public:
AutoReleaseStringArray( JNIEnv* _env, jobjectArray _javaStringArray );
~AutoReleaseStringArray();
operator LPCWSTR*() { return (LPCWSTR*) charsArray; }
};