Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Improve node selection handling in language panels #1380

Merged
merged 2 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 88 additions & 1 deletion src/panels/code_display.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/////////////////////////////////////////////////////////////////////////////
// Purpose: Display code in scintilla control
// Author: Ralph Walden
// Copyright: Copyright (c) 2020-2023 KeyWorks Software (Ralph Walden)
// Copyright: Copyright (c) 2020-2024 KeyWorks Software (Ralph Walden)
// License: Apache License -- see ../../LICENSE
/////////////////////////////////////////////////////////////////////////////

Expand All @@ -13,12 +13,14 @@

#include "base_panel.h" // BasePanel -- Code generation panel
#include "code.h" // Code -- Helper class for generating code
#include "image_handler.h" // ImageHandler class
#include "mainframe.h" // MainFrame -- Main window frame
#include "node.h" // Node class
#include "node_creator.h" // NodeCreator -- Class used to create nodes
#include "node_event.h" // NodeEvent and NodeEventInfo classes
#include "preferences.h" // Prefs -- Set/Get wxUiEditor preferences
#include "propgrid_panel.h" // PropGridPanel -- PropertyGrid class for node properties and events
#include "to_casts.h" // to_int, to_size_t, and to_char classes
#include "utils.h" // Miscellaneous utility functions

#ifndef SCI_SETKEYWORDS
Expand Down Expand Up @@ -594,6 +596,17 @@ void CodeDisplay::CodeGenerationComplete()

void CodeDisplay::OnNodeSelected(Node* node)
{
if (node->isGen(gen_embedded_image))
{
OnEmbedImageSelected(node);
return;
}
else if (node->isGen(gen_ribbonTool) || node->isGen(gen_ribbonButton))
{
OnRibbonToolSelected(node);
return;
}

if (!node->hasProp(prop_var_name) && m_panel_type != GEN_LANG_XRC && !node->isGen(gen_ribbonTool) &&
!node->isGen(gen_ribbonButton))
{
Expand Down Expand Up @@ -736,3 +749,77 @@ void CodeDisplay::OnNodeSelected(Node* node)
// Unlike GetLineVisible(), this function does ensure that the line is visible.
m_scintilla->ScrollToLine(line);
}

void CodeDisplay::OnRibbonToolSelected(Node* node)
{
tt_string search;
if (auto parent = node->getParent(); parent)
{
if (parent->isGen(gen_wxRibbonButtonBar))
{
search << '"' << node->as_string(prop_label) << '"';
}
else if (parent->isGen(gen_wxRibbonToolBar))
{
search << parent->as_string(prop_var_name) << "->AddTool(" << node->as_string(prop_id) << ",";
if (m_panel_type == GEN_LANG_PYTHON)
search.Replace("->", ".");
else if (m_panel_type == GEN_LANG_RUBY)
search.Replace("->AddTool(", ".add_tool($");
}
}

if (search.size())
{
if (auto line = (to_int) m_view.FindLineContaining(search); line >= 0)
{
m_scintilla->MarkerDeleteAll(node_marker);
m_scintilla->MarkerAdd(line, node_marker);
m_scintilla->ScrollToLine(line);
}
return;
}
}

void CodeDisplay::OnEmbedImageSelected(Node* node)
{
if (node->hasValue(prop_bitmap))
{
auto func_name = ProjectImages.GetBundleFuncName(node->as_string(prop_bitmap));
if (func_name.size())
{
if (func_name.starts_with("wxue_img::"))
func_name.erase(0, sizeof("wxue_img::") - 1);
if (auto pos = func_name.find("("); pos != tt::npos)
func_name.erase(pos, tt::npos);

if (auto line = (to_int) m_view.FindLineContaining(func_name); line >= 0)
{
m_scintilla->MarkerDeleteAll(node_marker);
m_scintilla->MarkerAdd(line, node_marker);
m_scintilla->ScrollToLine(line);
return;
}

// For icons, there is no bundle, just an image_ function
func_name.Replace("bundle_", "image_");
if (auto line = (to_int) m_view.FindLineContaining(func_name); line >= 0)
{
m_scintilla->MarkerDeleteAll(node_marker);
m_scintilla->MarkerAdd(line, node_marker);
m_scintilla->ScrollToLine(line);
return;
}

// If all else fails, try just the name. This will also handle Python and Ruby panels
func_name.Replace("image_", "");
if (auto line = (to_int) m_view.FindLineContaining(func_name); line >= 0)
{
m_scintilla->MarkerDeleteAll(node_marker);
m_scintilla->MarkerAdd(line, node_marker);
m_scintilla->ScrollToLine(line);
return;
}
}
}
}
15 changes: 9 additions & 6 deletions src/panels/code_display.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/////////////////////////////////////////////////////////////////////////////
// Purpose: Display code in scintilla control
// Author: Ralph Walden
// Copyright: Copyright (c) 2020-2021 KeyWorks Software (Ralph Walden)
// Copyright: Copyright (c) 2020-2024 KeyWorks Software (Ralph Walden)
// License: Apache License -- see ../../LICENSE
/////////////////////////////////////////////////////////////////////////////

Expand All @@ -16,10 +16,11 @@
class wxFindDialogEvent;
class Node;

// CodeDisplayBase creates and initializes a wxStyledTextCtrl (scintilla) control, and places it in a sizer.
//
// WriteCode expects a class to override the doWrite() method, which in this case sends the text to the scinitilla control
// created by CodeDisplayBase.
// CodeDisplayBase creates and initializes a wxStyledTextCtrl (scintilla) control, and places
// it in a sizer.

// WriteCode expects a class to override the doWrite() method, which in this case sends the
// text to the scinitilla control created by CodeDisplayBase.

class CodeDisplay : public CodeDisplayBase, public WriteCode
{
Expand All @@ -38,9 +39,11 @@ class CodeDisplay : public CodeDisplayBase, public WriteCode
wxStyledTextCtrl* GetTextCtrl() { return m_scintilla; };

protected:
void OnRibbonToolSelected(Node* node);
void OnEmbedImageSelected(Node* node);
void OnFind(wxFindDialogEvent& event);

// The following two functions are required to inherit from WriteCode
// The following function is required to inherit from WriteCode

void doWrite(tt_string_view code) override;

Expand Down