From 2a85f7e9777186cbdce386c10994d8f8a64a5f43 Mon Sep 17 00:00:00 2001 From: Markus Gans Date: Sat, 12 Aug 2017 20:10:27 +0200 Subject: [PATCH] Some code improvements --- src/fapp.cpp | 30 ++++++++++++++++-------------- src/fapp.h | 10 +++++----- src/fdialog.cpp | 6 ++---- src/fmenu.cpp | 4 +++- src/fmenuitem.cpp | 2 +- src/fmessagebox.cpp | 42 +++++++++++++++++------------------------- src/fmessagebox.h | 2 +- src/fobject.cpp | 2 +- src/fscrollview.h | 4 ++-- src/fstring.cpp | 4 ++-- src/fterm.cpp | 22 ++++++++++++++++------ src/fterm.h | 4 ++-- src/fvterm.cpp | 6 ++---- src/fwindow.cpp | 12 ++++++------ src/fwindow.h | 6 +++--- 15 files changed, 79 insertions(+), 77 deletions(-) diff --git a/src/fapp.cpp b/src/fapp.cpp index 6a229be3..a08866ee 100644 --- a/src/fapp.cpp +++ b/src/fapp.cpp @@ -157,10 +157,9 @@ void FApplication::quit() } //---------------------------------------------------------------------- -bool FApplication::sendEvent(FObject* receiver, FEvent* event) +bool FApplication::sendEvent ( const FObject* receiver + , const FEvent* event ) { - FWidget* widget; - if ( quit_now || app_exit_loop ) return false; @@ -170,11 +169,12 @@ bool FApplication::sendEvent(FObject* receiver, FEvent* event) if ( ! receiver->isWidget() ) return false; - widget = static_cast(receiver); + const FWidget* r_widget = static_cast(receiver); + FWidget* widget = const_cast(r_widget); if ( modal_dialogs > 0 ) { - FWidget* window; + const FWidget* window; if ( widget->isWindowWidget() ) window = widget; else @@ -206,19 +206,21 @@ bool FApplication::sendEvent(FObject* receiver, FEvent* event) } } - // throw away mouse events to disabled widgets + // Throw away mouse events for disabled widgets if ( event->type() >= fc::MouseDown_Event && event->type() <= fc::MouseMove_Event && ! widget->isEnabled() ) return false; - // sends event event directly to receiver - FApplication* w = static_cast(widget); - return w->event(event); // access to a protected base class member + // For access to a protected base class member + FApplication* w = const_cast(static_cast(widget)); + // Sends event event directly to receiver + return w->event(const_cast(event)); } //---------------------------------------------------------------------- -void FApplication::queueEvent (FObject* receiver, FEvent* event) +void FApplication::queueEvent ( const FObject* receiver + , const FEvent* event ) { if ( ! receiver ) return; @@ -255,7 +257,7 @@ bool FApplication::eventInQueue() } //---------------------------------------------------------------------- -bool FApplication::removeQueuedEvent(FObject* receiver) +bool FApplication::removeQueuedEvent (const FObject* receiver) { bool retval; eventQueue::iterator iter; @@ -666,7 +668,7 @@ void FApplication::processKeyboardEvent() // windows keyboard accelerator if ( ! accpt ) { - FWidget* window = active_window; + const FWidget* window = active_window; if ( window ) accpt = processAccelerator (window); @@ -675,7 +677,7 @@ void FApplication::processKeyboardEvent() // global keyboard accelerator if ( ! accpt ) { - FWidget* root_widget = getRootWidget(); + const FWidget* root_widget = getRootWidget(); if ( root_widget ) processAccelerator (root_widget); @@ -1022,7 +1024,7 @@ bool FApplication::processDialogSwitchAccelerator() } //---------------------------------------------------------------------- -bool FApplication::processAccelerator (FWidget*& widget) +bool FApplication::processAccelerator (const FWidget*& widget) { bool accpt = false; diff --git a/src/fapp.h b/src/fapp.h index a5830e9f..d3762c23 100644 --- a/src/fapp.h +++ b/src/fapp.h @@ -79,16 +79,16 @@ class FApplication : public FWidget void exit_loop(); static void exit (int = 0); void quit(); - static bool sendEvent (FObject*, FEvent*); - static void queueEvent (FObject*, FEvent*); + static bool sendEvent (const FObject*, const FEvent*); + static void queueEvent (const FObject*, const FEvent*); static void sendQueuedEvents (); static bool eventInQueue(); - static bool removeQueuedEvent(FObject*); + static bool removeQueuedEvent(const FObject*); static void print_cmd_Options(); private: // Typedefs and Enumerations - typedef std::pair eventPair; + typedef std::pair eventPair; typedef std::deque eventQueue; enum btn_state @@ -133,7 +133,7 @@ class FApplication : public FWidget int linuxModifierKeyCorrection (int& key); #endif bool processDialogSwitchAccelerator(); - bool processAccelerator (FWidget*&); + bool processAccelerator (const FWidget*&); void getX11ButtonState (int); bool parseX11Mouse(); bool parseSGRMouse(); diff --git a/src/fdialog.cpp b/src/fdialog.cpp index 96b63651..1ed76499 100644 --- a/src/fdialog.cpp +++ b/src/fdialog.cpp @@ -984,9 +984,7 @@ void FDialog::draw() { if ( tooltip && ! getMoveSizeWidget() ) { - if ( tooltip ) - delete tooltip; - + delete tooltip; tooltip = 0; } @@ -1342,7 +1340,7 @@ void FDialog::openMenu() dialog_menu->setVisible(); drawTitleBar(); dialog_menu->show(); - dialog_menu->raiseWindow(dialog_menu); + dialog_menu->raiseWindow(); dialog_menu->redraw(); } } diff --git a/src/fmenu.cpp b/src/fmenu.cpp index e10680c4..8fead790 100644 --- a/src/fmenu.cpp +++ b/src/fmenu.cpp @@ -984,7 +984,9 @@ FMenu* FMenu::superMenuAt (int x, int y) else { FMenu* smenu = dynamic_cast(getSuperMenu()); - return smenu->superMenuAt(x,y); + + if ( smenu ) + return smenu->superMenuAt(x,y); } } diff --git a/src/fmenuitem.cpp b/src/fmenuitem.cpp index a91fe2c2..89a086c6 100644 --- a/src/fmenuitem.cpp +++ b/src/fmenuitem.cpp @@ -283,7 +283,7 @@ void FMenuItem::openMenu() setOpenMenu(dd_menu); dd_menu->setVisible(); dd_menu->show(); - dd_menu->raiseWindow(dd_menu); + dd_menu->raiseWindow(); dd_menu->redraw(); updateTerminal(); flush_out(); diff --git a/src/fmessagebox.cpp b/src/fmessagebox.cpp index 41029290..8fb44262 100644 --- a/src/fmessagebox.cpp +++ b/src/fmessagebox.cpp @@ -58,9 +58,9 @@ FMessageBox::FMessageBox (const FMessageBox& mbox) , button() { setTitlebarText (mbox.getTitlebarText()); - init ( *mbox.button_digit[0] - , *mbox.button_digit[1] - , *mbox.button_digit[2] ); + init ( mbox.button_digit[0] + , mbox.button_digit[1] + , mbox.button_digit[2] ); } //---------------------------------------------------------------------- @@ -92,10 +92,6 @@ FMessageBox::~FMessageBox() // destructor { for (uInt n=0; n < num_buttons; n++) delete button[n]; - - delete button_digit[2]; - delete button_digit[1]; - delete button_digit[0]; } @@ -112,10 +108,6 @@ FMessageBox& FMessageBox::operator = (const FMessageBox& mbox) for (uInt n=0; n < num_buttons; n++) delete button[n]; - delete button_digit[2]; - delete button_digit[1]; - delete button_digit[0]; - if ( mbox.getParentWidget() ) mbox.getParentWidget()->addChild (this); @@ -130,9 +122,9 @@ FMessageBox& FMessageBox::operator = (const FMessageBox& mbox) text_num_lines = mbox.text_num_lines; setTitlebarText (mbox.getTitlebarText()); - init ( *mbox.button_digit[0] - , *mbox.button_digit[1] - , *mbox.button_digit[2] ); + init ( mbox.button_digit[0] + , mbox.button_digit[1] + , mbox.button_digit[2] ); return *this; } @@ -160,10 +152,10 @@ void FMessageBox::setText (const FString& txt) calculateDimensions(); button[0]->setY(getHeight()-4, false); - if ( *button_digit[1] != 0 ) + if ( button_digit[1] != 0 ) button[1]->setY(getHeight()-4, false); - if ( *button_digit[2] != 0 ) + if ( button_digit[2] != 0 ) button[2]->setY(getHeight()-4, false); adjustButtons(); @@ -282,9 +274,9 @@ void FMessageBox::init (int button0, int button1, int button2) else num_buttons = 3; - button_digit[0] = new int(button0); - button_digit[1] = new int(button1); - button_digit[2] = new int(button2); + button_digit[0] = button0; + button_digit[1] = button1; + button_digit[2] = button2; button[0] = new FButton (this); button[0]->setText(button_text[button0]); @@ -314,33 +306,33 @@ void FMessageBox::init (int button0, int button1, int button2) resizeButtons(); adjustButtons(); - if ( *button_digit[0] != 0 ) + if ( button_digit[0] != 0 ) { button[0]->addCallback ( "clicked", F_METHOD_CALLBACK (this, &FMessageBox::cb_processClick), - static_cast(button_digit[0]) + static_cast(&button_digit[0]) ); } - if ( *button_digit[1] != 0 ) + if ( button_digit[1] != 0 ) { button[1]->addCallback ( "clicked", F_METHOD_CALLBACK (this, &FMessageBox::cb_processClick), - static_cast(button_digit[1]) + static_cast(&button_digit[1]) ); } - if ( *button_digit[2] != 0 ) + if ( button_digit[2] != 0 ) { button[2]->addCallback ( "clicked", F_METHOD_CALLBACK (this, &FMessageBox::cb_processClick), - static_cast(button_digit[2]) + static_cast(&button_digit[2]) ); } diff --git a/src/fmessagebox.h b/src/fmessagebox.h index aaf71245..62f84032 100644 --- a/src/fmessagebox.h +++ b/src/fmessagebox.h @@ -142,7 +142,7 @@ class FMessageBox : public FDialog short emphasis_color; uInt num_buttons; uInt text_num_lines; - int* button_digit[3]; + int button_digit[3]; FButton* button[3]; }; #pragma pack(pop) diff --git a/src/fobject.cpp b/src/fobject.cpp index 3b24c9ba..1787db2e 100644 --- a/src/fobject.cpp +++ b/src/fobject.cpp @@ -293,7 +293,7 @@ bool FObject::event (FEvent* ev) { if ( ev->type() == fc::Timer_Event ) { - onTimer ( static_cast(ev) ); + onTimer ( const_cast(static_cast(ev)) ); return true; } diff --git a/src/fscrollview.h b/src/fscrollview.h index baa38a9a..cbabad03 100644 --- a/src/fscrollview.h +++ b/src/fscrollview.h @@ -94,7 +94,7 @@ class FScrollView : public FWidget virtual void clearArea (int = ' '); void scrollToX (int); void scrollToY (int); - void scrollTo (FPoint); + void scrollTo (const FPoint&); void scrollTo (int, int); void scrollBy (int, int); virtual void draw(); @@ -219,7 +219,7 @@ inline bool FScrollView::isViewportPrint() { return ! use_own_print_area; } //---------------------------------------------------------------------- -inline void FScrollView::scrollTo (FPoint pos) +inline void FScrollView::scrollTo (const FPoint& pos) { scrollTo(pos.getX(), pos.getY()); } diff --git a/src/fstring.cpp b/src/fstring.cpp index 190b7f44..e01e6336 100644 --- a/src/fstring.cpp +++ b/src/fstring.cpp @@ -1281,7 +1281,7 @@ bool FString::operator == (const FString& s) const if ( ! s ) return false; - if ( (string && ! s.string ) || (! string && s.string) ) + if ( bool(string) != bool(s.string) ) return false; if ( ! (string || s.string) ) @@ -1338,7 +1338,7 @@ bool FString::operator != (const FString& s) const if ( ! s ) return true; - if ( (string && ! s.string ) || (! string && s.string) ) + if ( bool(string) != bool(s.string) ) return true; if ( ! (string || s.string) ) diff --git a/src/fterm.cpp b/src/fterm.cpp index 892cdf29..1a415a6a 100644 --- a/src/fterm.cpp +++ b/src/fterm.cpp @@ -1401,7 +1401,10 @@ void FTerm::setEncoding (std::string enc) case fc::PC: if ( xterm_terminal && utf8_console ) Fputchar = &FTerm::putchar_UTF8; - // fall through + else + Fputchar = &FTerm::putchar_ASCII; + break; + case fc::ASCII: default: Fputchar = &FTerm::putchar_ASCII; @@ -2650,15 +2653,22 @@ char* FTerm::parseSecDA (char*& current_termtype) char* new_termtype = current_termtype; bool sec_da_supported = false; - // The Linux console knows no Sec_DA if ( linux_terminal ) return new_termtype; - // secondary device attributes (SEC_DA) <- decTerminalID string - sec_da = new FString(getSecDA()); + try + { + // secondary device attributes (SEC_DA) <- decTerminalID string + sec_da = new FString(getSecDA()); + } - if ( sec_da && sec_da->getLength() > 5 ) + catch (const std::bad_alloc&) + { + return new_termtype; + } + + if ( sec_da->getLength() > 5 ) { uLong num_components; @@ -2776,7 +2786,7 @@ char* FTerm::parseSecDA (char*& current_termtype) force_vt100 = true; // this rxvt terminal support on utf-8 if ( std::strncmp(termtype, "rxvt-", 5) != 0 - || std::strncmp(termtype, "rxvt-cygwin-native", 5) == 0 ) + && std::strncmp(termtype, "rxvt-cygwin-native", 18) == 0 ) new_termtype = const_cast("rxvt-16color"); break; diff --git a/src/fterm.h b/src/fterm.h index 7c215765..972346aa 100644 --- a/src/fterm.h +++ b/src/fterm.h @@ -307,7 +307,7 @@ class FTerm static bool isGpmMouseEnabled(); #endif // F_HAVE_LIBGPM static FPoint& getMousePos(); - static void setMousePos (FPoint&); + static void setMousePos (const FPoint&); static void setMousePos (short, short); // Data Members @@ -701,7 +701,7 @@ inline FPoint& FTerm::getMousePos() { return *mouse; } //---------------------------------------------------------------------- -inline void FTerm::setMousePos (FPoint& m) +inline void FTerm::setMousePos (const FPoint& m) { *mouse = m; } //---------------------------------------------------------------------- diff --git a/src/fvterm.cpp b/src/fvterm.cpp index 84f1baa2..75bdcc92 100644 --- a/src/fvterm.cpp +++ b/src/fvterm.cpp @@ -490,8 +490,7 @@ int FVTerm::print (term_area* area, const FString& s) nc.no_changes = false; nc.printed = false; - if ( area - && area->cursor_x > 0 + if ( area->cursor_x > 0 && area->cursor_y > 0 && ax < area->width + area->right_shadow && ay < area->height + area->bottom_shadow ) @@ -627,8 +626,7 @@ int FVTerm::print (term_area* area, const std::vector& termString) char_data nc = *iter; // next character - if ( area - && area->cursor_x > 0 + if ( area->cursor_x > 0 && area->cursor_y > 0 && ax < area->width + area->right_shadow && ay < area->height + area->bottom_shadow ) diff --git a/src/fwindow.cpp b/src/fwindow.cpp index 636d5f4b..a9844f1d 100644 --- a/src/fwindow.cpp +++ b/src/fwindow.cpp @@ -136,10 +136,10 @@ void FWindow::setActiveWindow (FWindow* window) } //---------------------------------------------------------------------- -void FWindow::setWindowFocusWidget (FWidget* obj) +void FWindow::setWindowFocusWidget (const FWidget* obj) { // set focus widget of this window - win_focus_widget = obj; + win_focus_widget = const_cast(obj); } //---------------------------------------------------------------------- @@ -500,7 +500,7 @@ void FWindow::delWindow (FWidget* obj) } //---------------------------------------------------------------------- -FWindow* FWindow::getWindowWidget (FWidget* obj) +FWindow* FWindow::getWindowWidget (const FWidget* obj) { // returns the window object to the given widget obj FWidget* p_obj = obj->getParentWidget(); @@ -512,17 +512,17 @@ FWindow* FWindow::getWindowWidget (FWidget* obj) } if ( obj->isWindowWidget() ) - return static_cast(obj); + return const_cast(static_cast(obj)); else return 0; } //---------------------------------------------------------------------- -int FWindow::getWindowLayer (FWidget* obj) +int FWindow::getWindowLayer (const FWidget* obj) { // returns the window layer from the widget obj widgetList::iterator iter, end; - FWidget* window; + const FWidget* window; if ( ! window_list ) return -1; diff --git a/src/fwindow.h b/src/fwindow.h index c08e9037..1956ef85 100644 --- a/src/fwindow.h +++ b/src/fwindow.h @@ -63,8 +63,8 @@ class FWindow : public FWidget // Accessors const char* getClassName() const; - static FWindow* getWindowWidget (FWidget*); - static int getWindowLayer (FWidget*); + static FWindow* getWindowWidget (const FWidget*); + static int getWindowLayer (const FWidget*); static FWindow* getActiveWindow(); FWidget* getWindowFocusWidget() const; @@ -73,7 +73,7 @@ class FWindow : public FWidget bool setWindowWidget(); bool unsetWindowWidget(); static void setActiveWindow (FWindow*); - void setWindowFocusWidget (FWidget*); + void setWindowFocusWidget (const FWidget*); bool activateWindow (bool); bool activateWindow(); void unsetActiveWindow();