xref: /core/basctl/source/basicide/baside2.hxx (revision 81ced402)
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #ifndef INCLUDED_BASCTL_SOURCE_BASICIDE_BASIDE2_HXX
21 #define INCLUDED_BASCTL_SOURCE_BASICIDE_BASIDE2_HXX
22 
23 #include <memory>
24 #include <layout.hxx>
25 #include "bastype3.hxx"
26 #include <basidesh.hxx>
27 #include "breakpoint.hxx"
28 #include "linenumberwindow.hxx"
29 
30 #include <svtools/svtabbx.hxx>
31 #include <svtools/headbar.hxx>
32 
33 #include <vcl/button.hxx>
34 #include <basic/sbmod.hxx>
35 #include <vcl/lstbox.hxx>
36 #include <vcl/idle.hxx>
37 
38 #include <sfx2/progress.hxx>
39 #include <o3tl/enumarray.hxx>
40 
41 #include <set>
42 
43 #include <vcl/textdata.hxx>
44 #include <basic/codecompletecache.hxx>
45 #include <com/sun/star/reflection/XIdlClass.hpp>
46 #include <comphelper/namedvaluecollection.hxx>
47 #include <comphelper/processfactory.hxx>
48 #include <comphelper/syntaxhighlight.hxx>
49 #include <com/sun/star/reflection/XIdlReflection.hpp>
50 
51 class ExtTextEngine;
52 class TextView;
53 class SvxSearchItem;
54 namespace com { namespace sun { namespace star { namespace beans {
55     class XMultiPropertySet;
56 } } } }
57 
58 namespace basctl
59 {
60 
61 class ObjectCatalog;
62 class CodeCompleteListBox;
63 class CodeCompleteWindow;
64 
65 // #108672 Helper functions to get/set text in TextEngine
66 // using the stream interface (get/setText() only supports
67 // tools Strings limited to 64K).
68 // defined in baside2b.cxx
69 OUString getTextEngineText (ExtTextEngine&);
70 void setTextEngineText (ExtTextEngine&, OUString const&);
71 
72 class EditorWindow : public vcl::Window, public SfxListener
73 {
74 friend class CodeCompleteListBox;
75 private:
76     class ChangesListener;
77 
78     std::unique_ptr<TextView>        pEditView;
79     std::unique_ptr<ExtTextEngine>   pEditEngine;
80     ModulWindow&                     rModulWindow;
81 
82     rtl::Reference< ChangesListener > listener_;
83     osl::Mutex                        mutex_;
84     css::uno::Reference< css::beans::XMultiPropertySet >
85                                       notifier_;
86 
87     long            nCurTextWidth;
88 
89     SyntaxHighlighter   aHighlighter;
90     Idle                aSyntaxIdle;
91     std::set<sal_uInt16>       aSyntaxLineTable;
92     DECL_LINK(SyntaxTimerHdl, Timer *, void);
93 
94     // progress bar
95     class ProgressInfo;
96     std::unique_ptr<ProgressInfo> pProgress;
97 
98     virtual void    Notify( SfxBroadcaster& rBC, const SfxHint& rHint ) override;
99 
100     void            ImpDoHighlight( sal_uLong nLineOff );
101     void            ImplSetFont();
102 
103     bool            bHighlighting;
104     bool            bDoSyntaxHighlight;
105     bool            bDelayHighlight;
106 
107     virtual css::uno::Reference< css::awt::XWindowPeer > GetComponentInterface(bool bCreate = true) override;
108     CodeCompleteDataCache aCodeCompleteCache;
109     VclPtr<CodeCompleteWindow> pCodeCompleteWnd;
110     OUString GetActualSubName( sal_uLong nLine ); // gets the actual subroutine name according to line number
111     void SetupAndShowCodeCompleteWnd(const std::vector< OUString >& aEntryVect, TextSelection aSel );
112     void HandleAutoCorrect();
113     void HandleAutoCloseParen();
114     void HandleAutoCloseDoubleQuotes();
115     void HandleCodeCompletion();
116     void HandleProcedureCompletion();
117     TextSelection GetLastHighlightPortionTextSelection();
118 
119 protected:
120     virtual void    Paint( vcl::RenderContext& rRenderContext, const tools::Rectangle& ) override;
121     virtual void    Resize() override;
122     virtual void    KeyInput( const KeyEvent& rKeyEvt ) override;
123     virtual void    MouseMove( const MouseEvent& rMEvt ) override;
124     virtual void    MouseButtonDown( const MouseEvent& rMEvt ) override;
125     virtual void    MouseButtonUp( const MouseEvent& rMEvt ) override;
126     virtual void    Command( const CommandEvent& rCEvt ) override;
127     virtual void    LoseFocus() override;
128     virtual void    RequestHelp( const HelpEvent& rHEvt ) override;
129 
130     void            DoSyntaxHighlight( sal_uLong nPara );
131     OUString        GetWordAtCursor();
132     bool            ImpCanModify();
133 
134 public:
135                     EditorWindow (vcl::Window* pParent, ModulWindow*);
136                     virtual ~EditorWindow() override;
137     virtual void    dispose() override;
138 
139     ExtTextEngine*  GetEditEngine() const   { return pEditEngine.get(); }
140     TextView*       GetEditView() const     { return pEditView.get(); }
141 
142     void            CreateProgress( const OUString& rText, sal_uLong nRange );
143     void            DestroyProgress();
144 
145     void            ParagraphInsertedDeleted( sal_uLong nNewPara, bool bInserted );
146     void            DoDelayedSyntaxHighlight( sal_uLong nPara );
147 
148     void            CreateEditEngine();
149     void            SetScrollBarRanges();
150     void            InitScrollBars();
151 
152     void            ForceSyntaxTimeout();
153     void            SetSourceInBasic();
154 
155     bool            CanModify() { return ImpCanModify(); }
156 
157     void            ChangeFontColor( Color aColor );
158     void            UpdateSyntaxHighlighting ();
159 
160     bool            GetProcedureName(OUString const & rLine, OUString& rProcType, OUString& rProcName) const;
161 };
162 
163 
164 class BreakPointWindow : public vcl::Window
165 {
166 private:
167     ModulWindow&    rModulWindow;
168     long            nCurYOffset;
169     sal_uInt16      nMarkerPos;
170     BreakPointList  aBreakPointList;
171     bool            bErrorMarker;
172     std::unique_ptr<VclBuilder> mpUIBuilder;
173 
174     virtual void DataChanged(DataChangedEvent const & rDCEvt) override;
175 
176     void setBackgroundColor(Color aColor);
177 
178 protected:
179     virtual void    Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle&) override;
180     BreakPoint*     FindBreakPoint( const Point& rMousePos );
181     void ShowMarker(vcl::RenderContext& rRenderContext);
182     virtual void    MouseButtonDown( const MouseEvent& rMEvt ) override;
183     virtual void    Command( const CommandEvent& rCEvt ) override;
184 
185     bool            SyncYOffset();
186 
187 public:
188                     BreakPointWindow (vcl::Window* pParent, ModulWindow*);
189     virtual void    dispose() override;
190 
191     void            SetMarkerPos( sal_uInt16 nLine, bool bErrorMarker = false );
192     void            SetNoMarker ();
193 
194     void            DoScroll( long nVertScroll );
195     long&           GetCurYOffset()         { return nCurYOffset; }
196     BreakPointList& GetBreakPoints()        { return aBreakPointList; }
197 };
198 
199 
200 class WatchTreeListBox : public SvHeaderTabListBox
201 {
202     OUString aEditingRes;
203 
204 protected:
205     virtual bool    EditingEntry( SvTreeListEntry* pEntry, Selection& rSel  ) override;
206     virtual bool    EditedEntry( SvTreeListEntry* pEntry, const OUString& rNewText ) override;
207 
208     bool            ImplBasicEntryEdited( SvTreeListEntry* pEntry, const OUString& rResult );
209     SbxBase*        ImplGetSBXForEntry( SvTreeListEntry* pEntry, bool& rbArrayElement );
210 
211 public:
212     WatchTreeListBox( vcl::Window* pParent, WinBits nWinBits );
213     virtual ~WatchTreeListBox() override;
214     virtual void    dispose() override;
215 
216     void            RequestingChildren( SvTreeListEntry * pParent ) override;
217     void            UpdateWatches( bool bBasicStopped = false );
218 
219     using           SvTabListBox::SetTabs;
220     virtual void    SetTabs() override;
221 };
222 
223 
224 class WatchWindow : public DockingWindow
225 {
226 private:
227     OUString            aWatchStr;
228     VclPtr<ExtendedEdit>        aXEdit;
229     VclPtr<ImageButton>         aRemoveWatchButton;
230     VclPtr<WatchTreeListBox>    aTreeListBox;
231     VclPtr<HeaderBar>           aHeaderBar;
232 
233 protected:
234     virtual void    Resize() override;
235     virtual void    Paint( vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect ) override;
236 
237     DECL_LINK( ButtonHdl, Button *, void );
238     DECL_LINK(TreeListHdl, SvTreeListBox*, void);
239     DECL_LINK( implEndDragHdl, HeaderBar *, void );
240     DECL_LINK( EditAccHdl, Accelerator&, void );
241 
242 
243 public:
244     explicit WatchWindow (Layout* pParent);
245     virtual ~WatchWindow() override;
246     virtual void    dispose() override;
247 
248     void            AddWatch( const OUString& rVName );
249     void            RemoveSelectedWatch();
250     void            UpdateWatches( bool bBasicStopped );
251 };
252 
253 
254 class StackWindow : public DockingWindow
255 {
256 private:
257     VclPtr<SvTreeListBox>  aTreeListBox;
258     OUString               aStackStr;
259 
260 protected:
261     virtual void    Resize() override;
262     virtual void    Paint( vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect ) override;
263 
264 public:
265     explicit StackWindow (Layout* pParent);
266     virtual ~StackWindow() override;
267     virtual void    dispose() override;
268 
269     void            UpdateCalls();
270 };
271 
272 
273 class ComplexEditorWindow : public vcl::Window
274 {
275 private:
276     VclPtr<BreakPointWindow> aBrkWindow;
277     VclPtr<LineNumberWindow> aLineNumberWindow;
278     VclPtr<EditorWindow>     aEdtWindow;
279     VclPtr<ScrollBar>        aEWVScrollBar;
280 
281     virtual void DataChanged(DataChangedEvent const & rDCEvt) override;
282 
283 protected:
284     virtual void        Resize() override;
285     DECL_LINK( ScrollHdl, ScrollBar*, void );
286 
287 public:
288     explicit ComplexEditorWindow( ModulWindow* pParent );
289     virtual             ~ComplexEditorWindow() override;
290     virtual void        dispose() override;
291     BreakPointWindow&   GetBrkWindow()      { return *aBrkWindow.get(); }
292     LineNumberWindow&   GetLineNumberWindow() { return *aLineNumberWindow.get(); }
293     EditorWindow&       GetEdtWindow()      { return *aEdtWindow.get(); }
294     ScrollBar&          GetEWVScrollBar()   { return *aEWVScrollBar.get(); }
295 
296     void SetLineNumberDisplay(bool b);
297 };
298 
299 
300 class ModulWindow: public BaseWindow
301 {
302 private:
303     ModulWindowLayout&  m_rLayout;
304     StarBASICRef        m_xBasic;
305     short               m_nValid;
306     VclPtr<ComplexEditorWindow> m_aXEditorWindow;
307     BasicStatus         m_aStatus;
308     SbModuleRef         m_xModule;
309     OUString            m_sCurPath;
310     OUString            m_aModule;
311 
312     void                CheckCompileBasic();
313     void                BasicExecute();
314 
315     sal_Int32           FormatAndPrint( Printer* pPrinter, sal_Int32 nPage );
316     SbModuleRef const & XModule();
317 protected:
318     virtual void    Resize() override;
319     virtual void    GetFocus() override;
320     virtual void    Paint( vcl::RenderContext& rRenderContext, const tools::Rectangle& ) override;
321     virtual void    DoInit() override;
322     virtual void    DoScroll( ScrollBar* pCurScrollBar ) override;
323 
324 public:
325     ModulWindow( ModulWindowLayout* pParent, const ScriptDocument& rDocument, const OUString& aLibName, const OUString& aName, OUString const & aModule );
326 
327                     virtual ~ModulWindow() override;
328     virtual void    dispose() override;
329 
330     virtual void    ExecuteCommand (SfxRequest& rReq) override;
331     virtual void    ExecuteGlobal (SfxRequest& rReq) override;
332     virtual void    GetState( SfxItemSet& ) override;
333     virtual void    StoreData() override;
334     virtual void    UpdateData() override;
335     virtual bool    CanClose() override;
336     // return number of pages to be printed
337     virtual sal_Int32 countPages( Printer* pPrinter ) override;
338     // print page
339     virtual void printPage( sal_Int32 nPage, Printer* pPrinter ) override;
340     virtual OUString  GetTitle() override;
341     virtual EntryDescriptor CreateEntryDescriptor() override;
342     virtual bool    AllowUndo() override;
343     virtual void    SetReadOnly (bool bReadOnly) override;
344     virtual bool    IsReadOnly() override;
345 
346     StarBASIC*      GetBasic() { XModule(); return m_xBasic.get(); }
347 
348     SbModule*       GetSbModule() { return m_xModule.get(); }
349     void            SetSbModule( SbModule* pModule ) { m_xModule = pModule; }
350     OUString        GetSbModuleName();
351 
352     void            CompileBasic();
353     void            BasicRun();
354     void            BasicStepOver();
355     void            BasicStepInto();
356     void            BasicStepOut();
357     void            BasicStop();
358     void            BasicToggleBreakPoint();
359     void            BasicToggleBreakPointEnabled();
360     void            ManageBreakPoints();
361     void            UpdateBreakPoint( const BreakPoint& rBrk );
362     void            BasicAddWatch();
363 
364     bool            BasicErrorHdl( StarBASIC const * pBasic );
365     BasicDebugFlags BasicBreakHdl();
366     void            AssertValidEditEngine();
367 
368     void            LoadBasic();
369     void            SaveBasicSource();
370     void            ImportDialog();
371 
372     void            EditMacro( const OUString& rMacroName );
373 
374     void            ToggleBreakPoint( sal_uLong nLine );
375 
376     BasicStatus&    GetBasicStatus() { return m_aStatus; }
377 
378     virtual bool    IsModified () override;
379     virtual bool    IsPasteAllowed () override;
380 
381     void            ShowCursor( bool bOn );
382 
383     virtual SearchOptionFlags GetSearchOptions() override;
384     virtual sal_uInt16  StartSearchAndReplace (SvxSearchItem const&, bool bFromStart = false) override;
385 
386     EditorWindow&       GetEditorWindow()       { return m_aXEditorWindow->GetEdtWindow(); }
387     BreakPointWindow&   GetBreakPointWindow()   { return m_aXEditorWindow->GetBrkWindow(); }
388     LineNumberWindow&   GetLineNumberWindow()   { return m_aXEditorWindow->GetLineNumberWindow(); }
389     ScrollBar&          GetEditVScrollBar()     { return m_aXEditorWindow->GetEWVScrollBar(); }
390     ExtTextEngine*      GetEditEngine()         { return GetEditorWindow().GetEditEngine(); }
391     TextView*           GetEditView()           { return GetEditorWindow().GetEditView(); }
392     BreakPointList&     GetBreakPoints()        { return GetBreakPointWindow().GetBreakPoints(); }
393     ModulWindowLayout&  GetLayout ()            { return m_rLayout; }
394 
395     virtual void        BasicStarted() override;
396     virtual void        BasicStopped() override;
397 
398     virtual ::svl::IUndoManager*
399                         GetUndoManager() override;
400 
401     const OUString&         GetModule() const { return m_aModule; }
402     void                    SetModule( const OUString& aModule ) { m_aModule = aModule; }
403 
404     virtual void Activating () override;
405     virtual void Deactivating () override;
406 
407     virtual void OnNewDocument () override;
408     virtual char const* GetHid () const override;
409     virtual ItemType GetType () const override;
410     virtual bool HasActiveEditor () const override;
411 
412     void UpdateModule ();
413 };
414 
415 class ModulWindowLayout: public Layout
416 {
417 public:
418     ModulWindowLayout (vcl::Window* pParent, ObjectCatalog&);
419     virtual ~ModulWindowLayout() override;
420     virtual void dispose() override;
421 public:
422     // Layout:
423     virtual void Activating (BaseWindow&) override;
424     virtual void Deactivating () override;
425     virtual void GetState (SfxItemSet&, unsigned nWhich) override;
426     virtual void UpdateDebug (bool bBasicStopped) override;
427 public:
428     void BasicAddWatch (OUString const&);
429     void BasicRemoveWatch ();
430     Color GetBackgroundColor () const { return aSyntaxColors.GetBackgroundColor(); }
431     Color GetFontColor () const { return aSyntaxColors.GetFontColor(); }
432     Color GetSyntaxColor (TokenType eType) const { return aSyntaxColors.GetColor(eType); }
433 
434 protected:
435     // Window:
436     virtual void Paint (vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect) override;
437     // Layout:
438     virtual void OnFirstSize (long nWidth, long nHeight) override;
439 
440 private:
441     // main child window
442     VclPtr<ModulWindow> pChild;
443     // dockable windows
444     VclPtr<WatchWindow> aWatchWindow;
445     VclPtr<StackWindow> aStackWindow;
446     ObjectCatalog& rObjectCatalog;
447 
448     // SyntaxColors -- stores Basic syntax highlighting colors
449     class SyntaxColors : public utl::ConfigurationListener
450     {
451     public:
452         SyntaxColors ();
453         virtual ~SyntaxColors () override;
454     public:
455         void SetActiveEditor (EditorWindow* pEditor_) { pEditor = pEditor_; }
456     public:
457         Color GetBackgroundColor () const { return m_aBackgroundColor; };
458         Color GetFontColor () const { return m_aFontColor; }
459         Color GetColor (TokenType eType) const { return aColors[eType]; }
460 
461     private:
462         virtual void ConfigurationChanged (utl::ConfigurationBroadcaster*, ConfigurationHints) override;
463         void NewConfig (bool bFirst);
464 
465     private:
466         Color m_aBackgroundColor;
467         Color m_aFontColor;
468         // the color values (the indexes are TokenType, see comphelper/syntaxhighlight.hxx)
469         o3tl::enumarray<TokenType, Color> aColors;
470         // the configuration
471         svtools::ColorConfig aConfig;
472         // the active editor
473         VclPtr<EditorWindow> pEditor;
474 
475     } aSyntaxColors;
476 };
477 
478 class CodeCompleteListBox: public ListBox
479 {
480 friend class CodeCompleteWindow;
481 friend class EditorWindow;
482 private:
483     OUStringBuffer aFuncBuffer;
484     /* a buffer to build up function name when typing
485      * a function name, used for showing/hiding listbox values
486      * */
487     VclPtr<CodeCompleteWindow> pCodeCompleteWindow; // parent window
488 
489     void SetMatchingEntries(); // sets the visible entries based on aFuncBuffer variable
490     void HideAndRestoreFocus();
491     TextView* GetParentEditView();
492 
493 public:
494     explicit CodeCompleteListBox( CodeCompleteWindow* pPar );
495     virtual ~CodeCompleteListBox() override;
496     virtual void dispose() override;
497     void InsertSelectedEntry(); //insert the selected entry
498 
499     DECL_LINK(ImplDoubleClickHdl, ListBox&, void);
500     DECL_LINK(ImplSelectHdl, ListBox&, void);
501 
502 protected:
503     virtual void KeyInput( const KeyEvent& rKeyEvt ) override;
504 };
505 
506 class CodeCompleteWindow: public vcl::Window
507 {
508 friend class CodeCompleteListBox;
509 private:
510     VclPtr<EditorWindow> pParent; // parent window
511     TextSelection aTextSelection;
512     VclPtr<CodeCompleteListBox> pListBox;
513 
514     void InitListBox(); // initialize the ListBox
515 
516 public:
517     explicit CodeCompleteWindow( EditorWindow* pPar );
518     virtual ~CodeCompleteWindow() override;
519     virtual void dispose() override;
520 
521     void InsertEntry( const OUString& aStr );
522     void ClearListBox();
523     void SetTextSelection( const TextSelection& aSel );
524     const TextSelection& GetTextSelection() const { return aTextSelection;}
525     void ResizeAndPositionListBox();
526     void SelectFirstEntry(); //selects first entry in ListBox
527     void ClearAndHide();
528     /*
529      * clears if typed anything, then hides
530      * the window, clear internal variables
531      * */
532     CodeCompleteListBox* GetListBox(){return pListBox;}
533 };
534 
535 class UnoTypeCodeCompletetor
536 {
537 private:
538     css::uno::Reference< css::reflection::XIdlClass > xClass;
539     bool bCanComplete;
540 
541     bool CheckField( const OUString& sFieldName );
542     bool CheckMethod( const OUString& sMethName );
543 
544 public:
545     UnoTypeCodeCompletetor( const std::vector< OUString >& aVect, const OUString& sVarType );
546 
547     std::vector< OUString > GetXIdlClassMethods() const;
548     std::vector< OUString > GetXIdlClassFields() const;
549 
550     bool CanCodeComplete() const { return bCanComplete;}
551 };
552 
553 } // namespace basctl
554 
555 #endif // INCLUDED_BASCTL_SOURCE_BASICIDE_BASIDE2_HXX
556 
557 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
558