comparison Libs/DF/textentry.lua @ 39:7944c081e5b4

- framework update. - ToC Update.
author Tercio
date Tue, 19 Jul 2016 13:23:40 -0300
parents 5da06cb420d4
children a960d5372b0c
comparison
equal deleted inserted replaced
38:219f04e5ba55 39:7944c081e5b4
14 local loadstring = loadstring --> lua local 14 local loadstring = loadstring --> lua local
15 local _string_len = string.len --> lua local 15 local _string_len = string.len --> lua local
16 16
17 local cleanfunction = function() end 17 local cleanfunction = function() end
18 local APITextEntryFunctions = false 18 local APITextEntryFunctions = false
19 local TextEntryMetaFunctions = {} 19
20 20 do
21 DF.TextEntryCounter = 1 21 local metaPrototype = {
22 WidgetType = "textentry",
23 SetHook = DF.SetHook,
24 RunHooksForWidget = DF.RunHooksForWidget,
25 }
26
27 _G [DF.GlobalWidgetControlNames ["textentry"]] = _G [DF.GlobalWidgetControlNames ["textentry"]] or metaPrototype
28 end
29
30 local TextEntryMetaFunctions = _G [DF.GlobalWidgetControlNames ["textentry"]]
31 DF.TextEntryCounter = DF.TextEntryCounter or 1
22 32
23 ------------------------------------------------------------------------------------------------------------ 33 ------------------------------------------------------------------------------------------------------------
24 --> metatables 34 --> metatables
25 35
26 TextEntryMetaFunctions.__call = function (_table, value) 36 TextEntryMetaFunctions.__call = function (_table, value)
49 --> get text 59 --> get text
50 local gmember_text = function (_object) 60 local gmember_text = function (_object)
51 return _object.editbox:GetText() 61 return _object.editbox:GetText()
52 end 62 end
53 63
54 local get_members_function_index = { 64 TextEntryMetaFunctions.GetMembers = TextEntryMetaFunctions.GetMembers or {}
55 ["tooltip"] = gmember_tooltip, 65 TextEntryMetaFunctions.GetMembers ["tooltip"] = gmember_tooltip
56 ["shown"] = gmember_shown, 66 TextEntryMetaFunctions.GetMembers ["shown"] = gmember_shown
57 ["width"] = gmember_width, 67 TextEntryMetaFunctions.GetMembers ["width"] = gmember_width
58 ["height"] = gmember_height, 68 TextEntryMetaFunctions.GetMembers ["height"] = gmember_height
59 ["text"] = gmember_text, 69 TextEntryMetaFunctions.GetMembers ["text"] = gmember_text
60 }
61 70
62 TextEntryMetaFunctions.__index = function (_table, _member_requested) 71 TextEntryMetaFunctions.__index = function (_table, _member_requested)
63 72 local func = TextEntryMetaFunctions.GetMembers [_member_requested]
64 local func = get_members_function_index [_member_requested]
65 if (func) then 73 if (func) then
66 return func (_table, _member_requested) 74 return func (_table, _member_requested)
67 end 75 end
68 76
69 local fromMe = _rawget (_table, _member_requested) 77 local fromMe = _rawget (_table, _member_requested)
119 --> text horizontal pos 127 --> text horizontal pos
120 local smember_horizontalpos = function (_object, _value) 128 local smember_horizontalpos = function (_object, _value)
121 return _object.editbox:SetJustifyH (string.lower (_value)) 129 return _object.editbox:SetJustifyH (string.lower (_value))
122 end 130 end
123 131
124 local set_members_function_index = { 132 TextEntryMetaFunctions.SetMembers = TextEntryMetaFunctions.SetMembers or {}
125 ["tooltip"] = smember_tooltip, 133 TextEntryMetaFunctions.SetMembers ["tooltip"] = smember_tooltip
126 ["show"] = smember_show, 134 TextEntryMetaFunctions.SetMembers ["show"] = smember_show
127 ["hide"] = smember_hide, 135 TextEntryMetaFunctions.SetMembers ["hide"] = smember_hide
128 ["width"] = smember_width, 136 TextEntryMetaFunctions.SetMembers ["width"] = smember_width
129 ["height"] = smember_height, 137 TextEntryMetaFunctions.SetMembers ["height"] = smember_height
130 ["text"] = smember_text, 138 TextEntryMetaFunctions.SetMembers ["text"] = smember_text
131 ["multiline"] = smember_multiline, 139 TextEntryMetaFunctions.SetMembers ["multiline"] = smember_multiline
132 ["align"] = smember_horizontalpos, 140 TextEntryMetaFunctions.SetMembers ["align"] = smember_horizontalpos
133 }
134 141
135 TextEntryMetaFunctions.__newindex = function (_table, _key, _value) 142 TextEntryMetaFunctions.__newindex = function (_table, _key, _value)
136 local func = set_members_function_index [_key] 143 local func = TextEntryMetaFunctions.SetMembers [_key]
137 if (func) then 144 if (func) then
138 return func (_table, _value) 145 return func (_table, _value)
139 else 146 else
140 return _rawset (_table, _key, _value) 147 return _rawset (_table, _key, _value)
141 end 148 end
142 end 149 end
143 150
144 ------------------------------------------------------------------------------------------------------------ 151 ------------------------------------------------------------------------------------------------------------
145 --> methods 152 --> methods
153 local cleanfunction = function()end
154 function TextEntryMetaFunctions:SetEnterFunction (func, param1, param2)
155 if (func) then
156 _rawset (self, "func", func)
157 else
158 _rawset (self, "func", cleanfunction)
159 end
160
161 if (param1 ~= nil) then
162 _rawset (self, "param1", param1)
163 end
164 if (param2 ~= nil) then
165 _rawset (self, "param2", param2)
166 end
167 end
146 168
147 --> set point 169 --> set point
148 function TextEntryMetaFunctions:SetPoint (MyAnchor, SnapTo, HisAnchor, x, y, Width) 170 function TextEntryMetaFunctions:SetPoint (MyAnchor, SnapTo, HisAnchor, x, y, Width)
149 171
150 if (type (MyAnchor) == "boolean" and MyAnchor and self.space) then 172 if (type (MyAnchor) == "boolean" and MyAnchor and self.space) then
250 function TextEntryMetaFunctions:GetTooltip() 272 function TextEntryMetaFunctions:GetTooltip()
251 return _rawget (self, "have_tooltip") 273 return _rawget (self, "have_tooltip")
252 end 274 end
253 275
254 --> hooks 276 --> hooks
255 function TextEntryMetaFunctions:SetHook (hookType, func)
256 if (func) then
257 _rawset (self, hookType.."Hook", func)
258 else
259 _rawset (self, hookType.."Hook", nil)
260 end
261 end
262
263 function TextEntryMetaFunctions:Enable() 277 function TextEntryMetaFunctions:Enable()
264 if (not self.editbox:IsEnabled()) then 278 if (not self.editbox:IsEnabled()) then
265 self.editbox:Enable() 279 self.editbox:Enable()
266 self.editbox:SetBackdropBorderColor (unpack (self.enabled_border_color)) 280 self.editbox:SetBackdropBorderColor (unpack (self.enabled_border_color))
267 self.editbox:SetBackdropColor (unpack (self.enabled_backdrop_color)) 281 self.editbox:SetBackdropColor (unpack (self.enabled_backdrop_color))
289 end 303 end
290 end 304 end
291 end 305 end
292 306
293 ------------------------------------------------------------------------------------------------------------ 307 ------------------------------------------------------------------------------------------------------------
294 --> scripts 308 --> scripts and hooks
309
295 local OnEnter = function (textentry) 310 local OnEnter = function (textentry)
296 311 local capsule = textentry.MyObject
297 if (textentry.MyObject.OnEnterHook) then 312
298 local interrupt = textentry.MyObject.OnEnterHook (textentry) 313 local kill = capsule:RunHooksForWidget ("OnEnter", textentry, capsule)
299 if (interrupt) then 314 if (kill) then
300 return 315 return
301 end 316 end
302 end 317
303 318 if (capsule.have_tooltip) then
304 if (textentry.MyObject.have_tooltip) then
305 GameCooltip2:Preset (2) 319 GameCooltip2:Preset (2)
306 GameCooltip2:AddLine (textentry.MyObject.have_tooltip) 320 GameCooltip2:AddLine (capsule.have_tooltip)
307 GameCooltip2:ShowCooltip (textentry, "tooltip") 321 GameCooltip2:ShowCooltip (textentry, "tooltip")
308 end 322 end
309 323
310 textentry.mouse_over = true 324 textentry.mouse_over = true
311 325
312 if (textentry:IsEnabled()) then 326 if (textentry:IsEnabled()) then
313 textentry.current_bordercolor = textentry.current_bordercolor or {textentry:GetBackdropBorderColor()} 327 textentry.current_bordercolor = textentry.current_bordercolor or {textentry:GetBackdropBorderColor()}
314 textentry:SetBackdropBorderColor (1, 1, 1, 1) 328 textentry:SetBackdropBorderColor (1, 1, 1, 1)
315 end 329 end
316
317 end 330 end
318 331
319 local OnLeave = function (textentry) 332 local OnLeave = function (textentry)
320 if (textentry.MyObject.OnLeaveHook) then 333 local capsule = textentry.MyObject
321 local interrupt = textentry.MyObject.OnLeaveHook (textentry) 334
322 if (interrupt) then 335 local kill = capsule:RunHooksForWidget ("OnLeave", textentry, capsule)
323 return 336 if (kill) then
324 end 337 return
325 end 338 end
326 339
327 if (textentry.MyObject.have_tooltip) then 340 if (textentry.MyObject.have_tooltip) then
328 GameCooltip2:ShowMe (false) 341 GameCooltip2:ShowMe (false)
329 end 342 end
330 343
331 textentry.mouse_over = false 344 textentry.mouse_over = false
332 345
333 if (textentry:IsEnabled()) then 346 if (textentry:IsEnabled()) then
334 textentry:SetBackdropBorderColor (unpack (textentry.current_bordercolor)) 347 textentry:SetBackdropBorderColor (unpack (textentry.current_bordercolor))
335 end 348 end
336
337 end 349 end
338 350
339 local OnHide = function (textentry) 351 local OnHide = function (textentry)
340 if (textentry.MyObject.OnHideHook) then 352 local capsule = textentry.MyObject
341 local interrupt = textentry.MyObject.OnHideHook (textentry) 353
342 if (interrupt) then 354 local kill = capsule:RunHooksForWidget ("OnHide", textentry, capsule)
343 return 355 if (kill) then
344 end 356 return
345 end 357 end
346 end 358 end
347 359
348 local OnShow = function (textentry) 360 local OnShow = function (textentry)
349 if (textentry.MyObject.OnShowHook) then 361 local capsule = textentry.MyObject
350 local interrupt = textentry.MyObject.OnShowHook (textentry) 362
351 if (interrupt) then 363 local kill = capsule:RunHooksForWidget ("OnShow", textentry, capsule)
352 return 364 if (kill) then
353 end 365 return
354 end 366 end
355 end 367 end
356 368
357 local OnEnterPressed = function (textentry, byScript) 369 local OnEnterPressed = function (textentry, byScript)
358 370 local capsule = textentry.MyObject
359 if (textentry.MyObject.OnEnterPressedHook) then 371
360 local interrupt = textentry.MyObject.OnEnterPressedHook (textentry) 372 local kill = capsule:RunHooksForWidget ("OnEnterPressed", textentry, capsule)
361 if (interrupt) then 373 if (kill) then
362 return 374 return
363 end
364 end 375 end
365 376
366 local texto = DF:trim (textentry:GetText()) 377 local texto = DF:trim (textentry:GetText())
367 if (_string_len (texto) > 0) then 378 if (_string_len (texto) > 0) then
368 textentry.text = texto 379 textentry.text = texto
371 end 382 end
372 else 383 else
373 textentry:SetText ("") 384 textentry:SetText ("")
374 textentry.MyObject.currenttext = "" 385 textentry.MyObject.currenttext = ""
375 end 386 end
376 textentry.focuslost = true --> perdeu_focus isso aqui pra quando estiver editando e clicar em outra caixa 387
377 textentry:ClearFocus() 388 if (not capsule.NoClearFocusOnEnterPressed) then
378 389 textentry.focuslost = true --> quando estiver editando e clicar em outra caixa
379 if (textentry.MyObject.tab_on_enter and textentry.MyObject.next) then 390 textentry:ClearFocus()
380 textentry.MyObject.next:SetFocus() 391
392 if (textentry.MyObject.tab_on_enter and textentry.MyObject.next) then
393 textentry.MyObject.next:SetFocus()
394 end
381 end 395 end
382 end 396 end
383 397
384 local OnEscapePressed = function (textentry) 398 local OnEscapePressed = function (textentry)
385 399 local capsule = textentry.MyObject
386 if (textentry.MyObject.OnEscapePressedHook) then 400
387 local interrupt = textentry.MyObject.OnEscapePressedHook (textentry) 401 local kill = capsule:RunHooksForWidget ("OnEscapePressed", textentry, capsule)
388 if (interrupt) then 402 if (kill) then
389 return 403 return
390 end 404 end
391 end 405
392
393 --textentry:SetText("")
394 --textentry.MyObject.currenttext = ""
395 textentry.focuslost = true 406 textentry.focuslost = true
396 textentry:ClearFocus() 407 textentry:ClearFocus()
397 end 408 end
398 409
410 local OnSpacePressed = function (textentry)
411 local capsule = textentry.MyObject
412
413 local kill = capsule:RunHooksForWidget ("OnSpacePressed", textentry, capsule)
414 if (kill) then
415 return
416 end
417 end
418
399 local OnEditFocusLost = function (textentry) 419 local OnEditFocusLost = function (textentry)
400 420
421 local capsule = textentry.MyObject
422
401 if (textentry:IsShown()) then 423 if (textentry:IsShown()) then
402 424
403 if (textentry.MyObject.OnEditFocusLostHook) then 425 local kill = capsule:RunHooksForWidget ("OnEditFocusLost", textentry, capsule)
404 local interrupt = textentry.MyObject.OnEditFocusLostHook (textentry) 426 if (kill) then
405 if (interrupt) then 427 return
406 return
407 end
408 end 428 end
409 429
410 if (not textentry.focuslost) then 430 if (not textentry.focuslost) then
411 local texto = DF:trim (textentry:GetText()) 431 local texto = DF:trim (textentry:GetText())
412 if (_string_len (texto) > 0) then 432 if (_string_len (texto) > 0) then
426 446
427 end 447 end
428 end 448 end
429 449
430 local OnEditFocusGained = function (textentry) 450 local OnEditFocusGained = function (textentry)
431 if (textentry.MyObject.OnEditFocusGainedHook) then 451
432 local interrupt = textentry.MyObject.OnEditFocusGainedHook (textentry) 452 local capsule = textentry.MyObject
433 if (interrupt) then 453
434 return 454 local kill = capsule:RunHooksForWidget ("OnEditFocusGained", textentry, capsule)
435 end 455 if (kill) then
436 end 456 return
457 end
458
437 textentry.MyObject.label:SetTextColor (1, 1, 1, 1) 459 textentry.MyObject.label:SetTextColor (1, 1, 1, 1)
438 end 460 end
439 461
440 local OnChar = function (textentry, text) 462 local OnChar = function (textentry, char)
441 if (textentry.MyObject.OnCharHook) then 463 local capsule = textentry.MyObject
442 local interrupt = textentry.MyObject.OnCharHook (textentry, text) 464
443 if (interrupt) then 465 local kill = capsule:RunHooksForWidget ("OnChar", textentry, char, capsule)
444 return 466 if (kill) then
445 end 467 return
446 end 468 end
447 end 469 end
448 470
449 local OnTextChanged = function (textentry, byUser) 471 local OnTextChanged = function (textentry, byUser)
450 if (textentry.MyObject.OnTextChangedHook) then 472 local capsule = textentry.MyObject
451 local interrupt = textentry.MyObject.OnTextChangedHook (textentry, byUser) 473
452 if (interrupt) then 474 local kill = capsule:RunHooksForWidget ("OnTextChanged", textentry, byUser, capsule)
453 return 475 if (kill) then
454 end 476 return
455 end 477 end
456 end 478 end
457 479
458 local OnTabPressed = function (textentry) 480 local OnTabPressed = function (textentry)
459 if (textentry.MyObject.OnTabPressedHook) then 481
460 local interrupt = textentry.MyObject.OnTabPressedHook (textentry, byUser) 482 local capsule = textentry.MyObject
461 if (interrupt) then 483
462 return 484 local kill = capsule:RunHooksForWidget ("OnTabPressed", textentry, byUser, capsule)
463 end 485 if (kill) then
486 return
464 end 487 end
465 488
466 if (textentry.MyObject.next) then 489 if (textentry.MyObject.next) then
467 OnEnterPressed (textentry, false) 490 OnEnterPressed (textentry, false)
468 textentry.MyObject.next:SetFocus() 491 textentry.MyObject.next:SetFocus()
581 604
582 if (not w and space) then 605 if (not w and space) then
583 w = space 606 w = space
584 elseif (w and space) then 607 elseif (w and space) then
585 if (DF.debug) then 608 if (DF.debug) then
586 print ("warning: you are using width and space, try use only space for better results.") 609 --print ("warning: you are using width and space, try use only space for better results.")
587 end 610 end
588 end 611 end
589 612
590 TextEntryObject.editbox:SetWidth (w) 613 TextEntryObject.editbox:SetWidth (w)
591 TextEntryObject.editbox:SetHeight (h) 614 TextEntryObject.editbox:SetHeight (h)
615 TextEntryObject.label = _G [name .. "_Desc"] 638 TextEntryObject.label = _G [name .. "_Desc"]
616 639
617 TextEntryObject.editbox:SetBackdrop ({bgFile = DF.folder .. "background", tileSize = 64, edgeFile = DF.folder .. "border_2", edgeSize = 10, insets = {left = 1, right = 1, top = 1, bottom = 1}}) 640 TextEntryObject.editbox:SetBackdrop ({bgFile = DF.folder .. "background", tileSize = 64, edgeFile = DF.folder .. "border_2", edgeSize = 10, insets = {left = 1, right = 1, top = 1, bottom = 1}})
618 641
619 --> hooks 642 --> hooks
643
644 TextEntryObject.HookList = {
645 OnEnter = {},
646 OnLeave = {},
647 OnHide = {},
648 OnShow = {},
649 OnEnterPressed = {},
650 OnEscapePressed = {},
651 OnSpacePressed = {},
652 OnEditFocusLost = {},
653 OnEditFocusGained = {},
654 OnChar = {},
655 OnTextChanged = {},
656 OnTabPressed = {},
657 }
658
620 TextEntryObject.editbox:SetScript ("OnEnter", OnEnter) 659 TextEntryObject.editbox:SetScript ("OnEnter", OnEnter)
621 TextEntryObject.editbox:SetScript ("OnLeave", OnLeave) 660 TextEntryObject.editbox:SetScript ("OnLeave", OnLeave)
622 TextEntryObject.editbox:SetScript ("OnHide", OnHide) 661 TextEntryObject.editbox:SetScript ("OnHide", OnHide)
623 TextEntryObject.editbox:SetScript ("OnShow", OnShow) 662 TextEntryObject.editbox:SetScript ("OnShow", OnShow)
624 663
625 TextEntryObject.editbox:SetScript ("OnEnterPressed", OnEnterPressed) 664 TextEntryObject.editbox:SetScript ("OnEnterPressed", OnEnterPressed)
626 TextEntryObject.editbox:SetScript ("OnEscapePressed", OnEscapePressed) 665 TextEntryObject.editbox:SetScript ("OnEscapePressed", OnEscapePressed)
666 TextEntryObject.editbox:SetScript ("OnSpacePressed", OnSpacePressed)
627 TextEntryObject.editbox:SetScript ("OnEditFocusLost", OnEditFocusLost) 667 TextEntryObject.editbox:SetScript ("OnEditFocusLost", OnEditFocusLost)
628 TextEntryObject.editbox:SetScript ("OnEditFocusGained", OnEditFocusGained) 668 TextEntryObject.editbox:SetScript ("OnEditFocusGained", OnEditFocusGained)
629 TextEntryObject.editbox:SetScript ("OnChar", OnChar) 669 TextEntryObject.editbox:SetScript ("OnChar", OnChar)
630 TextEntryObject.editbox:SetScript ("OnTextChanged", OnTextChanged) 670 TextEntryObject.editbox:SetScript ("OnTextChanged", OnTextChanged)
631 TextEntryObject.editbox:SetScript ("OnTabPressed", OnTabPressed) 671 TextEntryObject.editbox:SetScript ("OnTabPressed", OnTabPressed)
651 end 691 end
652 692
653 function DF:NewSpellEntry (parent, func, w, h, param1, param2, member, name) 693 function DF:NewSpellEntry (parent, func, w, h, param1, param2, member, name)
654 local editbox = DF:NewTextEntry (parent, parent, name, member, w, h, func, param1, param2) 694 local editbox = DF:NewTextEntry (parent, parent, name, member, w, h, func, param1, param2)
655 695
656 editbox:SetHook ("OnEditFocusGained", SpellEntryOnEditFocusGained) 696 -- editbox:SetHook ("OnEditFocusGained", SpellEntryOnEditFocusGained)
657 editbox:SetHook ("OnTextChanged", SpellEntryOnTextChanged) 697 -- editbox:SetHook ("OnTextChanged", SpellEntryOnTextChanged)
658 698
659 return editbox 699 return editbox
660 end 700 end
661 701
662 local function_gettext = function (self) 702 local function_gettext = function (self)
733 borderframe.editbox = scrollframe.editbox 773 borderframe.editbox = scrollframe.editbox
734 borderframe.editbox.borderframe = borderframe 774 borderframe.editbox.borderframe = borderframe
735 775
736 return borderframe 776 return borderframe
737 end 777 end
778
779
780 ------------------------------------------------------------------------------------
781 --auto complete
782
783 -- block -------------------
784 --code author Saiket from http://www.wowinterface.com/forums/showpost.php?p=245759&postcount=6
785 --- @return StartPos, EndPos of highlight in this editbox.
786 local function GetTextHighlight ( self )
787 local Text, Cursor = self:GetText(), self:GetCursorPosition();
788 self:Insert( "" ); -- Delete selected text
789 local TextNew, CursorNew = self:GetText(), self:GetCursorPosition();
790 -- Restore previous text
791 self:SetText( Text );
792 self:SetCursorPosition( Cursor );
793 local Start, End = CursorNew, #Text - ( #TextNew - CursorNew );
794 self:HighlightText( Start, End );
795 return Start, End;
796 end
797 local StripColors;
798 do
799 local CursorPosition, CursorDelta;
800 --- Callback for gsub to remove unescaped codes.
801 local function StripCodeGsub ( Escapes, Code, End )
802 if ( #Escapes % 2 == 0 ) then -- Doesn't escape Code
803 if ( CursorPosition and CursorPosition >= End - 1 ) then
804 CursorDelta = CursorDelta - #Code;
805 end
806 return Escapes;
807 end
808 end
809 --- Removes a single escape sequence.
810 local function StripCode ( Pattern, Text, OldCursor )
811 CursorPosition, CursorDelta = OldCursor, 0;
812 return Text:gsub( Pattern, StripCodeGsub ), OldCursor and CursorPosition + CursorDelta;
813 end
814 --- Strips Text of all color escape sequences.
815 -- @param Cursor Optional cursor position to keep track of.
816 -- @return Stripped text, and the updated cursor position if Cursor was given.
817 function StripColors ( Text, Cursor )
818 Text, Cursor = StripCode( "(|*)(|c%x%x%x%x%x%x%x%x)()", Text, Cursor );
819 return StripCode( "(|*)(|r)()", Text, Cursor );
820 end
821 end
822
823 local COLOR_END = "|r";
824 --- Wraps this editbox's selected text with the given color.
825 local function ColorSelection ( self, ColorCode )
826 local Start, End = GetTextHighlight( self );
827 local Text, Cursor = self:GetText(), self:GetCursorPosition();
828 if ( Start == End ) then -- Nothing selected
829 --Start, End = Cursor, Cursor; -- Wrap around cursor
830 return; -- Wrapping the cursor in a color code and hitting backspace crashes the client!
831 end
832 -- Find active color code at the end of the selection
833 local ActiveColor;
834 if ( End < #Text ) then -- There is text to color after the selection
835 local ActiveEnd;
836 local CodeEnd, _, Escapes, Color = 0;
837 while ( true ) do
838 _, CodeEnd, Escapes, Color = Text:find( "(|*)(|c%x%x%x%x%x%x%x%x)", CodeEnd + 1 );
839 if ( not CodeEnd or CodeEnd > End ) then
840 break;
841 end
842 if ( #Escapes % 2 == 0 ) then -- Doesn't escape Code
843 ActiveColor, ActiveEnd = Color, CodeEnd;
844 end
845 end
846
847 if ( ActiveColor ) then
848 -- Check if color gets terminated before selection ends
849 CodeEnd = 0;
850 while ( true ) do
851 _, CodeEnd, Escapes = Text:find( "(|*)|r", CodeEnd + 1 );
852 if ( not CodeEnd or CodeEnd > End ) then
853 break;
854 end
855 if ( CodeEnd > ActiveEnd and #Escapes % 2 == 0 ) then -- Terminates ActiveColor
856 ActiveColor = nil;
857 break;
858 end
859 end
860 end
861 end
862
863 local Selection = Text:sub( Start + 1, End );
864 -- Remove color codes from the selection
865 local Replacement, CursorReplacement = StripColors( Selection, Cursor - Start );
866
867 self:SetText( ( "" ):join(
868 Text:sub( 1, Start ),
869 ColorCode, Replacement, COLOR_END,
870 ActiveColor or "", Text:sub( End + 1 )
871 ) );
872
873 -- Restore cursor and highlight, adjusting for wrapper text
874 Cursor = Start + CursorReplacement;
875 if ( CursorReplacement > 0 ) then -- Cursor beyond start of color code
876 Cursor = Cursor + #ColorCode;
877 end
878 if ( CursorReplacement >= #Replacement ) then -- Cursor beyond end of color
879 Cursor = Cursor + #COLOR_END;
880 end
881
882 self:SetCursorPosition( Cursor );
883 -- Highlight selection and wrapper
884 self:HighlightText( Start, #ColorCode + ( #Replacement - #Selection ) + #COLOR_END + End );
885 end
886 -- end of the block ---------------------
887
888 local get_last_word = function (self)
889 self.lastword = ""
890 local cursor_pos = self.editbox:GetCursorPosition()
891 local text = self.editbox:GetText()
892 for i = cursor_pos, 1, -1 do
893 local character = text:sub (i, i)
894 if (character:match ("%a")) then
895 self.lastword = character .. self.lastword
896 else
897 break
898 end
899 end
900 end
901
902 --On Text Changed
903 local AutoComplete_OnTextChanged = function (editboxWidget, byUser, capsule)
904 capsule = capsule or editboxWidget.MyObject
905
906 local chars_now = editboxWidget:GetText():len()
907 if (not editboxWidget.ignore_textchange) then
908 --> backspace
909 if (chars_now == capsule.characters_count -1) then
910 capsule.lastword = capsule.lastword:sub (1, capsule.lastword:len()-1)
911 --> delete lots of text
912 elseif (chars_now < capsule.characters_count) then
913 --o auto complete selecionou outra palavra bem menor e caiu nesse filtro
914 editboxWidget.end_selection = nil
915 capsule:GetLastWord()
916 end
917 else
918 editboxWidget.ignore_textchange = nil
919 end
920 capsule.characters_count = chars_now
921 end
922
923 local AutoComplete_OnSpacePressed = function (editboxWidget, capsule)
924 capsule = capsule or editboxWidget.MyObject
925
926 -- if (not gotMatch) then
927 --editboxWidget.end_selection = nil
928 -- end
929 end
930
931 local AutoComplete_OnEscapePressed = function (editboxWidget)
932 editboxWidget.end_selection = nil
933 end
934
935 local AutoComplete_OnEnterPressed = function (editboxWidget)
936
937 local capsule = editboxWidget.MyObject
938 if (editboxWidget.end_selection) then
939 editboxWidget:SetCursorPosition (editboxWidget.end_selection)
940 editboxWidget:HighlightText (0, 0)
941 editboxWidget.end_selection = nil
942 --editboxWidget:Insert (" ") --estava causando a adição de uma palavra a mais quando o próximo catactere for um espaço
943 else
944 if (editboxWidget:IsMultiLine()) then
945 editboxWidget:Insert ("\n")
946 --reseta a palavra se acabou de ganhar focus e apertou enter
947 if (editboxWidget.focusGained) then
948 capsule.lastword = ""
949 editboxWidget.focusGained = nil
950 end
951 else
952 editboxWidget:Insert ("")
953 editboxWidget.focuslost = true
954 editboxWidget:ClearFocus()
955 end
956 end
957 capsule.lastword = ""
958
959 end
960
961 local AutoComplete_OnEditFocusGained = function (editboxWidget)
962 local capsule = editboxWidget.MyObject
963 capsule:GetLastWord()
964 editboxWidget.end_selection = nil
965 editboxWidget.focusGained = true
966 capsule.characters_count = editboxWidget:GetText():len()
967 end
968
969 local AutoComplete_OnChar = function (editboxWidget, char, capsule)
970 if (char == "") then
971 return
972 end
973
974 capsule = capsule or editboxWidget.MyObject
975 editboxWidget.end_selection = nil
976
977 if (editboxWidget.ignore_input) then
978 return
979 end
980
981 --reseta a palavra se acabou de ganhar focus e apertou espaço
982 if (editboxWidget.focusGained and char == " ") then
983 capsule.lastword = ""
984 editboxWidget.focusGained = nil
985 else
986 editboxWidget.focusGained = nil
987 end
988
989 if (char:match ("%a") or (char == " " and capsule.lastword ~= "")) then
990 capsule.lastword = capsule.lastword .. char
991 else
992 capsule.lastword = ""
993 end
994
995 editboxWidget.ignore_input = true
996 if (capsule.lastword:len() >= 2) then
997
998 local wordList = capsule [capsule.poolName]
999 if (not wordList) then
1000 if (DF.debug) then
1001 error ("Details! Framework: Invalid word list table.")
1002 end
1003 return
1004 end
1005
1006 for i = 1, #wordList do
1007 local thisWord = wordList [i]
1008 if (thisWord and (thisWord:find ("^" .. capsule.lastword) or thisWord:lower():find ("^" .. capsule.lastword))) then
1009 local rest = thisWord:gsub (capsule.lastword, "")
1010 rest = rest:lower():gsub (capsule.lastword, "")
1011 local cursor_pos = editboxWidget:GetCursorPosition()
1012 editboxWidget:Insert (rest)
1013 editboxWidget:HighlightText (cursor_pos, cursor_pos + rest:len())
1014 editboxWidget:SetCursorPosition (cursor_pos)
1015 editboxWidget.end_selection = cursor_pos + rest:len()
1016 editboxWidget.ignore_textchange = true
1017 break
1018 end
1019 end
1020
1021 end
1022 editboxWidget.ignore_input = false
1023 end
1024
1025 function TextEntryMetaFunctions:SetAsAutoComplete (poolName)
1026
1027 self.lastword = ""
1028 self.characters_count = 0
1029 self.poolName = poolName
1030 self.GetLastWord = get_last_word --editbox:GetLastWord()
1031 self.NoClearFocusOnEnterPressed = true --avoid auto clear focus
1032
1033 self:SetHook ("OnEditFocusGained", AutoComplete_OnEditFocusGained)
1034 self.editbox:HookScript ("OnEscapePressed", AutoComplete_OnEscapePressed)
1035
1036 -- self:SetHook ("OnTextChanged", AutoComplete_OnTextChanged)
1037 self:SetHook ("OnEnterPressed", AutoComplete_OnEnterPressed)
1038 -- self:SetHook ("OnChar", AutoComplete_OnChar)
1039 -- self:SetHook ("OnSpacePressed", AutoComplete_OnSpacePressed)
1040
1041 self.editbox:SetScript ("OnTextChanged", AutoComplete_OnTextChanged)
1042 -- self.editbox:SetScript ("OnEnterPressed", AutoComplete_OnEnterPressed)
1043 self.editbox:SetScript ("OnChar", AutoComplete_OnChar)
1044 self.editbox:SetScript ("OnSpacePressed", AutoComplete_OnSpacePressed)
1045
1046 end
1047
1048 -- endp