From 8c60155a16ac6fd9d30c2d2253c5e676d973178b Mon Sep 17 00:00:00 2001 From: RamonUnch <74856804+RamonUnch@users.noreply.github.com> Date: Fri, 6 Dec 2024 20:37:33 +0100 Subject: [PATCH 1/3] Add files via upload --- ConfigManager.h | 2 +- GpMain.cpp | 117 +++++++++++++++++---------------- RSearch.cpp | 2 +- Search.cpp | 12 ++-- Search.h | 2 +- editwing/ewCommon.h | 4 +- editwing/ewCtrl1.h | 2 +- editwing/ewView.h | 56 +--------------- editwing/ip_cursor.cpp | 9 +-- editwing/ip_draw.cpp | 29 +++++++-- editwing/ip_parse.cpp | 83 +++++++++++++++++------- editwing/ip_scroll.cpp | 51 +++++++-------- editwing/ip_view.h | 142 +++++++++++++++++++++++++---------------- editwing/ip_wrap.cpp | 8 +-- kilib/app.cpp | 3 + kilib/app.h | 2 +- kilib/cmdarg.h | 2 +- kilib/file.cpp | 2 +- kilib/file.h | 4 +- kilib/find.h | 2 +- kilib/ktlaptr.h | 1 + kilib/ktlgap.h | 34 +++++----- kilib/memory.h | 118 ++++++++++++++++++++++------------ kilib/registry.h | 2 +- kilib/string.cpp | 19 +++--- kilib/textfile.cpp | 4 +- kilib/textfile.h | 4 +- kilib/window.cpp | 15 +++-- kilib/window.h | 2 +- kilib/winutil.cpp | 8 +-- 30 files changed, 408 insertions(+), 333 deletions(-) diff --git a/ConfigManager.h b/ConfigManager.h index fa612571..dfc3e76c 100644 --- a/ConfigManager.h +++ b/ConfigManager.h @@ -19,7 +19,7 @@ void SetFontSize(LOGFONT *font, HDC hDC, int fsiz, int fx); //@} //========================================================================= -class ConfigManager : public ki::Object +class ConfigManager { public: diff --git a/GpMain.cpp b/GpMain.cpp index 3b2b72d8..962a0931 100644 --- a/GpMain.cpp +++ b/GpMain.cpp @@ -681,7 +681,7 @@ void GreenPadWnd::on_pagesetup() PAGESETUPDLG psd; mem00(&psd, sizeof(psd)); psd.lStructSize = sizeof(psd); - // FIXME: use local units... + // FIXME: use local uniTS->.. psd.Flags = PSD_INTHOUSANDTHSOFINCHES|PSD_DISABLEORIENTATION|PSD_DISABLEPAPER|PSD_DISABLEPRINTER|PSD_MARGINS; psd.hwndOwner = hwnd(); CopyRect(&psd.rtMargin, cfg_.PMargins()); @@ -914,12 +914,12 @@ void GreenPadWnd::on_drop( HDROP hd ) // Get length of the i string for array size. UINT len = ::myDragQueryFile( hd, i, NULL, 0)+1; len = Max(len, (UINT)MAX_PATH); // ^ the Above may fail on NT3.1 - TCHAR *str = (TCHAR *)malloc( sizeof(TCHAR) * len ); + TCHAR *str = (TCHAR *)TS.alloc( sizeof(TCHAR) * len ); if( str ) { ::myDragQueryFile( hd, i, str, len ); Open( str, AutoDetect ); - free( str ); + TS.freelast( str, sizeof(TCHAR) * len ); } } ::DragFinish( hd ); @@ -1959,85 +1959,82 @@ int kmain() { // MsgBox(GetCommandLine(), TEXT("Command Line"), MB_OK); LOGGER( "kmain() begin" ); + GreenPadWnd wnd; + { + Argv arg; + ulong i; + int optL = -1; + int optC = 0; - Argv arg; - ulong i; + LOGGER( "argv processed" ); - LOGGER( "argv processed" ); + //-- まずオプションスイッチを処理 - //-- まずオプションスイッチを処理 + for( i=1; i p+len+1 ) // delete np; // was allocated diff --git a/editwing/ip_draw.cpp b/editwing/ip_draw.cpp index e53b4967..13016542 100644 --- a/editwing/ip_draw.cpp +++ b/editwing/ip_draw.cpp @@ -302,11 +302,11 @@ static const uchar ctl2Map[34] = { }; Painter::Painter( HWND hwnd, const VConfig& vc ) : hwnd_ ( hwnd ) - , dc_ ( ::GetDC(hwnd) ) - , cdc_ ( ::CreateCompatibleDC( dc_ ) ) - , font_ ( init_font( vc ) ) + , dc_ ( NULL ) + , cdc_ ( NULL ) + , font_ ( NULL ) , pen_ ( NULL ) - , brush_ ( ::CreateSolidBrush( vc.color[BG] ) ) + , brush_ ( NULL ) // , widthTable_( new int[65536] ) , widthTable_( wtable ) , height_ ( 16 ) @@ -316,9 +316,16 @@ Painter::Painter( HWND hwnd, const VConfig& vc ) , useOutA_ ( app().isWin32s() || (!app().isNT() && app().getOOSVer() <= MKVER(4,00,99)) ) #endif { - // Release the DC that was captured just to make cdc_. + Init( vc ); +} +void Painter::Init( const VConfig& vc ) +{ + dc_ = ::GetDC(hwnd_); + cdc_ = ::CreateCompatibleDC( dc_ ); ::ReleaseDC( NULL, dc_ ); dc_ = NULL; + font_ = init_font( vc ); + brush_ = ::CreateSolidBrush( vc.color[BG] ); // 制御文字を描画するか否か?のフラグを記憶, // Whether to draw control characters or not? flag is stored. for( unsigned i=0; i( ar->alloc( (sizeof(Keyword)-2 + l*sizeof(unicode)) ) ); + ushort l = static_cast(ll); + Keyword *x = reinterpret_cast( ar->alloc( (sizeof(Keyword) + l * sizeof(unicode)) ) ); if( x ) { x->next = NULL; @@ -197,6 +198,8 @@ struct Keyword : public ki::Object memmove(x->str, s, l*sizeof(unicode)); x->str[l] = L'\0'; } + //else MessageBox(NULL, s, NULL, 0); + //LOGGERF( TEXT("sizeof(Keyword) = %d, allocated = %d / %d"), sizeof(Keyword), (UINT)(ar->end - ar->sta), ar->dim); return x; } }; @@ -239,7 +242,7 @@ class TagMap Keyword* tag_[3]; // 0:CE 1:CB 2:LB bool esc_, q1_, q2_, map_[768]; // 128 BYTE arbuf[64]; - DArena ar; + Arena ar; public: @@ -346,8 +349,7 @@ class TagMap class KeywordMap { Keyword* backet_[HTABLE_SIZE]; - //storage dustbox_; - DArena ar; + Arena ar; size_t elems_; bool (*compare_)(const unicode*,const unicode*,ulong); uint (*hash)( const unicode* a, ulong al ); @@ -355,7 +357,7 @@ class KeywordMap KeywordMap( bool bCaseSensitive ) : /*dustbox_( 32 )*/ - ar ( 65536 ) + ar ( NULL, 0 ) , elems_ ( 0 ) , compare_( bCaseSensitive ? compare_s : compare_i ) , hash ( bCaseSensitive ? hash_s : hash_i ) @@ -364,14 +366,24 @@ class KeywordMap mem00( backet_, sizeof(backet_) ); } - void Trim() { ar.trim(); } + void SetArenaBufSize( size_t count ) + { + BYTE *buf = NULL; + //count *= sizeof(unicode); + if(count != 0) + buf = (BYTE*)malloc(count); -// ~KeywordMap() -// { -// // 解放 -// for( ulong i=0; i 0 ) { - r.getLine(); - if( len != 0 ) - parser_->AddKeyword( str, len ); + // calculate the necessary memory for the keyword list. + size_t n = siz, totsz = 0; + const unicode *b = defbuf; + while( n-- ) + { + enum { align = sizeof(void*) -1 }; + ++b; + if( *b == '\n' ) + { + if( n && b[1] == '\n' || b[1] == '\r' ) + continue; // Skip extra empty lines + totsz = (totsz + align + sizeof(Keyword)) & ~align; + } + else + totsz += 2; + } + + parser_->SetKeywordArenaSize( totsz ); + // 5行目以降:キーワードリスト + while( !r.isEmpty() ) + { + r.getLine(); + if( len != 0 ) + parser_->AddKeyword( str, len ); + } } - parser_->Trim(); + // 全行解析し直し ReParse( 0, tln()-1 ); diff --git a/editwing/ip_scroll.cpp b/editwing/ip_scroll.cpp index 7e954555..83312906 100644 --- a/editwing/ip_scroll.cpp +++ b/editwing/ip_scroll.cpp @@ -154,8 +154,8 @@ bool Canvas::CalcLNAreaWidth() const int prev = txtZone_.left; if( showLN_ ) { - txtZone_.left = (1 + figNum_) * font_->F(); - if( txtZone_.left+4*font_->W() >= txtZone_.right ) + txtZone_.left = (1 + figNum_) * font_.F(); + if( txtZone_.left+4*font_.W() >= txtZone_.right ) txtZone_.left = 0; // 行番号ゾーンがデカすぎるときは表示しない } else @@ -174,10 +174,10 @@ void Canvas::CalcWrapWidth() wrapWidth_ = 0xffffffff; break; case RIGHTEDGE: - wrapWidth_ = txtZone_.right - txtZone_.left - font_->W()/2 - 1; + wrapWidth_ = txtZone_.right - txtZone_.left - font_.W()/2 - 1; break; //Caretの分-3補正 default: - wrapWidth_ = wrapType_ * font_->W(); + wrapWidth_ = wrapType_ * font_.W(); break; } } @@ -186,8 +186,7 @@ Canvas::Canvas( const View& vw ) : wrapType_ ( -1 ) , warpSmart_( false ) , showLN_ ( false ) - , font_ ( new Painter( vw.hwnd(), - VConfig(TEXT(""),0) ) ) + , font_ ( vw.hwnd(), VConfig(TEXT(""),0) ) , wrapWidth_( 0xffffffff ) , figNum_ ( 3 ) { @@ -196,7 +195,7 @@ Canvas::Canvas( const View& vw ) Canvas::~Canvas() { - delete font_; + //delete font_; } // Return true if warp width has changed. @@ -218,10 +217,12 @@ bool Canvas::on_view_resize( int cx, int cy ) void Canvas::on_font_change( const VConfig& vc ) { - HWND hwnd = font_->getWHND(); - delete font_; // 先にデストラクタを呼ばねばならない… + //HWND hwnd = font_->getWHND(); + //delete font_; // 先にデストラクタを呼ばねばならない… // ってうわー格好悪ぃーーー(T_T) - font_ = new Painter( hwnd, vc ); + //font_ = new Painter( hwnd, vc ); + font_.Destroy(); + font_.Init(vc); CalcLNAreaWidth(); CalcWrapWidth(); @@ -284,12 +285,12 @@ bool ViewImpl::ReSetScrollInfo() // rlScr_.nMax = Max( textCx_, cx ); // rlScr_.nPos = Min( rlScr_.nPos, rlScr_.nMax-rlScr_.nPage+1 ); rlScr_.nPage = cx + 1; - rlScr_.nMax = Max( textCx_+cvs_.getPainter().W()/2+1, cx ); + rlScr_.nMax = Max( textCx_+cvs_.font_.W()/2+1, cx ); rlScr_.nPos = Min( rlScr_.nPos, (int)(rlScr_.nMax-rlScr_.nPage+1) ); // 縦はnPageとnMaxはとりあえず補正 // nPosは場合によって直し方が異なるので別ルーチンにて - udScr_.nPage = cy / NZero(cvs_.getPainter().H()) + 1; + udScr_.nPage = cy / NZero(cvs_.font_.H()) + 1; //udScr_.nMax = vln() + udScr_.nPage - 2; // Old code (not so nice) // WIP: Adjust so that scroll bar appears only when we are shy oneline from the page. @@ -298,7 +299,7 @@ bool ViewImpl::ReSetScrollInfo() // udScr_.nMax = vln() + udScr_.nPage - Max( 2, Min( udScr_.nPage-1, vln()+1 ) ); // Limit more scrolling when there is more tha a single page - if( vln()*cvs_.getPainter().H() < cy ) + if( vln()*cvs_.font_.H() < cy ) udScr_.nMax = vln() + udScr_.nPage - 2; else udScr_.nMax = vln() + udScr_.nPage - Max( 2, (int)Min( udScr_.nPage-1, (uint)(vln()+1) ) ); @@ -407,7 +408,7 @@ void ViewImpl::ScrollTo( const VPos& vp ) } else { - const int W = cvs_.getPainter().W(); + const int W = cvs_.font_.W(); if( rlScr_.nPos + (signed)(rlScr_.nPage-W) <= vp.vx ) dx = vp.vx - (rlScr_.nPos + rlScr_.nPage) + W; } @@ -426,7 +427,7 @@ void ViewImpl::ScrollTo( const VPos& vp ) void ViewImpl::GetDrawPosInfo( VDrawInfo& v ) const { - const int H = cvs_.getPainter().H(); + const int H = cvs_.font_.H(); long most_under = (vln()-udScr_.nPos)*H; if( most_under <= v.rc.top ) @@ -472,7 +473,7 @@ void ViewImpl::ScrollView( int dx, int dy, bool update ) cur_.on_scroll_begin(); const RECT* clip = (dy==0 ? &cvs_.zone() : NULL); - const int H = cvs_.getPainter().H(); + const int H = cvs_.font_.H(); // スクロールバー更新, Scrollbar updated if( dx != 0 ) @@ -561,8 +562,8 @@ void ViewImpl::on_hscroll( int code, int pos ) switch( code ) { default: return; - case SB_LINELEFT: dx= -cvs_.getPainter().W(); break; - case SB_LINERIGHT: dx= +cvs_.getPainter().W(); break; + case SB_LINELEFT: dx= -cvs_.font_.W(); break; + case SB_LINERIGHT: dx= +cvs_.font_.W(); break; case SB_PAGELEFT: dx= -(cx()>>1); break; case SB_PAGERIGHT: dx= +(cx()>>1); break; case SB_THUMBTRACK: @@ -590,8 +591,8 @@ void ViewImpl::on_vscroll( int code, int pos ) default: return; case SB_LINEUP: dy= -1; break; case SB_LINEDOWN: dy= +1; break; - case SB_PAGEUP: dy= -( cy() / NZero(cvs_.getPainter().H()) ); break; - case SB_PAGEDOWN: dy= +( cy() / NZero(cvs_.getPainter().H()) ); break; + case SB_PAGEUP: dy= -( cy() / NZero(cvs_.font_.H()) ); break; + case SB_PAGEDOWN: dy= +( cy() / NZero(cvs_.font_.H()) ); break; case SB_THUMBTRACK: { SCROLLINFO si = { sizeof(SCROLLINFO), SIF_TRACKPOS }; @@ -621,7 +622,7 @@ int ViewImpl::getNumScrollLines( void ) // This automatically takes into account the page scroll mode where // SPI_GETWHEELSCROLLLINES value if 0xFFFFFFFF. uint nlpage; - nlpage = Max( (uint)cy() / (uint)NZero(cvs_.getPainter().H()), 1U ); + nlpage = Max( (uint)cy() / (uint)NZero(cvs_.font_.H()), 1U ); // scrolllines can be zero, in this case no scroll should occur. return (int)Min( scrolllines, nlpage ); } @@ -667,7 +668,7 @@ int ViewImpl::getNumScrollRaws( void ) } // Avoid scrolling more that halfa screen horizontally. uint nlpage; - nlpage = Max( (uint)cx() / (uint)NZero(cvs_.getPainter().W()/2), 1U ); + nlpage = Max( (uint)cx() / (uint)NZero(cvs_.font_.W()/2), 1U ); // scrollnum can be zero, in this case no scroll should occur. return (int)Min( scrollnum, nlpage ); } @@ -675,12 +676,12 @@ int ViewImpl::getNumScrollRaws( void ) void ViewImpl::on_hwheel( short delta ) { int nl = getNumScrollRaws(); - int dx = nl * cvs_.getPainter().W() * (int)delta / WHEEL_DELTA; + int dx = nl * cvs_.font_.W() * (int)delta / WHEEL_DELTA; if( dx == 0 ) { // step is too small, we need to accumulate delta accdeltax_ += delta; // Recalculate the step. - dx = nl * cvs_.getPainter().W() * (int)accdeltax_ / WHEEL_DELTA; + dx = nl * cvs_.font_.W() * (int)accdeltax_ / WHEEL_DELTA; if( dx ) { ScrollView( dx, 0, 0 ); @@ -743,7 +744,7 @@ void ViewImpl::UpDown( int dy, bool thumb ) void ViewImpl::InvalidateView( const DPos& dp, bool afterall ) const { - const int H = cvs_.getPainter().H(); + const int H = cvs_.font_.H(); // 表示域より上での更新, Update above the display area if( dp.tl < udScr_tl_ ) diff --git a/editwing/ip_view.h b/editwing/ip_view.h index 902b7c15..97db854e 100644 --- a/editwing/ip_view.h +++ b/editwing/ip_view.h @@ -13,7 +13,7 @@ namespace view { #define CW_INTTYPE int #endif - +class View; //========================================================================= //@{ @pkg editwing.View.Impl //@} @@ -26,12 +26,12 @@ namespace view { // あるはずもなく極めて適当に…。 //@} //========================================================================= - -class Painter : public ki::Object +class Document; +class Painter { public: - ~Painter(); + ~Painter() { Destroy(); } //@{ 指定位置に一文字出力, Output a single character at the specified position //@} void CharOut( unicode ch, int x, int y ); @@ -193,10 +193,10 @@ class Painter : public ki::Object const HWND hwnd_;// Window in which we paint HDC dc_; // Device context used for Painting (non const) - const HDC cdc_; // Compatible DC used for W() (const) - const HFONT font_; + HDC cdc_; // Compatible DC used for W() (const) + HFONT font_; HPEN pen_; - const HBRUSH brush_; + HBRUSH brush_; HFONT oldfont_; // Old objects to be released before HPEN oldpen_; // the EndPaint() call. HBRUSH oldbrush_; // @@ -214,6 +214,8 @@ class Painter : public ki::Object private: Painter( HWND hwnd, const VConfig& vc ); + void Init( const VConfig& vc ); + void Destroy(); HFONT init_font( const VConfig& vc ); HWND getWHND() { return hwnd_; } friend class Canvas; @@ -232,7 +234,7 @@ class Painter : public ki::Object //@} //========================================================================= -class Canvas : public ki::Object +class Canvas { public: @@ -270,16 +272,15 @@ class Canvas : public ki::Object //@{ 表示領域の位置(pixel) //@} const RECT& zone() const { return txtZone_; } - //@{ 描画用オブジェクト //@} - Painter& getPainter() const { return *font_; } - private: short wrapType_; // [ -1:折り返し無し 0:窓右端 else:指定文字数 ] bool warpSmart_; // [ Enable wrapping at word boundaries ] bool showLN_; // [ 行番号を表示するか否か ] - Painter *font_; // 描画用オブジェクト +public: + Painter font_; // 描画用オブジェクト +private: ulong wrapWidth_; // 折り返し幅(pixel) RECT txtZone_; // テキスト表示域の位置(pixel) int figNum_; // 行番号の桁数 @@ -378,28 +379,40 @@ struct VDrawInfo //@} //========================================================================= -class ViewImpl : public ki::Object +class ViewImpl { public: ViewImpl( View& vw, doc::Document &dc ); //@{ 折り返し方式切替 //@} - void SetWrapType( int wt ); + inline void SetWrapType( int wt ) + { cvs_.on_config_change( wt, cvs_.showLN(), cvs_.wrapSmart() ); + DoConfigChange(); } + + inline void SetWrapSmart( bool ws ) + { cvs_.on_config_change( cvs_.wrapType(), cvs_.showLN(), ws ); + DoConfigChange(); } - void SetWrapSmart( bool ws ); //@{ 行番号表示/非表示切替 //@} - void ShowLineNo( bool show ); + inline void ShowLineNo( bool show ) + { cvs_.on_config_change( cvs_.wrapType(), show, cvs_.wrapSmart() ); DoConfigChange(); } //@{ 表示色・フォント切替 //@} - void SetFont( const VConfig& vc ); + inline void SetFont( const VConfig& vc ) + { cvs_.on_font_change( vc ); + cur_.on_setfocus(); + CalcEveryLineWidth(); // 行幅再計算 + DoConfigChange(); } //@{ All of the above in one go //@} - void SetWrapLNandFont( int wt, bool ws, bool showLN, const VConfig& vc ); + inline void SetWrapLNandFont( int wt, bool ws, bool showLN, const VConfig& vc ) + { cvs_.on_config_change_nocalc( wt, showLN, ws ); + SetFont( vc ); } - //@{ テキスト領域のサイズ変更イベント //@} - void on_view_resize( int cx, int cy ); + //@{ テキスト領域のサイズ変更イベント //@} + inline void on_view_resize( int cx, int cy ) { DoResize( cvs_.on_view_resize( cx, cy ) ); } void DoResize( bool wrapWidthChanged ); void DoConfigChange(); @@ -429,7 +442,7 @@ class ViewImpl : public ki::Object Cursor& cur() { return cur_; } //@{ フォント //@} - const Painter& fnt() const { return cvs_.getPainter(); } + const Painter& fnt() const { return cvs_.font_; } void on_hscroll( int code, int pos ); @@ -438,7 +451,8 @@ class ViewImpl : public ki::Object void on_hwheel( short delta ); void GetVPos( int x, int y, VPos* vp, bool linemode=false ) const; - void GetOrigin( int* x, int* y ) const; + inline void GetOrigin( int* x, int* y ) const + { *x = left()-rlScr_.nPos, *y = -udScr_.nPos*cvs_.font_.H(); } void ConvDPosToVPos( DPos dp, VPos* vp, const VPos* base=NULL ) const; void ScrollTo( const VPos& vp ); int GetLastWidth( ulong tl ) const; @@ -447,14 +461,14 @@ class ViewImpl : public ki::Object public: - const RECT& zone() const { return cvs_.zone(); } - int left() const { return cvs_.zone().left; } - int right() const { return cvs_.zone().right; } - int bottom()const { return cvs_.zone().bottom; } - int lna() const { return cvs_.zone().left; } - int cx() const { return cvs_.zone().right - cvs_.zone().left; } - int cxAll() const { return cvs_.zone().right; } - int cy() const { return cvs_.zone().bottom; } + inline const RECT& zone() const { return cvs_.zone(); } + inline int left() const { return cvs_.zone().left; } + inline int right() const { return cvs_.zone().right; } + inline int bottom()const { return cvs_.zone().bottom; } + inline int lna() const { return cvs_.zone().left; } + inline int cx() const { return cvs_.zone().right - cvs_.zone().left; } + inline int cxAll() const { return cvs_.zone().right; } + inline int cy() const { return cvs_.zone().bottom; } private: @@ -508,42 +522,58 @@ class ViewImpl : public ki::Object -//------------------------------------------------------------------------- +//========================================================================= + +//========================================================================= +//@{ @pkg editwing.View //@} +//@{ +// 描画処理など +// +// このクラスでは、メッセージの分配を行うだけで、実装は +// Canvas/ViewImpl 等で行う。ので、詳しくはそちらを参照のこと。 +//@} +//========================================================================= + +class View A_FINAL: public ki::WndImpl, public doc::DocEvHandler +{ +public: + + //@{ 何もしないコンストラクタ //@} + View( doc::Document& d, HWND wnd ); + ~View(); + + //@{ 折り返し方式切替 //@} + void SetWrapType( int wt ); -inline void ViewImpl::on_view_resize( int cx, int cy ) - { DoResize( cvs_.on_view_resize( cx, cy ) ); } + void SetWrapSmart( bool ws); -inline void ViewImpl::SetWrapType( int wt ) - { cvs_.on_config_change( wt, cvs_.showLN(), cvs_.wrapSmart() ); - DoConfigChange(); } + //@{ 行番号表示/非表示切替 //@} + void ShowLineNo( bool show ); -inline void ViewImpl::SetWrapSmart( bool ws ) - { cvs_.on_config_change( cvs_.wrapType(), cvs_.showLN(), ws ); - DoConfigChange(); } + //@{ 表示色・フォント切替 //@} + void SetFont( const VConfig& vc ); -inline void ViewImpl::ShowLineNo( bool show ) - { cvs_.on_config_change( cvs_.wrapType(), show, cvs_.wrapSmart() ); - DoConfigChange(); } + //@{ Set all canva stuff at once (faster) //@} + void SetWrapLNandFont( int wt, bool ws, bool showLN, const VConfig& vc ); -inline void ViewImpl::SetFont( const VConfig& vc ) - { cvs_.on_font_change( vc ); - cur_.on_setfocus(); - CalcEveryLineWidth(); // 行幅再計算 - DoConfigChange(); } + //@{ カーソル //@} + Cursor& cur(); -inline void ViewImpl::SetWrapLNandFont( int wt, bool ws, bool showLN, const VConfig& vc ) - { cvs_.on_config_change_nocalc( wt, showLN, ws ); - cvs_.on_font_change( vc ); - cur_.on_setfocus(); - CalcEveryLineWidth(); // 行幅再計算 - DoConfigChange(); } +private: -inline void ViewImpl::GetOrigin( int* x, int* y ) const - { *x = left()-rlScr_.nPos, *y = -udScr_.nPos*cvs_.getPainter().H(); } + doc::Document& doc_; + ViewImpl* impl_; + static ClsName className_; +private: + void on_create( CREATESTRUCT* cs ) override; + void on_destroy() override; + LRESULT on_message( UINT msg, WPARAM wp, LPARAM lp ) override; + void on_text_update( const DPos& s, const DPos& e, const DPos& e2, bool bAft, bool mCur ) override; + void on_keyword_change() override; +}; -//========================================================================= }} // namespace editwing::view #endif // _EDITWING_IP_VIEW_H_ diff --git a/editwing/ip_wrap.cpp b/editwing/ip_wrap.cpp index d4dfadb1..37e757ff 100644 --- a/editwing/ip_wrap.cpp +++ b/editwing/ip_wrap.cpp @@ -48,11 +48,9 @@ ViewImpl::ViewImpl( View& vw, doc::Document& dc ) udScr_.nPos = rlScr_.nPos = 0; udScr_.nTrackPos = rlScr_.nTrackPos = 0; udScr_tl_ = udScr_vrl_ = 0; - ReSetScrollInfo(); + //ReSetScrollInfo(); } - - //------------------------------------------------------------------------- // 状態変更への対応 //------------------------------------------------------------------------- @@ -175,7 +173,7 @@ ulong A_HOT ViewImpl::CalcLineWidth( const unicode* txt, ulong len ) const // Calculate the width of the text when lines are written without wrapping // For text where most of the lines are displayed without wrapping. // Calculating this value can speed up the process. - const Painter& p = cvs_.getPainter(); + const Painter& p = cvs_.font_; ulong w=0; for( ulong i=0; i -class A_WUNUSED gapbuf : public Object +class A_WUNUSED gapbuf { public: @@ -62,7 +62,7 @@ class A_WUNUSED gapbuf : public Object return false; MakeGapAt( i ); - buf_[gs_++] = x; + buf_[gs_++] = x; return true; } @@ -176,21 +176,21 @@ class A_WUNUSED gapbuf : public Object } gs_ = i; } - bool Reallocate( ulong newalen ) - { - T *tmp = (T *)malloc( sizeof(T) * newalen ), *old=buf_; - if( !tmp ) return false; - const ulong tail = alen_-ge_; - - memmove( (char*)tmp, (char*)old, gs_*sizeof(T) ); - memmove( (char*)(tmp+newalen-tail), (char*)(old+ge_), tail*sizeof(T) ); - free( old ); - - buf_ = tmp; - ge_ = newalen-tail; - alen_ = newalen; - return true; - } + bool Reallocate( ulong newalen ) + { + T *tmp = (T *)malloc( sizeof(T) * newalen ), *old=buf_; + if( !tmp ) return false; + const ulong tail = alen_-ge_; + + memmove( (char*)tmp, (char*)old, gs_*sizeof(T) ); + memmove( (char*)(tmp+newalen-tail), (char*)(old+ge_), tail*sizeof(T) ); + free( old ); + + buf_ = tmp; + ge_ = newalen-tail; + alen_ = newalen; + return true; + } /* bool Reallocate( ulong newalen ) { diff --git a/kilib/memory.h b/kilib/memory.h index 13a92e99..e97447bc 100644 --- a/kilib/memory.h +++ b/kilib/memory.h @@ -171,28 +171,26 @@ class Object //========================================================================= // Stupid arena allocator -//struct Arena -//{ -// Arena(BYTE *buf=NULL, size_t capacity=0) -// : sta ( buf ) -// , end ( buf + capacity ) -// { } -// -// void SetBuf(BYTE *buf, size_t capacity) -// { sta = buf; end = buf+capacity; } -// -// void *alloc(size_t sz) -// { -// size_t pad = (UINT_PTR)end & 7; -// if( sz+pad > size_t(end - sta) ) -// return NULL; // OOM -// -// return reinterpret_cast(end -= sz + pad); -// } -// -// BYTE *sta; -// BYTE *end; -//}; +struct Arena +{ + explicit Arena(BYTE *buf, size_t capacity) + : sta ( buf ) + , end ( buf + capacity ) + { } + + void *alloc(size_t sz) + { + size_t pad = (UINT_PTR)end & (sizeof(void*)-1); + if( sz+pad > size_t(end - sta) ) + return NULL; // OOM + + return reinterpret_cast(end -= sz + pad); + } + + BYTE *sta; + BYTE *end; +}; + #pragma warning(disable: 4146) struct DArena { @@ -208,21 +206,27 @@ struct DArena dim = 0; } - // Fixed mode - DArena( BYTE *buf, size_t count) - : sta ( buf ) - , end ( sta ) - , dim ( count ) - , res ( 0 ) - { } + DArena() {}; + + void Init( size_t reserved ) + { + dim = pagesize(); + res = reserved; + + sta = (BYTE*)::VirtualAlloc(NULL, reserved, MEM_RESERVE, PAGE_READWRITE); + end = sta = (BYTE*)::VirtualAlloc(sta, dim, MEM_COMMIT, PAGE_READWRITE); + if( !sta ) // Alloc failed + dim = 0; + } void *alloc(size_t sz) { - size_t pad = -(UINT_PTR)end & 7; + size_t pad = -(UINT_PTR)end & (sizeof(void*)-1); if( sz+pad > dim - (end - sta) ) { - if( !res || !sta ) return NULL; + if( !sta ) return NULL; size_t ps = pagesize(); + ps = ps * (1 + (sz / ps)); if( !::VirtualAlloc(sta, dim + ps, MEM_COMMIT, PAGE_READWRITE) ) return NULL; // Unable to map!!!! dim += ps; @@ -232,21 +236,17 @@ struct DArena return reinterpret_cast(ret); } - void trim() + inline void freelast(void *ptr, size_t sz) { - res = dim; - ::VirtualFree(sta+dim, 0, MEM_RELEASE); + end -= sz; } - ~DArena() + void FreeAll() { - if( res ) - { - ::VirtualFree(sta, dim, MEM_DECOMMIT); - ::VirtualFree(sta, 0, MEM_RELEASE); - } + ::VirtualFree(sta, dim, MEM_DECOMMIT); + ::VirtualFree(sta, 0, MEM_RELEASE); } - + size_t pagesize() { SYSTEM_INFO si; si.dwPageSize = 4096; @@ -254,11 +254,47 @@ struct DArena return si.dwPageSize; } + void reset() + { + #ifdef _DEBUG + mem00(sta, dim); + #endif + size_t ps = pagesize(); + if (dim <= ps) + return; + ::VirtualFree(sta+ps, Max(dim, res)-ps, MEM_DECOMMIT); + dim = ps; + end = sta; + } + BYTE *sta; BYTE *end; size_t dim; size_t res; }; +extern DArena TS; +struct TmpObject +{ + static void* operator new( size_t sz ) noexcept + { + void *ptr = TS.alloc( sz );; +// TCHAR buf[128]; +// ::wsprintf(buf, TEXT("%x, %u"), ptr, sz); +// MessageBox(NULL, buf, TEXT("ALLOC"), 0); + + return ptr; + } + + static void operator delete( void* ptr, size_t sz ) noexcept + { +// TCHAR buf[128]; +// ::wsprintf(buf, TEXT("%x, %u"), ptr, sz); +// MessageBox(NULL, buf, TEXT("FREE"), 0); +// + TS.freelast( ptr, sz ); + } +}; + } // namespace ki #endif // _KILIB_MEMORY_H_ diff --git a/kilib/registry.h b/kilib/registry.h index b12742ff..e5061aeb 100644 --- a/kilib/registry.h +++ b/kilib/registry.h @@ -17,7 +17,7 @@ namespace ki { //@} //========================================================================= -class A_WUNUSED IniFile : public Object +class A_WUNUSED IniFile { public: diff --git a/kilib/string.cpp b/kilib/string.cpp index aeb47151..c17be3f0 100644 --- a/kilib/string.cpp +++ b/kilib/string.cpp @@ -531,19 +531,22 @@ bool String::isCompatibleWithACP(const TCHAR *uni, size_t len) // Some losses are thus invisible to the err flag! bool compatible = true; - char *mbbuf = (char *)malloc( sizeof(char) * mblen ); + byte *oend = TS.end; + char *mbbuf = (char *)TS.alloc( sizeof(char) * mblen ); if( !mbbuf ) return false; mblen = ::WideCharToMultiByte( CP_ACP, 0, uni, len, mbbuf, mblen, NULL, NULL ); if( !mblen ) return false; - wchar_t *backconv = (wchar_t *)malloc( sizeof(wchar_t) * len ); + wchar_t *backconv = (wchar_t *)TS.alloc( sizeof(wchar_t) * len ); if( !backconv ) return false; size_t backconvlen = ::MultiByteToWideChar(CP_ACP, 0, mbbuf, mblen, backconv, len); - free( mbbuf ); + //free( mbbuf ); if( backconvlen != len || my_lstrncmpW( uni, backconv, len ) ) compatible = false; // Not matching - free( backconv ); + //free( backconv ); + TS.end = oend; // resstore TS! + return compatible; #else @@ -555,12 +558,12 @@ bool String::isCompatibleWithACP(const TCHAR *uni, size_t len) String& String::operator+=( const char* s ) { int ln = ::MultiByteToWideChar( CP_ACP, 0, s, -1 , 0, 0 ); - wchar_t* p = (wchar_t *)malloc( sizeof(wchar_t) * (ln+1) ); + wchar_t* p = (wchar_t *)TS.alloc( sizeof(wchar_t) * (ln+1) ); if( p ) { ::MultiByteToWideChar( CP_ACP, 0, s, -1 , p, ln+1 ); CatString(p, ln); - free( p ); + TS.freelast( p, sizeof(wchar_t) * (ln+1) ); } return *this; } @@ -568,12 +571,12 @@ String& String::operator+=( const char* s ) String& String::operator+=( const wchar_t* s ) { int ln = ::WideCharToMultiByte( CP_ACP, 0, s, -1 , 0, 0, NULL, NULL ); - char* p = (char *)malloc( sizeof(char) * (ln+1) ); + char* p = (char *)TS.alloc( sizeof(char) * (ln+1) ); if( p ) { ::WideCharToMultiByte( CP_ACP, 0, s, -1 , p, ln+1, NULL, NULL ); CatString(p, ln); - free( p ); + TS.freelast( p, sizeof(char) * (ln+1) ); } return *this; } diff --git a/kilib/textfile.cpp b/kilib/textfile.cpp index 04380b39..46fc3959 100644 --- a/kilib/textfile.cpp +++ b/kilib/textfile.cpp @@ -11,7 +11,7 @@ using namespace ki; // テキストファイル読み出し共通インターフェイス //========================================================================= -struct ki::TextFileRPimpl: public Object +struct ki::TextFileRPimpl : public TmpObject { // inline TextFileRPimpl() {} @@ -2441,7 +2441,7 @@ bool TextFileR::CheckUTFConfidence(const uchar* ptr, ulong siz, unsigned int uCh //========================================================================= // テキストファイル出力共通インターフェイス //========================================================================= -struct ki::TextFileWPimpl : public Object +struct ki::TextFileWPimpl : public TmpObject { virtual void WriteLine( const unicode* buf, ulong siz ) { while( siz-- ) WriteChar( *buf++ ); } diff --git a/kilib/textfile.h b/kilib/textfile.h index b1d36a34..960f3ee9 100644 --- a/kilib/textfile.h +++ b/kilib/textfile.h @@ -161,7 +161,7 @@ struct TextFileWPimpl; //@} //========================================================================= -class TextFileR : public Object +class TextFileR : public TmpObject { public: @@ -256,7 +256,7 @@ inline bool TextFileR::nolb_found() const //@} //========================================================================= -class TextFileW : public Object +class TextFileW : public TmpObject { public: diff --git a/kilib/window.cpp b/kilib/window.cpp index ed35d5f7..3048cdfc 100644 --- a/kilib/window.cpp +++ b/kilib/window.cpp @@ -58,7 +58,7 @@ static HINSTANCE LoadIMM32DLL() HINSTANCE h = LoadLibrary(TEXT("IMM32.DLL")); if( !h ) return NULL; - // Helper with crazy cast to avoid further casts. + // Helper with crazy cast to avoid further casTS-> #define LOADPROC(proc, procname) if(!(*reinterpret_cast(&proc)=GetProcAddress(h,procname)))goto fail; LOADPROC( dyn_ImmIsIME, "ImmIsIME" ); @@ -432,7 +432,7 @@ void IMEManager::GetString( HWND wnd, unicode** str, ulong* len ) long s = ::ImmGetCompositionStringA(ime,GCS_RESULTSTR,NULL,0); if( s > 0 ) { - char* tmp = (char *)malloc( sizeof(char) * s ); + char* tmp = (char *)TS.alloc( sizeof(char) * s ); if( tmp ) { *str = (unicode *)malloc( sizeof(unicode) * (*len=s*2) ); @@ -442,7 +442,7 @@ void IMEManager::GetString( HWND wnd, unicode** str, ulong* len ) *len = ::MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, tmp, s, *str, *len ); } - free( tmp ); + TS.freelast( tmp, sizeof(char) * s ); } } } @@ -488,12 +488,12 @@ void IMEManager::SetString( HWND wnd, unicode* str, ulong len ) if( !app().isNT() ) { // Use ANSI functions on Win9x len = ::WideCharToMultiByte( CP_ACP,MB_PRECOMPOSED,str,-1, NULL,0 ,NULL,NULL ); - char* tmp = (char *)malloc( sizeof(char) * len ); + char* tmp = (char *)TS.alloc( sizeof(char) * len ); if( tmp ) { ::WideCharToMultiByte( CP_ACP,MB_PRECOMPOSED,str,-1,tmp,len,NULL,NULL ); ::ImmSetCompositionStringA(ime,SCS_SETSTR,tmp,len,NULL,0); - free( tmp ); + TS.freelast( tmp, sizeof(char) * len ); } } else @@ -533,11 +533,16 @@ void Window::MsgLoop() isLooping_ = true; ime().MsgLoopBegin(); for( MSG msg; ::GetMessage( &msg, NULL, 0, 0 ); ) + { + if( TS.end != TS.sta ) + LOGGERF( TEXT("leak %d bytes!"), TS.end - TS.sta ); + TS.reset(); if( !PreTranslateMessage( &msg ) ) { ime().TranslateMsg( &msg ); ::DispatchMessage( &msg ); } + } ime().MsgLoopEnd(); isLooping_ = false; } diff --git a/kilib/window.h b/kilib/window.h index 481f9a53..b7ed37b2 100644 --- a/kilib/window.h +++ b/kilib/window.h @@ -24,7 +24,7 @@ namespace ki { //@} //========================================================================= -class A_NOVTABLE Window : public Object +class A_NOVTABLE Window { public: diff --git a/kilib/winutil.cpp b/kilib/winutil.cpp index 72563ec7..90d4a701 100644 --- a/kilib/winutil.cpp +++ b/kilib/winutil.cpp @@ -115,7 +115,7 @@ Clipboard::Text Clipboard::GetUnicodeText() const { UINT nf = myDragQueryFile(h, 0xFFFFFFFF, NULL, 0); size_t totstrlen=0; - UINT *lenmap = (UINT *)malloc( sizeof(UINT) * nf ); + UINT *lenmap = (UINT *)TS.alloc( sizeof(UINT) * nf ); if (!lenmap) return Text( NULL, Text::MALLOC ); for( uint i=0; i < nf; i++ ) { // On Windows NT3.1 DragQueryFile() does not return @@ -146,7 +146,7 @@ Clipboard::Text Clipboard::GetUnicodeText() const } *ptr = L'\0'; GlobalUnlock( hg ); - free( lenmap ); + TS.freelast( lenmap, sizeof(UINT) * nf ); return Text( ustr, Text::MALLOC ); } } @@ -229,7 +229,7 @@ HRESULT STDMETHODCALLTYPE IDataObjectTxt::GetDataHere(FORMATETC *fmt, STGMEDIUM char *dest = (char *)( ((BYTE*)df) + df->pFiles ); // Convert multi line in multi file paths - unicode *flst = (unicode *)malloc( sizeof(unicode) * len_ ); + unicode *flst = (unicode *)TS.alloc( sizeof(unicode) * len_ ); if( !flst ) return E_OUTOFMEMORY; size_t flen = convCRLFtoNULLS(flst, str_, len_); @@ -243,7 +243,7 @@ HRESULT STDMETHODCALLTYPE IDataObjectTxt::GetDataHere(FORMATETC *fmt, STGMEDIUM { // Directly copy unicode data memmove( dest, flst, len ); } - free( flst ); + TS.freelast( flst, sizeof(unicode) * len_ ); remaining_bytes = gmemsz - len - df->pFiles ; } // Clear remaining bytes From c87470c921f77515050542bfc6d00617513eab41 Mon Sep 17 00:00:00 2001 From: RamonUnch <74856804+RamonUnch@users.noreply.github.com> Date: Fri, 6 Dec 2024 20:54:11 +0100 Subject: [PATCH 2/3] fix typo --- GpMain.cpp | 2 +- kilib/window.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/GpMain.cpp b/GpMain.cpp index 962a0931..2253a559 100644 --- a/GpMain.cpp +++ b/GpMain.cpp @@ -681,7 +681,7 @@ void GreenPadWnd::on_pagesetup() PAGESETUPDLG psd; mem00(&psd, sizeof(psd)); psd.lStructSize = sizeof(psd); - // FIXME: use local uniTS->.. + // FIXME: use local units. psd.Flags = PSD_INTHOUSANDTHSOFINCHES|PSD_DISABLEORIENTATION|PSD_DISABLEPAPER|PSD_DISABLEPRINTER|PSD_MARGINS; psd.hwndOwner = hwnd(); CopyRect(&psd.rtMargin, cfg_.PMargins()); diff --git a/kilib/window.cpp b/kilib/window.cpp index 3048cdfc..434ab59c 100644 --- a/kilib/window.cpp +++ b/kilib/window.cpp @@ -58,7 +58,7 @@ static HINSTANCE LoadIMM32DLL() HINSTANCE h = LoadLibrary(TEXT("IMM32.DLL")); if( !h ) return NULL; - // Helper with crazy cast to avoid further casTS-> + // Helper with crazy cast to avoid further casts. #define LOADPROC(proc, procname) if(!(*reinterpret_cast(&proc)=GetProcAddress(h,procname)))goto fail; LOADPROC( dyn_ImmIsIME, "ImmIsIME" ); From 87e752b177699e37d7eea525373fca342d842edc Mon Sep 17 00:00:00 2001 From: RamonUnch <74856804+RamonUnch@users.noreply.github.com> Date: Sat, 7 Dec 2024 09:42:54 +0100 Subject: [PATCH 3/3] Minor ajustments for size --- editwing/ip_parse.cpp | 8 ++++---- editwing/ip_view.h | 2 +- kilib/memory.h | 30 ++++++++++++++++++------------ kilib/textfile.cpp | 4 ++-- 4 files changed, 25 insertions(+), 19 deletions(-) diff --git a/editwing/ip_parse.cpp b/editwing/ip_parse.cpp index bd534f96..72846b87 100644 --- a/editwing/ip_parse.cpp +++ b/editwing/ip_parse.cpp @@ -199,7 +199,7 @@ struct Keyword x->str[l] = L'\0'; } //else MessageBox(NULL, s, NULL, 0); - //LOGGERF( TEXT("sizeof(Keyword) = %d, allocated = %d / %d"), sizeof(Keyword), (UINT)(ar->end - ar->sta), ar->dim); + //LOGGERF( TEXT("sizeof(Keyword) = %d, remaining = %d bytes"), sizeof(Keyword), (UINT)(ar->end - ar->sta)); return x; } }; @@ -790,11 +790,11 @@ void Document::SetKeyword( const unicode* defbuf, ulong siz ) if( prs ) parser_.reset( prs ); - if( siz > 0 ) + if( siz > 0 && !r.isEmpty() ) { // calculate the necessary memory for the keyword list. - size_t n = siz, totsz = 0; - const unicode *b = defbuf; + const unicode *b = str; + size_t n = siz - (str - defbuf), totsz = 0; while( n-- ) { enum { align = sizeof(void*) -1 }; diff --git a/editwing/ip_view.h b/editwing/ip_view.h index 97db854e..4d137260 100644 --- a/editwing/ip_view.h +++ b/editwing/ip_view.h @@ -317,7 +317,7 @@ struct WLine: public ki::storage } static ulong *dummy_buf() { - static ulong dummy_buf[2]; + static ulong dummy_buf[2] = {0,0}; return dummy_buf; } diff --git a/kilib/memory.h b/kilib/memory.h index e97447bc..08dfcfaa 100644 --- a/kilib/memory.h +++ b/kilib/memory.h @@ -195,18 +195,18 @@ struct Arena struct DArena { // Dynamic mode - DArena( size_t reserved ) - : sta ( (BYTE*)::VirtualAlloc(NULL, reserved, MEM_RESERVE, PAGE_READWRITE) ) - //, end ( sta ) - , dim ( pagesize() ) - , res ( reserved ) - { - end = sta = (BYTE*)::VirtualAlloc(sta, dim, MEM_COMMIT, PAGE_READWRITE); - if( !sta ) // Alloc failed - dim = 0; - } - - DArena() {}; +// DArena( size_t reserved ) +// : sta ( (BYTE*)::VirtualAlloc(NULL, reserved, MEM_RESERVE, PAGE_READWRITE) ) +// //, end ( sta ) +// , dim ( pagesize() ) +// , res ( reserved ) +// { +// end = sta = (BYTE*)::VirtualAlloc(sta, dim, MEM_COMMIT, PAGE_READWRITE); +// if( !sta ) // Alloc failed +// dim = 0; +// } + + //DArena() {}; void Init( size_t reserved ) { @@ -254,6 +254,12 @@ struct DArena return si.dwPageSize; } +// void trim() +// { +// res = dim; +// ::VirtualFree(sta+dim, 0, MEM_RELEASE); +// } + void reset() { #ifdef _DEBUG diff --git a/kilib/textfile.cpp b/kilib/textfile.cpp index 46fc3959..85dd660f 100644 --- a/kilib/textfile.cpp +++ b/kilib/textfile.cpp @@ -2471,8 +2471,8 @@ struct TextFileWPimplWithBuf: public ki::TextFileWPimpl : ki::TextFileWPimpl( w ) {} - ~TextFileWPimplWithBuf( ) - {}; +// ~TextFileWPimplWithBuf( ) +// {}; // This function should assume it will have enough room in buf_ virtual void WriteBuf( const unicode* str, ulong len ) = 0;