FLUID: Arrow keys move widgets inside Fl_Flex

This commit is contained in:
Matthias Melcher 2023-11-01 23:34:47 +01:00
parent 904d25dafe
commit fb6c38b1a4
5 changed files with 43 additions and 24 deletions

View File

@ -921,8 +921,8 @@ static void draw_right_brace(const Fl_Widget *w);
static void draw_top_brace(const Fl_Widget *w); static void draw_top_brace(const Fl_Widget *w);
static void draw_bottom_brace(const Fl_Widget *w); static void draw_bottom_brace(const Fl_Widget *w);
static void draw_grid(int x, int y, int dx, int dy); static void draw_grid(int x, int y, int dx, int dy);
static void draw_width(int x, int y, int r, Fl_Align a); void draw_width(int x, int y, int r, Fl_Align a);
static void draw_height(int x, int y, int b, Fl_Align a); void draw_height(int x, int y, int b, Fl_Align a);
static int nearest(int x, int left, int grid, int right=0x7fff) { static int nearest(int x, int left, int grid, int right=0x7fff) {
int grid_x = ((x-left+grid/2)/grid)*grid+left; int grid_x = ((x-left+grid/2)/grid)*grid+left;
@ -1734,7 +1734,7 @@ static void draw_bottom_brace(const Fl_Widget *w) {
fl_xyline(x-2, y, x+w->w()+1); fl_xyline(x-2, y, x+w->w()+1);
} }
static void draw_height(int x, int y, int b, Fl_Align a) { void draw_height(int x, int y, int b, Fl_Align a) {
char buf[16]; char buf[16];
int h = b - y; int h = b - y;
sprintf(buf, "%d", h); sprintf(buf, "%d", h);
@ -1768,7 +1768,7 @@ static void draw_height(int x, int y, int b, Fl_Align a) {
fl_xyline(x - 4, b, x + 4); fl_xyline(x - 4, b, x + 4);
} }
static void draw_width(int x, int y, int r, Fl_Align a) { void draw_width(int x, int y, int r, Fl_Align a) {
char buf[16]; char buf[16];
int w = r-x; int w = r-x;
sprintf(buf, "%d", w); sprintf(buf, "%d", w);

View File

@ -515,6 +515,25 @@ void Fl_Flex_Type::insert_child_at(Fl_Widget *child, int x, int y) {
} }
} }
void Fl_Flex_Type::keyboard_move_child(Fl_Widget_Type *child, int key) {
Fl_Flex *flex = ((Fl_Flex*)o);
int ix = flex->find(child->o);
if (ix == flex->children()) return;
if (flex->horizontal()) {
if (key==FL_Right) {
flex->insert(*child->o, ix+2);
} else if (key==FL_Left) {
if (ix > 0) flex->insert(*child->o, ix-1);
}
} else {
if (key==FL_Down) {
flex->insert(*child->o, ix+2);
} else if (key==FL_Up) {
if (ix > 0) flex->insert(*child->o, ix-1);
}
}
}
int Fl_Flex_Type::size(Fl_Type *t, char fixed_only) { int Fl_Flex_Type::size(Fl_Type *t, char fixed_only) {
if (!t->is_widget()) return 0; if (!t->is_widget()) return 0;
if (!t->parent) return 0; if (!t->parent) return 0;

View File

@ -126,6 +126,7 @@ public:
void layout_widget() FL_OVERRIDE; void layout_widget() FL_OVERRIDE;
void change_subtype_to(int n); void change_subtype_to(int n);
void insert_child_at(Fl_Widget *child, int x, int y); void insert_child_at(Fl_Widget *child, int x, int y);
void keyboard_move_child(Fl_Widget_Type*, int key);
static int parent_is_flex(Fl_Type*); static int parent_is_flex(Fl_Type*);
static int size(Fl_Type*, char fixed_only=0); static int size(Fl_Type*, char fixed_only=0);
static int is_fixed(Fl_Type*); static int is_fixed(Fl_Type*);

View File

@ -40,12 +40,16 @@
#include <FL/Fl_Menu_Item.H> #include <FL/Fl_Menu_Item.H>
#include <FL/Fl_Round_Button.H> #include <FL/Fl_Round_Button.H>
#include <FL/Fl_Shared_Image.H> #include <FL/Fl_Shared_Image.H>
#include <FL/Fl_Tooltip.H>
#include "../src/flstring.h" #include "../src/flstring.h"
#include <math.h> #include <math.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
extern void draw_width(int x, int y, int r, Fl_Align a);
extern void draw_height(int x, int y, int b, Fl_Align a);
extern Fl_Preferences fluid_prefs; extern Fl_Preferences fluid_prefs;
// Update the XYWH values in the widget panel... // Update the XYWH values in the widget panel...
@ -618,23 +622,13 @@ void Fl_Window_Type::draw_overlay() {
int x,y,r,t; int x,y,r,t;
newposition(myo,x,y,r,t); newposition(myo,x,y,r,t);
if (!show_guides || !drag || numselected != 1) { if (!show_guides || !drag || numselected != 1) {
if (Fl_Flex_Type::parent_is_flex(q) && !Fl_Flex_Type::is_fixed(q)) { if (Fl_Flex_Type::parent_is_flex(q) && Fl_Flex_Type::is_fixed(q)) {
if (((Fl_Flex*)((Fl_Flex_Type*)q->parent)->o)->horizontal()) { Fl_Flex *flex = ((Fl_Flex*)((Fl_Flex_Type*)q->parent)->o);
int yh = y + (t-y)/2; Fl_Widget *wgt = myo->o;
fl_begin_loop(); if (flex->horizontal()) {
fl_vertex(x+2, yh); fl_vertex(x+12, yh+5); fl_vertex(x+12, yh-5); draw_width(wgt->x(), wgt->y()+15, wgt->x()+wgt->w(), FL_ALIGN_CENTER);
fl_end_loop();
fl_begin_loop();
fl_vertex(r-3, yh); fl_vertex(r-13, yh+5); fl_vertex(r-13, yh-5);
fl_end_loop();
} else { } else {
int xh = x + (r-x)/2; draw_height(wgt->x()+15, wgt->y(), wgt->y()+wgt->h(), FL_ALIGN_CENTER);
fl_begin_loop();
fl_vertex(xh, y+2); fl_vertex(xh+5, y+12); fl_vertex(xh-5, y+12);
fl_end_loop();
fl_begin_loop();
fl_vertex(xh, t-3); fl_vertex(xh+5, t-13); fl_vertex(xh-5, t-13);
fl_end_loop();
} }
} }
fl_rect(x,y,r-x,t-y); fl_rect(x,y,r-x,t-y);
@ -837,8 +831,11 @@ extern Fl_Menu_Item New_Menu[];
This is not ideal for widgets that are moved or resized within a group that This is not ideal for widgets that are moved or resized within a group that
manages the layout of its children. We must create a more universal way to manages the layout of its children. We must create a more universal way to
modify move events per widget type. modify move events per widget type.
\param[in] key if key is not 0, it contains the code of the keypress that
caused this call. This must only be set when handle FL_KEYBOARD events.
*/ */
void Fl_Window_Type::moveallchildren() void Fl_Window_Type::moveallchildren(int key)
{ {
undo_checkpoint(); undo_checkpoint();
Fl_Type *i; Fl_Type *i;
@ -862,7 +859,9 @@ void Fl_Window_Type::moveallchildren()
// so that the user request is reflected. // so that the user request is reflected.
Fl_Flex_Type* ft = (Fl_Flex_Type*)myo->parent; Fl_Flex_Type* ft = (Fl_Flex_Type*)myo->parent;
Fl_Flex* f = (Fl_Flex*)ft->o; Fl_Flex* f = (Fl_Flex*)ft->o;
if (drag & FD_DRAG) { if (key) {
ft->keyboard_move_child(myo, key);
} else if (drag & FD_DRAG) {
ft->insert_child_at(myo->o, Fl::event_x(), Fl::event_y()); ft->insert_child_at(myo->o, Fl::event_x(), Fl::event_y());
} else { } else {
if (f->horizontal()) { if (f->horizontal()) {
@ -1181,7 +1180,7 @@ int Fl_Window_Type::handle(int event) {
dx *= x_step; dx *= x_step;
dy *= y_step; dy *= y_step;
} }
moveallchildren(); moveallchildren(Fl::event_key());
drag = 0; drag = 0;
return 1; return 1;

View File

@ -72,7 +72,7 @@ protected:
Fl_Widget_Type *_make() FL_OVERRIDE {return 0;} // we don't call this Fl_Widget_Type *_make() FL_OVERRIDE {return 0;} // we don't call this
Fl_Widget *widget(int,int,int,int) FL_OVERRIDE {return 0;} Fl_Widget *widget(int,int,int,int) FL_OVERRIDE {return 0;}
int recalc; // set by fix_overlay() int recalc; // set by fix_overlay()
void moveallchildren(); void moveallchildren(int key=0);
ID id() const FL_OVERRIDE { return ID_Window; } ID id() const FL_OVERRIDE { return ID_Window; }
bool is_a(ID inID) const FL_OVERRIDE { return (inID==ID_Window) ? true : super::is_a(inID); } bool is_a(ID inID) const FL_OVERRIDE { return (inID==ID_Window) ? true : super::is_a(inID); }
void open_(); void open_();