Skip to content

Commit 87a9967

Browse files
authored
add setIcon function for Windows (#95 #126)
1 parent 2e145d7 commit 87a9967

File tree

8 files changed

+41
-0
lines changed

8 files changed

+41
-0
lines changed

examples/java/org/jetbrains/jwm/examples/Example.java

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import java.util.concurrent.*;
88
import java.util.function.*;
99
import java.util.stream.*;
10+
import java.io.File;
11+
1012

1113
public class Example implements Consumer<Event> {
1214
public static int PADDING = 10;
@@ -66,6 +68,7 @@ public Example() {
6668
case 4 -> window.setWindowPosition(bounds.getLeft() + bounds.getWidth() / 2, bounds.getTop() + bounds.getHeight() / 2);
6769
}
6870
window.setTitle("JWM Window #" + count);
71+
window.setIcon(new File("sample.ico"));
6972
window.setMouseCursor(MouseCursor.ARROW);
7073
window.show();
7174
window.requestFrame();

linux/java/org/jetbrains/jwm/WindowX11.java

+6
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ public Window setMouseCursor(MouseCursor cursor) {
8282
return this;
8383
}
8484

85+
@Override
86+
public Window setIcon(File icon) {
87+
// TODO #95
88+
return this;
89+
}
90+
8591
@Override
8692
public Screen getScreen() {
8793
assert _onUIThread();

macos/java/org/jetbrains/jwm/WindowMac.java

+5
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ public Window setTitle(String title) {
7272
}
7373

7474
@Override
75+
public Window setIcon(File icon) {
76+
// TODO #95
77+
return this;
78+
}
79+
7580
public Window setMouseCursor(MouseCursor cursor) {
7681
assert _onUIThread();
7782
// TODO: impl me!

sample.ico

382 KB
Binary file not shown.

shared/java/org/jetbrains/jwm/Window.java

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.function.*;
55
import org.jetbrains.annotations.*;
66
import org.jetbrains.jwm.impl.*;
7+
import java.io.File;
78

89
public abstract class Window extends RefCounted {
910
@ApiStatus.Internal
@@ -54,6 +55,7 @@ public UIRect getContentRectAbsolute() {
5455
public abstract Window setTitle(String title);
5556

5657
@NotNull @Contract("-> this")
58+
public abstract Window setIcon(File icon);
5759
public abstract Window setMouseCursor(MouseCursor cursor);
5860

5961
public abstract Screen getScreen();

windows/cc/WindowWin32.cc

+14
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ void jwm::WindowWin32::setTitle(const std::wstring& title) {
6868
SetWindowTextW(_hWnd, title.c_str());
6969
}
7070

71+
void jwm::WindowWin32::setIcon(const std::wstring& iconPath) {
72+
JWM_VERBOSE("Set window icon '" << iconPath << "'");
73+
// width / height of 0 along with LR_DEFAULTSIZE tells windows to load the default icon size.
74+
HICON hicon = (HICON)LoadImage(NULL, iconPath.c_str(), IMAGE_ICON, 0, 0, LR_LOADFROMFILE | LR_DEFAULTSIZE);
75+
SendMessage(_hWnd, WM_SETICON, ICON_SMALL, (LPARAM)hicon);
76+
}
7177
void jwm::WindowWin32::setMouseCursor(MouseCursor cursor) {
7278
JWM_VERBOSE("Set window cursor '" << mouseCursorToStr(cursor) << "'");
7379

@@ -842,6 +848,14 @@ extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_jwm_WindowWin32__1nSetTitle
842848
env->ReleaseStringChars(title, titleStr);
843849
}
844850

851+
extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_jwm_WindowWin32__1nSetIcon
852+
(JNIEnv* env, jobject obj, jstring iconPath) {
853+
jwm::WindowWin32* instance = reinterpret_cast<jwm::WindowWin32*>(jwm::classes::Native::fromJava(env, obj));
854+
const jchar* iconPathStr = env->GetStringChars(iconPath, nullptr);
855+
jsize length = env->GetStringLength(iconPath);
856+
instance->setIcon(std::wstring(reinterpret_cast<const wchar_t*>(iconPathStr), length));
857+
env->ReleaseStringChars(iconPath, iconPathStr);
858+
}
845859
extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_jwm_WindowWin32__1nSetMouseCursor
846860
(JNIEnv* env, jobject obj, jint cursorId) {
847861
jwm::WindowWin32* instance = reinterpret_cast<jwm::WindowWin32*>(jwm::classes::Native::fromJava(env, obj));

windows/cc/WindowWin32.hh

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ namespace jwm {
5151
void unmarkText();
5252
void setImeEnabled(bool enabled);
5353
void setTitle(const std::wstring& title);
54+
void setIcon(const std::wstring& iconPath);
5455
void setMouseCursor(MouseCursor cursor);
5556
void show();
5657
void requestSwap();

windows/java/org/jetbrains/jwm/WindowWin32.java

+10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.jetbrains.annotations.*;
77
import org.jetbrains.jwm.*;
88
import org.jetbrains.jwm.impl.*;
9+
import java.io.File;
910

1011
public class WindowWin32 extends Window {
1112
@ApiStatus.Internal
@@ -72,6 +73,14 @@ public Window setTitle(String title) {
7273
return this;
7374
}
7475

76+
77+
@Override
78+
public Window setIcon(File icon){
79+
assert _onUIThread();
80+
_nSetIcon(icon.getAbsolutePath().toString());
81+
return this;
82+
}
83+
7584
@Override
7685
public Window setMouseCursor(MouseCursor cursor) {
7786
assert _onUIThread();
@@ -123,6 +132,7 @@ public void close() {
123132
@ApiStatus.Internal public native void _nSetWindowSize(int width, int height);
124133
@ApiStatus.Internal public native void _nSetContentSize(int width, int height);
125134
@ApiStatus.Internal public native void _nSetTitle(String title);
135+
@ApiStatus.Internal public native void _nSetIcon(String iconPath);
126136
@ApiStatus.Internal public native void _nSetMouseCursor(int cursorId);
127137
@ApiStatus.Internal public native Screen _nGetScreen();
128138
@ApiStatus.Internal public native void _nRequestFrame();

0 commit comments

Comments
 (0)