Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1 | pmbaty | 1 | /* |
| 2 | * Portions of this file are copyright Rebirth contributors and licensed as |
||
| 3 | * described in COPYING.txt. |
||
| 4 | * Portions of this file are copyright Parallax Software and licensed |
||
| 5 | * according to the Parallax license below. |
||
| 6 | * See COPYING.txt for license details. |
||
| 7 | |||
| 8 | THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX |
||
| 9 | SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO |
||
| 10 | END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A |
||
| 11 | ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS |
||
| 12 | IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS |
||
| 13 | SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE |
||
| 14 | FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE |
||
| 15 | CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS |
||
| 16 | AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE. |
||
| 17 | COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED. |
||
| 18 | */ |
||
| 19 | |||
| 20 | |||
| 21 | #include <stdlib.h> |
||
| 22 | #include <string.h> |
||
| 23 | |||
| 24 | #include "u_mem.h" |
||
| 25 | #include "maths.h" |
||
| 26 | #include "pstypes.h" |
||
| 27 | #include "event.h" |
||
| 28 | #include "gr.h" |
||
| 29 | #include "ui.h" |
||
| 30 | #include "key.h" |
||
| 31 | #include "mouse.h" |
||
| 32 | |||
| 33 | namespace dcx { |
||
| 34 | |||
| 35 | void ui_draw_inputbox( UI_DIALOG *dlg, UI_GADGET_INPUTBOX * inputbox ) |
||
| 36 | { |
||
| 37 | #if 0 //ndef OGL |
||
| 38 | if ((inputbox->status==1) || (inputbox->position != inputbox->oldposition)) |
||
| 39 | #endif |
||
| 40 | { |
||
| 41 | gr_set_current_canvas( inputbox->canvas ); |
||
| 42 | auto &canvas = *grd_curcanv; |
||
| 43 | |||
| 44 | gr_rect(canvas, 0, 0, inputbox->width-1, inputbox->height-1, CBLACK); |
||
| 45 | |||
| 46 | int w, h; |
||
| 47 | gr_get_string_size(*canvas.cv_font, inputbox->text.get(), &w, &h, nullptr); |
||
| 48 | if (dlg->keyboard_focus_gadget == inputbox) |
||
| 49 | { |
||
| 50 | if (inputbox->first_time) |
||
| 51 | { |
||
| 52 | gr_set_fontcolor(canvas, CBLACK, -1); |
||
| 53 | gr_rect(canvas, 2, 2, 2 + w, 2 + h, CRED); |
||
| 54 | } |
||
| 55 | else |
||
| 56 | gr_set_fontcolor(canvas, CRED, -1); |
||
| 57 | } |
||
| 58 | else |
||
| 59 | gr_set_fontcolor(canvas, CWHITE, -1); |
||
| 60 | |||
| 61 | inputbox->status = 0; |
||
| 62 | |||
| 63 | gr_string(canvas, *canvas.cv_font, 2, 2, inputbox->text.get(), w, h); |
||
| 64 | |||
| 65 | if (dlg->keyboard_focus_gadget == inputbox && !inputbox->first_time ) |
||
| 66 | { |
||
| 67 | const uint8_t cred = CRED; |
||
| 68 | Vline(canvas, 2, inputbox->height - 3, 3 + w, cred); |
||
| 69 | Vline(canvas, 2, inputbox->height - 3, 4 + w, cred); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | std::unique_ptr<UI_GADGET_INPUTBOX> ui_add_gadget_inputbox(UI_DIALOG * dlg, short x, short y, short length, short slength, const char * text) |
||
| 75 | { |
||
| 76 | int h, aw; |
||
| 77 | gr_get_string_size(*grd_curcanv->cv_font, nullptr, nullptr, &h, &aw); |
||
| 78 | std::unique_ptr<UI_GADGET_INPUTBOX> inputbox{ui_gadget_add<UI_GADGET_INPUTBOX>(dlg, x, y, x+aw*slength-1, y+h-1+4)}; |
||
| 79 | MALLOC(inputbox->text, char[], length + 1); |
||
| 80 | auto ltext = strlen(text) + 1; |
||
| 81 | memcpy(inputbox->text.get(), text, ltext); |
||
| 82 | inputbox->position = ltext - 1; |
||
| 83 | inputbox->oldposition = inputbox->position; |
||
| 84 | inputbox->width = aw*slength; |
||
| 85 | inputbox->height = h+4; |
||
| 86 | inputbox->length = length; |
||
| 87 | inputbox->slength = slength; |
||
| 88 | inputbox->pressed = 0; |
||
| 89 | inputbox->first_time = 1; |
||
| 90 | return inputbox; |
||
| 91 | } |
||
| 92 | |||
| 93 | window_event_result ui_inputbox_do( UI_DIALOG *dlg, UI_GADGET_INPUTBOX * inputbox,const d_event &event ) |
||
| 94 | { |
||
| 95 | unsigned char ascii; |
||
| 96 | int keypress = 0; |
||
| 97 | |||
| 98 | if (event.type == EVENT_KEY_COMMAND) |
||
| 99 | keypress = event_key_get(event); |
||
| 100 | |||
| 101 | inputbox->oldposition = inputbox->position; |
||
| 102 | inputbox->pressed=0; |
||
| 103 | |||
| 104 | window_event_result rval = window_event_result::ignored; |
||
| 105 | if (dlg->keyboard_focus_gadget==inputbox) |
||
| 106 | { |
||
| 107 | switch( keypress ) |
||
| 108 | { |
||
| 109 | case 0: |
||
| 110 | break; |
||
| 111 | case (KEY_LEFT): |
||
| 112 | case (KEY_BACKSP): |
||
| 113 | if (inputbox->position > 0) |
||
| 114 | inputbox->position--; |
||
| 115 | inputbox->text[inputbox->position] = 0; |
||
| 116 | inputbox->status = 1; |
||
| 117 | if (inputbox->first_time) inputbox->first_time = 0; |
||
| 118 | rval = window_event_result::handled; |
||
| 119 | break; |
||
| 120 | case (KEY_ENTER): |
||
| 121 | inputbox->pressed=1; |
||
| 122 | inputbox->status = 1; |
||
| 123 | if (inputbox->first_time) inputbox->first_time = 0; |
||
| 124 | rval = window_event_result::handled; |
||
| 125 | break; |
||
| 126 | default: |
||
| 127 | ascii = key_ascii(); |
||
| 128 | if ((ascii < 255 ) && (inputbox->position < inputbox->length-2)) |
||
| 129 | { |
||
| 130 | if (inputbox->first_time) { |
||
| 131 | inputbox->first_time = 0; |
||
| 132 | inputbox->position = 0; |
||
| 133 | } |
||
| 134 | inputbox->text[inputbox->position++] = ascii; |
||
| 135 | inputbox->text[inputbox->position] = 0; |
||
| 136 | rval = window_event_result::handled; |
||
| 137 | } |
||
| 138 | inputbox->status = 1; |
||
| 139 | break; |
||
| 140 | } |
||
| 141 | } else { |
||
| 142 | inputbox->first_time = 1; |
||
| 143 | } |
||
| 144 | |||
| 145 | if (inputbox->pressed) |
||
| 146 | { |
||
| 147 | rval = ui_gadget_send_event(dlg, EVENT_UI_GADGET_PRESSED, inputbox); |
||
| 148 | if (rval == window_event_result::ignored) |
||
| 149 | rval = window_event_result::handled; |
||
| 150 | } |
||
| 151 | |||
| 152 | if (event.type == EVENT_WINDOW_DRAW) |
||
| 153 | ui_draw_inputbox( dlg, inputbox ); |
||
| 154 | |||
| 155 | return rval; |
||
| 156 | } |
||
| 157 | |||
| 158 | void ui_inputbox_set_text(UI_GADGET_INPUTBOX *inputbox, const char *text) |
||
| 159 | { |
||
| 160 | auto ltext = std::min(static_cast<std::size_t>(inputbox->length), strlen(text)); |
||
| 161 | memcpy(inputbox->text.get(), text, ltext); |
||
| 162 | inputbox->text[ltext] = 0; |
||
| 163 | inputbox->position = ltext; |
||
| 164 | inputbox->oldposition = inputbox->position; |
||
| 165 | inputbox->status = 1; // redraw |
||
| 166 | inputbox->first_time = 1; // select all |
||
| 167 | } |
||
| 168 | |||
| 169 | } |