~efargaspro/+junk/codeblocks-16.01-release

« back to all changes in this revision

Viewing changes to src/plugins/contrib/cb_koders/cb_koders.cpp

  • Committer: damienlmoore at gmail
  • Date: 2016-02-02 02:43:22 UTC
  • Revision ID: damienlmoore@gmail.com-20160202024322-yql5qmtbwdyamdwd
Code::BlocksĀ 16.01

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of the Code::Blocks IDE and licensed under the GNU General Public License, version 3
 
3
 * http://www.gnu.org/licenses/gpl-3.0.html
 
4
 *
 
5
 * $Revision: 10245 $
 
6
 * $Id: cb_koders.cpp 10245 2015-05-02 14:45:07Z mortenmacfly $
 
7
 * $HeadURL: http://svn.code.sf.net/p/codeblocks/code/branches/release-16.xx/src/plugins/contrib/cb_koders/cb_koders.cpp $
 
8
 */
 
9
 
 
10
#include "sdk.h" // Code::Blocks SDK
 
11
#ifndef CB_PRECOMP
 
12
  #include <wx/intl.h>
 
13
  #include <wx/menu.h>
 
14
  #include <wx/string.h>
 
15
  #include <wx/utils.h> // wxLaunchDefaultBrowser
 
16
  #include "globals.h"
 
17
  #include "manager.h"
 
18
  #include "editormanager.h"
 
19
  #include "cbeditor.h"
 
20
#endif
 
21
#include "cbstyledtextctrl.h"
 
22
 
 
23
#include "cb_koders.h"
 
24
#include "kodersdialog.h"
 
25
 
 
26
// Register the plugin
 
27
namespace
 
28
{
 
29
    PluginRegistrant<CB_Koders> reg(_T("CB_Koders"));
 
30
};
 
31
 
 
32
const int idSearchKoders = wxNewId();
 
33
 
 
34
BEGIN_EVENT_TABLE(CB_Koders, cbToolPlugin)
 
35
  EVT_MENU(idSearchKoders, CB_Koders::OnSearchKoders)
 
36
END_EVENT_TABLE()
 
37
 
 
38
// constructor
 
39
CB_Koders::CB_Koders() :
 
40
  TheDialog(0)
 
41
{
 
42
}
 
43
 
 
44
// destructor
 
45
CB_Koders::~CB_Koders()
 
46
{
 
47
  if (TheDialog)
 
48
    TheDialog->Destroy();
 
49
}
 
50
 
 
51
void CB_Koders::OnAttach()
 
52
{
 
53
        // do whatever initialization you need for your plugin
 
54
        // NOTE: after this function, the inherited member variable
 
55
        // m_IsAttached will be TRUE...
 
56
        // You should check for it in other functions, because if it
 
57
        // is FALSE, it means that the application did *not* "load"
 
58
        // (see: does not need) this plugin...
 
59
}
 
60
 
 
61
void CB_Koders::OnRelease(bool /*appShutDown*/)
 
62
{
 
63
        // do de-initialization for your plugin
 
64
        // if appShutDown is false, the plugin is unloaded because Code::Blocks is being shut down,
 
65
        // which means you must not use any of the SDK Managers
 
66
        // NOTE: after this function, the inherited member variable
 
67
        // m_IsAttached will be FALSE...
 
68
}
 
69
 
 
70
int CB_Koders::Execute()
 
71
{
 
72
  if (IsReady() && TheDialog->ShowModal()==wxID_OK)
 
73
  {
 
74
    const wxString search = TheDialog->GetSearch();
 
75
    if (search.IsEmpty())
 
76
    {
 
77
      cbMessageBox(_("Cannot search for an empty expression."), _("Error"), wxICON_ERROR);
 
78
    }
 
79
    else
 
80
    {
 
81
      const wxString language = TheDialog->GetLanguage();
 
82
 
 
83
      wxString query;
 
84
      if ( language.IsEmpty() )
 
85
      {
 
86
        query.Printf(_("http://code.openhub.net/search?s=%s"), search.c_str());
 
87
      }
 
88
      else
 
89
      {
 
90
        query.Printf(_("http://code.openhub.net/search?s=%s&fl=%s"),
 
91
                     search.c_str(), language.c_str());
 
92
      }
 
93
 
 
94
      if (!wxLaunchDefaultBrowser(query))
 
95
        cbMessageBox(_("Could not launch the default browser of your system."), _("Error"), wxICON_ERROR);
 
96
    }
 
97
  }
 
98
 
 
99
        return 0;
 
100
}
 
101
 
 
102
void CB_Koders::BuildModuleMenu(const ModuleType type, wxMenu* menu, const FileTreeData* /*data*/)
 
103
{
 
104
        if (!menu || !IsAttached())
 
105
                return;
 
106
 
 
107
        if (type == mtEditorManager)
 
108
        {
 
109
                menu->AppendSeparator();
 
110
                menu->Append(idSearchKoders, _("Search at BlackDuck..."), _("Search keyword at BlackDuck webpage..."));
 
111
        }
 
112
}
 
113
 
 
114
bool CB_Koders::IsReady()
 
115
{
 
116
  if (!IsAttached())
 
117
    return false;
 
118
 
 
119
  if (!TheDialog)
 
120
    TheDialog = new KodersDialog(Manager::Get()->GetAppWindow());
 
121
 
 
122
  if (TheDialog)
 
123
    return true;
 
124
  else
 
125
    cbMessageBox(_("Could not initialise CB_Koders plugin."), _("Error"), wxICON_ERROR);
 
126
 
 
127
  return false;
 
128
}
 
129
 
 
130
void CB_Koders::OnSearchKoders(wxCommandEvent& /*event*/)
 
131
{
 
132
  if (IsReady())
 
133
  {
 
134
    wxString search(_T("")); // the word to search for (if any)
 
135
    cbEditor *ed = Manager::Get()->GetEditorManager()->GetBuiltinActiveEditor();
 
136
 
 
137
    if (ed)
 
138
    {
 
139
      // check if there is any text selected
 
140
      cbStyledTextCtrl *control = ed->GetControl();
 
141
      search = control->GetSelectedText();
 
142
 
 
143
      // if no selection, take the word under the cursor
 
144
      if (search.IsEmpty())
 
145
      {
 
146
        int origPos = control->GetCurrentPos();
 
147
        int start = control->WordStartPosition(origPos, true);
 
148
        int end = control->WordEndPosition(origPos, true);
 
149
        search = control->GetTextRange(start, end);
 
150
      }
 
151
    }
 
152
 
 
153
    TheDialog->SetSearch(search);
 
154
    Execute();
 
155
  }
 
156
}