comparison foosdk/sdk/foobar2000/foo_sample/ui_element.cpp @ 1:20d02a178406 default tip

*: check in everything else yay
author Paper <paper@tflc.us>
date Mon, 05 Jan 2026 02:15:46 -0500
parents
children
comparison
equal deleted inserted replaced
0:e9bb126753e7 1:20d02a178406
1 #include "stdafx.h"
2
3 #include <libPPUI/win32_op.h>
4 #include <helpers/BumpableElem.h>
5
6
7 namespace {
8 // Anonymous namespace : standard practice in fb2k components
9 // Nothing outside should have any reason to see these symbols, and we don't want funny results if another cpp has similarly named classes.
10 // service_factory at the bottom takes care of publishing our class.
11
12 // This is our GUID. Substitute with your own when reusing code.
13 static const GUID guid_myelem = { 0xb46dc166, 0x88f3, 0x4b45, { 0x9f, 0x77, 0xab, 0x33, 0xf4, 0xc3, 0xf2, 0xe4 } };
14
15 class CMyElemWindow : public ui_element_instance, public CWindowImpl<CMyElemWindow> {
16 public:
17 // ATL window class declaration. Replace class name with your own when reusing code.
18 DECLARE_WND_CLASS_EX(TEXT("{DC2917D5-1288-4434-A28C-F16CFCE13C4B}"),CS_VREDRAW | CS_HREDRAW,(-1));
19
20 void initialize_window(HWND parent) {WIN32_OP(Create(parent) != NULL);}
21
22 BEGIN_MSG_MAP_EX(CMyElemWindow)
23 MESSAGE_HANDLER(WM_LBUTTONDOWN,OnLButtonDown);
24 MSG_WM_ERASEBKGND(OnEraseBkgnd)
25 MSG_WM_PAINT(OnPaint)
26 END_MSG_MAP()
27
28 CMyElemWindow(ui_element_config::ptr,ui_element_instance_callback_ptr p_callback);
29 HWND get_wnd() {return *this;}
30 void set_configuration(ui_element_config::ptr config) {m_config = config;}
31 ui_element_config::ptr get_configuration() {return m_config;}
32 static GUID g_get_guid() {
33 return guid_myelem;
34 }
35 static GUID g_get_subclass() {return ui_element_subclass_utility;}
36 static void g_get_name(pfc::string_base & out) {out = "Sample UI Element";}
37 static ui_element_config::ptr g_get_default_configuration() {return ui_element_config::g_create_empty(g_get_guid());}
38 static const char * g_get_description() {return "This is a sample UI Element.";}
39
40 void notify(const GUID & p_what, t_size p_param1, const void * p_param2, t_size p_param2size);
41 private:
42 LRESULT OnLButtonDown(UINT,WPARAM,LPARAM,BOOL&) {m_callback->request_replace(this);return 0;}
43 void OnPaint(CDCHandle);
44 BOOL OnEraseBkgnd(CDCHandle);
45 ui_element_config::ptr m_config;
46 protected:
47 // this must be declared as protected for ui_element_impl_withpopup<> to work.
48 const ui_element_instance_callback_ptr m_callback;
49 };
50 void CMyElemWindow::notify(const GUID & p_what, t_size p_param1, const void * p_param2, t_size p_param2size) {
51 if (p_what == ui_element_notify_colors_changed || p_what == ui_element_notify_font_changed) {
52 // we use global colors and fonts - trigger a repaint whenever these change.
53 Invalidate();
54 }
55 }
56 CMyElemWindow::CMyElemWindow(ui_element_config::ptr config,ui_element_instance_callback_ptr p_callback) : m_callback(p_callback), m_config(config) {
57 }
58
59 BOOL CMyElemWindow::OnEraseBkgnd(CDCHandle dc) {
60 CRect rc; WIN32_OP_D( GetClientRect(&rc) );
61 CBrush brush;
62 WIN32_OP_D( brush.CreateSolidBrush( m_callback->query_std_color(ui_color_background) ) != NULL );
63 WIN32_OP_D( dc.FillRect(&rc, brush) );
64 return TRUE;
65 }
66 void CMyElemWindow::OnPaint(CDCHandle) {
67 CPaintDC dc(*this);
68 dc.SetTextColor( m_callback->query_std_color(ui_color_text) );
69 dc.SetBkMode(TRANSPARENT);
70 SelectObjectScope fontScope(dc, (HGDIOBJ) m_callback->query_font_ex(ui_font_default) );
71 const UINT format = DT_NOPREFIX | DT_CENTER | DT_VCENTER | DT_SINGLELINE;
72 CRect rc;
73 WIN32_OP_D( GetClientRect(&rc) );
74 WIN32_OP_D( dc.DrawText(_T("This is a sample element."), -1, &rc, format) > 0 );
75 }
76
77 // ui_element_impl_withpopup autogenerates standalone version of our component and proper menu commands. Use ui_element_impl instead if you don't want that.
78 class ui_element_myimpl : public ui_element_impl_withpopup<CMyElemWindow> {};
79
80 static service_factory_single_t<ui_element_myimpl> g_ui_element_myimpl_factory;
81
82 } // namespace