Annotation of nono/wx/wxlogmonitor.cpp, revision 1.1.1.15

1.1       root        1: //
                      2: // nono
1.1.1.3   root        3: // Copyright (C) 2020 nono project
                      4: // Licensed under nono-license.txt
1.1       root        5: //
                      6: 
                      7: //
                      8: // ログモニター
                      9: //
                     10: 
1.1.1.9   root       11: #include "wxlogmonitor.h"
                     12: #include "logger.h"
                     13: #include "mainapp.h"
1.1.1.12  root       14: #include "monitor.h"
1.1.1.9   root       15: #include "sjis.h"
                     16: 
1.1.1.13  root       17: //#define WINDOW_DEBUG 1
                     18: 
                     19: #if defined(WINDOW_DEBUG)
                     20: #define DPRINTF(fmt...) printf(fmt)
                     21: #else
                     22: #define DPRINTF(fmt...) /**/
                     23: #endif
1.1       root       24: 
                     25: // コンストラクタ
1.1.1.7   root       26: WXLogMonitor::WXLogMonitor(wxWindow *parent)
1.1.1.10  root       27:        : inherited(parent, wxID_ANY, _("Log"), DEFAULT_STYLE | wxRESIZE_BORDER)
1.1       root       28: {
1.1.1.13  root       29:        DPRINTF("%s begin\n", __method__);
1.1       root       30: 
1.1.1.14  root       31:        // +-----------------+---------+
                     32:        // | LogMonitorPanel | VScroll |
                     33:        // +-----------------+---------+
1.1.1.4   root       34: 
1.1.1.14  root       35:        // モニタ
                     36:        logger = gMainApp.GetLogger();
                     37:        screen = new LogMonitorPanel(this, logger->GetMonitor());
1.1.1.4   root       38: 
                     39:        // スクロールバー
1.1.1.9   root       40:        vscroll = new WXScrollBar(this, wxID_ANY, wxSB_VERTICAL);
1.1.1.3   root       41: 
1.1.1.14  root       42:        // 自前レイアウト。
                     43:        SetMyLayout();
1.1.1.13  root       44: 
1.1.1.4   root       45:        // ウィンドウサイズを確定させる
1.1.1.13  root       46:        FontChanged();
1.1.1.4   root       47: 
                     48:        // スクロールのために (パネル領域での) MouseWheel イベントもここで
1.1.1.9   root       49:        // 受け取りたい。スクロールバー上のマウスホイール操作はスクロールバーが
                     50:        // 処理しているので問題ないが、パネル領域でのマウスホイール操作はここ
                     51:        // (wxFrame)ではなくパネルに対して飛んでくる。
1.1.1.4   root       52:        // ここで一緒に処理したほうが楽なので、こちらに回す。
1.1.1.15! root       53:        screen->Bind(wxEVT_MOUSEWHEEL, &WXLogMonitor::OnMouseWheel, this);
1.1.1.9   root       54: 
1.1.1.13  root       55:        // スクロールバーからの位置変更通知イベントは表示パネルへ回す。
1.1.1.15! root       56:        vscroll->Bind(NONO_EVT_SCROLL, &WXLogMonitor::OnScroll, this);
1.1.1.9   root       57: 
                     58:        // XXX ログウィンドウのコピーはまだ動いてない
                     59:        screen->DisconnectContextMenu();
1.1.1.13  root       60:        DPRINTF("%s done\n", __method__);
1.1       root       61: }
                     62: 
                     63: // デストラクタ
                     64: WXLogMonitor::~WXLogMonitor()
                     65: {
                     66: }
                     67: 
1.1.1.13  root       68: void
                     69: WXLogMonitor::FontChanged()
1.1.1.10  root       70: {
1.1.1.13  root       71:        screen->FontChanged();
                     72:        vscroll->FontChanged();
                     73:        DPRINTF("%s %d\n", __method__, screen->GetFontHeight());
                     74: 
                     75:        Fit();
1.1.1.10  root       76: }
                     77: 
1.1.1.14  root       78: // 自前レイアウト方式のサイズに関する情報を返す。
1.1.1.13  root       79: bool
1.1.1.14  root       80: WXLogMonitor::GetMySizeHints(wxSize *newp, wxSize *minp, wxSize *maxp,
                     81:        wxSize *incp)
1.1.1.13  root       82: {
                     83:        auto ssize = screen->GetSize();
                     84:        auto vsize = vscroll->GetSize();
                     85: 
                     86:        int x = ssize.x + vsize.x;
                     87:        int min_y = std::max(vscroll->GetMinSize().y, screen->GetScreenHeight(1));
                     88:        int new_y = std::max(ssize.y, min_y);
1.1.1.14  root       89:        wxSize newsize(x, new_y);
1.1.1.13  root       90:        wxSize minsize(x, min_y);
                     91:        wxSize maxsize(x, GetMaxClientSize().y);
                     92:        wxSize incsize(0, screen->GetFontHeight());
                     93: 
1.1.1.14  root       94:        if (newp) *newp = newsize;
                     95:        if (minp) *minp = minsize;
                     96:        if (maxp) *maxp = maxsize;
                     97:        if (incp) *incp = incsize;
                     98:        return true;
                     99: }
                    100: 
                    101: bool
                    102: WXLogMonitor::Layout()
                    103: {
                    104:        if (MyLayout() == false) {
                    105:                // コントロールを配置。
                    106:                wxSize client = GetClientSize();
                    107:                const wxSize vsize = vscroll->GetSize();
                    108: 
                    109:                int panel_width  = client.x - vsize.x;
                    110:                int panel_height = client.y;
                    111:                // 最低でも 1px ないと GTK とかに怒られる。今は起きないはず?
                    112:                if (__predict_false(panel_width < 1)) {
                    113:                        panel_width = 1;
                    114:                }
                    115:                if (__predict_false(panel_height < 1)) {
                    116:                        panel_height = 1;
                    117:                }
                    118: 
                    119:                screen->SetSize(0, 0, panel_width, panel_height);
                    120:                vscroll->SetSize(panel_width, 0, vsize.x, panel_height);
                    121: 
                    122:                // スクロールバーを再設定
                    123:                SetScroll();
                    124:        }
                    125:        return true;
1.1.1.4   root      126: }
                    127: 
                    128: // スクロールバーを再設定
                    129: void
                    130: WXLogMonitor::SetScroll()
                    131: {
1.1.1.14  root      132:        int displines;
                    133:        int row;
1.1.1.4   root      134:        int pos;
                    135:        int thumbsize;
                    136:        int range;
                    137:        int pagesize;
                    138: 
1.1.1.14  root      139:        displines = logger->GetDispLines();
1.1.1.7   root      140:        row = screen->GetRow();
1.1.1.4   root      141: 
1.1.1.14  root      142:        if (displines <= row) {
1.1.1.4   root      143:                pos = 0;
                    144:                range = row;
                    145:        } else {
1.1.1.14  root      146:                pos = (displines - row) - screen->GetUserData();
                    147:                range = displines;
1.1.1.4   root      148:        }
                    149:        thumbsize = row;
                    150:        pagesize = thumbsize - 1;
1.1.1.9   root      151:        vscroll->SetScrollParam(pos, thumbsize, range, pagesize);
1.1.1.4   root      152: }
                    153: 
                    154: // マウスホイールイベント (WXTextScreen 上で起きたやつをこっちに回してある)
1.1       root      155: void
1.1.1.4   root      156: WXLogMonitor::OnMouseWheel(wxMouseEvent& event)
                    157: {
                    158:        // GetWheelRotation() は上(奥)向きに回すと +120、下(手前)に回すと -120。
                    159:        // どこの決まりか知らんけど、スクロールバーのほうはホイール1段階で3行
                    160:        // スクロールするのでそれに合わせる。
                    161:        int pos = vscroll->GetThumbPosition();
                    162:        pos = pos - event.GetWheelRotation() / 40;
                    163: 
                    164:        int maxpos = vscroll->GetRange() - vscroll->GetThumbSize();
                    165:        if (pos < 0)
                    166:                pos = 0;
                    167:        if (pos > maxpos)
                    168:                pos = maxpos;
                    169: 
                    170:        DoScroll(pos);
1.1.1.9   root      171:        // スクロールバーの位置を追従
                    172:        vscroll->SetThumbPosition(pos);
1.1.1.4   root      173: }
                    174: 
1.1.1.9   root      175: // スクロールバーからの通知イベント
1.1.1.4   root      176: void
                    177: WXLogMonitor::OnScroll(wxScrollEvent& event)
                    178: {
                    179:        DoScroll(event.GetPosition());
                    180: }
                    181: 
                    182: // スクロール処理 (OnScroll() と OnMouseWheel() から呼ばれる)
                    183: void
                    184: WXLogMonitor::DoScroll(int pos)
                    185: {
                    186:        // pos 即ちスクロールバーの Thumb の位置は上端の位置。
                    187:        // 対してログウィンドウでは下端を基準にしたほうが処理しやすい。
1.1.1.14  root      188:        // userdata = 0 だと Thumb が下に接していてパネルの最下行が最新行、
                    189:        // userdata = 1 だと Thumb が下から1段階分上にありパネルの最下行は
                    190:        // 最新行の一つ前の行、となる。
1.1.1.4   root      191:        //
                    192:        // Range=6              Range=6
                    193:        // Pos=4                Pos=0
                    194:        // ThumbSz=2    ThumbSz=2
1.1.1.14  root      195:        // userdata=0   userdata=4
1.1.1.4   root      196:        // △                  △
                    197:        // □                  ■
                    198:        // □                  ■
                    199:        // □                  □
                    200:        // □                  □
                    201:        // ■                  □
                    202:        // ■                  □
                    203:        // ▽                  ▽
                    204: 
                    205:        // パネル最下行に表示する行の最新行からのオフセット
                    206:        // (0 なら Thumb が下端に接している)
1.1.1.14  root      207:        int vpos = vscroll->GetRange() - vscroll->GetThumbSize() - pos;
                    208:        screen->SetUserData(vpos);
1.1.1.4   root      209: }
                    210: 
1.1.1.9   root      211: 
1.1.1.7   root      212: //
                    213: // モニターオブジェクト
                    214: //
                    215: 
                    216: // コンストラクタ
1.1.1.14  root      217: LogMonitorPanel::LogMonitorPanel(WXLogMonitor *parent_, Monitor *monitor_)
                    218:        : inherited(parent_, monitor_)
1.1.1.7   root      219: {
                    220:        parent = parent_;
1.1.1.4   root      221: }
1.1       root      222: 
1.1.1.14  root      223: // デストラクタ
                    224: LogMonitorPanel::~LogMonitorPanel()
1.1.1.4   root      225: {
1.1       root      226: }
                    227: 
                    228: void
1.1.1.14  root      229: LogMonitorPanel::DoRefresh()
1.1       root      230: {
1.1.1.14  root      231:        inherited::DoRefresh();
1.1       root      232: 
1.1.1.14  root      233:        // スクロールバーを再設定。
                    234:        parent->SetScroll();
1.1       root      235: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.