| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* GATE PROJECT LICENSE: | ||
| 2 | +----------------------------------------------------------------------------+ | ||
| 3 | | Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> | | ||
| 4 | | All rights reserved. | | ||
| 5 | | | | ||
| 6 | | Redistribution and use in source and binary forms, with or without | | ||
| 7 | | modification, are permitted provided that the following conditions are met:| | ||
| 8 | | | | ||
| 9 | | 1. Redistributions of source code must retain the above copyright notice, | | ||
| 10 | | this list of conditions and the following disclaimer. | | ||
| 11 | | 2. Redistributions in binary form must reproduce the above copyright | | ||
| 12 | | notice, this list of conditions and the following disclaimer in the | | ||
| 13 | | documentation and/or other materials provided with the distribution. | | ||
| 14 | | | | ||
| 15 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"| | ||
| 16 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | ||
| 17 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | ||
| 18 | | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | | ||
| 19 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | ||
| 20 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | ||
| 21 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | ||
| 22 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | ||
| 23 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | ||
| 24 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | | ||
| 25 | | THE POSSIBILITY OF SUCH DAMAGE. | | ||
| 26 | +----------------------------------------------------------------------------+ | ||
| 27 | */ | ||
| 28 | |||
| 29 | #include "gate/ui/textboxes.h" | ||
| 30 | #include "gate/results.h" | ||
| 31 | |||
| 32 | #if defined(GATE_UI_WINAPI) | ||
| 33 | |||
| 34 | |||
| 35 | #include "gate/ui/gateui_winapi.h" | ||
| 36 | #include "gate/platforms.h" | ||
| 37 | |||
| 38 | static gate_bool_t gate_ui_textbox_events(void* hwnd, gate_ui_ctrl_t* ctrl, gate_uint32_t msg, gate_uintptr_t wParam, gate_intptr_t lParam, gate_intptr_t* lresult) | ||
| 39 | { | ||
| 40 | if ((ctrl != NULL) && (hwnd != NULL)) | ||
| 41 | { | ||
| 42 | HWND hwnd_ctrl = GATE_UI_WINAPI_GET_HWND(ctrl); | ||
| 43 | if ((HWND)hwnd == hwnd_ctrl) | ||
| 44 | { | ||
| 45 | gate_ui_textbox_t* txtbx = (gate_ui_textbox_t*)ctrl; | ||
| 46 | switch (msg) | ||
| 47 | { | ||
| 48 | case WM_GETDLGCODE: | ||
| 49 | { | ||
| 50 | gate_uint32_t styles = gate_ui_winapi_get_window_styles(hwnd); | ||
| 51 | if (GATE_FLAG_ENABLED(styles, ES_MULTILINE)) | ||
| 52 | { | ||
| 53 | /* multi-line */ | ||
| 54 | if (GATE_FLAG_ENABLED(styles, ES_READONLY)) | ||
| 55 | { | ||
| 56 | *lresult = DLGC_WANTARROWS; | ||
| 57 | } | ||
| 58 | else | ||
| 59 | { | ||
| 60 | *lresult = DLGC_WANTCHARS | DLGC_WANTARROWS | DLGC_HASSETSEL | DLGC_WANTALLKEYS; | ||
| 61 | } | ||
| 62 | } | ||
| 63 | else | ||
| 64 | { | ||
| 65 | /* single-line */ | ||
| 66 | *lresult = DLGC_WANTCHARS | DLGC_WANTARROWS | DLGC_HASSETSEL; | ||
| 67 | } | ||
| 68 | return true; | ||
| 69 | } | ||
| 70 | case WM_CHAR: | ||
| 71 | { | ||
| 72 | gate_char32_t chr, chr2; | ||
| 73 | chr2 = chr = (gate_char32_t)wParam; | ||
| 74 | if (txtbx->on_char != NULL) | ||
| 75 | { | ||
| 76 | txtbx->on_char(ctrl, &chr); | ||
| 77 | if (chr2 != chr) | ||
| 78 | { | ||
| 79 | if (chr == 0) | ||
| 80 | { | ||
| 81 | *lresult = 0; | ||
| 82 | return true; | ||
| 83 | } | ||
| 84 | else | ||
| 85 | { | ||
| 86 | WNDPROC wndproc = (WNDPROC)(gate_uintptr_t)GATE_UI_WINAPI_GET_HWND_PROC(ctrl); | ||
| 87 | if (wndproc != NULL) | ||
| 88 | { | ||
| 89 | *lresult = wndproc(hwnd_ctrl, msg, (WPARAM)chr, (LPARAM)lParam); | ||
| 90 | return true; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | } | ||
| 94 | } | ||
| 95 | break; | ||
| 96 | } | ||
| 97 | case WM_KEYDOWN: | ||
| 98 | { | ||
| 99 | if (wParam == VK_TAB) | ||
| 100 | { | ||
| 101 | *lresult = 0; | ||
| 102 | return true; | ||
| 103 | } | ||
| 104 | if ((wParam == VK_RETURN) && ((gate_ui_winapi_get_window_styles(hwnd) & ES_MULTILINE) == 0)) | ||
| 105 | { | ||
| 106 | *lresult = 0; | ||
| 107 | return true; | ||
| 108 | } | ||
| 109 | break; | ||
| 110 | } | ||
| 111 | case WM_KEYUP: | ||
| 112 | { | ||
| 113 | if (wParam == VK_TAB) | ||
| 114 | { | ||
| 115 | *lresult = 0; | ||
| 116 | return true; | ||
| 117 | } | ||
| 118 | if ((wParam == VK_RETURN) && ((gate_ui_winapi_get_window_styles(hwnd) & ES_MULTILINE) == 0)) | ||
| 119 | { | ||
| 120 | *lresult = 0; | ||
| 121 | return true; | ||
| 122 | } | ||
| 123 | break; | ||
| 124 | } | ||
| 125 | } | ||
| 126 | } | ||
| 127 | } | ||
| 128 | return false; | ||
| 129 | } | ||
| 130 | |||
| 131 | gate_result_t gate_ui_textbox_create( | ||
| 132 | gate_ui_textbox_t* txtbox, gate_ui_ctrl_t* parent, | ||
| 133 | gate_ui_position_t const* position, | ||
| 134 | gate_string_t const* caption, | ||
| 135 | gate_uint32_t flags, | ||
| 136 | void* userparam | ||
| 137 | ) | ||
| 138 | { | ||
| 139 | gate_result_t ret; | ||
| 140 | |||
| 141 | do | ||
| 142 | { | ||
| 143 | gate_uint32_t styles = WS_CHILD | WS_CLIPSIBLINGS | WS_TABSTOP | ES_LEFT; | ||
| 144 | gate_uint32_t exstyles = 0; | ||
| 145 | HWND hwndParent; | ||
| 146 | gate_ui_host_t* host = gate_ui_ctrl_get_host(parent); | ||
| 147 | if (host == NULL) | ||
| 148 | { | ||
| 149 | ret = GATE_RESULT_INVALIDSTATE; | ||
| 150 | break; | ||
| 151 | } | ||
| 152 | hwndParent = GATE_UI_WINAPI_GET_HWND(parent); | ||
| 153 | if (hwndParent == NULL) | ||
| 154 | { | ||
| 155 | ret = GATE_RESULT_INVALIDSTATE; | ||
| 156 | break; | ||
| 157 | } | ||
| 158 | |||
| 159 | gate_mem_clear(txtbox, sizeof(gate_ui_textbox_t)); | ||
| 160 | |||
| 161 | #if !defined(GATE_SYS_WIN16) | ||
| 162 | exstyles |= WS_EX_CLIENTEDGE; | ||
| 163 | #endif | ||
| 164 | if (GATE_FLAG_ENABLED(flags, GATE_UI_FLAG_TEXTBOX_MULTILINE)) | ||
| 165 | { | ||
| 166 | styles |= (ES_MULTILINE | ES_WANTRETURN); | ||
| 167 | if (GATE_FLAG_ENABLED(flags, GATE_UI_FLAG_TEXTBOX_HSCROLL)) | ||
| 168 | { | ||
| 169 | styles |= WS_HSCROLL; | ||
| 170 | } | ||
| 171 | if (GATE_FLAG_ENABLED(flags, GATE_UI_FLAG_TEXTBOX_VSCROLL)) | ||
| 172 | { | ||
| 173 | styles |= WS_VSCROLL; | ||
| 174 | } | ||
| 175 | } | ||
| 176 | if (GATE_FLAG_ENABLED(flags, GATE_UI_FLAG_TEXTBOX_RETURNKEY)) | ||
| 177 | { | ||
| 178 | styles |= ES_WANTRETURN; | ||
| 179 | } | ||
| 180 | if (GATE_FLAG_ENABLED(flags, GATE_UI_FLAG_TEXTBOX_READONLY)) | ||
| 181 | { | ||
| 182 | styles |= ES_READONLY; | ||
| 183 | } | ||
| 184 | else | ||
| 185 | { | ||
| 186 | styles |= (ES_AUTOHSCROLL | ES_AUTOVSCROLL); | ||
| 187 | } | ||
| 188 | |||
| 189 | if (!GATE_FLAG_ENABLED(flags, GATE_UI_FLAG_ENABLED)) styles |= WS_DISABLED; | ||
| 190 | if (GATE_FLAG_ENABLED(flags, GATE_UI_FLAG_VISIBLE)) styles |= WS_VISIBLE; | ||
| 191 | |||
| 192 | ret = gate_ui_winapi_create(&txtbox->ctrl, host, hwndParent, _T("EDIT"), position, styles, exstyles, caption, userparam, true); | ||
| 193 | if (GATE_SUCCEEDED(ret)) | ||
| 194 | { | ||
| 195 | HWND hwnd = GATE_UI_WINAPI_GET_HWND(&txtbox->ctrl); | ||
| 196 | gate_ui_winapi_register_event(host, hwnd, WM_CHAR, &gate_ui_textbox_events, &txtbox->ctrl); | ||
| 197 | gate_ui_winapi_register_event(host, hwnd, WM_GETDLGCODE, &gate_ui_textbox_events, &txtbox->ctrl); | ||
| 198 | gate_ui_winapi_register_event(host, hwnd, WM_KEYDOWN, &gate_ui_textbox_events, &txtbox->ctrl); | ||
| 199 | gate_ui_winapi_register_event(host, hwnd, WM_KEYUP, &gate_ui_textbox_events, &txtbox->ctrl); | ||
| 200 | |||
| 201 | } | ||
| 202 | } while (0); | ||
| 203 | |||
| 204 | return ret; | ||
| 205 | } | ||
| 206 | |||
| 207 | gate_result_t gate_ui_textbox_get_line(gate_ui_textbox_t* txtbox, gate_uint32_t* currentLine, gate_uint32_t* lineCount) | ||
| 208 | { | ||
| 209 | gate_result_t ret = GATE_RESULT_OK; | ||
| 210 | HWND hwnd = GATE_UI_WINAPI_GET_HWND(&txtbox->ctrl); | ||
| 211 | LRESULT result; | ||
| 212 | if (lineCount != NULL) | ||
| 213 | { | ||
| 214 | result = SendMessage(hwnd, EM_GETLINECOUNT, 0, 0); | ||
| 215 | if (result < 1) | ||
| 216 | { | ||
| 217 | *lineCount = (gate_uint32_t)result; | ||
| 218 | ret = GATE_RESULT_FAILED; | ||
| 219 | } | ||
| 220 | } | ||
| 221 | if (currentLine != NULL) | ||
| 222 | { | ||
| 223 | result = SendMessage(hwnd, EM_LINEFROMCHAR, -1, 0); | ||
| 224 | *currentLine = (gate_uint32_t)result; | ||
| 225 | } | ||
| 226 | return ret; | ||
| 227 | } | ||
| 228 | gate_result_t gate_ui_textbox_get_column(gate_ui_textbox_t* textbox, gate_uint32_t* currentColumn) | ||
| 229 | { | ||
| 230 | gate_result_t ret; | ||
| 231 | gate_uint32_t currentlinecharpos; | ||
| 232 | DWORD selstart, selend; | ||
| 233 | LRESULT result; | ||
| 234 | HWND hwnd = GATE_UI_WINAPI_GET_HWND(&textbox->ctrl); | ||
| 235 | currentlinecharpos = (gate_uint32_t)SendMessage(hwnd, EM_LINEINDEX, -1, 0); | ||
| 236 | result = SendMessage(hwnd, EM_GETSEL, (WPARAM)(void*)&selstart, (LPARAM)(void*)&selend); | ||
| 237 | if (selstart >= currentlinecharpos) | ||
| 238 | { | ||
| 239 | if (currentColumn != NULL) | ||
| 240 | { | ||
| 241 | *currentColumn = selstart - currentlinecharpos; | ||
| 242 | } | ||
| 243 | ret = GATE_RESULT_OK; | ||
| 244 | } | ||
| 245 | else if (selend >= currentlinecharpos) | ||
| 246 | { | ||
| 247 | if (currentColumn != NULL) | ||
| 248 | { | ||
| 249 | *currentColumn = selend - currentlinecharpos; | ||
| 250 | } | ||
| 251 | ret = GATE_RESULT_OK; | ||
| 252 | } | ||
| 253 | else | ||
| 254 | { | ||
| 255 | ret = GATE_RESULT_FAILED; | ||
| 256 | } | ||
| 257 | |||
| 258 | return ret; | ||
| 259 | } | ||
| 260 | gate_result_t gate_ui_textbox_get_selection(gate_ui_textbox_t* textbox, gate_uint32_t* sel_start, gate_uint32_t* sel_end, gate_string_t* sel_text) | ||
| 261 | { | ||
| 262 | gate_result_t ret; | ||
| 263 | TCHAR buffer[1024 * 7]; | ||
| 264 | TCHAR* txtbuffer = &buffer[0]; | ||
| 265 | static gate_size_t const buffersize = sizeof(buffer) / sizeof(buffer[0]); | ||
| 266 | |||
| 267 | do | ||
| 268 | { | ||
| 269 | HWND hwnd = GATE_UI_WINAPI_GET_HWND(&textbox->ctrl); | ||
| 270 | DWORD selstart = (DWORD)-1; | ||
| 271 | DWORD selend = (DWORD)-1; | ||
| 272 | LRESULT result = SendMessage(hwnd, EM_GETSEL, (WPARAM)(void*)&selstart, (LPARAM)(void*)&selend); | ||
| 273 | |||
| 274 | if (sel_start != NULL) | ||
| 275 | { | ||
| 276 | *sel_start = (gate_uint32_t)selstart; | ||
| 277 | } | ||
| 278 | if (sel_end != NULL) | ||
| 279 | { | ||
| 280 | *sel_end = (gate_uint32_t)selend; | ||
| 281 | } | ||
| 282 | if (sel_text != NULL) | ||
| 283 | { | ||
| 284 | if (selstart == selend) | ||
| 285 | { | ||
| 286 | gate_string_create_empty(sel_text); | ||
| 287 | } | ||
| 288 | else | ||
| 289 | { | ||
| 290 | if (selend >= buffersize) | ||
| 291 | { | ||
| 292 | txtbuffer = (TCHAR*)gate_mem_alloc(((gate_size_t)selend + 4) * sizeof(TCHAR)); | ||
| 293 | if (txtbuffer == NULL) | ||
| 294 | { | ||
| 295 | ret = GATE_RESULT_OUTOFMEMORY; | ||
| 296 | break; | ||
| 297 | } | ||
| 298 | } | ||
| 299 | result = SendMessage(hwnd, WM_GETTEXT, (WPARAM)selend + 1, (LPARAM)(gate_uintptr_t)txtbuffer); | ||
| 300 | gate_win32_winstr_2_utf8_string(&txtbuffer[selstart], selend - selstart, sel_text); | ||
| 301 | } | ||
| 302 | } | ||
| 303 | ret = GATE_RESULT_OK; | ||
| 304 | } while (0); | ||
| 305 | if (txtbuffer != &buffer[0]) | ||
| 306 | { | ||
| 307 | gate_mem_dealloc(txtbuffer); | ||
| 308 | } | ||
| 309 | return ret; | ||
| 310 | } | ||
| 311 | gate_result_t gate_ui_textbox_set_selection(gate_ui_textbox_t* textbox, gate_uint32_t sel_start, gate_uint32_t sel_end, gate_bool_t scroll_to_sel) | ||
| 312 | { | ||
| 313 | gate_result_t ret = GATE_RESULT_OK; | ||
| 314 | HWND hwnd = GATE_UI_WINAPI_GET_HWND(&textbox->ctrl); | ||
| 315 | SendMessage(hwnd, EM_SETSEL, (WPARAM)sel_start, (LPARAM)sel_end); | ||
| 316 | if (scroll_to_sel) | ||
| 317 | { | ||
| 318 | #if !defined(GATE_SYS_WIN16) | ||
| 319 | SendMessage(hwnd, EM_SCROLLCARET, 0, 0); | ||
| 320 | #endif | ||
| 321 | } | ||
| 322 | return ret; | ||
| 323 | } | ||
| 324 | gate_result_t gate_ui_textbox_replace_selection(gate_ui_textbox_t* textbox, gate_string_t const* text) | ||
| 325 | { | ||
| 326 | gate_result_t ret; | ||
| 327 | TCHAR buffer[4096]; | ||
| 328 | TCHAR* txtbuffer = &buffer[0]; | ||
| 329 | do | ||
| 330 | { | ||
| 331 | HWND hwnd = GATE_UI_WINAPI_GET_HWND(&textbox->ctrl); | ||
| 332 | gate_size_t textlen = gate_string_length(text); | ||
| 333 | |||
| 334 | if (textlen >= sizeof(buffer) / sizeof(buffer[0])) | ||
| 335 | { | ||
| 336 | txtbuffer = gate_mem_alloc((textlen + 256) * sizeof(TCHAR)); | ||
| 337 | if (txtbuffer == NULL) | ||
| 338 | { | ||
| 339 | ret = GATE_RESULT_OUTOFMEMORY; | ||
| 340 | break; | ||
| 341 | } | ||
| 342 | } | ||
| 343 | if (textlen == 0) | ||
| 344 | { | ||
| 345 | txtbuffer[0] = 0; | ||
| 346 | } | ||
| 347 | else | ||
| 348 | { | ||
| 349 | gate_win32_utf8_2_winstr(text->str, text->length, txtbuffer, textlen + 255); | ||
| 350 | } | ||
| 351 | |||
| 352 | SendMessage(hwnd, EM_REPLACESEL, (WPARAM)TRUE, (LPARAM)(gate_intptr_t)txtbuffer); | ||
| 353 | ret = GATE_RESULT_OK; | ||
| 354 | } while (0); | ||
| 355 | |||
| 356 | if ((txtbuffer != &buffer[0]) && (txtbuffer != NULL)) | ||
| 357 | { | ||
| 358 | gate_mem_dealloc(txtbuffer); | ||
| 359 | } | ||
| 360 | |||
| 361 | return ret; | ||
| 362 | } | ||
| 363 | gate_result_t gate_ui_textbox_undo(gate_ui_textbox_t* textbox) | ||
| 364 | { | ||
| 365 | gate_result_t ret; | ||
| 366 | HWND hwnd = GATE_UI_WINAPI_GET_HWND(&textbox->ctrl); | ||
| 367 | if (FALSE == SendMessage(hwnd, EM_UNDO, 0, 0)) | ||
| 368 | { | ||
| 369 | ret = GATE_RESULT_FAILED; | ||
| 370 | } | ||
| 371 | else | ||
| 372 | { | ||
| 373 | ret = GATE_RESULT_OK; | ||
| 374 | } | ||
| 375 | return ret; | ||
| 376 | } | ||
| 377 | |||
| 378 | gate_result_t gate_ui_textbox_set_font(gate_ui_textbox_t* textbox, gate_ui_font_t const* font) | ||
| 379 | { | ||
| 380 | gate_result_t ret; | ||
| 381 | HFONT hfont = (HFONT)gate_ui_winapi_create_font(font); | ||
| 382 | if (hfont == NULL) | ||
| 383 | { | ||
| 384 | ret = GATE_RESULT_OUTOFRESOURCES; | ||
| 385 | } | ||
| 386 | else | ||
| 387 | { | ||
| 388 | ret = gate_ui_winapi_change_font(&textbox->ctrl, hfont); | ||
| 389 | } | ||
| 390 | return ret; | ||
| 391 | } | ||
| 392 | |||
| 393 | gate_result_t gate_ui_textbox_find_next(gate_ui_textbox_t* textbox, gate_string_t const* token, gate_uint32_t flags) | ||
| 394 | { | ||
| 395 | gate_result_t ret = GATE_RESULT_FAILED; | ||
| 396 | HWND hwnd = GATE_UI_WINAPI_GET_HWND(&textbox->ctrl); | ||
| 397 | do | ||
| 398 | { | ||
| 399 | |||
| 400 | } while (0); | ||
| 401 | return ret; | ||
| 402 | } | ||
| 403 | |||
| 404 | |||
| 405 | #endif /*GATE_UI_WINAPI*/ | ||
| 406 | |||
| 407 | #if defined(GATE_UI_GTK) | ||
| 408 | |||
| 409 | #include "gate/ui/gateui_gtk.h" | ||
| 410 | |||
| 411 | static gate_uint32_t gate_ui_gtk_textbox_get_text_length(gate_ui_ctrl_t* ctrl) | ||
| 412 | { | ||
| 413 | gint char_count = 0; | ||
| 414 | GtkWidget* widget = GATE_UI_GTK_GET_CTRL_WIDGET(ctrl); | ||
| 415 | gate_bool_t const multiline = GTK_IS_ENTRY(widget) ? false : true; | ||
| 416 | |||
| 417 | if (multiline) | ||
| 418 | { | ||
| 419 | GtkScrolledWindow* scroll_window = GTK_SCROLLED_WINDOW(widget); | ||
| 420 | if (scroll_window) | ||
| 421 | { | ||
| 422 | GtkTextView* text_view = GTK_TEXT_VIEW(gtk_bin_get_child(GTK_BIN(scroll_window))); | ||
| 423 | if (text_view) | ||
| 424 | { | ||
| 425 | GtkTextBuffer* buffer = gtk_text_view_get_buffer(text_view); | ||
| 426 | if (buffer) | ||
| 427 | { | ||
| 428 | char_count = gtk_text_buffer_get_char_count(buffer); | ||
| 429 | } | ||
| 430 | } | ||
| 431 | } | ||
| 432 | } | ||
| 433 | else | ||
| 434 | { | ||
| 435 | GtkEntry* entry = GTK_ENTRY(widget); | ||
| 436 | if (entry) | ||
| 437 | { | ||
| 438 | char_count = gtk_entry_get_text_length(entry); | ||
| 439 | } | ||
| 440 | } | ||
| 441 | |||
| 442 | return (gate_uint32_t)char_count; | ||
| 443 | } | ||
| 444 | static gate_result_t gate_ui_gtk_textbox_get_text(gate_ui_ctrl_t* ctrl, gate_string_t* text) | ||
| 445 | { | ||
| 446 | gate_result_t ret = GATE_RESULT_FAILED; | ||
| 447 | GtkWidget* widget = GATE_UI_GTK_GET_CTRL_WIDGET(ctrl); | ||
| 448 | gate_bool_t const multiline = GTK_IS_ENTRY(widget) ? false : true; | ||
| 449 | char const* ptr_text_buffer = NULL; | ||
| 450 | |||
| 451 | if (multiline) | ||
| 452 | { | ||
| 453 | GtkScrolledWindow* scroll_window = GTK_SCROLLED_WINDOW(GATE_UI_GTK_GET_CTRL_WIDGET(ctrl)); | ||
| 454 | GtkTextView* text_view = GTK_TEXT_VIEW(gtk_bin_get_child(GTK_BIN(scroll_window))); | ||
| 455 | |||
| 456 | if (text_view) | ||
| 457 | { | ||
| 458 | gchar* text_buffer = NULL; | ||
| 459 | GtkTextBuffer* buffer = gtk_text_view_get_buffer(text_view); | ||
| 460 | if (buffer) | ||
| 461 | { | ||
| 462 | GtkTextIter text_start, text_end; | ||
| 463 | gtk_widget_set_sensitive((GtkWidget*)text_view, FALSE); | ||
| 464 | gtk_text_buffer_get_start_iter(buffer, &text_start); | ||
| 465 | gtk_text_buffer_get_end_iter(buffer, &text_end); | ||
| 466 | text_buffer = gtk_text_buffer_get_text(buffer, &text_start, &text_end, FALSE); | ||
| 467 | gtk_text_buffer_set_modified(buffer, FALSE); | ||
| 468 | gtk_widget_set_sensitive((GtkWidget*)text_view, TRUE); | ||
| 469 | } | ||
| 470 | if (text_buffer == NULL) | ||
| 471 | { | ||
| 472 | gate_string_create_empty(text); | ||
| 473 | ret = GATE_RESULT_OK; | ||
| 474 | } | ||
| 475 | else | ||
| 476 | { | ||
| 477 | if (NULL == gate_string_create(text, text_buffer, gate_str_length(text_buffer))) | ||
| 478 | { | ||
| 479 | ret = GATE_RESULT_OUTOFMEMORY; | ||
| 480 | } | ||
| 481 | else | ||
| 482 | { | ||
| 483 | ret = GATE_RESULT_OK; | ||
| 484 | } | ||
| 485 | g_free(text_buffer); | ||
| 486 | } | ||
| 487 | } | ||
| 488 | } | ||
| 489 | else | ||
| 490 | { | ||
| 491 | GtkEntry* entry = GTK_ENTRY(widget); | ||
| 492 | ptr_text_buffer = gtk_entry_get_text(entry); | ||
| 493 | if (ptr_text_buffer == NULL) | ||
| 494 | { | ||
| 495 | gate_string_create_empty(text); | ||
| 496 | ret = GATE_RESULT_OK; | ||
| 497 | } | ||
| 498 | else | ||
| 499 | { | ||
| 500 | if (NULL == gate_string_create(text, ptr_text_buffer, gate_str_length(ptr_text_buffer))) | ||
| 501 | { | ||
| 502 | ret = GATE_RESULT_OUTOFMEMORY; | ||
| 503 | } | ||
| 504 | else | ||
| 505 | { | ||
| 506 | ret = GATE_RESULT_OK; | ||
| 507 | } | ||
| 508 | } | ||
| 509 | } | ||
| 510 | |||
| 511 | return ret; | ||
| 512 | } | ||
| 513 | static gate_result_t gate_ui_gtk_textbox_set_text(gate_ui_ctrl_t* ctrl, gate_string_t const* text) | ||
| 514 | { | ||
| 515 | gate_result_t ret = GATE_RESULT_FAILED; | ||
| 516 | GtkWidget* widget = GATE_UI_GTK_GET_CTRL_WIDGET(ctrl); | ||
| 517 | gate_bool_t const multiline = GTK_IS_ENTRY(widget) ? false : true; | ||
| 518 | |||
| 519 | if (multiline) | ||
| 520 | { | ||
| 521 | GtkScrolledWindow* scroll_window = GTK_SCROLLED_WINDOW(widget); | ||
| 522 | GtkTextView* text_view = GTK_TEXT_VIEW(gtk_bin_get_child(GTK_BIN(scroll_window))); | ||
| 523 | if (text_view) | ||
| 524 | { | ||
| 525 | GtkTextBuffer* buffer = gtk_text_view_get_buffer(text_view); | ||
| 526 | if (buffer) | ||
| 527 | { | ||
| 528 | gtk_widget_set_sensitive(GTK_WIDGET(text_view), FALSE); | ||
| 529 | |||
| 530 | if (gate_string_is_empty(text)) | ||
| 531 | { | ||
| 532 | gtk_text_buffer_set_text(buffer, "", 0); | ||
| 533 | } | ||
| 534 | else | ||
| 535 | { | ||
| 536 | gtk_text_buffer_set_text(buffer, (gchar const*)text->str, (gint)text->length); | ||
| 537 | } | ||
| 538 | gtk_text_buffer_set_modified(buffer, TRUE); | ||
| 539 | gtk_widget_set_sensitive(GTK_WIDGET(text_view), TRUE); | ||
| 540 | ret = GATE_RESULT_OK; | ||
| 541 | } | ||
| 542 | } | ||
| 543 | } | ||
| 544 | else | ||
| 545 | { | ||
| 546 | char text_buffer[GATE_MAX_COPYBUFFER_LENGTH]; | ||
| 547 | GtkEntry* entry = GTK_ENTRY(widget); | ||
| 548 | GATE_STRING_TO_BUFFER(text, text_buffer); | ||
| 549 | gtk_entry_set_text(entry, text_buffer); | ||
| 550 | ret = GATE_RESULT_OK; | ||
| 551 | } | ||
| 552 | |||
| 553 | return ret; | ||
| 554 | } | ||
| 555 | static gate_ui_gtk_dispatcher_t gate_ui_gtk_textbox_dispatcher = | ||
| 556 | { | ||
| 557 | gate_ui_gtk_textbox_get_text_length, | ||
| 558 | gate_ui_gtk_textbox_get_text, | ||
| 559 | gate_ui_gtk_textbox_set_text, | ||
| 560 | NULL, | ||
| 561 | NULL, | ||
| 562 | NULL | ||
| 563 | }; | ||
| 564 | |||
| 565 | |||
| 566 | gate_result_t gate_ui_textbox_create(gate_ui_textbox_t* txtbox, gate_ui_ctrl_t* parent, | ||
| 567 | gate_ui_position_t const* position, gate_string_t const* caption, gate_uint32_t flags, void* userparam) | ||
| 568 | { | ||
| 569 | GtkWidget* widget; | ||
| 570 | gate_ui_host_t* host = GATE_UI_GTK_GET_CTRL_HOST(parent); | ||
| 571 | gate_bool_t const multiline = GATE_FLAG_ENABLED(flags, GATE_UI_FLAG_TEXTBOX_MULTILINE); | ||
| 572 | |||
| 573 | gate_mem_clear(txtbox, sizeof(gate_ui_textbox_t)); | ||
| 574 | |||
| 575 | if (multiline) | ||
| 576 | { | ||
| 577 | GtkWidget* scroll_widget; | ||
| 578 | GtkTextBuffer* textbuffer; | ||
| 579 | |||
| 580 | widget = gtk_text_view_new(); | ||
| 581 | if (widget == NULL) | ||
| 582 | { | ||
| 583 | return GATE_RESULT_FAILED; | ||
| 584 | } | ||
| 585 | textbuffer = gtk_text_buffer_new(NULL); | ||
| 586 | if (textbuffer == NULL) | ||
| 587 | { | ||
| 588 | gtk_widget_destroy(widget); | ||
| 589 | return GATE_RESULT_OUTOFRESOURCES; | ||
| 590 | } | ||
| 591 | |||
| 592 | gtk_text_view_set_buffer(GTK_TEXT_VIEW(widget), textbuffer); | ||
| 593 | |||
| 594 | scroll_widget = gtk_scrolled_window_new(NULL, NULL); | ||
| 595 | if (scroll_widget == NULL) | ||
| 596 | { | ||
| 597 | gtk_widget_destroy(widget); | ||
| 598 | return GATE_RESULT_OUTOFRESOURCES; | ||
| 599 | } | ||
| 600 | |||
| 601 | gtk_container_add(GTK_CONTAINER(scroll_widget), widget); | ||
| 602 | |||
| 603 | gtk_widget_show(widget); | ||
| 604 | |||
| 605 | |||
| 606 | gate_ui_gtk_ctrl_init(&txtbox->ctrl, scroll_widget, host, userparam, parent, | ||
| 607 | &gate_ui_gtk_textbox_dispatcher, false, false, position, &flags); | ||
| 608 | |||
| 609 | if (gate_string_length(caption) > 0) | ||
| 610 | { | ||
| 611 | gate_ui_ctrl_set_text(&txtbox->ctrl, caption); | ||
| 612 | } | ||
| 613 | |||
| 614 | if (GATE_FLAG_ENABLED(flags, GATE_UI_FLAG_TEXTBOX_READONLY)) | ||
| 615 | { | ||
| 616 | gtk_text_view_set_editable(GTK_TEXT_VIEW(widget), FALSE); | ||
| 617 | } | ||
| 618 | } | ||
| 619 | else | ||
| 620 | { | ||
| 621 | GtkEntryBuffer* entrybuffer = gtk_entry_buffer_new(NULL, 0); | ||
| 622 | widget = gtk_entry_new_with_buffer(entrybuffer); | ||
| 623 | |||
| 624 | gate_ui_gtk_ctrl_init(&txtbox->ctrl, widget, host, userparam, parent, | ||
| 625 | &gate_ui_gtk_textbox_dispatcher, false, false, position, &flags); | ||
| 626 | |||
| 627 | if (gate_string_length(caption) > 0) | ||
| 628 | { | ||
| 629 | gate_ui_ctrl_set_text(&txtbox->ctrl, caption); | ||
| 630 | } | ||
| 631 | } | ||
| 632 | |||
| 633 | return GATE_RESULT_OK; | ||
| 634 | } | ||
| 635 | |||
| 636 | gate_result_t gate_ui_textbox_get_line(gate_ui_textbox_t* txtbox, gate_uint32_t* currentLine, gate_uint32_t* lineCount) | ||
| 637 | { | ||
| 638 | gate_result_t ret = GATE_RESULT_FAILED; | ||
| 639 | GtkWidget* widget = GATE_UI_GTK_GET_CTRL_WIDGET(&txtbox->ctrl); | ||
| 640 | gate_bool_t multiline = GTK_IS_ENTRY(widget) ? false : true; | ||
| 641 | |||
| 642 | if (multiline) | ||
| 643 | { | ||
| 644 | GtkScrolledWindow* scroll_window = GTK_SCROLLED_WINDOW(widget); | ||
| 645 | if (scroll_window) | ||
| 646 | { | ||
| 647 | GtkTextView* text_view = GTK_TEXT_VIEW(gtk_bin_get_child(GTK_BIN(scroll_window))); | ||
| 648 | if (text_view) | ||
| 649 | { | ||
| 650 | GtkTextBuffer* buffer = gtk_text_view_get_buffer(text_view); | ||
| 651 | if (buffer) | ||
| 652 | { | ||
| 653 | if (lineCount != NULL) | ||
| 654 | { | ||
| 655 | *lineCount = gtk_text_buffer_get_line_count(buffer); | ||
| 656 | } | ||
| 657 | if (currentLine != NULL) | ||
| 658 | { | ||
| 659 | GtkTextIter iter; | ||
| 660 | GtkTextMark* mark = gtk_text_buffer_get_insert(buffer); | ||
| 661 | gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark); | ||
| 662 | *currentLine = gtk_text_iter_get_line(&iter); | ||
| 663 | } | ||
| 664 | ret = GATE_RESULT_OK; | ||
| 665 | } | ||
| 666 | } | ||
| 667 | } | ||
| 668 | } | ||
| 669 | else | ||
| 670 | { | ||
| 671 | GtkEntry* entry = GTK_ENTRY(widget); | ||
| 672 | if (entry) | ||
| 673 | { | ||
| 674 | if (lineCount != NULL) | ||
| 675 | { | ||
| 676 | *lineCount = 1; | ||
| 677 | } | ||
| 678 | if (currentLine != NULL) | ||
| 679 | { | ||
| 680 | *currentLine = 0; | ||
| 681 | } | ||
| 682 | ret = GATE_RESULT_OK; | ||
| 683 | } | ||
| 684 | } | ||
| 685 | |||
| 686 | |||
| 687 | return ret; | ||
| 688 | } | ||
| 689 | gate_result_t gate_ui_textbox_get_column(gate_ui_textbox_t* txtbox, gate_uint32_t* currentColumn) | ||
| 690 | { | ||
| 691 | gate_result_t ret = GATE_RESULT_FAILED; | ||
| 692 | GtkWidget* widget = GATE_UI_GTK_GET_CTRL_WIDGET(&txtbox->ctrl); | ||
| 693 | gate_bool_t const multiline = GTK_IS_ENTRY(widget) ? false : true; | ||
| 694 | |||
| 695 | if (multiline) | ||
| 696 | { | ||
| 697 | GtkScrolledWindow* scroll_window = GTK_SCROLLED_WINDOW(GATE_UI_GTK_GET_CTRL_WIDGET(&txtbox->ctrl)); | ||
| 698 | GtkTextView* text_view = GTK_TEXT_VIEW(gtk_bin_get_child(GTK_BIN(scroll_window))); | ||
| 699 | if (text_view) | ||
| 700 | { | ||
| 701 | GtkTextBuffer* buffer = gtk_text_view_get_buffer(text_view); | ||
| 702 | if (buffer) | ||
| 703 | { | ||
| 704 | if (currentColumn != NULL) | ||
| 705 | { | ||
| 706 | GtkTextIter iter; | ||
| 707 | GtkTextMark* mark = gtk_text_buffer_get_insert(buffer); | ||
| 708 | gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark); | ||
| 709 | *currentColumn = gtk_text_iter_get_line_offset(&iter); | ||
| 710 | } | ||
| 711 | ret = GATE_RESULT_OK; | ||
| 712 | } | ||
| 713 | } | ||
| 714 | } | ||
| 715 | else | ||
| 716 | { | ||
| 717 | GtkEntry* entry = GTK_ENTRY(widget); | ||
| 718 | if (entry) | ||
| 719 | { | ||
| 720 | if (currentColumn) | ||
| 721 | { | ||
| 722 | *currentColumn = 0; | ||
| 723 | } | ||
| 724 | ret = GATE_RESULT_OK; | ||
| 725 | } | ||
| 726 | } | ||
| 727 | return ret; | ||
| 728 | } | ||
| 729 | gate_result_t gate_ui_textbox_get_selection(gate_ui_textbox_t* txtbox, gate_uint32_t* sel_start, | ||
| 730 | gate_uint32_t* sel_end, gate_string_t* sel_text) | ||
| 731 | { | ||
| 732 | gate_result_t ret = GATE_RESULT_FAILED; | ||
| 733 | GtkWidget* widget = GATE_UI_GTK_GET_CTRL_WIDGET(&txtbox->ctrl); | ||
| 734 | gate_bool_t const multiline = GTK_IS_ENTRY(widget) ? false : true; | ||
| 735 | |||
| 736 | if (multiline) | ||
| 737 | { | ||
| 738 | GtkScrolledWindow* scroll_window = GTK_SCROLLED_WINDOW(widget); | ||
| 739 | GtkTextView* text_view = GTK_TEXT_VIEW(gtk_bin_get_child(GTK_BIN(scroll_window))); | ||
| 740 | if (text_view) | ||
| 741 | { | ||
| 742 | GtkTextBuffer* buffer = gtk_text_view_get_buffer(text_view); | ||
| 743 | if (buffer) | ||
| 744 | { | ||
| 745 | GtkTextIter start_iter, end_iter; | ||
| 746 | gtk_text_buffer_get_selection_bounds(buffer, &start_iter, &end_iter); | ||
| 747 | if (sel_start != NULL) | ||
| 748 | { | ||
| 749 | *sel_start = gtk_text_iter_get_offset(&start_iter); | ||
| 750 | } | ||
| 751 | if (sel_end != NULL) | ||
| 752 | { | ||
| 753 | *sel_end = gtk_text_iter_get_offset(&start_iter); | ||
| 754 | } | ||
| 755 | ret = GATE_RESULT_OK; | ||
| 756 | if (sel_text != NULL) | ||
| 757 | { | ||
| 758 | gchar* data = gtk_text_buffer_get_text(buffer, &start_iter, &end_iter, FALSE); | ||
| 759 | if (NULL == gate_string_create(sel_text, data, gate_str_length(data))) | ||
| 760 | { | ||
| 761 | ret = GATE_RESULT_OUTOFMEMORY; | ||
| 762 | } | ||
| 763 | g_free(data); | ||
| 764 | } | ||
| 765 | } | ||
| 766 | } | ||
| 767 | } | ||
| 768 | else | ||
| 769 | { | ||
| 770 | GtkEntry* entry = GTK_ENTRY(widget); | ||
| 771 | if (entry) | ||
| 772 | { | ||
| 773 | /*TODO*/ | ||
| 774 | if (sel_start) | ||
| 775 | { | ||
| 776 | *sel_start = 0; | ||
| 777 | } | ||
| 778 | if (sel_end) | ||
| 779 | { | ||
| 780 | *sel_end = 0; | ||
| 781 | } | ||
| 782 | if (sel_text) | ||
| 783 | { | ||
| 784 | gate_string_create_empty(sel_text); | ||
| 785 | } | ||
| 786 | ret = GATE_RESULT_OK; | ||
| 787 | } | ||
| 788 | } | ||
| 789 | |||
| 790 | return ret; | ||
| 791 | } | ||
| 792 | |||
| 793 | gate_result_t gate_ui_textbox_set_selection(gate_ui_textbox_t* txtbox, gate_uint32_t sel_start, | ||
| 794 | gate_uint32_t sel_end, gate_bool_t scroll_to_sel) | ||
| 795 | { | ||
| 796 | gate_result_t ret = GATE_RESULT_FAILED; | ||
| 797 | GtkWidget* widget = GATE_UI_GTK_GET_CTRL_WIDGET(&txtbox->ctrl); | ||
| 798 | gate_bool_t const multiline = GTK_IS_ENTRY(widget) ? false : true; | ||
| 799 | |||
| 800 | do | ||
| 801 | { | ||
| 802 | if (multiline) | ||
| 803 | { | ||
| 804 | GtkScrolledWindow* scroll_window = GTK_SCROLLED_WINDOW(GATE_UI_GTK_GET_CTRL_WIDGET(&txtbox->ctrl)); | ||
| 805 | GtkTextView* text_view = GTK_TEXT_VIEW(gtk_bin_get_child(GTK_BIN(scroll_window))); | ||
| 806 | if (text_view) | ||
| 807 | { | ||
| 808 | GtkTextBuffer* buffer = gtk_text_view_get_buffer(text_view); | ||
| 809 | if (buffer) | ||
| 810 | { | ||
| 811 | /* TODO: set selection */ | ||
| 812 | } | ||
| 813 | } | ||
| 814 | } | ||
| 815 | else | ||
| 816 | { | ||
| 817 | GtkEntry* entry = GTK_ENTRY(widget); | ||
| 818 | if (entry) | ||
| 819 | { | ||
| 820 | /* TODO: set selection */ | ||
| 821 | } | ||
| 822 | } | ||
| 823 | } while (0); | ||
| 824 | return ret; | ||
| 825 | } | ||
| 826 | gate_result_t gate_ui_textbox_replace_selection(gate_ui_textbox_t* txtbox, gate_string_t const* text) | ||
| 827 | { | ||
| 828 | gate_result_t ret = GATE_RESULT_FAILED; | ||
| 829 | GtkWidget* widget = GATE_UI_GTK_GET_CTRL_WIDGET(&txtbox->ctrl); | ||
| 830 | gate_bool_t const multiline = GTK_IS_ENTRY(widget) ? false : true; | ||
| 831 | |||
| 832 | do | ||
| 833 | { | ||
| 834 | if (multiline) | ||
| 835 | { | ||
| 836 | GtkScrolledWindow* scroll_window = GTK_SCROLLED_WINDOW(GATE_UI_GTK_GET_CTRL_WIDGET(&txtbox->ctrl)); | ||
| 837 | GtkTextView* text_view = GTK_TEXT_VIEW(gtk_bin_get_child(GTK_BIN(scroll_window))); | ||
| 838 | if (text_view) | ||
| 839 | { | ||
| 840 | GtkTextBuffer* buffer = gtk_text_view_get_buffer(text_view); | ||
| 841 | if (buffer) | ||
| 842 | { | ||
| 843 | /* TODO: replace selection */ | ||
| 844 | } | ||
| 845 | } | ||
| 846 | } | ||
| 847 | else | ||
| 848 | { | ||
| 849 | GtkEntry* entry = GTK_ENTRY(widget); | ||
| 850 | if (entry) | ||
| 851 | { | ||
| 852 | /* TODO: replace selection */ | ||
| 853 | } | ||
| 854 | } | ||
| 855 | } while (0); | ||
| 856 | return ret; | ||
| 857 | } | ||
| 858 | |||
| 859 | gate_result_t gate_ui_textbox_undo(gate_ui_textbox_t* txtbox) | ||
| 860 | { | ||
| 861 | gate_result_t ret = GATE_RESULT_NOTSUPPORTED; | ||
| 862 | GtkWidget* widget = GATE_UI_GTK_GET_CTRL_WIDGET(&txtbox->ctrl); | ||
| 863 | gate_bool_t const multiline = GTK_IS_ENTRY(widget) ? false : true; | ||
| 864 | |||
| 865 | do | ||
| 866 | { | ||
| 867 | if (multiline) | ||
| 868 | { | ||
| 869 | GtkScrolledWindow* scroll_window = GTK_SCROLLED_WINDOW(GATE_UI_GTK_GET_CTRL_WIDGET(&txtbox->ctrl)); | ||
| 870 | GtkTextView* text_view = GTK_TEXT_VIEW(gtk_bin_get_child(GTK_BIN(scroll_window))); | ||
| 871 | if (text_view) | ||
| 872 | { | ||
| 873 | GtkTextBuffer* buffer = gtk_text_view_get_buffer(text_view); | ||
| 874 | if (buffer) | ||
| 875 | { | ||
| 876 | /*TODO: UNDO feature*/ | ||
| 877 | } | ||
| 878 | } | ||
| 879 | } | ||
| 880 | else | ||
| 881 | { | ||
| 882 | GtkEntry* entry = GTK_ENTRY(widget); | ||
| 883 | if (entry) | ||
| 884 | { | ||
| 885 | /* TODO: UNDO feature */ | ||
| 886 | } | ||
| 887 | } | ||
| 888 | } while (0); | ||
| 889 | return ret; | ||
| 890 | } | ||
| 891 | gate_result_t gate_ui_textbox_set_font(gate_ui_textbox_t* txtbox, gate_ui_font_t const* font) | ||
| 892 | { | ||
| 893 | gate_result_t ret = GATE_RESULT_FAILED; | ||
| 894 | GtkWidget* widget = GATE_UI_GTK_GET_CTRL_WIDGET(&txtbox->ctrl); | ||
| 895 | gate_bool_t const multiline = GTK_IS_ENTRY(widget) ? false : true; | ||
| 896 | |||
| 897 | do | ||
| 898 | { | ||
| 899 | if (multiline) | ||
| 900 | { | ||
| 901 | GtkScrolledWindow* scroll_window = GTK_SCROLLED_WINDOW(GATE_UI_GTK_GET_CTRL_WIDGET(&txtbox->ctrl)); | ||
| 902 | GtkTextView* text_view = GTK_TEXT_VIEW(gtk_bin_get_child(GTK_BIN(scroll_window))); | ||
| 903 | if (text_view) | ||
| 904 | { | ||
| 905 | ret = gate_ui_gtk_ctrl_set_font(GTK_WIDGET(text_view), font); | ||
| 906 | } | ||
| 907 | } | ||
| 908 | else | ||
| 909 | { | ||
| 910 | GtkEntry* entry = GTK_ENTRY(widget); | ||
| 911 | if (entry) | ||
| 912 | { | ||
| 913 | ret = gate_ui_gtk_ctrl_set_font(GTK_WIDGET(entry), font); | ||
| 914 | } | ||
| 915 | } | ||
| 916 | } while (0); | ||
| 917 | return ret; | ||
| 918 | } | ||
| 919 | |||
| 920 | gate_result_t gate_ui_textbox_find_next(gate_ui_textbox_t* txtbox, gate_string_t const* token, gate_uint32_t flags) | ||
| 921 | { | ||
| 922 | gate_result_t ret = GATE_RESULT_FAILED; | ||
| 923 | GtkWidget* widget = GATE_UI_GTK_GET_CTRL_WIDGET(&txtbox->ctrl); | ||
| 924 | gate_bool_t const multiline = GTK_IS_ENTRY(widget) ? false : true; | ||
| 925 | |||
| 926 | do | ||
| 927 | { | ||
| 928 | if (multiline) | ||
| 929 | { | ||
| 930 | GtkScrolledWindow* scroll_window = GTK_SCROLLED_WINDOW(GATE_UI_GTK_GET_CTRL_WIDGET(&txtbox->ctrl)); | ||
| 931 | GtkTextView* text_view = GTK_TEXT_VIEW(gtk_bin_get_child(GTK_BIN(scroll_window))); | ||
| 932 | if (text_view) | ||
| 933 | { | ||
| 934 | GtkTextBuffer* buffer = gtk_text_view_get_buffer(text_view); | ||
| 935 | if (buffer) | ||
| 936 | { | ||
| 937 | /* TODO: find implementation */ | ||
| 938 | } | ||
| 939 | } | ||
| 940 | } | ||
| 941 | else | ||
| 942 | { | ||
| 943 | GtkEntry* entry = GTK_ENTRY(widget); | ||
| 944 | if (entry) | ||
| 945 | { | ||
| 946 | /* TODO: find implementation */ | ||
| 947 | } | ||
| 948 | } | ||
| 949 | } while (0); | ||
| 950 | return ret; | ||
| 951 | } | ||
| 952 | |||
| 953 | #endif /* GATE_UI_GTK */ | ||
| 954 | |||
| 955 | |||
| 956 | |||
| 957 | #if defined(GATE_UI_MOTIF) | ||
| 958 | |||
| 959 | #include "gate/ui/gateui_motif.h" | ||
| 960 | #include "gate/debugging.h" | ||
| 961 | #include <Xm/TextF.h> | ||
| 962 | #include <Xm/Text.h> | ||
| 963 | #include <Xm/RowColumn.h> | ||
| 964 | #include <Xm/ScrolledW.h> | ||
| 965 | |||
| 966 | ✗ | static Widget textbox_get_widget(gate_ui_ctrl_t* ctrl) | |
| 967 | { | ||
| 968 | ✗ | Widget w = GATE_UI_MOTIF_GET_CTRL_CONTAINER(ctrl); | |
| 969 | ✗ | if (w == NULL) | |
| 970 | { | ||
| 971 | ✗ | w = GATE_UI_MOTIF_GET_CTRL_WIDGET(ctrl); | |
| 972 | } | ||
| 973 | ✗ | return w; | |
| 974 | } | ||
| 975 | |||
| 976 | 1 | static gate_result_t textbox_get_text(gate_ui_ctrl_t* ctrl, gate_string_t* text) | |
| 977 | { | ||
| 978 | Widget w, w_txt; | ||
| 979 | char* ptr_text; | ||
| 980 | 1 | gate_result_t ret = GATE_RESULT_OK; | |
| 981 | |||
| 982 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATE_DEBUG_ASSERT(ctrl != NULL); |
| 983 | 1 | w = GATE_UI_MOTIF_GET_CTRL_WIDGET(ctrl); | |
| 984 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!w) |
| 985 | { | ||
| 986 | ✗ | return GATE_RESULT_INVALIDARG; | |
| 987 | } | ||
| 988 | |||
| 989 | 1 | w_txt = GATE_UI_MOTIF_GET_CTRL_CONTAINER(ctrl); | |
| 990 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (w_txt) |
| 991 | { | ||
| 992 | ✗ | w = w_txt; | |
| 993 | } | ||
| 994 | 1 | ptr_text = XmTextGetString(w); | |
| 995 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (text) |
| 996 | { | ||
| 997 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | if (NULL == gate_string_create(text, ptr_text, gate_str_length(ptr_text))) |
| 998 | { | ||
| 999 | ✗ | ret = GATE_RESULT_OUTOFMEMORY; | |
| 1000 | } | ||
| 1001 | } | ||
| 1002 | 1 | XtFree(ptr_text); | |
| 1003 | 1 | return ret; | |
| 1004 | } | ||
| 1005 | |||
| 1006 | 3 | static gate_result_t textbox_set_text(gate_ui_ctrl_t* ctrl, gate_string_t const* text) | |
| 1007 | { | ||
| 1008 | Widget w, w_txt; | ||
| 1009 | char buffer[GATE_MAX_COPYBUFFER_LENGTH]; | ||
| 1010 | 3 | char* ptr_buffer = &buffer[0]; | |
| 1011 | 3 | gate_size_t bufferlen = gate_string_length(text); | |
| 1012 | |||
| 1013 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | GATE_DEBUG_ASSERT(ctrl != NULL); |
| 1014 | 3 | w = GATE_UI_MOTIF_GET_CTRL_WIDGET(ctrl); | |
| 1015 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (!w) |
| 1016 | { | ||
| 1017 | ✗ | return GATE_RESULT_INVALIDARG; | |
| 1018 | } | ||
| 1019 | 3 | w_txt = GATE_UI_MOTIF_GET_CTRL_CONTAINER(ctrl); | |
| 1020 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
|
3 | if (w_txt) |
| 1021 | { | ||
| 1022 | 1 | w = w_txt; | |
| 1023 | } | ||
| 1024 | |||
| 1025 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (bufferlen >= sizeof(buffer)) |
| 1026 | { | ||
| 1027 | ✗ | bufferlen += 2; | |
| 1028 | ✗ | ptr_buffer = (char*)gate_mem_alloc(bufferlen); | |
| 1029 | ✗ | if (ptr_buffer == NULL) | |
| 1030 | { | ||
| 1031 | ✗ | return GATE_RESULT_OUTOFMEMORY; | |
| 1032 | } | ||
| 1033 | } | ||
| 1034 | else | ||
| 1035 | { | ||
| 1036 | 3 | bufferlen = sizeof(buffer); | |
| 1037 | } | ||
| 1038 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (text) |
| 1039 | { | ||
| 1040 | 3 | gate_string_to_buffer(text, ptr_buffer, bufferlen); | |
| 1041 | } | ||
| 1042 | else | ||
| 1043 | { | ||
| 1044 | ✗ | ptr_buffer[0] = 0; | |
| 1045 | } | ||
| 1046 | 3 | XmTextSetString(w, ptr_buffer); | |
| 1047 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
|
3 | if (ptr_buffer && (ptr_buffer != &buffer[0])) |
| 1048 | { | ||
| 1049 | ✗ | gate_mem_dealloc(ptr_buffer); | |
| 1050 | } | ||
| 1051 | 3 | return GATE_RESULT_OK; | |
| 1052 | } | ||
| 1053 | |||
| 1054 | 2 | static gate_result_t textbox_destroy(gate_ui_ctrl_t* ctrl) | |
| 1055 | { | ||
| 1056 | Widget w; | ||
| 1057 | Widget w_txt; | ||
| 1058 | |||
| 1059 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | GATE_DEBUG_ASSERT(ctrl != NULL); |
| 1060 | |||
| 1061 | 2 | w_txt = GATE_UI_MOTIF_GET_CTRL_CONTAINER(ctrl); | |
| 1062 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (w_txt) |
| 1063 | { | ||
| 1064 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (XtIsRealized(w_txt)) |
| 1065 | { | ||
| 1066 | 1 | XtUnrealizeWidget(w_txt); | |
| 1067 | } | ||
| 1068 | 1 | XtUnmanageChild(w_txt); | |
| 1069 | 1 | XtDestroyWidget(w_txt); | |
| 1070 | 1 | GATE_UI_MOTIF_SET_CTRL_CONTAINER(ctrl, NULL); | |
| 1071 | } | ||
| 1072 | |||
| 1073 | 2 | w = GATE_UI_MOTIF_GET_CTRL_WIDGET(ctrl); | |
| 1074 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (w) |
| 1075 | { | ||
| 1076 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | if (XtIsRealized(w)) |
| 1077 | { | ||
| 1078 | 2 | XtUnrealizeWidget(w); | |
| 1079 | } | ||
| 1080 | 2 | XtUnmanageChild(w); | |
| 1081 | 2 | XtDestroyWidget(w); | |
| 1082 | 2 | GATE_UI_MOTIF_SET_CTRL_WIDGET(ctrl, NULL); | |
| 1083 | } | ||
| 1084 | |||
| 1085 | 2 | return GATE_RESULT_OK; | |
| 1086 | } | ||
| 1087 | |||
| 1088 | |||
| 1089 | static gate_ui_motif_dispatcher_t textbox_dispatcher = | ||
| 1090 | { | ||
| 1091 | &textbox_destroy, | ||
| 1092 | &textbox_get_text, | ||
| 1093 | &textbox_set_text, | ||
| 1094 | NULL, | ||
| 1095 | NULL, | ||
| 1096 | NULL, | ||
| 1097 | NULL | ||
| 1098 | }; | ||
| 1099 | |||
| 1100 | 2 | gate_result_t gate_ui_textbox_create(gate_ui_textbox_t* txtbox, gate_ui_ctrl_t* parent, gate_ui_position_t const* position, | |
| 1101 | gate_string_t const* caption, gate_uint32_t flags, void* userparam) | ||
| 1102 | { | ||
| 1103 | 2 | gate_result_t ret = GATE_RESULT_FAILED; | |
| 1104 | do | ||
| 1105 | { | ||
| 1106 | 2 | Widget w = NULL; | |
| 1107 | Arg args[12]; | ||
| 1108 | 2 | Cardinal args_count = 0; | |
| 1109 | |||
| 1110 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
2 | if (!txtbox || !parent) |
| 1111 | { | ||
| 1112 | ✗ | ret = GATE_RESULT_INVALIDARG; | |
| 1113 | ✗ | break; | |
| 1114 | } | ||
| 1115 | |||
| 1116 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (GATE_FLAG_ENABLED(flags, GATE_UI_FLAG_TEXTBOX_MULTILINE)) |
| 1117 | { | ||
| 1118 | 1 | Widget w_txt = NULL; | |
| 1119 | 1 | Widget parent_widget = GATE_UI_MOTIF_GET_CTRL_CONTAINER(parent); | |
| 1120 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!parent_widget) |
| 1121 | { | ||
| 1122 | ✗ | parent_widget = GATE_UI_MOTIF_GET_CTRL_WIDGET(parent); | |
| 1123 | } | ||
| 1124 | 1 | XtSetArg(args[args_count], XmNscrollingPolicy, XmAPPLICATION_DEFINED);++args_count; | |
| 1125 | 1 | XtSetArg(args[args_count], XmNvisualPolicy, XmVARIABLE);++args_count; | |
| 1126 | 1 | XtSetArg(args[args_count], XmNscrollBarDisplayPolicy, XmSTATIC);++args_count; | |
| 1127 | //XtSetArg(args[args_count], XmNshadowThickness, 0);++args_count; | ||
| 1128 | 1 | XtSetArg(args[args_count], XmNshadowThickness, 0); ++args_count; | |
| 1129 | 1 | XtSetArg(args[args_count], XmNhighlightThickness, 0); ++args_count; | |
| 1130 | |||
| 1131 | 1 | w = XmCreateScrolledWindow(parent_widget, NULL, args, args_count); | |
| 1132 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!w) |
| 1133 | { | ||
| 1134 | ✗ | ret = GATE_RESULT_OUTOFRESOURCES; | |
| 1135 | ✗ | break; | |
| 1136 | } | ||
| 1137 | 1 | XtManageChild(w); | |
| 1138 | |||
| 1139 | 1 | args_count = 0; | |
| 1140 | 1 | XtSetArg(args[args_count], XmNeditMode, XmMULTI_LINE_EDIT); ++args_count; | |
| 1141 | 1 | XtSetArg(args[args_count], XmNshadowThickness, 1); ++args_count; | |
| 1142 | 1 | XtSetArg(args[args_count], XmNhighlightThickness, 1); ++args_count; | |
| 1143 | |||
| 1144 | 1 | w_txt = XmCreateText(w, NULL, args, args_count); | |
| 1145 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!w_txt) |
| 1146 | { | ||
| 1147 | ✗ | XtUnmanageChild(w); | |
| 1148 | ✗ | XtDestroyWidget(w); | |
| 1149 | ✗ | ret = GATE_RESULT_OUTOFRESOURCES; | |
| 1150 | ✗ | break; | |
| 1151 | } | ||
| 1152 | |||
| 1153 | 1 | ret = gate_ui_motif_ctrl_init(&txtbox->ctrl, w, userparam, NULL, parent, | |
| 1154 | w_txt, position, &flags, &textbox_dispatcher); | ||
| 1155 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATE_BREAK_IF_FAILED(ret); |
| 1156 | |||
| 1157 | 1 | XtManageChild(w_txt); | |
| 1158 | 1 | textbox_set_text(&txtbox->ctrl, caption); | |
| 1159 | } | ||
| 1160 | else | ||
| 1161 | { | ||
| 1162 | 1 | WidgetClass cls = xmTextWidgetClass; | |
| 1163 | 1 | XtSetArg(args[args_count], XmNshadowThickness, 1); ++args_count; | |
| 1164 | 1 | XtSetArg(args[args_count], XmNhighlightThickness, 1); ++args_count; | |
| 1165 | |||
| 1166 | 1 | ret = gate_ui_motif_ctrl_create(&txtbox->ctrl, cls, userparam, NULL, parent, | |
| 1167 | position, &flags, false, &textbox_dispatcher, args, args_count); | ||
| 1168 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATE_BREAK_IF_FAILED(ret); |
| 1169 | |||
| 1170 | 1 | textbox_set_text(&txtbox->ctrl, caption); | |
| 1171 | } | ||
| 1172 | |||
| 1173 | } while (0); | ||
| 1174 | |||
| 1175 | 2 | return ret; | |
| 1176 | } | ||
| 1177 | |||
| 1178 | ✗ | gate_result_t gate_ui_textbox_get_line(gate_ui_textbox_t* txtbox, gate_uint32_t* currentLine, gate_uint32_t* lineCount) | |
| 1179 | { | ||
| 1180 | ✗ | Widget w = textbox_get_widget(&txtbox->ctrl); | |
| 1181 | ✗ | if (!gate_ui_motif_widget_is_valid(w)) | |
| 1182 | { | ||
| 1183 | ✗ | return GATE_RESULT_INVALIDARG; | |
| 1184 | } | ||
| 1185 | ✗ | if (currentLine) | |
| 1186 | { | ||
| 1187 | ✗ | XmTextPosition pos = XmTextGetCursorPosition(w); | |
| 1188 | Position x, y; | ||
| 1189 | ✗ | if (XmTextPosToXY(w, pos, &x, &y)) | |
| 1190 | { | ||
| 1191 | ✗ | *currentLine = y; | |
| 1192 | } | ||
| 1193 | } | ||
| 1194 | ✗ | if (lineCount) | |
| 1195 | { | ||
| 1196 | ✗ | int cnt = 0; | |
| 1197 | ✗ | XtVaGetValues(w, XmNtotalLines, &cnt, NULL, NULL); | |
| 1198 | ✗ | *lineCount = cnt; | |
| 1199 | } | ||
| 1200 | ✗ | return GATE_RESULT_OK; | |
| 1201 | } | ||
| 1202 | |||
| 1203 | ✗ | gate_result_t gate_ui_textbox_get_column(gate_ui_textbox_t* txtbox, gate_uint32_t* currentColumn) | |
| 1204 | { | ||
| 1205 | ✗ | Widget w = textbox_get_widget(&txtbox->ctrl); | |
| 1206 | ✗ | if (!gate_ui_motif_widget_is_valid(w)) | |
| 1207 | { | ||
| 1208 | ✗ | return GATE_RESULT_INVALIDARG; | |
| 1209 | } | ||
| 1210 | ✗ | if (currentColumn) | |
| 1211 | { | ||
| 1212 | ✗ | XmTextPosition pos = XmTextGetCursorPosition(w); | |
| 1213 | Position x, y; | ||
| 1214 | ✗ | if (XmTextPosToXY(w, pos, &x, &y)) | |
| 1215 | { | ||
| 1216 | ✗ | *currentColumn = x; | |
| 1217 | } | ||
| 1218 | } | ||
| 1219 | ✗ | return GATE_RESULT_OK; | |
| 1220 | } | ||
| 1221 | |||
| 1222 | ✗ | gate_result_t gate_ui_textbox_get_selection(gate_ui_textbox_t* textbox, gate_uint32_t* sel_start, gate_uint32_t* sel_end, gate_string_t* sel_text) | |
| 1223 | { | ||
| 1224 | ✗ | return GATE_RESULT_NOTIMPLEMENTED; | |
| 1225 | } | ||
| 1226 | |||
| 1227 | ✗ | gate_result_t gate_ui_textbox_set_selection(gate_ui_textbox_t* textbox, gate_uint32_t sel_start, gate_uint32_t sel_end, gate_bool_t scroll_to_sel) | |
| 1228 | { | ||
| 1229 | ✗ | return GATE_RESULT_NOTIMPLEMENTED; | |
| 1230 | } | ||
| 1231 | |||
| 1232 | ✗ | gate_result_t gate_ui_textbox_replace_selection(gate_ui_textbox_t* textbox, gate_string_t const* text) | |
| 1233 | { | ||
| 1234 | ✗ | return GATE_RESULT_NOTIMPLEMENTED; | |
| 1235 | } | ||
| 1236 | |||
| 1237 | ✗ | gate_result_t gate_ui_textbox_undo(gate_ui_textbox_t* textbox) | |
| 1238 | { | ||
| 1239 | ✗ | return GATE_RESULT_NOTIMPLEMENTED; | |
| 1240 | } | ||
| 1241 | |||
| 1242 | ✗ | gate_result_t gate_ui_textbox_set_font(gate_ui_textbox_t* textbox, gate_ui_font_t const* font) | |
| 1243 | { | ||
| 1244 | ✗ | return GATE_RESULT_OK; | |
| 1245 | } | ||
| 1246 | |||
| 1247 | ✗ | gate_result_t gate_ui_textbox_find_next(gate_ui_textbox_t* textbox, gate_string_t const* token, gate_uint32_t flags) | |
| 1248 | { | ||
| 1249 | ✗ | return GATE_RESULT_NOTIMPLEMENTED; | |
| 1250 | } | ||
| 1251 | |||
| 1252 | #endif /* GATE_UI_MOTIF */ | ||
| 1253 |