MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / DrawEditBox

Method DrawEditBox

src/misc_gui.cpp:736–785  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

734}
735
736void QueryString::DrawEditBox(const Window *w, WidgetID wid) const
737{
738 const NWidgetLeaf *wi = w->GetWidget<NWidgetLeaf>(wid);
739
740 assert((wi->type & WWT_MASK) == WWT_EDITBOX);
741
742 bool rtl = _current_text_dir == TD_RTL;
743 Dimension sprite_size = GetScaledSpriteSize(rtl ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT);
744 int clearbtn_width = sprite_size.width + WidgetDimensions::scaled.imgbtn.Horizontal();
745
746 Rect r = wi->GetCurrentRect();
747 Rect cr = r.WithWidth(clearbtn_width, !rtl);
748 Rect fr = r.Indent(clearbtn_width, !rtl);
749
750 DrawFrameRect(cr, wi->colour, wi->IsLowered() ? FrameFlag::Lowered : FrameFlags{});
751 DrawSpriteIgnorePadding(rtl ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT, PAL_NONE, cr, SA_CENTER);
752 if (this->text.GetText().empty()) GfxFillRect(cr.Shrink(WidgetDimensions::scaled.bevel), GetColourGradient(wi->colour, SHADE_DARKER), FILLRECT_CHECKER);
753
754 DrawFrameRect(fr, wi->colour, {FrameFlag::Lowered, FrameFlag::Darkened});
755 GfxFillRect(fr.Shrink(WidgetDimensions::scaled.bevel), PC_BLACK);
756
757 fr = fr.Shrink(WidgetDimensions::scaled.framerect);
758 /* Limit the drawing of the string inside the widget boundaries */
759 DrawPixelInfo dpi;
760 if (!FillDrawPixelInfo(&dpi, fr)) return;
761 /* Keep coordinates relative to the window. */
762 dpi.left += fr.left;
763 dpi.top += fr.top;
764
765 AutoRestoreBackup dpi_backup(_cur_dpi, &dpi);
766
767 /* We will take the current widget length as maximum width, with a small
768 * space reserved at the end for the caret to show */
769 const Textbuf *tb = &this->text;
770 fr = ScrollEditBoxTextRect(fr, *tb);
771
772 /* If we have a marked area, draw a background highlight. */
773 if (tb->marklength != 0) GfxFillRect(fr.left + tb->markxoffs, fr.top, fr.left + tb->markxoffs + tb->marklength - 1, fr.bottom, PC_GREY);
774
775 DrawString(fr.left, fr.right, CentreBounds(fr.top, fr.bottom, GetCharacterHeight(FS_NORMAL)), tb->GetText(), TC_YELLOW);
776 bool focussed = w->IsWidgetGloballyFocused(wid) || IsOSKOpenedFor(w, wid);
777 if (focussed && tb->caret) {
778 int caret_width = GetCaretWidth();
779 if (rtl) {
780 DrawString(fr.right - tb->pixels + tb->caretxoffs - caret_width, fr.right - tb->pixels + tb->caretxoffs, CentreBounds(fr.top, fr.bottom, GetCharacterHeight(FS_NORMAL)), "_", TC_WHITE);
781 } else {
782 DrawString(fr.left + tb->caretxoffs, fr.left + tb->caretxoffs + caret_width, CentreBounds(fr.top, fr.bottom, GetCharacterHeight(FS_NORMAL)), "_", TC_WHITE);
783 }
784 }
785}
786
787/**
788 * Get the current caret position.

Callers 1

DrawMethod · 0.80

Calls 15

GetScaledSpriteSizeFunction · 0.85
DrawSpriteIgnorePaddingFunction · 0.85
GetColourGradientFunction · 0.85
ScrollEditBoxTextRectFunction · 0.85
CentreBoundsFunction · 0.85
GetCharacterHeightFunction · 0.85
IsOSKOpenedForFunction · 0.85
GetCaretWidthFunction · 0.85
HorizontalMethod · 0.80
GetCurrentRectMethod · 0.80
WithWidthMethod · 0.80
IndentMethod · 0.80

Tested by

no test coverage detected