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
|
/*
* This file is part of Poedit (https://poedit.net)
*
* Copyright (C) 2014-2026 Vaclav Slavik
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
#ifndef Poedit_customcontrols_h
#define Poedit_customcontrols_h
#include "colorscheme.h"
#include "concurrency.h"
#include "language.h"
#include <wx/bmpbuttn.h>
#include <wx/dataview.h>
#include <wx/statbmp.h>
#include <wx/stattext.h>
#include <wx/statline.h>
#include <wx/hyperlink.h>
#include <wx/xrc/xmlres.h>
class WXDLLIMPEXP_ADV wxActivityIndicator;
#include <exception>
#include <functional>
/// Label marking a subsection of a dialog:
class HeadingLabel : public wxStaticText
{
public:
HeadingLabel(wxWindow *parent, const wxString& label);
};
/// Label that auto-wraps itself to fit surrounding control's width.
class AutoWrappingText : public wxStaticText
{
public:
AutoWrappingText(wxWindow *parent, wxWindowID winid, const wxString& label);
void SetLanguage(Language lang);
void SetAlignment(TextDirection dir);
void SetAndWrapLabel(const wxString& label);
bool InformFirstDirection(int direction, int size, int availableOtherDir) override;
void SetLabel(const wxString& label) override;
protected:
void OnSize(wxSizeEvent& e);
bool RewrapForWidth(int width);
#ifdef __WXOSX__
wxSize DoGetBestSize() const override;
#endif
wxString m_text;
int m_wrapWidth;
Language m_language;
};
/// A helper class that implements better sizer behavior for a window that contains AutoWrappingText
template<typename Base>
class WindowWith2DSizingConstraints : public Base
{
public:
using Base::Base;
void Fit() override
{
IterateUntilConvergence([=]{ this->SetSize(this->GetBestSize()); });
}
bool Layout() override
{
IterateUntilConvergence([=]{ Base::Layout(); });
return true;
}
private:
template<typename T>
void IterateUntilConvergence(T&& action)
{
// iterate sizing because performing layout may invalidate some best
// sizes (AutoWrappingText) and may need to be redone.
wxSize best = this->GetBestSize();
while ( true )
{
action();
wxSize updatedBest = this->GetBestSize();
if ( best == updatedBest )
break;
best = updatedBest;
}
}
};
/// Like AutoWrappingText, but allows selecting (macOS) or at least copying (Windows)
/// the text too.
class SelectableAutoWrappingText : public AutoWrappingText
{
public:
SelectableAutoWrappingText(wxWindow *parent, wxWindowID winid, const wxString& label);
};
/**
Longer, often multiline, explanation label used to provide more information
about the effects of some less obvious settings. Typeset using smaller font
on macOS and grey appearance. Auto-wraps itself to fit surrounding control's
width.
*/
class ExplanationLabel : public AutoWrappingText
{
public:
ExplanationLabel(wxWindow *parent, const wxString& label);
static wxColour GetTextColor() { return ColorScheme::Get(Color::SecondaryLabel); }
};
/// Like ExplanationLabel, but nonwrapping
class SecondaryLabel : public wxStaticText
{
public:
SecondaryLabel(wxWindow *parent, const wxString& label);
static wxColour GetTextColor() { return ExplanationLabel::GetTextColor(); }
};
/// "Learn more" hyperlink for dialogs.
class LearnMoreLink : public wxHyperlinkCtrl
{
public:
LearnMoreLink(wxWindow *parent, const wxString& url, wxString label = wxString(), wxWindowID winid = wxID_ANY);
};
class LearnMoreLinkXmlHandler : public wxXmlResourceHandler
{
public:
LearnMoreLinkXmlHandler() {}
wxObject *DoCreateResource() override;
bool CanHandle(wxXmlNode *node) override;
};
class HelpButton
#ifdef __WXOSX__
: public wxButton
#else
: public LearnMoreLink
#endif
{
public:
HelpButton(wxWindow *parent, const wxString& helpPage);
};
/// Indicator of background activity, using spinners where appropriate.
class ActivityIndicator : public wxWindow
{
public:
enum Flags
{
Centered = 1,
};
ActivityIndicator(wxWindow *parent, int flags = 0);
/// Start indicating, with optional progress label.
void Start(const wxString& msg = "");
/// Stop the indicator.
void Stop();
/// Stop the indicator and report error in its place.
void StopWithError(const wxString& msg);
/// Is between Start() and Stop() calls?
bool IsRunning() const { return m_running; }
/// Convenience function for showing error message in the indicator
std::function<void(dispatch::exception_ptr)> HandleError;
bool HasTransparentBackground() override { return true; }
private:
void UpdateLayoutAfterTextChange();
bool m_running;
wxActivityIndicator *m_spinner;
wxStaticText *m_label, *m_error;
};
/// A bit nicer (macOS), color scheme aware, and easier to use image button
class ImageButton : public wxBitmapButton
{
public:
ImageButton(wxWindow *parent, const wxString& bitmapName);
private:
wxString m_bitmapName;
};
/// Color scheme aware static bitmap
class StaticBitmap : public wxStaticBitmap
{
public:
StaticBitmap(wxWindow *parent, const wxString& bitmapName);
void SetBitmapName(const wxString& bitmapName);
private:
wxString m_bitmapName;
};
/// Avatar icon
class AvatarIcon : public wxWindow
{
public:
AvatarIcon(wxWindow *parent, const wxSize& size);
/// Set name to be used if image can't be loaded
void SetUserName(const wxString& name);
void LoadIcon(const wxFileName& f);
bool HasTransparentBackground() override { return true; }
private:
void InitForSize();
void OnPaint(wxPaintEvent&);
private:
wxRegion m_clipping;
wxBitmap m_bitmap;
wxString m_placeholder;
};
/// wxDataViewListCtrl with icon and two-line content
class IconAndSubtitleListCtrl : public wxDataViewListCtrl
{
public:
IconAndSubtitleListCtrl(wxWindow *parent, const wxString& columnTitle, long style = wxBORDER_NONE);
void AppendFormattedItem(const wxBitmap& icon, const wxString& title, const wxString& description);
void UpdateFormattedItem(unsigned row, const wxString& title, const wxString& description);
protected:
int GetDefaultRowHeight() const;
wxString FormatItemText(const wxString& title, const wxString& description);
private:
#ifndef __WXGTK__
void OnColorChange();
wxString GetSecondaryFormatting();
wxString m_secondaryFormatting[2];
#endif
class MultilineTextRenderer;
};
#endif // Poedit_customcontrols_h
|