From 096ec211833d663b3e8976452c3293756a7ededf Mon Sep 17 00:00:00 2001 From: Jedidiah Barber Date: Mon, 20 Jan 2025 16:02:12 +1300 Subject: format / Format method for Fl_Valuator --- doc/fl_valuator.html | 6 +++- progress.txt | 2 +- src/c_fl_adjuster.cpp | 7 ++++ src/c_fl_counter.cpp | 7 ++++ src/c_fl_dial.cpp | 7 ++++ src/c_fl_fill_dial.cpp | 7 ++++ src/c_fl_fill_slider.cpp | 7 ++++ src/c_fl_hor_fill_slider.cpp | 7 ++++ src/c_fl_hor_nice_slider.cpp | 7 ++++ src/c_fl_hor_value_slider.cpp | 7 ++++ src/c_fl_horizontal_slider.cpp | 7 ++++ src/c_fl_line_dial.cpp | 7 ++++ src/c_fl_nice_slider.cpp | 7 ++++ src/c_fl_roller.cpp | 7 ++++ src/c_fl_scrollbar.cpp | 7 ++++ src/c_fl_simple_counter.cpp | 7 ++++ src/c_fl_slider.cpp | 7 ++++ src/c_fl_valuator.cpp | 14 ++++++++ src/c_fl_valuator.h | 3 ++ src/c_fl_value_input.cpp | 7 ++++ src/c_fl_value_output.cpp | 7 ++++ src/c_fl_value_slider.cpp | 7 ++++ src/fltk-widgets-menus.ads | 2 +- src/fltk-widgets-valuators.adb | 73 +++++++++++++++++++++++++++++++++++++++++- src/fltk-widgets-valuators.ads | 8 +++++ src/fltk.ads | 3 ++ 26 files changed, 233 insertions(+), 4 deletions(-) diff --git a/doc/fl_valuator.html b/doc/fl_valuator.html index 50a0dd3..d443dbe 100644 --- a/doc/fl_valuator.html +++ b/doc/fl_valuator.html @@ -106,7 +106,11 @@ function Clamp
 virtual int format(char *);
 
-  +
+function Format
+       (This : in Valuator)
+    return String;
+
diff --git a/progress.txt b/progress.txt index 005eb86..b0689c3 100644 --- a/progress.txt +++ b/progress.txt @@ -226,6 +226,6 @@ Widgets Widgets.Groups.Scrolls (attributes, resize, type, protected) Widgets.Groups.Text_Displays Widgets.Groups.Windows -Widgets.Valuators (format) (a few derivative classes need type() checked too) +Widgets.Valuators (a few derivative classes need type() checked) diff --git a/src/c_fl_adjuster.cpp b/src/c_fl_adjuster.cpp index 4cd2b70..37a52cd 100644 --- a/src/c_fl_adjuster.cpp +++ b/src/c_fl_adjuster.cpp @@ -15,6 +15,8 @@ extern "C" void widget_draw_hook(void * ud); extern "C" int widget_handle_hook(void * ud, int e); +extern "C" int valuator_format_hook(void * ud, char * buf); + @@ -37,10 +39,15 @@ public: friend void fl_adjuster_draw(ADJUSTER a); friend int fl_adjuster_handle(ADJUSTER a, int e); + int format(char * buf); void draw(); int handle(int e); }; +int My_Adjuster::format(char * buf) { + return valuator_format_hook(this->user_data(), buf); +} + void My_Adjuster::draw() { widget_draw_hook(this->user_data()); } diff --git a/src/c_fl_counter.cpp b/src/c_fl_counter.cpp index b4e0cdf..9fe5d20 100644 --- a/src/c_fl_counter.cpp +++ b/src/c_fl_counter.cpp @@ -15,6 +15,8 @@ extern "C" void widget_draw_hook(void * ud); extern "C" int widget_handle_hook(void * ud, int e); +extern "C" int valuator_format_hook(void * ud, char * buf); + @@ -27,10 +29,15 @@ public: friend void fl_counter_draw(COUNTER c); friend int fl_counter_handle(COUNTER c, int e); + int format(char * buf); void draw(); int handle(int e); }; +int My_Counter::format(char * buf) { + return valuator_format_hook(this->user_data(), buf); +} + void My_Counter::draw() { widget_draw_hook(this->user_data()); } diff --git a/src/c_fl_dial.cpp b/src/c_fl_dial.cpp index 34c6df8..af83c21 100644 --- a/src/c_fl_dial.cpp +++ b/src/c_fl_dial.cpp @@ -15,6 +15,8 @@ extern "C" void widget_draw_hook(void * ud); extern "C" int widget_handle_hook(void * ud, int e); +extern "C" int valuator_format_hook(void * ud, char * buf); + @@ -39,10 +41,15 @@ public: friend void fl_dial_draw(DIAL v); friend int fl_dial_handle(DIAL v, int e); + int format(char * buf); void draw(); int handle(int e); }; +int My_Dial::format(char * buf) { + return valuator_format_hook(this->user_data(), buf); +} + void My_Dial::draw() { widget_draw_hook(this->user_data()); } diff --git a/src/c_fl_fill_dial.cpp b/src/c_fl_fill_dial.cpp index 57fd28b..47833c1 100644 --- a/src/c_fl_fill_dial.cpp +++ b/src/c_fl_fill_dial.cpp @@ -15,6 +15,8 @@ extern "C" void widget_draw_hook(void * ud); extern "C" int widget_handle_hook(void * ud, int e); +extern "C" int valuator_format_hook(void * ud, char * buf); + @@ -27,10 +29,15 @@ public: friend void fl_fill_dial_draw(FILLDIAL v); friend int fl_fill_dial_handle(FILLDIAL v, int e); + int format(char * buf); void draw(); int handle(int e); }; +int My_Fill_Dial::format(char * buf) { + return valuator_format_hook(this->user_data(), buf); +} + void My_Fill_Dial::draw() { widget_draw_hook(this->user_data()); } diff --git a/src/c_fl_fill_slider.cpp b/src/c_fl_fill_slider.cpp index d5552a4..49834d4 100644 --- a/src/c_fl_fill_slider.cpp +++ b/src/c_fl_fill_slider.cpp @@ -15,6 +15,8 @@ extern "C" void widget_draw_hook(void * ud); extern "C" int widget_handle_hook(void * ud, int e); +extern "C" int valuator_format_hook(void * ud, char * buf); + @@ -27,10 +29,15 @@ public: friend void fl_fill_slider_draw(FILLSLIDER s); friend int fl_fill_slider_handle(FILLSLIDER s, int e); + int format(char * buf); void draw(); int handle(int e); }; +int My_Fill_Slider::format(char * buf) { + return valuator_format_hook(this->user_data(), buf); +} + void My_Fill_Slider::draw() { widget_draw_hook(this->user_data()); } diff --git a/src/c_fl_hor_fill_slider.cpp b/src/c_fl_hor_fill_slider.cpp index c6416a9..9cd6ae2 100644 --- a/src/c_fl_hor_fill_slider.cpp +++ b/src/c_fl_hor_fill_slider.cpp @@ -15,6 +15,8 @@ extern "C" void widget_draw_hook(void * ud); extern "C" int widget_handle_hook(void * ud, int e); +extern "C" int valuator_format_hook(void * ud, char * buf); + @@ -27,10 +29,15 @@ public: friend void fl_hor_fill_slider_draw(HORFILLSLIDER s); friend int fl_hor_fill_slider_handle(HORFILLSLIDER s, int e); + int format(char * buf); void draw(); int handle(int e); }; +int My_Hor_Fill_Slider::format(char * buf) { + return valuator_format_hook(this->user_data(), buf); +} + void My_Hor_Fill_Slider::draw() { widget_draw_hook(this->user_data()); } diff --git a/src/c_fl_hor_nice_slider.cpp b/src/c_fl_hor_nice_slider.cpp index 141a85b..29b271d 100644 --- a/src/c_fl_hor_nice_slider.cpp +++ b/src/c_fl_hor_nice_slider.cpp @@ -15,6 +15,8 @@ extern "C" void widget_draw_hook(void * ud); extern "C" int widget_handle_hook(void * ud, int e); +extern "C" int valuator_format_hook(void * ud, char * buf); + @@ -27,10 +29,15 @@ public: friend void fl_hor_nice_slider_draw(HORNICESLIDER s); friend int fl_hor_nice_slider_handle(HORNICESLIDER s, int e); + int format(char * buf); void draw(); int handle(int e); }; +int My_Hor_Nice_Slider::format(char * buf) { + return valuator_format_hook(this->user_data(), buf); +} + void My_Hor_Nice_Slider::draw() { widget_draw_hook(this->user_data()); } diff --git a/src/c_fl_hor_value_slider.cpp b/src/c_fl_hor_value_slider.cpp index 7f5bc87..cff16f6 100644 --- a/src/c_fl_hor_value_slider.cpp +++ b/src/c_fl_hor_value_slider.cpp @@ -15,6 +15,8 @@ extern "C" void widget_draw_hook(void * ud); extern "C" int widget_handle_hook(void * ud, int e); +extern "C" int valuator_format_hook(void * ud, char * buf); + @@ -27,10 +29,15 @@ public: friend void fl_hor_value_slider_draw(HORVALUESLIDER s); friend int fl_hor_value_slider_handle(HORVALUESLIDER s, int e); + int format(char * buf); void draw(); int handle(int e); }; +int My_Hor_Value_Slider::format(char * buf) { + return valuator_format_hook(this->user_data(), buf); +} + void My_Hor_Value_Slider::draw() { widget_draw_hook(this->user_data()); } diff --git a/src/c_fl_horizontal_slider.cpp b/src/c_fl_horizontal_slider.cpp index 638502d..6a0ac22 100644 --- a/src/c_fl_horizontal_slider.cpp +++ b/src/c_fl_horizontal_slider.cpp @@ -15,6 +15,8 @@ extern "C" void widget_draw_hook(void * ud); extern "C" int widget_handle_hook(void * ud, int e); +extern "C" int valuator_format_hook(void * ud, char * buf); + @@ -27,10 +29,15 @@ public: friend void fl_horizontal_slider_draw(HORIZONTALSLIDER s); friend int fl_horizontal_slider_handle(HORIZONTALSLIDER s, int e); + int format(char * buf); void draw(); int handle(int e); }; +int My_Horizontal_Slider::format(char * buf) { + return valuator_format_hook(this->user_data(), buf); +} + void My_Horizontal_Slider::draw() { widget_draw_hook(this->user_data()); } diff --git a/src/c_fl_line_dial.cpp b/src/c_fl_line_dial.cpp index c01e8d8..388264f 100644 --- a/src/c_fl_line_dial.cpp +++ b/src/c_fl_line_dial.cpp @@ -15,6 +15,8 @@ extern "C" void widget_draw_hook(void * ud); extern "C" int widget_handle_hook(void * ud, int e); +extern "C" int valuator_format_hook(void * ud, char * buf); + @@ -27,10 +29,15 @@ public: friend void fl_line_dial_draw(LINEDIAL v); friend int fl_line_dial_handle(LINEDIAL v, int e); + int format(char * buf); void draw(); int handle(int e); }; +int My_Line_Dial::format(char * buf) { + return valuator_format_hook(this->user_data(), buf); +} + void My_Line_Dial::draw() { widget_draw_hook(this->user_data()); } diff --git a/src/c_fl_nice_slider.cpp b/src/c_fl_nice_slider.cpp index 378f31c..082bbfc 100644 --- a/src/c_fl_nice_slider.cpp +++ b/src/c_fl_nice_slider.cpp @@ -15,6 +15,8 @@ extern "C" void widget_draw_hook(void * ud); extern "C" int widget_handle_hook(void * ud, int e); +extern "C" int valuator_format_hook(void * ud, char * buf); + @@ -27,10 +29,15 @@ public: friend void fl_nice_slider_draw(NICESLIDER s); friend int fl_nice_slider_handle(NICESLIDER s, int e); + int format(char * buf); void draw(); int handle(int e); }; +int My_Nice_Slider::format(char * buf) { + return valuator_format_hook(this->user_data(), buf); +} + void My_Nice_Slider::draw() { widget_draw_hook(this->user_data()); } diff --git a/src/c_fl_roller.cpp b/src/c_fl_roller.cpp index 9ad40da..1c65422 100644 --- a/src/c_fl_roller.cpp +++ b/src/c_fl_roller.cpp @@ -15,6 +15,8 @@ extern "C" void widget_draw_hook(void * ud); extern "C" int widget_handle_hook(void * ud, int e); +extern "C" int valuator_format_hook(void * ud, char * buf); + @@ -27,10 +29,15 @@ public: friend void fl_roller_draw(ROLLER r); friend int fl_roller_handle(ROLLER r, int e); + int format(char * buf); void draw(); int handle(int e); }; +int My_Roller::format(char * buf) { + return valuator_format_hook(this->user_data(), buf); +} + void My_Roller::draw() { widget_draw_hook(this->user_data()); } diff --git a/src/c_fl_scrollbar.cpp b/src/c_fl_scrollbar.cpp index 4208744..2ebdb27 100644 --- a/src/c_fl_scrollbar.cpp +++ b/src/c_fl_scrollbar.cpp @@ -30,6 +30,8 @@ void fl_scrollbar_extra_final(void * adaobj) { extern "C" void widget_draw_hook(void * ud); extern "C" int widget_handle_hook(void * ud, int e); +extern "C" int valuator_format_hook(void * ud, char * buf); + @@ -42,10 +44,15 @@ public: friend void fl_scrollbar_draw(SCROLLBAR s); friend int fl_scrollbar_handle(SCROLLBAR s, int e); + int format(char * buf); void draw(); int handle(int e); }; +int My_Scrollbar::format(char * buf) { + return valuator_format_hook(this->user_data(), buf); +} + void My_Scrollbar::draw() { widget_draw_hook(this->user_data()); } diff --git a/src/c_fl_simple_counter.cpp b/src/c_fl_simple_counter.cpp index 8be586d..cf42d03 100644 --- a/src/c_fl_simple_counter.cpp +++ b/src/c_fl_simple_counter.cpp @@ -15,6 +15,8 @@ extern "C" void widget_draw_hook(void * ud); extern "C" int widget_handle_hook(void * ud, int e); +extern "C" int valuator_format_hook(void * ud, char * buf); + @@ -27,10 +29,15 @@ public: friend void fl_simple_counter_draw(SIMPLECOUNTER c); friend int fl_simple_counter_handle(SIMPLECOUNTER c, int e); + int format(char * buf); void draw(); int handle(int e); }; +int My_Simple_Counter::format(char * buf) { + return valuator_format_hook(this->user_data(), buf); +} + void My_Simple_Counter::draw() { widget_draw_hook(this->user_data()); } diff --git a/src/c_fl_slider.cpp b/src/c_fl_slider.cpp index 77bd171..449988c 100644 --- a/src/c_fl_slider.cpp +++ b/src/c_fl_slider.cpp @@ -15,6 +15,8 @@ extern "C" void widget_draw_hook(void * ud); extern "C" int widget_handle_hook(void * ud, int e); +extern "C" int valuator_format_hook(void * ud, char * buf); + @@ -39,10 +41,15 @@ public: friend void fl_slider_draw(SLIDER s); friend int fl_slider_handle(SLIDER s, int e); + int format(char * buf); void draw(); int handle(int e); }; +int My_Slider::format(char * buf) { + return valuator_format_hook(this->user_data(), buf); +} + void My_Slider::draw() { widget_draw_hook(this->user_data()); } diff --git a/src/c_fl_valuator.cpp b/src/c_fl_valuator.cpp index b878077..3b4ebba 100644 --- a/src/c_fl_valuator.cpp +++ b/src/c_fl_valuator.cpp @@ -15,6 +15,8 @@ extern "C" void widget_draw_hook(void * ud); extern "C" int widget_handle_hook(void * ud, int e); +extern "C" int valuator_format_hook(void * ud, char * buf); + @@ -38,10 +40,15 @@ public: friend void fl_valuator_draw(VALUATOR v); friend int fl_valuator_handle(VALUATOR v, int e); + int format(char * buf); void draw(); int handle(int e); }; +int My_Valuator::format(char * buf) { + return valuator_format_hook(this->user_data(), buf); +} + void My_Valuator::draw() { widget_draw_hook(this->user_data()); } @@ -67,6 +74,13 @@ void free_fl_valuator(VALUATOR v) { +int fl_valuator_format(VALUATOR v, char * buf) { + return static_cast(v)->Fl_Valuator::format(buf); +} + + + + double fl_valuator_clamp(VALUATOR v, double a) { return static_cast(v)->clamp(a); } diff --git a/src/c_fl_valuator.h b/src/c_fl_valuator.h index e3c6959..81107bc 100644 --- a/src/c_fl_valuator.h +++ b/src/c_fl_valuator.h @@ -15,6 +15,9 @@ extern "C" VALUATOR new_fl_valuator(int x, int y, int w, int h, char* label); extern "C" void free_fl_valuator(VALUATOR v); +extern "C" int fl_valuator_format(VALUATOR v, char * buf); + + extern "C" double fl_valuator_clamp(VALUATOR v, double a); extern "C" double fl_valuator_round(VALUATOR v, double a); extern "C" double fl_valuator_increment(VALUATOR v, double a, int s); diff --git a/src/c_fl_value_input.cpp b/src/c_fl_value_input.cpp index e6a5498..3d19845 100644 --- a/src/c_fl_value_input.cpp +++ b/src/c_fl_value_input.cpp @@ -15,6 +15,8 @@ extern "C" void widget_draw_hook(void * ud); extern "C" int widget_handle_hook(void * ud, int e); +extern "C" int valuator_format_hook(void * ud, char * buf); + @@ -27,10 +29,15 @@ public: friend void fl_value_input_draw(VALUEINPUT a); friend int fl_value_input_handle(VALUEINPUT a, int e); + int format(char * buf); void draw(); int handle(int e); }; +int My_Value_Input::format(char * buf) { + return valuator_format_hook(this->user_data(), buf); +} + void My_Value_Input::draw() { widget_draw_hook(this->user_data()); } diff --git a/src/c_fl_value_output.cpp b/src/c_fl_value_output.cpp index 3294b6b..5e42996 100644 --- a/src/c_fl_value_output.cpp +++ b/src/c_fl_value_output.cpp @@ -15,6 +15,8 @@ extern "C" void widget_draw_hook(void * ud); extern "C" int widget_handle_hook(void * ud, int e); +extern "C" int valuator_format_hook(void * ud, char * buf); + @@ -27,10 +29,15 @@ public: friend void fl_value_output_draw(VALUEOUTPUT a); friend int fl_value_output_handle(VALUEOUTPUT a, int e); + int format(char * buf); void draw(); int handle(int e); }; +int My_Value_Output::format(char * buf) { + return valuator_format_hook(this->user_data(), buf); +} + void My_Value_Output::draw() { widget_draw_hook(this->user_data()); } diff --git a/src/c_fl_value_slider.cpp b/src/c_fl_value_slider.cpp index 7d6d59b..ac7498c 100644 --- a/src/c_fl_value_slider.cpp +++ b/src/c_fl_value_slider.cpp @@ -15,6 +15,8 @@ extern "C" void widget_draw_hook(void * ud); extern "C" int widget_handle_hook(void * ud, int e); +extern "C" int valuator_format_hook(void * ud, char * buf); + @@ -27,10 +29,15 @@ public: friend void fl_value_slider_draw(VALUESLIDER s); friend int fl_value_slider_handle(VALUESLIDER s, int e); + int format(char * buf); void draw(); int handle(int e); }; +int My_Value_Slider::format(char * buf) { + return valuator_format_hook(this->user_data(), buf); +} + void My_Value_Slider::draw() { widget_draw_hook(this->user_data()); } diff --git a/src/fltk-widgets-menus.ads b/src/fltk-widgets-menus.ads index 63828a5..bce29dd 100644 --- a/src/fltk-widgets-menus.ads +++ b/src/fltk-widgets-menus.ads @@ -42,7 +42,7 @@ package FLTK.Widgets.Menus is -- If your menu item path names are longer than this, -- then calls to Item_Pathname will raise an exception. - Item_Path_Max : constant Integer := 1023; + Item_Path_Max : constant Natural := Integer'Max (0, FLTK.Buffer_Size - 1); No_Reference_Error : exception; diff --git a/src/fltk-widgets-valuators.adb b/src/fltk-widgets-valuators.adb index 62ef77b..4b8db3f 100644 --- a/src/fltk-widgets-valuators.adb +++ b/src/fltk-widgets-valuators.adb @@ -6,13 +6,22 @@ with + Ada.Assertions, FLTK.Widgets.Groups, - Interfaces.C.Strings; + Interfaces.C.Strings, + System.Address_To_Access_Conversions; package body FLTK.Widgets.Valuators is + package Chk renames Ada.Assertions; + + package Valuator_Convert is new System.Address_To_Access_Conversions (Valuator'Class); + + + + ------------------------ -- Functions From C -- ------------------------ @@ -32,6 +41,16 @@ package body FLTK.Widgets.Valuators is + function fl_valuator_format + (V : in Storage.Integer_Address; + B : out Interfaces.C.char_array) + return Interfaces.C.int; + pragma Import (C, fl_valuator_format, "fl_valuator_format"); + pragma Inline (fl_valuator_format); + + + + function fl_valuator_clamp (V : in Storage.Integer_Address; D : in Interfaces.C.double) @@ -159,6 +178,44 @@ package body FLTK.Widgets.Valuators is + ---------------------- + -- Callback Hooks -- + ---------------------- + + function Valuator_Format_Hook + (Userdata : in Storage.Integer_Address; + Buffer : in Interfaces.C.Strings.chars_ptr) + return Interfaces.C.int; + pragma Export (C, Valuator_Format_Hook, "valuator_format_hook"); + + function Valuator_Format_Hook + (Userdata : in Storage.Integer_Address; + Buffer : in Interfaces.C.Strings.chars_ptr) + return Interfaces.C.int + is + Ada_Obj : access Valuator'Class; + begin + pragma Assert (Userdata /= Null_Pointer); + Ada_Obj := Valuator_Convert.To_Pointer (Storage.To_Address (Userdata)); + declare + String_Result : String := Ada_Obj.Format; + begin + if String_Result'Length <= FLTK.Buffer_Size then + Interfaces.C.Strings.Update (Buffer, 0, String_Result); + return String_Result'Length; + else + Interfaces.C.Strings.Update (Buffer, 0, String_Result (1 .. Buffer_Size)); + return Interfaces.C.int (FLTK.Buffer_Size); + end if; + end; + exception + when Chk.Assertion_Error => raise Internal_FLTK_Error with + "Valuator::format callback hook was passed null userdata wrapper reference pointer"; + end Valuator_Format_Hook; + + + + ------------------- -- Destructors -- ------------------- @@ -243,6 +300,20 @@ package body FLTK.Widgets.Valuators is -- API Subprograms -- ----------------------- + function Format + (This : in Valuator) + return String + is + Buffer : Interfaces.C.char_array := + (1 .. Interfaces.C.size_t (FLTK.Buffer_Size) => Interfaces.C.To_C (Character'Val (0))); + Result : Interfaces.C.int := fl_valuator_format (This.Void_Ptr, Buffer); + begin + return Interfaces.C.To_Ada (Buffer (1 .. Interfaces.C.size_t (Result)), False); + end Format; + + + + function Clamp (This : in Valuator; Input : in Long_Float) diff --git a/src/fltk-widgets-valuators.ads b/src/fltk-widgets-valuators.ads index a056ded..1e60f4b 100644 --- a/src/fltk-widgets-valuators.ads +++ b/src/fltk-widgets-valuators.ads @@ -38,6 +38,14 @@ package FLTK.Widgets.Valuators is + -- You may override this to change the formatting of the Valuator + function Format + (This : in Valuator) + return String; + + + + function Clamp (This : in Valuator; Input : in Long_Float) diff --git a/src/fltk.ads b/src/fltk.ads index 4af6b42..f6b7292 100644 --- a/src/fltk.ads +++ b/src/fltk.ads @@ -30,6 +30,9 @@ package FLTK is -- acted in a way that the binding really did not expect. Internal_FLTK_Error : exception; + -- Text buffers for marshalling purposes will be this size. + Buffer_Size : constant Natural := 1024; + -- cgit