Add Windows initial code
This commit is contained in:
parent
7f299cf69b
commit
e7c8a9780d
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,2 +1,4 @@
|
||||||
/target
|
/target
|
||||||
Cargo.lock
|
Cargo.lock
|
||||||
|
.idea
|
||||||
|
*.iml
|
||||||
|
|
|
@ -7,3 +7,7 @@ edition = "2018"
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
winapi = { version = "0.3.8", features = ["libloaderapi", "winuser", "windef", "minwindef"] }
|
||||||
|
|
||||||
|
[[example]]
|
||||||
|
name = "windows_demo"
|
21
examples/windows_demo.rs
Normal file
21
examples/windows_demo.rs
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
use std::ptr::null_mut;
|
||||||
|
|
||||||
|
use baseview::{WindowOpenOptions, create_window, handle_msg};
|
||||||
|
use baseview::Parent;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let window = WindowOpenOptions {
|
||||||
|
title: "Baseview",
|
||||||
|
width: 800,
|
||||||
|
height: 400,
|
||||||
|
parent: Parent::None
|
||||||
|
};
|
||||||
|
|
||||||
|
create_window(window);
|
||||||
|
|
||||||
|
loop {
|
||||||
|
if !handle_msg(null_mut()) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,9 +1,13 @@
|
||||||
|
mod win;
|
||||||
|
|
||||||
|
pub use win::*;
|
||||||
|
|
||||||
use std::ffi::c_void;
|
use std::ffi::c_void;
|
||||||
|
|
||||||
pub enum Parent {
|
pub enum Parent {
|
||||||
None,
|
None,
|
||||||
AsIfParented,
|
AsIfParented,
|
||||||
WithParent(*mut c_void)
|
WithParent(*mut c_void),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct WindowOpenOptions<'a> {
|
pub struct WindowOpenOptions<'a> {
|
||||||
|
@ -12,5 +16,5 @@ pub struct WindowOpenOptions<'a> {
|
||||||
pub width: usize,
|
pub width: usize,
|
||||||
pub height: usize,
|
pub height: usize,
|
||||||
|
|
||||||
pub parent: Parent
|
pub parent: Parent,
|
||||||
}
|
}
|
||||||
|
|
3
src/win/mod.rs
Normal file
3
src/win/mod.rs
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
mod window;
|
||||||
|
|
||||||
|
pub use window::*;
|
83
src/win/window.rs
Normal file
83
src/win/window.rs
Normal file
|
@ -0,0 +1,83 @@
|
||||||
|
extern crate winapi;
|
||||||
|
|
||||||
|
use self::winapi::_core::mem::MaybeUninit;
|
||||||
|
use self::winapi::shared::minwindef::{LPARAM, LPVOID, LRESULT, UINT, WPARAM};
|
||||||
|
use self::winapi::shared::windef::{HBRUSH, HICON, HMENU, HWND};
|
||||||
|
use self::winapi::um::libloaderapi::GetModuleHandleA;
|
||||||
|
use self::winapi::um::winuser::{
|
||||||
|
CreateWindowExA, DefWindowProcA, DispatchMessageA, PeekMessageA, PostQuitMessage,
|
||||||
|
RegisterClassA, TranslateMessage, CS_HREDRAW, CS_OWNDC, CS_VREDRAW, CW_USEDEFAULT, MSG,
|
||||||
|
PM_REMOVE, WM_DESTROY, WM_QUIT, WNDCLASSA, WS_OVERLAPPEDWINDOW, WS_VISIBLE,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::WindowOpenOptions;
|
||||||
|
|
||||||
|
pub fn handle_msg(_window: HWND) -> bool {
|
||||||
|
unsafe {
|
||||||
|
let mut msg: MSG = MaybeUninit::uninit().assume_init();
|
||||||
|
loop {
|
||||||
|
if PeekMessageA(&mut msg, 0 as HWND, 0, 0, PM_REMOVE) == 0 {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if msg.message == WM_QUIT {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
TranslateMessage(&msg);
|
||||||
|
DispatchMessageA(&msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub unsafe extern "system" fn wnd_proc(
|
||||||
|
hwnd: HWND,
|
||||||
|
msg: UINT,
|
||||||
|
w_param: WPARAM,
|
||||||
|
l_param: LPARAM,
|
||||||
|
) -> LRESULT {
|
||||||
|
match msg {
|
||||||
|
WM_DESTROY => {
|
||||||
|
PostQuitMessage(0);
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
return DefWindowProcA(hwnd, msg, w_param, l_param);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn create_window(w: WindowOpenOptions) {
|
||||||
|
unsafe {
|
||||||
|
let hinstance = GetModuleHandleA(0 as *const i8);
|
||||||
|
let wnd_class = WNDCLASSA {
|
||||||
|
// todo: for OpenGL, will use it later
|
||||||
|
style: CS_OWNDC | CS_HREDRAW | CS_VREDRAW,
|
||||||
|
lpfnWndProc: Some(wnd_proc),
|
||||||
|
hInstance: hinstance,
|
||||||
|
lpszClassName: "BaseviewClass\0".as_ptr() as *const i8,
|
||||||
|
cbClsExtra: 0,
|
||||||
|
cbWndExtra: 0,
|
||||||
|
hIcon: 0 as HICON,
|
||||||
|
hCursor: 0 as HICON,
|
||||||
|
hbrBackground: 0 as HBRUSH,
|
||||||
|
lpszMenuName: 0 as *const i8,
|
||||||
|
};
|
||||||
|
RegisterClassA(&wnd_class);
|
||||||
|
|
||||||
|
let _hwnd = CreateWindowExA(
|
||||||
|
0,
|
||||||
|
"BaseviewClass\0".as_ptr() as *const i8,
|
||||||
|
"Baseview\0".as_ptr() as *const i8,
|
||||||
|
// todo: fine for now, will have to change with a parent
|
||||||
|
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
|
||||||
|
CW_USEDEFAULT,
|
||||||
|
CW_USEDEFAULT,
|
||||||
|
// todo: check if usize fits into i32
|
||||||
|
w.width as i32,
|
||||||
|
w.height as i32,
|
||||||
|
0 as HWND,
|
||||||
|
0 as HMENU,
|
||||||
|
hinstance,
|
||||||
|
0 as LPVOID,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue