combobox.cpp 4.43 KB
Newer Older
Ercan Ersoy's avatar
Ercan Ersoy committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
/*
 COMBOBOX.CPP

 License CC0 PUBLIC DOMAIN

 To the extent possible under law, Mark J. Olesen has waived all copyright 
 and related or neighboring rights to FDOSTUI Library. This work is published 
 from: United States.
*/
#include "combobox.hpp"
#include "draw.h"
#include "skin.h"
#include "cursor.h"
#include "block.h"
#include "mouse.h"
#include "wm.hpp"

combobox::combobox(
    int const                           i_pos_x,
    int const                           i_pos_y,
    unsigned int const                  i_len_x,
    unsigned int const                  i_len_y) :
    entry(i_pos_x, i_pos_y, i_len_x, i_len_y),
    m_listbox(i_pos_x, i_pos_y+1, i_len_x, 10)
{

    m_listbox.set_response(RESPONSE_ACCEPT);

    return;
}

combobox::~combobox()
{
    return;
}

void
combobox::draw() const
{
    int                                 l_pos_y;

    entry::draw();

    l_pos_y= m_box.m_pos_y + (m_box.m_len_y / 2);

    draw_putc(
        m_box.m_pos_x + m_box.m_len_x - 1,
        l_pos_y,
        _skin_buttons.m_down,
        1,
        _skin_slider.m_fcolor,
        _skin_slider.m_bcolor);
        
    return;
}

enum event_response
combobox::event_key(
    struct event_key const&             i_event)
{
    enum event_response                 l_response;

    l_response= RESPONSE_NONE;

    if (SCAN_DOWN == i_event.m_scan)
    {
        popup();
        l_response= RESPONSE_HANDLED;
    }

    return l_response;
}

enum event_response
combobox::event_mouse(
    struct event_mouse const&           i_event)
{
    enum event_response                 l_response;
    int                                 l_pos_y;

    l_response= RESPONSE_NONE;

    do
    {

        l_pos_y= m_box.m_pos_y + (m_box.m_len_y / 2);

        if (static_cast<int>(i_event.m_pos_y) == static_cast<int>(l_pos_y) &&
            static_cast<int>(i_event.m_pos_x) == 
            static_cast<int>(m_box.m_pos_x + m_box.m_len_x - 1))
        {
            if (MOUSE_BUTTON_CLICKED == i_event.m_state)
            {
                popup();
                l_response= RESPONSE_HANDLED;
            }
            break;
        }

        l_response= entry::event_mouse(i_event);

    }while(0);

    return l_response;
}

void
combobox::listbox_configure()
{
    struct box                          l_box;
    size_t                              l_items;

    translate_screen(&l_box.m_pos_x, &l_box.m_pos_y);

    if (0 > l_box.m_pos_x)
    {
        l_box.m_pos_x= 0;
    }

    l_box.m_len_x= m_box.m_len_x;
    l_items= m_listbox.get_count();

    if (_video_rows > l_items)
    {
        l_box.m_len_y= 2 + l_items;
    }
    else
    {
        l_box.m_pos_y= 0;
        l_box.m_len_y= _video_rows;
    }

    do
    {

        if (_video_rows >= l_box.m_pos_y + l_box.m_len_y)
        {
            break;
        }

        l_box.m_pos_y--;

    }while(1);

    m_listbox.move(l_box.m_pos_x, l_box.m_pos_y);
    m_listbox.resize(l_box.m_len_x, l_box.m_len_y);
    m_listbox.unselect_all();

    return;
}

void
combobox::popup()
{
    void __FAR*                         l_backing_store;
    struct box                          l_box;
    unsigned char const*                l_item;
    enum event_response                 l_response;
    int                                 l_selected;

    do
    {

        listbox_configure();
        m_listbox.get_box(l_box);

        cursor_hide();

        mouse_hide();
        l_backing_store= block_read(&l_box);
        m_listbox.draw();
        mouse_show();
    
        l_response= wm_run_modal(m_listbox);
    
        mouse_hide();
        block_write(l_backing_store, &l_box);
        mouse_show();
    
        block_free(l_backing_store);

        if (RESPONSE_ACCEPT == l_response)
        {
            l_selected= m_listbox.get_selected_first();
            l_item= m_listbox.get_item(l_selected);
            set_text(l_item);
            set_damage(DAMAGE_VALUE);
            wm_draw_widget(this);
            emit_changed();
        }

        show_cursor();

    }while(0);

#if defined(__CURSES__)
    if (0 == (CURSOR_CAN_HIDE & _cursor_features))
    {
        int                             l_cur_x;
        int                             l_cur_y;
        translate_screen(&l_cur_x, &l_cur_y);
        ::move(l_cur_y, l_cur_x);
    }
#endif

    return;
}