wm.cpp 13.9 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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
/*
 WM.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 "wm.hpp"
#include "wmhelper.hpp"
#include "hw.h"
#include "mouse.h"
#include "cursor.h"
#include "event.h"
#include "array.h"
#include "screen.h"
#include "clip.h"
#include "matrix.h"
#include "screen.h"
#include "poll.h"
#include "skin.h"
#include "fdtuicfg.h"
#if defined(__DOS__) || defined(__NT__) && !defined(__NTCRUSTY__)
#include <sys\types.h>
#include <dos.h>
#include <direct.h>
#else
#include <limits.h>
#include <unistd.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

#if !defined(PATH_MAX)
#define PATH_MAX                        4096
#endif

#define ARRAY_GROWBY                    30
wmhelper                                _wm_helper;
static window*                          _wm_grab;
static window*                          _wm_focused;
static struct array                     _wm_array;
static enum wm_error                    _wm_quit_flag;
#if (__WATCOMC__)
static unsigned int                     _saved_drive;
#endif
static char                             _saved_directory[PATH_MAX+1];

#ifdef __DOS__

int __far _int24h_handler(
    unsigned int                        i_device_error,
    unsigned int                        i_error_code,
    unsigned __far *                    i_device_header)
{
    return _HARDERR_FAIL;
}

#endif

extern enum wm_error
wm_init()
{
    enum wm_error                       l_exit;
    int                                 l_rc;

    l_exit= ERROR_NONE;
    _wm_quit_flag= ERROR_NONE;
    _wm_grab= reinterpret_cast<window*>(0);
    _wm_focused= reinterpret_cast<window*>(0);
    array_assign(&_wm_array, ARRAY_GROWBY);

#if defined(__WATCOMC__)
    _dos_getdrive(&_saved_drive);
    _getdcwd(_saved_drive, _saved_directory, sizeof(_saved_directory));
#elif defined(__NT__)
    GetCurrentDirectoryA(sizeof(_saved_directory), _saved_directory);
#else
    getcwd(_saved_directory, sizeof(_saved_directory));
#endif

    do
    {

        fdtuicfg_load();
        l_rc= screen_init();

        if (l_rc)
        {
            l_exit= ERROR_INIT_SCREEN;
            break;
        }

        skin_set_defaults();

        cursor_hide();

        screen_fill(
            _skin_desktop.m_fillc,
            _skin_desktop.m_fcolor, 
            _skin_desktop.m_bcolor);

        (void)mouse_init();

#if defined(__DOS__) || defined(__NT__) || defined(__DJGPP__)
        hw_detect_drives();
#endif

        mouse_show();

#ifdef __DOS__
        _harderr(_int24h_handler);
#endif

    }while(0);

    return l_exit;
}

extern void
wm_deinit()
{

    clip_cleanup();
    matrix_cleanup();
    array_discharge(&_wm_array);
    mouse_deinit();
    screen_deinit();

#if defined(__WATCOM_C__)
    unsigned                            l_total;
    _dos_setdrive(_saved_drive, &l_total);
    _chdir(_saved_directory);
#elif defined(__NT__)
    SetCurrentDirectoryA(_saved_directory);
#else
    chdir(_saved_directory);
#endif

    return;
}

extern void
wm_quit(
    enum wm_error const                 i_reason)
{
    _wm_quit_flag= i_reason;
    return;
}

extern void
wm_draw(
    window *const                       i_window)
{
    window *                            l_last;
    window *                            l_window;
    unsigned int                        l_slot;

    l_last= (i_window) ? i_window : _wm_focused;

    mouse_hide();
    screen_push(0);

    screen_fill(
        _skin_desktop.m_fillc,
        _skin_desktop.m_fcolor, 
        _skin_desktop.m_bcolor);

    for (l_slot= 0; _wm_array.m_taken > l_slot; l_slot++)
    {
        l_window= reinterpret_cast<window*>(_wm_array.m_base[l_slot]);
        if ((l_last != l_window) && (*l_window).get_visible())
        {
            (*l_window).set_damage(widget::DAMAGE_ALL);
            (*l_window).draw();
            (*l_window).set_damage();
        }
    }

    if (l_last)
    {
        if ((*l_last).get_visible())
        {
            (*l_last).draw();
        }
        (*l_last).set_damage();
    }

    screen_pop();
    mouse_show();

    return;
}

extern void
wm_draw_widget(
    widget *const                       i_widget)
{
    struct box                          l_box;
    struct box                          l_clip;
    group const*                        l_parent;

    do
    {

        if (false == (*i_widget).get_visible())
        {
            break;
        }

        l_parent= (*i_widget).get_parent();

        if (0 == l_parent)
        {
            (*i_widget).draw();
            break;
        }

        (*l_parent).get_box(l_clip);
        (*l_parent).translate_screen(&l_clip.m_pos_x, &l_clip.m_pos_y);
        l_clip.m_len_x-= 2;
        l_clip.m_len_y-= 2;

        matrix_push();

        do
        {

            matrix_translate((*l_parent).get_pos_x()+1, (*l_parent).get_pos_y()+1);

            l_parent= (*l_parent).get_parent();

            if (0 == l_parent)
            {
                break;
            }

            (*l_parent).get_box(l_box);
            (*l_parent).translate_screen(&l_box.m_pos_x, &l_box.m_pos_y);
            l_box.m_len_x-= 2;
            l_box.m_len_y-= 2;
            clip(&l_clip, &l_box);

        }while(1);


        l_clip.m_pos_x= 0;
        l_clip.m_pos_y= 0;
        matrix_transform(&l_clip.m_pos_x, &l_clip.m_pos_y);
        clip_push(&l_clip);

        (*i_widget).draw();

        clip_pop();
        matrix_pop();

        (*i_widget).set_damage();


    }while(0);

    return;
}

extern void
wm_register_window(
    window*const                        i_window)
{
    int                                 l_found;
    unsigned                            l_slot;
    window*                             l_window;

    l_found= 0;
    for (l_slot= 0; _wm_array.m_taken > l_slot; l_slot++)
    {
        l_window= (window*)_wm_array.m_base[l_slot];
        if (i_window == l_window)
        {
            l_found= 1;
            break;
        }
    }

    if (0 == l_found)
    {
        array_push(&_wm_array, i_window);
        if (_wm_focused)
        {
            (*_wm_focused).focus_leave();
        }
        _wm_focused= i_window;
        (*_wm_focused).focus_enter();
    }

    return;
}

static enum event_response
wm_event_keyboard(
    struct event_key const&             i_key)
{
    enum event_response                 l_response;

    do
    {

        if (_wm_helper.capture())
        {
            l_response= _wm_helper.event_key(i_key);
            break;
        }

        if (_wm_grab)
        {
            l_response= (*_wm_grab).event_key(i_key);
            break;
        }

        l_response= _wm_helper.event_key(_wm_focused, i_key);

        if (RESPONSE_NONE != l_response)
        {
            break;
        }

        l_response= (*_wm_focused).event_key(i_key);

        if (RESPONSE_NONE != l_response)
        {
            break;
        }

        if (SCAN_NONE != i_key.m_scan)
        {
            l_response= (*_wm_focused).event_key_default(i_key.m_scan);
        }

    }while(0);

    return l_response;
}

static enum event_response
wm_event_mouse(
    struct event_mouse const&           i_mouse)
{
    enum event_response                 l_response;
    int                                 l_hit;
    size_t                              l_slot;
    window*                             l_window;

    l_response= RESPONSE_NONE;

    do
    {

        if (_wm_helper.capture())
        {
            l_response= _wm_helper.event_mouse(i_mouse);
            break;
        }

        if (_wm_grab)
        {
            l_response= (*_wm_grab).event_mouse(i_mouse);
            break;
        }

        l_hit= (*_wm_focused).contains(i_mouse.m_pos_x, i_mouse.m_pos_y);

        if (l_hit)
        {
            l_response= _wm_helper.event_mouse(_wm_focused, i_mouse);
            if (RESPONSE_NONE != l_response)
            {
                break;
            }
            if (_wm_focused)
            {
                l_response= (*_wm_focused).event_mouse(i_mouse);
            }
            break;
        }

        if (0 == (MOUSE_BUTTON_CLICKED & i_mouse.m_state))
        {
            break;
        }

        for (l_slot= 0; _wm_array.m_taken > l_slot; l_slot++)
        {
            l_window= reinterpret_cast<window*>(_wm_array.m_base[l_slot]);
            if (l_window != _wm_focused)
            {
                l_hit= (*l_window).contains(i_mouse.m_pos_x, i_mouse.m_pos_y);
                if (l_hit && (*l_window).get_can_focus())
                {
                    (*_wm_focused).focus_leave();
                    _wm_focused= l_window;
                    wm_draw(l_window);
                    (*l_window).focus_enter();
                    l_response= _wm_helper.event_mouse(_wm_focused, i_mouse);
                    break;
                }
            }
        }

    }while(0);

    return l_response;
}

static void
wm_unregister_all()
{
    size_t                              l_slot;
    window*                             l_window;

    for (l_slot= 0; _wm_array.m_taken > l_slot; l_slot++)
    {
        l_window= reinterpret_cast<window*>(_wm_array.m_base[l_slot]);
        (void)(*l_window).close(true);
        delete l_window;
    }

    _wm_grab= 0;
    _wm_focused= 0;
    _wm_array.m_taken= 0;

    return;
}

static void
wm_focus_set()
{
    unsigned int                        l_slot;

    l_slot= _wm_array.m_taken;

    do
    {

	if (0 == l_slot)
	{
	    break;
	}

	l_slot--;
        _wm_focused= reinterpret_cast<window*>(_wm_array.m_base[l_slot]);

	if ((*_wm_focused).get_visible())
	{
	    (*_wm_focused).focus_enter();
	    break;
	}

    }while(1);

    return;
}

extern enum wm_error
wm_run()
{
    enum wm_error                       l_error;
    int                                 l_exit;
    struct poll_event                   l_event;

    l_error= ERROR_NONE;

    do
    {

        if (_wm_quit_flag)
        {
            break;
        }

	if (0 == _wm_focused || false == (*_wm_focused).get_visible())
	{

	    wm_focus_set();

	    if (0 == _wm_focused)
	    {
		break;
	    }

	}

        l_exit= poll_block(&l_event);

        if (0 == l_exit)
        {
            break;
        }

        if (EVENT_KEY == l_event.m_type)
        {
            if (ASCII_ESC == l_event.m_record.m_key.m_ascii)
            {
                break;
            }
            wm_event_keyboard(l_event.m_record.m_key);
        }
        else if (EVENT_MOUSE == l_event.m_type)
        {
            wm_event_mouse(l_event.m_record.m_mouse);
        }
#if !defined(__DOS__) && !defined(__DJGPP__)
        else if (EVENT_SCREEN_RESIZE == l_event.m_type)
        {
            screen_changed_size();
            wm_draw(_wm_focused);
        }
#endif

    }while(1);

    wm_unregister_all();

    return l_error;
}

extern enum event_response
wm_run_modal(
    widget&                             i_widget)
{
    struct poll_event                   l_event;
    int                                 l_exit;
    enum event_response                 l_response;

    i_widget.focus_enter();

    do
    {

        l_response= RESPONSE_NONE;

        l_exit= poll_block(&l_event);

        if (0 == l_exit)
        {
            break;
        }

        if (EVENT_KEY == l_event.m_type)
        {
            if (ASCII_ESC == l_event.m_record.m_key.m_ascii)
            {
                l_response= RESPONSE_CLOSE;
                break;
            }
            l_response= i_widget.event_key(l_event.m_record.m_key);
        }
        else if (EVENT_MOUSE == l_event.m_type)
        {
            l_response= i_widget.event_mouse(l_event.m_record.m_mouse);
        }
#if !defined(__DOS__) && !defined(__DJGPP__)
        else if (EVENT_SCREEN_RESIZE == l_event.m_type)
        {
            screen_changed_size();
            screen_push(1);
            if (_wm_focused)
            {
                wm_draw(_wm_focused);
            }
            wm_draw_widget(&i_widget);
            screen_pop();
        }
#endif

        if (l_exit)
        {
            if (
                RESPONSE_NONE != l_response &&
                RESPONSE_HANDLED != l_response)
            {
                break;
            }
        }

    }while(1);

    return l_response;
}

extern window*const
wm_grab(
    window*const                        i_window)
{
    window*                             l_prev;

    l_prev= _wm_grab;
    _wm_grab= i_window;

    return l_prev;
}

extern void
wm_unregister_window(
    window*const                        i_window)
{
    unsigned                            l_slot;
    window*                             l_window;

    if (_wm_grab == i_window)
    {
        _wm_grab= 0;
    }

    if (_wm_focused == i_window)
    {
        (*_wm_focused).focus_leave();
        _wm_focused= 0;
    }

    for (l_slot= 0; _wm_array.m_taken > l_slot; l_slot++)
    {
        l_window= (window*)_wm_array.m_base[l_slot];
        if (i_window == l_window)
        {
            array_remove(&_wm_array, l_slot);
            break;
        }
    }

    if (_wm_array.m_taken)
    {
        _wm_focused= reinterpret_cast<window*>
            (_wm_array.m_base[_wm_array.m_taken-1]);
         wm_draw(_wm_focused);
         (*_wm_focused).focus_enter();
    }
    else
    {
        _wm_quit_flag= ERROR_EMPTY_QUEUE;
    }

    return;
}