From 7ee98e22ee114b35621db155a253df5966171021 Mon Sep 17 00:00:00 2001 From: MoAlyousef Date: Mon, 10 Jul 2023 04:04:15 +0300 Subject: [PATCH] update --- README.md | 5 ++- build.zig | 31 ++++++++------- src/box.zig | 14 +------ src/browser.zig | 101 ++++++++++++++++++----------------------------- src/button.zig | 8 ---- src/menu.zig | 11 ++++++ src/table.zig | 10 +++++ src/tree.zig | 5 +++ src/valuator.zig | 15 +++++++ src/widget.zig | 54 ++++++++++++++++++++++++- src/window.zig | 16 ++++---- src/zfltk.zig | 5 +-- 12 files changed, 162 insertions(+), 113 deletions(-) diff --git a/README.md b/README.md index 39aae72..37ff3f2 100644 --- a/README.md +++ b/README.md @@ -23,12 +23,13 @@ If you're using the official package manager: .version = "0.0.1", .dependencies = .{ .zfltk = .{ - .url = "https://github.com/MoAlyousef/zfltk/archive/refs/tags/pkg0.0.6.tar.gz", - .hash = "1220ca0756f0d49e4f5be5776776fb74d359c540251e5bb6cd73b3df9b7f90e30afe", + .url = "https://github.com/MoAlyousef/zfltk/archive/refs/tags/pkg0.0.7.tar.gz", }, } } ``` +(This is missing the hash, zig build will give you the correct hash, which you should add after the url) + In your build.zig: ```zig const std = @import("std"); diff --git a/build.zig b/build.zig index 5725b13..640d176 100644 --- a/build.zig +++ b/build.zig @@ -9,14 +9,25 @@ install_prefix: []const u8, finalize_cfltk: *std.Build.Step, use_wayland: bool, -pub const SdkOpts = struct { - use_wayland: bool = false, -}; - pub fn init(b: *Build) !*Sdk { return init_with_opts(b, .{}); } +inline fn thisDir() []const u8 { + return comptime std.fs.path.dirname(@src().file) orelse @panic("error"); +} + +pub fn getZfltkModule(sdk: *Sdk, b: *Build) *Build.Module { + _ = sdk; + return b.createModule(.{ + .source_file = .{ .path = thisDir() ++ "/src/zfltk.zig" }, + }); +} + +pub const SdkOpts = struct { + use_wayland: bool = false, +}; + pub fn init_with_opts(b: *Build, opts: SdkOpts) !*Sdk { const install_prefix = b.install_prefix; const zig_exe = b.zig_exe; @@ -261,19 +272,9 @@ const examples = &[_]Example{ Example.init("threadawake", "examples/threadawake.zig", "Thread awake example"), Example.init("handle", "examples/handle.zig", "Handle example"), Example.init("flutterlike", "examples/flutterlike.zig", "Flutter-like example"), + Example.init("glwin", "examples/glwin.zig", "OpenGL window example"), }; -inline fn thisDir() []const u8 { - return comptime std.fs.path.dirname(@src().file) orelse @panic("error"); -} - -pub fn getZfltkModule(sdk: *Sdk, b: *Build) *Build.Module { - _ = sdk; - return b.createModule(.{ - .source_file = .{ .path = thisDir() ++ "/src/zfltk.zig" }, - }); -} - pub fn build(b: *Build) !void { const target = b.standardTargetOptions(.{}); const mode = b.standardOptimizeOption(.{}); diff --git a/src/box.zig b/src/box.zig index 9616375..e6dc380 100644 --- a/src/box.zig +++ b/src/box.zig @@ -9,7 +9,7 @@ const c = zfltk.c; pub const Box = struct { const Self = @This(); - + pub const RawPtr = *c.Fl_Box; pub usingnamespace zfltk.widget.methods(Self, *c.Fl_Box); pub const Options = struct { @@ -38,19 +38,9 @@ pub const Box = struct { } pub inline fn deinit(self: *Self) void { - c.Fl_Box_delete(self.inner); + c.Fl_Box_delete(self.raw()); app.allocator.destroy(self); } - - pub inline fn fromDynWidgetPtr(w: *c.Fl_Widget) ?Self { - if (c.Fl_Box_from_dyn_ptr(@ptrCast(w))) |v| { - return .{ - .inner = v, - }; - } else { - return null; - } - } }; test "all" { diff --git a/src/browser.zig b/src/browser.zig index be595ff..a85ae86 100644 --- a/src/browser.zig +++ b/src/browser.zig @@ -28,8 +28,6 @@ pub fn Browser(comptime kind: BrowserKind) type { .file => *c.Fl_File_Browser, }; - inner: RawPtr, - pub usingnamespace zfltk.widget.methods(Self, RawPtr); pub inline fn init(opts: Widget.Options) !*Self { @@ -44,10 +42,7 @@ pub fn Browser(comptime kind: BrowserKind) type { const label = if (opts.label != null) opts.label.?.ptr else null; if (init_func(opts.x, opts.y, opts.w, opts.h, label)) |ptr| { - var self = try app.allocator.create(Self); - self.inner = ptr; - - return self; + return Self.fromRaw(ptr); } unreachable; @@ -62,171 +57,151 @@ pub fn Browser(comptime kind: BrowserKind) type { .file => c.Fl_File_Browser_delete, }; - deinit_func(self.inner); - } - - pub inline fn raw(self: *Self) RawPtr { - return self.inner; - } - - pub inline fn fromRaw(ptr: RawPtr) Self { - return .{ .inner = ptr }; - } - - pub inline fn fromWidget(wid: Widget) Self { - return Self.fromRaw(@ptrCast(wid.inner)); - } - - pub inline fn fromVoidPtr(ptr: *anyopaque) Self { - return .{ .inner = @ptrCast(ptr) }; - } - - pub inline fn toVoidPtr(self: *Self) *anyopaque { - return @ptrCast(self.inner); + deinit_func(self.raw()); } pub fn remove(self: *Self, line: u32) void { - return c.Fl_Browser_remove(@ptrCast(self.inner), line); + return c.Fl_Browser_remove(@ptrCast(self.raw()), line); } pub fn add(self: *Self, item: [:0]const u8) void { - return c.Fl_Browser_add(@ptrCast(self.inner), item.ptr); + return c.Fl_Browser_add(@ptrCast(self.raw()), item.ptr); } pub fn insert(self: *Self, line: u32, item: [:0]const u8) void { - return c.Fl_Browser_insert(@ptrCast(self.inner), line, item.ptr); + return c.Fl_Browser_insert(@ptrCast(self.raw()), line, item.ptr); } pub fn moveItem(self: *Self, to: u32, from: u32) void { - return c.Fl_Browser_move_item(@ptrCast(self.inner), to, from); + return c.Fl_Browser_move_item(@ptrCast(self.raw()), to, from); } pub fn swap(self: *Self, a: u32, b: u32) void { - return c.Fl_Browser_swap(@ptrCast(self.inner), a, b); + return c.Fl_Browser_swap(@ptrCast(self.raw()), a, b); } pub fn clear(self: *Self) void { - return c.Fl_Browser_clear(@ptrCast(self.inner)); + return c.Fl_Browser_clear(@ptrCast(self.raw())); } pub fn size(self: *Self) u32 { - return c.Fl_Browser_size(@ptrCast(self.inner)); + return c.Fl_Browser_size(@ptrCast(self.raw())); } pub fn setSize(self: *Self, w: i32, h: i32) void { - return c.Fl_Browser_set_size(@ptrCast(self.inner), w, h); + return c.Fl_Browser_set_size(@ptrCast(self.raw()), w, h); } pub fn select(self: *Self, line: u32) void { - if (line <= self.size()) return c.Fl_Browser_select(@ptrCast(self.inner), line); + if (line <= self.size()) return c.Fl_Browser_select(@ptrCast(self.raw()), line); } pub fn selected(self: *Self, line: u32) bool { - return c.Fl_Browser_selected(@ptrCast(self.inner), line) != 0; + return c.Fl_Browser_selected(@ptrCast(self.raw()), line) != 0; } pub fn text(self: *Self, line: u32) [:0]const u8 { - return c.Fl_Browser_text(@ptrCast(self.inner), line); + return c.Fl_Browser_text(@ptrCast(self.raw()), line); } pub fn setText(self: *Self, line: u32, txt: [*c]const u8) void { - return c.Fl_Browser_set_text(@ptrCast(self.inner), line, txt); + return c.Fl_Browser_set_text(@ptrCast(self.raw()), line, txt); } pub fn load(self: *Self, path: [*c]const u8) void { - return c.Fl_Browser_load_file(@ptrCast(self.inner), path); + return c.Fl_Browser_load_file(@ptrCast(self.raw()), path); } pub fn textSize(self: *Self) u32 { - return c.Fl_Browser_text_size(@ptrCast(self.inner)); + return c.Fl_Browser_text_size(@ptrCast(self.raw())); } pub fn setTextSize(self: *Self, val: u32) void { - return c.Fl_Browser_set_text_size(@ptrCast(self.inner), val); + return c.Fl_Browser_set_text_size(@ptrCast(self.raw()), val); } pub fn topline(self: *Self, line: u32) void { - return c.Fl_Browser_topline(@ptrCast(self.inner), line); + return c.Fl_Browser_topline(@ptrCast(self.raw()), line); } pub fn bottomline(self: *Self, line: u32) void { - return c.Fl_Browser_bottomline(@ptrCast(self.inner), line); + return c.Fl_Browser_bottomline(@ptrCast(self.raw()), line); } pub fn middleline(self: *Self, line: u32) void { - return c.Fl_Browser_middleline(@ptrCast(self.inner), line); + return c.Fl_Browser_middleline(@ptrCast(self.raw()), line); } pub fn formatChar(self: *Self) u8 { - return c.Fl_Browser_format_char(@ptrCast(self.inner)); + return c.Fl_Browser_format_char(@ptrCast(self.raw())); } pub fn setFormatChar(self: *Self, char: u8) void { - return c.Fl_Browser_set_format_char(@ptrCast(self.inner), char); + return c.Fl_Browser_set_format_char(@ptrCast(self.raw()), char); } pub fn columnChar(self: *Self) u8 { - return c.Fl_Browser_column_char(@ptrCast(self.inner)); + return c.Fl_Browser_column_char(@ptrCast(self.raw())); } pub fn setColumnChar(self: *Self, char: u8) void { - return c.Fl_Browser_set_column_char(@ptrCast(self.inner), char); + return c.Fl_Browser_set_column_char(@ptrCast(self.raw()), char); } pub fn setColumnWidths(self: *Self, arr: [:0]const i32) void { - return c.Fl_Browser_set_column_widths(@ptrCast(self.inner), arr.ptr); + return c.Fl_Browser_set_column_widths(@ptrCast(self.raw()), arr.ptr); } pub fn displayed(self: *Self, line: u31) bool { - return c.Fl_Browser_displayed(@ptrCast(self.inner), line) != 0; + return c.Fl_Browser_displayed(@ptrCast(self.raw()), line) != 0; } pub fn makeVisible(self: *Self, line: u31) void { - return c.Fl_Browser_make_visible(@ptrCast(self.inner), line); + return c.Fl_Browser_make_visible(@ptrCast(self.raw()), line); } pub fn position(self: *Self) u31 { - return c.Fl_Browser_position(@ptrCast(self.inner)); + return c.Fl_Browser_position(@ptrCast(self.raw())); } pub fn setPosition(self: *Self, pos: u31) void { - return c.Fl_Browser_set_position(@ptrCast(self.inner), pos); + return c.Fl_Browser_set_position(@ptrCast(self.raw()), pos); } pub fn hposition(self: *Self) u31 { - return c.Fl_Browser_hposition(@ptrCast(self.inner)); + return c.Fl_Browser_hposition(@ptrCast(self.raw())); } pub fn setHposition(self: *Self, pos: u31) void { - return c.Fl_Browser_set_hposition(@ptrCast(self.inner), pos); + return c.Fl_Browser_set_hposition(@ptrCast(self.raw()), pos); } pub fn hasScrollbar(self: *Self) enums.BrowserScrollbar { - return c.Fl_Browser_has_scrollbar(@ptrCast(self.inner)); + return c.Fl_Browser_has_scrollbar(@ptrCast(self.raw())); } pub fn setHasScrollbar(self: *Self, mode: enums.BrowserScrollbar) void { - return c.Fl_Browser_set_has_scrollbar(@ptrCast(self.inner), mode); + return c.Fl_Browser_set_has_scrollbar(@ptrCast(self.raw()), mode); } pub fn scrollbarSize(self: *Self) u31 { - return c.Fl_Browser_scrollbar_size(@ptrCast(self.inner)); + return c.Fl_Browser_scrollbar_size(@ptrCast(self.raw())); } pub fn setScrollbarSize(self: *Self, new_size: u31) void { - return c.Fl_Browser_set_scrollbar_size(@ptrCast(self.inner), new_size); + return c.Fl_Browser_set_scrollbar_size(@ptrCast(self.raw()), new_size); } pub fn sort(self: *Self) void { - return c.Fl_Browser_sort(@ptrCast(self.inner)); + return c.Fl_Browser_sort(@ptrCast(self.raw())); } pub fn scrollbar(self: *Self) Valuator { - return .{ .inner = c.Fl_Browser_scrollbar(@ptrCast(self.inner)) }; + return .{ .inner = c.Fl_Browser_scrollbar(@ptrCast(self.raw())) }; } pub fn hscrollbar(self: *Self) .Valuator { - return .{ .inner = c.Fl_Browser_hscrollbar(@ptrCast(self.inner)) }; + return .{ .inner = c.Fl_Browser_hscrollbar(@ptrCast(self.raw())) }; } }; } diff --git a/src/button.zig b/src/button.zig index 1c9086b..f7e6a0d 100644 --- a/src/button.zig +++ b/src/button.zig @@ -68,14 +68,6 @@ pub fn Button(comptime kind: ButtonKind) type { deinit_func(self.raw()); app.allocator.destroy(self); } - - pub inline fn fromDynWidgetPtr(w: *c.Fl_Widget) ?Self { - if (c.Fl_Button_from_dyn_ptr(@ptrCast(w))) |v| { - return .{ .inner = v }; - } - - return null; - } }; } diff --git a/src/menu.zig b/src/menu.zig index 7e2d602..bb5fcb7 100644 --- a/src/menu.zig +++ b/src/menu.zig @@ -79,6 +79,17 @@ pub fn Menu(comptime kind: MenuKind) type { unreachable; } + + pub inline fn deinit(self: *Self) void { + const deinitFn = switch (kind) { + .menu_bar => c.Fl_Menu_Bar_delete, + .choice => c.Fl_Choice_delete, + .sys_menu_bar => c.Fl_Sys_Menu_Bar_delete, + }; + + deinitFn(self.raw()); + app.allocator.destroy(self); + } }; } diff --git a/src/table.zig b/src/table.zig index 0640799..1606aef 100644 --- a/src/table.zig +++ b/src/table.zig @@ -48,6 +48,16 @@ pub fn Table(comptime kind: TableKind) type { unreachable; } + + pub inline fn deinit(self: *Self) void { + const deinitFn = switch (kind) { + .table => c.Fl_Table_delete, + .table_row => c.Fl_Table_Row_delete, + }; + + deinitFn(self.raw()); + app.allocator.destroy(self); + } }; } diff --git a/src/tree.zig b/src/tree.zig index 0f638bc..7bed0c0 100644 --- a/src/tree.zig +++ b/src/tree.zig @@ -36,6 +36,11 @@ pub const Tree = struct { unreachable; } + + pub inline fn deinit(self: *Self) void { + c.Fl_Tree_delete(self.RawPtr); + app.allocator.destroy(self); + } }; pub fn methods(comptime Self: type) type { diff --git a/src/valuator.zig b/src/valuator.zig index 3e67103..0f32eff 100644 --- a/src/valuator.zig +++ b/src/valuator.zig @@ -4,6 +4,7 @@ const c = zfltk.c; const widget = zfltk.widget; const enums = zfltk.enums; const std = @import("std"); +const app = zfltk.app; pub const ValuatorKind = enum { slider, @@ -146,6 +147,20 @@ pub fn Valuator(comptime kind: ValuatorKind) type { unreachable; } + + pub inline fn deinit(self: *Self) void { + const deinitFn = switch (kind) { + .slider => c.Fl_Slider_delete, + .dial => c.Fl_Dial_delete, + .counter => c.Fl_Counter_delete, + .scrollbar => c.Fl_Scrollbar_delete, + .adjuster => c.Fl_Adjuster_delete, + .roller => c.Fl_Roller_delete, + }; + + deinitFn(self.raw()); + app.allocator.destroy(self); + } }; } diff --git a/src/widget.zig b/src/widget.zig index e415971..75bac8b 100644 --- a/src/widget.zig +++ b/src/widget.zig @@ -42,6 +42,7 @@ pub const Widget = struct { /// Methods to be used in everything derived from `Widget` pub fn methods(comptime Self: type, comptime RawPtr: type) type { return struct { + // const dyn_from_func = RawPtr ++ "_from_dyn_ptr"; const handle_func = switch (RawPtr) { *c.Fl_Box => c.Fl_Box_handle, *c.Fl_Button => c.Fl_Button_handle, @@ -130,6 +131,50 @@ pub fn methods(comptime Self: type, comptime RawPtr: type) type { else => c.Fl_Box_draw, }; + const dyn_ptr_func = switch (RawPtr) { + *c.Fl_Box => c.Fl_Box_from_dyn_ptr, + *c.Fl_Button => c.Fl_Button_from_dyn_ptr, + *c.Fl_Radio_Button => c.Fl_Radio_Button_from_dyn_ptr, + *c.Fl_Check_Button => c.Fl_Check_Button_from_dyn_ptr, + *c.Fl_Light_Button => c.Fl_Light_Button_from_dyn_ptr, + *c.Fl_Return_Button => c.Fl_Return_Button_from_dyn_ptr, + *c.Fl_Repeat_Button => c.Fl_Repeat_Button_from_dyn_ptr, + *c.Fl_Browser => c.Fl_Browser_from_dyn_ptr, + *c.Fl_Select_Browser => c.Fl_Select_Browser_from_dyn_ptr, + *c.Fl_Hold_Browser => c.Fl_Hold_Browser_from_dyn_ptr, + *c.Fl_Multi_Browser => c.Fl_Multi_Browser_from_dyn_ptr, + *c.Fl_Menu_Bar => c.Fl_Menu_Bar_from_dyn_ptr, + *c.Fl_Choice => c.Fl_Choice_from_dyn_ptr, + *c.Fl_Sys_Menu_Bar => c.Fl_Sys_Menu_Bar_from_dyn_ptr, + *c.Fl_Group => c.Fl_Group_from_dyn_ptr, + *c.Fl_Scroll => c.Fl_Scroll, + *c.Fl_Flex => c.Fl_Flex_from_dyn_ptr, + *c.Fl_Tabs => c.Fl_Tabs_from_dyn_ptr, + *c.Fl_Pack => c.Fl_Pack_from_dyn_ptr, + *c.Fl_Input => c.Fl_Input_from_dyn_ptr, + *c.Fl_Secret_Input => c.Fl_Secret_Input_from_dyn_ptr, + *c.Fl_Int_Input => c.Fl_Int_Input_from_dyn_ptr, + *c.Fl_Float_Input => c.Fl_Float_Input_from_dyn_ptr, + *c.Fl_Multiline_Input => c.Fl_Multiline_Input_from_dyn_ptr, + *c.Fl_Output => c.Fl_Output_from_dyn_ptr, + *c.Fl_Multiline_Output => c.Fl_Multiline_Output_from_dyn_ptr, + *c.Fl_Table => c.Fl_Table_from_dyn_ptr, + *c.Fl_Table_Row => c.Fl_Table_Row_from_dyn_ptr, + *c.Fl_Tree => c.Fl_Tree_from_dyn_ptr, + *c.Fl_Text_Display => c.Fl_Text_Display_from_dyn_ptr, + *c.Fl_Text_Editor => c.Fl_Text_Editor_from_dyn_ptr, + *c.Fl_Window => c.Fl_Window_from_dyn_ptr, + *c.Fl_Double_Window => c.Fl_Double_Window_from_dyn_ptr, + *c.Fl_Glut_Window => c.Fl_Glut_Window_from_dyn_ptr, + *c.Fl_Slider => c.Fl_Slider_from_dyn_ptr, + *c.Fl_Dial => c.Fl_Dial_from_dyn_ptr, + *c.Fl_Counter => c.Fl_Counter_from_dyn_ptr, + *c.Fl_Scrollbar => c.Fl_Scrollbar_from_dyn_ptr, + *c.Fl_Adjuster => c.Fl_Adjuster_from_dyn_ptr, + *c.Fl_Roller => c.Fl_Roller_from_dyn_ptr, + else => c.Fl_Box_from_dyn_ptr, + }; + pub inline fn widget(self: *Self) *Widget { return @ptrCast(self); } @@ -146,6 +191,14 @@ pub fn methods(comptime Self: type, comptime RawPtr: type) type { return @ptrCast(ptr); } + pub inline fn fromDynWidget(other: anytype) ?*Self { + if (dyn_ptr_func(@ptrCast(@alignCast(other)))) |o| { + return Self.fromRaw(o); + } + + return null; + } + /// Sets a function to be called upon `activation` of a widget such as /// pushing a button pub inline fn setCallback(self: *Self, f: *const fn (*Self) void) void { @@ -374,7 +427,6 @@ pub fn methods(comptime Self: type, comptime RawPtr: type) type { } } - pub inline fn setDrawHandler(self: *Self, f: *const fn (*Self) void) void { draw_func( self.raw(), diff --git a/src/window.zig b/src/window.zig index f8633cc..c3061e3 100644 --- a/src/window.zig +++ b/src/window.zig @@ -105,7 +105,7 @@ pub const GlutWindow = struct { /// Mark the OpeGL context as still valid pub fn setValid(self: *GlutWindow, v: bool) void { - return c.Fl_Glut_Window_set_valid(self.raw(), v); + return c.Fl_Glut_Window_set_valid(self.raw(), @intFromBool(v)); } /// Returns whether the context is valid upon creation @@ -115,7 +115,7 @@ pub const GlutWindow = struct { /// Mark the context as valid upon creation pub fn setContextValid(self: *GlutWindow, v: bool) void { - c.Fl_Glut_Window_set_context_valid(self.raw(), v); + c.Fl_Glut_Window_set_context_valid(self.raw(), @intFromBool(v)); } /// Returns the GlContext @@ -125,30 +125,30 @@ pub const GlutWindow = struct { /// Sets the GlContext pub fn setContext(self: *GlutWindow, ctx: ?*anyopaque, destroy_flag: bool) void { - c.Fl_Glut_Window_set_context(self.raw(), ctx, destroy_flag); + c.Fl_Glut_Window_set_context(self.raw(), ctx, @intFromBool(destroy_flag)); } }; pub fn methods(comptime Self: type) type { return struct { pub fn setSizeRange(self: *Self, min_w: u31, min_h: u31, max_w: u31, max_h: u31) void { - return c.Fl_Window_size_range(self.raw(), min_w, min_h, max_w, max_h); + return c.Fl_Window_size_range(@ptrCast(self.raw()), min_w, min_h, max_w, max_h); } pub fn iconize(self: *Self) void { - c.Fl_Window_iconize(self.raw()); + c.Fl_Window_iconize(@ptrCast(self.raw())); } pub fn setCursor(self: *Self, cursor: enums.Cursor) void { - return c.Fl_Window_set_cursor(self.raw(), @intFromEnum(cursor)); + return c.Fl_Window_set_cursor(@ptrCast(self.raw()), @intFromEnum(cursor)); } pub fn makeModal(self: *Self, val: bool) void { - return c.Fl_Window_make_modal(self.raw(), @intFromBool(val)); + return c.Fl_Window_make_modal(@ptrCast(self.raw()), @intFromBool(val)); } pub fn setFullscreen(self: *Window, val: bool) void { - return c.Fl_Window_fullscreen(self.raw(), @intFromBool(val)); + return c.Fl_Window_fullscreen(@ptrCast(self.raw()), @intFromBool(val)); } }; } diff --git a/src/zfltk.zig b/src/zfltk.zig index 124db3f..a5ad521 100644 --- a/src/zfltk.zig +++ b/src/zfltk.zig @@ -32,7 +32,6 @@ pub const Table = table.Table; pub const Input = input.Input; pub const Output = output.Output; pub const TextDisplay = text.TextDisplay; -pub const TextEditor = text.TextEditor; pub const TextBuffer = text.TextBuffer; pub const FileDialog = dialog.FileDialog; pub const Valuator = valuator.Valuator; @@ -59,9 +58,7 @@ pub const c = @cImport({ }); pub fn widgetCast(comptime T: type, wid: anytype) T { - return T{ - .inner = @ptrCast(wid.inner), - }; + return @ptrCast(@alignCast(wid)); } // TODO: improve helper function