diff --git a/src/main.cpp b/src/main.cpp index 1560dbd..2f826b1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1905,6 +1905,7 @@ static void on_prop_enum_changed(GtkComboBoxText*, gpointer binding); static void on_prop_pick_clicked(GtkButton*, gpointer binding); static void on_prop_font_clicked(GtkButton*, gpointer binding); static void on_xframe_selection_changed(int id); +static void on_xframe_placed(int id); static void on_xframe_motion(); static void on_xframe_spin_changed(GtkSpinButton*, gpointer which); static void on_xframe_delete_clicked(GtkButton*, gpointer); @@ -3343,6 +3344,13 @@ static std::string xframe_icons_dir() { static void on_xframe_palette_clicked(GtkButton*, gpointer data) { const XFramePaletteEntry* e = (const XFramePaletteEntry*)data; + // Clicking the already-selected entry again cancels placement. + if (xframe_canvas_selected_type() == e->index) { + xframe_canvas_set_selected_type(-1, nullptr); + g_xframe_selected.clear(); + status("Placement cancelled"); + return; + } xframe_canvas_set_selected_type(e->index, e->name); xframe_canvas_set_border(g_xframe_border_width, colour_to_hex(g_xframe_border_color), @@ -3518,6 +3526,21 @@ static void palette_window_hide() { if (g_palette_window) gtk_widget_hide(g_palette_window); } +// Escape in the palette window cancels an armed placement (the palette +// is its own top-level window, so the canvas key handler never sees +// the key when the palette has focus). +static gboolean on_palette_window_key_press(GtkWidget*, GdkEventKey* e, + gpointer) { + if (e->keyval == GDK_KEY_Escape && + xframe_canvas_selected_type() >= 0) { + xframe_canvas_set_selected_type(-1, nullptr); + g_xframe_selected.clear(); + status("Placement cancelled"); + return TRUE; + } + return FALSE; +} + static gboolean on_palette_window_delete(GtkWidget*, GdkEvent*, gpointer) { // Close button hides the palette instead of destroying it, so the @@ -6616,6 +6639,28 @@ static gboolean selftest_step(gpointer) { // ------------------------------------------------------- main +// A widget was just placed by a canvas click: refresh the code view +// so the generated code shows it at once (Lazarus-style sync), and +// clear the armed palette entry (single-placement). +static void on_xframe_placed(int id) { + g_xframe_selected.clear(); + std::string name; + for (const XFramePlacedInfo& info : xframe_canvas_placed()) + if (info.id == id) { + char buf[64]; + std::snprintf(buf, sizeof buf, "%s_%03d", info.name.c_str(), + info.id); + name = buf; + break; + } + refresh_code_view(); + status(name.empty() + ? std::string("Widget placed - placement off; select a " + "widget to place another") + : "Placed " + name + + " - placement off; select a widget to place another"); +} + int main(int argc, char** argv) { gtk_init(&argc, &argv); @@ -6705,6 +6750,10 @@ int main(int argc, char** argv) { xframe_canvas_set_selection_callback( +[](int id, void*) { on_xframe_selection_changed(id); }, nullptr); + // A widget placed by a canvas click refreshes the code view + // at once (Lazarus-style sync) and disarms placement. + xframe_canvas_set_placed_callback( + +[](int id, void*) { on_xframe_placed(id); }, nullptr); xframe_canvas_set_motion_callback( +[](void*) { on_xframe_motion(); }, nullptr); } @@ -6812,6 +6861,8 @@ int main(int argc, char** argv) { } g_signal_connect(g_palette_window, "delete-event", G_CALLBACK(on_palette_window_delete), nullptr); + g_signal_connect(g_palette_window, "key-press-event", + G_CALLBACK(on_palette_window_key_press), nullptr); // View menu -> object inspector window (show/hide toggle). GObject* mi_insp = gtk_builder_get_object(builder, "mi_view_inspector"); if (mi_insp) { diff --git a/src/xframe_canvas.cpp b/src/xframe_canvas.cpp index beb14c9..5ded3f5 100644 --- a/src/xframe_canvas.cpp +++ b/src/xframe_canvas.cpp @@ -43,10 +43,22 @@ void* s_empty_click_data = nullptr; void (*s_selection_cb)(int, void*) = nullptr; void* s_selection_data = nullptr; +void (*s_placed_cb)(int, void*) = nullptr; +void* s_placed_data = nullptr; int s_last_notified_sel = -1; void (*s_motion_cb)(void*) = nullptr; void* s_motion_data = nullptr; +// Sync the palette-selection mirror from the core (the core clears +// the pending type when a widget is placed or Escape is pressed). +void sync_sel_type_from_core() { + if (!s_core) return; + int t = xframe_core_selected_type(s_core); + if (t == s_sel_type) return; + s_sel_type = t; + if (t < 0) s_sel_name.clear(); +} + // Fix 6: tell the designer when the canvas selection changed (and only // then) so the Inspector can follow it. void maybe_notify_selection() { @@ -116,9 +128,17 @@ gboolean on_area_button_press(GtkWidget* area, GdkEventButton* ev, // Take keyboard focus so Delete/Escape reach the canvas (same as // the standalone layout window). gtk_widget_grab_focus(area); + int before = xframe_core_count(s_core); int rc = xframe_core_button_press(s_core, (int)ev->x, (int)ev->y, ev->button); maybe_notify_selection(); + // A placement happened: tell the designer (code-view sync) and + // drop the armed palette type (single-placement). + if (xframe_core_count(s_core) > before) { + if (s_placed_cb) + s_placed_cb(xframe_core_selected_id(s_core), s_placed_data); + } + sync_sel_type_from_core(); if (rc == 1 && s_empty_click_cb) s_empty_click_cb(s_empty_click_data); return TRUE; // do not let the GtkFixed also see the click @@ -145,6 +165,7 @@ gboolean on_area_key_press(GtkWidget*, GdkEventKey* ev, gpointer) { if (s_core) { xframe_core_key(s_core, (KeySym)ev->keyval); maybe_notify_selection(); + sync_sel_type_from_core(); } return TRUE; } @@ -328,3 +349,9 @@ void xframe_canvas_set_motion_callback(void (*cb)(void*), void* data) { s_motion_cb = cb; s_motion_data = data; } + +void xframe_canvas_set_placed_callback(void (*cb)(int id, void*), + void* data) { + s_placed_cb = cb; + s_placed_data = data; +} diff --git a/src/xframe_canvas.h b/src/xframe_canvas.h index d93ad19..dcddc48 100644 --- a/src/xframe_canvas.h +++ b/src/xframe_canvas.h @@ -96,3 +96,7 @@ void xframe_canvas_set_selection_callback(void (*cb)(int id, void*), // Called on canvas pointer motion (Fix 6: the Inspector geometry spins // follow drags). void xframe_canvas_set_motion_callback(void (*cb)(void*), void* data); +// Called with the id of a widget just placed by a canvas click, so the +// designer can refresh the code view (Lazarus-style sync). +void xframe_canvas_set_placed_callback(void (*cb)(int id, void*), + void* data); diff --git a/src/xframe_canvas_core.cpp b/src/xframe_canvas_core.cpp index f173730..19eb14e 100644 --- a/src/xframe_canvas_core.cpp +++ b/src/xframe_canvas_core.cpp @@ -1540,7 +1540,15 @@ int xframe_core_button_press(XFrameCanvasCore* c, int x, int y, int hh = lg::kWidgetDefaultH[c->sel_type]; int id = xframe_core_place(c, c->sel_type, x - w / 2, y - hh / 2, w, hh); - if (id >= 0) xframe_core_redraw(c); + if (id >= 0) { + // Single-placement: placing one widget disarms placement + // mode (the palette type is cleared), so the next click + // does not place another fresh widget. Select a widget in + // the palette again to place another. + c->sel_type = -1; + c->sel_name.clear(); + xframe_core_redraw(c); + } return 0; } return 1; // empty click, nothing pending @@ -1612,6 +1620,10 @@ void xframe_core_key(XFrameCanvasCore* c, KeySym key) { } else if (key == XK_Escape) { c->handles_on = false; c->selected_id = -1; + // Escape also cancels an armed placement (same as clicking + // the selected palette entry again). + c->sel_type = -1; + c->sel_name.clear(); write_state(c); xframe_core_redraw(c); } diff --git a/src/xframe_canvas_core.h b/src/xframe_canvas_core.h index fa4462f..ec0ef43 100644 --- a/src/xframe_canvas_core.h +++ b/src/xframe_canvas_core.h @@ -78,8 +78,9 @@ void xframe_core_motion(XFrameCanvasCore* core, int x, int y); void xframe_core_key(XFrameCanvasCore* core, unsigned long key); // Palette -> canvas, in-process: the widget type to place on the next -// click on empty canvas space (-1 = none). Persists across placements -// (click repeatedly to place many), like the standalone layout. +// click on empty canvas space (-1 = none). Single-placement: placing +// one widget clears the type, and Escape clears it too, so placement +// mode never sticks. void xframe_core_set_selected_type(XFrameCanvasCore* core, int index); int xframe_core_selected_type(XFrameCanvasCore* core); int xframe_core_selected_id(XFrameCanvasCore* core);