<commit_before>#include "viewer_window_list.h"

#include "allrefs.h"
#include "clipboard.h"
#include "commandref.h"
#include "string_util.h"

#include <boost/range/iterator_range.hpp>

#include "XPLMGraphics.h"

#include <chrono>
#include <iterator>
#include <string>

#include "../lib/glew/glew.h"

#if APL
#include <OpenGL/gl.h>
#endif

const XPLMFontID font = xplmFont_Basic;

ViewerWindowList::ViewerWindowList(XPWidgetID window, std::vector<RefRecord *> & results)
: results(results) {

    XPLMGetFontDimensions(font, &fontwidth, &fontheight, nullptr);
    list_widget = XPCreateCustomWidget(0, 0, 1, 1, 1,"", 0, window, listCallback);
    XPSetWidgetProperty(list_widget, xpProperty_Object, (intptr_t)this);

    scroll_bar_widget = XPCreateWidget(0, 0, 1, 1, 1,"", 0, window, xpWidgetClass_ScrollBar);
    XPSetWidgetProperty(scroll_bar_widget, xpProperty_ScrollBarType, xpScrollBarTypeScrollBar);	//might need changing
    updateScroll();

    edit_field = XPCreateWidget(0, 0, 1, 1, 1,"", 0, window, xpWidgetClass_TextField);
    XPSetWidgetProperty(edit_field, xpProperty_TextFieldType, xpTextEntryField);
    XPAddWidgetCallback(edit_field, editFieldCallback);
    XPSetWidgetProperty(edit_field, xpProperty_Object, (intptr_t)this);
    XPHideWidget(edit_field);
}


std::string ViewerWindowList::getEditText() const {
    char editfield_text[1024];
    XPGetWidgetDescriptor(edit_field, editfield_text, 1024);
    return std::string(editfield_text);
}

intptr_t ViewerWindowList::getEditSelectionStart() const {
    return XPGetWidgetProperty(edit_field, xpProperty_EditFieldSelStart, NULL);
}

intptr_t ViewerWindowList::getEditSelectionStop() const {
    return XPGetWidgetProperty(edit_field, xpProperty_EditFieldSelEnd, NULL);
}

void ViewerWindowList::setEditSelection(intptr_t start, intptr_t stop) {
    XPSetWidgetProperty(edit_field, xpProperty_EditFieldSelStart, start);
    XPSetWidgetProperty(edit_field, xpProperty_EditFieldSelEnd, stop);
}

void ViewerWindowList::deselectEditField() {
    edit_modified = false;
    select_edit_dataref = nullptr;
    selected_command = nullptr;
    XPLoseKeyboardFocus(edit_field);
    XPHideWidget(edit_field);
}

int ViewerWindowList::listCallback(XPWidgetMessage  inMessage, XPWidgetID  inWidget, intptr_t inParam1, intptr_t) {
    ViewerWindowList * obj = (ViewerWindowList *) XPGetWidgetProperty(inWidget, xpProperty_Object, nullptr);
    switch(inMessage) {
        case xpMsg_Draw:
            obj->draw();
            return 1;
        case xpMsg_MouseDown: {
            XPMouseState_t * mouse_info = (XPMouseState_t *) inParam1;
            if(mouse_info->button == 0) {
                return obj->leftClick(mouse_info->x, mouse_info->y) ? 1 : 0;
            }

            return 0;
        }
    }
    return 0;
}

bool ViewerWindowList::leftClick(int x, int y) {
    int list_left, list_top, list_bottom, list_right;
    XPGetWidgetGeometry(list_widget, &list_left, &list_top, &list_right, &list_bottom);

    x -= list_left;

    int y_from_top = list_top - y;

    const int scroll_position = getScrollPosition();
    int list_y = y_from_top + getScrollPosition();
    int element_index = list_y / fontheight;

    int element_top = list_top + scroll_position - element_index * fontheight;
    int element_bottom = element_top - fontheight;

    // if not a click on an element
    if(element_index < 0 || element_index >= static_cast<int>(results.size())) {
        return false;
    }

    RefRecord * record = results[element_index];
    const std::string & name = record->getName();
    float dataref_name_width = XPLMMeasureString(font, name.c_str(), int(name.size()) + 1);
    float dataref_name_width_plus_eq = XPLMMeasureString(font, (name + "=").c_str(), int(name.size()) + 2);

    const int box_padding_x = 2, box_padding_y = 2;

    DataRefRecord * dr_record = dynamic_cast<DataRefRecord *>(record);
    CommandRefRecord * cr_record = dynamic_cast<CommandRefRecord *>(record);
    if(nullptr != dr_record) {
        if(x < dataref_name_width) {
            XPSetWidgetGeometry(edit_field, list_left, element_top + box_padding_y, list_left + dataref_name_width + box_padding_x, element_bottom - box_padding_y);
            XPSetWidgetDescriptor(edit_field, name.c_str());
            setEditSelection(0, name.size());
        } else {
            const std::string value_str = dr_record->getEditString();
            select_edit_dataref = dr_record;
            selected_command = nullptr;
            XPSetWidgetGeometry(edit_field, list_left + dataref_name_width_plus_eq - box_padding_x, element_top + box_padding_y, list_right, element_bottom - box_padding_y);
            XPSetWidgetDescriptor(edit_field, value_str.c_str());
            setEditSelection(0, value_str.size());
        }
        XPShowWidget(edit_field);
        edit_modified = false;
    } else  if(nullptr != cr_record) {
        selected_command = cr_record;
        select_edit_dataref = nullptr;
        XPHideWidget(edit_field);
    } else {
        selected_command = nullptr;
        select_edit_dataref = nullptr;
    }
    return 1;
}

size_t ViewerWindowList::getScrollPosition() const {
    const int scroll_pos = (int)XPGetWidgetProperty(scroll_bar_widget, xpProperty_ScrollBarSliderPosition, nullptr);
    const int scroll_pos_max = (int)XPGetWidgetProperty(scroll_bar_widget, xpProperty_ScrollBarMax, nullptr);
    return scroll_pos_max - scroll_pos;
}<commit_msg>Fix missing text field for commands.<commit_after>#include "viewer_window_list.h"

#include "allrefs.h"
#include "clipboard.h"
#include "commandref.h"
#include "string_util.h"

#include <boost/range/iterator_range.hpp>

#include "XPLMGraphics.h"

#include <chrono>
#include <iterator>
#include <string>

#include "../lib/glew/glew.h"

#if APL
#include <OpenGL/gl.h>
#endif

const XPLMFontID font = xplmFont_Basic;

ViewerWindowList::ViewerWindowList(XPWidgetID window, std::vector<RefRecord *> & results)
: results(results) {

    XPLMGetFontDimensions(font, &fontwidth, &fontheight, nullptr);
    list_widget = XPCreateCustomWidget(0, 0, 1, 1, 1,"", 0, window, listCallback);
    XPSetWidgetProperty(list_widget, xpProperty_Object, (intptr_t)this);

    scroll_bar_widget = XPCreateWidget(0, 0, 1, 1, 1,"", 0, window, xpWidgetClass_ScrollBar);
    XPSetWidgetProperty(scroll_bar_widget, xpProperty_ScrollBarType, xpScrollBarTypeScrollBar);	//might need changing
    updateScroll();

    edit_field = XPCreateWidget(0, 0, 1, 1, 1,"", 0, window, xpWidgetClass_TextField);
    XPSetWidgetProperty(edit_field, xpProperty_TextFieldType, xpTextEntryField);
    XPAddWidgetCallback(edit_field, editFieldCallback);
    XPSetWidgetProperty(edit_field, xpProperty_Object, (intptr_t)this);
    XPHideWidget(edit_field);
}


std::string ViewerWindowList::getEditText() const {
    char editfield_text[1024];
    XPGetWidgetDescriptor(edit_field, editfield_text, 1024);
    return std::string(editfield_text);
}

intptr_t ViewerWindowList::getEditSelectionStart() const {
    return XPGetWidgetProperty(edit_field, xpProperty_EditFieldSelStart, NULL);
}

intptr_t ViewerWindowList::getEditSelectionStop() const {
    return XPGetWidgetProperty(edit_field, xpProperty_EditFieldSelEnd, NULL);
}

void ViewerWindowList::setEditSelection(intptr_t start, intptr_t stop) {
    XPSetWidgetProperty(edit_field, xpProperty_EditFieldSelStart, start);
    XPSetWidgetProperty(edit_field, xpProperty_EditFieldSelEnd, stop);
}

void ViewerWindowList::deselectEditField() {
    edit_modified = false;
    select_edit_dataref = nullptr;
    selected_command = nullptr;
    XPLoseKeyboardFocus(edit_field);
    XPHideWidget(edit_field);
}

int ViewerWindowList::listCallback(XPWidgetMessage  inMessage, XPWidgetID  inWidget, intptr_t inParam1, intptr_t) {
    ViewerWindowList * obj = (ViewerWindowList *) XPGetWidgetProperty(inWidget, xpProperty_Object, nullptr);
    switch(inMessage) {
        case xpMsg_Draw:
            obj->draw();
            return 1;
        case xpMsg_MouseDown: {
            XPMouseState_t * mouse_info = (XPMouseState_t *) inParam1;
            if(mouse_info->button == 0) {
                return obj->leftClick(mouse_info->x, mouse_info->y) ? 1 : 0;
            }

            return 0;
        }
    }
    return 0;
}

bool ViewerWindowList::leftClick(int x, int y) {
    int list_left, list_top, list_bottom, list_right;
    XPGetWidgetGeometry(list_widget, &list_left, &list_top, &list_right, &list_bottom);

    x -= list_left;

    int y_from_top = list_top - y;

    const int scroll_position = getScrollPosition();
    int list_y = y_from_top + getScrollPosition();
    int element_index = list_y / fontheight;

    int element_top = list_top + scroll_position - element_index * fontheight;
    int element_bottom = element_top - fontheight;

    // if not a click on an element
    if(element_index < 0 || element_index >= static_cast<int>(results.size())) {
        return false;
    }

    RefRecord * record = results[element_index];
    const std::string & name = record->getName();
    float dataref_name_width = XPLMMeasureString(font, name.c_str(), int(name.size()) + 1);
    float dataref_name_width_plus_eq = XPLMMeasureString(font, (name + "=").c_str(), int(name.size()) + 2);

    const int box_padding_x = 2, box_padding_y = 2;

    DataRefRecord * dr_record = dynamic_cast<DataRefRecord *>(record);
    CommandRefRecord * cr_record = dynamic_cast<CommandRefRecord *>(record);
    if(nullptr != dr_record) {
        if(x < dataref_name_width) {
            XPSetWidgetGeometry(edit_field, list_left, element_top + box_padding_y, list_left + dataref_name_width + box_padding_x, element_bottom - box_padding_y);
            XPSetWidgetDescriptor(edit_field, name.c_str());
            setEditSelection(0, name.size());
        } else {
            const std::string value_str = dr_record->getEditString();
            select_edit_dataref = dr_record;
            selected_command = nullptr;
            XPSetWidgetGeometry(edit_field, list_left + dataref_name_width_plus_eq - box_padding_x, element_top + box_padding_y, list_right, element_bottom - box_padding_y);
            XPSetWidgetDescriptor(edit_field, value_str.c_str());
            setEditSelection(0, value_str.size());
        }
        XPShowWidget(edit_field);
        edit_modified = false;
    } else  if(nullptr != cr_record) {
        selected_command = cr_record;
        select_edit_dataref = nullptr;

        XPSetWidgetGeometry(edit_field, list_left, element_top + box_padding_y, list_left + dataref_name_width + box_padding_x, element_bottom - box_padding_y);
        XPSetWidgetDescriptor(edit_field, name.c_str());
        setEditSelection(0, name.size());
        XPShowWidget(edit_field);
        edit_modified = false;
    } else {
        selected_command = nullptr;
        select_edit_dataref = nullptr;
        XPHideWidget(edit_field);
    }
    return 1;
}

size_t ViewerWindowList::getScrollPosition() const {
    const int scroll_pos = (int)XPGetWidgetProperty(scroll_bar_widget, xpProperty_ScrollBarSliderPosition, nullptr);
    const int scroll_pos_max = (int)XPGetWidgetProperty(scroll_bar_widget, xpProperty_ScrollBarMax, nullptr);
    return scroll_pos_max - scroll_pos;
}
{'fraction_non_alphanumeric': 0.10287699296339183, 'fraction_numerical': 0.007571034114189009, 'mean_word_length': 4.198148148148148, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '785150', 'n_tokens_mistral': 3766, 'n_tokens_neox': 3642, 'n_words': 687}